示例

    SimpleBuf是一个协议,我们可以定义一些协议的配置,并可以用SimpleBuf代码生成动作生成相关的Java代码,本示例就是这个代码生成的动作。

动作编辑截图

   

    如图代码生成的动作是由一系列的子动作来实现,子动作通常都是SelfAction,其中Begin的作用是把原先的变量self命名成其他变量名。

动作的XML代码

    使用XML表示这个动作的代码如下:

<Begin name="run" descriptors="xworker.lang.actions.Actions/@ActionGroup" changeSelf="true" selfVarName="data" createLocalVarScope="true">
    <actions>
        <AttributeVarGetter name="setProjectPath" _xmeta_id_="setRootPath" thingVarName="data" ifVarAttribute="projectPathVar" varAttribute="projectPath" varName="projectPath"></AttributeVarGetter>
        <AttributeVarGetter name="setRootPackage" thingVarName="data" ifVarAttribute="rootPackageVar" varAttribute="rootPackage" varName="rootPackage"></AttributeVarGetter>
        <TextTemplate name="setJavaPath" _xmeta_id_="javaPath" descriptors="xworker.lang.text.TextTemplate" outputVarName="javaPath" templateCode="${projectPath}/src/${rootPackage?replace(&quot;.&quot;, &quot;/&quot;)}"></TextTemplate>
        <ThingCollection name="generateProtoclHandlerAll" label="所有协议列表" varName="things" thingPaths="${data.protocolThings?if_exists}" attributeTemplate="true"></ThingCollection>
        <ThingCollection name="setOtherDataThings" varName="otherDataThings" thingPaths="${data.dataThings?if_exists}" attributeTemplate="true"></ThingCollection>
        <Iterator name="generateProtocols" varName="thing" collectionName="things">
            <ChildAction>
                <Iterator label="生成协议模型" varName="dataThing">
                    <actions>
                        <GroovyAction name="getCollection">
                            <code><![CDATA[def things = [];
def req = thing.get("Request@0");
if(req != null){
    things.add(req);
    getChilds(req, things);
}
def res = thing.get("Response@0");
if(res != null){
    things.add(res);
    getChilds(res, things);
}

if(otherDataThings != null){
   for(otherDataThing in otherDataThings){
       things.add(otherDataThing);
       getChilds(otherDataThing, things);
   }
}
return things;

def addDataThing(thingPath, things){
    def adInfo = world.getThing(thingPath);
    if(adInfo != null){
        things.add(adInfo);
        getChilds(adInfo, things);
    }
}

def getChilds(data, things){
    for(child in data.getChilds()){
        if((child.getThingName() == "array" && child.className != null && child.className != "") || (child.getThingName() == "If" && child.type == "ifclass" && child.className != null && child.className != "")){
            things.add(child);
        }
        
        getChilds(child, things);
    }
}]]></code>
                        </GroovyAction>
                    </actions>
                    <ChildAction>
                        <GroovyAction name="setRefThings">
                            <code><![CDATA[def refThings = [];
def context = [:];
for(child in dataThing.getChilds()){
    initRefThings(child, refThings, context);
}
actionContext.getScope().put("refThings", refThings);

def initRefThings(thing, refThings, context){    
    if((thing.getThingName() == "array" && thing.className != null && thing.className != "") || (thing.getThingName() == "If" && thing.type == "ifclass" && thing.className != null && thing.className != "")){
        def refThing = [:];
        if(thing.className == null || thing.className == ""){
            refThing.className = thing.refClass;
        }else{
            refThing.className = thing.className;
        }
        refThing.packagePath = thing.packagePath;
        if(refThing.packagePath != null && refThing.packagePath != ""){
            refThing.packagePath = refThing.packagePath + ".";
        }
        
        if(context.get(refThing.packagePath + "." + refThing.className) == null){
            refThings.add(refThing)
            context.put(refThing.packagePath + "." + refThing.className, refThing);
        }
    }
    
    for(child in thing.getChilds()){
        initRefThings(child, refThings, context);
    }
}]]></code>
                        </GroovyAction>
                        <CodeGenerator name="generateProtocol" filePath="${javaPath}/protocol/model/&lt;#if dataThing.packagePath?exists&gt;${dataThing.packagePath?replace(&quot;.&quot;,&quot;/&quot;)}&lt;/#if&gt;/${dataThing.className}.java" attributeTemplate="true">
                            <Contents>
                                <FreemarkerContent name="JavaModelCode" templatePath="xworker/protocol/simplebuf/ftl/protocol_dataJava.ftl"></FreemarkerContent>
                            </Contents>
                        </CodeGenerator>
                    </ChildAction>
                </Iterator>
                <CodeGenerator name="generateHandler" label="生成协议处理器" type="once" filePath="${javaPath}/protocol/handler/${thing.className}Handler.java" attributeTemplate="true">
                    <Contents>
                        <FreemarkerContent name="handlerTempalte" _xmeta_id_="javaCode" templatePath="xworker/protocol/simplebuf/ftl/protocol_HandlerJava.ftl"></FreemarkerContent>
                    </Contents>
                </CodeGenerator>
            </ChildAction>
        </Iterator>
        <CodeGenerator name="generateProtoclHandlerAll" _xmeta_id_="generateProtoclHandlerAll1" label="生成协议总Handler" filePath="${javaPath}/protocol/ProtocolHandler.java" attributeTemplate="true">
            <Contents>
                <FreemarkerContent name="handleTempalte" templatePath="xworker/protocol/simplebuf/ftl/protocol_HandlerMainJava.ftl"></FreemarkerContent>
            </Contents>
        </CodeGenerator>
        <CodeGenerator name="generateUserData" type="once" filePath="${javaPath}/protocol/UserData.java" attributeTemplate="true">
            <Contents>
                <FreemarkerContent templatePath="xworker/protocol/simplebuf/ftl/UserData_java.ftl"></FreemarkerContent>
            </Contents>
        </CodeGenerator>
        <CodeGenerator name="generateCodeable" type="once" filePath="${javaPath}/protocol/codec/Codeable.java" attributeTemplate="true">
            <Contents>
                <FreemarkerContent templatePath="xworker/protocol/simplebuf/ftl/Codeable_java.ftl"></FreemarkerContent>
            </Contents>
        </CodeGenerator>
        <CodeGenerator name="generateCoder" type="once" filePath="${javaPath}/protocol/codec/Coder.java" attributeTemplate="true">
            <Contents>
                <FreemarkerContent templatePath="xworker/protocol/simplebuf/ftl/Coder_java.ftl"></FreemarkerContent>
            </Contents>
        </CodeGenerator>
    </actions>
</Begin>

 

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

沪ICP备08000575号