Does anyone know what approach one can take to automatically generate Java source code, from for example an xml or json file, in eclipse?
One great example of what I am thinking of doing is what Google Android sdk does: they have an R class generated automatically from the resources.
Every time a resource file is saved in Eclipse R class is automatically regenerated.
UPDATE: Example:
In the text (xml or json file) I have the following:
<tags>
<tag id="ALPHA">
<description>The first alpha tag.</description>
<value>231232</value>
</tag>
<tag id="BETA">
<description>This is the beta tag.</description>
<value>231232</value>
</tag>
Then in my generated java class, say R I would have something like:
R.tags.ids.ALPHA //refers to an enum value for example
R.tags.values.ALPHA //refers to final int with avlue 231232
R.tags.descriptions.ALPHA //refers to the String with description
Thanks!
I’m adding another answer based on your comments and also because I don’t really recommend doing this outside of Google Android Resource SDK.
Google is basically using a hierarchy of static classes (singletons) for their resources.
You need to make your XSLT generate static member variables instead of getters and setters.
I basically took my old answer and changed it to static for all member variables.
You have to be very careful doing this because I have seen so many bugs with incorrect use of the “static” modifier.
If you process with this example XML:
You get something like:
public class Human {