首页 > 代码库 > VO对象通过groovy模板映射XML文件
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; 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; 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=http://www.mamicode.com/it.value;%>>
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; 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; 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>
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。