8144593: Suppress not recognized property/feature warning messages from SAXParser

Reviewed-by: joehw
This commit is contained in:
Aleksei Efimov 2016-02-04 18:31:11 +03:00
parent 7f0667866d
commit 6df6c45593
11 changed files with 429 additions and 23 deletions

View file

@ -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;
/**
@ -415,6 +417,23 @@ public final class XMLSecurityManager {
}
// 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
*

View file

@ -489,18 +489,20 @@ public class Parser implements Constants, ContentHandler {
}
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));

View file

@ -29,7 +29,6 @@ import java.io.Reader;
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.Source;
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 @@ public final class Util {
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 @@ public final class Util {
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 ) {

View file

@ -2249,8 +2249,8 @@ public class XSDHandler {
try {
parser.setProperty(XMLConstants.ACCESS_EXTERNAL_DTD, fAccessExternalDTD);
} catch (SAXNotRecognizedException exc) {
System.err.println("Warning: " + parser.getClass().getName() + ": " +
exc.getMessage());
XMLSecurityManager.printWarning(parser.getClass().getName(),
XMLConstants.ACCESS_EXTERNAL_DTD, exc);
}
}
// If XML names and Namespace URIs are already internalized we

View file

@ -697,8 +697,8 @@ final class ValidatorHandlerImpl extends ValidatorHandler implements
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 ) {

View file

@ -27,6 +27,8 @@ package com.sun.org.apache.xerces.internal.utils;
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.
@ -496,6 +498,23 @@ public final class XMLSecurityManager {
}
// 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
*

View file

@ -128,8 +128,8 @@ public class XMLReaderManager {
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 @@ public class XMLReaderManager {
//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;