jaxp/src/java.xml/share/classes/com/sun/org/apache/xalan/internal/xsltc/compiler/XSLTC.java
changeset 39907 db51759e3695
parent 36520 cb52aa47ff0a
child 39909 00e4298ae168
equal deleted inserted replaced
39906:230d872f56ea 39907:db51759e3695
     1 /*
     1 /*
     2  * Copyright (c) 2012, 2015, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2012, 2016, Oracle and/or its affiliates. All rights reserved.
     3  */
     3  */
     4 /*
     4 /*
     5  * Licensed to the Apache Software Foundation (ASF) under one or more
     5  * Licensed to the Apache Software Foundation (ASF) under one or more
     6  * contributor license agreements.  See the NOTICE file distributed with
     6  * contributor license agreements.  See the NOTICE file distributed with
     7  * this work for additional information regarding copyright ownership.
     7  * this work for additional information regarding copyright ownership.
    15  * distributed under the License is distributed on an "AS IS" BASIS,
    15  * distributed under the License is distributed on an "AS IS" BASIS,
    16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    17  * See the License for the specific language governing permissions and
    17  * See the License for the specific language governing permissions and
    18  * limitations under the License.
    18  * limitations under the License.
    19  */
    19  */
    20 /*
       
    21  * $Id: XSLTC.java,v 1.2.4.1 2005/09/05 09:51:38 pvedula Exp $
       
    22  */
       
    23 
    20 
    24 package com.sun.org.apache.xalan.internal.xsltc.compiler;
    21 package com.sun.org.apache.xalan.internal.xsltc.compiler;
    25 
    22 
    26 import com.sun.org.apache.bcel.internal.classfile.JavaClass;
    23 import com.sun.org.apache.bcel.internal.classfile.JavaClass;
    27 import com.sun.org.apache.xalan.internal.XalanConstants;
    24 import com.sun.org.apache.xalan.internal.XalanConstants;
    28 import com.sun.org.apache.xalan.internal.utils.FeatureManager;
       
    29 import com.sun.org.apache.xalan.internal.utils.FeatureManager.Feature;
       
    30 import com.sun.org.apache.xalan.internal.utils.SecuritySupport;
    25 import com.sun.org.apache.xalan.internal.utils.SecuritySupport;
    31 import com.sun.org.apache.xalan.internal.utils.XMLSecurityManager;
    26 import com.sun.org.apache.xalan.internal.utils.XMLSecurityManager;
    32 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ErrorMsg;
    27 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ErrorMsg;
    33 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.Util;
    28 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.Util;
    34 import com.sun.org.apache.xml.internal.dtm.DTM;
    29 import com.sun.org.apache.xml.internal.dtm.DTM;
    37 import java.io.File;
    32 import java.io.File;
    38 import java.io.FileOutputStream;
    33 import java.io.FileOutputStream;
    39 import java.io.IOException;
    34 import java.io.IOException;
    40 import java.io.InputStream;
    35 import java.io.InputStream;
    41 import java.net.URL;
    36 import java.net.URL;
    42 import java.security.AccessController;
    37 import java.util.ArrayList;
    43 import java.security.PrivilegedAction;
       
    44 import java.util.Collections;
    38 import java.util.Collections;
    45 import java.util.Date;
    39 import java.util.Date;
    46 import java.util.Enumeration;
    40 import java.util.Enumeration;
    47 import java.util.HashMap;
    41 import java.util.HashMap;
    48 import java.util.Map;
    42 import java.util.Map;
       
    43 import java.util.Objects;
    49 import java.util.Properties;
    44 import java.util.Properties;
    50 import java.util.Objects;
       
    51 import java.util.Vector;
    45 import java.util.Vector;
    52 import java.util.jar.JarEntry;
    46 import java.util.jar.JarEntry;
    53 import java.util.jar.JarOutputStream;
    47 import java.util.jar.JarOutputStream;
    54 import java.util.jar.Manifest;
    48 import java.util.jar.Manifest;
    55 import javax.xml.XMLConstants;
    49 import javax.xml.XMLConstants;
       
    50 import javax.xml.catalog.CatalogFeatures;
       
    51 import jdk.xml.internal.JdkXmlFeatures;
    56 import org.xml.sax.InputSource;
    52 import org.xml.sax.InputSource;
    57 import org.xml.sax.XMLReader;
    53 import org.xml.sax.XMLReader;
    58 
    54 
    59 /**
    55 /**
    60  * @author Jacek Ambroziak
    56  * @author Jacek Ambroziak
    99     private Map<String, Integer> _namespaces; // Map of all registered namespaces
    95     private Map<String, Integer> _namespaces; // Map of all registered namespaces
   100     private Map<String, Integer> _namespacePrefixes;// Map of all registered namespace prefixes
    96     private Map<String, Integer> _namespacePrefixes;// Map of all registered namespace prefixes
   101 
    97 
   102 
    98 
   103     // All literal text in the stylesheet
    99     // All literal text in the stylesheet
   104     private Vector m_characterData;
   100     private ArrayList<StringBuilder> m_characterData;
   105 
   101 
   106     // These define the various methods for outputting the translet
   102     // These define the various methods for outputting the translet
   107     public static final int FILE_OUTPUT        = 0;
   103     public static final int FILE_OUTPUT        = 0;
   108     public static final int JAR_OUTPUT         = 1;
   104     public static final int JAR_OUTPUT         = 1;
   109     public static final int BYTEARRAY_OUTPUT   = 2;
   105     public static final int BYTEARRAY_OUTPUT   = 2;
   150      */
   146      */
   151     private String _accessExternalDTD = XalanConstants.EXTERNAL_ACCESS_DEFAULT;
   147     private String _accessExternalDTD = XalanConstants.EXTERNAL_ACCESS_DEFAULT;
   152 
   148 
   153     private XMLSecurityManager _xmlSecurityManager;
   149     private XMLSecurityManager _xmlSecurityManager;
   154 
   150 
   155     private final FeatureManager _featureManager;
   151     private final JdkXmlFeatures _xmlFeatures;
   156 
   152 
   157     /**
   153     /**
   158     *  Extension function class loader variables
   154     *  Extension function class loader variables
   159     */
   155     */
   160 
   156 
   165     *  HashSet with the loaded classes
   161     *  HashSet with the loaded classes
   166     */
   162     */
   167     private final Map<String, Class> _externalExtensionFunctions;
   163     private final Map<String, Class> _externalExtensionFunctions;
   168 
   164 
   169     /**
   165     /**
       
   166      * Catalog features
       
   167      */
       
   168     CatalogFeatures _catalogFeatures;
       
   169 
       
   170     /**
   170      * XSLTC compiler constructor
   171      * XSLTC compiler constructor
   171      */
   172      */
   172     public XSLTC(boolean useServicesMechanism, FeatureManager featureManager) {
   173     public XSLTC(boolean useServicesMechanism, JdkXmlFeatures featureManager) {
   173         _parser = new Parser(this, useServicesMechanism);
   174         _parser = new Parser(this, useServicesMechanism);
   174         _featureManager = featureManager;
   175         _xmlFeatures = featureManager;
   175         _extensionClassLoader = null;
   176         _extensionClassLoader = null;
   176         _externalExtensionFunctions = new HashMap<>();
   177         _externalExtensionFunctions = new HashMap<>();
   177     }
   178     }
   178 
   179 
   179     /**
   180     /**
   206      /**
   207      /**
   207      * Return the value of the specified feature
   208      * Return the value of the specified feature
   208      * @param name name of the feature
   209      * @param name name of the feature
   209      * @return true if the feature is enabled, false otherwise
   210      * @return true if the feature is enabled, false otherwise
   210      */
   211      */
   211     public boolean getFeature(Feature name) {
   212     public boolean getFeature(JdkXmlFeatures.XmlFeature name) {
   212         return _featureManager.isFeatureEnabled(name);
   213         return _xmlFeatures.getFeature(name);
   213     }
   214     }
   214 
   215 
   215     /**
   216     /**
   216      * Return allowed protocols for accessing external stylesheet.
   217      * Return allowed protocols for accessing external stylesheet.
       
   218      * @param name the name of the property
       
   219      * @return the value of the property
   217      */
   220      */
   218     public Object getProperty(String name) {
   221     public Object getProperty(String name) {
   219         if (name.equals(XMLConstants.ACCESS_EXTERNAL_STYLESHEET)) {
   222         if (name.equals(XMLConstants.ACCESS_EXTERNAL_STYLESHEET)) {
   220             return _accessExternalStylesheet;
   223             return _accessExternalStylesheet;
   221         }
   224         }
   223             return _accessExternalDTD;
   226             return _accessExternalDTD;
   224         } else if (name.equals(XalanConstants.SECURITY_MANAGER)) {
   227         } else if (name.equals(XalanConstants.SECURITY_MANAGER)) {
   225             return _xmlSecurityManager;
   228             return _xmlSecurityManager;
   226         } else if (name.equals(XalanConstants.JDK_EXTENSION_CLASSLOADER)) {
   229         } else if (name.equals(XalanConstants.JDK_EXTENSION_CLASSLOADER)) {
   227             return _extensionClassLoader;
   230             return _extensionClassLoader;
       
   231         } else if (JdkXmlFeatures.CATALOG_FEATURES.equals(name)) {
       
   232             return _catalogFeatures;
   228         }
   233         }
   229         return null;
   234         return null;
   230     }
   235     }
   231 
   236 
   232     /**
   237     /**
   233      * Set allowed protocols for accessing external stylesheet.
   238      * Set allowed protocols for accessing external stylesheet.
       
   239      * @param name the name of the property
       
   240      * @param value the value of the property
   234      */
   241      */
   235     public void setProperty(String name, Object value) {
   242     public void setProperty(String name, Object value) {
   236         if (name.equals(XMLConstants.ACCESS_EXTERNAL_STYLESHEET)) {
   243         if (name.equals(XMLConstants.ACCESS_EXTERNAL_STYLESHEET)) {
   237             _accessExternalStylesheet = (String)value;
   244             _accessExternalStylesheet = (String)value;
   238         }
   245         }
   243         } else if (name.equals(XalanConstants.JDK_EXTENSION_CLASSLOADER)) {
   250         } else if (name.equals(XalanConstants.JDK_EXTENSION_CLASSLOADER)) {
   244             _extensionClassLoader = (ClassLoader) value;
   251             _extensionClassLoader = (ClassLoader) value;
   245             /* Clear the external extension functions HashMap if extension class
   252             /* Clear the external extension functions HashMap if extension class
   246                loader was changed */
   253                loader was changed */
   247             _externalExtensionFunctions.clear();
   254             _externalExtensionFunctions.clear();
       
   255         } else if (JdkXmlFeatures.CATALOG_FEATURES.equals(name)) {
       
   256             _catalogFeatures = (CatalogFeatures)value;
   248         }
   257         }
   249     }
   258     }
   250 
   259 
   251     /**
   260     /**
   252      * Only for user by the internal TrAX implementation.
   261      * Only for user by the internal TrAX implementation.
   599     public XMLReader getXMLReader() {
   608     public XMLReader getXMLReader() {
   600         return _reader ;
   609         return _reader ;
   601     }
   610     }
   602 
   611 
   603     /**
   612     /**
   604      * Get a Vector containing all compile error messages
   613      * Get a list of all compile error messages
   605      * @return A Vector containing all compile error messages
   614      * @return A List containing all compile error messages
   606      */
   615      */
   607     public Vector getErrors() {
   616     public ArrayList<ErrorMsg> getErrors() {
   608         return _parser.getErrors();
   617         return _parser.getErrors();
   609     }
   618     }
   610 
   619 
   611     /**
   620     /**
   612      * Get a Vector containing all compile warning messages
   621      * Get a list of all compile warning messages
   613      * @return A Vector containing all compile error messages
   622      * @return A List containing all compile error messages
   614      */
   623      */
   615     public Vector getWarnings() {
   624     public ArrayList<ErrorMsg> getWarnings() {
   616         return _parser.getWarnings();
   625         return _parser.getWarnings();
   617     }
   626     }
   618 
   627 
   619     /**
   628     /**
   620      * Print all compile error messages to standard output
   629      * Print all compile error messages to standard output
   989      * @param index The index of the <code>char[]</code>.  Zero-based.
   998      * @param index The index of the <code>char[]</code>.  Zero-based.
   990      * @return String The character data to be stored in the corresponding
   999      * @return String The character data to be stored in the corresponding
   991      *               <code>char[]</code>.
  1000      *               <code>char[]</code>.
   992      */
  1001      */
   993     public String getCharacterData(int index) {
  1002     public String getCharacterData(int index) {
   994         return ((StringBuffer) m_characterData.elementAt(index)).toString();
  1003         return (m_characterData.get(index)).toString();
   995     }
  1004     }
   996 
  1005 
   997     /**
  1006     /**
   998      * Get the number of char[] arrays, thus far, that will be created to
  1007      * Get the number of char[] arrays, thus far, that will be created to
   999      * store literal text in the stylesheet.
  1008      * store literal text in the stylesheet.
  1008      * @param newData String data to be added to char arrays.
  1017      * @param newData String data to be added to char arrays.
  1009      *                Pre-condition:  <code>newData.length() &le; 21845</code>
  1018      *                Pre-condition:  <code>newData.length() &le; 21845</code>
  1010      * @return int offset at which character data will be stored
  1019      * @return int offset at which character data will be stored
  1011      */
  1020      */
  1012     public int addCharacterData(String newData) {
  1021     public int addCharacterData(String newData) {
  1013         StringBuffer currData;
  1022         StringBuilder currData;
  1014         if (m_characterData == null) {
  1023         if (m_characterData == null) {
  1015             m_characterData = new Vector();
  1024             m_characterData = new ArrayList<>();
  1016             currData = new StringBuffer();
  1025             currData = new StringBuilder();
  1017             m_characterData.addElement(currData);
  1026             m_characterData.add(currData);
  1018         } else {
  1027         } else {
  1019             currData = (StringBuffer) m_characterData
  1028             currData = m_characterData.get(m_characterData.size()-1);
  1020                                            .elementAt(m_characterData.size()-1);
       
  1021         }
  1029         }
  1022 
  1030 
  1023         // Character data could take up to three-times as much space when
  1031         // Character data could take up to three-times as much space when
  1024         // written to the class file as UTF-8.  The maximum size for a
  1032         // written to the class file as UTF-8.  The maximum size for a
  1025         // constant is 65535/3.  If we exceed that,
  1033         // constant is 65535/3.  If we exceed that,
  1026         // (We really should use some "bin packing".)
  1034         // (We really should use some "bin packing".)
  1027         if (newData.length() + currData.length() > 21845) {
  1035         if (newData.length() + currData.length() > 21845) {
  1028             currData = new StringBuffer();
  1036             currData = new StringBuilder();
  1029             m_characterData.addElement(currData);
  1037             m_characterData.add(currData);
  1030         }
  1038         }
  1031 
  1039 
  1032         int newDataOffset = currData.length();
  1040         int newDataOffset = currData.length();
  1033         currData.append(newData);
  1041         currData.append(newData);
  1034 
  1042