Ard Schrijvers wrote:
> > > I'm wondering if there is a way to pre-compile XSLT into Java
> > > classes, using the command-line xsltc compiler, put those classes
> > > somewhere in Cocoon's classpath and use them as transformers.
> >
> > Actually, since Cocoon can already run the cached compiled
> > stylesheets it shouldn't be that hard to get it to use them in the
> > first place.
>
> Yes. You can take a look at the excalibur xmlutil in
> org.apache.excalibur.xml.xslt.XSLTProcessorImpl and write your own
> that for example first looks at compiled version at some place.

Thank you for your suggestion.

I was able to understand how XSLTProcessor, TraxProcessor, and
TraxTransformer relate to one another and I could write my own
xslt-processor-role that loads pre-compiled stylesheets from the
classpath.

Here it is, if anybody is interested or wants to take it from here.
Suggestions are welcome.


Tobia


=== cocoon.xconf =======================================================

    <component class="ClassLoadingTraxProcessor" logger="core.xslt" 
role="org.apache.excalibur.xml.xslt.XSLTProcessor/clxsltc">
        <parameter name="use-store" value="true"/>
        <parameter name="transformer-factory" 
value="org.apache.xalan.xsltc.trax.TransformerFactoryImpl"/>
    </component>

=== sitemap.xmap =======================================================

    <transformer name="clxsltc" 
src="org.apache.cocoon.transformation.TraxTransformer">
        <xslt-processor-role>clxsltc</xslt-processor-role>
    </transformer>

    ...

    <transform src="test.class" type="clxsltc"/>

=== Java patch =========================================================

--- cocoon-2.1.10/src/java/org/apache/cocoon/components/xslt/TraxProcessor.java 
2006-12-19 12:03:37.000000000 +0100
+++ ClassLoadingTraxProcessor.java      2007-10-23 18:03:34.000000000 +0200
@@ -14,7 +14,7 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package org.apache.cocoon.components.xslt;
+import org.apache.cocoon.components.xslt.*;

 import java.io.File;
 import java.io.IOException;
@@ -72,6 +72,14 @@

-public class TraxProcessor extends AbstractLogEnabled implements 
XSLTProcessor, Serviceable, Initializable, Disposable, Parameterizable,
+public class ClassLoadingTraxProcessor extends AbstractLogEnabled implements 
XSLTProcessor, Serviceable, Initializable, Disposable, Parameterizable,
         Recyclable, URIResolver {
+
+       private class SourceWrapper implements javax.xml.transform.Source {
+               private String id;
+               public SourceWrapper(String id) { this.id = id; }
+               public String getSystemId() { return id; }
+               public void setSystemId(String id) {}
+       }
+
     /** The store service instance */
     protected Store m_store;

@@ -208,6 +216,36 @@

         TraxErrorListener errorListener = new TraxErrorListener(getLogger(), 
stylesheet.getURI());
         try{
+
+                       if (m_factory instanceof 
org.apache.xalan.xsltc.trax.TransformerFactoryImpl
+                                       && id.endsWith(".class")) {
+
+                               // custom code to load pre-compiled translet 
from the classpath
+
+                               if(getLogger().isDebugEnabled())
+                                       getLogger().debug("Loading Templates 
from classpath " + id);
+
+                               m_factory.setAttribute("use-classpath", 
Boolean.TRUE);
+
+                               final Templates template = 
m_factory.newTemplates(new SourceWrapper(id));
+
+                               putTemplates(template, stylesheet, id);
+
+                               final TransformerHandler handler = 
m_factory.newTransformerHandler(template);
+                               
handler.getTransformer().setErrorListener(errorListener);
+                               handler.getTransformer().setURIResolver(this);
+
+                               m_factory.setAttribute("use-classpath", 
Boolean.FALSE);
+
+                               final SourceValidity alwaysValid = new 
SourceValidity() {
+                                       public int isValid() { return 1; }
+                                       public int isValid(SourceValidity 
newValidity) { return 1; }
+                               };
+
+                               return new 
MyTransformerHandlerAndValidity(handler, alwaysValid);
+                               // end custom code
+                       }
+
             if (getLogger().isDebugEnabled()) {
                 getLogger().debug("Creating new Templates for " + id);
             }

Reply via email to