src/java.xml/share/classes/jdk/xml/internal/JdkXmlUtils.java
author thartmann
Fri, 24 Aug 2018 08:17:23 +0200
changeset 51514 1e332d63bd96
parent 48577 8dff65f1d611
permissions -rw-r--r--
8209833: C2 compilation fails with "assert(ex_map->jvms()->same_calls_as(_exceptions->jvms())) failed: all collected exceptions must come from the same place" Summary: Deoptimize if exception is thrown in _clone intrinsic. Reviewed-by: kvn
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
39907
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
     1
/*
48577
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
     2
 * Copyright (c) 2016, 2017, Oracle and/or its affiliates. All rights reserved.
39907
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
     4
 *
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
    10
 *
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
    15
 * accompanied this code).
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
    16
 *
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
    20
 *
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
    23
 * questions.
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
    24
 */
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
    25
package jdk.xml.internal;
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
    26
42247
52fafb950d5a 8158619: Very large CDATA section in XML document causes OOME
joehw
parents: 39907
diff changeset
    27
import com.sun.org.apache.xalan.internal.utils.XMLSecurityManager;
48577
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
    28
import com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl;
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
    29
import com.sun.org.apache.xerces.internal.impl.Constants;
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
    30
import com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl;
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
    31
import com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl;
39907
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
    32
import com.sun.org.apache.xerces.internal.util.ParserConfigurationSettings;
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
    33
import com.sun.org.apache.xerces.internal.xni.parser.XMLComponentManager;
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
    34
import com.sun.org.apache.xerces.internal.xni.parser.XMLConfigurationException;
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
    35
import javax.xml.XMLConstants;
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
    36
import javax.xml.catalog.CatalogFeatures;
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
    37
import javax.xml.catalog.CatalogFeatures.Feature;
48577
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
    38
import javax.xml.parsers.DocumentBuilderFactory;
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
    39
import javax.xml.parsers.ParserConfigurationException;
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
    40
import javax.xml.parsers.SAXParserFactory;
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
    41
import javax.xml.transform.TransformerConfigurationException;
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
    42
import javax.xml.transform.sax.SAXTransformerFactory;
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
    43
import org.w3c.dom.Document;
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
    44
import org.xml.sax.SAXException;
39907
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
    45
import org.xml.sax.SAXNotRecognizedException;
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
    46
import org.xml.sax.SAXNotSupportedException;
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
    47
import org.xml.sax.XMLReader;
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
    48
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
    49
/**
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
    50
 * Constants for use across JAXP processors.
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
    51
 */
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
    52
public class JdkXmlUtils {
48577
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
    53
    private static final String DOM_FACTORY_ID = "javax.xml.parsers.DocumentBuilderFactory";
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
    54
    private static final String SAX_FACTORY_ID = "javax.xml.parsers.SAXParserFactory";
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
    55
    private static final String SAX_DRIVER = "org.xml.sax.driver";
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
    56
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
    57
    /**
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
    58
     * Xerces features
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
    59
     */
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
    60
    public static final String NAMESPACES_FEATURE =
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
    61
        Constants.SAX_FEATURE_PREFIX + Constants.NAMESPACES_FEATURE;
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
    62
    public static final String NAMESPACE_PREFIXES_FEATURE =
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
    63
        Constants.SAX_FEATURE_PREFIX + Constants.NAMESPACE_PREFIXES_FEATURE;
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
    64
42247
52fafb950d5a 8158619: Very large CDATA section in XML document causes OOME
joehw
parents: 39907
diff changeset
    65
39907
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
    66
    /**
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
    67
     * Catalog features
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
    68
     */
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
    69
    public final static String USE_CATALOG = XMLConstants.USE_CATALOG;
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
    70
    public final static String SP_USE_CATALOG = "javax.xml.useCatalog";
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
    71
    public final static String CATALOG_FILES = CatalogFeatures.Feature.FILES.getPropertyName();
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
    72
    public final static String CATALOG_DEFER = CatalogFeatures.Feature.DEFER.getPropertyName();
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
    73
    public final static String CATALOG_PREFER = CatalogFeatures.Feature.PREFER.getPropertyName();
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
    74
    public final static String CATALOG_RESOLVE = CatalogFeatures.Feature.RESOLVE.getPropertyName();
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
    75
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
    76
    /**
48577
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
    77
     * Reset SymbolTable feature System property name is identical to feature
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
    78
     * name
44802
89a545213cad 8176168: Performance drop due to SAXParser SymbolTable reset
aefimov
parents: 42247
diff changeset
    79
     */
89a545213cad 8176168: Performance drop due to SAXParser SymbolTable reset
aefimov
parents: 42247
diff changeset
    80
    public final static String RESET_SYMBOL_TABLE = "jdk.xml.resetSymbolTable";
89a545213cad 8176168: Performance drop due to SAXParser SymbolTable reset
aefimov
parents: 42247
diff changeset
    81
89a545213cad 8176168: Performance drop due to SAXParser SymbolTable reset
aefimov
parents: 42247
diff changeset
    82
    /**
48577
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
    83
     * jdk.xml.overrideDefaultParser: enables the use of a 3rd party's parser
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
    84
     * implementation to override the system-default parser.
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
    85
     */
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
    86
    public static final String OVERRIDE_PARSER = "jdk.xml.overrideDefaultParser";
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
    87
    public static final boolean OVERRIDE_PARSER_DEFAULT = SecuritySupport.getJAXPSystemProperty(
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
    88
                    Boolean.class, OVERRIDE_PARSER, "false");
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
    89
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
    90
    /**
39907
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
    91
     * Values for a feature
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
    92
     */
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
    93
    public static final String FEATURE_TRUE = "true";
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
    94
    public static final String FEATURE_FALSE = "false";
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
    95
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
    96
    /**
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
    97
     * Default value of USE_CATALOG. This will read the System property
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
    98
     */
42247
52fafb950d5a 8158619: Very large CDATA section in XML document causes OOME
joehw
parents: 39907
diff changeset
    99
    public static final boolean USE_CATALOG_DEFAULT
52fafb950d5a 8158619: Very large CDATA section in XML document causes OOME
joehw
parents: 39907
diff changeset
   100
            = SecuritySupport.getJAXPSystemProperty(Boolean.class, SP_USE_CATALOG, "true");
52fafb950d5a 8158619: Very large CDATA section in XML document causes OOME
joehw
parents: 39907
diff changeset
   101
52fafb950d5a 8158619: Very large CDATA section in XML document causes OOME
joehw
parents: 39907
diff changeset
   102
    /**
44802
89a545213cad 8176168: Performance drop due to SAXParser SymbolTable reset
aefimov
parents: 42247
diff changeset
   103
     * Default value of RESET_SYMBOL_TABLE. This will read the System property
89a545213cad 8176168: Performance drop due to SAXParser SymbolTable reset
aefimov
parents: 42247
diff changeset
   104
     */
89a545213cad 8176168: Performance drop due to SAXParser SymbolTable reset
aefimov
parents: 42247
diff changeset
   105
    public static final boolean RESET_SYMBOL_TABLE_DEFAULT
89a545213cad 8176168: Performance drop due to SAXParser SymbolTable reset
aefimov
parents: 42247
diff changeset
   106
            = SecuritySupport.getJAXPSystemProperty(Boolean.class, RESET_SYMBOL_TABLE, "false");
89a545213cad 8176168: Performance drop due to SAXParser SymbolTable reset
aefimov
parents: 42247
diff changeset
   107
89a545213cad 8176168: Performance drop due to SAXParser SymbolTable reset
aefimov
parents: 42247
diff changeset
   108
    /**
42247
52fafb950d5a 8158619: Very large CDATA section in XML document causes OOME
joehw
parents: 39907
diff changeset
   109
     * JDK features (will be consolidated in the next major feature revamp
52fafb950d5a 8158619: Very large CDATA section in XML document causes OOME
joehw
parents: 39907
diff changeset
   110
     */
52fafb950d5a 8158619: Very large CDATA section in XML document causes OOME
joehw
parents: 39907
diff changeset
   111
    public final static String CDATA_CHUNK_SIZE = "jdk.xml.cdataChunkSize";
52fafb950d5a 8158619: Very large CDATA section in XML document causes OOME
joehw
parents: 39907
diff changeset
   112
    public static final int CDATA_CHUNK_SIZE_DEFAULT
52fafb950d5a 8158619: Very large CDATA section in XML document causes OOME
joehw
parents: 39907
diff changeset
   113
            = SecuritySupport.getJAXPSystemProperty(Integer.class, CDATA_CHUNK_SIZE, "0");
52fafb950d5a 8158619: Very large CDATA section in XML document causes OOME
joehw
parents: 39907
diff changeset
   114
52fafb950d5a 8158619: Very large CDATA section in XML document causes OOME
joehw
parents: 39907
diff changeset
   115
    /**
48577
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   116
     * The system-default factory
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   117
     */
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   118
    private static final SAXParserFactory defaultSAXFactory = getSAXFactory(false);
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   119
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   120
    /**
42247
52fafb950d5a 8158619: Very large CDATA section in XML document causes OOME
joehw
parents: 39907
diff changeset
   121
     * Returns the value.
52fafb950d5a 8158619: Very large CDATA section in XML document causes OOME
joehw
parents: 39907
diff changeset
   122
     *
52fafb950d5a 8158619: Very large CDATA section in XML document causes OOME
joehw
parents: 39907
diff changeset
   123
     * @param value the specified value
52fafb950d5a 8158619: Very large CDATA section in XML document causes OOME
joehw
parents: 39907
diff changeset
   124
     * @param defValue the default value
52fafb950d5a 8158619: Very large CDATA section in XML document causes OOME
joehw
parents: 39907
diff changeset
   125
     * @return the value, or the default value if the value is null
52fafb950d5a 8158619: Very large CDATA section in XML document causes OOME
joehw
parents: 39907
diff changeset
   126
     */
52fafb950d5a 8158619: Very large CDATA section in XML document causes OOME
joehw
parents: 39907
diff changeset
   127
    public static int getValue(Object value, int defValue) {
52fafb950d5a 8158619: Very large CDATA section in XML document causes OOME
joehw
parents: 39907
diff changeset
   128
        if (value == null) {
52fafb950d5a 8158619: Very large CDATA section in XML document causes OOME
joehw
parents: 39907
diff changeset
   129
            return defValue;
52fafb950d5a 8158619: Very large CDATA section in XML document causes OOME
joehw
parents: 39907
diff changeset
   130
        }
52fafb950d5a 8158619: Very large CDATA section in XML document causes OOME
joehw
parents: 39907
diff changeset
   131
52fafb950d5a 8158619: Very large CDATA section in XML document causes OOME
joehw
parents: 39907
diff changeset
   132
        if (value instanceof Number) {
52fafb950d5a 8158619: Very large CDATA section in XML document causes OOME
joehw
parents: 39907
diff changeset
   133
            return ((Number) value).intValue();
52fafb950d5a 8158619: Very large CDATA section in XML document causes OOME
joehw
parents: 39907
diff changeset
   134
        } else if (value instanceof String) {
52fafb950d5a 8158619: Very large CDATA section in XML document causes OOME
joehw
parents: 39907
diff changeset
   135
            return Integer.parseInt(String.valueOf(value));
52fafb950d5a 8158619: Very large CDATA section in XML document causes OOME
joehw
parents: 39907
diff changeset
   136
        } else {
52fafb950d5a 8158619: Very large CDATA section in XML document causes OOME
joehw
parents: 39907
diff changeset
   137
            throw new IllegalArgumentException("Unexpected class: "
52fafb950d5a 8158619: Very large CDATA section in XML document causes OOME
joehw
parents: 39907
diff changeset
   138
                    + value.getClass());
52fafb950d5a 8158619: Very large CDATA section in XML document causes OOME
joehw
parents: 39907
diff changeset
   139
        }
52fafb950d5a 8158619: Very large CDATA section in XML document causes OOME
joehw
parents: 39907
diff changeset
   140
    }
52fafb950d5a 8158619: Very large CDATA section in XML document causes OOME
joehw
parents: 39907
diff changeset
   141
52fafb950d5a 8158619: Very large CDATA section in XML document causes OOME
joehw
parents: 39907
diff changeset
   142
    /**
52fafb950d5a 8158619: Very large CDATA section in XML document causes OOME
joehw
parents: 39907
diff changeset
   143
     * Sets the XMLReader instance with the specified property if the the
52fafb950d5a 8158619: Very large CDATA section in XML document causes OOME
joehw
parents: 39907
diff changeset
   144
     * property is supported, ignores error if not, issues a warning if so
52fafb950d5a 8158619: Very large CDATA section in XML document causes OOME
joehw
parents: 39907
diff changeset
   145
     * requested.
52fafb950d5a 8158619: Very large CDATA section in XML document causes OOME
joehw
parents: 39907
diff changeset
   146
     *
52fafb950d5a 8158619: Very large CDATA section in XML document causes OOME
joehw
parents: 39907
diff changeset
   147
     * @param reader an XMLReader instance
52fafb950d5a 8158619: Very large CDATA section in XML document causes OOME
joehw
parents: 39907
diff changeset
   148
     * @param property the name of the property
52fafb950d5a 8158619: Very large CDATA section in XML document causes OOME
joehw
parents: 39907
diff changeset
   149
     * @param value the value of the property
52fafb950d5a 8158619: Very large CDATA section in XML document causes OOME
joehw
parents: 39907
diff changeset
   150
     * @param warn a flag indicating whether a warning should be issued
52fafb950d5a 8158619: Very large CDATA section in XML document causes OOME
joehw
parents: 39907
diff changeset
   151
     */
52fafb950d5a 8158619: Very large CDATA section in XML document causes OOME
joehw
parents: 39907
diff changeset
   152
    public static void setXMLReaderPropertyIfSupport(XMLReader reader, String property,
52fafb950d5a 8158619: Very large CDATA section in XML document causes OOME
joehw
parents: 39907
diff changeset
   153
            Object value, boolean warn) {
52fafb950d5a 8158619: Very large CDATA section in XML document causes OOME
joehw
parents: 39907
diff changeset
   154
        try {
52fafb950d5a 8158619: Very large CDATA section in XML document causes OOME
joehw
parents: 39907
diff changeset
   155
            reader.setProperty(property, value);
52fafb950d5a 8158619: Very large CDATA section in XML document causes OOME
joehw
parents: 39907
diff changeset
   156
        } catch (SAXNotRecognizedException | SAXNotSupportedException e) {
52fafb950d5a 8158619: Very large CDATA section in XML document causes OOME
joehw
parents: 39907
diff changeset
   157
            if (warn) {
52fafb950d5a 8158619: Very large CDATA section in XML document causes OOME
joehw
parents: 39907
diff changeset
   158
                XMLSecurityManager.printWarning(reader.getClass().getName(),
52fafb950d5a 8158619: Very large CDATA section in XML document causes OOME
joehw
parents: 39907
diff changeset
   159
                        property, e);
52fafb950d5a 8158619: Very large CDATA section in XML document causes OOME
joehw
parents: 39907
diff changeset
   160
            }
52fafb950d5a 8158619: Very large CDATA section in XML document causes OOME
joehw
parents: 39907
diff changeset
   161
        }
52fafb950d5a 8158619: Very large CDATA section in XML document causes OOME
joehw
parents: 39907
diff changeset
   162
    }
39907
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
   163
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
   164
    /**
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
   165
     * Returns the value of a Catalog feature by the property name.
42247
52fafb950d5a 8158619: Very large CDATA section in XML document causes OOME
joehw
parents: 39907
diff changeset
   166
     *
39907
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
   167
     * @param features a CatalogFeatures instance
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
   168
     * @param name the name of a Catalog feature
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
   169
     * @return the value of a Catalog feature, null if the name does not match
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
   170
     * any feature supported by the Catalog.
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
   171
     */
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
   172
    public static String getCatalogFeature(CatalogFeatures features, String name) {
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
   173
        for (Feature feature : Feature.values()) {
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
   174
            if (feature.getPropertyName().equals(name)) {
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
   175
                return features.get(feature);
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
   176
            }
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
   177
        }
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
   178
        return null;
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
   179
    }
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
   180
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
   181
    /**
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
   182
     * Creates an instance of a CatalogFeatures.
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
   183
     *
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
   184
     * @param defer the defer property defined in CatalogFeatures
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
   185
     * @param file the file path to a catalog
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
   186
     * @param prefer the prefer property defined in CatalogFeatures
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
   187
     * @param resolve the resolve property defined in CatalogFeatures
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
   188
     * @return a {@link javax.xml.transform.Source} object
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
   189
     */
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
   190
    public static CatalogFeatures getCatalogFeatures(String defer, String file,
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
   191
            String prefer, String resolve) {
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
   192
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
   193
        CatalogFeatures.Builder builder = CatalogFeatures.builder();
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
   194
        if (file != null) {
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
   195
            builder = builder.with(CatalogFeatures.Feature.FILES, file);
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
   196
        }
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
   197
        if (prefer != null) {
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
   198
            builder = builder.with(CatalogFeatures.Feature.PREFER, prefer);
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
   199
        }
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
   200
        if (defer != null) {
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
   201
            builder = builder.with(CatalogFeatures.Feature.DEFER, defer);
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
   202
        }
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
   203
        if (resolve != null) {
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
   204
            builder = builder.with(CatalogFeatures.Feature.RESOLVE, resolve);
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
   205
        }
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
   206
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
   207
        return builder.build();
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
   208
    }
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
   209
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
   210
    /**
42247
52fafb950d5a 8158619: Very large CDATA section in XML document causes OOME
joehw
parents: 39907
diff changeset
   211
     * Passing on the CatalogFeatures settings from one Xerces configuration
52fafb950d5a 8158619: Very large CDATA section in XML document causes OOME
joehw
parents: 39907
diff changeset
   212
     * object to another.
39907
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
   213
     *
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
   214
     * @param config1 a Xerces configuration object
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
   215
     * @param config2 a Xerces configuration object
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
   216
     */
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
   217
    public static void catalogFeaturesConfig2Config(XMLComponentManager config1,
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
   218
            ParserConfigurationSettings config2) {
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
   219
        boolean supportCatalog = true;
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
   220
        boolean useCatalog = config1.getFeature(XMLConstants.USE_CATALOG);
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
   221
        try {
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
   222
            config2.setFeature(JdkXmlUtils.USE_CATALOG, useCatalog);
42247
52fafb950d5a 8158619: Very large CDATA section in XML document causes OOME
joehw
parents: 39907
diff changeset
   223
        } catch (XMLConfigurationException e) {
39907
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
   224
            supportCatalog = false;
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
   225
        }
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
   226
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
   227
        if (supportCatalog && useCatalog) {
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
   228
            try {
42247
52fafb950d5a 8158619: Very large CDATA section in XML document causes OOME
joehw
parents: 39907
diff changeset
   229
                for (CatalogFeatures.Feature f : CatalogFeatures.Feature.values()) {
39907
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
   230
                    config2.setProperty(f.getPropertyName(), config1.getProperty(f.getPropertyName()));
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
   231
                }
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
   232
            } catch (XMLConfigurationException e) {
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
   233
                //shall not happen for internal settings
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
   234
            }
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
   235
        }
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
   236
    }
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
   237
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
   238
    /**
42247
52fafb950d5a 8158619: Very large CDATA section in XML document causes OOME
joehw
parents: 39907
diff changeset
   239
     * Passing on the CatalogFeatures settings from a Xerces configuration
52fafb950d5a 8158619: Very large CDATA section in XML document causes OOME
joehw
parents: 39907
diff changeset
   240
     * object to an XMLReader.
39907
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
   241
     *
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
   242
     * @param config a Xerces configuration object
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
   243
     * @param reader an XMLReader
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
   244
     */
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
   245
    public static void catalogFeaturesConfig2Reader(XMLComponentManager config, XMLReader reader) {
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
   246
        boolean supportCatalog = true;
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
   247
        boolean useCatalog = config.getFeature(XMLConstants.USE_CATALOG);
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
   248
        try {
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
   249
            reader.setFeature(JdkXmlUtils.USE_CATALOG, useCatalog);
42247
52fafb950d5a 8158619: Very large CDATA section in XML document causes OOME
joehw
parents: 39907
diff changeset
   250
        } catch (SAXNotRecognizedException | SAXNotSupportedException e) {
39907
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
   251
            supportCatalog = false;
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
   252
        }
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
   253
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
   254
        if (supportCatalog && useCatalog) {
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
   255
            try {
42247
52fafb950d5a 8158619: Very large CDATA section in XML document causes OOME
joehw
parents: 39907
diff changeset
   256
                for (CatalogFeatures.Feature f : CatalogFeatures.Feature.values()) {
39907
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
   257
                    reader.setProperty(f.getPropertyName(), config.getProperty(f.getPropertyName()));
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
   258
                }
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
   259
            } catch (SAXNotRecognizedException | SAXNotSupportedException e) {
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
   260
                //shall not happen for internal settings
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
   261
            }
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
   262
        }
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
   263
    }
48577
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   264
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   265
    /**
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   266
     * Returns an XMLReader instance. If overrideDefaultParser is requested, use
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   267
     * SAXParserFactory or XMLReaderFactory, otherwise use the system-default
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   268
     * SAXParserFactory to locate an XMLReader.
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   269
     *
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   270
     * @param overrideDefaultParser a flag indicating whether a 3rd party's
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   271
     * parser implementation may be used to override the system-default one
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   272
     * @param secureProcessing a flag indicating whether secure processing is
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   273
     * requested
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   274
     * @param useXMLReaderFactory a flag indicating when the XMLReader should be
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   275
     * created using XMLReaderFactory. True is a compatibility mode that honors
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   276
     * the property org.xml.sax.driver (see JDK-6490921).
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   277
     * @return an XMLReader instance
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   278
     */
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   279
    public static XMLReader getXMLReader(boolean overrideDefaultParser,
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   280
            boolean secureProcessing) {
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   281
        SAXParserFactory saxFactory;
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   282
        XMLReader reader = null;
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   283
        String spSAXDriver = SecuritySupport.getSystemProperty(SAX_DRIVER);
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   284
        if (spSAXDriver != null) {
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   285
            reader = getXMLReaderWXMLReaderFactory();
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   286
        } else if (overrideDefaultParser) {
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   287
            reader = getXMLReaderWSAXFactory(overrideDefaultParser);
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   288
        }
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   289
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   290
        if (reader != null) {
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   291
            if (secureProcessing) {
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   292
                try {
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   293
                    reader.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, secureProcessing);
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   294
                } catch (SAXException e) {
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   295
                    XMLSecurityManager.printWarning(reader.getClass().getName(),
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   296
                            XMLConstants.FEATURE_SECURE_PROCESSING, e);
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   297
                }
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   298
            }
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   299
            try {
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   300
                reader.setFeature(NAMESPACES_FEATURE, true);
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   301
                reader.setFeature(NAMESPACE_PREFIXES_FEATURE, false);
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   302
            } catch (SAXException se) {
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   303
                // older version of a parser
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   304
            }
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   305
            return reader;
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   306
        }
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   307
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   308
        // use the system-default
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   309
        saxFactory = defaultSAXFactory;
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   310
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   311
        try {
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   312
            reader = saxFactory.newSAXParser().getXMLReader();
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   313
        } catch (ParserConfigurationException | SAXException ex) {
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   314
            // shall not happen with the system-default reader
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   315
        }
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   316
        return reader;
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   317
    }
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   318
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   319
    /**
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   320
     * Creates a system-default DOM Document.
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   321
     *
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   322
     * @return a DOM Document instance
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   323
     */
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   324
    public static Document getDOMDocument() {
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   325
        try {
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   326
            DocumentBuilderFactory dbf = JdkXmlUtils.getDOMFactory(false);
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   327
            return dbf.newDocumentBuilder().newDocument();
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   328
        } catch (ParserConfigurationException pce) {
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   329
            // can never happen with the system-default configuration
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   330
        }
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   331
        return null;
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   332
    }
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   333
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   334
    /**
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   335
     * Returns a DocumentBuilderFactory instance.
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   336
     *
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   337
     * @param overrideDefaultParser a flag indicating whether the system-default
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   338
     * implementation may be overridden. If the system property of the
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   339
     * DOM factory ID is set, override is always allowed.
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   340
     *
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   341
     * @return a DocumentBuilderFactory instance.
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   342
     */
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   343
    public static DocumentBuilderFactory getDOMFactory(boolean overrideDefaultParser) {
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   344
        boolean override = overrideDefaultParser;
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   345
        String spDOMFactory = SecuritySupport.getJAXPSystemProperty(DOM_FACTORY_ID);
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   346
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   347
        if (spDOMFactory != null && System.getSecurityManager() == null) {
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   348
            override = true;
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   349
        }
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   350
        DocumentBuilderFactory dbf
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   351
                = !override
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   352
                        ? new DocumentBuilderFactoryImpl()
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   353
                        : DocumentBuilderFactory.newInstance();
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   354
        dbf.setNamespaceAware(true);
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   355
        // false is the default setting. This step here is for compatibility
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   356
        dbf.setValidating(false);
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   357
        return dbf;
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   358
    }
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   359
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   360
    /**
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   361
     * Returns a SAXParserFactory instance.
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   362
     *
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   363
     * @param overrideDefaultParser a flag indicating whether the system-default
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   364
     * implementation may be overridden. If the system property of the
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   365
     * DOM factory ID is set, override is always allowed.
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   366
     *
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   367
     * @return a SAXParserFactory instance.
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   368
     */
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   369
    public static SAXParserFactory getSAXFactory(boolean overrideDefaultParser) {
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   370
        boolean override = overrideDefaultParser;
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   371
        String spSAXFactory = SecuritySupport.getJAXPSystemProperty(SAX_FACTORY_ID);
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   372
        if (spSAXFactory != null && System.getSecurityManager() == null) {
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   373
            override = true;
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   374
        }
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   375
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   376
        SAXParserFactory factory
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   377
                = !override
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   378
                        ? new SAXParserFactoryImpl()
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   379
                        : SAXParserFactory.newInstance();
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   380
        factory.setNamespaceAware(true);
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   381
        return factory;
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   382
    }
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   383
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   384
    public static SAXTransformerFactory getSAXTransformFactory(boolean overrideDefaultParser) {
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   385
        SAXTransformerFactory tf = overrideDefaultParser
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   386
                ? (SAXTransformerFactory) SAXTransformerFactory.newInstance()
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   387
                : (SAXTransformerFactory) new TransformerFactoryImpl();
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   388
        try {
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   389
            tf.setFeature(OVERRIDE_PARSER, overrideDefaultParser);
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   390
        } catch (TransformerConfigurationException ex) {
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   391
            // ignore since it'd never happen with the JDK impl.
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   392
        }
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   393
        return tf;
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   394
    }
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   395
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   396
    private static XMLReader getXMLReaderWSAXFactory(boolean overrideDefaultParser) {
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   397
        SAXParserFactory saxFactory = getSAXFactory(overrideDefaultParser);
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   398
        try {
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   399
            return saxFactory.newSAXParser().getXMLReader();
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   400
        } catch (ParserConfigurationException | SAXException ex) {
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   401
            return getXMLReaderWXMLReaderFactory();
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   402
        }
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   403
    }
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   404
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   405
    @SuppressWarnings("deprecation")
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   406
    private static XMLReader getXMLReaderWXMLReaderFactory() {
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   407
        try {
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   408
            return org.xml.sax.helpers.XMLReaderFactory.createXMLReader();
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   409
        } catch (SAXException ex1) {
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   410
        }
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   411
        return null;
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 47216
diff changeset
   412
    }
39907
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents:
diff changeset
   413
}