zoukankan      html  css  js  c++  java
  • 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>





  • 相关阅读:
    系统安全方案
    模态框的使用
    thinkphp修改分页为post方式
    手动配置apache、php
    Djang之ModelForm组件的简单使用
    连接池还是连接迟?
    金融量化
    luasocket编译安装遇到的坑
    numpy&pandas补充常用示例
    Matplotlib画正弦余弦曲线
  • 原文地址:https://www.cnblogs.com/gcczhongduan/p/5066293.html
Copyright © 2011-2022 走看看