< prev index next >

src/java.xml/share/classes/com/sun/org/apache/xalan/internal/xsltc/compiler/Parser.java

Print this page


   1 /*
   2  * Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.
   3  */
   4 /*
   5  * Licensed to the Apache Software Foundation (ASF) under one or more
   6  * contributor license agreements.  See the NOTICE file distributed with
   7  * this work for additional information regarding copyright ownership.
   8  * The ASF licenses this file to You under the Apache License, Version 2.0
   9  * (the "License"); you may not use this file except in compliance with
  10  * the License.  You may obtain a copy of the License at
  11  *
  12  *     http://www.apache.org/licenses/LICENSE-2.0
  13  *
  14  * Unless required by applicable law or agreed to in writing, software
  15  * distributed under the License is distributed on an "AS IS" BASIS,
  16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17  * See the License for the specific language governing permissions and
  18  * limitations under the License.
  19  */
  20 
  21 package com.sun.org.apache.xalan.internal.xsltc.compiler;
  22 


  25 import com.sun.org.apache.xalan.internal.utils.ObjectFactory;
  26 import com.sun.org.apache.xalan.internal.utils.XMLSecurityManager;
  27 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ErrorMsg;
  28 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.MethodType;
  29 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.Type;
  30 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.TypeCheckError;
  31 import com.sun.org.apache.xml.internal.serializer.utils.SystemIDResolver;
  32 import java.io.File;
  33 import java.io.IOException;
  34 import java.io.StringReader;
  35 import java.util.ArrayList;
  36 import java.util.HashMap;
  37 import java.util.Iterator;
  38 import java.util.List;
  39 import java.util.Map;
  40 import java.util.Properties;
  41 import java.util.Stack;
  42 import java.util.StringTokenizer;
  43 import javax.xml.XMLConstants;
  44 import javax.xml.catalog.CatalogFeatures;

  45 import jdk.xml.internal.JdkXmlFeatures;
  46 import jdk.xml.internal.JdkXmlUtils;
  47 import jdk.xml.internal.SecuritySupport;
  48 import org.xml.sax.Attributes;
  49 import org.xml.sax.ContentHandler;
  50 import org.xml.sax.InputSource;
  51 import org.xml.sax.Locator;
  52 import org.xml.sax.SAXException;
  53 import org.xml.sax.SAXNotRecognizedException;
  54 import org.xml.sax.SAXNotSupportedException;
  55 import org.xml.sax.XMLReader;
  56 import org.xml.sax.helpers.AttributesImpl;
  57 
  58 /**
  59  * @author Jacek Ambroziak
  60  * @author Santiago Pericas-Geertsen
  61  * @author G. Todd Miller
  62  * @author Morten Jorgensen
  63  * @author Erwin Bolwidt <ejb@klomp.org>
  64  * @LastModified: Nov 2017
  65  */
  66 public class Parser implements Constants, ContentHandler {
  67 
  68     private static final String XSL = "xsl";           // standard prefix
  69     private static final String TRANSLET = "translet"; // extension prefix
  70 
  71     private Locator _locator = null;
  72 
  73     private XSLTC _xsltc;                  // Reference to the compiler object.
  74     private XPathParser _xpathParser;      // Reference to the XPath parser.
  75     private ArrayList<ErrorMsg> _errors;   // Contains all compilation errors
  76     private ArrayList<ErrorMsg> _warnings; // Contains all compilation warnings
  77 
  78     private Map<String, String>   _instructionClasses; // Maps instructions to classes
  79     private Map<String, String[]> _instructionAttrs;  // reqd and opt attrs
  80     private Map<String, QName>    _qNames;
  81     private Map<String, Map<String, QName>> _namespaces;
  82     private QName       _useAttributeSets;
  83     private QName       _excludeResultPrefixes;
  84     private QName       _extensionElementPrefixes;
  85     private Map<String, Object>   _variableScope;
  86     private Stylesheet  _currentStylesheet;
  87     private SymbolTable _symbolTable; // Maps QNames to syntax-tree nodes
  88     private Output      _output;
  89     private Template    _template;    // Reference to the template being parsed.
  90 
  91     private boolean     _rootNamespaceDef; // Used for validity check
  92 
  93     private SyntaxTreeNode _root;
  94 
  95     private String _target;
  96 
  97     private int _currentImportPrecedence;
  98 
  99     private boolean _overrideDefaultParser;
 100 
 101     public Parser(XSLTC xsltc, boolean useOverrideDefaultParser) {



 102         _xsltc = xsltc;
 103         _overrideDefaultParser = useOverrideDefaultParser;

 104     }
 105 
 106     public void init() {
 107         _qNames              = new HashMap<>(512);
 108         _namespaces          = new HashMap<>();
 109         _instructionClasses  = new HashMap<>();
 110         _instructionAttrs    = new HashMap<>();
 111         _variableScope       = new HashMap<>();
 112         _template            = null;
 113         _errors              = new ArrayList<>();
 114         _warnings            = new ArrayList<>();
 115         _symbolTable         = new SymbolTable();
 116         _xpathParser         = new XPathParser(this);
 117         _currentStylesheet   = null;
 118         _output              = null;
 119         _root                = null;
 120         _rootNamespaceDef    = false;
 121         _currentImportPrecedence = 1;
 122 
 123         initStdClasses();


 409                 if (!errorsFound()) {
 410                     stylesheet.typeCheck(_symbolTable);
 411                 }
 412             }
 413         }
 414         catch (TypeCheckError e) {
 415             reportError(ERROR, new ErrorMsg(ErrorMsg.JAXP_COMPILE_ERR, e));
 416         }
 417     }
 418 
 419     /**
 420      * Parses a stylesheet and builds the internal abstract syntax tree
 421      * @param reader A SAX2 SAXReader (parser)
 422      * @param input A SAX2 InputSource can be passed to a SAX reader
 423      * @return The root of the abstract syntax tree
 424      */
 425     public SyntaxTreeNode parse(XMLReader reader, InputSource input) {
 426         try {
 427             // Parse the input document and build the abstract syntax tree
 428             reader.setContentHandler(this);





 429             reader.parse(input);
 430             // Find the start of the stylesheet within the tree
 431             return getStylesheet(_root);
 432         }
 433         catch (IOException e) {
 434             if (_xsltc.debug()) e.printStackTrace();
 435             reportError(ERROR,new ErrorMsg(ErrorMsg.JAXP_COMPILE_ERR, e));
 436         }
 437         catch (SAXException e) {
 438             Throwable ex = e.getException();
 439             if (_xsltc.debug()) {
 440                 e.printStackTrace();
 441                 if (ex != null) ex.printStackTrace();
 442             }
 443             reportError(ERROR, new ErrorMsg(ErrorMsg.JAXP_COMPILE_ERR, e));
 444         }
 445         catch (CompilerException e) {
 446             if (_xsltc.debug()) e.printStackTrace();
 447             reportError(ERROR, new ErrorMsg(ErrorMsg.JAXP_COMPILE_ERR, e));
 448         }


   1 /*
   2  * Copyright (c) 2015, 2019, Oracle and/or its affiliates. All rights reserved.
   3  */
   4 /*
   5  * Licensed to the Apache Software Foundation (ASF) under one or more
   6  * contributor license agreements.  See the NOTICE file distributed with
   7  * this work for additional information regarding copyright ownership.
   8  * The ASF licenses this file to You under the Apache License, Version 2.0
   9  * (the "License"); you may not use this file except in compliance with
  10  * the License.  You may obtain a copy of the License at
  11  *
  12  *     http://www.apache.org/licenses/LICENSE-2.0
  13  *
  14  * Unless required by applicable law or agreed to in writing, software
  15  * distributed under the License is distributed on an "AS IS" BASIS,
  16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17  * See the License for the specific language governing permissions and
  18  * limitations under the License.
  19  */
  20 
  21 package com.sun.org.apache.xalan.internal.xsltc.compiler;
  22 


  25 import com.sun.org.apache.xalan.internal.utils.ObjectFactory;
  26 import com.sun.org.apache.xalan.internal.utils.XMLSecurityManager;
  27 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ErrorMsg;
  28 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.MethodType;
  29 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.Type;
  30 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.TypeCheckError;
  31 import com.sun.org.apache.xml.internal.serializer.utils.SystemIDResolver;
  32 import java.io.File;
  33 import java.io.IOException;
  34 import java.io.StringReader;
  35 import java.util.ArrayList;
  36 import java.util.HashMap;
  37 import java.util.Iterator;
  38 import java.util.List;
  39 import java.util.Map;
  40 import java.util.Properties;
  41 import java.util.Stack;
  42 import java.util.StringTokenizer;
  43 import javax.xml.XMLConstants;
  44 import javax.xml.catalog.CatalogFeatures;
  45 import jdk.xml.internal.ErrorHandlerProxy;
  46 import jdk.xml.internal.JdkXmlFeatures;
  47 import jdk.xml.internal.JdkXmlUtils;
  48 import jdk.xml.internal.SecuritySupport;
  49 import org.xml.sax.Attributes;
  50 import org.xml.sax.ContentHandler;
  51 import org.xml.sax.InputSource;
  52 import org.xml.sax.Locator;
  53 import org.xml.sax.SAXException;
  54 import org.xml.sax.SAXNotRecognizedException;
  55 import org.xml.sax.SAXNotSupportedException;
  56 import org.xml.sax.XMLReader;
  57 import org.xml.sax.helpers.AttributesImpl;
  58 
  59 /**
  60  * @author Jacek Ambroziak
  61  * @author Santiago Pericas-Geertsen
  62  * @author G. Todd Miller
  63  * @author Morten Jorgensen
  64  * @author Erwin Bolwidt <ejb@klomp.org>
  65  * @LastModified: July 2019
  66  */
  67 public class Parser implements Constants, ContentHandler {
  68 
  69     private static final String XSL = "xsl";           // standard prefix
  70     private static final String TRANSLET = "translet"; // extension prefix
  71 
  72     private Locator _locator = null;
  73 
  74     private XSLTC _xsltc;                  // Reference to the compiler object.
  75     private XPathParser _xpathParser;      // Reference to the XPath parser.
  76     private ArrayList<ErrorMsg> _errors;   // Contains all compilation errors
  77     private ArrayList<ErrorMsg> _warnings; // Contains all compilation warnings
  78 
  79     private Map<String, String>   _instructionClasses; // Maps instructions to classes
  80     private Map<String, String[]> _instructionAttrs;  // reqd and opt attrs
  81     private Map<String, QName>    _qNames;
  82     private Map<String, Map<String, QName>> _namespaces;
  83     private QName       _useAttributeSets;
  84     private QName       _excludeResultPrefixes;
  85     private QName       _extensionElementPrefixes;
  86     private Map<String, Object>   _variableScope;
  87     private Stylesheet  _currentStylesheet;
  88     private SymbolTable _symbolTable; // Maps QNames to syntax-tree nodes
  89     private Output      _output;
  90     private Template    _template;    // Reference to the template being parsed.
  91 
  92     private boolean     _rootNamespaceDef; // Used for validity check
  93 
  94     private SyntaxTreeNode _root;
  95 
  96     private String _target;
  97 
  98     private int _currentImportPrecedence;
  99 
 100     private boolean _overrideDefaultParser;
 101 
 102     // flag indicates whether there's an user's ErrorListener
 103     private boolean _hasUserErrListener;
 104 
 105     public Parser(XSLTC xsltc, boolean useOverrideDefaultParser, boolean hasUserErrListener) {
 106         _xsltc = xsltc;
 107         _overrideDefaultParser = useOverrideDefaultParser;
 108         _hasUserErrListener = hasUserErrListener;
 109     }
 110 
 111     public void init() {
 112         _qNames              = new HashMap<>(512);
 113         _namespaces          = new HashMap<>();
 114         _instructionClasses  = new HashMap<>();
 115         _instructionAttrs    = new HashMap<>();
 116         _variableScope       = new HashMap<>();
 117         _template            = null;
 118         _errors              = new ArrayList<>();
 119         _warnings            = new ArrayList<>();
 120         _symbolTable         = new SymbolTable();
 121         _xpathParser         = new XPathParser(this);
 122         _currentStylesheet   = null;
 123         _output              = null;
 124         _root                = null;
 125         _rootNamespaceDef    = false;
 126         _currentImportPrecedence = 1;
 127 
 128         initStdClasses();


 414                 if (!errorsFound()) {
 415                     stylesheet.typeCheck(_symbolTable);
 416                 }
 417             }
 418         }
 419         catch (TypeCheckError e) {
 420             reportError(ERROR, new ErrorMsg(ErrorMsg.JAXP_COMPILE_ERR, e));
 421         }
 422     }
 423 
 424     /**
 425      * Parses a stylesheet and builds the internal abstract syntax tree
 426      * @param reader A SAX2 SAXReader (parser)
 427      * @param input A SAX2 InputSource can be passed to a SAX reader
 428      * @return The root of the abstract syntax tree
 429      */
 430     public SyntaxTreeNode parse(XMLReader reader, InputSource input) {
 431         try {
 432             // Parse the input document and build the abstract syntax tree
 433             reader.setContentHandler(this);
 434             if (_hasUserErrListener) {
 435                 // Set a ErrorHandler proxy to pass any parsing error on to be handled
 436                 // by the user's ErrorListener
 437                 reader.setErrorHandler(new ErrorHandlerProxy());
 438             }
 439             reader.parse(input);
 440             // Find the start of the stylesheet within the tree
 441             return getStylesheet(_root);
 442         }
 443         catch (IOException e) {
 444             if (_xsltc.debug()) e.printStackTrace();
 445             reportError(ERROR,new ErrorMsg(ErrorMsg.JAXP_COMPILE_ERR, e));
 446         }
 447         catch (SAXException e) {
 448             Throwable ex = e.getException();
 449             if (_xsltc.debug()) {
 450                 e.printStackTrace();
 451                 if (ex != null) ex.printStackTrace();
 452             }
 453             reportError(ERROR, new ErrorMsg(ErrorMsg.JAXP_COMPILE_ERR, e));
 454         }
 455         catch (CompilerException e) {
 456             if (_xsltc.debug()) e.printStackTrace();
 457             reportError(ERROR, new ErrorMsg(ErrorMsg.JAXP_COMPILE_ERR, e));
 458         }


< prev index next >