< prev index next >

src/java.xml/share/classes/com/sun/org/apache/xalan/internal/xsltc/trax/TransformerFactoryImpl.java

Print this page
rev 1063 : 8172974: [JAXP] XALAN: Wrong result when transforming namespace unaware StAX Input

@@ -1,7 +1,7 @@
 /*
- * Copyright (c) 2007, 2016, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2007, 2017, Oracle and/or its affiliates. All rights reserved.
  */
 /*
  * Licensed to the Apache Software Foundation (ASF) under one or more
  * contributor license agreements.  See the NOTICE file distributed with
  * this work for additional information regarding copyright ownership.

@@ -45,11 +45,10 @@
 import java.net.URL;
 import java.util.ArrayList;
 import java.util.Enumeration;
 import java.util.Map;
 import java.util.Properties;
-import java.util.Vector;
 import java.util.zip.ZipEntry;
 import java.util.zip.ZipFile;
 import javax.xml.XMLConstants;
 import javax.xml.catalog.CatalogException;
 import javax.xml.catalog.CatalogFeatures;

@@ -148,31 +147,10 @@
      * The jar file name which the translet classes are packaged into
      */
     private String _jarFileName = null;
 
     /**
-     * This Map is used to store parameters for locating
-     * <?xml-stylesheet ...?> processing instructions in XML docs.
-     */
-    private Map<Source, PIParamWrapper> _piParams = null;
-
-    /**
-     * The above Map stores objects of this class.
-     */
-    private static class PIParamWrapper {
-        public String _media = null;
-        public String _title = null;
-        public String _charset = null;
-
-        public PIParamWrapper(String media, String title, String charset) {
-            _media = media;
-            _title = title;
-            _charset = charset;
-        }
-    }
-
-    /**
      * Set to <code>true</code> when debugging is enabled.
      */
     private boolean _debug = false;
 
     /**

@@ -207,28 +185,30 @@
 
     /**
      * <p>State of secure processing feature.</p>
      */
     private boolean _isNotSecureProcessing = true;
+
     /**
      * <p>State of secure mode.</p>
      */
     private boolean _isSecureMode = false;
 
     /**
      * Indicates whether implementation parts should use
      *   service loader (or similar).
-     * Note the default value (false) is the safe option..
+     * Note the default value (false) is the safe option.
      */
     private boolean _useServicesMechanism;
 
     /**
      * protocols allowed for external references set by the stylesheet
      * processing instruction, Import and Include element.
      */
     private String _accessExternalStylesheet = XalanConstants.EXTERNAL_ACCESS_DEFAULT;
-     /**
+
+    /**
      * protocols allowed for external DTD references in source file and/or stylesheet.
      */
     private String _accessExternalDTD = XalanConstants.EXTERNAL_ACCESS_DEFAULT;
 
     private XMLSecurityPropertyManager _xmlSecurityPropertyMgr;

@@ -564,13 +544,12 @@
 
         // feature name cannot be null
         if (name == null) {
             ErrorMsg err = new ErrorMsg(ErrorMsg.JAXP_SET_FEATURE_NULL_NAME);
             throw new NullPointerException(err.toString());
-        }
         // secure processing?
-        else if (name.equals(XMLConstants.FEATURE_SECURE_PROCESSING)) {
+        } else if (name.equals(XMLConstants.FEATURE_SECURE_PROCESSING)) {
             if ((_isSecureMode) && (!value)) {
                 ErrorMsg err = new ErrorMsg(ErrorMsg.JAXP_SECUREPROCESSING_FEATURE);
                 throw new TransformerConfigurationException(err.toString());
             }
             _isNotSecureProcessing = !value;

@@ -590,19 +569,17 @@
 
             if (value && _xmlFeatures != null) {
                 _xmlFeatures.setFeature(JdkXmlFeatures.XmlFeature.ENABLE_EXTENSION_FUNCTION,
                         JdkXmlFeatures.State.FSP, false);
             }
-        }
-        else if (name.equals(XalanConstants.ORACLE_FEATURE_SERVICE_MECHANISM)) {
+        } else if (name.equals(XalanConstants.ORACLE_FEATURE_SERVICE_MECHANISM)) {
             //in secure mode, let _useServicesMechanism be determined by the constructor
             if (!_isSecureMode)
                 _useServicesMechanism = value;
-        }
-        else {
+        } else {
             if (_xmlFeatures != null &&
-                    _xmlFeatures.setFeature(name, JdkXmlFeatures.State.APIPROPERTY, value)) {
+                _xmlFeatures.setFeature(name, JdkXmlFeatures.State.APIPROPERTY, value)) {
                 return;
             }
 
             // unknown feature
             ErrorMsg err = new ErrorMsg(ErrorMsg.JAXP_UNSUPPORTED_FEATURE, name);

@@ -619,11 +596,11 @@
      * @param name The feature name
      * @return 'true' if feature is supported, 'false' if not
      */
     @Override
     public boolean getFeature(String name) {
-        // All supported features should be listed here
+        // all supported features should be listed here
         String[] features = {
             DOMSource.FEATURE,
             DOMResult.FEATURE,
             SAXSource.FEATURE,
             SAXResult.FEATURE,

@@ -641,37 +618,39 @@
             ErrorMsg err = new ErrorMsg(ErrorMsg.JAXP_GET_FEATURE_NULL_NAME);
             throw new NullPointerException(err.toString());
         }
 
         // Inefficient, but array is small
-        for (int i =0; i < features.length; i++) {
+        for (int i = 0; i < features.length; i++) {
             if (name.equals(features[i])) {
                 return true;
             }
         }
 
+        // secure processing?
         if (name.equals(XMLConstants.FEATURE_SECURE_PROCESSING)) {
             return !_isNotSecureProcessing;
         }
 
-        /** Check to see if the property is managed by the JdkXmlFeatues **/
+        // check to see if the property is managed by the JdkXmlFeatues
         int index = _xmlFeatures.getIndex(name);
         if (index > -1) {
             return _xmlFeatures.getFeature(index);
         }
 
-        // Feature not supported
+        // feature not supported
         return false;
     }
+
     /**
      * Return the state of the services mechanism feature.
      */
     public boolean useServicesMechnism() {
         return _useServicesMechanism;
     }
 
-     /**
+    /**
      * @return the feature manager
      */
     public JdkXmlFeatures getJdkXmlFeatures() {
         return _xmlFeatures;
     }

@@ -832,11 +811,11 @@
         final Templates templates = newTemplates(source);
         final Transformer transformer = templates.newTransformer();
         if (_uriResolver != null) {
             transformer.setURIResolver(_uriResolver);
         }
-        return(transformer);
+        return transformer;
     }
 
     /**
      * Pass warning messages from the compiler to the error listener
      */

@@ -985,21 +964,10 @@
         if (_uriResolver != null || ( _catalogFiles != null
                 && _xmlFeatures.getFeature(JdkXmlFeatures.XmlFeature.USE_CATALOG))) {
             xsltc.setSourceLoader(this);
         }
 
-        // Pass parameters to the Parser to make sure it locates the correct
-        // <?xml-stylesheet ...?> PI in an XML input document
-        if ((_piParams != null) && (_piParams.get(source) != null)) {
-            // Get the parameters for this Source object
-            PIParamWrapper p = _piParams.get(source);
-            // Pass them on to the compiler (which will pass then to the parser)
-            if (p != null) {
-                xsltc.setPIParameters(p._media, p._title, p._charset);
-            }
-        }
-
         // Set the attributes for translet generation
         int outputType = XSLTC.BYTEARRAY_OUTPUT;
         if (_generateTranslet || _autoTranslet) {
             // Set the translet name
             xsltc.setClassName(getTransletBaseName(source));

@@ -1440,11 +1408,11 @@
             if (transletTimestamp < xslTimestamp)
                 return null;
         }
 
         // Load the translet into a bytecode array.
-        Vector bytecodes = new Vector();
+        ArrayList<byte[]> bytecodes = new ArrayList<>();
         int fileLength = (int)transletFile.length();
         if (fileLength > 0) {
             FileInputStream input;
             try {
                 input = new FileInputStream(transletFile);

@@ -1460,14 +1428,14 @@
             }
             catch (IOException e) {
                 return null;
             }
 
-            bytecodes.addElement(bytes);
-        }
-        else
+            bytecodes.add(bytes);
+        } else {
             return null;
+        }
 
         // Find the parent directory of the translet.
         String transletParentDir = transletFile.getParent();
         if (transletParentDir == null)
             transletParentDir = SecuritySupport.getSystemProperty("user.dir");

@@ -1506,20 +1474,20 @@
                 }
                 catch (IOException e) {
                     continue;
                 }
 
-                bytecodes.addElement(bytes);
+                bytecodes.add(bytes);
             }
         }
 
         // Convert the Vector of byte[] to byte[][].
         final int count = bytecodes.size();
         if ( count > 0) {
             final byte[][] result = new byte[count][1];
             for (int i = 0; i < count; i++) {
-                result[i] = (byte[])bytecodes.elementAt(i);
+                result[i] = bytecodes.get(i);
             }
 
             return result;
         }
         else

@@ -1564,62 +1532,51 @@
             if (transletTimestamp < xslTimestamp)
                 return null;
         }
 
         // Create a ZipFile object for the jar file
-        ZipFile jarFile;
-        try {
-            jarFile = new ZipFile(file);
-        }
-        catch (IOException e) {
-            return null;
-        }
-
-        String transletPath = fullClassName.replace('.', '/');
-        String transletAuxPrefix = transletPath + "$";
-        String transletFullName = transletPath + ".class";
-
-        Vector bytecodes = new Vector();
-
-        // Iterate through all entries in the jar file to find the
-        // translet and auxiliary classes.
-        Enumeration entries = jarFile.entries();
-        while (entries.hasMoreElements())
-        {
-            ZipEntry entry = (ZipEntry)entries.nextElement();
-            String entryName = entry.getName();
-            if (entry.getSize() > 0 &&
-                  (entryName.equals(transletFullName) ||
-                  (entryName.endsWith(".class") &&
+        try (ZipFile jarFile = new ZipFile(file)) {
+            String transletPath = fullClassName.replace('.', '/');
+            String transletAuxPrefix = transletPath + "$";
+            String transletFullName = transletPath + ".class";
+
+            ArrayList<byte[]> bytecodes = new ArrayList<>();
+
+            // Iterate through all entries in the jar file to find the
+            // translet and auxiliary classes.
+            Enumeration<? extends ZipEntry> entries = jarFile.entries();
+            while (entries.hasMoreElements()) {
+                ZipEntry entry = entries.nextElement();
+                String entryName = entry.getName();
+                if (entry.getSize() > 0 &&
+                    (entryName.equals(transletFullName) ||
+                     (entryName.endsWith(".class") &&
                       entryName.startsWith(transletAuxPrefix))))
-            {
-                try {
+                {
                     InputStream input = jarFile.getInputStream(entry);
                     int size = (int)entry.getSize();
                     byte[] bytes = new byte[size];
                     readFromInputStream(bytes, input, size);
                     input.close();
-                    bytecodes.addElement(bytes);
-                }
-                catch (IOException e) {
-                    return null;
+                    bytecodes.add(bytes);
                 }
             }
-        }
 
-        // Convert the Vector of byte[] to byte[][].
-        final int count = bytecodes.size();
-        if (count > 0) {
-            final byte[][] result = new byte[count][1];
-            for (int i = 0; i < count; i++) {
-                result[i] = (byte[])bytecodes.elementAt(i);
+            // Convert the Vector of byte[] to byte[][].
+            final int count = bytecodes.size();
+            if (count > 0) {
+                final byte[][] result = new byte[count][1];
+                for (int i = 0; i < count; i++) {
+                    result[i] = bytecodes.get(i);
+                }
+                return result;
+            } else {
+                return null;
             }
-
-            return result;
-        }
-        else
+        } catch (IOException e) {
             return null;
+        }
     }
 
     /**
      * Read a given number of bytes from the InputStream into a byte array.
      *

@@ -1628,17 +1585,17 @@
      * @param size The number of bytes to read.
      */
     private void readFromInputStream(byte[] bytes, InputStream input, int size)
         throws IOException
     {
-      int n = 0;
-      int offset = 0;
-      int length = size;
-      while (length > 0 && (n = input.read(bytes, offset, length)) > 0) {
-          offset = offset + n;
-          length = length - n;
-      }
+        int n = 0;
+        int offset = 0;
+        int length = size;
+        while (length > 0 && (n = input.read(bytes, offset, length)) > 0) {
+            offset = offset + n;
+            length = length - n;
+        }
     }
 
     /**
      * Return the base class name of the translet.
      * The translet name is resolved using the following rules:

@@ -1648,12 +1605,11 @@
      * 3. return "GregorSamsa" if the result from step 2 is null.
      *
      * @param source The input Source
      * @return The name of the translet class
      */
-    private String getTransletBaseName(Source source)
-    {
+    private String getTransletBaseName(Source source) {
         String transletBaseName = null;
         if (!_transletName.equals(DEFAULT_TRANSLET_NAME))
             return _transletName;
         else {
             String systemId = source.getSystemId();

@@ -1674,12 +1630,11 @@
      *
      * @param source The Source
      * @return The file name in the local filesystem, or null if the
      * systemId does not represent a local file.
      */
-    private String getStylesheetFileName(Source source)
-    {
+    private String getStylesheetFileName(Source source) {
         String systemId = source.getSystemId();
         if (systemId != null) {
             File file = new File(systemId);
             if (file.exists())
                 return systemId;

@@ -1695,13 +1650,13 @@
                 if ("file".equals(url.getProtocol()))
                     return url.getFile();
                 else
                     return null;
             }
-        }
-        else
+        } else {
             return null;
+        }
     }
 
     /**
      * Returns a new instance of the XSLTC DTM Manager service.
      */
< prev index next >