Index: /xslt/src/java/org/apache/fulcrum/xslt/XSLTServiceFacade.java
===================================================================
--- /xslt/src/java/org/apache/fulcrum/xslt/XSLTServiceFacade.java
(revision 344962)
+++ /xslt/src/java/org/apache/fulcrum/xslt/XSLTServiceFacade.java
(working copy)
@@ -1,6 +1,5 @@
package org.apache.fulcrum.xslt;
-
/*
* Copyright 2001-2004 The Apache Software Foundation.
*
@@ -17,22 +16,24 @@
* limitations under the License.
*/
-
import java.io.Reader;
import java.io.Writer;
+import java.util.Map;
+
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="mailto:[EMAIL PROTECTED]">Thomas Vandahl</a>
*/
public class XSLTServiceFacade
{
private static XSLTService xsltService;
+
/**
- * Utility method for accessing the service
- * implementation
+ * Utility method for accessing the service implementation
*
* @return a XSLTService implementation instance
*/
@@ -40,33 +41,121 @@
{
return xsltService;
}
-
- protected static void setService(XSLTService xsltService){
- XSLTServiceFacade.xsltService=xsltService;
+
+ /**
+ * Static utility method to set the service instance to be
used in the
+ * facade
+ *
+ * @param xsltService
+ * the service instance
+ */
+ protected static void setService(XSLTService xsltService)
+ {
+ XSLTServiceFacade.xsltService = xsltService;
}
- public static void transform (String xslName, Reader in,
Writer out)
- 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
+ */
+ public static void transform(String xslName, Reader in, Writer
out)
+ throws Exception
{
- getService().transform (xslName,in,out);
+ getService().transform(xslName, in, out);
}
- public static String transform (String xslName, Reader in)
- 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
+ */
+ public static String transform(String xslName, Reader in)
throws Exception
{
- return getService().transform (xslName,in);
+ return getService().transform(xslName, in);
}
- public void transform (String xslName, Node in, Writer out)
- 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
+ */
+ public void transform(String xslName, Node in, Writer out)
throws Exception
{
- getService().transform (xslName,in,out);
+ getService().transform(xslName, in, out);
}
+ /**
+ * 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 in The DOM Node to be transformed
+ */
+ public String transform(String xslName, Node in) throws Exception
+ {
+ return getService().transform(xslName, in);
+ }
- public 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
{
- return getService().transform (xslName,in);
+ getService().transform(xslName, in, out, params);
}
+
+ /**
+ * 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
+ {
+ return getService().transform(xslName, in, params);
+ }
+
+ /**
+ * 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
+ {
+ getService().transform(xslName, in, out, params);
+ }
+
+ /**
+ * 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 in The DOM Node to be transformed
+ * @param params A set of parameters that will be forwarded to
the XSLT
+ */
+ String transform(String xslName, Node in, Map params) throws
Exception
+ {
+ return getService().transform(xslName, in, params);
+ }
}
Index: /xslt/src/java/org/apache/fulcrum/xslt/DefaultXSLTService.java
===================================================================
--- /xslt/src/java/org/apache/fulcrum/xslt/DefaultXSLTService.java
(revision 344962)
+++ /xslt/src/java/org/apache/fulcrum/xslt/DefaultXSLTService.java
(working copy)
@@ -1,6 +1,5 @@
package org.apache.fulcrum.xslt;
-
/*
* Copyright 2001-2004 The Apache Software Foundation.
*
@@ -17,14 +16,16 @@
* limitations under the License.
*/
-
import java.io.File;
import java.io.Reader;
import java.io.StringWriter;
import java.io.Writer;
+import java.net.MalformedURLException;
+import java.net.URL;
import java.util.Hashtable;
+import java.util.Iterator;
+import java.util.Map;
-
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Templates;
@@ -44,132 +45,131 @@
import org.apache.avalon.framework.logger.AbstractLogEnabled;
import org.apache.avalon.framework.service.ServiceManager;
import org.apache.avalon.framework.service.Serviceable;
+import org.w3c.dom.Node;
/**
* Implementation of the Turbine XSLT Service. It transforms xml
with a given
- * xsl file. XSL stylesheets are compiled and cached (if the
service property
- * is set) to improve speeds.
+ * xsl file. XSL stylesheets are compiled and cached (if the
service property is
+ * set) to improve speeds.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Leon
Messerschmidt</a>
* @author <a href="mailto:[EMAIL PROTECTED]">Sam Ruby</a>
* @author <a href="mailto:[EMAIL PROTECTED]">Eric
Pugh</a>
+ * @author <a href="mailto:[EMAIL PROTECTED]">Thomas Vandahl</a>
*/
-public class DefaultXSLTService
- extends AbstractLogEnabled
- implements
XSLTService,Initializable,Configurable,Contextualizable,Serviceable
-
+public class DefaultXSLTService extends AbstractLogEnabled implements
+ XSLTService, Initializable, Configurable,
Contextualizable, Serviceable
{
/**
* The application root
*/
- private String applicationRoot;
-
+ private String applicationRoot;
+
/**
* Property to control the caching of Templates.
*/
- protected boolean caching = false;
+ protected boolean caching = false;
/**
- * Path to style sheets used for tranforming well-formed
- * XML documents. The path is relative to the webapp context.
+ * Path to style sheets used for tranforming well-formed XML
documents. The
+ * path is relative to the webapp context.
*/
- protected String path;
+ protected String path;
/**
- * What the configured value was
- */
- private String styleSheetPath;
-
- /**
* Cache of compiled Templates.
*/
- protected Hashtable cache = new Hashtable();
+ protected Hashtable cache = new
Hashtable();
- protected final static String STYLESHEET_PATH = "path";
+ protected final static String STYLESHEET_PATH = "path";
- protected final static String STYLESHEET_CACHING = "cache";
+ protected final static String STYLESHEET_CACHING = "cache";
/**
* Factory for producing templates and null transformers
*/
private static TransformerFactory tfactory;
-
-
/**
- * 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 (".");
+ StringBuffer sb = new StringBuffer(128);
+
+ sb.append(path);
+
+ // we chop off the existing extension
+ int colon = style.lastIndexOf(".");
+
if (colon > 0)
{
- templateName = templateName.substring (0,colon);
+ sb.append(style.substring(0, colon));
}
+ else
+ {
+ sb.append(style);
+ }
- // Now we try to find the file ...
- File f = new File (path+templateName+".xsl");
- if (f.exists())
+ sb.append(".xsl");
+
+ URL url = null;
+
+ try
{
- return path+templateName+".xsl";
+ url = new URL(sb.toString());
}
- else
+ catch (MalformedURLException e)
{
- // ... or the default file
- f = new File (path+"default.xsl");
- if (f.exists())
- {
- return path+"default.xsl";
- }
- else
- {
- return null;
- }
+ getLogger().error("Malformed URL: " + sb, e);
}
+
+ return url;
}
/**
* Compile Templates from an input file.
*/
- 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;
}
/**
- * Retrieves Templates. If caching is switched on the
- * first attempt is to load Templates from the cache.
- * If caching is switched of or if the Stylesheet is not found
- * in the cache new Templates are compiled from an input
- * file.
+ * Retrieves Templates. If caching is switched on the first
attempt is to
+ * load Templates from the cache. If caching is switched of or
if the
+ * Stylesheet is not found in the cache new Templates are
compiled from an
+ * input file.
* <p>
- * This method is synchronized on the xsl cache so that a thread
- * does not attempt to load Templates from the cache while
- * it is still being compiled.
+ * This method is synchronized on the xsl cache so that a
thread does not
+ * attempt to load Templates from the cache while it is still
being
+ * compiled.
*/
protected Templates getTemplates(String xslName) throws Exception
{
synchronized (cache)
{
- String fn = getFileName (xslName);
- if (fn == null) return null;
+ URL fn = getStyleURL(xslName);
+ if (fn == null)
+ return null;
- if (caching && cache.containsKey (fn))
+ if (caching && cache.containsKey(fn))
{
- return (Templates)cache.get(fn);
+ return (Templates) cache.get(fn);
}
- Templates sr = compileTemplates (fn);
+ Templates sr = compileTemplates(fn);
if (caching)
{
- cache.put (fn,sr);
+ cache.put(fn, sr);
}
return sr;
@@ -177,67 +177,183 @@
}
- protected void transform (String xslName, Source xmlin, Result
xmlout)
- throws Exception
+ protected void transform(String xslName, Source xmlin, Result
xmlout, Map params)
+ throws Exception
{
- Transformer transformer = getTransformer( xslName );
+ Transformer transformer = getTransformer(xslName);
+ if (params != null)
+ {
+ 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);
}
/**
- * Execute an xslt
+ * 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
*/
- public void transform (String xslName, Reader in, Writer out)
- throws Exception
+ public void transform(String xslName, Reader in, Writer out)
+ throws Exception
{
Source xmlin = new StreamSource(in);
Result xmlout = new StreamResult(out);
- transform (xslName,xmlin,xmlout);
+ transform(xslName, xmlin, xmlout, null);
}
/**
- * Execute an xslt
+ * 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
*/
- 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);
+ transform(xslName, in, sw, null);
return sw.toString();
}
+ /**
+ * 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
+ */
+ public void transform(String xslName, org.w3c.dom.Node in,
Writer out)
+ throws Exception
+ {
+ Source xmlin = new DOMSource(in);
+ Result xmlout = new StreamResult(out);
+ transform(xslName, xmlin, xmlout, null);
+ }
/**
- * Execute an xslt
+ * 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 in
+ * The DOM Node to be transformed
*/
- public void transform (String xslName, org.w3c.dom.Node in,
Writer out)
- throws Exception
+ public String transform(String xslName, org.w3c.dom.Node in)
+ throws Exception
{
+ StringWriter sw = new StringWriter();
+ transform(xslName, in, sw);
+ return sw.toString();
+ }
+
+ /**
+ * 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
+ */
+ 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);
+ }
+
+ /**
+ * 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
+ */
+ public String transform(String xslName, Reader in, Map params)
+ throws Exception
+ {
+ StringWriter sw = new StringWriter();
+ transform(xslName, in, sw, params);
+ return sw.toString();
+ }
+
+ /**
+ * 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
+ */
+ 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);
+ transform(xslName, xmlin, xmlout, params);
}
/**
- * Execute an xslt
+ * 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 in
+ * The DOM Node to be transformed
+ * @param params
+ * A set of parameters that will be forwarded to
the XSLT
*/
- public String transform (String xslName, org.w3c.dom.Node in)
- throws Exception
+ public String transform(String xslName, Node in, Map params)
+ throws Exception
{
StringWriter sw = new StringWriter();
- transform (xslName,in,sw);
+ transform(xslName, in, sw, params);
return sw.toString();
}
/**
- * Retrieve a transformer for the given stylesheet name. If no
stylesheet
- * is available for the provided name, an identity transformer
will be
+ * Retrieve a transformer for the given stylesheet name. If no
stylesheet is
+ * available for the provided name, an identity transformer
will be
* returned. This allows clients of this service to perform
more complex
* transformations (for example, where parameters must be
set). When
* possible prefer using one of the forms of [EMAIL PROTECTED] #transform}.
*
- * @param xslName Identifies stylesheet to get transformer for
+ * @param xslName
+ * Identifies stylesheet to get transformer for
* @return A transformer for that stylesheet
*/
public Transformer getTransformer(String xslName) throws
Exception
@@ -253,70 +369,58 @@
return sr.newTransformer();
}
}
-
+
+ // ---------------- Avalon Lifecycle Methods
---------------------
/**
- * @see org.apache.fulcrum.ServiceBroker#getRealPath(String)
+ * Avalon component lifecycle method
+ *
+ * This method processes the repository path, to make it
relative to the web
+ * application root, if neccessary. It supports URL-style
repositories.
*/
- public String getRealPath(String path)
+ public void configure(Configuration conf) throws
ConfigurationException
{
- String absolutePath = null;
- if (applicationRoot == null)
+ StringBuffer sb = new StringBuffer(conf.getAttribute
(STYLESHEET_PATH,
+ "/"));
+
+ // is URL?
+ if (!sb.toString().matches("[a-zA-Z]{3,}://.*"))
{
- absolutePath = new File(path).getAbsolutePath();
+ // No
+ if (sb.charAt(0) != '/')
+ {
+ sb.insert(0, '/');
+ }
+ sb.insert(0, applicationRoot);
+ sb.insert(0, "file:");
}
- else
+
+ if (sb.charAt(sb.length() - 1) != '/')
{
- absolutePath = new File(applicationRoot,
path).getAbsolutePath();
+ sb.append('/');
}
-
- return absolutePath;
- }
- // ---------------- Avalon Lifecycle Methods
---------------------
- /**
- * Avalon component lifecycle method
- */
- public void configure(Configuration conf) throws
ConfigurationException
- {
- styleSheetPath =conf.getAttribute(STYLESHEET_PATH);
- caching = conf.getAttributeAsBoolean(STYLESHEET_CACHING);
+ path = sb.toString();
+ caching = conf.getAttributeAsBoolean(STYLESHEET_CACHING,
false);
}
-
+
/**
* Initializes the service.
- *
- * This method processes the repository path, to make it
relative to the
- * web application root, if neccessary
*/
public void initialize() throws Exception
{
- path = getRealPath(styleSheetPath);
- if (!path.endsWith("/") && !path.endsWith ("\\"))
- {
- path=path+File.separator;
- }
-
tfactory = TransformerFactory.newInstance();
- }
-
- public void contextualize(Context context) throws
ContextException {
- this.applicationRoot = context.get
( "urn:avalon:home" ).toString();
- }
-
+ }
+
+ public void contextualize(Context context) throws
ContextException
+ {
+ this.applicationRoot = context.get
("urn:avalon:home").toString();
+ }
+
/**
* Avalon component lifecycle method
*/
- public void service( ServiceManager manager) {
-
+ public void service(ServiceManager manager)
+ {
XSLTServiceFacade.setService(this);
-
- }
- /**
- * Avalon component lifecycle method
- */
- public void dispose()
- {
-
- }
-
+ }
}
Index: /xslt/src/java/org/apache/fulcrum/xslt/XSLTService.java
===================================================================
--- /xslt/src/java/org/apache/fulcrum/xslt/XSLTService.java
(revision 344962)
+++ /xslt/src/java/org/apache/fulcrum/xslt/XSLTService.java
(working copy)
@@ -20,6 +20,8 @@
import java.io.Reader;
import java.io.Writer;
+import java.util.Map;
+
import org.w3c.dom.Node;
/**
@@ -28,10 +30,11 @@
*
*
* @author <a href="mailto:[EMAIL PROTECTED]">Leon
Messerschmidt</a>
+ * @author <a href="mailto:[EMAIL PROTECTED]">Thomas Vandahl</a>
*/
public interface XSLTService
{
- public static final String ROLE = XSLTService.class.getName();
+ String ROLE = XSLTService.class.getName();
/**
* Uses an xsl file to transform xml input from a reader and
writes the
@@ -41,7 +44,7 @@
* @param in The reader that passes the xml to be transformed
* @param out The writer for the transformed output
*/
- public void transform (String xslName, Reader in, Writer out)
throws Exception;
+ void transform (String xslName, Reader in, Writer out) throws
Exception;
/**
* Uses an xsl file to transform xml input from a reader and
returns a
@@ -50,7 +53,7 @@
* @param xslName The name of the file that contains the xsl
stylesheet.
* @param in The reader that passes the xml to be transformed
*/
- public String transform (String xslName, Reader in) throws
Exception;
+ String transform (String xslName, Reader in) throws Exception;
/**
* Uses an xsl file to transform xml input from a DOM note and
writes the
@@ -60,7 +63,7 @@
* @param in The DOM Node to be transformed
* @param out The writer for the transformed output
*/
- public void transform (String xslName, Node in, Writer out)
throws Exception;
+ void transform (String xslName, Node in, Writer out) throws
Exception;
/**
* Uses an xsl file to transform xml input from a DOM note and
returns a
@@ -69,5 +72,47 @@
* @param xslName The name of the file that contains the xsl
stylesheet.
* @param out The writer for the transformed output
*/
- public String transform (String xslName, Node in) throws
Exception;
+ 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 in The DOM Node to be transformed
+ * @param params A set of parameters that will be forwarded to
the XSLT
+ */
+ String transform(String xslName, Node in, Map params) throws
Exception;
}