morten 01/07/20 04:56:49
Modified: java/src/org/apache/xalan/xsltc/runtime
DefaultSAXOutputHandler.java
java/src/org/apache/xalan/xsltc/trax
TransformerFactoryImpl.java TransformerImpl.java
Log:
Added support for SAXSource and SAXResult in TransformerFactoryImpl
and TransformerFactory. I had to add a new consturctor the the
default SAX output handler (in the xsltc runtime library) to acommodate
the SAXResult TrAX output handler.
PR: n/a
Obtained from: n/a
Submitted by: [EMAIL PROTECTED]
Reviewed by: [EMAIL PROTECTED]
Revision Changes Path
1.8 +12 -4
xml-xalan/java/src/org/apache/xalan/xsltc/runtime/DefaultSAXOutputHandler.java
Index: DefaultSAXOutputHandler.java
===================================================================
RCS file:
/home/cvs/xml-xalan/java/src/org/apache/xalan/xsltc/runtime/DefaultSAXOutputHandler.java,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -r1.7 -r1.8
--- DefaultSAXOutputHandler.java 2001/07/18 15:36:01 1.7
+++ DefaultSAXOutputHandler.java 2001/07/20 11:56:48 1.8
@@ -1,5 +1,5 @@
/*
- * @(#)$Id: DefaultSAXOutputHandler.java,v 1.7 2001/07/18 15:36:01 morten Exp $
+ * @(#)$Id: DefaultSAXOutputHandler.java,v 1.8 2001/07/20 11:56:48 morten Exp $
*
* The Apache Software License, Version 1.1
*
@@ -123,12 +123,20 @@
private AttributeList _namespaceDeclarations = new AttributeList();
/**
- * Constructor - simple, initially for use in servlets
+ * Constructor - set Writer to send output to and output encoding
*/
- public DefaultSAXOutputHandler(Writer writer) throws IOException {
+ public DefaultSAXOutputHandler(Writer writer, String encoding)
+ throws IOException {
_writer = writer;
- _encoding = "utf-8";
+ _encoding = encoding;
init();
+ }
+
+ /**
+ * Constructor - simple, initially for use in servlets
+ */
+ public DefaultSAXOutputHandler(Writer writer) throws IOException {
+ this(writer, "utf-8");
}
/**
1.9 +36 -41
xml-xalan/java/src/org/apache/xalan/xsltc/trax/TransformerFactoryImpl.java
Index: TransformerFactoryImpl.java
===================================================================
RCS file:
/home/cvs/xml-xalan/java/src/org/apache/xalan/xsltc/trax/TransformerFactoryImpl.java,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -r1.8 -r1.9
--- TransformerFactoryImpl.java 2001/07/19 18:48:31 1.8
+++ TransformerFactoryImpl.java 2001/07/20 11:56:48 1.9
@@ -1,5 +1,5 @@
/*
- * @(#)$Id: TransformerFactoryImpl.java,v 1.8 2001/07/19 18:48:31 morten Exp $
+ * @(#)$Id: TransformerFactoryImpl.java,v 1.9 2001/07/20 11:56:48 morten Exp $
*
* The Apache Software License, Version 1.1
*
@@ -87,7 +87,7 @@
/**
* Implementation of a JAXP1.1 SAXTransformerFactory for Translets.
*/
-public class TransformerFactoryImpl extends SAXTransformerFactory {
+public class TransformerFactoryImpl extends TransformerFactory {
// This constant should be removed once all abstract methods are impl'ed.
private static final String NYI = "Not yet implemented";
@@ -111,13 +111,10 @@
private static final String ERROR_LISTENER_NULL =
"Attempting to set ErrorListener for TransformerFactory to null";
private static final String UNKNOWN_SOURCE_ERR =
- "Only StreamSource is supported by XSLTC";
+ "Only StreamSource and SAXSource is supported by XSLTC";
private static final String COMPILE_ERR =
"Could not compile stylesheet";
- private static final String SOURCE_CONTENTS_ERR =
- "The input Source contains an invalid system id";
-
/**
* javax.xml.transform.sax.TransformerFactory implementation.
* Contains nothing yet
@@ -192,11 +189,19 @@
* @return 'true' if feature is supported, 'false' if not
*/
public boolean getFeature(String name) {
- if (name.equals(StreamSource.FEATURE) ||
- name.equals(StreamResult.FEATURE) ||
- name.equals(SAXTransformerFactory.FEATURE)) {
- return true;
- }
+ // All supported features should be listed here
+ String[] features = {
+ SAXSource.FEATURE,
+ SAXResult.FEATURE,
+ StreamSource.FEATURE,
+ StreamResult.FEATURE
+ };
+
+ // Inefficient, but it really does not matter in a function like this
+ for (int i=0; i<features.length; i++)
+ if (name.equals(features[i])) return true;
+
+ // Feature not supported
return false;
}
@@ -345,9 +350,7 @@
* Process the Source into a Templates object, which is a a compiled
* representation of the source.
*
- * @param stylesheet The input stylesheet. Only StreamSource is suported
- * for now (otherwise the Source URI must be set using the setSystemId()
- * method). This methods needs more work!!!
+ * @param stylesheet The input stylesheet - DOMSource not supported!!!
* @return A Templates object that can be used to create Transformers.
* @throws TransformerConfigurationException
*/
@@ -360,47 +363,39 @@
// Create and initialize a stylesheet compiler
XSLTC xsltc = new XSLTC();
xsltc.init();
+
+ InputSource input = null;
+ final String systemId = source.getSystemId();
- // Handle SAXSource input
+ // Try to get InputSource from SAXSource input
if (source instanceof SAXSource) {
final SAXSource sax = (SAXSource)source;
- InputSource input = sax.getInputSource();
- String systemId = sax.getSystemId();
-
+ input = sax.getInputSource();
// Pass the SAX parser to the compiler
xsltc.setXMLReader(sax.getXMLReader());
-
- // Then pass the XSL stylesheet doc to the compiler
- if ((input == null) && (systemId != null))
- input = new InputSource(systemId);
- // Pass system id to InputSource just to be on the safe side
- input.setSystemId(systemId);
- // Compile the stylesheet
- bytecodes = xsltc.compile(null, input);
}
- // Handle StreamSource input
+ // Try to get InputStream or Reader from StreamSource
else if (source instanceof StreamSource) {
final StreamSource stream = (StreamSource)source;
- final InputStream input = stream.getInputStream();
+ final InputStream istream = stream.getInputStream();
final Reader reader = stream.getReader();
- final String systemId = stream.getSystemId();
-
- InputSource blob;
- if (input != null)
- blob = new InputSource(input);
+ // Create InputSource from Reader or InputStream in Source
+ if (istream != null)
+ input = new InputSource(istream);
else if (reader != null)
- blob = new InputSource(reader);
- else if (systemId != null)
- blob = new InputSource(systemId);
- else
- blob = null; // TODO - signal error!!!!
-
- blob.setSystemId(systemId);
- bytecodes = xsltc.compile(null, blob);
+ input = new InputSource(reader);
}
else {
throw new TransformerConfigurationException(UNKNOWN_SOURCE_ERR);
}
+
+ // Try to create an InputStream from the SystemId if no input so far
+ if (input == null) input = new InputSource(systemId);
+
+ // Pass system id to InputSource just to be on the safe side
+ input.setSystemId(systemId);
+ // Compile the stylesheet
+ bytecodes = xsltc.compile(null, input);
final String transletName = xsltc.getClassName();
1.7 +94 -50
xml-xalan/java/src/org/apache/xalan/xsltc/trax/TransformerImpl.java
Index: TransformerImpl.java
===================================================================
RCS file:
/home/cvs/xml-xalan/java/src/org/apache/xalan/xsltc/trax/TransformerImpl.java,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -r1.6 -r1.7
--- TransformerImpl.java 2001/07/19 18:48:31 1.6
+++ TransformerImpl.java 2001/07/20 11:56:48 1.7
@@ -1,5 +1,5 @@
/*
- * @(#)$Id: TransformerImpl.java,v 1.6 2001/07/19 18:48:31 morten Exp $
+ * @(#)$Id: TransformerImpl.java,v 1.7 2001/07/20 11:56:48 morten Exp $
*
* The Apache Software License, Version 1.1
*
@@ -63,14 +63,18 @@
package org.apache.xalan.xsltc.trax;
-import java.io.File;
import java.io.Writer;
+import java.io.Reader;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.FileOutputStream;
import java.io.FileNotFoundException;
+
+import java.net.URL;
+import java.net.URLConnection;
import java.net.MalformedURLException;
import java.net.UnknownHostException;
+
import java.lang.IllegalArgumentException;
import java.util.Enumeration;
import java.util.StringTokenizer;
@@ -82,8 +86,8 @@
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.transform.*;
-import javax.xml.transform.stream.StreamResult;
-import javax.xml.transform.stream.StreamSource;
+import javax.xml.transform.sax.*;
+import javax.xml.transform.stream.*;
import org.apache.xalan.xsltc.Translet;
import org.apache.xalan.xsltc.TransletException;
@@ -145,41 +149,59 @@
}
/**
- * Create an output handler, and get the requested output encoding
- * from the translet instance
+ * Create an output handler (SAX2 handler) for the transformation output
+ * based on the type and contents of the TrAX Result object passed to
+ * the transform() method. Only StreamResult and SAXResult are currently
+ * handled.
*/
private ContentHandler getOutputHandler(Result result)
throws TransformerException {
// Try to get the encoding from Translet (may not be set)
- _encoding = _translet._encoding;
- if (_encoding == null) _encoding = "UTF-8";
+ if (_translet._encoding != null)
+ _encoding = _translet._encoding;
+ else
+ _encoding = "utf-8"; // default output encoding
- StreamResult target = (StreamResult)result;
- Writer writer = target.getWriter();
- OutputStream ostream = target.getOutputStream();
- String systemid = target.getSystemId();
-
try {
- if (writer != null) {
- // no constructor that takes encoding yet...
- return (new DefaultSAXOutputHandler(writer));
- }
- else if (ostream != null) {
- return (new DefaultSAXOutputHandler(ostream, _encoding));
- }
- else if (systemid != null) {
- String filePrefix = new String("file:///");
- if (systemid.startsWith(filePrefix)) {
- systemid = systemid.substring(filePrefix.length());
- }
- ostream = (OutputStream)(new FileOutputStream(systemid));
+ final String systemId = result.getSystemId();
+
+ // Handle SAXResult output handler
+ if (result instanceof SAXResult) {
+ final SAXResult target = (SAXResult)result;
+ final ContentHandler handler = target.getHandler();
+ // Simple as feck, just pass the SAX handler back...
+ if (handler != null) return handler;
+ }
+ // Handle StreamResult output handler
+ else if (result instanceof StreamResult) {
+ final StreamResult target = (StreamResult)result;
+ final OutputStream ostream = target.getOutputStream();
+ final Writer writer = target.getWriter();
+
+ if (ostream != null)
+ return (new DefaultSAXOutputHandler(ostream, _encoding));
+ else if (writer != null)
+ return (new DefaultSAXOutputHandler(writer, _encoding));
+ }
+
+ // Common, final handling of all input sources, only used if the
+ // other contents of the Result object could not be used
+ if (systemId != null) {
+ final URL url = new URL(systemId);
+ final URLConnection connection = url.openConnection();
+ final OutputStream ostream = connection.getOutputStream();
return(new DefaultSAXOutputHandler(ostream, _encoding));
}
- return null;
+ else {
+ // Handle error!!!
+ return null;
+ }
}
- catch (java.io.FileNotFoundException e) {
+ // If we cannot write to the location specified by the SystemId
+ catch (java.net.UnknownServiceException e) {
throw new TransformerException(e);
}
+ // If we cannot create the file specified by the SystemId
catch (java.io.IOException e) {
throw new TransformerException(e);
}
@@ -189,34 +211,56 @@
* Internal transformation method - uses the internal APIs of XSLTC
*/
private void transform(Source source,
- ContentHandler handler,
+ ContentHandler outputHandler,
String encoding) throws TransformerException {
try {
- // Create a SAX parser and get the XMLReader object it uses
- final SAXParserFactory factory = SAXParserFactory.newInstance();
- final SAXParser parser = factory.newSAXParser();
- final XMLReader reader = parser.getXMLReader();
-
- // Set the DOM's DOM builder as the XMLReader's SAX2 content handler
+ // Create an internal DOM (not W3C) and get SAX2 input handler
final DOMImpl dom = new DOMImpl();
- reader.setContentHandler(dom.getBuilder());
- // Create a DTD monitor and pass it to the XMLReader object
+ final ContentHandler inputHandler = dom.getBuilder();
+
+ // Create a DTDMonitor that will trace all unparsed entity URIs
final DTDMonitor dtdMonitor = new DTDMonitor();
- dtdMonitor.handleDTD(reader);
-
- String url = source.getSystemId();
- if (url != null) {
- dom.setDocumentURI(url);
- if (url.startsWith("file:/")) {
- reader.parse(url);
- } else {
- reader.parse("file:"+(new File(url).getAbsolutePath()));
- }
+
+ // Handle SAXSource input
+ if (source instanceof SAXSource) {
+ // Get all info from the input SAXSource object
+ final SAXSource sax = (SAXSource)source;
+ final XMLReader reader = sax.getXMLReader();
+ final InputSource input = sax.getInputSource();
+ final String systemId = sax.getSystemId();
+ dtdMonitor.handleDTD(reader);
+
+ reader.setContentHandler(inputHandler);
+ reader.parse(input);
+ dom.setDocumentURI(systemId);
}
+ // Handle StreamSource input
else if (source instanceof StreamSource) {
- InputStream stream = ((StreamSource)source).getInputStream();
- InputSource input = new InputSource(stream);
+ // With a StreamSource we need to create our own parser
+ final SAXParserFactory factory = SAXParserFactory.newInstance();
+ final SAXParser parser = factory.newSAXParser();
+ final XMLReader reader = parser.getXMLReader();
+ dtdMonitor.handleDTD(reader);
+
+ // Get all info from the input StreamSource object
+ final StreamSource stream = (StreamSource)source;
+ final InputStream streamInput = stream.getInputStream();
+ final Reader streamReader = stream.getReader();
+ final String systemId = stream.getSystemId();
+
+ reader.setContentHandler(inputHandler);
+
+ InputSource input;
+ if (streamInput != null)
+ input = new InputSource(streamInput);
+ else if (streamReader != null)
+ input = new InputSource(streamReader);
+ else if (systemId != null)
+ input = new InputSource(systemId);
+ else
+ input = null; // TODO - signal error!!!!
reader.parse(input);
+ dom.setDocumentURI(systemId);
}
else {
throw new TransformerException("Unsupported input.");
@@ -233,7 +277,7 @@
setOutputProperties(_translet, _properties);
// Transform the document
- TextOutput textOutput = new TextOutput(handler, _encoding);
+ TextOutput textOutput = new TextOutput(outputHandler, _encoding);
_translet.transform(dom, textOutput);
}
catch (TransletException e) {
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]