morten 01/07/17 08:34:22 Modified: java/src/org/apache/xalan/xsltc/dom DOMImpl.java java/src/org/apache/xalan/xsltc/trax TemplatesImpl.java TransformerFactoryImpl.java TransformerImpl.java Log: Added some javadoc comments to our trax stuff. Added preliminary support for getting/setting output properties, and for getting/setting URL resolvers. PR: n/a Obtained from: n/a Submitted by: [EMAIL PROTECTED] Reviewed by: [EMAIL PROTECTED] Revision Changes Path 1.12 +11 -8 xml-xalan/java/src/org/apache/xalan/xsltc/dom/DOMImpl.java Index: DOMImpl.java =================================================================== RCS file: /home/cvs/xml-xalan/java/src/org/apache/xalan/xsltc/dom/DOMImpl.java,v retrieving revision 1.11 retrieving revision 1.12 diff -u -r1.11 -r1.12 --- DOMImpl.java 2001/06/12 13:22:05 1.11 +++ DOMImpl.java 2001/07/17 15:34:00 1.12 @@ -1,5 +1,5 @@ /* - * @(#)$Id: DOMImpl.java,v 1.11 2001/06/12 13:22:05 morten Exp $ + * @(#)$Id: DOMImpl.java,v 1.12 2001/07/17 15:34:00 morten Exp $ * * The Apache Software License, Version 1.1 * @@ -2282,12 +2282,17 @@ throws TransletException { final char[] text = _text; final int start = _offsetOrChild[node]; - int i = start + 1; - while (text[i] != ' ') ++i; - final int length = _lengthOrAttr[node]; - handler.processingInstruction(new String(text, start, i - start), - new String(text, i + 1, length - i)); + + // Target and Value are separated by a whitespace - find it! + int i = start; + while (text[i] != ' ') i++; + + final int len = i - start; + final String target = new String(text, start, len); + final String value = new String(text, i + 1, length - len); + + handler.processingInstruction(target, value); } /** @@ -2845,8 +2850,6 @@ final int node = nextNode(); _type[node] = PROCESSING_INSTRUCTION; linkChildren(node); - // This is really stupid - do NOT create String object here!!! - //characters(target + ' ' + data); characters(target); characters(" "); characters(data); 1.4 +17 -10 xml-xalan/java/src/org/apache/xalan/xsltc/trax/TemplatesImpl.java Index: TemplatesImpl.java =================================================================== RCS file: /home/cvs/xml-xalan/java/src/org/apache/xalan/xsltc/trax/TemplatesImpl.java,v retrieving revision 1.3 retrieving revision 1.4 diff -u -r1.3 -r1.4 --- TemplatesImpl.java 2001/07/13 10:32:20 1.3 +++ TemplatesImpl.java 2001/07/17 15:34:10 1.4 @@ -1,5 +1,5 @@ /* - * @(#)$Id: TemplatesImpl.java,v 1.3 2001/07/13 10:32:20 morten Exp $ + * @(#)$Id: TemplatesImpl.java,v 1.4 2001/07/17 15:34:10 morten Exp $ * * The Apache Software License, Version 1.1 * @@ -188,10 +188,12 @@ } /** - * JAXP interface implementation + * Implements JAXP's Templates.newTransformer() + * + * @throws TransformerConfigurationException */ - public Transformer newTransformer() throws - TransformerConfigurationException { + public Transformer newTransformer() + throws TransformerConfigurationException { Translet translet = getTransletInstance(); if (translet == null) throw new TransformerConfigurationException(TRANSLET_ERR_MSG); @@ -200,14 +202,19 @@ } /** - * JAXP interface implementation - UNFINISHED!!! + * Implements JAXP's Templates.getOutputProperties() */ public Properties getOutputProperties() { - // TODO - this method should extract the output properties stored in - // a translet instance. This could prove to be pretty tricky as this - // class has a reference to a translet class only and not to an - // actual instance of a translet - return new Properties(); + // We need to instanciate a translet to get the output settings, so + // we might as well just instanciate a Transformer and use its + // implementation of this method + try { + Transformer transformer = newTransformer(); + return transformer.getOutputProperties(); + } + catch (TransformerConfigurationException e) { + return null; + } } } 1.7 +316 -163 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.6 retrieving revision 1.7 diff -u -r1.6 -r1.7 --- TransformerFactoryImpl.java 2001/07/13 10:32:23 1.6 +++ TransformerFactoryImpl.java 2001/07/17 15:34:13 1.7 @@ -1,5 +1,5 @@ /* - * @(#)$Id: TransformerFactoryImpl.java,v 1.6 2001/07/13 10:32:23 morten Exp $ + * @(#)$Id: TransformerFactoryImpl.java,v 1.7 2001/07/17 15:34:13 morten Exp $ * * The Apache Software License, Version 1.1 * @@ -88,6 +88,7 @@ import java.io.File; import java.io.InputStream; +import java.io.ByteArrayInputStream; import java.net.URL; import java.net.MalformedURLException; @@ -95,164 +96,208 @@ /** * Implementation of a JAXP1.1 SAXTransformerFactory for Translets. */ -public class TransformerFactoryImpl extends TransformerFactory { - public TransformerFactoryImpl() { /* nothing yet */ } +public class TransformerFactoryImpl extends SAXTransformerFactory { - ////////////////////////////////////////////////////// - // SAXTransformerFactory (subclass of TransformerFactory) - // - public TemplatesHandler newTemplatesHandler() - throws TransformerConfigurationException - { - /*TBD*/ - throw new TransformerConfigurationException( - "TransformerFactoryImpl:newTemplatesHandler() " + - "not implemented yet."); - //return null; - } - public TransformerHandler newTransformerHandler() - throws TransformerConfigurationException - { - /*TBD*/ - throw new TransformerConfigurationException( - "TransformerFactoryImpl:newTransformerHandler() " + - "not implemented yet."); - // return null; - } - public TransformerHandler newTransformerHandler(Source src) - throws TransformerConfigurationException - { - /*TBD*/ - throw new TransformerConfigurationException( - "TransformerFactoryImpl:newTransformerHandler(Source) " + - "not implemented yet."); - // return null; - } - public TransformerHandler newTransformerHandler(Templates templates) - throws TransformerConfigurationException - { - /*TBD*/ - throw new TransformerConfigurationException( - "TransformerFactoryImpl:newTransformerHandler(Templates) " + - "not implemented yet."); - //return null; - } + // This constant should be removed once all abstract methods are impl'ed. + private static final String NYI = "Not yet implemented"; + // This error listener is used only for this factory and is not passed to + // the Templates or Transformer objects that we create!!! + private ErrorListener _errorListener = null; - /** - * Create an XMLFilter that uses the given source as the - * transformation instructions. - * - * @param src The source of the transformation instructions. - * - * @return An XMLFilter object, or null if this feature is not supported. - * - * @throws TransformerConfigurationException - */ - public XMLFilter newXMLFilter(Source src) - throws TransformerConfigurationException - { - Templates templates = newTemplates(src); - if (templates == null ) { - return null; - } - return newXMLFilter(templates); - } + // This URIResolver is passed to all created Templates and Transformers + private URIResolver _uriResolver = null; - public XMLFilter newXMLFilter(Templates templates) - throws TransformerConfigurationException - { - try { - return new org.apache.xalan.xsltc.trax.TrAXFilter(templates); - } catch( TransformerConfigurationException ex ) { - if( _errorListener != null) { - try { - _errorListener.fatalError( ex ); - return null; - } catch( TransformerException ex1 ) { - new TransformerConfigurationException(ex1); - } - } - throw ex; - } - } - // - // End SAXTransformerFactory methods - ////////////////////////////////////////////////////// - - ////////////////////////////////////////////////////// - // TransformerFactory - // - public ErrorListener getErrorListener() { - return _errorListener; + // Cache for the newTransformer() method - see method for details + private Transformer _copyTransformer = null; + private static final String COPY_TRANSLET_NAME = "GregorSamsa"; + private static final String COPY_TRANSLET_CODE = + "<xsl:stylesheet xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\">"+ + "<xsl:template match=\"/\"><xsl:copy-of select=\".\"/></xsl:template>"+ + "</xsl:stylesheet>"; + + // All used error messages should be listed here + private static final String ERROR_LISTENER_NULL = + "Attempting to set ErrorListener for TransformerFactory to null"; + private static final String SOURCE_NOT_SUPPORTED = + "Only StreamSource is supported by XSLTC"; + private static final String STREAM_SOURCE_ERROR = + "StreamSource must have a system id or be an InputStream"; + private static final String COMPILATION_ERROR = + "Could not compile stylesheet"; + + + /** + * javax.xml.transform.sax.TransformerFactory implementation. + * Contains nothing yet + */ + public TransformerFactoryImpl() { + // Don't need anything here so far... } + /** + * javax.xml.transform.sax.TransformerFactory implementation. + * Set the error event listener for the TransformerFactory, which is used + * for the processing of transformation instructions, and not for the + * transformation itself. + * + * @param listener The error listener to use with the TransformerFactory + * @throws IllegalArgumentException + */ public void setErrorListener(ErrorListener listener) - throws IllegalArgumentException - { - if (listener == null) { - throw new IllegalArgumentException( - "Error: setErrorListener() call where ErrorListener is null"); - } + throws IllegalArgumentException { + if (listener == null) + throw new IllegalArgumentException(ERROR_LISTENER_NULL); _errorListener = listener; } + /** + * javax.xml.transform.sax.TransformerFactory implementation. + * Get the error event handler for the TransformerFactory. + * + * @return The error listener used with the TransformerFactory + */ + public ErrorListener getErrorListener() { + return _errorListener; + } + + /** + * javax.xml.transform.sax.TransformerFactory implementation. + * Returns the value set for a TransformerFactory attribute + * We are currently not using any attributes with XSLTC. We should try to + * avoid using attributes as this can make us less flexible. + * + * @param name The attribute name + * @return An object representing the attribute value + * @throws IllegalArgumentException + */ public Object getAttribute(String name) - throws IllegalArgumentException - { - /*TBD*/ - throw new IllegalArgumentException( - "TransformerFactoryImpl:getAttribute(String) " + - "not implemented yet."); - //return null; + throws IllegalArgumentException { + throw new IllegalArgumentException(NYI); } + + /** + * javax.xml.transform.sax.TransformerFactory implementation. + * Returns the value set for a TransformerFactory attribute + * We are currently not using any attributes with XSLTC. We should try to + * avoid using attributes as this can make us less flexible. + * + * @param name The attribute name + * @param value An object representing the attribute value + * @throws IllegalArgumentException + */ public void setAttribute(String name, Object value) - throws IllegalArgumentException - { - /*TBD*/ - throw new IllegalArgumentException( - "TransformerFactoryImpl:getAttribute(String) " + - "not implemented yet."); + throws IllegalArgumentException { + throw new IllegalArgumentException(NYI); } + + /** + * javax.xml.transform.sax.TransformerFactory implementation. + * Look up the value of a feature (to see if it is supported). + * This method must be updated as the various methods and features of this + * class are implemented. + * + * @param name The feature name + * @return 'true' if feature is supported, 'false' if not + */ public boolean getFeature(String name) { - if ((StreamSource.FEATURE == name) || - (StreamResult.FEATURE == name) || - (SAXTransformerFactory.FEATURE == name)) { + if (name.equals(StreamSource.FEATURE) || + name.equals(StreamResult.FEATURE) || + name.equals(SAXTransformerFactory.FEATURE)) { return true; - } else if ((StreamSource.FEATURE.equals(name)) - || (StreamResult.FEATURE.equals(name)) - || (SAXTransformerFactory.FEATURE.equals(name))) { - return true; - } else { - return false; } + return false; + } + + /** + * javax.xml.transform.sax.TransformerFactory implementation. + * Get the object that is used by default during the transformation to + * resolve URIs used in document(), xsl:import, or xsl:include. + * + * @return The URLResolver used for this TransformerFactory and all + * Templates and Transformer objects created using this factory + */ + public URIResolver getURIResolver() { + return(_uriResolver); } - public URIResolver getURIResolver() { /*TBD*/ return null; } - public void setURIResolver(URIResolver resolver) {/*TBD*/ } - public Source getAssociatedStylesheet(Source src, String media, - String title, String charset) throws TransformerConfigurationException - { - /*TBD*/ - throw new TransformerConfigurationException( - "TransformerFactoryImpl:getAssociatedStylesheet(Source,String," + - "String, String) not implemented yet."); - //return null; - } - public Transformer newTransformer() throws - TransformerConfigurationException - { - /*TBD*/ - throw new TransformerConfigurationException( - "TransformerFactoryImpl:newTransformer() " + - " not implemented yet."); - //return null; - } - // - // End TransformerFactory methods - ////////////////////////////////////////////////////// /** + * javax.xml.transform.sax.TransformerFactory implementation. + * Set the object that is used by default during the transformation to + * resolve URIs used in document(), xsl:import, or xsl:include. Note that + * this does not affect Templates and Transformers that are already + * created with this factory. + * + * @param resolver The URLResolver used for this TransformerFactory and all + * Templates and Transformer objects created using this factory + */ + public void setURIResolver(URIResolver resolver) { + _uriResolver = resolver; + } + + /** + * javax.xml.transform.sax.TransformerFactory implementation. + * Get the stylesheet specification(s) associated via the xml-stylesheet + * processing instruction (see http://www.w3.org/TR/xml-stylesheet/) with + * the document document specified in the source parameter, and that match + * the given criteria. + * + * @param source The XML source document. + * @param media The media attribute to be matched. May be null, in which + * case the prefered templates will be used (i.e. alternate = no). + * @param title The value of the title attribute to match. May be null. + * @param charset The value of the charset attribute to match. May be null. + * @return A Source object suitable for passing to the TransformerFactory. + * @throws TransformerConfigurationException + */ + public Source getAssociatedStylesheet(Source source, String media, + String title, String charset) + throws TransformerConfigurationException { + throw new TransformerConfigurationException(NYI); + } + + /** + * javax.xml.transform.sax.TransformerFactory implementation. + * Create a Transformer object that copies the input document to the result. + * + * @return A Transformer object that simply copies the source to the result. + * @throws TransformerConfigurationException + */ + public Transformer newTransformer() + throws TransformerConfigurationException { + + if (_copyTransformer != null) return _copyTransformer; + + byte[][] bytecodes = null; // The translet classes go in here + + XSLTC xsltc = new XSLTC(); + xsltc.init(); + + // Compile the default copy-stylesheet + byte[] bytes = COPY_TRANSLET_CODE.getBytes(); + ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes); + bytecodes = xsltc.compile(inputStream, COPY_TRANSLET_NAME, 77); + + // Check that the transformation went well before returning + if (bytecodes == null) { + throw new TransformerConfigurationException(COMPILATION_ERROR); + } + + // Create a Transformer object and store for other calls + Templates templates = new TemplatesImpl(bytecodes, COPY_TRANSLET_NAME); + _copyTransformer = templates.newTransformer(); + return(_copyTransformer); + } + + /** + * javax.xml.transform.sax.TransformerFactory implementation. + * Process the Source into a Templates object, which is a a compiled + * representation of the source. Note that this method should not be + * used with XSLTC, as the time-consuming compilation is done for each + * and every transformation. * + * @return A Templates object that can be used to create Transformers. + * @throws TransformerConfigurationException */ public Transformer newTransformer(Source source) throws TransformerConfigurationException { @@ -261,29 +306,28 @@ } /** + * javax.xml.transform.sax.TransformerFactory implementation. + * 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!!! + * @return A Templates object that can be used to create Transformers. + * @throws TransformerConfigurationException */ - public Templates newTemplates(Source stylesheet) throws - TransformerConfigurationException { + public Templates newTemplates(Source stylesheet) + throws TransformerConfigurationException { + + byte[][] bytecodes = null; // The translet classes go in here XSLTC xsltc = new XSLTC(); xsltc.init(); - - // Check if destination has been set with system property - // TODO: We probably have to change this. - // Xalan might already have a property defined for this - String transletDestDir = System.getProperty("transletPool"); - if (transletDestDir != null) { - xsltc.setDestDirectory(transletDestDir); - } - - // Get down to business: Compile the stylesheet - InputStream inputStream = ((StreamSource)stylesheet).getInputStream(); - String stylesheetName = stylesheet.getSystemId(); - URL url = null; - // Attempt to get a decent name for the translet... + // Attempt using the URL returned by the Source's getSystemId() method + final String stylesheetName = stylesheet.getSystemId(); String transletName = "undefined"; + URL url = null; if (stylesheetName != null) { final String base = Util.baseName(stylesheetName); final String noext = Util.noExtName(base); @@ -297,19 +341,128 @@ } catch (MalformedURLException e) { url = null; } } - - byte[][] bytecodes = null; - if (url != null) + // Now do the actual compilation - store results in the bytecodes array + if (url != null) { bytecodes = xsltc.compile(url, transletName); - else if (inputStream != null) - bytecodes = xsltc.compile(inputStream, transletName, 77); - else - throw new TransformerConfigurationException( - "Stylesheet must have a system id or be an InputStream."); + } + else if (stylesheet instanceof StreamSource) { + StreamSource streamSource = (StreamSource)stylesheet; + InputStream inputStream = streamSource.getInputStream(); + if (inputStream != null) + bytecodes = xsltc.compile(inputStream, transletName, 77); + } + else { + throw new TransformerConfigurationException(STREAM_SOURCE_ERROR); + } + + // Check that the transformation went well before returning + if (bytecodes == null) { + throw new TransformerConfigurationException(COMPILATION_ERROR); + } return(new TemplatesImpl(bytecodes, transletName)); } - private ErrorListener _errorListener = null; + /** + * javax.xml.transform.sax.SAXTransformerFactory implementation. + * Get a TemplatesHandler object that can process SAX ContentHandler + * events into a Templates object. + * + * @return A TemplatesHandler object that can handle SAX events + * @throws TransformerConfigurationException + */ + public TemplatesHandler newTemplatesHandler() + throws TransformerConfigurationException { + throw new TransformerConfigurationException(NYI); + } + + /** + * javax.xml.transform.sax.SAXTransformerFactory implementation. + * Get a TransformerHandler object that can process SAX ContentHandler + * events into a Result. + * + * @return A TransformerHandler object that can handle SAX events + * @throws TransformerConfigurationException + */ + public TransformerHandler newTransformerHandler() + throws TransformerConfigurationException { + throw new TransformerConfigurationException(NYI); + } + + /** + * javax.xml.transform.sax.SAXTransformerFactory implementation. + * Get a TransformerHandler object that can process SAX ContentHandler + * events into a Result, based on the transformation instructions + * specified by the argument. + * + * @param src The source of the transformation instructions. + * @return A TransformerHandler object that can handle SAX events + * @throws TransformerConfigurationException + */ + public TransformerHandler newTransformerHandler(Source src) + throws TransformerConfigurationException { + throw new TransformerConfigurationException(NYI); + } + + /** + * javax.xml.transform.sax.SAXTransformerFactory implementation. + * Get a TransformerHandler object that can process SAX ContentHandler + * events into a Result, based on the transformation instructions + * specified by the argument. + * + * @param templates Represents a pre-processed stylesheet + * @return A TransformerHandler object that can handle SAX events + * @throws TransformerConfigurationException + */ + public TransformerHandler newTransformerHandler(Templates templates) + throws TransformerConfigurationException { + throw new TransformerConfigurationException(NYI); + } + + + /** + * javax.xml.transform.sax.SAXTransformerFactory implementation. + * Create an XMLFilter that uses the given source as the + * transformation instructions. + * + * @param src The source of the transformation instructions. + * @return An XMLFilter object, or null if this feature is not supported. + * @throws TransformerConfigurationException + */ + public XMLFilter newXMLFilter(Source src) + throws TransformerConfigurationException { + Templates templates = newTemplates(src); + if (templates == null ) return null; + return newXMLFilter(templates); + } + + /** + * javax.xml.transform.sax.SAXTransformerFactory implementation. + * Create an XMLFilter that uses the given source as the + * transformation instructions. + * + * @param src The source of the transformation instructions. + * @return An XMLFilter object, or null if this feature is not supported. + * @throws TransformerConfigurationException + */ + public XMLFilter newXMLFilter(Templates templates) + throws TransformerConfigurationException { + try { + return new org.apache.xalan.xsltc.trax.TrAXFilter(templates); + } + catch(TransformerConfigurationException e1) { + if(_errorListener != null) { + try { + _errorListener.fatalError(e1); + return null; + } + catch( TransformerException e2) { + new TransformerConfigurationException(e2); + } + } + throw e1; + } + } + } 1.4 +187 -51 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.3 retrieving revision 1.4 diff -u -r1.3 -r1.4 --- TransformerImpl.java 2001/07/13 10:32:25 1.3 +++ TransformerImpl.java 2001/07/17 15:34:14 1.4 @@ -1,5 +1,5 @@ /* - * @(#)$Id: TransformerImpl.java,v 1.3 2001/07/13 10:32:25 morten Exp $ + * @(#)$Id: TransformerImpl.java,v 1.4 2001/07/17 15:34:14 morten Exp $ * * The Apache Software License, Version 1.1 * @@ -65,20 +65,24 @@ import java.io.File; import java.io.Writer; +import java.io.InputStream; import java.io.OutputStream; import java.io.FileOutputStream; import java.io.FileNotFoundException; import java.net.MalformedURLException; import java.net.UnknownHostException; import java.lang.IllegalArgumentException; +import java.util.Enumeration; import org.xml.sax.XMLReader; import org.xml.sax.ContentHandler; +import org.xml.sax.InputSource; 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 org.apache.xalan.xsltc.Translet; import org.apache.xalan.xsltc.TransletException; @@ -94,12 +98,26 @@ private String _encoding = null; private ContentHandler _handler = null; + private ErrorListener _errorListener = null; + private URIResolver _uriResolver = null; + private Properties _properties = null; + + // Used for default output property settings + private final static String EMPTY_STRING = ""; + private final static String NO_STRING = "no"; + private final static String YES_STRING = "yes"; + private final static String XML_STRING = "xml"; + + // List all error messages here private final static String TRANSLET_ERR_MSG = "The transformer has no encapsulated translet object."; private final static String HANDLER_ERR_MSG = "No defined output handler for transformation result."; + private static final String ERROR_LISTENER_NULL = + "Attempting to set ErrorListener for Transformer to null"; /** + * Implements JAXP's Transformer constructor * Our Transformer objects always need a translet to do the actual work */ public TransformerImpl(Translet translet) { @@ -107,7 +125,11 @@ } /** - * JAXP interface implementation + * Implements JAXP's Transformer.transform() + * + * @param source Contains the input XML document + * @param result Will contain the output from the transformation + * @throws TransformerException */ public void transform(Source source, Result result) throws TransformerException { @@ -118,7 +140,7 @@ if (_handler == null) throw new TransformerException(HANDLER_ERR_MSG); // Run the transformation - transform(source.getSystemId(), _handler, _encoding); + transform(source, _handler, _encoding); } /** @@ -165,7 +187,7 @@ /** * Internal transformation method - uses the internal APIs of XSLTC */ - private void transform(String source, + private void transform(Source source, ContentHandler handler, String encoding) throws TransformerException { try { @@ -181,20 +203,34 @@ final DTDMonitor dtdMonitor = new DTDMonitor(); dtdMonitor.handleDTD(reader); - dom.setDocumentURI(source); - if (source.startsWith("file:/")) { - reader.parse(source); - } else { - reader.parse("file:"+(new File(source).getAbsolutePath())); + 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())); + } } - + else if (source instanceof StreamSource) { + InputStream stream = ((StreamSource)source).getInputStream(); + InputSource input = new InputSource(stream); + reader.parse(input); + } + else { + throw new TransformerException("Unsupported input."); + } + // Set size of key/id indices _translet.setIndexSize(dom.getSize()); // If there are any elements with ID attributes, build an index dtdMonitor.buildIdIndex(dom, 0, _translet); // Pass unparsed entity URIs to the translet _translet.setDTDMonitor(dtdMonitor); - + + // Pass output properties to the translet + setOutputProperties(_translet, _properties); + // Transform the document TextOutput textOutput = new TextOutput(handler, _encoding); _translet.transform(dom, textOutput); @@ -231,95 +267,183 @@ } } - // TrAX support methods, get/setErrorListener - private ErrorListener _errorListener = null; - /** - * Get the TrAX error listener + * Implements JAXP's Transformer.getErrorListener() + * Get the error event handler in effect for the transformation. + * + * @return The error event handler currently in effect */ public ErrorListener getErrorListener() { return _errorListener; } /** - * Set the TrAX error listener + * Implements JAXP's Transformer.setErrorListener() + * Set the error event listener in effect for the transformation. + * + * @param listener The error event listener to use + * @throws IllegalArgumentException */ public void setErrorListener(ErrorListener listener) throws IllegalArgumentException { - if (listener == null) { - throw new IllegalArgumentException( - "Error: setErrorListener() call where ErrorListener is null"); - } + if (listener == null) + throw new IllegalArgumentException(ERROR_LISTENER_NULL); _errorListener = listener; } /** * Inform TrAX error listener of an error */ - private void postErrorToListener(String msg) { + private void postErrorToListener(String message) { try { - _errorListener.error(new TransformerException( - "Translet Error: " + msg)); - } catch (TransformerException e) { - // TBD + _errorListener.error(new TransformerException(message)); + } + catch (TransformerException e) { + // ignored - transformation cannot be continued } } /** * Inform TrAX error listener of a warning */ - private void postWarningToListener(String msg) { + private void postWarningToListener(String message) { try { - _errorListener.warning(new TransformerException( - "Translet Warning: " + msg)); - } catch (TransformerException e) { - // TBD + _errorListener.warning(new TransformerException(message)); } + catch (TransformerException e) { + // ignored - transformation cannot be continued + } } /** * Implements JAXP's Transformer.getOutputProperties(). - * Returns a copy of the output properties for the transformation. + * Returns a copy of the output properties for the transformation. Note that + * this method will only return properties that were set in this class. + * The output settings defined in the stylesheet's <xsl:output> element + * and default XSLT output settings will not be returned by this method. + * + * @return Properties explicitly set for this Transformer */ - public Properties getOutputProperties() throws IllegalArgumentException { - // TODO - return(null); + public Properties getOutputProperties() { + return(_properties); } /** * Implements JAXP's Transformer.getOutputProperty(). - * Set an output property that will be in effect for the transformation. + * Get an output property that is in effect for the transformation. The + * property specified may be a property that was set with setOutputProperty, + * or it may be a property specified in the stylesheet. + * + * @param name A non-null string that contains the name of the property + * @throws IllegalArgumentException if the property name is not known */ public String getOutputProperty(String name) - throws IllegalArgumentException { - // TODO - return(null); + throws IllegalArgumentException { + + // First check if the property is overridden in this Transformer + if (_properties != null) { + String value = _properties.getProperty(name); + if (value != null) return value; + } + + // Then check if it is set in the translet + if (_translet != null) { + // TODO: get propertie value from translet + if (name.equals(OutputKeys.ENCODING)) { + return _encoding; + } + else if (name.equals(OutputKeys.METHOD)) { + + } + + } + + // Then return the default values + if (name.equals(OutputKeys.ENCODING)) + return _encoding; + else if (name.equals(OutputKeys.METHOD)) + return XML_STRING; + else if (name.equals(OutputKeys.INDENT)) + return NO_STRING; + else if (name.equals(OutputKeys.DOCTYPE_PUBLIC)) + return EMPTY_STRING; + else if (name.equals(OutputKeys.DOCTYPE_SYSTEM)) + return EMPTY_STRING; + else if (name.equals(OutputKeys.CDATA_SECTION_ELEMENTS)) + return EMPTY_STRING; + else if (name.equals(OutputKeys.MEDIA_TYPE)) + return "text/xml"; + else if (name.equals(OutputKeys.OMIT_XML_DECLARATION)) + return NO_STRING; + else if (name.equals(OutputKeys.STANDALONE)) + return NO_STRING; + else if (name.equals(OutputKeys.VERSION)) + return "1.0"; + + + return null; } /** * Implements JAXP's Transformer.setOutputProperties(). * Set the output properties for the transformation. These properties * will override properties set in the Templates with xsl:output. + * Unrecognised properties will be quitely ignored. + * + * @param properties The properties to use for the Transformer + * @throws IllegalArgumentException Never, errors are ignored */ - public void setOutputProperties(Properties props) + public void setOutputProperties(Properties properties) throws IllegalArgumentException { - // TODO + _properties = properties; } /** * Implements JAXP's Transformer.setOutputProperty(). * Get an output property that is in effect for the transformation. The - * property specified may be a property that was set with + * property specified may be a property that was set with * setOutputProperty(), or it may be a property specified in the stylesheet. + * + * @param name The name of the property to set + * @param value The value to assign to the property + * @throws IllegalArgumentException Never, errors are ignored */ public void setOutputProperty(String name, String value) - throws IllegalArgumentException { - // TODO + throws IllegalArgumentException { + if (_properties == null) _properties = new Properties(); + _properties.setProperty(name, value); + } + + /** + * Internal method to pass any properties to the translet prior to + * initiating the transformation + */ + private void setOutputProperties(AbstractTranslet translet, + Properties properties) { + // TODO - pass properties to the translet + Enumeration names = properties.propertyNames(); + while (names.hasMoreElements()) { + String name = (String)names.nextElement(); + if (name.equals(OutputKeys.ENCODING)) { + _encoding = (String)properties.getProperty(name); + } + else if (name.equals(OutputKeys.CDATA_SECTION_ELEMENTS)) { + // ignored - too late now, translet is already compiled + } + else if (name.equals(OutputKeys.METHOD)) { + + } + } } /** * Implements JAXP's Transformer.setParameter() - * Add a parameter for the transformation. + * Add a parameter for the transformation. The parameter is simply passed + * on to the translet - no validation is performed - so any unused + * parameters are quitely ignored by the translet. + * + * @param name The name of the parameter + * @param value The value to assign to the parameter */ public void setParameter(String name, Object value) { _translet.addParameter(name, value, false); @@ -327,7 +451,8 @@ /** * Implements JAXP's Transformer.clearParameters() - * Clears the parameter stack. + * Clear all parameters set with setParameter. Clears the translet's + * parameter stack. */ public void clearParameters() { _translet.clearParameters(); @@ -335,23 +460,34 @@ /** * Implements JAXP's Transformer.getParameter() - * Returns the value of a given parameter + * Returns the value of a given parameter. Note that the translet will not + * keep values for parameters that were not defined in the stylesheet. + * + * @param name The name of the parameter + * @return An object that contains the value assigned to the parameter */ public final Object getParameter(String name) { return(_translet.getParameter(name)); } /** - * These two methods need to pass the URI resolver to the dom/LoadDocument - * class, which again must use the URI resolver if present. + * Implements JAXP's Transformer.getURIResolver() + * Set the object currently used to resolve URIs used in document(). + * + * @returns The URLResolver object currently in use */ public URIResolver getURIResolver() { - // TODO - return null; + return _uriResolver; } + /** + * Implements JAXP's Transformer.setURIResolver() + * Set an object that will be used to resolve URIs used in document(). + * + * @param resolver The URIResolver to use in document() + */ public void setURIResolver(URIResolver resolver) { - // TODO + _uriResolver = resolver; } } --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]