org.apache.cocoon.components.xslt
public class: XSLTProcessorImpl [javadoc |
source]
java.lang.Object
org.apache.avalon.framework.logger.AbstractLogEnabled
org.apache.cocoon.components.xslt.XSLTProcessorImpl
All Implemented Interfaces:
URIResolver, org.apache.avalon.framework.activity.Disposable, org.apache.avalon.framework.parameters.Parameterizable, XSLTProcessor, org.apache.avalon.framework.component.Composable
Deprecated! Use - the avalon excalibur xslt processor instead.
This class defines the implementation of the
XSLTProcessor
component.
To configure it, add the following lines in the
cocoon.xconf file:
<xslt-processor class="org.apache.cocoon.components.xslt.XSLTProcessorImpl">
<parameter name="use-store" value="true"/>
<parameter name="transformer-factory" value="org.apache.xalan.processor.TransformerFactoryImpl"/>
</xslt-processor>
The <use-store> configuration forces the transformer to put the
Templates generated from the XSLT stylesheet into the
Store. This property is true by default.
The <transformer-factory> configuration tells the transformer to use a particular
implementation of javax.xml.transform.TransformerFactory. This allows to force
the use of a given TRAX implementation (e.g. xalan or saxon) if several are available in the
classpath. If this property is not set, the transformer uses the standard TRAX mechanism
(TransformerFactory.newInstance()).
- author:
< - a href="mailto:ovidiu@cup.hp.com">Ovidiu Predescu
- author:
< - a href="mailto:stefano@apache.org">Stefano Mazzocchi
- version:
CVS - $Id: XSLTProcessorImpl.java 540711 2007-05-22 19:36:07Z cziegeler $
- version:
1.0 -
- since:
July - 11, 2001
| Field Summary |
|---|
| protected ComponentManager | manager | |
| protected Store | store | The store service instance |
| protected HashMap | factories | The trax TransformerFactory lookup table |
| protected SAXTransformerFactory | factory | The trax TransformerFactory this component uses |
| protected boolean | useStore | Is the store turned on? (default is on) |
| protected boolean | incrementalProcessing | Is incremental processing turned on? (default for Xalan: no) |
| protected SourceResolver | resolver | The source resolver used by this processor |
| protected TraxErrorHandler | errorHandler | The error handler for the transformer |
| Method from org.apache.cocoon.components.xslt.XSLTProcessorImpl Detail: |
public void compose(ComponentManager manager) throws ComponentException {
this.manager = manager;
if (this.getLogger().isDebugEnabled())
this.getLogger().debug("XSLTProcessorImpl component initialized.");
this.store = (Store) manager.lookup(Store.TRANSIENT_STORE);
this.errorHandler = new TraxErrorHandler( this.getLogger() );
this.resolver = (SourceResolver) manager.lookup(SourceResolver.ROLE);
} Deprecated!Compose. Try to get the store |
public void dispose() {
if (this.manager != null) {
this.manager.release(this.store);
this.store = null;
this.manager.release((Component)this.resolver);
this.resolver = null;
}
this.errorHandler = null;
this.manager = null;
} Deprecated! |
public TransformerHandler getTransformerHandler(Source stylesheet) throws ProcessingException {
return this.getTransformerHandler(stylesheet, null);
} Deprecated! |
public TransformerHandler getTransformerHandler(Source stylesheet,
XMLFilter filter) throws ProcessingException {
try {
final String id = stylesheet.getSystemId();
Templates templates = this.getTemplates(stylesheet, id);
if (templates == null) {
if (this.getLogger().isDebugEnabled()) {
this.getLogger().debug("Creating new Templates for " + id);
}
// Create a Templates ContentHandler to handle parsing of the
// stylesheet.
TemplatesHandler templatesHandler = this.factory.newTemplatesHandler();
// Set the system ID for the template handler since some
// TrAX implementations (XSLTC) rely on this in order to obtain
// a meaningful identifier for the Templates instances.
templatesHandler.setSystemId(id);
if (filter != null) {
filter.setContentHandler(templatesHandler);
}
if (this.getLogger().isDebugEnabled()) {
this.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();
this.putTemplates (templates, stylesheet, id);
} else {
if (this.getLogger().isDebugEnabled()) {
this.getLogger().debug("Reusing Templates for " + id);
}
}
TransformerHandler handler = this.factory.newTransformerHandler(templates);
handler.getTransformer().setErrorListener(this.errorHandler);
handler.getTransformer().setURIResolver(this);
return handler;
} catch (ProcessingException e) {
throw e;
} catch (SAXException e) {
if (e.getException() == null) {
throw new ProcessingException("Exception in creating Transform Handler", e);
} else {
if (this.getLogger().isDebugEnabled())
this.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);
}
} Deprecated! |
public void parameterize(Parameters params) throws ParameterException {
this.useStore = params.getParameterAsBoolean("use-store", true);
this.incrementalProcessing = params.getParameterAsBoolean("incremental-processing", false);
this.factory = this.getTransformerFactory(params.getParameter("transformer-factory", DEFAULT_FACTORY));
} Deprecated! |
public Source resolve(String href,
String base) throws TransformerException {
if (this.getLogger().isDebugEnabled()) {
this.getLogger().debug("resolve(href = " + href +
", base = " + base + "); resolver = " + this.resolver);
}
Source xslSource = null;
try {
if (href.indexOf(":") > 1) {
xslSource = this.resolver.resolveURI(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 = this.resolver.resolveURI(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 = this.resolver.resolveURI(parent2.toURL().toExternalForm());
}
}
if (this.getLogger().isDebugEnabled()) {
this.getLogger().debug("xslSource = " + xslSource
+ ", system id = " + xslSource.getURI());
}
return new StreamSource(xslSource.getInputStream(), xslSource.getURI());
} catch (java.net.MalformedURLException mue) {
return null;
} catch (SourceException pe) {
throw new TransformerException(pe);
} catch (IOException ioe) {
return null;
} finally {
this.resolver.release( xslSource );
}
} Deprecated!Called by the processor when it encounters
an xsl:include, xsl:import, or document() function. |
public void setSourceResolver(SourceResolver resolver) {
if (this.getLogger().isDebugEnabled()) {
this.getLogger().debug("XSLTProcessor: the setSourceResolver() method is deprecated.");
}
} Deprecated! The - processor can now simply lookup the source resolver.
Deprecated!Set the source resolver used by this component |
public void setTransformerFactory(String classname) {
this.factory = this.getTransformerFactory(classname);
} Deprecated!Set the transformer factory used by this component |
public void transform(Source source,
Source stylesheet,
Parameters params,
Result result) throws ProcessingException {
try {
if (this.getLogger().isDebugEnabled()) {
this.getLogger().debug("XSLTProcessorImpl: transform source = " + source
+ ", stylesheet = " + stylesheet
+ ", parameters = " + params
+ ", result = " + result);
}
TransformerHandler handler = this.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]));
}
}
if (this.getLogger().isDebugEnabled())
this.getLogger().debug("XSLTProcessorImpl: starting transform");
// Is it possible to use Source's toSAX method?
handler.setResult(result);
source.toSAX(handler);
if (this.getLogger().isDebugEnabled())
this.getLogger().debug("XSLTProcessorImpl: transform done");
} catch (Exception e) {
throw new ProcessingException("Error in running Transformation", e);
}
} Deprecated! |