VO对象通过groovy模板映射XML文件
介绍
之前写过JAVA+XSLT相关的技术博客,近期研究了一个开源工具包org.codehaus.groovy,处理VO对象和XML文件映射很方便。
简言之:将VO对象中的属性(包含Collection, Map),通过groovy模板,映射XML文件。
Maven pom.xml
<dependency> <groupId>org.codehaus.groovy</groupId> <artifactId>groovy-all</artifactId> <version>2.3.0</version> </dependency>
VO类:
package shuai.study.groovy.demo.vo; import java.util.LinkedHashMap; import java.util.Map; /** * @author shengshu * */ public class MeasurementVO { private String dn = null; private String measurementType = null; private Map<String, Integer> counterMap = new LinkedHashMap<String, Integer>(); public String getDn() { return this.dn; } public void setDn(String dn) { this.dn = dn; } public String getMeasurementType() { return this.measurementType; } public void setMeasurementType(String measurementType) { this.measurementType = measurementType; } public Map<String, Integer> getCounterMap() { return this.counterMap; } public void setCounterMap(Map<String, Integer> counterMap) { this.counterMap = counterMap; } }
Collection类:
package shuai.study.groovy.demo.collection; import java.util.LinkedList; import java.util.List; import shuai.study.groovy.demo.vo.MeasurementVO; /** * @author shengshu * */ public class MeasurementCollection { List<MeasurementVO> MeasurementVoList = new LinkedList<MeasurementVO>(); public List<MeasurementVO> getMeasurementVoList() { return this.MeasurementVoList; } public void setMeasurementVoList(MeasurementVO measurementVO) { this.MeasurementVoList.add(measurementVO); } }
groovy模板文件:
<?xml version="1.0"?> <OMeS> <PMSetup> <PMMOResult> <% def measurementVoList=measurementCollection.getMeasurementVoList(); for (measurementVo in measurementVoList) { def dn=measurementVo.getDn(); def measurementType=measurementVo.getMeasurementType();%> <MO> <DN>${dn}</DN> </MO> <PMTarget measurementType="${measurementType}"> <% def counterMap=measurementVo.getCounterMap(); counterMap.each { def counterKey=it.key; def counterValue=it.value;%> <${counterKey}>${counterValue}</${counterKey}> <%}%> </PMTarget> <%}%> </PMMOResult> </PMSetup> </OMeS>
Transfer类:
package shuai.study.groovy.demo.transfer; import groovy.text.SimpleTemplateEngine; import groovy.text.Template; import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.io.Writer; import java.util.HashMap; import java.util.Map; import org.apache.commons.io.IOUtils; import org.codehaus.groovy.control.CompilationFailedException; import shuai.study.groovy.demo.collection.MeasurementCollection; /** * @author shengshu * */ public class MeasurementTransfer { private static MeasurementTransfer measurementTransfer = null; private Template template = null; public static MeasurementTransfer getMeasurementTransfer() { if (measurementTransfer == null) { synchronized (MeasurementTransfer.class) { if (measurementTransfer == null) { measurementTransfer = new MeasurementTransfer(); } } } return measurementTransfer; } private MeasurementTransfer() { File measurementTemplateFile = new File("/demo/groovy/template/measurementTemplate.xml"); if (!measurementTemplateFile.exists()) { throw new NullPointerException("Measurement template file " + measurementTemplateFile + " is null"); } try { template = new SimpleTemplateEngine().createTemplate(measurementTemplateFile); } catch (CompilationFailedException cfe) { cfe.printStackTrace(); } catch (ClassNotFoundException cnfe) { cnfe.printStackTrace(); } catch (IOException ioe) { ioe.printStackTrace(); } } public void transfer(MeasurementCollection measurementCollection, File outputFile) { Map<String, MeasurementCollection> measurementCollectionMap = new HashMap<String, MeasurementCollection>(); measurementCollectionMap.put("measurementCollection", measurementCollection); Writer writer = null; try { FileWriter fileWriter = new FileWriter(outputFile); BufferedWriter bufferedWriter = new BufferedWriter(fileWriter); writer = template.make(measurementCollectionMap).writeTo(bufferedWriter); } catch (IOException ioe) { ioe.printStackTrace(); } finally { IOUtils.closeQuietly(writer); } } }Executer启动类:
package shuai.study.groovy.demo; import java.io.File; import java.io.IOException; import java.util.LinkedHashMap; import java.util.Map; import org.apache.commons.io.FileUtils; import shuai.study.groovy.demo.collection.MeasurementCollection; import shuai.study.groovy.demo.transfer.MeasurementTransfer; import shuai.study.groovy.demo.vo.MeasurementVO; /** * @author shengshu * */ public class MeasurementExecuter { public static void main(String[] args) { Map<String, Integer> counterMap = new LinkedHashMap<String, Integer>(); counterMap.put("nbrOfProvModify", 100); counterMap.put("nbrOfProvCreate", 300); counterMap.put("nbrOfProvDelete", 500); MeasurementVO measurementVO = new MeasurementVO(); measurementVO.setDn("PLMN-PLMN/IPS-1/CNODE-2/STYP-3"); measurementVO.setMeasurementType("PERFMGMT"); measurementVO.setCounterMap(counterMap); MeasurementCollection measurementCollection = new MeasurementCollection(); measurementCollection.setMeasurementVoList(measurementVO); File measurementOutputFile = new File("/demo/groovy/output/measurementOutput.xml"); try { FileUtils.touch(measurementOutputFile); } catch (IOException ioe) { ioe.printStackTrace(); } MeasurementTransfer measurementTransfer = MeasurementTransfer.getMeasurementTransfer(); measurementTransfer.transfer(measurementCollection, measurementOutputFile); } }
Output执行输出文件:
<?xml version="1.0"?> <OMeS> <PMSetup> <PMMOResult> <MO> <DN>PLMN-PLMN/IPS-1/CNODE-2/STYP-3</DN> </MO> <PMTarget measurementType="PERFMGMT"> <nbrOfProvModify>100</nbrOfProvModify> <nbrOfProvCreate>300</nbrOfProvCreate> <nbrOfProvDelete>500</nbrOfProvDelete> </PMTarget> </PMMOResult> </PMSetup> </OMeS>