jdk/src/java.base/share/classes/jdk/internal/util/xml/PropertiesDefaultHandler.java
author martin
Tue, 15 Sep 2015 21:56:04 -0700
changeset 32649 2ee9017c7597
parent 25859 3317bb8137f4
permissions -rw-r--r--
8136583: Core libraries should use blessed modifier order Summary: Run blessed-modifier-order script (see bug) Reviewed-by: psandoz, chegar, alanb, plevart
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14908
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 21303
diff changeset
     2
 * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
14908
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
     4
 *
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
    10
 *
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
    15
 * accompanied this code).
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
    16
 *
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
    20
 *
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
    23
 * questions.
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
    24
 */
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
    25
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
    26
package jdk.internal.util.xml;
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
    27
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
    28
import java.io.*;
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
    29
import java.util.InvalidPropertiesFormatException;
21303
24143f06640d 8016344: (props) Properties.storeToXML behaviour has changed from JDK 6 to 7
dfuchs
parents: 14908
diff changeset
    30
import java.util.Map.Entry;
14908
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
    31
import java.util.Properties;
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
    32
import jdk.internal.org.xml.sax.Attributes;
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
    33
import jdk.internal.org.xml.sax.InputSource;
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
    34
import jdk.internal.org.xml.sax.SAXException;
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
    35
import jdk.internal.org.xml.sax.SAXParseException;
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
    36
import jdk.internal.org.xml.sax.helpers.DefaultHandler;
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
    37
import jdk.internal.util.xml.impl.SAXParserImpl;
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
    38
import jdk.internal.util.xml.impl.XMLStreamWriterImpl;
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
    39
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
    40
/**
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
    41
 * A class used to aid in Properties load and save in XML. This class is
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
    42
 * re-implemented using a subset of SAX
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
    43
 *
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
    44
 * @author Joe Wang
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 24373
diff changeset
    45
 * @since 1.8
14908
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
    46
 */
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
    47
public class PropertiesDefaultHandler extends DefaultHandler {
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
    48
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
    49
    // Elements specified in the properties.dtd
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
    50
    private static final String ELEMENT_ROOT = "properties";
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
    51
    private static final String ELEMENT_COMMENT = "comment";
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
    52
    private static final String ELEMENT_ENTRY = "entry";
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
    53
    private static final String ATTR_KEY = "key";
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
    54
    // The required DTD URI for exported properties
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
    55
    private static final String PROPS_DTD_DECL =
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
    56
            "<!DOCTYPE properties SYSTEM \"http://java.sun.com/dtd/properties.dtd\">";
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
    57
    private static final String PROPS_DTD_URI =
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
    58
            "http://java.sun.com/dtd/properties.dtd";
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
    59
    private static final String PROPS_DTD =
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
    60
            "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
    61
            + "<!-- DTD for properties -->"
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
    62
            + "<!ELEMENT properties ( comment?, entry* ) >"
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
    63
            + "<!ATTLIST properties"
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
    64
            + " version CDATA #FIXED \"1.0\">"
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
    65
            + "<!ELEMENT comment (#PCDATA) >"
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
    66
            + "<!ELEMENT entry (#PCDATA) >"
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
    67
            + "<!ATTLIST entry "
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
    68
            + " key CDATA #REQUIRED>";
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
    69
    /**
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
    70
     * Version number for the format of exported properties files.
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
    71
     */
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
    72
    private static final String EXTERNAL_XML_VERSION = "1.0";
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
    73
    private Properties properties;
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
    74
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
    75
    public void load(Properties props, InputStream in)
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
    76
        throws IOException, InvalidPropertiesFormatException, UnsupportedEncodingException
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
    77
    {
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
    78
        this.properties = props;
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
    79
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
    80
        try {
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
    81
            SAXParser parser = new SAXParserImpl();
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
    82
            parser.parse(in, this);
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
    83
        } catch (SAXException saxe) {
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
    84
            throw new InvalidPropertiesFormatException(saxe);
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
    85
        }
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
    86
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
    87
        /**
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
    88
         * String xmlVersion = propertiesElement.getAttribute("version"); if
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
    89
         * (xmlVersion.compareTo(EXTERNAL_XML_VERSION) > 0) throw new
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
    90
         * InvalidPropertiesFormatException( "Exported Properties file format
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
    91
         * version " + xmlVersion + " is not supported. This java installation
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
    92
         * can read" + " versions " + EXTERNAL_XML_VERSION + " or older. You" +
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
    93
         * " may need to install a newer version of JDK.");
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
    94
         */
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
    95
    }
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
    96
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
    97
    public void store(Properties props, OutputStream os, String comment, String encoding)
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
    98
        throws IOException
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
    99
    {
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
   100
        try {
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
   101
            XMLStreamWriter writer = new XMLStreamWriterImpl(os, encoding);
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
   102
            writer.writeStartDocument();
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
   103
            writer.writeDTD(PROPS_DTD_DECL);
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
   104
            writer.writeStartElement(ELEMENT_ROOT);
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
   105
            if (comment != null && comment.length() > 0) {
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
   106
                writer.writeStartElement(ELEMENT_COMMENT);
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
   107
                writer.writeCharacters(comment);
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
   108
                writer.writeEndElement();
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
   109
            }
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
   110
21303
24143f06640d 8016344: (props) Properties.storeToXML behaviour has changed from JDK 6 to 7
dfuchs
parents: 14908
diff changeset
   111
            synchronized(props) {
24143f06640d 8016344: (props) Properties.storeToXML behaviour has changed from JDK 6 to 7
dfuchs
parents: 14908
diff changeset
   112
                for (Entry<Object, Object> e : props.entrySet()) {
24143f06640d 8016344: (props) Properties.storeToXML behaviour has changed from JDK 6 to 7
dfuchs
parents: 14908
diff changeset
   113
                    final Object k = e.getKey();
24143f06640d 8016344: (props) Properties.storeToXML behaviour has changed from JDK 6 to 7
dfuchs
parents: 14908
diff changeset
   114
                    final Object v = e.getValue();
24143f06640d 8016344: (props) Properties.storeToXML behaviour has changed from JDK 6 to 7
dfuchs
parents: 14908
diff changeset
   115
                    if (k instanceof String && v instanceof String) {
24143f06640d 8016344: (props) Properties.storeToXML behaviour has changed from JDK 6 to 7
dfuchs
parents: 14908
diff changeset
   116
                        writer.writeStartElement(ELEMENT_ENTRY);
24143f06640d 8016344: (props) Properties.storeToXML behaviour has changed from JDK 6 to 7
dfuchs
parents: 14908
diff changeset
   117
                        writer.writeAttribute(ATTR_KEY, (String)k);
24143f06640d 8016344: (props) Properties.storeToXML behaviour has changed from JDK 6 to 7
dfuchs
parents: 14908
diff changeset
   118
                        writer.writeCharacters((String)v);
24143f06640d 8016344: (props) Properties.storeToXML behaviour has changed from JDK 6 to 7
dfuchs
parents: 14908
diff changeset
   119
                        writer.writeEndElement();
24143f06640d 8016344: (props) Properties.storeToXML behaviour has changed from JDK 6 to 7
dfuchs
parents: 14908
diff changeset
   120
                    }
24143f06640d 8016344: (props) Properties.storeToXML behaviour has changed from JDK 6 to 7
dfuchs
parents: 14908
diff changeset
   121
                }
14908
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
   122
            }
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
   123
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
   124
            writer.writeEndElement();
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
   125
            writer.writeEndDocument();
24373
d20f4a10c235 8043119: (props) Properties.storeToXML closes output stream
alanb
parents: 23010
diff changeset
   126
            writer.flush();
14908
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
   127
        } catch (XMLStreamException e) {
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
   128
            if (e.getCause() instanceof UnsupportedEncodingException) {
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
   129
                throw (UnsupportedEncodingException) e.getCause();
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
   130
            }
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
   131
            throw new IOException(e);
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
   132
        }
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
   133
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
   134
    }
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
   135
    ////////////////////////////////////////////////////////////////////
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
   136
    // Validate while parsing
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
   137
    ////////////////////////////////////////////////////////////////////
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 25859
diff changeset
   138
    static final String ALLOWED_ELEMENTS = "properties, comment, entry";
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 25859
diff changeset
   139
    static final String ALLOWED_COMMENT = "comment";
14908
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
   140
    ////////////////////////////////////////////////////////////////////
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
   141
    // Handler methods
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
   142
    ////////////////////////////////////////////////////////////////////
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
   143
    StringBuffer buf = new StringBuffer();
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
   144
    boolean sawComment = false;
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
   145
    boolean validEntry = false;
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
   146
    int rootElem = 0;
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
   147
    String key;
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
   148
    String rootElm;
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
   149
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
   150
    @Override
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
   151
    public void startElement(String uri, String localName, String qName, Attributes attributes)
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
   152
        throws SAXException
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
   153
    {
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
   154
        if (rootElem < 2) {
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
   155
            rootElem++;
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
   156
        }
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
   157
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
   158
        if (rootElm == null) {
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
   159
            fatalError(new SAXParseException("An XML properties document must contain"
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
   160
                    + " the DOCTYPE declaration as defined by java.util.Properties.", null));
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
   161
        }
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
   162
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
   163
        if (rootElem == 1 && !rootElm.equals(qName)) {
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
   164
            fatalError(new SAXParseException("Document root element \"" + qName
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
   165
                    + "\", must match DOCTYPE root \"" + rootElm + "\"", null));
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
   166
        }
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
   167
        if (!ALLOWED_ELEMENTS.contains(qName)) {
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
   168
            fatalError(new SAXParseException("Element type \"" + qName + "\" must be declared.", null));
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
   169
        }
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
   170
        if (qName.equals(ELEMENT_ENTRY)) {
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
   171
            validEntry = true;
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
   172
            key = attributes.getValue(ATTR_KEY);
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
   173
            if (key == null) {
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
   174
                fatalError(new SAXParseException("Attribute \"key\" is required and must be specified for element type \"entry\"", null));
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
   175
            }
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
   176
        } else if (qName.equals(ALLOWED_COMMENT)) {
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
   177
            if (sawComment) {
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
   178
                fatalError(new SAXParseException("Only one comment element may be allowed. "
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
   179
                        + "The content of element type \"properties\" must match \"(comment?,entry*)\"", null));
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
   180
            }
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
   181
            sawComment = true;
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
   182
        }
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
   183
    }
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
   184
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
   185
    @Override
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
   186
    public void characters(char[] ch, int start, int length) throws SAXException {
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
   187
        if (validEntry) {
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
   188
            buf.append(ch, start, length);
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
   189
        }
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
   190
    }
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
   191
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
   192
    @Override
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
   193
    public void endElement(String uri, String localName, String qName) throws SAXException {
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
   194
        if (!ALLOWED_ELEMENTS.contains(qName)) {
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
   195
            fatalError(new SAXParseException("Element: " + qName + " is invalid, must match  \"(comment?,entry*)\".", null));
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
   196
        }
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
   197
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
   198
        if (validEntry) {
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
   199
            properties.setProperty(key, buf.toString());
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
   200
            buf.delete(0, buf.length());
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
   201
            validEntry = false;
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
   202
        }
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
   203
    }
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
   204
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
   205
    @Override
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
   206
    public void notationDecl(String name, String publicId, String systemId) throws SAXException {
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
   207
        rootElm = name;
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
   208
    }
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
   209
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
   210
    @Override
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
   211
    public InputSource resolveEntity(String pubid, String sysid)
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
   212
            throws SAXException, IOException {
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
   213
        {
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
   214
            if (sysid.equals(PROPS_DTD_URI)) {
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
   215
                InputSource is;
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
   216
                is = new InputSource(new StringReader(PROPS_DTD));
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
   217
                is.setSystemId(PROPS_DTD_URI);
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
   218
                return is;
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
   219
            }
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
   220
            throw new SAXException("Invalid system identifier: " + sysid);
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
   221
        }
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
   222
    }
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
   223
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
   224
    @Override
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
   225
    public void error(SAXParseException x) throws SAXException {
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
   226
        throw x;
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
   227
    }
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
   228
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
   229
    @Override
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
   230
    public void fatalError(SAXParseException x) throws SAXException {
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
   231
        throw x;
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
   232
    }
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
   233
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
   234
    @Override
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
   235
    public void warning(SAXParseException x) throws SAXException {
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
   236
        throw x;
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
   237
    }
1d01112e9ce5 8004371: (props) Properties.loadFromXML needs small footprint XML parser as fallback when JAXP is not present
joehw
parents:
diff changeset
   238
}