changeset 1263:abfbb18f6f3a

8144593: Suppress not recognized property/feature warning messages from SAXParser Reviewed-by: joehw
author aefimov
date Mon, 05 Feb 2018 11:38:57 +0000
parents d2c96b59a5c3
children 9d02485a8af6
files src/com/sun/org/apache/xalan/internal/utils/XMLSecurityManager.java src/com/sun/org/apache/xalan/internal/xsltc/compiler/Parser.java src/com/sun/org/apache/xalan/internal/xsltc/trax/Util.java src/com/sun/org/apache/xerces/internal/impl/xs/traversers/XSDHandler.java src/com/sun/org/apache/xerces/internal/jaxp/validation/ValidatorHandlerImpl.java src/com/sun/org/apache/xerces/internal/utils/XMLSecurityManager.java src/com/sun/org/apache/xml/internal/utils/XMLReaderManager.java
diffstat 7 files changed, 69 insertions(+), 27 deletions(-) [+]
line wrap: on
line diff
--- a/src/com/sun/org/apache/xalan/internal/utils/XMLSecurityManager.java	Thu Nov 30 02:51:51 2017 +0000
+++ b/src/com/sun/org/apache/xalan/internal/utils/XMLSecurityManager.java	Mon Feb 05 11:38:57 2018 +0000
@@ -26,6 +26,8 @@
 package com.sun.org.apache.xalan.internal.utils;
 
 import com.sun.org.apache.xalan.internal.XalanConstants;
+import java.util.concurrent.CopyOnWriteArrayList;
+import org.xml.sax.SAXException;
 
 
 /**
@@ -417,6 +419,23 @@
 
     }
 
+    // Array list to store printed warnings for each SAX parser used
+    private static final CopyOnWriteArrayList<String> printedWarnings = new CopyOnWriteArrayList<>();
+
+    /**
+     * Prints out warnings if a parser does not support the specified feature/property.
+     *
+     * @param parserClassName the name of the parser class
+     * @param propertyName the property name
+     * @param exception the exception thrown by the parser
+     */
+    public static void printWarning(String parserClassName, String propertyName, SAXException exception) {
+        String key = parserClassName+":"+propertyName;
+        if (printedWarnings.addIfAbsent(key)) {
+            System.err.println( "Warning: "+parserClassName+": "+exception.getMessage());
+        }
+    }
+
     /**
      * Read from system properties, or those in jaxp.properties
      *
--- a/src/com/sun/org/apache/xalan/internal/xsltc/compiler/Parser.java	Thu Nov 30 02:51:51 2017 +0000
+++ b/src/com/sun/org/apache/xalan/internal/xsltc/compiler/Parser.java	Mon Feb 05 11:38:57 2018 +0000
@@ -489,18 +489,20 @@
             }
 
             final XMLReader reader = parser.getXMLReader();
+            String lastProperty = "";
             try {
                 XMLSecurityManager securityManager =
                         (XMLSecurityManager)_xsltc.getProperty(XalanConstants.SECURITY_MANAGER);
                 for (XMLSecurityManager.Limit limit : XMLSecurityManager.Limit.values()) {
-                    reader.setProperty(limit.apiProperty(), securityManager.getLimitValueAsString(limit));
+                    lastProperty = limit.apiProperty();
+                    reader.setProperty(lastProperty, securityManager.getLimitValueAsString(limit));
                 }
                 if (securityManager.printEntityCountInfo()) {
+                    lastProperty = XalanConstants.JDK_ENTITY_COUNT_INFO;
                     parser.setProperty(XalanConstants.JDK_ENTITY_COUNT_INFO, XalanConstants.JDK_YES);
                 }
             } catch (SAXException se) {
-                System.err.println("Warning:  " + reader.getClass().getName() + ": "
-                            + se.getMessage());
+                XMLSecurityManager.printWarning(reader.getClass().getName(), lastProperty, se);
             }
 
             return(parse(reader, input));
--- a/src/com/sun/org/apache/xalan/internal/xsltc/trax/Util.java	Thu Nov 30 02:51:51 2017 +0000
+++ b/src/com/sun/org/apache/xalan/internal/xsltc/trax/Util.java	Mon Feb 05 11:38:57 2018 +0000
@@ -29,7 +29,6 @@
 
 import javax.xml.XMLConstants;
 import javax.xml.parsers.ParserConfigurationException;
-import javax.xml.parsers.SAXParser;
 import javax.xml.parsers.SAXParserFactory;
 
 import javax.xml.stream.XMLEventReader;
@@ -39,7 +38,6 @@
 import javax.xml.transform.TransformerConfigurationException;
 import javax.xml.transform.dom.DOMSource;
 import javax.xml.transform.sax.SAXSource;
-import javax.xml.transform.stax.StAXResult;
 import javax.xml.transform.stax.StAXSource;
 import javax.xml.transform.stream.StreamSource;
 
@@ -111,8 +109,8 @@
                                 reader.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING,
                                             xsltc.isSecureProcessing());
                            } catch (SAXNotRecognizedException e) {
-                                System.err.println("Warning:  " + reader.getClass().getName() + ": "
-                                        + e.getMessage());
+                                XMLSecurityManager.printWarning(reader.getClass().getName(),
+                                        XMLConstants.FEATURE_SECURE_PROCESSING, e);
                            }
                        } catch (Exception e ) {
                            try {
@@ -149,25 +147,27 @@
                         reader.setProperty(XMLConstants.ACCESS_EXTERNAL_DTD,
                                    xsltc.getProperty(XMLConstants.ACCESS_EXTERNAL_DTD));
                     } catch (SAXNotRecognizedException e) {
-                        System.err.println("Warning:  " + reader.getClass().getName() + ": "
-                                + e.getMessage());
+                        XMLSecurityManager.printWarning(reader.getClass().getName(),
+                                XMLConstants.ACCESS_EXTERNAL_DTD, e);
                     }
 
+                    String lastProperty = "";
                     try {
                         XMLSecurityManager securityManager =
                                 (XMLSecurityManager)xsltc.getProperty(XalanConstants.SECURITY_MANAGER);
                         if (securityManager != null) {
                             for (XMLSecurityManager.Limit limit : XMLSecurityManager.Limit.values()) {
-                                reader.setProperty(limit.apiProperty(),
+                                lastProperty = limit.apiProperty();
+                                reader.setProperty(lastProperty,
                                         securityManager.getLimitValueAsString(limit));
                             }
                             if (securityManager.printEntityCountInfo()) {
+                                lastProperty = XalanConstants.JDK_ENTITY_COUNT_INFO;
                                 reader.setProperty(XalanConstants.JDK_ENTITY_COUNT_INFO, XalanConstants.JDK_YES);
                             }
                         }
                     } catch (SAXException se) {
-                        System.err.println("Warning:  " + reader.getClass().getName() + ": "
-                                    + se.getMessage());
+                        XMLSecurityManager.printWarning(reader.getClass().getName(), lastProperty, se);
                     }
                     xsltc.setXMLReader(reader);
                 }catch (SAXNotRecognizedException snre ) {
--- a/src/com/sun/org/apache/xerces/internal/impl/xs/traversers/XSDHandler.java	Thu Nov 30 02:51:51 2017 +0000
+++ b/src/com/sun/org/apache/xerces/internal/impl/xs/traversers/XSDHandler.java	Mon Feb 05 11:38:57 2018 +0000
@@ -2247,12 +2247,12 @@
                     }
                     catch (SAXException se) {}
 
-                     try {
-                             parser.setProperty(XMLConstants.ACCESS_EXTERNAL_DTD, fAccessExternalDTD);
-                     } catch (SAXNotRecognizedException exc) {
-                         System.err.println("Warning: " + parser.getClass().getName() + ": " +
-                                 exc.getMessage());
-                     }
+                    try {
+                        parser.setProperty(XMLConstants.ACCESS_EXTERNAL_DTD, fAccessExternalDTD);
+                    } catch (SAXNotRecognizedException exc) {
+                        XMLSecurityManager.printWarning(parser.getClass().getName(),
+                                                        XMLConstants.ACCESS_EXTERNAL_DTD, exc);
+                    }
                 }
                 // If XML names and Namespace URIs are already internalized we
                 // can avoid running them through the SymbolTable.
--- a/src/com/sun/org/apache/xerces/internal/jaxp/validation/ValidatorHandlerImpl.java	Thu Nov 30 02:51:51 2017 +0000
+++ b/src/com/sun/org/apache/xerces/internal/jaxp/validation/ValidatorHandlerImpl.java	Mon Feb 05 11:38:57 2018 +0000
@@ -698,8 +698,8 @@
                                reader.setProperty(XMLConstants.ACCESS_EXTERNAL_DTD,
                                        spm.getValue(XMLSecurityPropertyManager.Property.ACCESS_EXTERNAL_DTD));
                            } catch (SAXException exc) {
-                               System.err.println("Warning: " + reader.getClass().getName() + ": " +
-                                      exc.getMessage());
+                               XMLSecurityManager.printWarning(reader.getClass().getName(),
+                                       XMLConstants.ACCESS_EXTERNAL_DTD, exc);
                            }
                         }
                     } catch( Exception e ) {
--- a/src/com/sun/org/apache/xerces/internal/utils/XMLSecurityManager.java	Thu Nov 30 02:51:51 2017 +0000
+++ b/src/com/sun/org/apache/xerces/internal/utils/XMLSecurityManager.java	Mon Feb 05 11:38:57 2018 +0000
@@ -27,6 +27,8 @@
 
 import com.sun.org.apache.xerces.internal.impl.Constants;
 import com.sun.org.apache.xerces.internal.util.SecurityManager;
+import java.util.concurrent.CopyOnWriteArrayList;
+import org.xml.sax.SAXException;
 
 /**
  * This class manages standard and implementation-specific limitations.
@@ -499,6 +501,23 @@
 
     }
 
+    // Array list to store printed warnings for each SAX parser used
+    private static final CopyOnWriteArrayList<String> printedWarnings = new CopyOnWriteArrayList<>();
+
+    /**
+     * Prints out warnings if a parser does not support the specified feature/property.
+     *
+     * @param parserClassName the name of the parser class
+     * @param propertyName the property name
+     * @param exception the exception thrown by the parser
+     */
+    public static void printWarning(String parserClassName, String propertyName, SAXException exception) {
+        String key = parserClassName+":"+propertyName;
+        if (printedWarnings.addIfAbsent(key)) {
+            System.err.println( "Warning: "+parserClassName+": "+exception.getMessage());
+        }
+    }
+
     /**
      * Read from system properties, or those in jaxp.properties
      *
--- a/src/com/sun/org/apache/xml/internal/utils/XMLReaderManager.java	Thu Nov 30 02:51:51 2017 +0000
+++ b/src/com/sun/org/apache/xml/internal/utils/XMLReaderManager.java	Mon Feb 05 11:38:57 2018 +0000
@@ -128,8 +128,8 @@
                     try {
                         reader.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, _secureProcessing);
                     } catch (SAXNotRecognizedException e) {
-                        System.err.println("Warning:  " + reader.getClass().getName() + ": "
-                                + e.getMessage());
+                        XMLSecurityManager.printWarning(reader.getClass().getName(),
+                                XMLConstants.FEATURE_SECURE_PROCESSING, e);
                     }
                 } catch (Exception e) {
                    try {
@@ -172,23 +172,25 @@
             //reader is cached, but this property might have been reset
             reader.setProperty(XMLConstants.ACCESS_EXTERNAL_DTD, _accessExternalDTD);
         } catch (SAXException se) {
-            System.err.println("Warning:  " + reader.getClass().getName() + ": "
-                        + se.getMessage());
+            XMLSecurityManager.printWarning(reader.getClass().getName(),
+                    XMLConstants.ACCESS_EXTERNAL_DTD, se);
         }
 
+        String lastProperty = "";
         try {
             if (_xmlSecurityManager != null) {
                 for (XMLSecurityManager.Limit limit : XMLSecurityManager.Limit.values()) {
-                    reader.setProperty(limit.apiProperty(),
+                    lastProperty = limit.apiProperty();
+                    reader.setProperty(lastProperty,
                             _xmlSecurityManager.getLimitValueAsString(limit));
                 }
                 if (_xmlSecurityManager.printEntityCountInfo()) {
+                    lastProperty = XalanConstants.JDK_ENTITY_COUNT_INFO;
                     reader.setProperty(XalanConstants.JDK_ENTITY_COUNT_INFO, XalanConstants.JDK_YES);
                 }
             }
         } catch (SAXException se) {
-            System.err.println("Warning:  " + reader.getClass().getName() + ": "
-                        + se.getMessage());
+            XMLSecurityManager.printWarning(reader.getClass().getName(), lastProperty, se);
         }
 
         return reader;