📄 transformationmanager.java
字号:
package org.jawin.browser.xsl;
import java.util.HashMap;
import javax.xml.transform.*;
import javax.xml.transform.stream.*;
import java.io.*;
import org.w3c.dom.Node;
import javax.xml.transform.dom.DOMSource;
import org.jawin.browser.config.ConfigManager;
import org.jawin.browser.log.Log;
/**
* Manages on the fly XSL transformations of XML and transparent caching of
* loaded and compiled XSL stylesheets
*
* Use of this class implies Xalan (or another XSLT processor)
* is available on the classpath.
*
* Download the latest version or Xalan from http://xml.apache.org
*
* Due to intended useage, this class may not be thread safe for concurrent
* additions of new stylesheets.
*
* <p>Title: Jawin Code Generation GUI</p>
* <p>Description: GUI for exploring type libraries and generating Java code</p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: Open Source Incentive</p>
*
* @author Josh Passenger
* @version 1.0
*/
public class TransformationManager
{
/**
* The singleton TransformationManager instance
*/
private static TransformationManager instance = null;
/**
* A HashMap of cached compiled stylesheets
*/
private HashMap stylesheets = new HashMap();
private boolean cacheStylesheets = ConfigManager.getInstance().getBoolean("xsl.cacheStylesheets");
/**
* The TransformerFactory that we will use to compile stylesheets
*/
private TransformerFactory transformerFactory = null;
/**
* private constructor to support the singleton pattern
*/
private TransformationManager()
{
try
{
transformerFactory = TransformerFactory.newInstance();
}
catch (Exception e)
{
Log.getInstance().exception("TransformationManager() failed to start up", e);
}
}
/**
* Initialize the TransformationManager
*/
public static synchronized void initialize()
{
instance = new TransformationManager();
}
/**
* Fetch the shared singleton TransformationManager instance
*
* @return the singleton TransformationManager instance
*/
public static TransformationManager getInstance()
{
if (instance == null)
{
throw new IllegalStateException("TransformationManager.getInstance() TransformationManager was not initialized");
}
return instance;
}
/**
* Checks to see if this stylesheet is currently in the cache
*
* @param xslUrl the url of the stylesheet
* @return true if the stylesheet is cached, false if it is not
*/
private boolean isStylesheetCached(String xslUrl)
{
if (!cacheStylesheets)
{
return false;
}
return stylesheets.containsKey(xslUrl);
}
/**
* Removes all cached XSL stylsheets from the cache
*/
public void clearCache()
{
stylesheets.clear();
}
/**
* Caches the requested stylesheet
*
* @param xslUrl the url of the stylesheet to cache
* @throws TransformationException thrown if the stylesheet cannot be compiled
*/
public void cacheStylesheet(String xslUrl) throws TransformationException
{
try
{
Transformer transformer = transformerFactory.newTransformer(new StreamSource(xslUrl));
stylesheets.put(xslUrl, transformer);
}
catch (Exception e)
{
throw new TransformationException("TransformationManager.cacheStylesheet() failed to compile stylesheet from: " + xslUrl, e);
}
}
/**
* Transforms the requested XML string using the cached XSL stylesheet.
*
* If the xsl stylesheet is not in the cache it should be compiled and
* added to the cache.
*
* @param xml the XML to transform
* @param xslUrl an XSL url that is used to load this stylesheet from cache
* @return the transformed XML
* @throws TransformationException if something goes wrong
*/
public String transform(String xml, String xslUrl) throws TransformationException
{
checkStylesheet(xslUrl);
Transformer transformer = getCachedStylesheet(xslUrl);
StringReader reader = new StringReader(xml);
StringWriter writer = new StringWriter();
StreamSource source = new StreamSource(reader);
StreamResult result = new StreamResult(writer);
try
{
transformer.transform(source, result);
}
catch (TransformerException t)
{
throw new TransformationException("TransformationManager.transform() failed to transform XML with stylesheet: " + xslUrl, t);
}
return writer.toString();
}
/**
* Check to see if this stylesheet is cached and cache it if it is not
* already cached
* @param xslUrl the stylesheet to chech for in the cache
* @throws TransformationException if the stylesheet did not exist and
* was not cacheable
*/
private void checkStylesheet(String xslUrl) throws TransformationException
{
if (!isStylesheetCached(xslUrl))
{
cacheStylesheet(xslUrl);
}
}
/**
* Fetch a compiled stylesheet from the cache
*
* @param xslUrl the url of the stylesheet
* @return the compiled Transformer object
* @throws TransformationException if the cached stylesheet did not
* exist in the cache
*/
private Transformer getCachedStylesheet(String xslUrl) throws TransformationException
{
Transformer transformer = (Transformer) stylesheets.get(xslUrl);
if (transformer == null)
{
throw new TransformationException("TransformationManager.getCachedStylesheet() failed to find cached stylesheet: " + xslUrl);
}
return transformer;
}
/**
* Transforms the requested XML string using without using any caching
* of the XSL stylesheet
*
* @param xml the XML to transform
* @param xsl an XSL stylesheetString that is used to perform the transformation
* @return the transformed XML
* @throws TransformationException thrown if the transformation fails
*/
public String transformRaw(String xml, String xsl) throws TransformationException
{
StringReader xslReader = new StringReader(xsl);
StringReader xmlReader = new StringReader(xml);
StreamSource source = new StreamSource(xmlReader);
StringWriter writer = new StringWriter();
StreamResult result = new StreamResult(writer);
try
{
Transformer transformer = transformerFactory.newTransformer(new StreamSource(xslReader));
transformer.transform(source, result);
}
catch (Exception e)
{
throw new TransformationException("TransformationManager.transformRaw() failed to transform inline stylesheet and XML", e);
}
return writer.toString();
}
/**
* TRansform the DOM source into a result String using the provided xslUrl
* @param node the node to transform
* @param xslUrl the XSL stylesheet to sue to transform the node
* @return the transformed String
* @throws TransformationException thrown if something goes wrong
*/
public String transformNode(Node node, String xslUrl) throws TransformationException
{
checkStylesheet(xslUrl);
Transformer transformer = getCachedStylesheet(xslUrl);
DOMSource domSource = new DOMSource(node);
StringWriter writer = new StringWriter();
StreamResult result = new StreamResult(writer);
try
{
transformer.transform(domSource, result);
}
catch (Exception e)
{
throw new TransformationException("TransformationManager.transformNode() failed to transform node with stylesheet: " + xslUrl, e);
}
return writer.toString();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -