我正在一个项目中,该项目将具有几个彼此非常相似的Java类,并且我想从XML文件生成该类。我想要做的是将Eclipse构建过程更改为执行以下操作:
我可以手动完成所有操作,但我希望能够让Eclipse为我完成所有操作。
例
我希望能够获取如下所示的源XML文件:
<command-list>
<command name="DATE" />
<command name="GROUP">
<capability "READER" />
<argument "groupname" />
</command>
<command name="ARTICLE">
<capability "READER" />
<argument "message-id" optional="true" />
</command>
</command-list>
并给我类似以下内容(适当时放在单独的文件中):
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
--%>
<c:setVariable var="org.eclipse.jet.taglib.control.iterateSetsContext" select="true()"/>
<c:setVariable select="/command-list" var="command-list" />
--- traverse input model, performing calculations and storing
--- the results as model annotations via c:set tag
<c:set select="$command-list" name="project">com.lacqui.commands</c:set>
<c:set select="$command-list" name="commandPkg">com.lacqui.commands</c:set>
<c:set select="$command-list" name="commandDir"><c:get select="translate($command-list/@commandPkg,'.','/')" /></c:set>
<c:iterate select="$command-list/command" var="command" >
- Derive the class name from the name of the command
<c:set select="$command" name="classname"><c:get select="camelCase($command/@name)" />Command</c:set>
<c:iterate select="$command/argument" var="argument">
<c:if test="not($argument/@optional)">
<c:set select="$argument" name="optional">false</c:set>
</c:if>
</c:iterate>
</c:iterate>
--- traverse annotated model, performing text generation actions
--- such as ws:file, ws:folder and ws:project
<ws:project name="{$command-list/@project}" />
<ws:file template="templates/project.jet" path="{$command-list/@project}/.project" />
<ws:file template="templates/classpath.jet" path="{$command-list/@project}/.classpath"/>
<c:iterate select="$command-list/command" var="command" >
<ws:file template="templates/class.jet" path="{$command-list/@project}/src/{$command-list/@commandDir}/{$command/@classname}.java" replace="true"/>
</c:iterate>
/templates/classpath.jet
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="output" path="bin"/>
</classpath>
/templates/project.jet
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name><c:get select="$command-list/@project" /></name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
/templates/class.jet
package <c:get select="$command-list/@commandPkg" />;
public class <c:get select="$command/@classname" /> extends Command {
public ResponseCode execute() {
Server srv = getServer();
<c:iterate select="$command/capability" var="capability">
if (srv.hasCapability(Capabilities.<c:get select="$capability/@name"/>) == false) {
Log.debug("Attempting non-available capability: <c:get select="$capability/@name"/>");
}
</c:iterate>
<c:iterate select="$command/argument" var="argument">
String <c:get select="$argument/@name"/> = getArgument("<c:get select="$argument/@name"/>");
<c:if test="$argument/@optional = 'false'" >
if (<c:get select="$argument/@name"/> == null) {
throw new InvalidArgumentException("Require <c:get select="$argument/@name"/>");
}
</c:if>
</c:iterate>
String command = "GROUP";
<c:iterate select="$command/argument" var="argument">
if (<c:get select="$argument/@name"/> != null) command += " -<c:get select="$argument/@name"/> " + <c:get select="$argument/@name"/>;
</c:iterate>
srv.send(command);
return srv.getResponse();
}
}
并使用此模型:
<command-list>
<command name="DATE" />
<command name="GROUP">
<capability name="LOGGER" />
<capability name="AUTHENTICATOR" />
<argument name="groupname" />
</command>
<command name="ARTICLE">
<capability name="READER" />
<argument name="article-id" optional="false" />
<argument name="message-id" optional="true" />
</command>
</command-list>
提供了一个完整的Java项目com.lacqui.commands,其中包含三个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();
}
}
有什么Eclipse插件能够从Java代码生成UML图吗?我知道其中一些,但重点是它们都是从代码生成类图的。重点是,我还想生成一些其他的图表(例如:dfd-data Flow diagraph),如果不可能,请告诉我--我将使用它...
问题内容: 这是我要构建的工作流程 使用脚本编译我的原型文件,将生成的src放在指定目录中 链接到Eclipse中生成的类 编译我的项目 我可以很容易地使用Eclipse CDT针对C ++做到这一点:在我的项目中,选择File-> New-> Other,然后在General下选择File。然后,我单击“高级”,然后选择指向系统文件的链接。 不过,我还没有发现如何针对Java执行此操作。一旦回答
我将感谢任何帮助或反馈。
我有一个由许多maven项目组成的应用程序。我在日蚀中工作。一些项目使用Maven插件为Web服务等生成存根类。 当我将项目导入到新的工作区时,我必须发出maven generate sources命令,然后附加源文件夹以在每个项目上构建路径。我处理的应用程序有5-6个以上的项目需要这些步骤。 是否有一个插件可以安装在Eclipse中以获取生成的源代码,或者甚至是一个生成源代码并更新构建路径以保存
问题内容: 我使用XJC工具为XSD架构生成Java类。当我使用JAXB Marshaller将类编组为XML有效负载时,我在输出XML中缺少“ schemaLocation”参数,但在xsd文件中声明了此参数。如何在输出XML中强制执行“ schemaLocation”参数? 以下是用于代码生成的xsd模式文件的开头: 问候 问题答案: 试试这个: