zongaro 2003/02/03 14:35:56
Modified: java/samples/CompiledBrazil Tag: XSLTC_DTM
TransformHandler.java
Log:
Changed code to use the XSLTC TransformerFactory's "use-classpath" attribute
via the JAXP Transform API, rather than using the XSLTC Native API.
Revision Changes Path
No revision
No revision
1.3.2.2 +46 -65
xml-xalan/java/samples/CompiledBrazil/TransformHandler.java
Index: TransformHandler.java
===================================================================
RCS file:
/home/cvs/xml-xalan/java/samples/CompiledBrazil/TransformHandler.java,v
retrieving revision 1.3.2.1
retrieving revision 1.3.2.2
diff -u -r1.3.2.1 -r1.3.2.2
--- TransformHandler.java 27 Jan 2003 19:43:37 -0000 1.3.2.1
+++ TransformHandler.java 3 Feb 2003 22:35:56 -0000 1.3.2.2
@@ -59,17 +59,18 @@
*
*/
-import java.io.*;
-import java.util.NoSuchElementException;
-import java.util.StringTokenizer;
-import java.util.Vector;
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.io.StringWriter;
-import org.xml.sax.*;
+import java.util.StringTokenizer;
-import org.apache.xalan.xsltc.*;
-import org.apache.xalan.xsltc.runtime.AbstractTranslet;
-import org.apache.xalan.xsltc.runtime.output.*;
-import org.apache.xalan.xsltc.dom.*;
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.TransformerException;
+import javax.xml.transform.ErrorListener;
+import javax.xml.transform.stream.StreamResult;
+import javax.xml.transform.stream.StreamSource;
import sunlabs.brazil.server.Handler;
import sunlabs.brazil.server.Request;
@@ -81,14 +82,15 @@
* implements the Handler interface from the Brazil project, see:
* http://www.sun.com/research/brazil/
*
- * Note that the XSLTC transformation engine is invoked through its native
- * interface and not the javax.xml.transform (JAXP) interface. This because
- * XSLTC still does not offer precompiled transformations through JAXP.
+ * Note that the XSLTC transformation engine is invoked through the JAXP
+ * interface, using the XSLTC "use-classpath" attribute. The
+ * "use-from-classpath" attribute specifies to the XSLTC TransformerFactory
+ * that a precompiled version of the stylesheet (translet) may be available,
+ * and that should be used in preference to recompiling the stylesheet.
*/
public class TransformHandler implements Handler {
- // A cache for internal DOM structures
- private static DocumentCache cache = null;
+ private TransformerFactory m_tf = null;
// These two are used while parsing the parameters in the URL
private final String PARAM_TRANSLET = "translet=";
@@ -96,20 +98,22 @@
private final String PARAM_STATS = "stats=";
// All output goes here:
- private PrintWriter _out = null;
+ private PrintWriter m_out = null;
/**
* Dump an error message to output
*/
public void errorMessage(String message, Exception e) {
- if (_out == null) return;
- _out.println("<h1>XSL transformation error</h1>"+message);
- _out.println("<br>Exception:</br>"+e.toString());
+ if (m_out == null) {
+ return;
+ }
+ m_out.println("<h1>XSL transformation error</h1>"+message);
+ m_out.println("<br>Exception:</br>"+e.toString());
}
public void errorMessage(String message) {
- if (_out == null) return;
- _out.println("<h1>XSL transformation error</h1>"+message);
+ if (m_out == null) return;
+ m_out.println("<h1>XSL transformation error</h1>"+message);
}
/**
@@ -126,7 +130,7 @@
// Initialise the output buffer
final StringWriter sout = new StringWriter();
- _out = new PrintWriter(sout);
+ m_out = new PrintWriter(sout);
// These two hold the parameters from the URL 'translet' and 'document'
String transletName = null;
@@ -149,55 +153,32 @@
}
try {
- // Initialize the document cache with 32 DOM slots
- if (cache == null) cache = new DocumentCache(32);
-
- // Output statistics if user looked up "server:port/?stats=xxx"
- if (stats != null) {
- cache.getStatistics(_out); // get cache statistics (in HTML)
- }
// Make sure that both parameters were specified
- else if ((transletName == null) || (document == null)) {
+ if ((transletName == null) || (document == null)) {
errorMessage("Parameters <b><tt>translet</tt></b> and/or "+
"<b><tt>document</tt></b> not specified.");
}
else {
- // Get a reference to the translet class
- Class transletClass = Class.forName(transletName);
- // Instanciate the translet object (inherits AbstractTranslet)
- AbstractTranslet translet =
- (AbstractTranslet)transletClass.newInstance();
- translet.setDOMCache(cache);
-
- // Get the DOM from the DOM cache
- DOMImpl dom = cache.retrieveDocument(document, 0, translet);
-
- if (dom == null) {
- errorMessage("Could not locate: \"<b>"+document+"\"</b>");
- }
- else {
- // Create output handler
- TransletOutputHandlerFactory tohFactory =
- TransletOutputHandlerFactory.newInstance();
-
tohFactory.setOutputType(TransletOutputHandlerFactory.STREAM);
- tohFactory.setEncoding(translet._encoding);
- tohFactory.setOutputMethod(translet._method);
- tohFactory.setWriter(_out);
-
- // Do the actual transformation
- final long start = System.currentTimeMillis();
- translet.transform(dom,
tohFactory.getTransletOutputHandler());
- final long done = System.currentTimeMillis() - start;
- _out.println("<!-- transformed by XSLTC in "+done+"ms -->");
- }
+ if (m_tf == null) {
+ m_tf = TransformerFactory.newInstance();
+ try {
+ m_tf.setAttribute("use-classpath", Boolean.TRUE);
+ } catch (IllegalArgumentException iae) {
+ System.err.println(
+ "Could not set XSLTC-specific TransformerFactory
"
+ + "attributes. Transformation failed.");
+ }
+ }
+ Transformer t =
+ m_tf.newTransformer(new StreamSource(transletName));
+
+ // Do the actual transformation
+ final long start = System.currentTimeMillis();
+ t.transform(new StreamSource(document),
+ new StreamResult(m_out));
+ final long done = System.currentTimeMillis() - start;
+ m_out.println("<!-- transformed by XSLTC in "+done+"ms -->");
}
- }
- catch (SAXException e) {
- errorMessage("Error parsing document \""+document+"\"");
- }
- catch (ClassNotFoundException e) {
- errorMessage("Could not locate the translet class: \""+
- transletName+"\"<br>Exception:</br>",e);
}
catch (Exception e) {
errorMessage("Internal error.",e);
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]