package com.uas.report.crystal2jasper;

import java.io.File;
import java.io.StringWriter;

import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

/**
 * 格式化的xml与可读xml之间的映射
 * 
 * @author sunyj
 * @since 2017年1月18日 下午3:36:36
 */
public class MapHelper {

	/**
	 * 映射
	 * 
	 * @param sourceXmlFile
	 *            源xml文件
	 * @param mapRuleFile
	 *            映射处理文件
	 * @return 映射后的xml
	 * @throws TransformerException
	 */
	public static String map(File sourceXmlFile, File mapRuleFile) throws TransformerException {
		TransformerFactory factory = TransformerFactory.newInstance();
		Transformer transformer = factory.newTransformer(new StreamSource(mapRuleFile));
		StringWriter writer = new StringWriter();
		StreamResult result = new StreamResult(writer);
		transformer.transform(new StreamSource(sourceXmlFile), result);
		return writer.toString();
	}

}
