Details
-
Improvement
-
Status: Resolved
-
Minor
-
Resolution: Fixed
-
1.0 Alpha
-
None
-
None
-
Operating System: All
Platform: All
-
29833
Description
Hi All,
This is the doc I wrote about using inner classes / non-public classes. The
inners classes approach is not so important but the other one I believe is of a
great value because the Betwixt engine quietly ignores the attributes as it
falls in the IllegalAccessException and I believe it will help a lot if it's
well documented.
If some native english speaker could review I'd thank very much as I'm from
Brazil and my english is far from perfect.
--------------------------------------------------------------------------------
WORKING WITH INNER CLASSES:
When using a public inner class, static or not, the name of the element obey
the Inner Classes naming semantics (using $).
The example bellow show this behaviour:
public class BetWixtWriter {
public class TallTreeBean {
private float heightOfTree;
public TallTreeBean(float height)
{ setHeightOfTree(height); }public float getHeightOfTree()
{ return heightOfTree; }
public void setHeightOfTree(float heightOfTree)
{ this.heightOfTree = heightOfTree; }}
public static final void main(String args[]) throws Exception
{ // create write and set basic properties BeanWriter writer = new BeanWriter(); writer.getXMLIntrospector().setAttributesForPrimitives (true); writer.enablePrettyPrint(); writer.setWriteIDs(false); // set a custom name mapper for attributes writer.getXMLIntrospector().setAttributeNameMapper(new HyphenatedNameMapper()); // set a custom name mapper for elements writer.getXMLIntrospector().setElementNameMapper(new DecapitalizeNameMapper()); // write out the bean writer.write(new BetWixtWriter().new TallTreeBean (15.1f)); System.out.println(""); }}
Now, the application should print out an xml fragment which looks like:
<betWixtWriter$TallTreeBean height-of-tree="15.1"/>
--------------------------------------------------------------------------------
WORKING WITH NON-PUBLIC CLASSES:
When working with a non-public inner/normal class, it's attributes are not
acessible even if the methods are public:
public class BetWixtWriter {
public static final void main(String args[]) throws Exception
{ // create write and set basic properties BeanWriter writer = new BeanWriter(); writer.getXMLIntrospector().setAttributesForPrimitives(true); writer.enablePrettyPrint(); writer.setWriteIDs(false); // set a custom name mapper for attributes writer.getXMLIntrospector().setAttributeNameMapper(new HyphenatedNameMapper()); // set a custom name mapper for elements writer.getXMLIntrospector().setElementNameMapper(new DecapitalizeNameMapper()); // write out the bean TallTreeBean ttb = new TallTreeBean(15.1f); writer.write(ttb); System.out.println(""); }}
class TallTreeBean {
private float heightOfTree;
public TallTreeBean(float height)
{ setHeightOfTree(height); }public float getHeightOfTree()
{ return heightOfTree; }
public void setHeightOfTree(float heightOfTree)
{ this.heightOfTree = heightOfTree; }}
Now, the application should print out an xml fragment which looks like the
following, so only the name of the class is present, without the attribute:
<tallTreeBean/>
So, be aware when using non-public JavaBeans classes.