I am using org.simpleframework.xml (http://simple.sourceforge.net/) to serialize Java Objects to XML.
What I would like to add is to add a comments area in the resulting XML, based on Annotations in the Java object.
So for example I would like to write some Java Object like:
@Root(name = "myclass")
public class MyClass {
@Element(required=true)
@Version(revision=1.1)
@Comment(text=This Element is new since, version 1.1, it is a MD5 encrypted value)
private String activateHash;
}
And the resulting xml would look like:
<myclass version="1.1">
<!-- This Element is new since, version 1.1, it is a MD5 encrypted value -->
<activateHash>129831923131s3jjs3s3jjk93jk1</activateHash>
</myclass>
There is an example in their docs on howto write a Visitor that will write a comments in the xml:
http://simple.sourceforge.net/download/stream/doc/tutorial/tutorial.php#intercept
However: How can I attach a Visitor to a Strategy at all?
And further the Visitor concept of simpleframework does not allow access to the raw parsing class.
In the Visitor there is only a method to overwrite:
public void write(Type type, NodeMap<OutputNode> node) { ... }
=> OutputNode does not give me a chance to read the Annotation of the Element that I am parsing. So how should one access the Annotations of the attribute.
Thanks!
Sebastian
Update as of 2012-11-05:
Answer by the author of org.simpleframework.xml:
This works
https://simple.svn.sourceforge.net/svnroot/simple/trunk/download/stream/src/test/java/org/simpleframework/xml/strategy/CommentTest.java
Update as of 2012-11-01 20:16
this is the workaround that seems to get the desired effect – the necessary FieldHelper is described in (Get the value of a field, given the hierarchical path)
Here is how far I got with this. Unfortunately it does not work as expected. I have written an E-Mail to the author of the Simpleframwork for XML.
I declared the Comment annotation like this:
which is then usable like this:
adding the Visitor was possible like this: