Author: henning
Date: Mon Aug 29 04:40:10 2005
New Revision: 264112

URL: http://svn.apache.org/viewcvs?rev=264112&view=rev
Log:
- Get rid of direct File() access in TurbineXSLTService and replace it
  with URL access through the servlet container. The change also
  allows to place the repository of XSL files elsewhere.

- Replace the cache Map() with a LRUMap() to avoid infinite memory growth.

- Add transform() methods having an additional parameter to forward a
  parameter set to the XSLT. The parameters can be used in the style
  sheet.

Patches contributed by Thomas Vandahl.


Modified:
    jakarta/turbine/core/branches/TURBINE_2_3_BRANCH/project.xml
    
jakarta/turbine/core/branches/TURBINE_2_3_BRANCH/src/java/org/apache/turbine/services/xslt/TurbineXSLT.java
    
jakarta/turbine/core/branches/TURBINE_2_3_BRANCH/src/java/org/apache/turbine/services/xslt/TurbineXSLTService.java
    
jakarta/turbine/core/branches/TURBINE_2_3_BRANCH/src/java/org/apache/turbine/services/xslt/XSLTService.java
    jakarta/turbine/core/branches/TURBINE_2_3_BRANCH/xdocs/changes.xml

Modified: jakarta/turbine/core/branches/TURBINE_2_3_BRANCH/project.xml
URL: 
http://svn.apache.org/viewcvs/jakarta/turbine/core/branches/TURBINE_2_3_BRANCH/project.xml?rev=264112&r1=264111&r2=264112&view=diff
==============================================================================
--- jakarta/turbine/core/branches/TURBINE_2_3_BRANCH/project.xml (original)
+++ jakarta/turbine/core/branches/TURBINE_2_3_BRANCH/project.xml Mon Aug 29 
04:40:10 2005
@@ -351,6 +351,10 @@
       <email>[EMAIL PROTECTED]</email>
     </contributor>
     <contributor>
+      <name>Thomas Vandahl</name>
+      <email>[EMAIL PROTECTED]</email>
+    </contributor>
+    <contributor>
       <name>Scott Weaver</name>
       <email>[EMAIL PROTECTED]</email>
     </contributor>

Modified: 
jakarta/turbine/core/branches/TURBINE_2_3_BRANCH/src/java/org/apache/turbine/services/xslt/TurbineXSLT.java
URL: 
http://svn.apache.org/viewcvs/jakarta/turbine/core/branches/TURBINE_2_3_BRANCH/src/java/org/apache/turbine/services/xslt/TurbineXSLT.java?rev=264112&r1=264111&r2=264112&view=diff
==============================================================================
--- 
jakarta/turbine/core/branches/TURBINE_2_3_BRANCH/src/java/org/apache/turbine/services/xslt/TurbineXSLT.java
 (original)
+++ 
jakarta/turbine/core/branches/TURBINE_2_3_BRANCH/src/java/org/apache/turbine/services/xslt/TurbineXSLT.java
 Mon Aug 29 04:40:10 2005
@@ -18,15 +18,16 @@
 
 import java.io.Reader;
 import java.io.Writer;
+import java.util.Map;
 
 import org.apache.turbine.services.TurbineServices;
-
 import org.w3c.dom.Node;
 
 /**
  * This is a static accesor class for [EMAIL PROTECTED] XSLTService}.
  *
  * @author <a href="mailto:[EMAIL PROTECTED]">Leon Messerschmidt</a>
+ * @author <a href="[EMAIL PROTECTED]">Thomas Vandahl</a>
  * @version $Id$
  */
 public class TurbineXSLT
@@ -65,5 +66,29 @@
             throws Exception
     {
         return getService().transform(xslName, in);
+    }
+
+    public static void transform(String xslName, Reader in, Writer out, Map 
params)
+            throws Exception
+    {
+        getService().transform(xslName, in, out, params);
+    }
+
+    public static String transform(String xslName, Reader in, Map params)
+            throws Exception
+    {
+        return getService().transform(xslName, in, params);
+    }
+
+    public void transform(String xslName, Node in, Writer out, Map params)
+            throws Exception
+    {
+        getService().transform(xslName, in, out, params);
+    }
+
+    public String transform(String xslName, Node in, Map params)
+            throws Exception
+    {
+        return getService().transform(xslName, in, params);
     }
 }

Modified: 
jakarta/turbine/core/branches/TURBINE_2_3_BRANCH/src/java/org/apache/turbine/services/xslt/TurbineXSLTService.java
URL: 
http://svn.apache.org/viewcvs/jakarta/turbine/core/branches/TURBINE_2_3_BRANCH/src/java/org/apache/turbine/services/xslt/TurbineXSLTService.java?rev=264112&r1=264111&r2=264112&view=diff
==============================================================================
--- 
jakarta/turbine/core/branches/TURBINE_2_3_BRANCH/src/java/org/apache/turbine/services/xslt/TurbineXSLTService.java
 (original)
+++ 
jakarta/turbine/core/branches/TURBINE_2_3_BRANCH/src/java/org/apache/turbine/services/xslt/TurbineXSLTService.java
 Mon Aug 29 04:40:10 2005
@@ -16,31 +16,30 @@
  * limitations under the License.
  */
 
-import java.io.File;
+import java.io.IOException;
 import java.io.Reader;
 import java.io.StringWriter;
 import java.io.Writer;
-
-import java.util.HashMap;
+import java.net.URL;
+import java.util.Iterator;
 import java.util.Map;
 
 import javax.xml.transform.Result;
 import javax.xml.transform.Source;
 import javax.xml.transform.Templates;
 import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerException;
 import javax.xml.transform.TransformerFactory;
 import javax.xml.transform.dom.DOMSource;
 import javax.xml.transform.stream.StreamResult;
 import javax.xml.transform.stream.StreamSource;
 
+import org.apache.commons.collections.map.LRUMap;
 import org.apache.commons.configuration.Configuration;
-
 import org.apache.commons.lang.StringUtils;
-
-import org.apache.turbine.Turbine;
 import org.apache.turbine.services.InitializationException;
 import org.apache.turbine.services.TurbineBaseService;
-
+import org.apache.turbine.services.servlet.TurbineServlet;
 import org.w3c.dom.Node;
 
 /**
@@ -50,6 +49,7 @@
  *
  * @author <a href="mailto:[EMAIL PROTECTED]">Leon Messerschmidt</a>
  * @author <a href="mailto:[EMAIL PROTECTED]">Sam Ruby</a>
+ * @author <a href="[EMAIL PROTECTED]">Thomas Vandahl</a>
  * @version $Id$
  */
 public class TurbineXSLTService
@@ -70,7 +70,7 @@
     /**
      * Cache of compiled StyleSheetRoots.
      */
-    protected Map cache = new HashMap();
+    private LRUMap cache = new LRUMap(20);
 
     /**
      * Factory for producing templates and null transformers
@@ -82,21 +82,12 @@
      * xsl files and initiates the cache.
      */
     public void init()
-        throws InitializationException
+            throws InitializationException
     {
         Configuration conf = getConfiguration();
 
-        path = Turbine.getRealPath(conf.getString(STYLESHEET_PATH, null));
-
-        if (StringUtils.isNotEmpty(path))
-        {
-            if (!path.endsWith("/") && !path.endsWith ("\\"))
-            {
-                path = path + File.separator;
-            }
-        }
-
-        caching = conf.getBoolean(STYLESHEET_CACHING);
+        path = conf.getString(STYLESHEET_PATH, STYLESHEET_PATH_DEFAULT);
+        caching = conf.getBoolean(STYLESHEET_CACHING, 
STYLESHEET_CACHING_DEFAULT);
 
         tfactory = TransformerFactory.newInstance();
 
@@ -104,47 +95,57 @@
     }
 
     /**
-     * Get a valid and existing filename from a template name.
-     * The extension is removed and replaced with .xsl.  If this
-     * file does not exist the method attempts to find default.xsl.
-     * If it fails to find default.xsl it returns null.
+     * Try to create a valid url object from the style parameter.
+     *
+     * @param style the xsl-Style
+     * @return a <code>URL</code> object or null if the style sheet could not 
be found
      */
-    protected String getFileName(String templateName)
+    private URL getStyleURL(String style)
     {
-        // First we chop of the existing extension
-        int colon = templateName.lastIndexOf(".");
-        if (colon > 0)
+        StringBuffer sb = new StringBuffer(128);
+
+        if (StringUtils.isNotEmpty(path))
+        {
+            if (path.charAt(0) != '/')
+            {
+                sb.append('/');
+            }
+
+            sb.append(path);
+
+            if (path.charAt(path.length() - 1) != '/')
+            {
+                sb.append('/');
+            }
+        }
+        else
         {
-            templateName = templateName.substring(0, colon);
+            sb.append('/');
         }
 
-        // Now we try to find the file ...
-        File f = new File(path + templateName + ".xsl");
-        if (f.exists())
+        // we chop off the existing extension
+        int colon = style.lastIndexOf(".");
+
+        if (colon > 0)
         {
-            return path + templateName + ".xsl";
+            sb.append(style.substring(0, colon));
         }
         else
         {
-            // ... or the default file
-            f = new File(path + "default.xsl");
-            if (f.exists())
-            {
-                return path + "default.xsl";
-            }
-            else
-            {
-                return null;
-            }
+            sb.append(style);
         }
+
+        sb.append(".xsl");
+
+        return TurbineServlet.getResource(sb.toString());
     }
 
     /**
-     * Compile Templates from an input file.
+     * Compile Templates from an input URL.
      */
-    protected Templates compileTemplates(String source) throws Exception
+    protected Templates compileTemplates(URL source) throws Exception
     {
-        StreamSource xslin = new StreamSource(new File(source));
+        StreamSource xslin = new StreamSource(source.openStream());
         Templates root = tfactory.newTemplates(xslin);
         return root;
     }
@@ -169,11 +170,14 @@
                 return (Templates) cache.get(xslName);
             }
 
-            String fn = getFileName(xslName);
+            URL url = getStyleURL(xslName);
 
-            if (fn == null) return null;
+            if (url == null)
+            {
+                return null;
+            }
 
-            Templates sr = compileTemplates(fn);
+            Templates sr = compileTemplates(url);
 
             if (caching)
             {
@@ -185,24 +189,36 @@
 
     }
 
-    protected void transform(String xslName, Source xmlin, Result xmlout)
-            throws Exception
+    /**
+     * Transform the input source into the output source using the given style
+     *
+     * @param style the stylesheet parameter
+     * @param in the input source
+     * @param out the output source
+     * @param params XSLT parameter for the style sheet
+     *
+     * @throws TransformerException
+     */
+    protected void transform(String style, Source in, Result out, Map params)
+            throws TransformerException, IOException, Exception
     {
-        Templates sr = getTemplates(xslName);
-        Transformer transformer;
+        Templates styleTemplate = getTemplates(style);
 
+        Transformer transformer = (styleTemplate != null)
+                ? styleTemplate.newTransformer()
+                : tfactory.newTransformer();
 
-        // If there is no stylesheet we just echo the xml
-        if (sr == null)
+        if (params != null)
         {
-            transformer = tfactory.newTransformer();
-        }
-        else
-        {
-            transformer = sr.newTransformer();
+            for (Iterator it = params.entrySet().iterator(); it.hasNext(); )
+            {
+                Map.Entry entry = (Map.Entry) it.next();
+                transformer.setParameter(String.valueOf(entry.getKey()), 
entry.getValue());
+            }
         }
 
-        transformer.transform(xmlin, xmlout);
+        //      Start the transformation and rendering process
+        transformer.transform(in, out);
     }
 
     /**
@@ -214,13 +230,14 @@
         Source xmlin = new StreamSource(in);
         Result xmlout = new StreamResult(out);
 
-        transform(xslName, xmlin, xmlout);
+        transform(xslName, xmlin, xmlout, null);
     }
 
     /**
      * Execute an xslt
      */
-    public String transform(String xslName, Reader in) throws Exception
+    public String transform(String xslName, Reader in)
+            throws Exception
     {
         StringWriter sw = new StringWriter();
         transform(xslName, in, sw);
@@ -231,22 +248,67 @@
      * Execute an xslt
      */
     public void transform (String xslName, Node in, Writer out)
-        throws Exception
+            throws Exception
     {
         Source xmlin = new DOMSource(in);
         Result xmlout = new StreamResult(out);
 
-        transform(xslName, xmlin, xmlout);
+        transform(xslName, xmlin, xmlout, null);
     }
 
     /**
      * Execute an xslt
      */
     public String transform (String xslName, Node in)
-        throws Exception
+            throws Exception
     {
         StringWriter sw = new StringWriter();
         transform(xslName, in, sw);
+        return sw.toString();
+    }
+
+    /**
+     * Execute an xslt
+     */
+    public void transform(String xslName, Reader in, Writer out, Map params)
+            throws Exception
+    {
+        Source xmlin = new StreamSource(in);
+        Result xmlout = new StreamResult(out);
+
+        transform(xslName, xmlin, xmlout, params);
+    }
+
+    /**
+     * Execute an xslt
+     */
+    public String transform(String xslName, Reader in, Map params) throws 
Exception
+    {
+        StringWriter sw = new StringWriter();
+        transform(xslName, in, sw, params);
+        return sw.toString();
+    }
+
+    /**
+     * Execute an xslt
+     */
+    public void transform (String xslName, Node in, Writer out, Map params)
+            throws Exception
+    {
+        Source xmlin = new DOMSource(in);
+        Result xmlout = new StreamResult(out);
+
+        transform(xslName, xmlin, xmlout, params);
+    }
+
+    /**
+     * Execute an xslt
+     */
+    public String transform (String xslName, Node in, Map params)
+            throws Exception
+    {
+        StringWriter sw = new StringWriter();
+        transform(xslName, in, sw, params);
         return sw.toString();
     }
 

Modified: 
jakarta/turbine/core/branches/TURBINE_2_3_BRANCH/src/java/org/apache/turbine/services/xslt/XSLTService.java
URL: 
http://svn.apache.org/viewcvs/jakarta/turbine/core/branches/TURBINE_2_3_BRANCH/src/java/org/apache/turbine/services/xslt/XSLTService.java?rev=264112&r1=264111&r2=264112&view=diff
==============================================================================
--- 
jakarta/turbine/core/branches/TURBINE_2_3_BRANCH/src/java/org/apache/turbine/services/xslt/XSLTService.java
 (original)
+++ 
jakarta/turbine/core/branches/TURBINE_2_3_BRANCH/src/java/org/apache/turbine/services/xslt/XSLTService.java
 Mon Aug 29 04:40:10 2005
@@ -18,9 +18,9 @@
 
 import java.io.Reader;
 import java.io.Writer;
+import java.util.Map;
 
 import org.apache.turbine.services.Service;
-
 import org.w3c.dom.Node;
 
 /**
@@ -28,6 +28,7 @@
  * The service makes use of the Xalan xslt engine available from apache.
  *
  * @author <a href="mailto:[EMAIL PROTECTED]">Leon Messerschmidt</a>
+ * @author <a href="[EMAIL PROTECTED]">Thomas Vandahl</a>
  * @version $Id$
  */
 public interface XSLTService
@@ -39,9 +40,15 @@
     /** Name of the Style sheet path property */
     String STYLESHEET_PATH = "path";
 
+    /** Default value of the Style sheet path */
+    String STYLESHEET_PATH_DEFAULT = "/";
+
     /** Property for caching the stylesheets */
     String STYLESHEET_CACHING = "cache";
 
+    /** Default for caching the stylesheets */
+    boolean STYLESHEET_CACHING_DEFAULT = false;
+
     /**
      * Uses an xsl file to transform xml input from a reader and writes the
      * output to a writer.
@@ -79,4 +86,46 @@
      * @param out The writer for the transformed output
      */
     String transform(String xslName, Node in) throws Exception;
+
+    /**
+     * Uses an xsl file to transform xml input from a reader and writes the
+     * output to a writer.
+     *
+     * @param xslName The name of the file that contains the xsl stylesheet.
+     * @param in The reader that passes the xml to be transformed
+     * @param out The writer for the transformed output
+     * @param params A set of parameters that will be forwarded to the XSLT
+     */
+    void transform(String xslName, Reader in, Writer out, Map params) throws 
Exception;
+
+    /**
+     * Uses an xsl file to transform xml input from a reader and returns a
+     * string containing the transformed output.
+     *
+     * @param xslName The name of the file that contains the xsl stylesheet.
+     * @param in The reader that passes the xml to be transformed
+     * @param params A set of parameters that will be forwarded to the XSLT
+     */
+    String transform(String xslName, Reader in, Map params) throws Exception;
+
+    /**
+     * Uses an xsl file to transform xml input from a DOM note and writes the
+     * output to a writer.
+     *
+     * @param xslName The name of the file that contains the xsl stylesheet.
+     * @param in The DOM Node to be transformed
+     * @param out The writer for the transformed output
+     * @param params A set of parameters that will be forwarded to the XSLT
+     */
+    void transform(String xslName, Node in, Writer out, Map params) throws 
Exception;
+
+    /**
+     * Uses an xsl file to transform xml input from a DOM note and returns a
+     * string containing the transformed output.
+     *
+     * @param xslName The name of the file that contains the xsl stylesheet.
+     * @param out The writer for the transformed output
+     * @param params A set of parameters that will be forwarded to the XSLT
+     */
+    String transform(String xslName, Node in, Map params) throws Exception;
 }

Modified: jakarta/turbine/core/branches/TURBINE_2_3_BRANCH/xdocs/changes.xml
URL: 
http://svn.apache.org/viewcvs/jakarta/turbine/core/branches/TURBINE_2_3_BRANCH/xdocs/changes.xml?rev=264112&r1=264111&r2=264112&view=diff
==============================================================================
--- jakarta/turbine/core/branches/TURBINE_2_3_BRANCH/xdocs/changes.xml 
(original)
+++ jakarta/turbine/core/branches/TURBINE_2_3_BRANCH/xdocs/changes.xml Mon Aug 
29 04:40:10 2005
@@ -28,6 +28,15 @@
 <body>
   <release version="2.3.2-dev" date="in CVS">
     <action type="update" dev="henning">
+      Get rid of direct File() access in TurbineXSLTService and replace it 
with URL 
+      access through the servlet container. The change also allows to place the
+      repository of XSL files elsewhere.
+      Replace the cache Map() with a LRUMap() to avoid infinite memory growth.
+      Add transform() methods having an additional parameter to forward a 
parameter
+      set to the XSLT. The parameters can be used in the style sheet.
+      Patches contributed by Thomas Vandahl.
+    </action>
+    <action type="update" dev="henning">
       Change UIManager to allow customization of directories and default 
values.
       Get rid of File() in UIManager.
       Allow UIManager to return relative links if configured.



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to