cocoon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From simonetrip...@apache.org
Subject svn commit: r904811 - /cocoon/cocoon3/trunk/cocoon-sax/src/main/java/org/apache/cocoon/sax/component/XSLTTransformer.java
Date Sat, 30 Jan 2010 16:46:18 GMT
Author: simonetripodi
Date: Sat Jan 30 16:46:18 2010
New Revision: 904811

URL: http://svn.apache.org/viewvc?rev=904811&view=rev
Log:
XSLTTransformer resources optimized using InMemoryLRUResourceCache

Modified:
    cocoon/cocoon3/trunk/cocoon-sax/src/main/java/org/apache/cocoon/sax/component/XSLTTransformer.java

Modified: cocoon/cocoon3/trunk/cocoon-sax/src/main/java/org/apache/cocoon/sax/component/XSLTTransformer.java
URL: http://svn.apache.org/viewvc/cocoon/cocoon3/trunk/cocoon-sax/src/main/java/org/apache/cocoon/sax/component/XSLTTransformer.java?rev=904811&r1=904810&r2=904811&view=diff
==============================================================================
--- cocoon/cocoon3/trunk/cocoon-sax/src/main/java/org/apache/cocoon/sax/component/XSLTTransformer.java
(original)
+++ cocoon/cocoon3/trunk/cocoon-sax/src/main/java/org/apache/cocoon/sax/component/XSLTTransformer.java
Sat Jan 30 16:46:18 2010
@@ -36,6 +36,7 @@
 import org.apache.cocoon.pipeline.util.StringRepresentation;
 import org.apache.cocoon.sax.AbstractSAXTransformer;
 import org.apache.cocoon.sax.SAXConsumer;
+import org.apache.cocoon.sax.util.InMemoryLRUResourceCache;
 import org.apache.cocoon.sax.util.SAXConsumerAdapter;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
@@ -43,6 +44,11 @@
 public class XSLTTransformer extends AbstractSAXTransformer {
 
     /**
+     * The memory based LRU cache for already loaded XSLTs.
+     */
+    private static final InMemoryLRUResourceCache<Templates> XSLT_CACHE = new InMemoryLRUResourceCache<Templates>();
+
+    /**
      * A generic transformer factory to parse XSLTs.
      */
     private static final SAXTransformerFactory TRAX_FACTORY = createNewSAXTransformerFactory();
@@ -120,25 +126,35 @@
 
         this.source = source;
 
-        Source urlSource = new StreamSource(this.source.toExternalForm());
+        // check the XSLT is in the cache first
+        if (XSLT_CACHE.containsKey(source)) {
+            // get the XSLT directly from the cache
+            this.templates = XSLT_CACHE.get(this.source);
+        } else {
+            // XSLT has to be parsed
+            Source urlSource = new StreamSource(this.source.toExternalForm());
 
-        SAXTransformerFactory transformerFactory;
-        if (attributes != null && !attributes.isEmpty()) {
-            transformerFactory = createNewSAXTransformerFactory();
-            for (Entry<String, Object> attribute : attributes.entrySet()) {
-                String name = attribute.getKey();
-                Object value = attribute.getValue();
-                transformerFactory.setAttribute(name, value);
+            SAXTransformerFactory transformerFactory;
+            if (attributes != null && !attributes.isEmpty()) {
+                transformerFactory = createNewSAXTransformerFactory();
+                for (Entry<String, Object> attribute : attributes.entrySet()) {
+                    String name = attribute.getKey();
+                    Object value = attribute.getValue();
+                    transformerFactory.setAttribute(name, value);
+                }
+            } else {
+                transformerFactory = TRAX_FACTORY;
             }
-        } else {
-            transformerFactory = TRAX_FACTORY;
-        }
 
-        try {
-            this.templates = transformerFactory.newTemplates(urlSource);
-        } catch (TransformerConfigurationException e) {
-            throw new SetupException("Impossible to read XSLT from '" + this.source.toExternalForm()
-                    + "', see nested exception", e);
+            try {
+                this.templates = transformerFactory.newTemplates(urlSource);
+                // store the XSLT into the cache for future reuse
+                XSLT_CACHE.put(this.source, this.templates);
+            } catch (TransformerConfigurationException e) {
+                throw new SetupException("Impossible to read XSLT from '"
+                        + this.source.toExternalForm()
+                        + "', see nested exception", e);
+            }
         }
     }
 



Mime
View raw message