zongaro 2003/02/04 07:54:44
Modified: java/samples/CompiledApplet Tag: XSLTC_DTM
TransformApplet.java
Log:
Updated sample to use the JAXP API, with XSLTC's "use-classpath" attribute to
load pre-compiled translets.
Revision Changes Path
No revision
No revision
1.4.2.2 +138 -174
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.4.2.1
retrieving revision 1.4.2.2
diff -u -r1.4.2.1 -r1.4.2.2
--- TransformApplet.java 27 Jan 2003 19:43:37 -0000 1.4.2.1
+++ TransformApplet.java 4 Feb 2003 15:54:44 -0000 1.4.2.2
@@ -54,210 +54,174 @@
* 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
*
*/
-import java.io.*;
-import java.applet.*;
-import java.awt.*;
-import java.awt.event.*;
-
-import javax.xml.parsers.SAXParser;
-import javax.xml.parsers.SAXParserFactory;
-import javax.xml.parsers.ParserConfigurationException;
-
-import org.xml.sax.XMLReader;
-import org.xml.sax.SAXException;
-
-import org.apache.xalan.xsltc.*;
-import org.apache.xalan.xsltc.runtime.AbstractTranslet;
-import org.apache.xalan.xsltc.runtime.MessageHandler;
-import org.apache.xalan.xsltc.runtime.output.*;
-import org.apache.xalan.xsltc.dom.*;
+import java.applet.Applet;
+
+import java.awt.BorderLayout;
+import java.awt.Button;
+import java.awt.Frame;
+import java.awt.Label;
+import java.awt.Panel;
+import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
+
+import java.io.PrintWriter;
+import java.io.StringWriter;
+
+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"));
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]