Path: blob/aarch64-shenandoah-jdk8u272-b10/jaxp/src/jdk/xml/internal/JdkXmlUtils.java
38813 views
/*1* Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package jdk.xml.internal;2627import com.sun.org.apache.xalan.internal.utils.XMLSecurityManager;28import com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl;29import com.sun.org.apache.xerces.internal.impl.Constants;30import com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl;31import com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl;32import javax.xml.XMLConstants;33import javax.xml.parsers.DocumentBuilderFactory;34import javax.xml.parsers.ParserConfigurationException;35import javax.xml.parsers.SAXParserFactory;36import javax.xml.transform.TransformerConfigurationException;37import javax.xml.transform.sax.SAXTransformerFactory;38import org.w3c.dom.Document;39import org.xml.sax.SAXException;40import org.xml.sax.SAXNotRecognizedException;41import org.xml.sax.SAXNotSupportedException;42import org.xml.sax.XMLReader;4344/**45* Constants for use across JAXP processors.46*/47public class JdkXmlUtils {48private static final String DOM_FACTORY_ID = "javax.xml.parsers.DocumentBuilderFactory";49private static final String SAX_FACTORY_ID = "javax.xml.parsers.SAXParserFactory";50private static final String SAX_DRIVER = "org.xml.sax.driver";5152/**53* Xerces features54*/55public static final String NAMESPACES_FEATURE =56Constants.SAX_FEATURE_PREFIX + Constants.NAMESPACES_FEATURE;57public static final String NAMESPACE_PREFIXES_FEATURE =58Constants.SAX_FEATURE_PREFIX + Constants.NAMESPACE_PREFIXES_FEATURE;5960/**61* jdk.xml.overrideDefaultParser: enables the use of a 3rd party's parser62* implementation to override the system-default parser.63*/64public static final String OVERRIDE_PARSER = "jdk.xml.overrideDefaultParser";65public static final boolean OVERRIDE_PARSER_DEFAULT = SecuritySupport.getJAXPSystemProperty(66Boolean.class, OVERRIDE_PARSER, "false");6768/**69* Values for a feature70*/71public static final String FEATURE_TRUE = "true";72public static final String FEATURE_FALSE = "false";7374/**75* The system-default factory76*/77private static final SAXParserFactory defaultSAXFactory = getSAXFactory(false);7879/**80* Returns the value.81*82* @param value the specified value83* @param defValue the default value84* @return the value, or the default value if the value is null85*/86public static int getValue(Object value, int defValue) {87if (value == null) {88return defValue;89}9091if (value instanceof Number) {92return ((Number) value).intValue();93} else if (value instanceof String) {94return Integer.parseInt(String.valueOf(value));95} else {96throw new IllegalArgumentException("Unexpected class: "97+ value.getClass());98}99}100101/**102* Sets the XMLReader instance with the specified property if the the103* property is supported, ignores error if not, issues a warning if so104* requested.105*106* @param reader an XMLReader instance107* @param property the name of the property108* @param value the value of the property109* @param warn a flag indicating whether a warning should be issued110*/111public static void setXMLReaderPropertyIfSupport(XMLReader reader, String property,112Object value, boolean warn) {113try {114reader.setProperty(property, value);115} catch (SAXNotRecognizedException | SAXNotSupportedException e) {116if (warn) {117XMLSecurityManager.printWarning(reader.getClass().getName(),118property, e);119}120}121}122123/**124* Returns an XMLReader instance. If overrideDefaultParser is requested, use125* SAXParserFactory or XMLReaderFactory, otherwise use the system-default126* SAXParserFactory to locate an XMLReader.127*128* @param overrideDefaultParser a flag indicating whether a 3rd party's129* parser implementation may be used to override the system-default one130* @param secureProcessing a flag indicating whether secure processing is131* requested132* @return an XMLReader instance133*/134public static XMLReader getXMLReader(boolean overrideDefaultParser,135boolean secureProcessing) {136SAXParserFactory saxFactory;137XMLReader reader = null;138String spSAXDriver = SecuritySupport.getSystemProperty(SAX_DRIVER);139if (spSAXDriver != null) {140reader = getXMLReaderWXMLReaderFactory();141} else if (overrideDefaultParser) {142reader = getXMLReaderWSAXFactory(overrideDefaultParser);143}144145if (reader != null) {146if (secureProcessing) {147try {148reader.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, secureProcessing);149} catch (SAXException e) {150XMLSecurityManager.printWarning(reader.getClass().getName(),151XMLConstants.FEATURE_SECURE_PROCESSING, e);152}153}154try {155reader.setFeature(NAMESPACES_FEATURE, true);156reader.setFeature(NAMESPACE_PREFIXES_FEATURE, false);157} catch (SAXException se) {158// older version of a parser159}160return reader;161}162163// use the system-default164saxFactory = defaultSAXFactory;165166try {167reader = saxFactory.newSAXParser().getXMLReader();168} catch (ParserConfigurationException | SAXException ex) {169// shall not happen with the system-default reader170}171return reader;172}173174/**175* Creates a system-default DOM Document.176*177* @return a DOM Document instance178*/179public static Document getDOMDocument() {180try {181DocumentBuilderFactory dbf = JdkXmlUtils.getDOMFactory(false);182return dbf.newDocumentBuilder().newDocument();183} catch (ParserConfigurationException pce) {184// can never happen with the system-default configuration185}186return null;187}188189/**190* Returns a DocumentBuilderFactory instance.191*192* @param overrideDefaultParser a flag indicating whether the system-default193* implementation may be overridden. If the system property of the194* DOM factory ID is set, override is always allowed.195*196* @return a DocumentBuilderFactory instance.197*/198public static DocumentBuilderFactory getDOMFactory(boolean overrideDefaultParser) {199boolean override = overrideDefaultParser;200String spDOMFactory = SecuritySupport.getJAXPSystemProperty(DOM_FACTORY_ID);201202if (spDOMFactory != null && System.getSecurityManager() == null) {203override = true;204}205DocumentBuilderFactory dbf206= !override207? new DocumentBuilderFactoryImpl()208: DocumentBuilderFactory.newInstance();209dbf.setNamespaceAware(true);210// false is the default setting. This step here is for compatibility211dbf.setValidating(false);212return dbf;213}214215/**216* Returns a SAXParserFactory instance.217*218* @param overrideDefaultParser a flag indicating whether the system-default219* implementation may be overridden. If the system property of the220* DOM factory ID is set, override is always allowed.221*222* @return a SAXParserFactory instance.223*/224public static SAXParserFactory getSAXFactory(boolean overrideDefaultParser) {225boolean override = overrideDefaultParser;226String spSAXFactory = SecuritySupport.getJAXPSystemProperty(SAX_FACTORY_ID);227if (spSAXFactory != null && System.getSecurityManager() == null) {228override = true;229}230231SAXParserFactory factory232= !override233? new SAXParserFactoryImpl()234: SAXParserFactory.newInstance();235factory.setNamespaceAware(true);236return factory;237}238239public static SAXTransformerFactory getSAXTransformFactory(boolean overrideDefaultParser) {240SAXTransformerFactory tf = overrideDefaultParser241? (SAXTransformerFactory) SAXTransformerFactory.newInstance()242: (SAXTransformerFactory) new TransformerFactoryImpl();243try {244tf.setFeature(OVERRIDE_PARSER, overrideDefaultParser);245} catch (TransformerConfigurationException ex) {246// ignore since it'd never happen with the JDK impl.247}248return tf;249}250251private static XMLReader getXMLReaderWSAXFactory(boolean overrideDefaultParser) {252SAXParserFactory saxFactory = getSAXFactory(overrideDefaultParser);253try {254return saxFactory.newSAXParser().getXMLReader();255} catch (ParserConfigurationException | SAXException ex) {256return getXMLReaderWXMLReaderFactory();257}258}259260@SuppressWarnings("deprecation")261private static XMLReader getXMLReaderWXMLReaderFactory() {262try {263return org.xml.sax.helpers.XMLReaderFactory.createXMLReader();264} catch (SAXException ex1) {265}266return null;267}268}269270271