zongaro     2003/02/04 08:06:18

  Modified:    java/samples/CompiledEJB Tag: XSLTC_DTM TransformBean.java
  Log:
  Changed code to use the JAXP transform API's, rather than the XSLTC native API.
  This code relies on XSLTC's "use-classpath" attribute to ensure that
  previously compiled translets are used for the transformation.
  
  Revision  Changes    Path
  No                   revision
  
  
  No                   revision
  
  
  1.3.2.2   +29 -81    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.3.2.1
  retrieving revision 1.3.2.2
  diff -u -r1.3.2.1 -r1.3.2.2
  --- TransformBean.java        27 Jan 2003 19:43:37 -0000      1.3.2.1
  +++ TransformBean.java        4 Feb 2003 16:06:18 -0000       1.3.2.2
  @@ -60,29 +60,21 @@
    *
    */
   
  -import java.io.*;
  -import java.text.*;
  -import java.util.*;
  +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 java.rmi.RemoteException;
   import javax.ejb.SessionBean;
   import javax.ejb.SessionContext;
   
  -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.output.*;
  -import org.apache.xalan.xsltc.dom.*;
  -
   public class TransformBean implements SessionBean {
   
  -    private SessionContext _context = null;
  +    private SessionContext m_context = null;
       
       private final static String nullErrorMsg =
        "<h1>XSL transformation error</h1>"+
  @@ -93,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) {
  @@ -147,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.");
        }
  @@ -194,7 +142,7 @@
        *
        */
       public void setSessionContext(SessionContext context) {
  -     _context = context;
  +     m_context = context;
       }
   
       // General EJB entry points
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to