jon 01/06/11 12:44:07
Modified: src/java/org/apache/turbine/services/xslt
TurbineXSLTService.java
Log:
patch from Sam Ruby that supports that latest xalan
Revision Changes Path
1.11 +57 -59
jakarta-turbine/src/java/org/apache/turbine/services/xslt/TurbineXSLTService.java
Index: TurbineXSLTService.java
===================================================================
RCS file:
/home/cvs/jakarta-turbine/src/java/org/apache/turbine/services/xslt/TurbineXSLTService.java,v
retrieving revision 1.10
retrieving revision 1.11
diff -u -r1.10 -r1.11
--- TurbineXSLTService.java 2001/05/13 15:58:01 1.10
+++ TurbineXSLTService.java 2001/06/11 19:44:05 1.11
@@ -61,19 +61,25 @@
import java.io.StringWriter;
import java.io.Writer;
import java.util.Hashtable;
+
+import javax.xml.transform.Result;
+import javax.xml.transform.Source;
+import javax.xml.transform.dom.DOMSource;
+import javax.xml.transform.stream.StreamSource;
+import javax.xml.transform.stream.StreamResult;
+import javax.xml.transform.Templates;
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerFactory;
+
+import org.w3c.dom.Node;
+import org.xml.sax.InputSource;
+
import org.apache.turbine.services.BaseInitable;
import org.apache.turbine.services.TurbineBaseService;
import org.apache.turbine.services.TurbineServices;
import org.apache.turbine.services.resources.TurbineResources;
import org.apache.turbine.services.servlet.TurbineServlet;
import org.apache.turbine.util.Log;
-import org.apache.xalan.xslt.StylesheetRoot;
-import org.apache.xalan.xslt.XSLTInputSource;
-import org.apache.xalan.xslt.XSLTProcessor;
-import org.apache.xalan.xslt.XSLTProcessorFactory;
-import org.apache.xalan.xslt.XSLTResultTarget;
-import org.w3c.dom.Node;
-import org.xml.sax.InputSource;
/**
* Implementation of the Turbine XSLT Service. It transforms xml with a given
@@ -81,6 +87,7 @@
* TurbineResources.properties is set) to improve speeds.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Leon Messerschmidt</a>
+ * @author <a href="mailto:[EMAIL PROTECTED]">Sam Ruby</a>
*/
public class TurbineXSLTService
extends TurbineBaseService
@@ -88,7 +95,7 @@
{
/**
- * Property to control the caching of StyleSheetRoots.
+ * Property to control the caching of Templates.
*/
protected boolean caching = false;
@@ -99,9 +106,14 @@
protected String path;
/**
- * Cache of compiled StyleSheetRoots.
+ * Cache of compiled Templates.
*/
protected Hashtable cache = new Hashtable();
+
+ /**
+ * Factory for producing templates and null transformers
+ */
+ private static TransformerFactory tfactory;
/**
* Initialize the TurbineXSLT Service. Load the path to search for
@@ -128,7 +140,9 @@
caching = TurbineResources.getBoolean (
TurbineServices.SERVICE_PREFIX +
XSLTService.SERVICE_NAME + ".cache");
-
+
+ tfactory = TransformerFactory.newInstance();
+
setInit(true);
}
@@ -169,43 +183,40 @@
}
/**
- * Compile a new StylesheetRoot from an input file.
+ * Compile Templates from an input file.
*/
- protected StylesheetRoot compileStylesheetRoot (String source) throws Exception
+ protected Templates compileTemplates (String source) throws Exception
{
- XSLTProcessor processor = XSLTProcessorFactory.getProcessor();
-
- XSLTInputSource xslin = new XSLTInputSource("file:///"+source);
- StylesheetRoot root = processor.processStylesheet(xslin);
-
+ StreamSource xslin = new StreamSource(new File(source));
+ Templates root = tfactory.newTemplates(xslin);
return root;
}
/**
- * Retrieves a StylesheetRoot. If caching is switched on the
- * first attempt is to load the StylesheetRoot from the cache.
+ * 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 a new StyleSheetRoot is compiled from an input
+ * 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 a StyleSheetRoot from the cache while
+ * does not attempt to load Templates from the cache while
* it is still being compiled.
*/
- protected StylesheetRoot getStylesheetRoot(String xslName) throws Exception
+ protected Templates getTemplates(String xslName) throws Exception
{
synchronized (cache)
{
if (caching && cache.containsKey (xslName))
{
- return (StylesheetRoot)cache.get(xslName);
+ return (Templates)cache.get(xslName);
}
String fn = getFileName (xslName);
if (fn == null) return null;
- StylesheetRoot sr = compileStylesheetRoot (fn);
+ Templates sr = compileTemplates (fn);
if (caching)
{
@@ -217,54 +228,41 @@
}
- protected void transform (String xslName, XSLTProcessor processor,
XSLTInputSource xmlin, XSLTResultTarget xmlout) throws Exception
+ protected void transform (String xslName, Source xmlin, Result xmlout)
+ throws Exception
{
- StylesheetRoot sr = getStylesheetRoot(xslName);
+ Templates sr = getTemplates(xslName);
+ Transformer transformer;
// If there is no stylesheet we just echo the xml
if (sr == null)
{
- String line;
- BufferedReader br = new BufferedReader (xmlin.getCharacterStream());
- BufferedWriter bw = new BufferedWriter (xmlout.getCharacterStream());
- line = br.readLine();
- while (line != null)
- {
- try
- {
- bw.write (line);
- line = br.readLine();
- }
- finally
- {
- bw.flush();
- }
- }
+ transformer = tfactory.newTransformer();
}
else
{
- processor.setStylesheet (sr);
- processor.process(xmlin, null, xmlout);
+ transformer = sr.newTransformer();
}
+
+ transformer.transform(xmlin, xmlout);
}
-
/**
* Execute an xslt
*/
- public void transform (String xslName, Reader in, Writer out) throws Exception
+ public void transform (String xslName, Reader in, Writer out)
+ throws Exception
{
- XSLTProcessor processor = XSLTProcessorFactory.getProcessor();
- XSLTInputSource xmlin = new XSLTInputSource(in);
- XSLTResultTarget xmlout = new XSLTResultTarget(out);
-
- transform (xslName,processor,xmlin,xmlout);
+ Source xmlin = new StreamSource(in);
+ Result xmlout = new StreamResult(out);
+ transform (xslName,xmlin,xmlout);
}
/**
* 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);
@@ -275,19 +273,19 @@
/**
* Execute an xslt
*/
- public void transform (String xslName, org.w3c.dom.Node in, Writer out) throws
Exception
+ public void transform (String xslName, org.w3c.dom.Node in, Writer out)
+ throws Exception
{
- XSLTProcessor processor = XSLTProcessorFactory.getProcessor();
- XSLTInputSource xmlin = new XSLTInputSource(in);
- XSLTResultTarget xmlout = new XSLTResultTarget(out);
-
- transform (xslName,processor,xmlin,xmlout);
+ Source xmlin = new DOMSource(in);
+ Result xmlout = new StreamResult(out);
+ transform (xslName,xmlin,xmlout);
}
/**
* Execute an xslt
*/
- public String transform (String xslName, org.w3c.dom.Node in) throws Exception
+ public String transform (String xslName, org.w3c.dom.Node in)
+ throws Exception
{
StringWriter sw = new StringWriter();
transform (xslName,in,sw);
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]