XWorker
简介
特点
下载

 面向事物
面向事物的概念
面向事物和面向对象对比
面向事物的哲学

 示例
事物的创建过程 
SWT
WEB
代码生成
YahooYUI

 关于
协议
联系我们

 

 代码生成演示

    XWorker除了可以开发SWT和WEB应用外,也可以编写一些工具类事物,比如这里将要演示的代码生成事物。

代码生成事物说明

    代码生成事物能够通过模板批量生成文本代码到文件中。

    代码生成事物可以指定多个用于生成事物(数据),也可以指定多个模板,每个事物+模板就能生成一个文件,所有的事物+模板也能生成一个文件。

    代码生成事物也使用了其他工具事物,可参看:TextTemplate、ThingCollection。

创建代码生成事物

    代码生成事物core:things:util.CodeGenerator使用XML描述大致如下:

<thing description="&lt;p&gt;代码生成。&lt;/p&gt;" name="CodeGenerator" label="CodeGenerator" descriptors="core:things:lang.MetaDescriptor3">
    <attribute size="40" name="name"/>
    <attribute colspan="2" size="60" description="&lt;p&gt;输出目录。&lt;/p&gt;" name="outputDir"/>
    <thing extends="core:things:util.ThingCollection" description="&lt;p&gt;需要导出的事物集合。&lt;/p&gt;" 
             name="Things" descriptors="core:things:lang.MetaDescriptor3:/@thing"/>
    <thing description="&lt;p&gt;通过文本模板生成代码。&lt;/p&gt;" 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="&lt;p&gt;模板类型。&lt;/p&gt;" name="templateType">
            <value name="freemarker" value="freemarker" label="freemarker"/>
            <value name="velocity" value="velocity" label="velocity"/>
        </attribute>
        <attribute default="UTF-8" description="&lt;p&gt;模板文件的编码。&lt;/p&gt;" name="templateEncoding"/>
        <attribute default="things" description="&lt;p&gt;上模板上下文中事物列表的名称,供模板访问事物列表。&lt;/p&gt;" name="thingsName"/>
        <attribute default="thing" description="&lt;p&gt;在模板上下文中单个模板的名称。&lt;/p&gt;" name="thingName"/>
        <attribute colspan="2" size="60" description="" name="outputDir"/>
        <attribute colspan="2" size="60" description="" name="outputFile"/>
        <attribute default="UTF-8" description="&lt;p&gt;输出编码。&lt;/p&gt;" 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方法可以生成代码。

 

Copyright ©  2007-2008 XWorker.org  版权所有

沪ICP备08000575号