Discussion:
[FreeMarker-user] accessing JAXB generated properties of a bean (accessing static inner class)
Walker, Robert
2012-03-30 03:55:30 UTC
Permalink
Hi all, I was turned onto freemarker today and really impressed with it, and wanted to use it to generate spreadsheetML
(xml that Excel will open as if it were a xlsx file)

I am having problems accessing properties buried a few layers deep.

Basically, I have a OrganisationInfo class which contains an OrganisationInfo. NetworkList object,
And that networkList property contains a List of Network objects, from which I need to get
property values.

I am wondering if JAXB has generated something freemarker can't be used to access in it's template.

I want to try something like

cfg.setObjectWrapper( new DefaultObjectWrapper() );
SimpleHash root = new SimpleHash();
// expose JAXB generated java object
root.put("organisationInfo", organisationInfo);

then my template has accessing syntax like

<h1> ${organisationInfo.networkList[0].tadigCode} </h1>

Is this possible with freemarker? Is there an example of this anywhere?
I get various errors Expected hash. organisationInfo.networkList[0] evaluated instead to freemarker.template.SimpleScalar.

Thanks all




I have JAXB generated classes like this

OrganisationInfo.java
====================@XmlRootElement(name = "OrganisationInfo")
public class OrganisationInfo
{
@XmlElement(name = "NetworkList", required = true)
protected OrganisationInfo.NetworkList networkList;

.
.
.

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {"network"})
public static class NetworkList
{
@XmlElement(name = "Network", required = true)
protected List<Network> network;
.
.
}
}


Network.java
===========@XmlRootElement(name = "Network")
public class Network
{
@XmlElement(name = "TADIGCode", required = true)
protected String tadigCode;

.
.

}
Daniel Dekany
2012-03-30 11:27:32 UTC
Permalink
How the templates sees the objects depends on the ObjectWrapper. As I
see you are using the DefaultObjectWrapper, which means that when you
write someExp.bar in a template, that it does one of these:

- someExp is instanceof Map, in which case it calls someExp.get("bar")
in Java

- someExp is instanceof org.w3c.dom.Node, in which case it calls
something like someExp.getElementsByTagNameNS(defaultNS, "bar") in
Java

- Some other cases exist that are not relevant for you...

- Otherwise it calls someExp.getBar() in Java (or whatever the
BeanInfo sais the reader method is)

and if you have someExp[numericalIndex] then it will try to do
someExp.get(numericalIndex), the List method. (Unless it's also a
Map... I can't remember which one had priority in that case.)

So keeping these in mind, you thing your code should work? I don't
know... I don't see much from the source code excerpts given. Like how
a NetworkList ended up being a scalar (i.e., a string), I don't know.

Why is it relevant that the classes are generated with JAXB? I mean,
it should be clear that to use the XML-wrapper, the objects had to
implement org.w3c.dom.Node.
--
Best regards,
Daniel Dekany
Post by Walker, Robert
Hi all, I was turned onto freemarker today and really impressed
with it, and wanted to use it to generate spreadsheetML
(xml that Excel will open as if it were a xlsx file)
I am having problems accessing properties buried a few layers deep.
Basically, I have a OrganisationInfo class which contains an
OrganisationInfo. NetworkList object,
And that networkList property contains a List of Network objects, from which I need to get
property values.
I am wondering if JAXB has generated something freemarker can’t be used to access in it’s template.
I want to try something like
cfg.setObjectWrapper( new DefaultObjectWrapper() );
SimpleHash root = new SimpleHash();
// expose JAXB generated java object
root.put("organisationInfo", organisationInfo);
then my template has accessing syntax like
<h1> ${organisationInfo.networkList[0].tadigCode} </h1>
Is this possible with freemarker? Is there an example of this anywhere?
I get various errors Expected hash. organisationInfo.networkList[0]
evaluated instead to freemarker.template.SimpleScalar.
Thanks all
I have JAXB generated classes like this
OrganisationInfo.java
=====================
@XmlRootElement(name = "OrganisationInfo")
public class OrganisationInfo
{
@XmlElement(name = "NetworkList", required = true)
protected OrganisationInfo.NetworkList networkList;
.
.
.
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {"network"})
public static class NetworkList
{
@XmlElement(name = "Network", required = true)
protected List<Network> network;
.
.
}
}
Network.java
============
@XmlRootElement(name = "Network")
public class Network
{
@XmlElement(name = "TADIGCode", required = true)
protected String tadigCode;
.
.
}
Angelo zerr
2012-03-30 12:14:41 UTC
Permalink
Hi Robert,

" wanted to use it to generate spreadsheetML(xml that Excel will open as
if it were a xlsx file)"

It seems that you wish do the same thing than our
XDocReport<http://code.google.com/p/xdocreport/>project.
Today we have implemented well docx+odt (and a few pptx).
We could start to implement xlsx if you wish and work together?

Jus a question : you wish generate a xlsx from scratch or design your xlsx
with MS Excel and set Freemarker directive (it's the goal of XDocReport).

Regards Angelo
Post by Daniel Dekany
How the templates sees the objects depends on the ObjectWrapper. As I
see you are using the DefaultObjectWrapper, which means that when you
- someExp is instanceof Map, in which case it calls someExp.get("bar")
in Java
- someExp is instanceof org.w3c.dom.Node, in which case it calls
something like someExp.getElementsByTagNameNS(defaultNS, "bar") in
Java
- Some other cases exist that are not relevant for you...
- Otherwise it calls someExp.getBar() in Java (or whatever the
BeanInfo sais the reader method is)
and if you have someExp[numericalIndex] then it will try to do
someExp.get(numericalIndex), the List method. (Unless it's also a
Map... I can't remember which one had priority in that case.)
So keeping these in mind, you thing your code should work? I don't
know... I don't see much from the source code excerpts given. Like how
a NetworkList ended up being a scalar (i.e., a string), I don't know.
Why is it relevant that the classes are generated with JAXB? I mean,
it should be clear that to use the XML-wrapper, the objects had to
implement org.w3c.dom.Node.
--
Best regards,
Daniel Dekany
Post by Walker, Robert
Hi all, I was turned onto freemarker today and really impressed
with it, and wanted to use it to generate spreadsheetML
(xml that Excel will open as if it were a xlsx file)
I am having problems accessing properties buried a few layers deep.
Basically, I have a OrganisationInfo class which contains an
OrganisationInfo. NetworkList object,
And that networkList property contains a List of Network objects, from
which I need to get
Post by Walker, Robert
property values.
I am wondering if JAXB has generated something freemarker can’t be used
to access in it’s template.
Post by Walker, Robert
I want to try something like
cfg.setObjectWrapper( new DefaultObjectWrapper() );
SimpleHash root = new SimpleHash();
// expose JAXB generated java object
root.put("organisationInfo", organisationInfo);
then my template has accessing syntax like
<h1> ${organisationInfo.networkList[0].tadigCode} </h1>
Is this possible with freemarker? Is there an example of this anywhere?
I get various errors Expected hash. organisationInfo.networkList[0]
evaluated instead to freemarker.template.SimpleScalar.
Thanks all
I have JAXB generated classes like this
OrganisationInfo.java
=====================
@XmlRootElement(name = "OrganisationInfo")
public class OrganisationInfo
{
@XmlElement(name = "NetworkList", required = true)
protected OrganisationInfo.NetworkList networkList;
.
.
.
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {"network"})
public static class NetworkList
{
@XmlElement(name = "Network", required = true)
protected List<Network> network;
.
.
}
}
Network.java
============
@XmlRootElement(name = "Network")
public class Network
{
@XmlElement(name = "TADIGCode", required = true)
protected String tadigCode;
.
.
}
------------------------------------------------------------------------------
Try Windows Azure free for 90 days Click Here
http://p.sf.net/sfu/sfd2d-msazure
_______________________________________________
FreeMarker-user mailing list
https://lists.sourceforge.net/lists/listinfo/freemarker-user
Walker, Robert
2012-03-30 14:00:13 UTC
Permalink
I want to generate a xml file that can be opened by excel, and that format is pretty
Well known so I can use xslt to output the correct spreadsheetML markup, if I can get
Freemarker to read my java beans



From: Angelo zerr [mailto:***@gmail.com]
Sent: Friday, March 30, 2012 8:15 AM
To: Daniel Dekany; FreeMarker-user
Subject: Re: [FreeMarker-user] accessing JAXB generated properties of a bean (accessing static inner class)

Hi Robert,

" wanted to use it to generate spreadsheetML(xml that Excel will open as if it were a xlsx file)"

It seems that you wish do the same thing than our XDocReport<http://code.google.com/p/xdocreport/> project.
Today we have implemented well docx+odt (and a few pptx).
We could start to implement xlsx if you wish and work together?

Jus a question : you wish generate a xlsx from scratch or design your xlsx with MS Excel and set Freemarker directive (it's the goal of XDocReport).

Regards Angelo
Le 30 mars 2012 13:27, Daniel Dekany <***@freemail.hu<mailto:***@freemail.hu>> a écrit :
How the templates sees the objects depends on the ObjectWrapper. As I
see you are using the DefaultObjectWrapper, which means that when you
write someExp.bar in a template, that it does one of these:

- someExp is instanceof Map, in which case it calls someExp.get("bar")
in Java

- someExp is instanceof org.w3c.dom.Node, in which case it calls
something like someExp.getElementsByTagNameNS(defaultNS, "bar") in
Java

- Some other cases exist that are not relevant for you...

- Otherwise it calls someExp.getBar() in Java (or whatever the
BeanInfo sais the reader method is)

and if you have someExp[numericalIndex] then it will try to do
someExp.get(numericalIndex), the List method. (Unless it's also a
Map... I can't remember which one had priority in that case.)

So keeping these in mind, you thing your code should work? I don't
know... I don't see much from the source code excerpts given. Like how
a NetworkList ended up being a scalar (i.e., a string), I don't know.

Why is it relevant that the classes are generated with JAXB? I mean,
it should be clear that to use the XML-wrapper, the objects had to
implement org.w3c.dom.Node.

--
Best regards,
Daniel Dekany
Post by Walker, Robert
Hi all, I was turned onto freemarker today and really impressed
with it, and wanted to use it to generate spreadsheetML
(xml that Excel will open as if it were a xlsx file)
I am having problems accessing properties buried a few layers deep.
Basically, I have a OrganisationInfo class which contains an
OrganisationInfo. NetworkList object,
And that networkList property contains a List of Network objects, from which I need to get
property values.
I am wondering if JAXB has generated something freemarker can't be used to access in it's template.
I want to try something like
cfg.setObjectWrapper( new DefaultObjectWrapper() );
SimpleHash root = new SimpleHash();
// expose JAXB generated java object
root.put("organisationInfo", organisationInfo);
then my template has accessing syntax like
<h1> ${organisationInfo.networkList[0].tadigCode} </h1>
Is this possible with freemarker? Is there an example of this anywhere?
I get various errors Expected hash. organisationInfo.networkList[0]
evaluated instead to freemarker.template.SimpleScalar.
Thanks all
I have JAXB generated classes like this
OrganisationInfo.java
public class OrganisationInfo
{
@XmlElement(name = "NetworkList", required = true)
protected OrganisationInfo.NetworkList networkList;
.
.
.
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {"network"})
public static class NetworkList
{
@XmlElement(name = "Network", required = true)
protected List<Network> network;
.
.
}
}
Network.java
public class Network
{
@XmlElement(name = "TADIGCode", required = true)
protected String tadigCode;
.
.
}
Walker, Robert
2012-03-30 14:28:41 UTC
Permalink
Thanks Daniel
Post by Daniel Dekany
you thing your code should work?
Yes, I thought it should work, but I am new to this so I was getting hung up on syntax



You set me on the right path though, I wasn't exactly at my list yet when I tried to use indexing [0]



I should have used



${organisationInfo.networkList.network[0].networkType}
Post by Daniel Dekany
Why is it relevant that the classes are generated with JAXB?
JAXB generates a lot of static inner classes and I thought that was tripping me up,

(As opposed to conventional non-inner classes, and not static either)



I really wish there was a freemarker book, this templating is really powerful

And I'd love to learn more about it

















-----Original Message-----
From: Daniel Dekany [mailto:***@freemail.hu]
Sent: Friday, March 30, 2012 7:28 AM
To: Walker, Robert
Cc: 'freemarker-***@lists.sourceforge.net'
Subject: Re: [FreeMarker-user] accessing JAXB generated properties of a bean (accessing static inner class)



How the templates sees the objects depends on the ObjectWrapper. As I

see you are using the DefaultObjectWrapper, which means that when you

write someExp.bar in a template, that it does one of these:



- someExp is instanceof Map, in which case it calls someExp.get("bar")

in Java



- someExp is instanceof org.w3c.dom.Node, in which case it calls

something like someExp.getElementsByTagNameNS(defaultNS, "bar") in

Java



- Some other cases exist that are not relevant for you...



- Otherwise it calls someExp.getBar() in Java (or whatever the

BeanInfo sais the reader method is)



and if you have someExp[numericalIndex] then it will try to do

someExp.get(numericalIndex), the List method. (Unless it's also a

Map... I can't remember which one had priority in that case.)



So keeping these in mind, you thing your code should work? I don't

know... I don't see much from the source code excerpts given. Like how

a NetworkList ended up being a scalar (i.e., a string), I don't know.



Why is it relevant that the classes are generated with JAXB? I mean,

it should be clear that to use the XML-wrapper, the objects had to

implement org.w3c.dom.Node.



--

Best regards,

Daniel Dekany
Post by Daniel Dekany
Hi all, I was turned onto freemarker today and really impressed
with it, and wanted to use it to generate spreadsheetML
(xml that Excel will open as if it were a xlsx file)
I am having problems accessing properties buried a few layers deep.
Basically, I have a OrganisationInfo class which contains an
OrganisationInfo. NetworkList object,
And that networkList property contains a List of Network objects, from which I need to get
property values.
I am wondering if JAXB has generated something freemarker can't be used to access in it's template.
I want to try something like
cfg.setObjectWrapper( new DefaultObjectWrapper() );
SimpleHash root = new SimpleHash();
// expose JAXB generated java object
root.put("organisationInfo", organisationInfo);
then my template has accessing syntax like
<h1> ${organisationInfo.networkList[0].tadigCode} </h1>
Is this possible with freemarker? Is there an example of this anywhere?
I get various errors Expected hash. organisationInfo.networkList[0]
evaluated instead to freemarker.template.SimpleScalar.
Thanks all
I have JAXB generated classes like this
OrganisationInfo.java
====================
@XmlRootElement(name = "OrganisationInfo")
public class OrganisationInfo
{
@XmlElement(name = "NetworkList", required = true)
protected OrganisationInfo.NetworkList networkList;
.
.
.
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {"network"})
public static class NetworkList
{
@XmlElement(name = "Network", required = true)
protected List<Network> network;
.
.
}
}
Network.java
===========
@XmlRootElement(name = "Network")
public class Network
{
@XmlElement(name = "TADIGCode", required = true)
protected String tadigCode;
.
.
}
------------------------------------------------------------------------------

This SF email is sponsosred by:

Try Windows Azure free for 90 days Click Here

http://p.sf.net/sfu/sfd2d-msazure

_______________________________________________

FreeMarker-user mailing list

FreeMarker-***@lists.sourceforge.net

https://lists.sourceforge.net/lists/listinfo/freemarker-user

Loading...