changeset 148:5381e4f5902f

8144593: Suppress not recognized property/feature warning messages from SAXParser Reviewed-by: joehw
author aefimov
date Fri, 05 Feb 2016 14:40:11 +0300
parents eda0f59587b6
children 9f9aa1efd581
files drop_included/jaxp_src/src/com/sun/org/apache/xalan/internal/utils/XMLSecurityManager.java drop_included/jaxp_src/src/com/sun/org/apache/xalan/internal/xsltc/compiler/Parser.java drop_included/jaxp_src/src/com/sun/org/apache/xalan/internal/xsltc/trax/Util.java drop_included/jaxp_src/src/com/sun/org/apache/xerces/internal/utils/XMLSecurityManager.java drop_included/jaxp_src/src/com/sun/org/apache/xml/internal/utils/XMLReaderManager.java
diffstat 5 files changed, 56 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/drop_included/jaxp_src/src/com/sun/org/apache/xalan/internal/utils/XMLSecurityManager.java	Thu Dec 21 18:18:09 2017 +0300
+++ b/drop_included/jaxp_src/src/com/sun/org/apache/xalan/internal/utils/XMLSecurityManager.java	Fri Feb 05 14:40:11 2016 +0300
@@ -40,6 +40,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;
 
 
 /**
@@ -431,6 +433,23 @@
 
     }
 
+    // Array list to store printed warnings for each SAX parser used
+    private static final CopyOnWriteArrayList<String> printedWarnings = new CopyOnWriteArrayList<String>();
+
+    /**
+     * 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/drop_included/jaxp_src/src/com/sun/org/apache/xalan/internal/xsltc/compiler/Parser.java	Thu Dec 21 18:18:09 2017 +0300
+++ b/drop_included/jaxp_src/src/com/sun/org/apache/xalan/internal/xsltc/compiler/Parser.java	Fri Feb 05 14:40:11 2016 +0300
@@ -469,18 +469,20 @@
             }
             final SAXParser parser = factory.newSAXParser();
             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/drop_included/jaxp_src/src/com/sun/org/apache/xalan/internal/xsltc/trax/Util.java	Thu Dec 21 18:18:09 2017 +0300
+++ b/drop_included/jaxp_src/src/com/sun/org/apache/xalan/internal/xsltc/trax/Util.java	Fri Feb 05 14:40:11 2016 +0300
@@ -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;
 
@@ -110,8 +108,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 {
@@ -145,21 +143,23 @@
                     reader.setFeature
                         ("http://xml.org/sax/features/namespace-prefixes",false);
 
+                    String lastProperty = "";
                     try {
                         XMLSecurityManager securityManager =
                                 (XMLSecurityManager)xsltc.getProperty(XalanConstants.SECURITY_MANAGER);
                         if (securityManager != null) {
                             for (XMLSecurityManager.Limit limit : XMLSecurityManager.Limit.values()) {
+                                lastProperty = limit.apiProperty();
                                 reader.setProperty(limit.apiProperty(),
                                         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/drop_included/jaxp_src/src/com/sun/org/apache/xerces/internal/utils/XMLSecurityManager.java	Thu Dec 21 18:18:09 2017 +0300
+++ b/drop_included/jaxp_src/src/com/sun/org/apache/xerces/internal/utils/XMLSecurityManager.java	Fri Feb 05 14:40:11 2016 +0300
@@ -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<String>();
+
+    /**
+     * 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/drop_included/jaxp_src/src/com/sun/org/apache/xml/internal/utils/XMLReaderManager.java	Thu Dec 21 18:18:09 2017 +0300
+++ b/drop_included/jaxp_src/src/com/sun/org/apache/xml/internal/utils/XMLReaderManager.java	Fri Feb 05 14:40:11 2016 +0300
@@ -121,8 +121,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 {
@@ -159,21 +159,23 @@
                 m_readers.set(reader);
                 m_inUse.put(reader, Boolean.TRUE);
             }
-        } 
+        }
 
+        String lastProperty = "";
         try {
             if (_xmlSecurityManager != null) {
                 for (XMLSecurityManager.Limit limit : XMLSecurityManager.Limit.values()) {
+                    lastProperty = limit.apiProperty();
                     reader.setProperty(limit.apiProperty(),
                             _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;