sgala 01/05/29 11:21:44
Modified: src/java/org/apache/jetspeed/util SimpleTransform.java
Log:
Definition of org.xml.sax.driver and better documentation and formatting
Revision Changes Path
1.16 +274 -204
jakarta-jetspeed/src/java/org/apache/jetspeed/util/SimpleTransform.java
Index: SimpleTransform.java
===================================================================
RCS file:
/home/cvs/jakarta-jetspeed/src/java/org/apache/jetspeed/util/SimpleTransform.java,v
retrieving revision 1.15
retrieving revision 1.16
diff -u -r1.15 -r1.16
--- SimpleTransform.java 2001/05/04 15:51:10 1.15
+++ SimpleTransform.java 2001/05/29 18:21:37 1.16
@@ -113,104 +113,136 @@
* runtime failure.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Kevin A. Burton</a>
- * @version $Id: SimpleTransform.java,v 1.15 2001/05/04 15:51:10 sgala Exp $
+ * @author <a href="mailto:[EMAIL PROTECTED]">Santiago Gala</a>
+ * @version $Id: SimpleTransform.java,v 1.16 2001/05/29 18:21:37 sgala Exp $
*/
-public class SimpleTransform {
+public class SimpleTransform
+{
-
+ //FIXME: This code should go into the Turbine XSLTservice.
+ //Also, it is a temporary hack, as it should be configurable,
+ // and done later.
+ static
+ {
+ try
+ {
+ if( System.getProperty( "org.xml.sax.driver" ) == null )
+ {
+ System.setProperty( "org.xml.sax.driver",
+ "org.apache.xerces.parsers.SAXParser" );
+ }
+ }
+ catch (Throwable t)
+ {
+ //be very cautious here. We are in class initialization.
+ t.printStackTrace();
+ }
+ }
+
/**
- * Given a a DOM and a URL to a stylesheet, transform the original document.
+ * Given a a DOM and a URL to a stylesheet,
+ * transform the original document.
*/
public static String transform( Document doc,
String stylesheet_url)
- throws SAXException {
- return transform( doc, stylesheet_url, null );
+ throws SAXException
+ {
+ return transform( doc, stylesheet_url, null );
}
/**
- * Given a a DOM and a URL to a stylesheet, transform the original document.
- * SGP Adapt it to pass parameters to the stylesheet
+ * Given a a DOM and a URL to a stylesheet,
+ * transform the original document,
+ * passing parameters to the stylesheet
*/
public static String transform( Document doc,
String stylesheet_url,
Map params)
- throws SAXException {
+ 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) )
+ // Instantiate a TransformerFactory.
+ TransformerFactory tFactory = TransformerFactory.newInstance();
+ // Determine whether the TransformerFactory supports the use of SAXSource
+ // and SAXResult
+ if (!tFactory.getFeature(SAXTransformerFactory.FEATURE) )
{
Log.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);
+ 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.
- final InputSource xstyle = new InputSource( JetspeedDiskCache.getInstance()
+ // Parse the stylesheet.
+ final InputSource xstyle = new InputSource(
JetspeedDiskCache.getInstance()
.getEntry( stylesheet_url
).getReader() );
- reader.parse( xstyle );
+ 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);
+ //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);
+ // 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 ) {}
+ // 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();
+ final Transformer processor = handler.getTransformer();
- if( params != null )
- {
+ 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,
- value ); //FIXME: maybe we need to
quote again...
- // was processor.createXString(
value)
- }
+ {
+ String name = (String) keys.next();
+ String value = (String) params.get(name);
+ processor.setParameter(name,
+ value ); //FIXME: maybe we need to quote
again...
+ // was processor.createXString( value)
+ }
}
- StringWriter pw = new StringWriter();
+ 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 DOMSource( doc ),
- new StreamResult( pw ) );
+ // Have the XSLTProcessor processor object transform "foo.xml" to
+ // System.out, using the XSLT instructions found in "foo.xsl".
+ processor.transform( new DOMSource( doc ),
+ new StreamResult( pw ) );
- try {
+ try
+ {
- pw.flush();
- pw.close();
+ pw.flush();
+ pw.close();
- } catch (IOException e) {
- //should never really happen
- Log.error( e );
- }
- return pw.toString();
- } catch (Exception e) {
- Log.error( "Invalid SAX Transformer: " + e.toString() );
- throw new SAXException( "problem in SAX transform: " + e.toString() );
+ }
+ catch (IOException e)
+ {
+ //should never really happen
+ Log.error( e );
+ }
+ return pw.toString();
+ }
+ catch (Exception e)
+ {
+ Log.error( "Invalid SAX Transformer: " + e.toString() );
+ throw new SAXException( "problem in SAX transform: " + e.toString() );
}
}
@@ -220,7 +252,8 @@
*/
public static String transform( String url,
String stylesheet_url )
- throws SAXException {
+ throws SAXException
+ {
return transform( url, stylesheet_url, null );
@@ -233,16 +266,20 @@
public static String transform( String url,
String stylesheet_url,
Map params )
- throws SAXException {
+ throws SAXException
+ {
//bring these URLs local if they happen to be remote
- InputSource in;
- InputSource style;
- try {
+ InputSource in;
+ InputSource style;
+ try
+ {
in = new InputSource( JetspeedDiskCache.getInstance().getEntry( url
).getReader() );
style = new InputSource( JetspeedDiskCache.getInstance().getEntry(
stylesheet_url ).getReader() );
- } catch (IOException e) {
+ }
+ catch (IOException e)
+ {
Log.error( e );
//at this point we can just use the original url and stylesheet_url so
this shouldn't be a problem
in = new InputSource( url );
@@ -266,178 +303,211 @@
public static String transform( InputSource content,
InputSource stylesheet,
Map params)
- throws SAXException {
+ 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) )
+ // Instantiate a TransformerFactory.
+ TransformerFactory tFactory = TransformerFactory.newInstance();
+ // Determine whether the TransformerFactory supports the use of SAXSource
+ // and SAXResult
+ if (!tFactory.getFeature(SAXTransformerFactory.FEATURE) )
{
Log.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);
+ 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 );
+ // 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);
+ //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 ) {}
+ // Reset the XMLReader's ContentHandler.
+ reader.setContentHandler(handler);
- final Transformer processor = handler.getTransformer();
+ // 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 )
+ 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) */ );
- }
+ {
+ 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();
+ 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 ) );
+ // 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
- Log.error( e );
+ try
+ {
+ pw.flush();
+ pw.close();
+ }
+ catch (IOException e)
+ {
+ //should never really happen
+ Log.error( e );
+ }
+ return pw.toString();
}
- return pw.toString();
- } catch (Exception e) {
- Log.error( "Invalid SAX Transformer: " + e.toString() );
- throw new SAXException( "problem in SAX transform: " + e.toString() );
+ catch (Exception e)
+ {
+ Log.error( "Invalid SAX Transformer: " + e.toString() );
+ throw new SAXException( "problem in SAX transform: " + e.toString() );
}
}
-
- public static Reader SAXTransform( String content,
- String stylesheet,
- 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) )
+ /**
+ * 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) )
{
Log.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);
+ 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.
- final InputSource xstyle = new InputSource( JetspeedDiskCache.getInstance()
- .getEntry( stylesheet
).getReader() );
- 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);
+ // Parse the stylesheet.
+ final InputSource xstyle = new InputSource(
JetspeedDiskCache.getInstance()
+ .getEntry( stylesheet_url
).getReader() );
+ 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);
+ // 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 ) {}
+ // 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 )
+ 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);
- processor.setParameter(name,
- value ); //FIXME: maybe we need to
quote again...
- // was processor.createXString(
value)
- }
+ {
+ 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 {
+ PipedInputStream pis = new PipedInputStream();
+ PipedOutputStream pos = new PipedOutputStream( pis );
+ try
+ {
- final Writer pw = new OutputStreamWriter( pos, "utf-8" );
+ final Writer pw = new OutputStreamWriter( pos, "utf-8" );
-
-
- final SAXSource xinput = new SAXSource( new InputSource(
JetspeedDiskCache.getInstance().getEntry( content ).getReader() ) );
- 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".
- Log.debug("Starting SAX thread...");
- try {
- processor.transform( xinput,
- new StreamResult( pw ) );
- pw.close();
- Log.debug("...ending SAX thread.");
- } catch( Exception se) {
- Log.debug("Error in SAXTransform");
- Log.debug( se.toString() );
- }
- }
- } );
- t.start();
- } catch (java.io.UnsupportedEncodingException uee) {
- Log.debug("Need utf-8 encoding to SAXTransform");
+ final SAXSource xinput = new SAXSource( new InputSource(
JetspeedDiskCache.getInstance().getEntry( content_url ).getReader() ) );
+ //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".
+ Log.debug("Starting SAX thread...");
+ try
+ {
+ processor.transform( xinput,
+ new StreamResult( pw ) );
+ pw.close();
+ Log.debug("...ending SAX thread.");
+ }
+ catch( Exception se)
+ {
+ Log.debug("Error in SAXTransform");
+ Log.debug( se.toString() );
+ }
+ }
+ } );
+ t.start();
+ }
+ catch (java.io.UnsupportedEncodingException uee)
+ {
+ Log.debug("Need utf-8 encoding to SAXTransform");
}
- return new InputStreamReader ( pis, "utf-8" );
- } catch (Exception e) {
- Log.error( "Invalid SAX Transformer:" + e.toString() );
- throw new IOException( "problem in SAX transform: " + e.toString() );
+ return new InputStreamReader ( pis, "utf-8" );
+ }
+ catch (Exception e)
+ {
+ Log.error( "Invalid SAX Transformer:" + e.toString() );
+ throw new IOException( "problem in SAX transform: " + e.toString() );
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]