我现在grails+groovy,用Groovy 生成 Flex tree 功能树需要的XML文件,里面用到了递归,可以无限级。下现有二个类:一个是对象TabFunction,这就是我的功能表影射出的对象,Test3是生成XML的测试类。(附件中有:groovy语方法文档。)
package ce.bean
class TabFunction {
String cCode;
String cName;
String cDescription;
String cParentCode;
Date dCreate=new Date();
String cState;
String cGrade
static mapping ={
version false;
}
static constraints = {
}
}
package com
import groovy.xml.StreamingMarkupBuilder
import ce.bean.TabFunction
class Test3 {
def str="<funlist></funlist>";
def void roolMothed(){
def obj= new TabFunction(cCode:"TOP_SysUserManager",cName:"基础信息维护",cDescription:"系统注册信息管理",cState:"0",cGrade:1,cParentCode:"00");
obj.id=2;
def obj1= new TabFunction(cCode:"SYS_UserManager",cName:"用户信息管理",cDescription:"系统注册用户管理",cState:"0",cGrade:2,cParentCode:"TOP_SysUserManager");
obj1.id=1;
def obj2= new TabFunction(cCode:"TOP_SystemManager",cName:"系统信息维护",cDescription:"系统注册信息管理",cState:"0",cGrade:1,cParentCode:"00");
obj2.id=3;
def obj3= new TabFunction(cCode:"fff",cName:"系统信息维护",cDescription:"系统注册信息管理",cState:"0",cGrade:3,cParentCode:"SYS_UserManager");
obj3.id=5;
List list=new ArrayList();
list<<obj1<<obj<<obj2<<obj3;
println "LAST:::"+ testMethod(list,list,"00",str)
}
def String testMethod(all,list,parentCode,str){
def temp = all.findAll {[email protected]==parentCode};
def root = new XmlSlurper().parseText(str) ;
def outputBuilder = new StreamingMarkupBuilder()
def tempList=new ArrayList();
String result="";
if(parentCode.length()>0){
temp.each {
def tempFun=it;
//def obj=root.sysfun.find {[email protected] == parentCode };
root.appendNode{
sysfun(cCode:tempFun.cCode,cName:tempFun.cName);
}
def ll= all.findAll {[email protected]==tempFun.cCode}
if (ll.size()>0) ll.each{ tempList<<it};
}
}else{
list.each {
def tempFun=it;
// println tempFun.cParentCode+":::::"+tempFun.cCode;
def obj=root.'**'.find{[email protected]==tempFun.cParentCode};
obj.appendNode{
sysfun(cCode:tempFun.cCode,cName:tempFun.cName);
}
def ll= all.findAll {[email protected]==tempFun.cCode}
if (ll.size()>0) ll.each{ tempList<<it};
}
}
result = outputBuilder.bind{ mkp.yield root }
if(tempList.size()>0)
testMethod(all,tempList,"",result);
else{
return result;
}
}
public static void main(def a){
new Test3().roolMothed();
}
}