开发者

Groovy template - code generation

开发者 https://www.devze.com 2023-03-11 05:08 出处:网络
I am asking for advice and opition as of the code to use with groovy templates. All template examples on the web used a very limited logic but I simply cannot overcome that barrier and the code in my

I am asking for advice and opition as of the code to use with groovy templates.

All template examples on the web used a very limited logic but I simply cannot overcome that barrier and the code in my template is substantial.

Is this acceptable? What could be a better way to do this?

Thanks Peter

The task开发者_StackOverflow中文版 is to generate TCL type code - specifically if then/elsif/else type contraint

if { [streq $pm_enrichment('a1') "'aaaa2'"] && [strlen $pm_enrichment('aaaa3')] &&\
    [strlen $pm_enrichment('aaaa4') ] } { 
     set pm_enrichment('ResultAAA') 0 
}
elseif { [streq $pm_enrichment('b1') "'bb2'"] && [strlen $pm_enrichment('bbb3')] &&\
[strlen $pm_enrichment('bbbb4') ] } { 
     set pm_enrichment('ResultBBB') 1 
}
else { [streq $pm_enrichment('c1') "'cc2'"] && [strlen $pm_enrichment('ccc3')] &&\
[strlen $pm_enrichment('cccc4') ] } { 
     set pm_enrichment('ResultCCC') 2G 
} 


//////////////////////////////////////
def dataCasesClosure= {->
    pos=0
    arrSorted = []
    mapStmt.each{arrSorted.add(it.key) }
    arrSorted = arrSorted.sort()
    outStr=''''''

arrSorted.each { i ->
    tmpStatement = statement
    tmpResultStmt = resultStmt
    list=mapStmt[i]
    resultList=mapResultStmt[i]

pos=0
int index = tmpStatement.indexOf(keyword);
while (index >=0){
    val = list[pos].replaceAll(~'"','')
    pos +=1
    tmpStatement=tmpStatement.replaceFirst( ~/#/,/${val}/)
    index = tmpStatement.indexOf(keyword, index+keyword.length())   ;
}

if (tmpStatement ==~/\W+$/) {
    tmpStatement=tmpStatement[0..-2]
}

pos=0
index = tmpResultStmt.indexOf(keyword);
while (index >=0){
    val = resultList[pos]
    pos +=1
    tmpResultStmt=tmpResultStmt.replaceFirst( ~/#/,/${val}/)                
    index = tmpResultStmt.indexOf(keyword, index+keyword.length())  ;
}

if (i==0) {
    outStr= "if {${tmpStatement} } { \n\t\t ${tmpResultStmt} \n  }"
} else if (i < arrSorted.size()-1  ){
    outStr += "elseif {${tmpStatement} } { \n\t\t ${tmpResultStmt} \n  }"
} else {
    outStr += "else {${tmpStatement} } { \n\t\t ${tmpResultStmt} \n  }"
}
}

outStr

} // ### dataCasesClosure

def valuesIfThenStmt= [
"statement":dataCasesClosure
]

tplIfThenStmt = '''
##############################
${statement}
'''

def engine = new SimpleTemplateEngine()
templateResult = engine.createTemplate(tplIfThenStmt).make(valuesIfThenStmt)
println templateResult.toString()


If this is all you have to generate, the template is overkill. You could have just called the dataCasesClosure directly to get its output.

Assuming it is part of a larger template then, I think it is very reasonable to use closures to produce output for a particularly complex parts, just as you have done. I have personally done this on an extreme scale with good results.

0

精彩评论

暂无评论...
验证码 换一张
取 消