cziegeler 02/02/07 05:10:41 Modified: src/java/org/apache/cocoon/components/xslt XSLTProcessorImpl.java Log: PP of code Revision Changes Path 1.12 +279 -285 xml-cocoon2/src/java/org/apache/cocoon/components/xslt/XSLTProcessorImpl.java Index: XSLTProcessorImpl.java =================================================================== RCS file: /home/cvs/xml-cocoon2/src/java/org/apache/cocoon/components/xslt/XSLTProcessorImpl.java,v retrieving revision 1.11 retrieving revision 1.12 diff -u -r1.11 -r1.12 --- XSLTProcessorImpl.java 4 Feb 2002 12:36:29 -0000 1.11 +++ XSLTProcessorImpl.java 7 Feb 2002 13:10:41 -0000 1.12 @@ -120,32 +120,36 @@ * (<code>TransformerFactory.newInstance()</code>). * * @author <a href="mailto:[EMAIL PROTECTED]">Ovidiu Predescu</a> - * @version CVS $Id: XSLTProcessorImpl.java,v 1.11 2002/02/04 12:36:29 cziegeler Exp $ + * @version CVS $Id: XSLTProcessorImpl.java,v 1.12 2002/02/07 13:10:41 cziegeler Exp $ * @version 1.0 * @since July 11, 2001 */ public class XSLTProcessorImpl extends AbstractLoggable - implements XSLTProcessor, Composable, Disposable, Parameterizable, URIResolver, Component -{ - protected ComponentManager manager; + implements XSLTProcessor, + Composable, + Disposable, + Parameterizable, + URIResolver { - /** The store service instance */ - Store store; + protected ComponentManager manager; - /** The trax TransformerFactory */ - SAXTransformerFactory tfactory; + /** The store service instance */ + protected Store store; - /** The factory class used to create tfactory */ - Class tfactoryClass; + /** The trax TransformerFactory */ + protected SAXTransformerFactory tfactory; - /** Is the store turned on? (default is on) */ - boolean useStore = true; + /** The factory class used to create tfactory */ + protected Class tfactoryClass; - /** Is incremental processing turned on? (default for Xalan: no) */ - boolean incrementalProcessing = false; + /** Is the store turned on? (default is on) */ + protected boolean useStore = true; - SourceResolver resolver; + /** Is incremental processing turned on? (default for Xalan: no) */ + protected boolean incrementalProcessing = false; + + protected SourceResolver resolver; /** * Compose. Try to get the store @@ -153,8 +157,8 @@ public void compose(ComponentManager manager) throws ComponentException { this.manager = manager; - getLogger().debug("XSLTProcessorImpl component initialized."); - store = (Store)manager.lookup(Store.ROLE); + this.getLogger().debug("XSLTProcessorImpl component initialized."); + this.store = (Store)manager.lookup(Store.ROLE); } /** @@ -162,184 +166,177 @@ */ public void dispose() { if (this.manager != null) { - this.manager.release(store); + this.manager.release(this.store); } } /** * Configure the component */ - public void parameterize(Parameters params) - throws ParameterException - { - useStore = params.getParameterAsBoolean("use-store", true); - incrementalProcessing = params.getParameterAsBoolean("incremental-processing", false); - - String factoryName = params.getParameter("transformer-factory", null); - if (factoryName == null) { - // Will use default TRAX mechanism - this.tfactoryClass = null; - - } else { - // Will use specific class - getLogger().debug("Using factory " + factoryName); - try { - this.tfactoryClass = ClassUtils.loadClass(factoryName); - } catch(ClassNotFoundException cnfe) { - throw new ParameterException("Cannot load TransformerFactory class", cnfe); - } - - if (! TransformerFactory.class.isAssignableFrom(tfactoryClass)) { - throw new ParameterException("Class " + factoryName + " isn't a TransformerFactory"); - } - } - } - - public TransformerHandler getTransformerHandler(Source stylesheet) - throws ProcessingException - { - return getTransformerHandler(stylesheet, null); - } - - public TransformerHandler getTransformerHandler(Source stylesheet, - XMLFilter filter) - throws ProcessingException - { - try { - final String id = stylesheet.getSystemId(); - Templates templates = getTemplates(stylesheet, id); - if (templates == null) { - if (this.getLogger().isDebugEnabled()) { - getLogger().debug("Creating new Templates for " + id); - } - - // Create a Templates ContentHandler to handle parsing of the - // stylesheet. - TemplatesHandler templatesHandler - = getTransformerFactory().newTemplatesHandler(); - - if (filter != null) { - filter.setContentHandler(templatesHandler); + public void parameterize(Parameters params) + throws ParameterException { + this.useStore = params.getParameterAsBoolean("use-store", true); + this.incrementalProcessing = params.getParameterAsBoolean("incremental-processing", false); + + String factoryName = params.getParameter("transformer-factory", null); + if (factoryName == null) { + // Will use default TRAX mechanism + this.tfactoryClass = null; + } else { + // Will use specific class + getLogger().debug("Using factory " + factoryName); + try { + this.tfactoryClass = ClassUtils.loadClass(factoryName); + } catch(ClassNotFoundException cnfe) { + throw new ParameterException("Cannot load TransformerFactory class", cnfe); + } + + if (! TransformerFactory.class.isAssignableFrom(tfactoryClass)) { + throw new ParameterException("Class " + factoryName + " isn't a TransformerFactory"); + } } + } - if (this.getLogger().isDebugEnabled()) { - getLogger().debug("Source = " + stylesheet - + ", templatesHandler = " + templatesHandler); - } + public TransformerHandler getTransformerHandler(Source stylesheet) + throws ProcessingException { + return getTransformerHandler(stylesheet, null); + } - // Process the stylesheet. - stylesheet.toSAX(filter != null ? - (ContentHandler)filter : (ContentHandler)templatesHandler); - - // Get the Templates object (generated during the parsing of - // the stylesheet) from the TemplatesHandler. - templates = templatesHandler.getTemplates(); - putTemplates (templates, stylesheet, id); - } else { - if (this.getLogger().isDebugEnabled()) { - getLogger().debug("Reusing Templates for " + id); + public TransformerHandler getTransformerHandler(Source stylesheet, + XMLFilter filter) + throws ProcessingException { + try { + final String id = stylesheet.getSystemId(); + Templates templates = getTemplates(stylesheet, id); + if (templates == null) { + if (this.getLogger().isDebugEnabled()) { + getLogger().debug("Creating new Templates for " + id); + } + + // Create a Templates ContentHandler to handle parsing of the + // stylesheet. + TemplatesHandler templatesHandler + = getTransformerFactory().newTemplatesHandler(); + + if (filter != null) { + filter.setContentHandler(templatesHandler); + } + + if (this.getLogger().isDebugEnabled()) { + getLogger().debug("Source = " + stylesheet + + ", templatesHandler = " + templatesHandler); + } + + // Process the stylesheet. + stylesheet.toSAX(filter != null ? + (ContentHandler)filter : (ContentHandler)templatesHandler); + + // Get the Templates object (generated during the parsing of + // the stylesheet) from the TemplatesHandler. + templates = templatesHandler.getTemplates(); + putTemplates (templates, stylesheet, id); + } else { + if (this.getLogger().isDebugEnabled()) { + getLogger().debug("Reusing Templates for " + id); + } + } + + TransformerHandler handler = getTransformerFactory().newTransformerHandler(templates); + + /* (VG) + From http://java.sun.com/j2se/1.4/docs/api/javax/xml/transform/TransformerFactory.html#newTemplates(javax.xml.transform.Source) + Or http://xml.apache.org/xalan-j/apidocs/javax/xml/transform/TransformerFactory.html#newTemplates(javax.xml.transform.Source) + + "Returns: Templates object capable of being used for transformation + purposes, never null." + if (handler == null) { + if (this.getLogger().isDebugEnabled()) { + getLogger().debug("Re-creating new Templates for " + id); + } + + templates = getTransformerFactory().newTemplates(new SAXSource(stylesheet.getInputSource())); + putTemplates (templates, stylesheet, id); + handler = getTransformerFactory().newTransformerHandler(templates); + } + */ + + handler.getTransformer().setErrorListener(new TraxErrorHandler(getLogger())); + return handler; + } catch (ProcessingException e) { + throw e; + } catch (SAXException e) { + getLogger().debug("Got SAXException. Rethrowing cause exception.", e); + throw new ProcessingException("Exception in creating Transform Handler", e.getException()); + } catch (Exception e) { + throw new ProcessingException("Exception in creating Transform Handler", e); } - } - - TransformerHandler handler - = getTransformerFactory().newTransformerHandler(templates); + } - /* (VG) - From http://java.sun.com/j2se/1.4/docs/api/javax/xml/transform/TransformerFactory.html#newTemplates(javax.xml.transform.Source) - Or http://xml.apache.org/xalan-j/apidocs/javax/xml/transform/TransformerFactory.html#newTemplates(javax.xml.transform.Source) - - "Returns: Templates object capable of being used for transformation - purposes, never null." - if (handler == null) { - if (this.getLogger().isDebugEnabled()) { - getLogger().debug("Re-creating new Templates for " + id); + public void transform(Source source, + Source stylesheet, + Parameters params, + Result result) + throws ProcessingException { + try { + if (this.getLogger().isDebugEnabled()) { + getLogger().debug("XSLTProcessorImpl: transform source = " + source + + ", stylesheet = " + stylesheet + + ", parameters = " + params + + ", result = " + result); + } + TransformerHandler handler = getTransformerHandler(stylesheet); + Transformer transformer = handler.getTransformer(); + + if (params != null) { + transformer.clearParameters(); + String[] names = params.getNames(); + for (int i = names.length -1 ; i >= 0; i--) { + transformer.setParameter(names[i], params.getParameter(names[i])); + } + } + InputSource is = source.getInputSource(); + InputStream bs = is.getByteStream(); + this.getLogger().debug("XSLTProcessorImpl: starting transform"); + // FIXME (VG): Is it possible to use Source's toSAX method? + try { + transformer.transform(new StreamSource(bs, is.getSystemId()), result); + } finally { + bs.close(); + } + this.getLogger().debug("XSLTProcessorImpl: transform done"); + } catch (Exception e) { + throw new ProcessingException("Error in running Transformation", e); } + } - templates = getTransformerFactory().newTemplates(new SAXSource(stylesheet.getInputSource())); - putTemplates (templates, stylesheet, id); - handler = getTransformerFactory().newTransformerHandler(templates); - } - */ - - handler.getTransformer() - .setErrorListener(new TraxErrorHandler(getLogger())); - return handler; - } catch (ProcessingException e) { - throw e; - } catch (SAXException e) { - getLogger().debug("Got SAXException. Rethrowing cause exception.", e); - throw new ProcessingException("Exception in creating Transform Handler", e.getException()); - } catch (Exception e) { - throw new ProcessingException("Exception in creating Transform Handler", e); - } - } - - public void transform(Source source, Source stylesheet, Parameters params, - Result result) - throws ProcessingException - { - try { - if (this.getLogger().isDebugEnabled()) { - getLogger().debug("XSLTProcessorImpl: transform source = " + source - + ", stylesheet = " + stylesheet - + ", parameters = " + params - + ", result = " + result); - } - TransformerHandler handler = getTransformerHandler(stylesheet); - Transformer transformer = handler.getTransformer(); - - if (params != null) { - transformer.clearParameters(); - String[] names = params.getNames(); - for (int i = names.length -1 ; i >= 0; i--) { - transformer.setParameter(names[i], params.getParameter(names[i])); - } - } - InputSource is = source.getInputSource(); - InputStream bs = is.getByteStream(); - getLogger().debug("XSLTProcessorImpl: starting transform"); - // FIXME (VG): Is it possible to use Source's toSAX method? - try { - transformer.transform(new StreamSource(bs, is.getSystemId()), - result); - } finally { - bs.close(); - } - getLogger().debug("XSLTProcessorImpl: transform done"); - } catch (Exception e) { - throw new ProcessingException("Error in running Transformation", e); - } - } - - /** - * Helper for TransformerFactory. - */ - private SAXTransformerFactory getTransformerFactory() throws Exception - { + /** + * Helper for TransformerFactory. + */ + private SAXTransformerFactory getTransformerFactory() throws Exception + { if(tfactory == null) { - if (tfactoryClass == null) { - tfactory = (SAXTransformerFactory)TransformerFactory.newInstance(); - } else { - tfactory = (SAXTransformerFactory)tfactoryClass.newInstance(); - } - tfactory.setErrorListener(new TraxErrorHandler(getLogger())); - tfactory.setURIResolver(this); - // TODO: If we will support this feature with a different - // transformer than Xalan we'll have to set that corresponding - // feature - if (tfactory.getClass().getName().equals("org.apache.xalan.processor.TransformerFactoryImpl")) { - tfactory.setAttribute("http://xml.apache.org/xalan/features/incremental", - new Boolean (incrementalProcessing)); - } + if (tfactoryClass == null) { + tfactory = (SAXTransformerFactory)TransformerFactory.newInstance(); + } else { + tfactory = (SAXTransformerFactory)tfactoryClass.newInstance(); + } + tfactory.setErrorListener(new TraxErrorHandler(getLogger())); + tfactory.setURIResolver(this); + // TODO: If we will support this feature with a different + // transformer than Xalan we'll have to set that corresponding + // feature + if (tfactory.getClass().getName().equals("org.apache.xalan.processor.TransformerFactoryImpl")) { + tfactory.setAttribute("http://xml.apache.org/xalan/features/incremental", + new Boolean (incrementalProcessing)); + } } return tfactory; - } + } - private Templates getTemplates(Source stylesheet, String id) - throws IOException, ProcessingException - { + private Templates getTemplates(Source stylesheet, String id) + throws IOException, ProcessingException { if (!useStore) - return null; + return null; getLogger().debug("XSLTProcessorImpl getTemplates: stylesheet " + id); @@ -347,120 +344,117 @@ // only stylesheets with a last modification date are stored if (stylesheet.getLastModified() != 0) { - // Stored is an array of the template and the caching time - if (store.containsKey(id)) { - Object[] templateAndTime = (Object[])store.get(id); - - if(templateAndTime != null && templateAndTime[1] != null) { - long storedTime = ((Long)templateAndTime[1]).longValue(); - - if (storedTime < stylesheet.getLastModified()) { - store.remove(id); - } else { - templates = (Templates)templateAndTime[0]; - } - } - } + // Stored is an array of the template and the caching time + if (store.containsKey(id)) { + Object[] templateAndTime = (Object[])store.get(id); + + if(templateAndTime != null && templateAndTime[1] != null) { + long storedTime = ((Long)templateAndTime[1]).longValue(); + + if (storedTime < stylesheet.getLastModified()) { + store.remove(id); + } else { + templates = (Templates)templateAndTime[0]; + } + } + } } else if (store.containsKey(id)) { - // remove an old template if it exists - store.remove(id); + // remove an old template if it exists + store.remove(id); } return templates; - } + } - private void putTemplates (Templates templates, Source stylesheet, String id) - throws IOException, ProcessingException - { - if (!useStore) - return; + private void putTemplates (Templates templates, Source stylesheet, String id) + throws IOException, ProcessingException { + if (!useStore) + return; + + // only stylesheets with a last modification date are stored + if (stylesheet.getLastModified() != 0) { + + // Stored is an array of the template and the current time + Object[] templateAndTime = new Object[2]; + templateAndTime[0] = templates; + templateAndTime[1] = new Long(stylesheet.getLastModified()); + store.hold(id, templateAndTime); + } + } - // only stylesheets with a last modification date are stored - if (stylesheet.getLastModified() != 0) { + /** + * Called by the processor when it encounters + * an xsl:include, xsl:import, or document() function. + * + * @param href An href attribute, which may be relative or absolute. + * @param base The base URI in effect when the href attribute + * was encountered. + * + * @return A Source object, or null if the href cannot be resolved, + * and the processor should try to resolve the URI itself. + * + * @throws TransformerException if an error occurs when trying to + * resolve the URI. + */ + public javax.xml.transform.Source resolve(String href, String base) + throws TransformerException { + if (this.getLogger().isDebugEnabled()) { + this.getLogger().debug("resolve(href = " + href + + ", base = " + base + "); resolver = " + resolver); + } - // Stored is an array of the template and the current time - Object[] templateAndTime = new Object[2]; - templateAndTime[0] = templates; - templateAndTime[1] = new Long(stylesheet.getLastModified()); - store.hold(id, templateAndTime); - } - } - - /** - * Called by the processor when it encounters - * an xsl:include, xsl:import, or document() function. - * - * @param href An href attribute, which may be relative or absolute. - * @param base The base URI in effect when the href attribute - * was encountered. - * - * @return A Source object, or null if the href cannot be resolved, - * and the processor should try to resolve the URI itself. - * - * @throws TransformerException if an error occurs when trying to - * resolve the URI. - */ - public javax.xml.transform.Source resolve(String href, String base) - throws TransformerException - { - if (this.getLogger().isDebugEnabled()) { - getLogger().debug("resolve(href = " + href - + ", base = " + base + "); resolver = " + resolver); - } - - Source xslSource = null; - try { - if (href.indexOf(":") > 1) { - xslSource = resolver.resolve(href); - } else { - // patch for a null pointer passed as base - if (base == null) - throw new IllegalArgumentException("Null pointer passed as base"); - - // is the base a file or a real url - if (!base.startsWith("file:")) { - int lastPathElementPos = base.lastIndexOf('/'); - if (lastPathElementPos == -1) { - // this should never occur as the base should - // always be protocol:/.... - return null; // we can't resolve this - } else { - xslSource = resolver.resolve(new StringBuffer(base.substring(0, lastPathElementPos)) - .append("/").append(href).toString()); - } - } else { - File parent = new File(base.substring(5)); - File parent2 = new File(parent.getParentFile(), href); - xslSource = resolver.resolve(parent2.toURL().toExternalForm()); + Source xslSource = null; + try { + if (href.indexOf(":") > 1) { + xslSource = resolver.resolve(href); + } else { + // patch for a null pointer passed as base + if (base == null) + throw new IllegalArgumentException("Null pointer passed as base"); + + // is the base a file or a real url + if (!base.startsWith("file:")) { + int lastPathElementPos = base.lastIndexOf('/'); + if (lastPathElementPos == -1) { + // this should never occur as the base should + // always be protocol:/.... + return null; // we can't resolve this + } else { + xslSource = resolver.resolve(new StringBuffer(base.substring(0, lastPathElementPos)) + .append("/").append(href).toString()); + } + } else { + File parent = new File(base.substring(5)); + File parent2 = new File(parent.getParentFile(), href); + xslSource = resolver.resolve(parent2.toURL().toExternalForm()); + } + } + + InputSource is = xslSource.getInputSource(); + if (this.getLogger().isDebugEnabled()) { + getLogger().debug("xslSource = " + xslSource + + ", system id = " + is.getSystemId()); + } + + return new StreamSource(is.getByteStream(), is.getSystemId()); + + } catch (ResourceNotFoundException rnfe) { + // to obtain the same behaviour as when the resource is + // transformed by the XSLT Transformer we should return null here. + return null; + } catch (java.net.MalformedURLException mue) { + return null; + } catch (IOException ioe) { + return null; + } catch (SAXException se) { + throw new TransformerException(se); + } catch (ProcessingException pe) { + throw new TransformerException(pe); + } finally { + if (xslSource != null) xslSource.recycle(); } - } + } - InputSource is = xslSource.getInputSource(); - if (this.getLogger().isDebugEnabled()) { - getLogger().debug("xslSource = " + xslSource - + ", system id = " + is.getSystemId()); - } - - return new StreamSource(is.getByteStream(), is.getSystemId()); - - } catch (ResourceNotFoundException rnfe) { - // to obtain the same behaviour as when the resource is - // transformed by the XSLT Transformer we should return null here. - return null; - } catch (java.net.MalformedURLException mue) { - return null; - } catch (IOException ioe) { - return null; - } catch (SAXException se) { - throw new TransformerException(se); - } catch (ProcessingException pe) { - throw new TransformerException(pe); - } finally { - if (xslSource != null) xslSource.recycle(); - } - } - - public void setSourceResolver(SourceResolver resolver) - { - this.resolver = resolver; - } + public void setSourceResolver(SourceResolver resolver) { + this.resolver = resolver; + } }
---------------------------------------------------------------------- In case of troubles, e-mail: [EMAIL PROTECTED] To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]