You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@xalan.apache.org by mk...@apache.org on 2003/04/02 19:26:05 UTC
cvs commit: xml-xalan/java/samples/translets JAXPTransletMultipleTransformations.java JAXPTransletOneTransformation.java todo.xsl
mkwan 2003/04/02 09:26:05
Modified: java/samples/CompiledApplet README.applet
TransformApplet.java menu.html
java/samples/CompiledBrazil README.brazil
TransformHandler.java
java/samples/CompiledEJB README.ejb TransformBean.java
java/samples/CompiledJAXP Compile.java README.cjaxp
Transform.java
java/samples/CompiledServlet README.servlet
TransformServlet.java
java/samples/DOM2DOM DOM2DOM.java
java/samples/Pipe Pipe.java
java/samples/SAX2SAX SAX2SAX.java
java/samples/UseXMLFilters UseXMLFilters.java
java/samples/translets
JAXPTransletMultipleTransformations.java
JAXPTransletOneTransformation.java todo.xsl
Log:
Merging XSLTC_DTM and common serializer to the head
Update in the samples
1. Changes from Henry Zongaro (zongaro@ca.ibm.com) in the
XSLTC samples. Use TrAX API instead of native API.
2. Changes from Brian Minchau (minchau@ca.ibm.com) to use
the new serializer classes.
Revision Changes Path
1.3 +32 -22 xml-xalan/java/samples/CompiledApplet/README.applet
Index: README.applet
===================================================================
RCS file: /home/cvs/xml-xalan/java/samples/CompiledApplet/README.applet,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- README.applet 28 Feb 2003 00:25:14 -0000 1.2
+++ README.applet 2 Apr 2003 17:26:03 -0000 1.3
@@ -1,9 +1,9 @@
======================================================================
-The Sun XSLT Compiler (XSLTC) is a Java-based tool for compiling XSL
-stylesheets into extremely lightweight and portable Java byte code.
-The XSLTC Java Runtime environment can then process XML files against
-these compiled stylesheets (Translets) to generate any manner of
-output per the stylesheet instructions.
+The Apache XSLT Compiler (XSLTC) is a Java-based tool for compiling
+XSL stylesheets into extremely lightweight and portable Java byte
+code. The XSLTC Java Runtime environment can then process XML files
+against these compiled stylesheets (Translets) to generate any manner
+of output per the stylesheet instructions.
This Applet Demo shows you how translets can be run in a client
browser as Java applets to perform XSLT transformations on XML
@@ -12,7 +12,7 @@
(translets) and the runtime classes in the applet. Whereas, this
would not be easy with a fullsize Java-based XSLT processor.
-There are two important advatages of this approach:
+There are two important advantages of this approach:
1) It offloads XSLT processing from the server
2) It enables browsers, such as Netscape 4.x, that do not have native
@@ -33,37 +33,47 @@
menu.html - The HTML document for your dialog frame. This is the
document that invokes the TransformApplet
+
+ singleTransform.html
+ - Another main HTML document that invokes the applet using
+ two <param> tags to specify the input document and stylesheet URI
+
-----------------------------------------------------------------------
HOW TO SET UP THE DEMO ON YOUR SITE
-----------------------------------------------------------------------
1. Install and configure Xalan with XSLTC
2. Compile the stylesheets you want to export. This will result in one
- or more small Java classes (translets). Store them under the same
- directory as you put the TransformApplet class.
+ or more small Java classes (translets).
+
+3. Create a JAR file containing your translet classes. Include a file
+ named META-INF/services/javax.xml.transform.TransformerFactory in
+ your JAR file that contains the following line:
-3. Create a JAR file with your the applet class (compile it first, of
- cource), your translet classes and these classes from the xalan/xsltc
- package:
-
- org/apache/xalan/xsltc/*.class
- org/apache/xalan/xsltc/dom/*.class
- org/apache/xalan/xsltc/runtime/*.class
- org/apache/xalan/xsltc/util/*.class
+ org.apache.xalan.xsltc.trax.TransformerFactoryImpl
Be sure to make the JAR file world readable!
-4. Make your XML source documents readable through a URL (either place
+4. Copy the JAR file containing your translet classes and the following
+ JAR files to the same directory containing index.html: xalan.jar,
+ xml-apis.jar, xercesImpl.jar and xsltcapplet.jar.
+
+5. Make your XML source documents readable through a URL (either place
them behind a web server or make them readable for your browser as
a file).
-5. Open the supplied index.html and try out the demo!
-
-6. Using JDK1.4.0, it works on both IE5.5 and NetScape7.0. With lower
- version of JDK, you may have problems with the JavaScript code
+6. Open the supplied index.html and try out the demo! Alternatively,
+ open the singleTransform.html, with the value attribute of the
+ "input-document" <param> tag set to the URI of your input document,
+ and the value attribute of the "stylesheet-name" <param> tag set to
+ the URI of the stylesheet.
+
+7. This sample has been tested successfully with JDK 1.4.0 using IE5.5
+ and NetScape7.0. With an earlier version of the JDK, you may have
+ problems with the JavaScript code.
----------------------------------------------------------------------
-KNOW PROBLEMS
+KNOWN PROBLEMS
----------------------------------------------------------------------
The following problems are specific to the Netscape 4.x browser.
1.7 +125 -168 xml-xalan/java/samples/CompiledApplet/TransformApplet.java
Index: TransformApplet.java
===================================================================
RCS file: /home/cvs/xml-xalan/java/samples/CompiledApplet/TransformApplet.java,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -r1.6 -r1.7
--- TransformApplet.java 30 Jan 2003 15:59:24 -0000 1.6
+++ TransformApplet.java 2 Apr 2003 17:26:03 -0000 1.7
@@ -54,7 +54,7 @@
* Microsystems., http://www.sun.com. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
- *
+ *
* @author Morten Jorgensen
* @author Jacek Ambroziak
*
@@ -73,198 +73,155 @@
import java.io.PrintWriter;
import java.io.StringWriter;
-import javax.xml.parsers.SAXParser;
-import javax.xml.parsers.SAXParserFactory;
-
-import org.apache.xalan.xsltc.DOM;
-import org.apache.xalan.xsltc.dom.DOMImpl;
-import org.apache.xalan.xsltc.dom.DTDMonitor;
-import org.apache.xalan.xsltc.runtime.AbstractTranslet;
-import org.apache.xalan.xsltc.runtime.MessageHandler;
-import org.apache.xalan.xsltc.runtime.output.TransletOutputHandlerFactory;
-
-import org.xml.sax.XMLReader;
+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;
/**
- * This applet demonstrates how XSL transformations can be made run in
+ * This applet demonstrates how XSL transformations can be made to run in
* browsers without native XSLT support.
*
- * 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-classpath" attribute specifies to the XSLTC TransformerFactory
+ * that a precompiled version of the stylesheet (translet) may be available,
+ * and that that should be used in preference to recompiling the stylesheet.
*/
public final class TransformApplet extends Applet {
- // Single document cache
- private String _documentUrl = "";
- private DOMImpl _dom = null;
- private DTDMonitor _dtdMonitor = null;
-
- private static final String NAMESPACE_FEATURE =
- "http://xml.org/sax/features/namespaces";
-
/**
* This class implements a dialog box used for XSL messages/comments
*/
public class MessageFrame extends Frame {
- public Frame frame;
+ public Frame frame;
public class ButtonHandler implements ActionListener {
- public void actionPerformed(ActionEvent e) {
- frame.setVisible(false);
- }
- }
-
- /**
- * This method handles xml:message and xsl:comment by displaying
- * the message/comment in a dialog box.
- */
- public MessageFrame(String title, String message) {
- super(title);
- frame = this; // Make visible to ButtonHandler
- setSize(320,200);
-
- // Create a panel for the message itself
- Panel center = new Panel();
- center.add(new Label(message));
-
- // Create a panel for the 'OK' button
- Panel bottom = new Panel();
- Button okButton = new Button(" OK ");
- okButton.addActionListener(new ButtonHandler());
- bottom.add(okButton);
-
- // Add the two panels to the window/frame
- add(center, BorderLayout.CENTER);
- add(bottom,BorderLayout.SOUTH);
-
- // Show the fecking thing
- setVisible(true);
- }
+ public void actionPerformed(ActionEvent e) {
+ frame.setVisible(false);
+ }
+ }
+
+ /**
+ * This method handles xml:message and xsl:comment by displaying
+ * the message/comment in a dialog box.
+ */
+ public MessageFrame(String title, String message) {
+ super(title);
+ frame = this; // Make visible to ButtonHandler
+ setSize(320,200);
+
+ // Create a panel for the message itself
+ Panel center = new Panel();
+ center.add(new Label(message));
+
+ // Create a panel for the 'OK' button
+ Panel bottom = new Panel();
+ Button okButton = new Button(" OK ");
+ okButton.addActionListener(new ButtonHandler());
+ bottom.add(okButton);
+
+ // Add the two panels to the window/frame
+ add(center, BorderLayout.CENTER);
+ add(bottom,BorderLayout.SOUTH);
+
+ // Show the fecking thing
+ setVisible(true);
+ }
}
/**
- * The appled uses this method to display messages and comments
+ * The applet uses this method to display messages and comments
* generated by xsl:message and xsl:comment elements.
*/
- public class AppletMessageHandler extends MessageHandler {
- public void displayMessage(String msg) {
- MessageFrame z = new MessageFrame("XSL transformation alert",msg);
- }
- }
-
- /**
- * Reads the input document from the supplied URL and builds the
- * internal "DOM" tree.
- */
- private DOM getDOM(String url) throws Exception {
- // Check if the document is already in the 1-document cache
- if (url.equals(_documentUrl) == false) {
-
- // Create a SAX parser and get the XMLReader object it uses
- final SAXParserFactory factory = SAXParserFactory.newInstance();
- try {
- factory.setFeature(NAMESPACE_FEATURE,true);
- }
- catch (Exception e) {
- factory.setNamespaceAware(true);
- }
- final SAXParser parser = factory.newSAXParser();
- final XMLReader reader = parser.getXMLReader();
-
- // Set the DOM's builder as the XMLReader's SAX2 content handler
- _dom = new DOMImpl();
- reader.setContentHandler(_dom.getBuilder());
-
- // Create a DTD monitor and pass it to the XMLReader object
- _dtdMonitor = new DTDMonitor();
- _dtdMonitor.handleDTD(reader);
-
- // Parse the input document
- reader.parse(url);
-
- // Update the 1-document cahce with this DOM
- _documentUrl = url;
- }
- return _dom;
+ public class AppletErrorListener implements ErrorListener {
+ public void displayMessage(TransformerException e) {
+ MessageFrame z = new MessageFrame("XSL transformation alert",
+ e.getMessageAndLocation());
+ }
+
+ public void error(TransformerException e) {
+ displayMessage(e);
+ }
+
+ public void fatalError(TransformerException e) {
+ displayMessage(e);
+ }
+
+ public void warning(TransformerException e) {
+ displayMessage(e);
+ }
}
/**
* This method is the main body of the applet. The method is called
* by some JavaScript code in an HTML document.
- */
+ */
public String transform(Object arg1, Object arg2) {
+ // Convert the two arguments to strings.
+ final String stylesheetURL = (String)arg1;
+ final String documentURL = (String)arg2;
+
+ // Initialise the output stream
+ final StringWriter sout = new StringWriter();
+ final PrintWriter out = new PrintWriter(sout);
+
+ try {
+ // Check that the parameters are valid
+ if (stylesheetURL == null || documentURL == null) {
+ out.println("<h1>Transformation error</h1>");
+ out.println("The parameters <b><tt>stylesheetURL</tt></b> "+
+ "and <b><tt>source</tt></b> must be specified");
+ }
+ else {
+ TransformerFactory tf = null;
+ tf = TransformerFactory.newInstance();
+
+ try {
+ tf.setAttribute("use-classpath", Boolean.TRUE);
+ } catch (IllegalArgumentException iae) {
+ System.err.println(
+ "Could not set XSLTC-specific TransformerFactory "
+ + "attributes. Transformation failed.");
+ }
+ Transformer t =
+ tf.newTransformer(new StreamSource(stylesheetURL));
+ t.setErrorListener(new AppletErrorListener());
+
+ final long start = System.currentTimeMillis();
+
+ t.transform(new StreamSource(documentURL),
+ new StreamResult(out));
+
+ final long done = System.currentTimeMillis() - start;
+ out.println("<!-- transformed by XSLTC in "+done+"msecs -->");
+ }
+ // Now close up the sink, and return the HTML output in the
+ // StringWrite object as a string.
+ out.close();
+ System.err.println("Transformation complete!");
+ System.err.println(sout.toString());
+ return sout.toString();
+ }
+ catch (RuntimeException e) {
+ out.println("<h1>RTE</h1>");
+ out.close();
+ return sout.toString();
+ }
+ catch (Exception e) {
+ out.println("<h1>exception</h1>");
+ out.println(e.toString());
+ out.close();
+ return sout.toString();
+ }
+ }
- // Convert the two arguments to strings.
- final String transletName = (String)arg1;
- final String documentUrl = (String)arg2;
-
- // Initialise the output stream
- final StringWriter sout = new StringWriter();
- final PrintWriter out = new PrintWriter(sout);
-
- try {
- // Check that the parameters are valid
- if (transletName == null || documentUrl == null) {
- out.println("<h1>Transformation error</h1>");
- out.println("The parameters <b><tt>class</tt></b> "+
- "and <b><tt>source</tt></b> must be specified");
- }
- else {
- // Instanciate a message handler for xsl:message/xsl:comment
- AppletMessageHandler msgHandler = new AppletMessageHandler();
-
- // Get a refenrence to the translet class
- final Class tc = Class.forName(transletName);
-
- // Instanciate and initialise the tranlet object
- AbstractTranslet translet = (AbstractTranslet)tc.newInstance();
- ((AbstractTranslet)translet).setMessageHandler(msgHandler);
-
- // Create output handler
- TransletOutputHandlerFactory tohFactory =
- TransletOutputHandlerFactory.newInstance();
- tohFactory.setOutputType(TransletOutputHandlerFactory.STREAM);
- tohFactory.setEncoding(translet._encoding);
- tohFactory.setOutputMethod(translet._method);
- tohFactory.setWriter(out);
-
- getDOM(documentUrl);
-
- final long start = System.currentTimeMillis();
-
- // Set size of key/id indices
- translet.setIndexSize(_dom.getSize());
- // If there are any elements with ID attributes, build an index
-
- _dtdMonitor.buildIdIndex(_dom, 0, translet);
- // Pass unparsed entities to translet
- translet.setUnparsedEntityURIs(_dtdMonitor.
- getUnparsedEntityURIs());
- // Do the actual transformation
- translet.transform(_dom, tohFactory.getTransletOutputHandler());
-
- final long done = System.currentTimeMillis() - start;
- out.println("<!-- transformed by XSLTC in "+done+"msecs -->");
- }
- // Now close up the sink, and return the HTML output in the
- // StringWrite object as a string.
- out.close();
- System.err.println("Transformation complete!");
- System.err.println(sout.toString());
- return sout.toString();
- }
- catch (RuntimeException e) {
- out.close();
- return sout.toString();
- }
- catch (Exception e) {
- out.println("<h1>exception</h1>");
- out.println(e.toString());
- out.close();
- return sout.toString();
- }
+ public void start() {
+ String result = transform(getParameter("stylesheet-name"),
+ getParameter("input-document"));
}
}
1.3 +1 -1 xml-xalan/java/samples/CompiledApplet/menu.html
Index: menu.html
===================================================================
RCS file: /home/cvs/xml-xalan/java/samples/CompiledApplet/menu.html,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- menu.html 28 Feb 2003 00:25:14 -0000 1.2
+++ menu.html 2 Apr 2003 17:26:03 -0000 1.3
@@ -40,7 +40,7 @@
The xsltc.jar file must contain the XSLTC runtime classes and your
pre-compiled translets, as described in the README.applet file.
-->
- <applet archive="../../bin/xsltc.jar"
+ <applet archive="mytranslet.jar, xsltcapplet.jar, xalan.jar, xml-apis.jar, xercesImpl.jar"
code="TransformApplet"
name="TransformApplet"
width="10"
1.3 +19 -125 xml-xalan/java/samples/CompiledBrazil/README.brazil
Index: README.brazil
===================================================================
RCS file: /home/cvs/xml-xalan/java/samples/CompiledBrazil/README.brazil,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- README.brazil 19 Jul 2001 12:48:34 -0000 1.2
+++ README.brazil 2 Apr 2003 17:26:04 -0000 1.3
@@ -3,7 +3,6 @@
o) HOW TO PROVIDE XSL TRANSFORMATIONS AS A WEB SERVICE
o) HOW TO INVOKE TRANSLETS FROM A BRAZIL HANDLER
- o) BUILDING YOUR OWN DOM CACHE
------------------------------------------------------------
HOW TO PROVIDE XSL TRANSFORMATIONS AS A WEB SERVICE
@@ -18,144 +17,39 @@
"handler", which very much resembles a servlet.
The CompiledEJB and CompiledServlet sample code
-demonstrate other aproaches to providing XSL transformations
+demonstrate other approaches to providing XSL transformations
as a web service.
------------------------------------------------------------
HOW TO INVOKE TRANSLETS FROM A BRAZIL HANDLER
-The .CompiledBrazil directory contains the example source code:
+The CompiledBrazil directory contains the example source code:
TransformHandler.java
This file contains a minimal implementation of an XSL
-transformation handler. The handler performs the same basic
-steps as the class implementing the XSLTC command-line tool:
+transformation handler. You can find a precompiled version
+in xsltcbrazil.jar.
- // Obtain a reference to the translet class
- Class cls = Class.forName(transletName);
- // Instanciate a translet object (inherits AbstractTranslet)
- AbstractTranslet translet = (AbstractTranslet)cls.newInstance();
-
- // Prepare the internal DOM tree
- final DOMImpl dom = new DOMImpl();
- dom.setDocumentURI(inputURI);
-
- // Create a parser for the input document
- // org.apache.xalan.xsltc.runtime.Constants sets NAMESPACE_FEATURE
- final SAXParserFactory facory = SAXFactory.newInstance();
- try {
- factory.setFeature(NAMESPACE_FEATURE,true);
- }
- catch (Exception e) {
- factory.setNamespaceAware(true);
- }
- parser = factory.newSAXParser();
- reader = parser.getXMLReader();
- reader.setContentHandler(dom.getBuilder());
-
- // Create a DTDMonitor for handling ID references in the DTD
- DTDMonitor dtdMonitor = new DTDMonitor();
- dtdMonitor.handleDTD(reader);
-
- // Create output handler (you can plug in your own)
- DefaultSAXOutputHandler saxHandler;
- saxHandler = new DefaultSAXOutputHandler(out);
-
- // Parse the document and build the internal DOM
- reader.parse(inputURI);
-
- // Pass information on id/key indicies to the translet
- translet.setIndexSize(dom.getSize());
- dtdMonitor.buildIdIndex(dom, 0, translet);
- translet.setUnparsedEntityURIs(dtdMonitor.getUnparsedEntityURIs());
-
- // Start the transformation
- translet.transform(dom, new TextOutput(saxHandler));
-
-Alternatively the handler can use a cache for storing
-frequently accessed XML documents. This is not only a matter
-of reading the initial input document from the cache, as
-the translet may load other XML input documents as runtime.
-(If the xsl:document() function was used in the stylesheet.)
-
- // Get a reference to the translet class
- Class cls = Class.forName(transletName);
-
- // Instanciate a translet object (inherits AbstractTranslet)
- AbstractTranslet translet = (AbstractTranslet)cls.newInstance();
-
- // The translet needs a reference to the cache in case
- // in needs to load additional XML documents.
- translet.setDOMCache(cache);
-
- // Get the DOM from the DOM cache
- DOMImpl dom = cache.retrieveDocument(documentURI, 0, translet);
-
- // Create output handler (you can plug in your own)
- DefaultSAXOutputHandler saxHandler;
- saxHandler = new DefaultSAXOutputHandler(out);
+Compile any stylesheets you're interested in into translets.
+Set your CLASSPATH to include xalan.jar, xercesImpl.jar,
+xml-apis.jar, your translet classes and the Brazil server
+jar file.
- // Start the transformation
- translet.transform(dom, new TextOutput(saxHandler));
+You can now set up the Brazil server to service requests by
+using the following command:
-------------------------------------------------------------
-BUILDING YOUR OWN DOM CACHE
-
-The interface for the DOM cache consists of a single method,
-and its definition can be found in:
-
- org/apache/xalan/xsltc/DOMCache.java
-
-The method contained in the interface is:
-
- public DOMImpl retrieveDocument(String uri,
- int mask,
- Translet translet);
-
-The responsibilities of this method are:
-
- A) Build new a DOMImpl and DTDMonitor for XML documents
- that are not already in the cache:
-
- // Instanciate a DOMImpl object
- Parser parser = new Parser();
- DOMImpl dom = new DOMImpl();
- dom.setDocumentURI(uri);
- parser.setDocumentHandler(dom.getBuilder());
-
- // Use a DTDMonitor to track ID references in DTD
- DTDMonitor dtdMonitor = new DTDMonitor();
- parser.setDTDHandler(dtdMonitor);
-
- // Parse the input document and build DOM
- parser.parse(uri);
-
- At this point the DOMImpl and DTDMonitor objects are
- populated with the necessary data. The two objects
- are ready to be put in the cache (using the URI as
- the lookup key).
-
- B) For each time a new document is requested by a translet:
-
- // Expand translet's index array to fit this DOM
- translet.setIndexSize(dom.getSize());
-
- // Build indices for this DOM's DTD's ID references
- dtd.buildIdIndex(dom, mask, translet);
-
- // Pass unparsed entity URIs to the translet
- translet.setUnparsedEntityURIs(dtd.getUnparsedEntityURIs());
-
-Step A) must be done every time a document is read into the
-cache, and step B) every time a document is given to a
-translet.
+$ java -Djavax.xml.transform.TransformerFactory=org.apache.xalan.xsltc.TransformerFactoryImpl \
+ sunlabs.brazil.server.Main -port 8080 \
+ -handler TransformHandler
-The XSLTC package contains an example implementation of a
-DOM cache, based on a very simple round-robin caching
-algorithm:
+In a browser, you can enter a URI similar to the following:
- org/apache/xalan/xsltc/dom/DocumentCache.java
+ http://localhost:8080/?translet=myTrans&document=myDoc
+where "myTrans" is the URI of a stylesheet that you've
+compiled into a translet and "myDoc" is URI of an XML document
+you'd like to process using that stylesheet. The result of
+the transformation will be displayed in your browser.
------------------------------------------------------------
END OF README
1.6 +42 -62 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.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- TransformHandler.java 30 Jan 2003 15:59:24 -0000 1.5
+++ TransformHandler.java 2 Apr 2003 17:26:04 -0000 1.6
@@ -65,12 +65,12 @@
import java.util.StringTokenizer;
-import org.apache.xalan.xsltc.dom.DOMImpl;
-import org.apache.xalan.xsltc.dom.DocumentCache;
-import org.apache.xalan.xsltc.runtime.AbstractTranslet;
-import org.apache.xalan.xsltc.runtime.output.TransletOutputHandlerFactory;
-
-import org.xml.sax.SAXException;
+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;
@@ -82,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=";
@@ -97,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);
}
/**
@@ -127,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;
@@ -150,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);
1.3 +26 -8 xml-xalan/java/samples/CompiledEJB/README.ejb
Index: README.ejb
===================================================================
RCS file: /home/cvs/xml-xalan/java/samples/CompiledEJB/README.ejb,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- README.ejb 28 Feb 2003 18:26:05 -0000 1.2
+++ README.ejb 2 Apr 2003 17:26:04 -0000 1.3
@@ -3,7 +3,7 @@
o) HOW TO PROVIDE XSL TRANSFORMATIONS AS A WEB SERVICE
o) HOW TO INVOKE TRANSLETS FROM AN ENTERPRISE JAVA BEAN
- o) TIPS OF RUNNING THE SAMPLES ON JBOSS-3.0.4_TOMCAT-4.1.12
+ o) TIPS FOR RUNNING THE SAMPLE ON JBOSS-3.0.4_TOMCAT-4.1.12
------------------------------------------------------------
HOW TO PROVIDE XSL TRANSFORMATIONS AS A WEB SERVICE
@@ -13,7 +13,7 @@
This sample code demonstrates how that can be implemented.
The CompiledServlet and CompiledBrazil sample code
-demonstrates other aproaches to providing XSL transformations
+demonstrate other approaches to providing XSL transformations
as a web service.
------------------------------------------------------------
@@ -43,7 +43,7 @@
}
o) Create the EJB's home interface, which your servlet
- will use to instanciate the remote interface:
+ will use to instantiate the remote interface:
public interface TransformHome extends EJBHome {
TransformRemote create()
@@ -72,24 +72,42 @@
o) Set up your J2EE_CLASSPATH to include JAXP and the XSLTC
runtime jars.
+
o) Compile your XSL stylesheets and place them either in
your J2EE_CLASSPATH or wrap them in your EJB jar.
+ Make sure that the XSLTC TransformerFactory will be used
+ by either setting the system property
+ "javax.xml.transform.TransformerFactory" with the value
+ "org.apache.xalan.xsltc.trax.TransformerFactoryImpl", or
+ by making a file with the name
+ "META-INF/services/javax.xml.transform.TransformerFactory"
+ containing the single line
+
+ org.apache.xalan.xsltc.trax.TransformerFactoryImpl
+
+ available on your J2EE_CLASSPATH.
+
o) Deploy your EJB
o) Call the servlet with the necessary parameters (at least
an URI to the source XML document and the name of the
translet class).
+
------------------------------------------------------------
-TIPS OF RUNNING THE SAMPLES ON JBOSS-3.0.4_TOMCAT-4.1.12
+TIPS FOR RUNNING THE SAMPLE ON JBOSS-3.0.4_TOMCAT-4.1.12
-Copy the xsltc.jar to %Jboss_Home%/server/default/lib directory
+ o) Copy the bundled xalan.jar (a version containing XSLTC)
+ to %Jboss_Home%/server/default/lib directory.
-Put translet .class the same place where the EJB classes go
+ o) Put the translet .class file in the same directory as
+ the EJB classes.
-The value of "translet" parameter must be the name of translet .class
+ o) Set the value of the "translet" parameter to the name
+ of the translet .class
-The value of "document" parameter must be a valide xml URI
+ o) Set the value of the "document" parameter to a valid xml
+ URI
------------------------------------------------------------
END OF README
1.6 +27 -77 xml-xalan/java/samples/CompiledEJB/TransformBean.java
Index: TransformBean.java
===================================================================
RCS file: /home/cvs/xml-xalan/java/samples/CompiledEJB/TransformBean.java,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- TransformBean.java 30 Jan 2003 15:59:24 -0000 1.5
+++ TransformBean.java 2 Apr 2003 17:26:04 -0000 1.6
@@ -60,27 +60,21 @@
*
*/
-import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.ErrorListener;
+import javax.xml.transform.stream.StreamResult;
+import javax.xml.transform.stream.StreamSource;
+
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
-import javax.xml.parsers.SAXParser;
-import javax.xml.parsers.SAXParserFactory;
-
-import org.apache.xalan.xsltc.dom.DOMImpl;
-import org.apache.xalan.xsltc.dom.DTDMonitor;
-import org.apache.xalan.xsltc.runtime.AbstractTranslet;
-import org.apache.xalan.xsltc.runtime.output.TransletOutputHandlerFactory;
-
-import org.xml.sax.SAXException;
-import org.xml.sax.XMLReader;
-
public class TransformBean implements SessionBean {
- private SessionContext _context = null;
+ private SessionContext m_context = null;
private final static String nullErrorMsg =
"<h1>XSL transformation error</h1>"+
@@ -91,38 +85,6 @@
"http://xml.org/sax/features/namespaces";
/**
- * Read the input document and build the internal "DOM" tree.
- */
- private DOMImpl getDOM(String url, AbstractTranslet translet)
- throws Exception {
-
- // Create a SAX parser and get the XMLReader object it uses
- final SAXParserFactory factory = SAXParserFactory.newInstance();
- try {
- factory.setFeature(NAMESPACE_FEATURE,true);
- }
- catch (Exception e) {
- factory.setNamespaceAware(true);
- }
- final SAXParser parser = factory.newSAXParser();
- final XMLReader reader = parser.getXMLReader();
-
- // Set the DOM's builder as the XMLReader's SAX2 content handler
- DOMImpl dom = new DOMImpl();
- reader.setContentHandler(dom.getBuilder());
-
- // Create a DTD monitor and pass it to the XMLReader object
- final DTDMonitor dtdMonitor = new DTDMonitor();
- dtdMonitor.handleDTD(reader);
- translet.setDTDMonitor(dtdMonitor);
-
- // Parse the input document
- reader.parse(url);
-
- return dom;
- }
-
- /**
* Generates HTML from a basic error message and an exception
*/
private void errorMsg(PrintWriter out, Exception e, String msg) {
@@ -145,39 +107,27 @@
out.println(nullErrorMsg);
}
else {
- // Instanciate a translet object (inherits AbstractTranslet)
- Class tc = Class.forName(transletName);
- AbstractTranslet translet = (AbstractTranslet)tc.newInstance();
-
- // Read input document from the DOM cache
- DOMImpl dom = getDOM(document, translet);
-
- // Create output handler
- TransletOutputHandlerFactory tohFactory =
- TransletOutputHandlerFactory.newInstance();
- tohFactory.setOutputType(TransletOutputHandlerFactory.STREAM);
- tohFactory.setEncoding(translet._encoding);
- tohFactory.setOutputMethod(translet._method);
- tohFactory.setWriter(out);
-
- // Start the transformation
- final long start = System.currentTimeMillis();
- translet.transform(dom, tohFactory.getTransletOutputHandler());
- final long done = System.currentTimeMillis() - start;
- out.println("<!-- transformed by XSLTC in "+done+"msecs -->");
+ TransformerFactory tf = TransformerFactory.newInstance();
+ try {
+ tf.setAttribute("use-classpath", Boolean.TRUE);
+ } catch (IllegalArgumentException iae) {
+ System.err.println(
+ "Could not set XSLTC-specific TransformerFactory "
+ + "attributes. Transformation failed.");
+ }
+
+ Transformer t =
+ tf.newTransformer(new StreamSource(transletName));
+
+ // Do the actual transformation
+ final long start = System.currentTimeMillis();
+ t.transform(new StreamSource(document),
+ new StreamResult(out));
+ final long done = System.currentTimeMillis() - start;
+ out.println("<!-- transformed by XSLTC in "+done+"msecs -->");
}
}
- catch (IOException e) {
- errorMsg(out, e, "Could not locate source document: "+document);
- }
- catch (ClassNotFoundException e) {
- errorMsg(out, e, "Could not locate the translet class: "+
- transletName);
- }
- catch (SAXException e) {
- errorMsg(out, e, "Error parsing document "+document);
- }
catch (Exception e) {
errorMsg(out, e, "Impossible state reached.");
}
@@ -192,7 +142,7 @@
*
*/
public void setSessionContext(SessionContext context) {
- _context = context;
+ m_context = context;
}
// General EJB entry points
1.4 +5 -35 xml-xalan/java/samples/CompiledJAXP/Compile.java
Index: Compile.java
===================================================================
RCS file: /home/cvs/xml-xalan/java/samples/CompiledJAXP/Compile.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- Compile.java 30 Jan 2003 15:59:25 -0000 1.3
+++ Compile.java 2 Apr 2003 17:26:04 -0000 1.4
@@ -56,10 +56,6 @@
*
*/
-import java.io.File;
-import java.io.FileOutputStream;
-import java.io.ObjectOutputStream;
-
import javax.xml.transform.Templates;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamSource;
@@ -77,50 +73,24 @@
*/
public void run(String xsl) {
try {
+ // Set XSLTC's TransformerFactory implementation as the default
+ System.setProperty("javax.xml.transform.TransformerFactory",
+ "org.apache.xalan.xsltc.trax.TransformerFactoryImpl");
+
// Get an input stream for the XSL stylesheet
StreamSource stylesheet = new StreamSource(xsl);
// The TransformerFactory will compile the stylesheet and
// put the translet classes inside the Templates object
TransformerFactory factory = TransformerFactory.newInstance();
+ factory.setAttribute("generate-translet", Boolean.TRUE);
Templates templates = factory.newTemplates(stylesheet);
-
- // Send the Templates object to a '.translet' file
- dumpTemplate(getBaseName(xsl)+".translet", templates);
}
catch (Exception e) {
System.err.println("Exception: " + e);
e.printStackTrace();
}
System.exit(0);
- }
-
- /**
- * Returns the base-name of a file/url
- */
- private String getBaseName(String filename) {
- int start = filename.lastIndexOf(File.separatorChar);
- int stop = filename.lastIndexOf('.');
- if (stop <= start) stop = filename.length() - 1;
- return filename.substring(start+1, stop);
- }
-
- /**
- * Writes a Templates object to a file
- */
- private void dumpTemplate(String file, Templates templates) {
- try {
- FileOutputStream ostream = new FileOutputStream(file);
- ObjectOutputStream p = new ObjectOutputStream(ostream);
- p.writeObject(templates);
- p.flush();
- ostream.close();
- }
- catch (Exception e) {
- System.err.println(e);
- e.printStackTrace();
- System.err.println("Could not write file "+file);
- }
}
private void usage() {
1.2 +10 -12 xml-xalan/java/samples/CompiledJAXP/README.cjaxp
Index: README.cjaxp
===================================================================
RCS file: /home/cvs/xml-xalan/java/samples/CompiledJAXP/README.cjaxp,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- README.cjaxp 1 Oct 2001 13:41:50 -0000 1.1
+++ README.cjaxp 2 Apr 2003 17:26:04 -0000 1.2
@@ -1,26 +1,24 @@
======================================================================
-The Sun XSLT Compiler (XSLTC) is a Java-based tool for compiling XSL
+The XSLT Compiler (XSLTC) is a Java-based tool for compiling XSL
stylesheets into extremely lightweight and portable Java byte code.
-This Compiled JAXP Demo shows you how can compile and use compiled
+This Compiled JAXP Demo shows you one way to compile and use compiled
translets with JAXP.
-You use the two classes provided, Compile and Transform, just like the
+Use the two classes provided, Compile and Transform, just like the
org.apache.xalan.xsltc.cmdline Compile and Transform classes.
Compile.java
- Compiles an XSL stylesheet into a translet, wraps the translet
- inside a Templates object and serializes it to a
- '.translet' file.
+ Compiles an XSL stylesheet into a translet, which is written to
+ a '.class' file.
Transform.java
- Reads a Templates object from the .translet file, the Templates
- object creates a translet and wraps it in a Transformer. The
- translet performs the transformation on behalf of the
- Transformer.transform() method.
+ Constructs a TransformerFactory and asks it to load a translet
+ in the form of a Transformer. The translet performs the
+ transformation on behalf of the Transformer.transform() method.
-Usuage
+Usage
java Compile <stylesheet.xsl>
- java Transform <xmlfile.xml> <stylesheet>.translet
+ java Transform <xmlfile.xml> <stylesheet>
----------------------------------------------------------------------
END OF README
1.5 +17 -40 xml-xalan/java/samples/CompiledJAXP/Transform.java
Index: Transform.java
===================================================================
RCS file: /home/cvs/xml-xalan/java/samples/CompiledJAXP/Transform.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- Transform.java 30 Jan 2003 15:59:25 -0000 1.4
+++ Transform.java 2 Apr 2003 17:26:04 -0000 1.5
@@ -56,13 +56,10 @@
*
*/
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.ObjectInputStream;
import java.io.OutputStreamWriter;
-import javax.xml.transform.Templates;
import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
@@ -74,19 +71,28 @@
}
/**
- * Reads a Templates object from a file, the Templates object creates
- * a translet and wraps it in a Transformer. The translet performs the
- * transformation on behalf of the Transformer.transform() method.
+ * Asks the TransformerFactory to try to load a precompiled version of
+ * the translet from the class path to construct a Transformer object.
+ * The translet performs the transformation on behalf of the
+ * Transformer.transform() method.
*/
public void run(String[] args){
String xml = args[0];
- String translet = args[1];
+ String transletURI = args[1];
try {
+ // Set XSLTC's TransformerFactory implementation as the default
+ System.setProperty("javax.xml.transform.TransformerFactory",
+ "org.apache.xalan.xsltc.trax.TransformerFactoryImpl");
+
+ TransformerFactory tf = TransformerFactory.newInstance();
+ tf.setAttribute("use-classpath", Boolean.TRUE);
+ Transformer transformer = tf.newTransformer(
+ new StreamSource(transletURI));
+
StreamSource document = new StreamSource(xml);
- StreamResult result = new StreamResult(new OutputStreamWriter(System.out));
- Templates templates = readTemplates(translet);
- Transformer transformer = templates.newTransformer();
+ StreamResult result = new StreamResult(
+ new OutputStreamWriter(System.out));
transformer.transform(document, result);
}
catch (Exception e) {
@@ -94,35 +100,6 @@
e.printStackTrace();
}
System.exit(0);
- }
-
- /**
- * Reads a Templates object from a file
- */
- private Templates readTemplates(String file) {
- try {
- FileInputStream ostream = new FileInputStream(file);
- ObjectInputStream p = new ObjectInputStream(ostream);
- Templates templates = (Templates)p.readObject();
- ostream.close();
- return(templates);
- }
- catch (Exception e) {
- System.err.println(e);
- e.printStackTrace();
- System.err.println("Could not write file "+file);
- return null;
- }
- }
-
- /**
- * Returns the base-name of a file/url
- */
- private String getBaseName(String filename) {
- int start = filename.lastIndexOf(File.separatorChar);
- int stop = filename.lastIndexOf('.');
- if (stop <= start) stop = filename.length() - 1;
- return filename.substring(start+1, stop);
}
public void usage() {
1.4 +23 -153 xml-xalan/java/samples/CompiledServlet/README.servlet
Index: README.servlet
===================================================================
RCS file: /home/cvs/xml-xalan/java/samples/CompiledServlet/README.servlet,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- README.servlet 28 Feb 2003 18:26:05 -0000 1.3
+++ README.servlet 2 Apr 2003 17:26:04 -0000 1.4
@@ -3,8 +3,7 @@
o) HOW TO PROVIDE XSL TRANSFORMATIONS AS A WEB SERVICE
o) HOW TO INVOKE TRANSLETS FROM A SERVLET
- o) BUILDING YOUR OWN DOM CACHE
- o) TIPS FOR RUNNING THE SAMPLES ON TOMCAT
+ o) TIPS FOR RUNNING THE SAMPLE ON TOMCAT
-----------------------------------------------------------------------
HOW TO PROVIDE XSL TRANSFORMATIONS AS A WEB SERVICE
@@ -23,166 +22,37 @@
TransformServlet.java
This file contains a minimal implementation of an XSL transformation
-servlet. It utilizes a cache to store the DOM trees for frequently
-accessed XML documents. These are not W3C DOM objects; They are
-specialized DOMs, native to XSLTC and optimzed for use with compiled
-translets. In addition to the initial input XML documents, the cache
-may contain DOMs for other XML input documents the translet requires
-at runtime, when the xsl:document() function is used in the stylesheet.
-
-Here's the essential code in the servlet for doing the transformation:
-
- // Get a reference to the translet class
- Class cls = Class.forName(transletName);
-
- // Instanciate a translet object (inherits AbstractTranslet)
- AbstractTranslet translet = (AbstractTranslet)cls.newInstance();
-
- // The translet needs a reference to the cache in case
- // in needs to load additional XML documents.
- translet.setDOMCache(cache);
-
- // Get the DOM from the DOM cache if current, otherwise
- // build and cache the DOM first
- DOMImpl dom = cache.retrieveDocument(documentURI, 0, translet);
-
- // Create output handler (you can plug in your own)
- DefaultSAXOutputHandler saxHandler;
- saxHandler = new DefaultSAXOutputHandler(out);
+servlet. When you deploy your servlet, make sure that the XSLTC
+TransformerFactory will be used by either setting the system property
+"javax.xml.transform.TransformerFactory" with the value
+"org.apache.xalan.xsltc.trax.TransformerFactoryImpl", or by making a
+file with the name "META-INF/services/javax.xml.transform.TransformerFactory"
+containing the single line
- // Start the transformation
- translet.transform(dom, new TextOutput(saxHandler));
+ org.apache.xalan.xsltc.trax.TransformerFactoryImpl
------------------------------------------------------------------------
-BUILDING YOUR OWN DOM CACHE
-
-The interface for the DOM cache consists of a single method,
-and its definition can be found in:
-
- org/apache/xalan/xsltc/DOMCache.java
-
-The method contained in the interface is:
-
- public DOMImpl retrieveDocument(String uri,
- int mask,
- Translet translet);
-
-The responsibilities of this method are:
-
- A) Build new a DOMImpl and DTDMonitor for XML documents
- that are not already in the cache:
-
- // Instanciate a DOMImpl object
- Parser parser = new Parser();
- DOMImpl dom = new DOMImpl();
- // Set URI for imports, includes, and document() functions
- dom.setDocumentURI(uri);
- parser = factory.newSAXParser();
- reader = parser.getXMLReader();
- reader.setContentHandler(dom.getBuilder());
-
- // Use a DTDMonitor to track ID references in DTD
- DTDMonitor dtdMonitor = new DTDMonitor();
- dtdMonitor.handleDTD(reader);
-
- // Parse the input document and build DOM
- reader.parse(uri);
-
- At this point the DOMImpl and DTDMonitor objects are
- populated with the necessary data. The two objects
- are ready to be put in the cache (using the URI as
- the lookup key).
-
- B) For each time a new document is requested by a translet:
-
- // Expand translet's index array to fit this DOM
- translet.setIndexSize(dom.getSize());
-
- // Build indices for this DOM's DTD's ID references
- dtdMonitor.buildIdIndex(dom, mask, translet);
-
- // Pass unparsed entity URIs to the translet
- translet.setUnparsedEntityURIs(dtd.getUnparsedEntityURIs());
-
-Step A) must be done every time a document is read into the
-cache, and step B) every time a document is given to a
-translet.
-
-The XSLTC package contains an example implementation of a
-DOM cache, based on a very simple round-robin caching
-algorithm:
-
- org/apache/xalan/xsltc/dom/DocumentCache.java
+available on your CLASSPATH.
-----------------------------------------------------------------------
-DOING TRANSFORMATIONS WITHOUT A DOM CACHE
-
-Alternatively, you can program a servlet to perform the same basic
-steps as the XSLTC command-line tool
+TIPS FOR RUNNING THE SAMPLE ON TOMCAT
- org.apache.xalan.xsltc.cmdline.Transform
+When compiling the java files supplied with the sample, you need to
+add xalan.jar, xml-apis.jar, xercesImpl.jar and servlet.jar
+(in %Tomcat_Home%/common/lib) to the classpath
-as follows:
+o Copy the translet .class file to the %Tomcat_Home%/common/classes
+ directory
+o Make sure that the value of the "class" parameter is the same as
+ the name of the translet .class
- // Obtain a reference to the translet class
- Class cls = Class.forName(transletName);
- // Instanciate a translet object (inherits AbstractTranslet)
- AbstractTranslet translet = (AbstractTranslet)cls.newInstance();
-
- // Prepare the internal DOM tree
- final DOMImpl dom = new DOMImpl();
- dom.setDocumentURI(inputURI);
-
- // Create a parser for the input document
- // org.apache.xalan.xsltc.runtime.Constants sets NAMESPACE_FEATURE
- final SAXParserFactory facory = SAXFactory.newInstance();
- try {
- factory.setFeature(NAMESPACE_FEATURE,true);
- }
- catch (Exception e) {
- factory.setNamespaceAware(true);
- }
- parser = factory.newSAXParser();
- reader = parser.getXMLReader();
- reader.setContentHandler(dom.getBuilder());
-
- // Create a DTDMonitor for handling ID references in the DTD
- DTDMonitor dtdMonitor = new DTDMonitor();
- dtdMonitor.handleDTD(reader);
-
- // Create output handler (you can plug in your own)
- DefaultSAXOutputHandler saxHandler;
- saxHandler = new DefaultSAXOutputHandler(out);
-
- // Parse the document and build the internal DOM
- reader.parse(inputURI);
-
- // Pass information on id/key indicies to the translet
- translet.setIndexSize(dom.getSize());
- dtdMonitor.buildIdIndex(dom, 0, translet);
- translet.setUnparsedEntityURIs(dtdMonitor.getUnparsedEntityURIs());
+o Make sure that the value of the "source" parameter is a valid
+ xml URI
- // Start the transformation
- translet.transform(dom, new TextOutput(saxHandler));
+o Make sure that the value of the "sheet" parameter is a valid
+ xsl URI
+o The translet .class generated by CompiledServlet will be placed
+ under %Tomcat_Home%/bin/
-----------------------------------------------------------------------
-TIPS FOR RUNNING THE SAMPLES ON TOMCAT
-
-When compiling those two java files, you need to add the xalan.jar, xml-apis.jar,
-xercesImpl.jar, xsltc.jar and servlet.jar (in %Tomcat_Home%/common/lib) to
-the classpath
-
-Copy xsltc.jar to the %Tomcat_Home%/common/lib directory
-
-Copy the translet .class to %Tomcat_Home%/common/classes directory
-
-The value of "class" parameter must be the name of translet .class
-
-The value of "source" parameter must be a valide xml URI
-
-The value of "sheet" parameter must be a valide xsl URI
-
-The transleted xsl is stored under %Tomcat_Home%/bin/ (???)
-------------------------------------------------------------
END OF README
1.5 +26 -58 xml-xalan/java/samples/CompiledServlet/TransformServlet.java
Index: TransformServlet.java
===================================================================
RCS file: /home/cvs/xml-xalan/java/samples/CompiledServlet/TransformServlet.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- TransformServlet.java 30 Jan 2003 15:59:25 -0000 1.4
+++ TransformServlet.java 2 Apr 2003 17:26:04 -0000 1.5
@@ -69,10 +69,11 @@
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
-import org.apache.xalan.xsltc.dom.DOMImpl;
-import org.apache.xalan.xsltc.dom.DocumentCache;
-import org.apache.xalan.xsltc.runtime.AbstractTranslet;
-import org.apache.xalan.xsltc.runtime.output.TransletOutputHandlerFactory;
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.TransformerException;
+import javax.xml.transform.stream.StreamResult;
+import javax.xml.transform.stream.StreamSource;
import org.xml.sax.SAXException;
@@ -81,19 +82,14 @@
* a web service. See the CompileServlet for an example on how stylesheets
* can be pre-compiled before this servlet is invoked.
*
- * 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-classpath" attribute specifies to the XSLTC TransformerFactory
+ * that a precompiled version of the stylesheet (translet) may be available,
+ * and that that should be used in preference to recompiling the stylesheet.
*/
public final class TransformServlet extends HttpServlet {
- /*
- * This is a document cache with 32 document slots. This servlet returns
- * cache statistics if the 'stats' parameter is passed with the HTTP
- * request in doGet().
- */
- private static DocumentCache cache = null;
-
/**
* Main servlet entry point
*/
@@ -106,62 +102,34 @@
PrintWriter out = response.getWriter();
// Get the two paramters "class" and "source".
- String className = request.getParameter("class");
- String documentURI = request.getParameter("source");
+ String transletName = request.getParameter("translet");
+ String documentURI = request.getParameter("source");
try {
- // Initialize document cache with 32 DOM slots
- if (cache == null) cache = new DocumentCache(32);
-
- if (request.getParameter("stats") != null) {
- cache.getStatistics(out);
- }
- else if ((className == null) || (documentURI == null)) {
+ if ((transletName == null) || (documentURI == null)) {
out.println("<h1>XSL transformation error</h1>");
out.println("The parameters <b><tt>class</tt></b> and " +
"<b><tt>source</tt></b> must be specified");
}
else {
- // Get a reference to the translet class (not object yet)
- Class tc = Class.forName(className);
- // Instanciate a translet object (inherits AbstractTranslet)
- AbstractTranslet translet = (AbstractTranslet)tc.newInstance();
-
- // Set the document cache for the translet. This is needed in
- // case the translet uses the document() function.
- translet.setDOMCache(cache);
-
- // Read input document from the DOM cache
- DOMImpl dom = cache.retrieveDocument(documentURI, 0, translet);
-
- // Create output handler
- TransletOutputHandlerFactory tohFactory =
- TransletOutputHandlerFactory.newInstance();
- tohFactory.setOutputType(TransletOutputHandlerFactory.STREAM);
- tohFactory.setEncoding(translet._encoding);
- tohFactory.setOutputMethod(translet._method);
- tohFactory.setWriter(out);
+ TransformerFactory tf = TransformerFactory.newInstance();
+ try {
+ tf.setAttribute("use-classpath", Boolean.TRUE);
+ } catch (IllegalArgumentException iae) {
+ System.err.println(
+ "Could not set XSLTC-specific TransformerFactory "
+ + "attributes. Transformation failed.");
+ }
+ Transformer t =
+ tf.newTransformer(new StreamSource(transletName));
// Start the transformation
final long start = System.currentTimeMillis();
- translet.transform(dom, tohFactory.getTransletOutputHandler());
+ t.transform(new StreamSource(documentURI),
+ new StreamResult(out));
final long done = System.currentTimeMillis() - start;
out.println("<!-- transformed by XSLTC in "+done+"msecs -->");
}
- }
- catch (IOException e) {
- out.println("<h1>Error</h1>");
- out.println("Could not locate source document: " + documentURI);
- out.println(e.toString());
- }
- catch (ClassNotFoundException e) {
- out.println("<h1>Error</h1>");
- out.println("Could not locate the translet class: " + className);
- out.println(e.toString());
- }
- catch (SAXException e) {
- out.println("<h1>Error</h1>");
- out.println("Error parsing document " + documentURI);
}
catch (Exception e) {
out.println("<h1>Error</h1>");
1.7 +4 -4 xml-xalan/java/samples/DOM2DOM/DOM2DOM.java
Index: DOM2DOM.java
===================================================================
RCS file: /home/cvs/xml-xalan/java/samples/DOM2DOM/DOM2DOM.java,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -r1.6 -r1.7
--- DOM2DOM.java 30 Jan 2003 18:21:50 -0000 1.6
+++ DOM2DOM.java 2 Apr 2003 17:26:04 -0000 1.7
@@ -68,9 +68,9 @@
import javax.xml.transform.dom.DOMResult;
import javax.xml.transform.dom.DOMSource;
-import org.apache.xalan.serialize.Serializer;
-import org.apache.xalan.serialize.SerializerFactory;
-import org.apache.xalan.templates.OutputProperties;
+import org.apache.xml.serializer.Serializer;
+import org.apache.xml.serializer.SerializerFactory;
+import org.apache.xml.serializer.OutputPropertiesFactory;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;
@@ -129,7 +129,7 @@
//Instantiate an Xalan XML serializer and use it to serialize the output DOM to System.out
// using a default output format.
Serializer serializer = SerializerFactory.getSerializer
- (OutputProperties.getDefaultMethodProperties("xml"));
+ (OutputPropertiesFactory.getDefaultMethodProperties("xml"));
serializer.setOutputStream(System.out);
serializer.asDOMSerializer().serialize(domResult.getNode());
}
1.10 +4 -4 xml-xalan/java/samples/Pipe/Pipe.java
Index: Pipe.java
===================================================================
RCS file: /home/cvs/xml-xalan/java/samples/Pipe/Pipe.java,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -r1.9 -r1.10
--- Pipe.java 30 Jan 2003 18:21:50 -0000 1.9
+++ Pipe.java 2 Apr 2003 17:26:04 -0000 1.10
@@ -66,9 +66,9 @@
import javax.xml.transform.sax.TransformerHandler;
import javax.xml.transform.stream.StreamSource;
-import org.apache.xalan.serialize.Serializer;
-import org.apache.xalan.serialize.SerializerFactory;
-import org.apache.xalan.templates.OutputProperties;
+import org.apache.xml.serializer.Serializer;
+import org.apache.xml.serializer.SerializerFactory;
+import org.apache.xml.serializer.OutputPropertiesFactory;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory;
@@ -107,7 +107,7 @@
// transformer3 outputs SAX events to the serializer.
Serializer serializer = SerializerFactory.getSerializer
- (OutputProperties.getDefaultMethodProperties("xml"));
+ (OutputPropertiesFactory.getDefaultMethodProperties("xml"));
serializer.setOutputStream(System.out);
tHandler3.setResult(new SAXResult(serializer.asContentHandler()));
1.11 +4 -4 xml-xalan/java/samples/SAX2SAX/SAX2SAX.java
Index: SAX2SAX.java
===================================================================
RCS file: /home/cvs/xml-xalan/java/samples/SAX2SAX/SAX2SAX.java,v
retrieving revision 1.10
retrieving revision 1.11
diff -u -r1.10 -r1.11
--- SAX2SAX.java 30 Jan 2003 18:21:50 -0000 1.10
+++ SAX2SAX.java 2 Apr 2003 17:26:04 -0000 1.11
@@ -75,9 +75,9 @@
import javax.xml.transform.sax.TemplatesHandler;
import javax.xml.transform.sax.TransformerHandler;
-import org.apache.xalan.serialize.Serializer;
-import org.apache.xalan.serialize.SerializerFactory;
-import org.apache.xalan.templates.OutputProperties;
+import org.apache.xml.serializer.Serializer;
+import org.apache.xml.serializer.SerializerFactory;
+import org.apache.xml.serializer.OutputPropertiesFactory;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory;
@@ -123,7 +123,7 @@
FileOutputStream fos = new FileOutputStream("birds.out");
Serializer serializer = SerializerFactory.getSerializer
- (OutputProperties.getDefaultMethodProperties("xml"));
+ (OutputPropertiesFactory.getDefaultMethodProperties("xml"));
serializer.setOutputStream(fos);
1.9 +4 -4 xml-xalan/java/samples/UseXMLFilters/UseXMLFilters.java
Index: UseXMLFilters.java
===================================================================
RCS file: /home/cvs/xml-xalan/java/samples/UseXMLFilters/UseXMLFilters.java,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -r1.8 -r1.9
--- UseXMLFilters.java 30 Jan 2003 18:21:50 -0000 1.8
+++ UseXMLFilters.java 2 Apr 2003 17:26:05 -0000 1.9
@@ -65,9 +65,9 @@
import javax.xml.transform.sax.SAXTransformerFactory;
import javax.xml.transform.stream.StreamSource;
-import org.apache.xalan.serialize.Serializer;
-import org.apache.xalan.serialize.SerializerFactory;
-import org.apache.xalan.templates.OutputProperties;
+import org.apache.xml.serializer.Serializer;
+import org.apache.xml.serializer.SerializerFactory;
+import org.apache.xml.serializer.OutputPropertiesFactory;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLFilter;
@@ -112,7 +112,7 @@
// xmlFilter3 outputs SAX events to the serializer.
Serializer serializer = SerializerFactory.getSerializer
- (OutputProperties.getDefaultMethodProperties("xml"));
+ (OutputPropertiesFactory.getDefaultMethodProperties("xml"));
serializer.setOutputStream(System.out);
xmlFilter3.setContentHandler(serializer.asContentHandler());
1.9 +5 -5 xml-xalan/java/samples/translets/JAXPTransletMultipleTransformations.java
Index: JAXPTransletMultipleTransformations.java
===================================================================
RCS file: /home/cvs/xml-xalan/java/samples/translets/JAXPTransletMultipleTransformations.java,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -r1.8 -r1.9
--- JAXPTransletMultipleTransformations.java 30 Jan 2003 15:59:25 -0000 1.8
+++ JAXPTransletMultipleTransformations.java 2 Apr 2003 17:26:05 -0000 1.9
@@ -112,15 +112,15 @@
Templates translet = tFactory.newTemplates(new StreamSource(xslInURI));
// Perform each transformation
- doTransform(translet, "../../xsltc_todo.xml", "todo-xsltc.html");
- System.out.println("Produced todo-xsltc.html");
+ doTransform(translet, "todo.xml", "todo.html");
+ System.out.println("Produced todo.html");
- doTransform(translet, "../../todo.xml", "todo-xalan.html");
- System.out.println("Produced todo-xalan.html");
+ doTransform(translet, "todotoo.xml", "todotoo.html");
+ System.out.println("Produced todotoo.html");
}
catch (Exception e)
{
e.printStackTrace();
}
}
-}
\ No newline at end of file
+}
1.10 +4 -4 xml-xalan/java/samples/translets/JAXPTransletOneTransformation.java
Index: JAXPTransletOneTransformation.java
===================================================================
RCS file: /home/cvs/xml-xalan/java/samples/translets/JAXPTransletOneTransformation.java,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -r1.9 -r1.10
--- JAXPTransletOneTransformation.java 30 Jan 2003 15:59:25 -0000 1.9
+++ JAXPTransletOneTransformation.java 2 Apr 2003 17:26:05 -0000 1.10
@@ -97,8 +97,8 @@
System.setProperties(props);
String xslInURI = "todo.xsl";
- String xmlInURI = "../../xsltc_todo.xml";
- String htmlOutURI = "todo-xsltc.html";
+ String xmlInURI = "todo.xml";
+ String htmlOutURI = "todo.html";
try
{
// Instantiate the TransformerFactory, and use it along with a SteamSource
@@ -108,7 +108,7 @@
// Perform the transformation from a StreamSource to a StreamResult;
transformer.transform(new StreamSource(xmlInURI),
new StreamResult(new FileOutputStream(htmlOutURI)));
- System.out.println("Produced todo-xsltc.html");
+ System.out.println("Produced todo.html");
}
catch (Exception e)
{
@@ -116,4 +116,4 @@
e.printStackTrace();
}
}
-}
\ No newline at end of file
+}
1.2 +23 -10 xml-xalan/java/samples/translets/todo.xsl
Index: todo.xsl
===================================================================
RCS file: /home/cvs/xml-xalan/java/samples/translets/todo.xsl,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- todo.xsl 18 May 2001 15:29:14 -0000 1.1
+++ todo.xsl 2 Apr 2003 17:26:05 -0000 1.2
@@ -126,21 +126,34 @@
<xsl:text>Developers:</xsl:text>
</xsl:element>
</H3>
- <p>A list of some of people currently working on working on <xsl:value-of select="/todo/@project"/>:</p>
+ <p>A list of some of the people currently working on <xsl:value-of select="/todo/@project"/>:</p>
<ul>
- <xsl:for-each select="devs/person">
+ <xsl:for-each select="devs/person[not(@status = 'emeritus')]">
<li>
- <a href="mailto:{@email}">
- <xsl:value-of select="@name"/>
- </a>
- <xsl:element name="a">
- <xsl:attribute name="name"><xsl:text>personref-</xsl:text><xsl:value-of select="@id"/></xsl:attribute>
- <xsl:text> (</xsl:text><xsl:value-of select="@id"/><xsl:text>)</xsl:text>
- </xsl:element>
- <BR/><xsl:value-of select="."/>
+ <xsl:apply-templates select="."/>
</li>
</xsl:for-each>
</ul>
+ <xsl:if test="boolean(devs/person[@status = 'emeritus'])">
+ <p>And the Hall-of-Fame list of past developers:</p>
+ <ul>
+ <xsl:for-each select="devs/person[@status = 'emeritus']">
+ <li>
+ <xsl:apply-templates select="."/>
+ </li>
+ </xsl:for-each>
+ </ul>
+ </xsl:if>
</xsl:template>
+ <xsl:template match="person">
+ <a href="mailto:{@email}">
+ <xsl:value-of select="@name"/>
+ </a>
+ <xsl:element name="a">
+ <xsl:attribute name="name"><xsl:text>personref-</xsl:text><xsl:value-of select="@id"/></xsl:attribute>
+ <xsl:text> (</xsl:text><xsl:value-of select="@id"/><xsl:text>)</xsl:text>
+ </xsl:element>
+ <BR/><xsl:value-of select="."/>
+ </xsl:template>
</xsl:stylesheet>
---------------------------------------------------------------------
To unsubscribe, e-mail: xalan-cvs-unsubscribe@xml.apache.org
For additional commands, e-mail: xalan-cvs-help@xml.apache.org