|
XWorker除了可以开发SWT和WEB应用外,也可以编写一些工具类事物,比如这里将要演示的代码生成事物。
代码生成事物说明
代码生成事物能够通过模板批量生成文本代码到文件中。
代码生成事物可以指定多个用于生成事物(数据),也可以指定多个模板,每个事物+模板就能生成一个文件,所有的事物+模板也能生成一个文件。
代码生成事物也使用了其他工具事物,可参看:TextTemplate、ThingCollection。
创建代码生成事物
代码生成事物core:things:util.CodeGenerator使用XML描述大致如下:
<thing description="<p>代码生成。</p>" name="CodeGenerator" label="CodeGenerator" descriptors="core:things:lang.MetaDescriptor3">
<attribute size="40" name="name"/>
<attribute colspan="2" size="60" description="<p>输出目录。</p>" name="outputDir"/>
<thing extends="core:things:util.ThingCollection" description="<p>需要导出的事物集合。</p>"
name="Things" descriptors="core:things:lang.MetaDescriptor3:/@thing"/>
<thing description="<p>通过文本模板生成代码。</p>" name="TextTemplate" descriptors="core:things:lang.MetaDescriptor3:/@thing">
<attribute name="name"/>
<attribute name="label"/>
<attribute colspan="2" size="60" description="" name="templatePath"/>
<attribute inputtype="select" default="freemarker" description="<p>模板类型。</p>" name="templateType">
<value name="freemarker" value="freemarker" label="freemarker"/>
<value name="velocity" value="velocity" label="velocity"/>
</attribute>
<attribute default="UTF-8" description="<p>模板文件的编码。</p>" name="templateEncoding"/>
<attribute default="things" description="<p>上模板上下文中事物列表的名称,供模板访问事物列表。</p>" name="thingsName"/>
<attribute default="thing" description="<p>在模板上下文中单个模板的名称。</p>" name="thingName"/>
<attribute colspan="2" size="60" description="" name="outputDir"/>
<attribute colspan="2" size="60" description="" name="outputFile"/>
<attribute default="UTF-8" description="<p>输出编码。</p>" name="outputEncoding"/>
<attribute inputtype="truefalse" default="false" description="" name="forAllThings"/>
<attribute colspan="2" inputtype="textarea" description="" name="filterDescriptors"/>
</thing>
<thing description="" name="CodeGenerator" descriptors="core:things:lang.MetaDescriptor3:/@thing"/>
<actions name="actions">
<GroovyAction name="run" code="..."/>
</actions>
</thing>
使用core:things:util.CodeGenerator事物来创建代码生成事物,比如example:core:examples.util.TestCodeGenerator。
TestCodeGenerator使用XML表示大致如下。
<CodeGenerator outputDir="d:/temps/" name="TestCodeGenerator">
<Things name="Things" things="core:things"/>
<TextTemplate outputFile="{categoryName}/{thing.name}.java" name="TextTemplate" templatePath="core:test/test.ftl"/>
</CodeGenerator>
编辑TestCodeGenerator的表单如下。

CodeGenerator节点

指定事物集合节点

模板节点
执行生成代码
代码生成事物定义了run方法,执行example:core:examples.util.TestCodeGenerator事物的run方法可以生成代码。
|