You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@turbine.apache.org by sg...@apache.org on 2009/11/05 18:46:26 UTC
svn commit: r833099 - in /turbine/fulcrum/trunk/xslt/src:
java/org/apache/fulcrum/xslt/ test/org/apache/fulcrum/xslt/ test/xslt/
Author: sgoeschl
Date: Thu Nov 5 17:46:25 2009
New Revision: 833099
URL: http://svn.apache.org/viewvc?rev=833099&view=rev
Log:
Adding an additional transform method which takes no XML input document
Added:
turbine/fulcrum/trunk/xslt/src/test/xslt/hello.xslt (contents, props changed)
- copied, changed from r825670, turbine/fulcrum/trunk/xslt/src/test/xslt/identity.xslt
Modified:
turbine/fulcrum/trunk/xslt/src/java/org/apache/fulcrum/xslt/DefaultXSLTService.java
turbine/fulcrum/trunk/xslt/src/java/org/apache/fulcrum/xslt/XSLTService.java
turbine/fulcrum/trunk/xslt/src/java/org/apache/fulcrum/xslt/XSLTServiceFacade.java
turbine/fulcrum/trunk/xslt/src/test/org/apache/fulcrum/xslt/XSLTServiceTest.java
Modified: turbine/fulcrum/trunk/xslt/src/java/org/apache/fulcrum/xslt/DefaultXSLTService.java
URL: http://svn.apache.org/viewvc/turbine/fulcrum/trunk/xslt/src/java/org/apache/fulcrum/xslt/DefaultXSLTService.java?rev=833099&r1=833098&r2=833099&view=diff
==============================================================================
--- turbine/fulcrum/trunk/xslt/src/java/org/apache/fulcrum/xslt/DefaultXSLTService.java (original)
+++ turbine/fulcrum/trunk/xslt/src/java/org/apache/fulcrum/xslt/DefaultXSLTService.java Thu Nov 5 17:46:25 2009
@@ -36,6 +36,8 @@
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
import org.apache.avalon.framework.activity.Initializable;
import org.apache.avalon.framework.configuration.Configurable;
@@ -48,6 +50,7 @@
import org.apache.avalon.framework.service.ServiceManager;
import org.apache.avalon.framework.service.Serviceable;
import org.w3c.dom.Node;
+import org.w3c.dom.Document;
/**
* Implementation of the Turbine XSLT Service. It transforms xml with a given
@@ -103,7 +106,7 @@
*/
private URL getStyleURL(String style)
{
- StringBuffer sb = new StringBuffer(128);
+ StringBuffer sb = new StringBuffer();
sb.append(path);
@@ -137,12 +140,15 @@
/**
* Compile Templates from an input file.
+ *
+ * @param source the source URL
+ * @return the compiled template
+ * @throws Exception the compilation failed
*/
protected Templates compileTemplates(URL source) throws Exception
{
StreamSource xslin = new StreamSource(source.openStream());
- Templates root = tfactory.newTemplates(xslin);
- return root;
+ return tfactory.newTemplates(xslin);
}
/**
@@ -154,6 +160,10 @@
* 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.
+ *
+ * @param xslName the name of the XSL file
+ * @return the correspondint template or null if the XSL was not found
+ * @throws Exception getting the template failed
*/
protected Templates getTemplates(String xslName) throws Exception
{
@@ -177,24 +187,38 @@
return sr;
}
-
}
protected void transform(String xslName, Source xmlin, Result xmlout, Map params)
throws Exception
{
- Transformer transformer = getTransformer(xslName);
-
- if (params != null)
+ try
{
- for (Iterator it = params.entrySet().iterator(); it.hasNext(); )
+ long startTime = System.currentTimeMillis();
+ Transformer transformer = getTransformer(xslName);
+
+ if (params != null)
{
- Map.Entry entry = (Map.Entry) it.next();
- transformer.setParameter(String.valueOf(entry.getKey()), entry.getValue());
+ 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);
+ transformer.transform(xmlin, xmlout);
+
+ if(getLogger().isDebugEnabled())
+ {
+ long duration = System.currentTimeMillis() - startTime;
+ getLogger().debug("The transforamtion '" + xslName + "' took " + duration + " ms");
+ }
+ }
+ catch(Exception e)
+ {
+ getLogger().debug("The transformation '" + xslName + "' failed due to : " + e.getMessage());
+ throw e;
+ }
}
/**
@@ -349,6 +373,38 @@
}
/**
+ * Uses an xsl file without any input.
+ *
+ * @param xslName The name of the file that contains the xsl stylesheet.
+ * @param params A set of parameters that will be forwarded to the XSLT
+ * @return the transformed output
+ * @throws Exception the transformation failed
+ */
+ public String transform(String xslName, Map params) throws Exception {
+
+ StringWriter sw = new StringWriter();
+ transform(xslName, sw, params);
+ return sw.toString();
+ }
+
+ /**
+ * Uses an xsl file without any xml input (simplified stylesheet)
+ *
+ * @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
+ * @throws Exception the transformation failed
+ */
+ public void transform(String xslName, Writer out, Map params) throws Exception {
+
+ DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
+ DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
+ Document document = documentBuilder.newDocument();
+
+ transform(xslName, document.getDocumentElement(), out, params);
+ }
+
+ /**
* 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
@@ -358,6 +414,7 @@
* @param xslName
* Identifies stylesheet to get transformer for
* @return A transformer for that stylesheet
+ * @throws Exception retrieving the transformer failed
*/
public Transformer getTransformer(String xslName) throws Exception
{
Modified: turbine/fulcrum/trunk/xslt/src/java/org/apache/fulcrum/xslt/XSLTService.java
URL: http://svn.apache.org/viewvc/turbine/fulcrum/trunk/xslt/src/java/org/apache/fulcrum/xslt/XSLTService.java?rev=833099&r1=833098&r2=833099&view=diff
==============================================================================
--- turbine/fulcrum/trunk/xslt/src/java/org/apache/fulcrum/xslt/XSLTService.java (original)
+++ turbine/fulcrum/trunk/xslt/src/java/org/apache/fulcrum/xslt/XSLTService.java Thu Nov 5 17:46:25 2009
@@ -129,4 +129,26 @@
* @throws Exception the transformation failed
*/
String transform(String xslName, Node in, Map params) throws Exception;
+
+ /**
+ * Uses an xsl file without any xml input.
+ *
+ * @param xslName The name of the file that contains the xsl stylesheet.
+ * @param params A set of parameters that will be forwarded to the XSLT
+ * @return the transformed output
+ * @throws Exception the transformation failed
+ */
+ String transform(String xslName, Map params) throws Exception;
+
+ /**
+ * Uses an xsl file without any xml input.
+ *
+ * @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
+ * @return the transformed output
+ * @throws Exception the transformation failed
+ */
+ void transform(String xslName, Writer out, Map params) throws Exception;
+
}
Modified: turbine/fulcrum/trunk/xslt/src/java/org/apache/fulcrum/xslt/XSLTServiceFacade.java
URL: http://svn.apache.org/viewvc/turbine/fulcrum/trunk/xslt/src/java/org/apache/fulcrum/xslt/XSLTServiceFacade.java?rev=833099&r1=833098&r2=833099&view=diff
==============================================================================
--- turbine/fulcrum/trunk/xslt/src/java/org/apache/fulcrum/xslt/XSLTServiceFacade.java (original)
+++ turbine/fulcrum/trunk/xslt/src/java/org/apache/fulcrum/xslt/XSLTServiceFacade.java Thu Nov 5 17:46:25 2009
@@ -174,4 +174,28 @@
{
return getService().transform(xslName, in, params);
}
+
+ /**
+ * Uses an xsl file without any xml input.
+ *
+ * @param xslName The name of the file that contains the xsl stylesheet.
+ * @param params A set of parameters that will be forwarded to the XSLT
+ * @return the transformed output
+ * @throws Exception the transformation failed
+ */
+ public String transform(String xslName, Map params) throws Exception {
+ return getService().transform(xslName, params);
+ }
+
+ /**
+ * Uses an xsl file without any xml input.
+ *
+ * @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
+ * @throws Exception the transformation failed
+ */
+ public void transform(String xslName, Writer out, Map params) throws Exception {
+ getService().transform(xslName, out, params);
+ }
}
Modified: turbine/fulcrum/trunk/xslt/src/test/org/apache/fulcrum/xslt/XSLTServiceTest.java
URL: http://svn.apache.org/viewvc/turbine/fulcrum/trunk/xslt/src/test/org/apache/fulcrum/xslt/XSLTServiceTest.java?rev=833099&r1=833098&r2=833099&view=diff
==============================================================================
--- turbine/fulcrum/trunk/xslt/src/test/org/apache/fulcrum/xslt/XSLTServiceTest.java (original)
+++ turbine/fulcrum/trunk/xslt/src/test/org/apache/fulcrum/xslt/XSLTServiceTest.java Thu Nov 5 17:46:25 2009
@@ -27,6 +27,8 @@
import java.io.FileReader;
import java.io.Writer;
import java.io.FileWriter;
+import java.util.HashMap;
+import java.util.Map;
/**
* XSLTServiceTest
@@ -72,7 +74,7 @@
}
/**
- * Test an indentity transformation to make sure that the service
+ * Test an identity transformation to make sure that the service
* works.
*
* @throws Exception the test failed
@@ -81,8 +83,21 @@
{
Reader reader = new FileReader("./pom.xml");
Writer writer = new FileWriter("./target/testTransform.xml");
- xsltService.transform("identity.xsl", reader, writer);
+ xsltService.transform("identity.xslt", reader, writer);
reader.close();
writer.close();
}
+
+ /**
+ * Test invocation of a XSLT having no source document.
+ *
+ * @throws Exception the test failed
+ */
+ public void testTransformXsltOnly() throws Exception
+ {
+ Map values = new HashMap();
+ values.put("name", "Fulcrum");
+ String result = xsltService.transform("hello.xslt", values );
+ assertTrue(result.contains("Hello Fulcrum"));
+ }
}
Copied: turbine/fulcrum/trunk/xslt/src/test/xslt/hello.xslt (from r825670, turbine/fulcrum/trunk/xslt/src/test/xslt/identity.xslt)
URL: http://svn.apache.org/viewvc/turbine/fulcrum/trunk/xslt/src/test/xslt/hello.xslt?p2=turbine/fulcrum/trunk/xslt/src/test/xslt/hello.xslt&p1=turbine/fulcrum/trunk/xslt/src/test/xslt/identity.xslt&r1=825670&r2=833099&rev=833099&view=diff
==============================================================================
--- turbine/fulcrum/trunk/xslt/src/test/xslt/identity.xslt (original)
+++ turbine/fulcrum/trunk/xslt/src/test/xslt/hello.xslt Thu Nov 5 17:46:25 2009
@@ -1,10 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
-<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
-
- <xsl:template match="@*|node()">
- <xsl:copy>
- <xsl:apply-templates select="@*|node()"/>
- </xsl:copy>
- </xsl:template>
-
-</xsl:stylesheet>
+
+<html xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xsl:version="1.0">
+ <xsl:param name="name"/>
+ <body>
+ <h1>Hello <xsl:value-of select="$name"/></h1>
+ </body>
+</html>
Propchange: turbine/fulcrum/trunk/xslt/src/test/xslt/hello.xslt
------------------------------------------------------------------------------
svn:mergeinfo =