morten      01/07/19 11:48:31

  Modified:    java/src/org/apache/xalan/xsltc/cmdline Compile.java
               java/src/org/apache/xalan/xsltc/compiler DocumentCall.java
                        Import.java Include.java Parser.java
                        Stylesheet.java XSLTC.java
               java/src/org/apache/xalan/xsltc/compiler/util ErrorMsg.java
               java/src/org/apache/xalan/xsltc/trax
                        TransformerFactoryImpl.java TransformerImpl.java
  Log:
  Moved all handling of ErrorListener references to the trax code.
  Completed support for StreamSource input to the newTemplates()
  and newTransformer() methods of TransformerFactory. Also, added
  support for SAXSource. The Parser, XSLTC, Import, Include,
  Stylesheet and DocumentCall classes from the xsltc.compiler
  package were all modified to accomodate this.
  PR:           n/a
  Obtained from:        n/a
  Submitted by: [EMAIL PROTECTED]
  Reviewed by:  [EMAIL PROTECTED]
  
  Revision  Changes    Path
  1.3       +14 -13    xml-xalan/java/src/org/apache/xalan/xsltc/cmdline/Compile.java
  
  Index: Compile.java
  ===================================================================
  RCS file: /home/cvs/xml-xalan/java/src/org/apache/xalan/xsltc/cmdline/Compile.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- Compile.java      2001/07/12 12:54:28     1.2
  +++ Compile.java      2001/07/19 18:48:31     1.3
  @@ -1,5 +1,5 @@
   /*
  - * @(#)$Id: Compile.java,v 1.2 2001/07/12 12:54:28 morten Exp $
  + * @(#)$Id: Compile.java,v 1.3 2001/07/19 18:48:31 morten Exp $
    *
    * The Apache Software License, Version 1.1
    *
  @@ -147,29 +147,30 @@
            final String[] stylesheetNames = getopt.getCmdArgs();
            final Vector   stylesheetVector = new Vector();
            for (int i = 0; i < stylesheetNames.length; i++) {
  -             final String stylesheetName = stylesheetNames[i];
  -             final URL    stylesheetURL;
  -             if (inputIsURL) {
  -                 stylesheetVector.addElement(new URL(stylesheetName));
  -             }
  -             else {
  -                 stylesheetVector.addElement((new File(stylesheetName)).toURL());
  -             }
  -
  +             final String name = stylesheetNames[i];
  +             URL url;
  +             if (inputIsURL)
  +                 url = new URL(name);
  +             else
  +                 url = (new File(name)).toURL();
  +             stylesheetVector.addElement(url);
            }
   
            // Compile the stylesheet and output class/jar file(s)
            if (xsltc.compile(stylesheetVector)) {
  +             xsltc.printWarnings();
                if (xsltc.getJarFileName() != null) xsltc.outputToJar();
  +             System.exit(0);
            }
            else {
  -             Util.println("compilation failed");
  -             System.exit(-1); return;
  +             xsltc.printWarnings();
  +             xsltc.printErrors();
  +             System.exit(-1);
            }
        }
        catch (GetOptsException ex) {
            System.err.println(ex);
  -         printUsage();
  +         printUsage(); // exits with code '-1'
        }
        catch (Exception e) {
            e.printStackTrace();
  
  
  
  1.5       +2 -2      
xml-xalan/java/src/org/apache/xalan/xsltc/compiler/DocumentCall.java
  
  Index: DocumentCall.java
  ===================================================================
  RCS file: 
/home/cvs/xml-xalan/java/src/org/apache/xalan/xsltc/compiler/DocumentCall.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- DocumentCall.java 2001/06/17 12:23:28     1.4
  +++ DocumentCall.java 2001/07/19 18:48:31     1.5
  @@ -1,5 +1,5 @@
   /*
  - * @(#)$Id: DocumentCall.java,v 1.4 2001/06/17 12:23:28 curcuru Exp $
  + * @(#)$Id: DocumentCall.java,v 1.5 2001/07/19 18:48:31 morten Exp $
    *
    * The Apache Software License, Version 1.1
    *
  @@ -110,7 +110,7 @@
                                                "to document() function");
                    throw new TypeCheckError(msg);
                }
  -             _uri = new LiteralExpr(stylesheet.getURL().toString(),
  +             _uri = new LiteralExpr(stylesheet.getSystemId(),
                                       Constants.EMPTYSTRING);
            }
        }
  
  
  
  1.5       +10 -7     xml-xalan/java/src/org/apache/xalan/xsltc/compiler/Import.java
  
  Index: Import.java
  ===================================================================
  RCS file: /home/cvs/xml-xalan/java/src/org/apache/xalan/xsltc/compiler/Import.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- Import.java       2001/07/10 17:45:17     1.4
  +++ Import.java       2001/07/19 18:48:31     1.5
  @@ -1,5 +1,5 @@
   /*
  - * @(#)$Id: Import.java,v 1.4 2001/07/10 17:45:17 morten Exp $
  + * @(#)$Id: Import.java,v 1.5 2001/07/19 18:48:31 morten Exp $
    *
    * The Apache Software License, Version 1.1
    *
  @@ -88,17 +88,20 @@
       public void parseContents(final Parser parser) {
        final Stylesheet context = parser.getCurrentStylesheet();
        try {
  -         final String href = getAttribute("href");
  -         final URL toImport = new URL(context.getURL(), href);
  -         if (context.checkForLoop(toImport))
  -             throw new Exception(toImport.toString() + " already loaded");
  +         final String systemId = getAttribute("href");
  +         if (context.checkForLoop(systemId)) {
  +             ErrorMsg msg = new ErrorMsg(ErrorMsg.CIRCULAR_INC,systemId,this);
  +             parser.reportError(Constants.FATAL, msg);
  +             return;
  +         }
   
  -         final SyntaxTreeNode root = parser.parse(toImport);
  +         InputSource input = new InputSource(systemId);
  +         final SyntaxTreeNode root = parser.parse(input);
            if (root == null) return;
            final Stylesheet _imported = parser.makeStylesheet(root);
            if (_imported == null) return;
   
  -         _imported.setURL(toImport);
  +         _imported.setSystemId(systemId);
            _imported.setParentStylesheet(context);
   
            // precedence for the including stylesheet
  
  
  
  1.5       +10 -8     xml-xalan/java/src/org/apache/xalan/xsltc/compiler/Include.java
  
  Index: Include.java
  ===================================================================
  RCS file: /home/cvs/xml-xalan/java/src/org/apache/xalan/xsltc/compiler/Include.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- Include.java      2001/07/10 17:45:18     1.4
  +++ Include.java      2001/07/19 18:48:31     1.5
  @@ -1,5 +1,5 @@
   /*
  - * @(#)$Id: Include.java,v 1.4 2001/07/10 17:45:18 morten Exp $
  + * @(#)$Id: Include.java,v 1.5 2001/07/19 18:48:31 morten Exp $
    *
    * The Apache Software License, Version 1.1
    *
  @@ -88,17 +88,19 @@
       public void parseContents(final Parser parser) {
        final Stylesheet context = parser.getCurrentStylesheet();
        try {
  -         final String href = getAttribute("href");
  -         final URL toInclude = new URL(context.getURL(), href);
  -         if (context.checkForLoop(toInclude))
  -             throw new Exception(toInclude.toString() + " already loaded");
  -
  -         final SyntaxTreeNode root = parser.parse(toInclude);
  +         final String systemId = getAttribute("href");
  +         if (context.checkForLoop(systemId)) {
  +             ErrorMsg msg = new ErrorMsg(ErrorMsg.CIRCULAR_INC,systemId,this);
  +             parser.reportError(Constants.FATAL, msg);
  +             return;
  +         }
  +         InputSource input = new InputSource(systemId);
  +         final SyntaxTreeNode root = parser.parse(input);
            if (root == null) return;
            final Stylesheet _included = parser.makeStylesheet(root);
            if (_included == null) return;
   
  -         _included.setURL(toInclude);
  +         _included.setSystemId(systemId);
            _included.setParentStylesheet(context);
   
            // An included stylesheet gets the same import precedence
  
  
  
  1.17      +57 -126   xml-xalan/java/src/org/apache/xalan/xsltc/compiler/Parser.java
  
  Index: Parser.java
  ===================================================================
  RCS file: /home/cvs/xml-xalan/java/src/org/apache/xalan/xsltc/compiler/Parser.java,v
  retrieving revision 1.16
  retrieving revision 1.17
  diff -u -r1.16 -r1.17
  --- Parser.java       2001/07/12 10:45:56     1.16
  +++ Parser.java       2001/07/19 18:48:31     1.17
  @@ -1,5 +1,5 @@
   /*
  - * @(#)$Id: Parser.java,v 1.16 2001/07/12 10:45:56 morten Exp $
  + * @(#)$Id: Parser.java,v 1.17 2001/07/19 18:48:31 morten Exp $
    *
    * The Apache Software License, Version 1.1
    *
  @@ -78,9 +78,6 @@
   
   import javax.xml.parsers.*;
   
  -import javax.xml.transform.ErrorListener;
  -import javax.xml.transform.TransformerException;
  -
   import org.w3c.dom.*;
   import org.xml.sax.*;
   
  @@ -99,8 +96,6 @@
       private Vector _errors;           // Contains all compilation errors
       private Vector _warnings;        // Contains all compilation errors
   
  -    private ErrorListener _errorListener = null;
  -
       private Hashtable   _instructionClasses; // Maps instructions to classes
       private Hashtable   _qNames;
       private Hashtable   _namespaces;
  @@ -170,10 +165,6 @@
            output.disable();
       }
   
  -    public void setErrorListener(ErrorListener listener) {
  -     _errorListener = listener;
  -    } 
  -    
       public void addVariable(Variable var) {
        _variableScope.put(var.getName(), var);
       }
  @@ -360,40 +351,24 @@
        }
       }
   
  -    // GTM prototype:
  -    public SyntaxTreeNode parse(InputStream input){
  +    /**
  +     * Parses a stylesheet and builds the internal abstract syntax tree
  +     * @param reader A SAX2 SAXReader (parser)
  +     * @param input A SAX2 InputSource can be passed to a SAX reader
  +     * @return The root of the abstract syntax tree
  +     */
  +    public SyntaxTreeNode parse(XMLReader reader, InputSource input) {
        try {
  -         // Create a SAX parser and get the XMLReader object it uses
  -         final SAXParserFactory factory = SAXParserFactory.newInstance();
  -         try {
  -             factory.setFeature(Constants.NAMESPACE_FEATURE,true);
  -         }
  -         catch (Exception e) {
  -             factory.setNamespaceAware(true);
  -         }
  -         final SAXParser parser = factory.newSAXParser();
  -         final XMLReader reader = parser.getXMLReader();
  -
            // Parse the input document and build the abstract syntax tree
            reader.setContentHandler(this);
  -         InputSource is = new InputSource(input);
  -         reader.parse(new InputSource(input));
  -
  +         reader.parse(input);
            // Find the start of the stylesheet within the tree
  -         return (SyntaxTreeNode)getStylesheet(_root);
  -     }
  -     catch (ParserConfigurationException e) {
  -         reportError(Constants.ERROR,
  -             new ErrorMsg("JAXP parser not configured correctly"));
  +         return (SyntaxTreeNode)getStylesheet(_root);        
        }
        catch (IOException e) {
            reportError(Constants.ERROR,
                new ErrorMsg(e.getMessage()));
        }
  -     catch (SAXParseException e){
  -         reportError(Constants.ERROR,
  -             new ErrorMsg(e.getMessage(),e.getLineNumber()));
  -     }
        catch (SAXException e) {
            reportError(Constants.ERROR, new ErrorMsg(e.getMessage()));
        }
  @@ -404,16 +379,11 @@
       }
   
       /**
  -     * Instanciates a SAX2 parser and generate the AST from the input.
  +     * Parses a stylesheet and builds the internal abstract syntax tree
  +     * @param input A SAX2 InputSource can be passed to a SAX reader
  +     * @return The root of the abstract syntax tree
        */
  -    public SyntaxTreeNode parse(URL url) {
  -     return(parse(url.toString(), true));
  -    }
  -
  -    /**
  -     * Instanciates a SAX2 parser and generate the AST from the input.
  -     */
  -    public SyntaxTreeNode parse(String location, boolean isURL) {
  +    public SyntaxTreeNode parse(InputSource input) {
        try {
            // Create a SAX parser and get the XMLReader object it uses
            final SAXParserFactory factory = SAXParserFactory.newInstance();
  @@ -425,25 +395,12 @@
            }
            final SAXParser parser = factory.newSAXParser();
            final XMLReader reader = parser.getXMLReader();
  -
  -         // Parse the input document and build the abstract syntax tree
  -         reader.setContentHandler(this);
  -         if (isURL)
  -             reader.parse(location);
  -         else
  -             reader.parse("file:"+(new File(location).getAbsolutePath()));
  -
  -         // Find the start of the stylesheet within the tree
  -         return (SyntaxTreeNode)getStylesheet(_root);
  +         return(parse(reader, input));
        }
        catch (ParserConfigurationException e) {
            reportError(Constants.ERROR,
                new ErrorMsg("JAXP parser not configured correctly"));
        }
  -     catch (IOException e) {
  -         reportError(Constants.ERROR,
  -             new ErrorMsg(ErrorMsg.FILECANT_ERR, location));
  -     }
        catch (SAXParseException e){
            reportError(Constants.ERROR,
                new ErrorMsg(e.getMessage(),e.getLineNumber()));
  @@ -451,9 +408,6 @@
        catch (SAXException e) {
            reportError(Constants.ERROR, new ErrorMsg(e.getMessage()));
        }
  -     catch (CompilerException e) {
  -         reportError(Constants.ERROR, new ErrorMsg(e.getMessage()));
  -     }
        return null;
       }
   
  @@ -511,14 +465,18 @@
       /**
        * For embedded stylesheets: Load an external file with stylesheet
        */
  -    private SyntaxTreeNode loadExternalStylesheet(String url)
  +    private SyntaxTreeNode loadExternalStylesheet(String location)
        throws CompilerException {
   
  -     // Check if the URL is a local file
  -     if ((new File(url)).exists()) url = "file:"+url;
  +     InputSource source;
   
  -     SyntaxTreeNode external = (SyntaxTreeNode)parse(url, true);
  +     // Check if the location is URL or a local file
  +     if ((new File(location)).exists())
  +         source = new InputSource("file:"+location);
  +     else
  +         source = new InputSource(location);
   
  +     SyntaxTreeNode external = (SyntaxTreeNode)parse(source);
        return(external);
       }
   
  @@ -930,7 +888,6 @@
        * Prints all compile-time errors
        */
       public void printErrors() {
  -     if (_errorListener != null) return;   //support for TrAX Error Listener
        final int size = _errors.size();
        if (size > 0) {
            System.err.println("Compile errors:");
  @@ -944,7 +901,6 @@
        * Prints all compile-time warnings
        */
       public void printWarnings() {
  -     if (_errorListener != null) return;  //support for TrAX Error Listener 
        final int size = _warnings.size();
        if (size > 0) {
            System.err.println("Warning:");
  @@ -958,66 +914,41 @@
        * Common error/warning message handler
        */
       public void reportError(final int category, final ErrorMsg error) {
  -     try {
  -         switch (category) {
  -         case Constants.INTERNAL:
  -             // Unexpected internal errors, such as null-ptr exceptions, etc.
  -             // Immediately terminates compilation, no translet produced
  -             _errors.addElement(error);
  -             if (_errorListener != null) {
  -                 _errorListener.fatalError(new TransformerException(
  -                     error.toString()));
  -             }
  -             break;
  -         case Constants.UNSUPPORTED:
  -             // XSLT elements that are not implemented and unsupported ext.
  -             // Immediately terminates compilation, no translet produced
  -             _errors.addElement(error);
  -             if (_errorListener != null) {
  -                 final String msg = error.toString();
  -                    _errorListener.fatalError(new TransformerException(
  -                        "Not Implemented, Unsupported Extension: " +
  -                     msg));
  -                }
  -
  -             break;
  -         case Constants.FATAL:
  -             // Fatal error in the stylesheet input (parsing or content)
  -             // Immediately terminates compilation, no translet produced
  -
  -             _errors.addElement(error);
  -             if (_errorListener != null ) {
  -                 final String msg = error.toString();
  -                 _errorListener.fatalError(new TransformerException(
  -                        "Stylesheet Parsing Fatal Error: " + msg));
  -             }
  -             break;
  -         case Constants.ERROR:
  -             // Other error in the stylesheet input (parsing or content)
  -             // Does not terminate compilation, no translet produced
  -             _errors.addElement(error);
  -             if (_errorListener != null) {
  -                 final String msg = error.toString();
  -                 _errorListener.error(new TransformerException(
  -                     "Stylesheet Parsing Error: " + msg));
  -             }
  -             break;
  -         case Constants.WARNING:
  -             // Other error in the stylesheet input (content errors only)
  -             // Does not terminate compilation, a translet is produced
  -             _warnings.addElement(error);
  -             if (_errorListener != null) {
  -                 final String msg = error.toString();
  -                 _errorListener.warning(new TransformerException(
  -                     "Stylesheet Parsing Warning: " + msg));
  -             }
  -             break;
  -         }
  -     }
  -     catch (TransformerException e) {
  -         // If the error listener does not handle the exception then
  -         // we're certainly not doing anything about it....
  +     switch (category) {
  +     case Constants.INTERNAL:
  +         // Unexpected internal errors, such as null-ptr exceptions, etc.
  +         // Immediately terminates compilation, no translet produced
  +         _errors.addElement(error);
  +         break;
  +     case Constants.UNSUPPORTED:
  +         // XSLT elements that are not implemented and unsupported ext.
  +         // Immediately terminates compilation, no translet produced
  +         _errors.addElement(error);
  +         break;
  +     case Constants.FATAL:
  +         // Fatal error in the stylesheet input (parsing or content)
  +         // Immediately terminates compilation, no translet produced
  +         _errors.addElement(error);
  +         break;
  +     case Constants.ERROR:
  +         // Other error in the stylesheet input (parsing or content)
  +         // Does not terminate compilation, no translet produced
  +         _errors.addElement(error);
  +         break;
  +     case Constants.WARNING:
  +         // Other error in the stylesheet input (content errors only)
  +         // Does not terminate compilation, a translet is produced
  +         _warnings.addElement(error);
  +         break;
        }
  +    }
  +
  +    public Vector getErrors() {
  +     return _errors;
  +    }
  +
  +    public Vector getWarnings() {
  +     return _warnings;
       }
   
       /************************ SAX2 ContentHandler INTERFACE *****************/
  
  
  
  1.12      +15 -9     
xml-xalan/java/src/org/apache/xalan/xsltc/compiler/Stylesheet.java
  
  Index: Stylesheet.java
  ===================================================================
  RCS file: 
/home/cvs/xml-xalan/java/src/org/apache/xalan/xsltc/compiler/Stylesheet.java,v
  retrieving revision 1.11
  retrieving revision 1.12
  diff -u -r1.11 -r1.12
  --- Stylesheet.java   2001/07/18 15:36:01     1.11
  +++ Stylesheet.java   2001/07/19 18:48:31     1.12
  @@ -1,5 +1,5 @@
   /*
  - * @(#)$Id: Stylesheet.java,v 1.11 2001/07/18 15:36:01 morten Exp $
  + * @(#)$Id: Stylesheet.java,v 1.12 2001/07/19 18:48:31 morten Exp $
    *
    * The Apache Software License, Version 1.1
    *
  @@ -91,7 +91,7 @@
       private String       _version;
   
       private QName        _name;
  -    private URL          _url;
  +    private String       _systemId;
       private Stylesheet   _parentStylesheet;
        
       // Contains global variables and parameters defined in the stylesheet
  @@ -172,9 +172,15 @@
        return _importPrecedence;
       }
   
  -    public boolean checkForLoop(URL url) {
  -     return _url.sameFile(url) ||
  -         _parentStylesheet != null && _parentStylesheet.checkForLoop(url);
  +    public boolean checkForLoop(String systemId) {
  +     // Return true if this stylesheet includes/imports itself
  +     if (_systemId.equals(systemId))
  +         return true;
  +     // Then check with any stylesheets that included/imported this one
  +     if (_parentStylesheet != null) 
  +         return _parentStylesheet.checkForLoop(systemId);
  +     // Otherwise OK
  +     return false;
       }
       
       public void setParser(Parser parser) {
  @@ -190,12 +196,12 @@
        return _parentStylesheet;
       }
   
  -    public void setURL(URL url) {
  -     _url = url;
  +    public void setSystemId(String systemId) {
  +     _systemId = systemId;
       }
       
  -    public URL getURL() {
  -     return _url;
  +    public String getSystemId() {
  +     return _systemId;
       }
   
       private QName makeStylesheetName(String prefix) {
  
  
  
  1.16      +149 -68   xml-xalan/java/src/org/apache/xalan/xsltc/compiler/XSLTC.java
  
  Index: XSLTC.java
  ===================================================================
  RCS file: /home/cvs/xml-xalan/java/src/org/apache/xalan/xsltc/compiler/XSLTC.java,v
  retrieving revision 1.15
  retrieving revision 1.16
  diff -u -r1.15 -r1.16
  --- XSLTC.java        2001/07/13 10:32:07     1.15
  +++ XSLTC.java        2001/07/19 18:48:31     1.16
  @@ -1,5 +1,5 @@
   /*
  - * @(#)$Id: XSLTC.java,v 1.15 2001/07/13 10:32:07 morten Exp $
  + * @(#)$Id: XSLTC.java,v 1.16 2001/07/19 18:48:31 morten Exp $
    *
    * The Apache Software License, Version 1.1
    *
  @@ -82,7 +82,6 @@
   import org.xml.sax.*;
   
   import javax.xml.parsers.*;
  -import javax.xml.transform.ErrorListener;
   
   import org.apache.xalan.xsltc.compiler.util.*;
   import org.apache.xalan.xsltc.cmdline.getopt.*;
  @@ -91,8 +90,11 @@
   
   public final class XSLTC {
   
  -    // A reference to the main parsergobject.
  +    // A reference to the main stylesheet parser object.
       private final Parser _parser;
  +
  +    // A reference to an external XMLReader (SAX parser) passed to us
  +    private XMLReader _reader = null;
       
       // A reference to the stylesheet being compiled.
       private Stylesheet _stylesheet = null;
  @@ -134,7 +136,7 @@
   
       private Vector  _classes;
       private boolean _multiDocument = false;
  -    
  +
       /**
        * XSLTC compiler constructor
        */
  @@ -154,6 +156,7 @@
        * Initializes the compiler to produce a new translet
        */
       private void reset() {
  +     _reader = null;
        _nextGType      = DOM.NTYPES;
        _elements       = new Hashtable();
        _attributes     = new Hashtable();
  @@ -178,81 +181,91 @@
       
       /**
        * Compiles an XSL stylesheet pointed to by a URL
  +     * @param url An URL containing the input XSL stylesheet
        */
  -    public boolean compile(URL url) { 
  -     return compile(url, (ErrorListener)null);
  +    public boolean compile(URL url) {
  +     try {
  +         // Open input stream from URL and wrap inside InputSource
  +         final InputStream stream = url.openStream();
  +         final InputSource input = new InputSource(stream);
  +         input.setSystemId(url.toString());
  +         return compile(input, _className);
  +     }
  +     catch (IOException e) {
  +         _parser.reportError(Constants.FATAL, new ErrorMsg(e.getMessage()));
  +         return false;
  +     }
       }
   
       /**
        * Compiles an XSL stylesheet pointed to by a URL
  -     *   @listener parameter may be null
  +     * @param url An URL containing the input XSL stylesheet
  +     * @param name The name to assign to the translet class
        */
  -    public boolean compile(URL url, ErrorListener listener) {
  +    public boolean compile(URL url, String name) {
        try {
  -         // Open input stream from URL
  -         final InputStream input = url.openStream();
  -         // Get class name from URL if not explicitly set
  -         if (_className == null) {
  -             final String name = Util.baseName(url.getFile());
  -             return compile(input, name, url, listener);
  -         }
  -         else {
  -             return compile(input, _className, url, listener);
  -         }
  +         // Open input stream from URL and wrap inside InputSource
  +         final InputStream stream = url.openStream();
  +         final InputSource input = new InputSource(stream);
  +         input.setSystemId(url.toString());
  +         return compile(input, name);
        }
  -     catch (MalformedURLException e) {
  -         _parser.reportError(Constants.FATAL, new ErrorMsg(e.getMessage()));
  -         _parser.printErrors();
  -         return false;
  -     }
        catch (IOException e) {
            _parser.reportError(Constants.FATAL, new ErrorMsg(e.getMessage()));
  -         _parser.printErrors();
            return false;
        }
       }
   
       /**
        * Compiles an XSL stylesheet passed in through an InputStream
  -     *   @transletName parameter must be set since the compiler cannot get
  -     *   the XSL stylesheet name from the input stream
  -     */
  -    public boolean compile(InputStream input, String transletName) {
  -     return compile(input, transletName, null, null);
  +     * @param input An InputStream that will pass in the stylesheet contents
  +     * @param name The name of the translet class to generate
  +     * @return 'true' if the compilation was successful
  +     */
  +    public boolean compile(InputStream stream, String name) {
  +     final InputSource input = new InputSource(stream);
  +     input.setSystemId(name); // We have nothing else!!!
  +     return compile(input, name);
       }
   
       /**
        * Compiles an XSL stylesheet passed in through an InputStream
  -     *   @input passes the stylesheet to the compiler
  -     *   @transletName may be set, name taken from URL if this param is null
  -     *   @url parameter must be set
  -     *   @listener parameter may be null
  -     */
  -    public boolean compile(InputStream input,
  -                        String transletName,
  -                        URL url,
  -                        ErrorListener listener) {
  -
  -     // Set the parser's error listener if defined
  -     if (listener != null) _parser.setErrorListener(listener);
  +     * @param input An InputStream that will pass in the stylesheet contents
  +     * @param name The name of the translet class to generate - can be null
  +     * @param url Identifies the original stylesheet location
  +     * @return 'true' if the compilation was successful
  +     */
  +    public boolean compile(InputSource input, String name) {
   
        try {
            // Reset globals in case we're called by compile(Vector v);
            reset();
   
  -         // Set the translet's class name
  -         if (transletName == null) 
  -             transletName = Util.baseName(url.getFile());
  -         setClassName(transletName);
  +         // The systemId may not be set, so we'll have to check the URL
  +         String systemId = input.getSystemId();
  +
  +         // Set the translet class name if not already set
  +         if (_className == null) {
  +             if (name != null)
  +                 setClassName(name);
  +             else if (systemId != null)
  +                 setClassName(Util.baseName(systemId));
  +             else
  +                 setClassName("GregorSamsa"); // default translet name
  +         }
   
            // Get the root node of the abstract syntax tree
  -         final SyntaxTreeNode element = _parser.parse(input);
  +         SyntaxTreeNode element = null;
  +         if (_reader == null)
  +             element = _parser.parse(input);
  +         else
  +             element = _parser.parse(_reader, input);
   
            // Compile the translet - this is where the work is done!
            if ((!_parser.errorsFound()) && (element != null)) {
                // Create a Stylesheet element from the root node
                _stylesheet = _parser.makeStylesheet(element);
  -             _stylesheet.setURL(url);
  +             _stylesheet.setSystemId(systemId);
                _stylesheet.setParentStylesheet(null);
                _parser.setCurrentStylesheet(_stylesheet);
                // Create AST under the Stylesheet element (parse & type-check)
  @@ -268,14 +281,14 @@
            _parser.reportError(Constants.FATAL, new ErrorMsg(e.getMessage()));
        }
        finally {
  -         _parser.printErrors();
  -         _parser.printWarnings();
            return !_parser.errorsFound();
        }
       }
   
       /**
        * Compiles a set of stylesheets pointed to by a Vector of URLs
  +     * @param stylesheets A Vector containing URLs pointing to the stylesheets
  +     * @return 'true' if the compilation was successful
        */
       public boolean compile(Vector stylesheets) {
        // Get the number of stylesheets (ie. URLs) in the vector
  @@ -309,42 +322,110 @@
       /**
        * Compiles a stylesheet pointed to by a URL. The result is put in a
        * set of byte arrays. One byte array for each generated class.
  +     * @param name The name of the translet class to generate
  +     * @param input An InputSource that will pass in the stylesheet contents
  +     * @return JVM bytecodes that represent translet class definition
        */
  -    public byte[][] compile(URL stylesheetURL, String className) {
  +    public byte[][] compile(String name, InputSource input) {
        _outputType = BYTEARRAY_OUTPUT;
  -     setClassName(className);
  -     if (compile(stylesheetURL)) {
  +     if (compile(input, name)) {
            final int count = _classes.size();
            final byte[][] result = new byte[count][1];
            for (int i = 0; i < count; i++)
                result[i] = (byte[])_classes.elementAt(i);
            return result;
        }
  +     /*
  +     catch (IOException e) {
  +         _parser.reportError(Constants.FATAL, new ErrorMsg(e.getMessage()));
  +         return null;
  +     }
  +     */
        return null;
       }
   
       /**
        * Compiles a stylesheet pointed to by a URL. The result is put in a
        * set of byte arrays. One byte array for each generated class.
  +     * @param name The name of the translet class to generate
  +     * @param input An InputStream that will pass in the stylesheet contents
  +     * @return JVM bytecodes that represent translet class definition
        */
  -    public byte[][] compile(InputStream source, String className, int dummy) {
  +    /*
  +    public byte[][] compile(InputStream stream, String systemId, String name) {
        _outputType = BYTEARRAY_OUTPUT;
  -     setClassName(className);
  -     if (compile(source, className)) {
  -         final int count = _classes.size();
  -         final byte[][] result = new byte[1][count];
  -         for (int i = 0; i < count; i++)
  -             result[i] = (byte[])_classes.elementAt(i);
  -         return result;
  -     }
  -     return null;
  +     final InputSource input = new InputSource(stream);
  +     input.setSystemId(systemId);
  +     if (compile(input, name, null))
  +         return getBytecodes();
  +     else
  +         return null;
  +    }
  +    */
  +
  +    /**
  +     * Compiles a stylesheet pointed to by a URL. The result is put in a
  +     * set of byte arrays. One byte array for each generated class.
  +     * @param name The name of the translet class to generate
  +     * @param reader The Reader object to get the stylesheet contents from
  +     * @return JVM bytecodes that represent translet class definition
  +     */
  +    /*
  +    public byte[][] compile(Reader reader, String systemId, String name) {
  +     _outputType = BYTEARRAY_OUTPUT;
  +     final InputSource input = new InputSource(reader);
  +     input.setSystemId(systemId);
  +     if (compile(input, name, null))
  +         return getBytecodes();
  +     else
  +         return null;
  +    }
  +    */
  +    
  +    /**
  +     * Set the XMLReader to use for parsing the next input stylesheet
  +     * @param reader XMLReader (SAX2 parser) to use
  +     */
  +    public void setXMLReader(XMLReader reader) {
  +     _reader = reader;
  +    }
  +
  +    /**
  +     * Get a Vector containing all compile error messages
  +     * @return A Vector containing all compile error messages
  +     */
  +    public Vector getErrors() {
  +     return _parser.getErrors();
  +    }
  +
  +    /**
  +     * Get a Vector containing all compile warning messages
  +     * @return A Vector containing all compile error messages
  +     */
  +    public Vector getWarnings() {
  +     return _parser.getWarnings();
       }
   
       /**
  +     * Print all compile error messages to standard output
  +     */
  +    public void printErrors() {
  +     _parser.printErrors();
  +    }
  +
  +    /**
  +     * Print all compile warning messages to standard output
  +     */
  +    public void printWarnings() {
  +     _parser.printWarnings();
  +    }
  +
  +
  +    /**
        * This method is called by the XPathParser when it encounters a call
        * to the document() function. Affects the DOM used by the translet.
        */
  -    public void setMultiDocument(boolean flag) {
  +    protected void setMultiDocument(boolean flag) {
        _multiDocument = flag;
       }
   
  @@ -352,6 +433,7 @@
        * Set the class name for the generated translet. This class name is
        * overridden if multiple stylesheets are compiled in one go using the
        * compile(Vector urls) method.
  +     * @param className The name to assign to the translet class
        */
       public void setClassName(String className) {
        final String base  = Util.baseName(className);
  @@ -564,7 +646,7 @@
       /**
        * File separators are converted to forward slashes for ZIP files.
        */
  -    private String entryName( File f ) throws IOException {
  +    private String entryName(File f) throws IOException {
        return f.getName().replace(File.separatorChar, '/');
       }
       
  @@ -574,8 +656,8 @@
       public void outputToJar() throws IOException {
        // create the manifest
        final Manifest manifest = new Manifest();
  -     manifest.getMainAttributes()
  -         .put(java.util.jar.Attributes.Name.MANIFEST_VERSION, "1.0");
  +     final java.util.jar.Attributes atrs = manifest.getMainAttributes();
  +     atrs.put(java.util.jar.Attributes.Name.MANIFEST_VERSION,"1.0");
   
        final Map map = manifest.getEntries();
        // create manifest
  @@ -585,8 +667,7 @@
            new java.util.jar.Attributes.Name("Date");
        while (classes.hasMoreElements()) {
            final JavaClass clazz = (JavaClass)classes.nextElement();
  -         final java.util.jar.Attributes attr = 
  -             new java.util.jar.Attributes();
  +         final java.util.jar.Attributes attr = new java.util.jar.Attributes();
            attr.put(dateAttr, now);
            map.put(classFileName(clazz.getClassName()), attr);
        }
  
  
  
  1.3       +5 -7      
xml-xalan/java/src/org/apache/xalan/xsltc/compiler/util/ErrorMsg.java
  
  Index: ErrorMsg.java
  ===================================================================
  RCS file: 
/home/cvs/xml-xalan/java/src/org/apache/xalan/xsltc/compiler/util/ErrorMsg.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- ErrorMsg.java     2001/07/10 17:46:08     1.2
  +++ ErrorMsg.java     2001/07/19 18:48:31     1.3
  @@ -1,5 +1,5 @@
   /*
  - * @(#)$Id: ErrorMsg.java,v 1.2 2001/07/10 17:46:08 morten Exp $
  + * @(#)$Id: ErrorMsg.java,v 1.3 2001/07/19 18:48:31 morten Exp $
    *
    * The Apache Software License, Version 1.1
    *
  @@ -98,6 +98,7 @@
       public static final int ILLEG_PI_ERR = 18;
       public static final int ATTROUTS_ERR = 19;
       public static final int ILL_ATTR_ERR = 20;
  +    public static final int CIRCULAR_INC = 21;
   
       static final String messages_d[] = { 
        "More than one stylesheet defined in the same file.",
  @@ -120,7 +121,8 @@
        "Illegal use of function ''{0}''.",
        "Illegal name ''{0}'' for processing instruction.",
        "Attribute ''{0}'' outside of element.",
  -     "Illegal attribute name ''{0}''."
  +     "Illegal attribute name ''{0}''.",
  +     "Circular import/include. Stylesheet ''{0}'' already loaded."
       };
   
       public ErrorMsg(int code) {
  @@ -184,11 +186,7 @@
       }
   
       private String getFileName(SyntaxTreeNode node) {
  -     final URL url = node.getStylesheet().getURL();
  -     if (url != null)
  -         return url.toString();
  -     else
  -         return null;
  +     return node.getStylesheet().getSystemId();
       }
   
       private String formatLine() {
  
  
  
  1.8       +111 -59   
xml-xalan/java/src/org/apache/xalan/xsltc/trax/TransformerFactoryImpl.java
  
  Index: TransformerFactoryImpl.java
  ===================================================================
  RCS file: 
/home/cvs/xml-xalan/java/src/org/apache/xalan/xsltc/trax/TransformerFactoryImpl.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- TransformerFactoryImpl.java       2001/07/17 15:34:13     1.7
  +++ TransformerFactoryImpl.java       2001/07/19 18:48:31     1.8
  @@ -1,5 +1,5 @@
   /*
  - * @(#)$Id: TransformerFactoryImpl.java,v 1.7 2001/07/17 15:34:13 morten Exp $
  + * @(#)$Id: TransformerFactoryImpl.java,v 1.8 2001/07/19 18:48:31 morten Exp $
    *
    * The Apache Software License, Version 1.1
    *
  @@ -57,28 +57,26 @@
    * <http://www.apache.org/>.
    *
    * @author G. Todd Miller 
  + * @author Morten Jorgensen
    *
    */
   
   
   package org.apache.xalan.xsltc.trax;
   
  -import javax.xml.transform.Templates; 
  -import javax.xml.transform.Transformer; 
  -import javax.xml.transform.TransformerFactory; 
  -import javax.xml.transform.TransformerException; 
  -import javax.xml.transform.ErrorListener; 
  -import javax.xml.transform.Source; 
  -import javax.xml.transform.stream.StreamSource; 
  -import javax.xml.transform.stream.StreamResult; 
  -import javax.xml.transform.URIResolver; 
  -import javax.xml.transform.TransformerConfigurationException; 
  -
  -import javax.xml.transform.sax.SAXTransformerFactory; 
  -import javax.xml.transform.sax.TemplatesHandler;
  -import javax.xml.transform.sax.TransformerHandler;
  +import java.io.Reader;
  +import java.io.InputStream;
  +import java.io.ByteArrayInputStream;
  +import java.net.URL;
  +import java.net.MalformedURLException;
  +import java.util.Vector;
   
  +import javax.xml.transform.*;
  +import javax.xml.transform.stream.*;
  +import javax.xml.transform.sax.*;
  +
   import org.xml.sax.XMLFilter;
  +import org.xml.sax.InputSource;
   
   import org.apache.xalan.xsltc.Translet;
   import org.apache.xalan.xsltc.compiler.XSLTC;
  @@ -86,13 +84,6 @@
   import org.apache.xalan.xsltc.compiler.util.Util;
   import org.apache.xalan.xsltc.runtime.AbstractTranslet;
   
  -import java.io.File;
  -import java.io.InputStream;
  -import java.io.ByteArrayInputStream;
  -import java.net.URL;
  -import java.net.MalformedURLException;
  -
  -
   /**
    * Implementation of a JAXP1.1 SAXTransformerFactory for Translets.
    */
  @@ -119,12 +110,12 @@
       // All used error messages should be listed here
       private static final String ERROR_LISTENER_NULL =
        "Attempting to set ErrorListener for TransformerFactory to null";
  -    private static final String SOURCE_NOT_SUPPORTED =
  +    private static final String UNKNOWN_SOURCE_ERR =
        "Only StreamSource is supported by XSLTC";
  -    private static final String STREAM_SOURCE_ERROR =
  -     "StreamSource must have a system id or be an InputStream";
  -    private static final String COMPILATION_ERROR =
  +    private static final String COMPILE_ERR =
        "Could not compile stylesheet";
  +    private static final String SOURCE_CONTENTS_ERR =
  +     "The input Source contains an invalid system id";
   
   
       /**
  @@ -275,12 +266,14 @@
   
        // Compile the default copy-stylesheet
        byte[] bytes = COPY_TRANSLET_CODE.getBytes();
  -     ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);
  -     bytecodes = xsltc.compile(inputStream, COPY_TRANSLET_NAME, 77);
  +     ByteArrayInputStream bytestream = new ByteArrayInputStream(bytes);
  +     InputSource input = new InputSource(bytestream);
  +     input.setSystemId(COPY_TRANSLET_NAME);
  +     bytecodes = xsltc.compile(COPY_TRANSLET_NAME, input);
   
        // Check that the transformation went well before returning
        if (bytecodes == null) {
  -         throw new TransformerConfigurationException(COMPILATION_ERROR);
  +         throw new TransformerConfigurationException(COMPILE_ERR);
        }
   
        // Create a Transformer object and store for other calls
  @@ -306,6 +299,48 @@
       }
   
       /**
  +     * Pass warning messages from the compiler to the error listener
  +     */
  +    private void passWarningsToListener(Vector messages) {
  +     try {
  +         // Nothing to do if there is no registered error listener
  +         if (_errorListener == null) return;
  +         // Nothing to do if there are not warning messages
  +         if (messages == null) return;
  +         // Pass messages to listener, one by one
  +         final int count = messages.size();
  +         for (int pos=0; pos<count; pos++) {
  +             String message = messages.elementAt(pos).toString();
  +             _errorListener.warning(new TransformerException(message));
  +         }
  +     }
  +     catch (TransformerException e) {
  +         // nada
  +     }
  +    }
  +
  +    /**
  +     * Pass error messages from the compiler to the error listener
  +     */
  +    private void passErrorsToListener(Vector messages) {
  +     try {
  +         // Nothing to do if there is no registered error listener
  +         if (_errorListener == null) return;
  +         // Nothing to do if there are not warning messages
  +         if (messages == null) return;
  +         // Pass messages to listener, one by one
  +         final int count = messages.size();
  +         for (int pos=0; pos<count; pos++) {
  +             String message = messages.elementAt(pos).toString();
  +             _errorListener.error(new TransformerException(message));
  +         }
  +     }
  +     catch (TransformerException e) {
  +         // nada
  +     }
  +    }
  +
  +    /**
        * javax.xml.transform.sax.TransformerFactory implementation.
        * Process the Source into a Templates object, which is a a compiled
        * representation of the source.
  @@ -316,51 +351,68 @@
        * @return A Templates object that can be used to create Transformers.
        * @throws TransformerConfigurationException
        */
  -    public Templates newTemplates(Source stylesheet)
  +    public Templates newTemplates(Source source)
        throws TransformerConfigurationException {
   
  -     byte[][] bytecodes = null; // The translet classes go in here
  +     // Create a placeholder for the translet bytecodes
  +     byte[][] bytecodes = null;
   
  +     // Create and initialize a stylesheet compiler
        XSLTC xsltc = new XSLTC();
        xsltc.init();
   
  -     // Attempt using the URL returned by the Source's getSystemId() method
  -        final String stylesheetName = stylesheet.getSystemId();
  -        String transletName = "undefined";
  -     URL url = null;
  -     if (stylesheetName != null) {
  -         final String base  = Util.baseName(stylesheetName);
  -         final String noext = Util.noExtName(base);
  -            transletName = Util.toJavaName(noext);
  -         try {
  -             if (stylesheetName.startsWith("file:/")) {
  -                 url = new URL(stylesheetName);
  -             } else {
  -                 url = (new File(stylesheetName)).toURL();
  -             }
  -         }
  -         catch (MalformedURLException e) { url = null; }
  +     // Handle SAXSource input
  +     if (source instanceof SAXSource) {
  +         final SAXSource sax = (SAXSource)source;
  +         InputSource input = sax.getInputSource();
  +         String systemId = sax.getSystemId();
  +
  +         // Pass the SAX parser to the compiler
  +         xsltc.setXMLReader(sax.getXMLReader());
  +
  +         // Then pass the XSL stylesheet doc to the compiler
  +         if ((input == null) && (systemId != null))
  +             input = new InputSource(systemId);
  +         // Pass system id to InputSource just to be on the safe side
  +         input.setSystemId(systemId);
  +         // Compile the stylesheet
  +         bytecodes = xsltc.compile(null, input);
        }
  +     // Handle StreamSource input
  +     else if (source instanceof StreamSource) {
  +         final StreamSource stream = (StreamSource)source;
  +         final InputStream input = stream.getInputStream();
  +         final Reader reader = stream.getReader();
  +         final String systemId = stream.getSystemId();
  +
  +         InputSource blob;
  +         if (input != null)
  +             blob = new InputSource(input);
  +         else if (reader != null)
  +             blob = new InputSource(reader);
  +         else if (systemId != null)
  +             blob = new InputSource(systemId);
  +         else
  +             blob = null; // TODO - signal error!!!!
   
  -     // Now do the actual compilation - store results in the bytecodes array
  -     if (url != null) {
  -         bytecodes = xsltc.compile(url, transletName);
  -     }
  -     else if (stylesheet instanceof StreamSource) {
  -         StreamSource streamSource = (StreamSource)stylesheet;
  -         InputStream inputStream = streamSource.getInputStream();
  -         if (inputStream != null)
  -             bytecodes = xsltc.compile(inputStream, transletName, 77);
  +         blob.setSystemId(systemId);
  +         bytecodes = xsltc.compile(null, blob);
        }
        else {
  -         throw new TransformerConfigurationException(STREAM_SOURCE_ERROR);
  +         throw new TransformerConfigurationException(UNKNOWN_SOURCE_ERR);
        }
   
  +     final String transletName = xsltc.getClassName();
  +
  +     // Pass compiler warnings to the error listener
  +     passWarningsToListener(xsltc.getWarnings());
  +
        // Check that the transformation went well before returning
        if (bytecodes == null) {
  -         throw new TransformerConfigurationException(COMPILATION_ERROR);
  +         // Pass compiler errors to the error listener
  +         passErrorsToListener(xsltc.getErrors());
  +         throw new TransformerConfigurationException(COMPILE_ERR);
        }
  -
        return(new TemplatesImpl(bytecodes, transletName));
       }
   
  
  
  
  1.6       +4 -1      
xml-xalan/java/src/org/apache/xalan/xsltc/trax/TransformerImpl.java
  
  Index: TransformerImpl.java
  ===================================================================
  RCS file: 
/home/cvs/xml-xalan/java/src/org/apache/xalan/xsltc/trax/TransformerImpl.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- TransformerImpl.java      2001/07/19 13:16:17     1.5
  +++ TransformerImpl.java      2001/07/19 18:48:31     1.6
  @@ -1,5 +1,5 @@
   /*
  - * @(#)$Id: TransformerImpl.java,v 1.5 2001/07/19 13:16:17 morten Exp $
  + * @(#)$Id: TransformerImpl.java,v 1.6 2001/07/19 18:48:31 morten Exp $
    *
    * The Apache Software License, Version 1.1
    *
  @@ -459,6 +459,9 @@
        */
       private void setOutputProperties(AbstractTranslet translet,
                                     Properties properties) {
  +     // Return right now if no properties are set
  +     if (properties == null) return;
  +
        // Get a list of all the defined properties
        Enumeration names = properties.propertyNames();
        while (names.hasMoreElements()) {
  
  
  

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

Reply via email to