📄 simpletransform.java
字号:
//at this point we can just use the original url and stylesheet_url so this shouldn't be a problem
in = new InputSource( url );
style = new InputSource( stylesheet_url );
}
if ( logger.isInfoEnabled() )
{
logger.info( "SimpleTransform: transforming url: " +
url +
" with stylesheet: " +
stylesheet_url );
}
in.setSystemId( url );
style.setSystemId( stylesheet_url );
return transform( in,
style,
params );
}
/**
* Used internally to handle doing XSLT transformations directly.
*/
public static String transform( InputSource content,
InputSource stylesheet,
Map params)
throws SAXException
{
// Instantiate a TransformerFactory.
TransformerFactory tFactory = TransformerFactory.newInstance();
// Determine whether the TransformerFactory supports the use of SAXSource
// and SAXResult
if (!tFactory.getFeature(SAXTransformerFactory.FEATURE) )
{
logger.error( "SimpleTransform: nobody told you that we need a SAX Transformer?" );
throw new SAXException( "Invalid SAX Tranformer" );
}
try
{
// Cast the TransformerFactory.
SAXTransformerFactory saxTFactory = ((SAXTransformerFactory) tFactory);
// Create a ContentHandler to handle parsing of the stylesheet.
TemplatesHandler templatesHandler = saxTFactory.newTemplatesHandler();
// Create an XMLReader and set its ContentHandler.
XMLReader reader = XMLReaderFactory.createXMLReader();
reader.setContentHandler(templatesHandler);
// Parse the stylesheet.
reader.parse( stylesheet );
//Get the Templates object from the ContentHandler.
Templates templates = templatesHandler.getTemplates();
// Create a ContentHandler to handle parsing of the XML source.
TransformerHandler handler
= saxTFactory.newTransformerHandler(templates);
// Reset the XMLReader's ContentHandler.
reader.setContentHandler(handler);
// Set the ContentHandler to also function as a LexicalHandler, which
// includes "lexical" events (e.g., comments and CDATA).
try
{
reader.setProperty("http://xml.org/sax/properties/lexical-handler", handler);
}
catch( org.xml.sax.SAXNotRecognizedException e ) {}
final Transformer processor = handler.getTransformer();
if( params != null )
{
Iterator keys = params.keySet().iterator();
while( keys.hasNext() )
{
String name = (String) keys.next();
String value = (String) params.get(name);
processor.setParameter(name,
new XString( value )
/*FIXME: was processor.createXString( value) */ );
}
}
StringWriter pw = new StringWriter();
// Have the XSLTProcessor processor object transform "foo.xml" to
// System.out, using the XSLT instructions found in "foo.xsl".
processor.transform( new SAXSource( content ),
new StreamResult( pw ) );
try
{
pw.flush();
pw.close();
}
catch (IOException e)
{
//should never really happen
logger.error("Exception", e);
}
return pw.toString();
}
catch (Exception e)
{
logger.error( "Invalid SAX Transformer: " , e);
throw new SAXException( "problem in SAX transform: " + e.toString() );
}
}
/**
* Perform a event based parsing of the given content_url,
* process it with the XSLT stylesheet stylesheet_url, using the params
* parameters, and return a Reader that will do the transformation dynamically.
*
* @param content_url The url of the xml document
* @param stylesheet_url The url of the stylesheet
* @param params A Map containing stylesheet parameters
* @return a Reader on the transformed document
*
*/
public static Reader SAXTransform( String content_url,
String stylesheet_url,
Map params) throws IOException
{
// Instantiate a TransformerFactory.
TransformerFactory tFactory = TransformerFactory.newInstance();
// Determine whether the TransformerFactory supports the use of SAXSource
// and SAXResult
if (!tFactory.getFeature(SAXTransformerFactory.FEATURE) )
{
logger.error( "SimpleTransform: nobody told you that we need a SAX Transformer?" );
throw new IOException( "Invalid SAX Tranformer" );
}
try
{
// Cast the TransformerFactory.
SAXTransformerFactory saxTFactory = ((SAXTransformerFactory) tFactory);
// Create a ContentHandler to handle parsing of the stylesheet.
TemplatesHandler templatesHandler = saxTFactory.newTemplatesHandler();
// Create an XMLReader and set its ContentHandler.
XMLReader reader = XMLReaderFactory.createXMLReader();
reader.setContentHandler(templatesHandler);
// Set it to solve Entities through Jetspeed URL Manager
reader.setEntityResolver( new JetspeedXMLEntityResolver() );
// Parse the stylesheet.
InputSource style = new InputSource( JetspeedDiskCache.getInstance()
.getEntry( stylesheet_url ).getReader() );
style.setSystemId( stylesheet_url );
final InputSource xstyle = style;
reader.parse( xstyle );
//Get the Templates object from the ContentHandler.
Templates templates = templatesHandler.getTemplates();
// Create a ContentHandler to handle parsing of the XML source.
TransformerHandler handler
= saxTFactory.newTransformerHandler(templates);
// Reset the XMLReader's ContentHandler.
reader.setContentHandler(handler);
// Set the ContentHandler to also function as a LexicalHandler, which
// includes "lexical" events (e.g., comments and CDATA).
try
{
reader.setProperty("http://xml.org/sax/properties/lexical-handler", handler);
}
catch( org.xml.sax.SAXNotRecognizedException e ) {}
final Transformer processor = handler.getTransformer();
//Set the parameters (if any)
if( params != null )
{
Iterator keys = params.keySet().iterator();
while( keys.hasNext() )
{
String name = (String) keys.next();
String value = (String) params.get(name);
//FIXME: maybe we need to quote again...
// was processor.createXString( value)
processor.setParameter(name,
new XString( value ) );
}
}
PipedInputStream pis = new PipedInputStream();
PipedOutputStream pos = new PipedOutputStream( pis );
try
{
final Writer pw = new OutputStreamWriter( pos, "utf-8" );
InputSource is = new InputSource( JetspeedDiskCache.getInstance()
.getEntry( content_url ).getReader() );
is.setSystemId( content_url );
final SAXSource xinput = new SAXSource( is );
//Perform the transformation on a new thread, using
// PipedStreams
Thread t = new Thread( new Runnable()
{
public void run()
{
// Have the processor object transform
// "foo.xml" to
// System.out, using the XSLT instructions
//found in "foo.xsl".
logger.debug("Starting SAX thread...");
try
{
processor.transform( xinput,
new StreamResult( pw ) );
pw.close();
logger.debug("...ending SAX thread.");
}
catch( Exception se)
{
logger.debug("Error in SAXTransform" + se.toString(), se );
}
}
} );
t.start();
}
catch (java.io.UnsupportedEncodingException uee)
{
logger.error("Need utf-8 encoding to SAXTransform", uee);
}
return new InputStreamReader ( pis, "utf-8" );
}
catch (Exception e)
{
logger.error( "Invalid SAX Transformer:" , e);
throw new IOException( "problem in SAX transform: " + e.toString() );
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -