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>




相关文章
|
1月前
|
XML 前端开发 Java
讲解SSM的xml文件
本文详细介绍了SSM框架中的xml配置文件,包括springMVC.xml和applicationContext.xml,涉及组件扫描、数据源配置、事务管理、MyBatis集成以及Spring MVC的视图解析器配置。
57 1
|
3月前
|
XML Java 数据格式
Spring5入门到实战------7、IOC容器-Bean管理XML方式(外部属性文件)
这篇文章是Spring5框架的实战教程,主要介绍了如何在Spring的IOC容器中通过XML配置方式使用外部属性文件来管理Bean,特别是数据库连接池的配置。文章详细讲解了创建属性文件、引入属性文件到Spring配置、以及如何使用属性占位符来引用属性文件中的值。
Spring5入门到实战------7、IOC容器-Bean管理XML方式(外部属性文件)
|
8天前
|
Java Maven
maven项目的pom.xml文件常用标签使用介绍
第四届人文,智慧教育与服务管理国际学术会议(HWESM 2025) 2025 4th International Conference on Humanities, Wisdom Education and Service Management
55 8
|
1月前
|
XML JavaScript Java
java与XML文件的读写
java与XML文件的读写
25 3
|
1月前
|
XML 存储 缓存
C#使用XML文件的详解及示例
C#使用XML文件的详解及示例
88 0
|
1月前
|
XML 存储 Web App开发
查看 XML 文件
查看 XML 文件
|
2月前
|
SQL XML Java
mybatis :sqlmapconfig.xml配置 ++++Mapper XML 文件(sql/insert/delete/update/select)(增删改查)用法
当然,这些仅是MyBatis功能的初步介绍。MyBatis还提供了高级特性,如动态SQL、类型处理器、插件等,可以进一步提供对数据库交互的强大支持和灵活性。希望上述内容对您理解MyBatis的基本操作有所帮助。在实际使用中,您可能还需要根据具体的业务要求调整和优化SQL语句和配置。
44 1
|
5月前
|
XML Java 数据格式
java创建xml文件内容
java创建xml文件内容
|
5月前
|
XML Java 数据格式
java解析xml文件内容
java解析xml文件内容
|
5月前
|
XML Java 数据库
配置applicationContext.xml文件
配置applicationContext.xml文件