All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.rt.core.util.XMLURIResolver Maven / Gradle / Ivy

There is a newer version: 1.1.17
Show newest version
package com.rt.core.util;

import java.io.StringReader;

import javax.xml.transform.Source;
import javax.xml.transform.TransformerException;
import javax.xml.transform.URIResolver;
import javax.xml.transform.stream.StreamSource;

import com.rt.core.constant.RTConst;
import com.rt.core.log.Log;

/**
 * 主要用于获取xsl(t)样式文件中被引用的其它样式文件
 */
public class XMLURIResolver implements URIResolver {

	private static Log log = Log.getLog(XMLURIResolver.class);

	/** xsl path */
	private String location;

	/**
	 * 默认构造 处理路径
	 * 
	 * @param location
	 *            xsl path
	 */
	public XMLURIResolver(String location) {
		if (null != location) {
			StringBuffer path = new StringBuffer(location);
			// 如果路径中包含文件名,需要先去掉它。
			if (path.lastIndexOf(RTConst.DOT) != -1) {
				this.location = path.substring(0, location
						.lastIndexOf(RTConst.ROOT_PATH) + 1);
				// 其它情况直接在最后追加 '/'
			} else {
				this.location = path.append(RTConst.ROOT_PATH).toString();
			}
		}
	}

	/**
	 * 通过构造函数传入的路径获取被引入的模板并返回
	 */
	public Source resolve(String href, String base) throws TransformerException {
		if (null != location) {
			StringReader sr = null;
			StringBuffer path = new StringBuffer(location);
			path.append(href);
			try {
				sr = new StringReader(XMLUtil.readDocument(path.toString())
						.asXML());
				return new StreamSource(sr);
			} catch (Exception e) {
				log.error(e.toString());
			}
		}
		return null;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy