VO对象通过groovy模板映射XML文件

简介: <p>介绍</p> <p>    之前写过JAVA+XSLT相关的技术博客,最近研究了一个开源工具包org.codehaus.groovy,处理VO对象和XML文件映射非常方便。简言之:将VO对象中的属性(包括Collection, Map),通过groovy模板,映射XML文件。</p> <p><br></p> <p>Maven pom.xml</p> <p></p> <pre

介绍

    之前写过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>




相关文章
|
4天前
|
XML 数据格式
小米备份descript.xml文件
小米备份descript.xml文件
11 0
|
16天前
|
XML Java 数据库连接
mybatis中在xml文件中通用查询结果列如何使用
mybatis中在xml文件中通用查询结果列如何使用
20 0
|
17天前
|
XML JavaScript 前端开发
xml文件使用及解析
xml文件使用及解析
|
1月前
|
XML C# 数据格式
使用C#操作XML文件
使用C#操作XML文件
11 0
|
7天前
|
XML C# 数据格式
C# 解析XML文件
C# 解析XML文件
15 1
|
1月前
Mybatis+mysql动态分页查询数据案例——配置映射文件(HouseDaoMapper.xml)
Mybatis+mysql动态分页查询数据案例——配置映射文件(HouseDaoMapper.xml)
15 1
|
1月前
|
Java
java实现遍历树形菜单方法——映射文件VoteTree.hbm.xml
java实现遍历树形菜单方法——映射文件VoteTree.hbm.xml
10 0
|
1月前
|
Kubernetes Cloud Native Java
Activiti 简介以及最新activiti依赖 pom.xml文件(使用时注意对应版本号)
Activiti 简介以及最新activiti依赖 pom.xml文件(使用时注意对应版本号)
40 2
|
1月前
|
XML Java 数据库连接
struts+hibernate+oracle+easyui实现lazyout组件的简单案例——hibernate的config文件(hibernate.cfg.xml)
struts+hibernate+oracle+easyui实现lazyout组件的简单案例——hibernate的config文件(hibernate.cfg.xml)
11 0
|
2月前
|
XML JavaScript 数据格式
打开 XML 文件报错 There is no Unicode byte order mark
打开 XML 文件报错 There is no Unicode byte order mark
43 0