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">
<开发者_如何学JAVAdescription>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!
The way I do it is that I have an XSLT file that simply transforms my xml-data (in my case a protocol specification) to java source code. This XSLT-transformation can easily be done in an ANT-task which could be included in the build-chain in eclipse.
Perhaps there is a plugin for this particular task.
Some useful links:
- XSLT tutorial
- Ant: Performing XSLT Tranformations
- Using Ant to Auto-Build in Eclipse
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.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" />
<xsl:template match="/" priority="100">
public class <xsl:value-of select="name(node())" /> {
<xsl:apply-templates select="child::node()" />
}
</xsl:template>
<xsl:template match="/*/*">
public static String <xsl:value-of select="name()" />;
public static String get<xsl:value-of select="name()" /> {
return <xsl:value-of select=" name()" />;
}
public void static set<xsl:value-of select="name()" />(String value) {
<xsl:value-of select="name()" /> = value;
}
</xsl:template>
</xsl:stylesheet>
If you process with this example XML:
<?xml version="1.0" encoding="UTF-8"?>
<Human>
<EyeColor>brown</EyeColor>
<HairColor>brown</HairColor>
</Human>
You get something like: public class Human {
public static String EyeColor;
public static String getEyeColor {
return EyeColor;
}
public static void setEyeColor(String value) {
this.EyeColor = value;
}
public static String HairColor;
public static String getHairColor {
return HairColor;
}
public static void setHairColor(String value) {
this.HairColor = value;
}
}
Well Eclipse Modelling Framework (EMF) is meant for the application you are looking forward to. I assume you have a model and you want to convert it into code. Very specific hint I can give is JET (Java Emitter template) you can refer here for more details.
Also the newer framework XPand introduced by eclipse,
Revolving around the abstract syntax-development components are model-transformation tech- nologies. 1. model-to-text (Java Emitter Templates [JET] and Xpand) 2. model-to-model (QVT and ATL)
Here model refers to XML-XSLT / UML (Rational rose) model.
Take a look at XDoclet. Its an extensible open source code generation engine for Java.
EDIT: As bozho points out, XDoclet has mostly been replaced by annotations: Annotations vs. XDoclet
Do any java libraries use annotations for code generation?
You could have a look at the Eclipse modeling project's model to text components.
From XML to JAVA and vice Versa I highly recommend JAXB.
You can generate Java source code from Schema's using JAXB 2.0 or greater. Or you can generate Schemas from Java.
You can also easily make JAXB generate/consume JSON using Jettison.
You can also have JAXB generate POJO's in a Martin Fowler Fluent Style or a whole bunch of different ways using its plugin system.
EDIT based on your comments: Have XSLT generate your JAXB POJO:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" />
<xsl:template match="/" priority="100">
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name="Human")
public class <xsl:value-of select="name(node())" /> {
<xsl:apply-templates select="child::node()" />
}
</xsl:template>
<xsl:template match="/*/*">
private String <xsl:value-of select="name()" />;
public String get<xsl:value-of select="name()" /> {
return <xsl:value-of select=" name()" />;
}
public void set<xsl:value-of select="name()" />(String value) {
this.<xsl:value-of select="name()" /> = value;
}
</xsl:template>
</xsl:stylesheet>
If you process with this example XML:
<?xml version="1.0" encoding="UTF-8"?>
<Human>
<EyeColor>brown</EyeColor>
<HairColor>brown</HairColor>
</Human>
You get something like:
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name="Human")
public class Human {
private String EyeColor;
public String getEyeColor {
return EyeColor;
}
public void setEyeColor(String value) {
this.EyeColor = value;
}
private String HairColor;
public String getHairColor {
return HairColor;
}
public void setHairColor(String value) {
this.HairColor = value;
}
}
Yes you can do it using xml file.Create your own xml file template then using preferences-java-code template-import choose that file and you can use that template.
精彩评论