在Eclipse中从XML生成Java代码

我正在开发一个项目,它将有几个彼此非常相似的Java类,并且我想从XML文件生成。 我希望能够做的是更改Eclipse构建过程以执行以下操作:

  • 编译代码生成器
  • 运行代码生成器,将XML转换为Java
  • 编译项目的其余部分

我可以手动完成所有操作,但我更愿意让Eclipse为我做这一切。

我希望能够获取如下所示的源XML文件:

           

并让它给我类似于以下内容(在适当的单独文件中):

 public class Date extends Command { public ResponseCode execute() { Server srv = getServer(); srv.send("DATE"); return srv.getResponse(); } } public class Group extends Command { public ResponseCode execute() { Server srv = getServer(); if (srv.hasCapability(Capabilities.READER) == false) { Log.debug("Attempting non-available capability: READER"); } String groupname = getArgument("groupname"); if (groupname == null) { throw new InvalidArgumentException("Require groupname"); } String command = "GROUP"; if (groupname != null) command += " " + groupname; srv.send(command); return srv.getResponse(); } } public class Article extends Command { public ResponseCode execute() { Server srv = getServer(); if (srv.hasCapability(Capabilities.READER) == false) { Log.debug("Attempting non-available capability: READER"); } String messageId = getArgument("messageId"); String command = "ARTICLE"; if (messageId != null) command += " " + messageId; srv.send(command); return srv.getResponse(); } } 

这正是模型到文本(M2T)项目中的JET组件的用途。 实际上,您甚至可以使用JET创建项目,.classpath和任何其他文件。

Jet模板如下。 请注意,这些模板必须完全按照所示命名。

/templates/main.jet

 <%@taglib prefix="ws" id="org.eclipse.jet.workspaceTags" %> <%-- Main entry point for com.lacqui.command.xform --%> <%-- Let c:iterate tags set the XPath context object. For 100% compatibility with JET 0.9.x or earlier, remove this statement --%>   --- traverse input model, performing calculations and storing --- the results as model annotations via c:set tag com.lacqui.commands com.lacqui.commands   - Derive the class name from the name of the command Command   false    --- traverse annotated model, performing text generation actions --- such as ws:file, ws:folder and ws:project       

/templates/classpath.jet

       

/templates/project.jet

         org.eclipse.jdt.core.javabuilder      org.eclipse.jdt.core.javanature   

/templates/class.jet

 package ; public class  extends Command { public ResponseCode execute() { Server srv = getServer();  if (srv.hasCapability(Capabilities.) == false) { Log.debug("Attempting non-available capability: "); }   String  = getArgument("");  if ( == null) { throw new InvalidArgumentException("Require "); }   String command = "GROUP";  if ( != null) command += " - " + ;  srv.send(command); return srv.getResponse(); } } 

并使用此模型:

              

给出一个名为com.lacqui.commands的完整java项目,其中包含三个java文件:

 package com.lacqui.commands; public class ArticleCommand extends Command { public ResponseCode execute() { Server srv = getServer(); if (srv.hasCapability(Capabilities.READER) == false) { Log.debug("Attempting non-available capability: READER"); } String article-id = getArgument("article-id"); if (article-id == null) { throw new InvalidArgumentException("Require article-id"); } String message-id = getArgument("message-id"); String command = "GROUP"; if (article-id != null) command += " -article-id " + article-id; if (message-id != null) command += " -message-id " + message-id; srv.send(command); return srv.getResponse(); } } 

和这个:

 package com.lacqui.commands; public class GroupCommand extends Command { public ResponseCode execute() { Server srv = getServer(); if (srv.hasCapability(Capabilities.LOGGER) == false) { Log.debug("Attempting non-available capability: LOGGER"); } if (srv.hasCapability(Capabilities.AUTHENTICATOR) == false) { Log.debug("Attempting non-available capability: AUTHENTICATOR"); } String groupname = getArgument("groupname"); if (groupname == null) { throw new InvalidArgumentException("Require groupname"); } String command = "GROUP"; if (groupname != null) command += " -groupname " + groupname; srv.send(command); return srv.getResponse(); } } 

您可能想看看JaxB。

如果你想快速浏览一下,Lars Vogel做了一个简单的教程。
JAXB教程

这里还有一个xml unmarshaling的基准测试。
Java中的XML解组基准:JAXB vs STax vs Woodstox