jdk/src/share/classes/java/util/XMLUtils.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2003-2004 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.util;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import org.xml.sax.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import org.xml.sax.helpers.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import org.w3c.dom.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import javax.xml.parsers.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import javax.xml.transform.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import javax.xml.transform.dom.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import javax.xml.transform.stream.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * A class used to aid in Properties load and save in XML. Keeping this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * code outside of Properties helps reduce the number of classes loaded
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * when Properties is loaded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * @author  Michael McCloskey
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * @since   1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
class XMLUtils {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    // XML loading and saving methods for Properties
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    // The required DTD URI for exported properties
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    private static final String PROPS_DTD_URI =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    "http://java.sun.com/dtd/properties.dtd";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private static final String PROPS_DTD =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    "<!-- DTD for properties -->"                +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    "<!ELEMENT properties ( comment?, entry* ) >"+
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    "<!ATTLIST properties"                       +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        " version CDATA #FIXED \"1.0\">"         +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    "<!ELEMENT comment (#PCDATA) >"              +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    "<!ELEMENT entry (#PCDATA) >"                +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    "<!ATTLIST entry "                           +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        " key CDATA #REQUIRED>";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * Version number for the format of exported properties files.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    private static final String EXTERNAL_XML_VERSION = "1.0";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    static void load(Properties props, InputStream in)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        throws IOException, InvalidPropertiesFormatException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        Document doc = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            doc = getLoadingDoc(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        } catch (SAXException saxe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            throw new InvalidPropertiesFormatException(saxe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        Element propertiesElement = (Element)doc.getChildNodes().item(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        String xmlVersion = propertiesElement.getAttribute("version");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        if (xmlVersion.compareTo(EXTERNAL_XML_VERSION) > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            throw new InvalidPropertiesFormatException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                "Exported Properties file format version " + xmlVersion +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                " is not supported. This java installation can read" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                " versions " + EXTERNAL_XML_VERSION + " or older. You" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                " may need to install a newer version of JDK.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        importProperties(props, propertiesElement);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    static Document getLoadingDoc(InputStream in)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        throws SAXException, IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        dbf.setIgnoringElementContentWhitespace(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        dbf.setValidating(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        dbf.setCoalescing(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        dbf.setIgnoringComments(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            DocumentBuilder db = dbf.newDocumentBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            db.setEntityResolver(new Resolver());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            db.setErrorHandler(new EH());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            InputSource is = new InputSource(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            return db.parse(is);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        } catch (ParserConfigurationException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            throw new Error(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    static void importProperties(Properties props, Element propertiesElement) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        NodeList entries = propertiesElement.getChildNodes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        int numEntries = entries.getLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        int start = numEntries > 0 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            entries.item(0).getNodeName().equals("comment") ? 1 : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        for (int i=start; i<numEntries; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            Element entry = (Element)entries.item(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            if (entry.hasAttribute("key")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                Node n = entry.getFirstChild();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                String val = (n == null) ? "" : n.getNodeValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                props.setProperty(entry.getAttribute("key"), val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    static void save(Properties props, OutputStream os, String comment,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                     String encoding)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        DocumentBuilder db = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            db = dbf.newDocumentBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        } catch (ParserConfigurationException pce) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            assert(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        Document doc = db.newDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        Element properties =  (Element)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            doc.appendChild(doc.createElement("properties"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        if (comment != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            Element comments = (Element)properties.appendChild(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                doc.createElement("comment"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            comments.appendChild(doc.createTextNode(comment));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        Set keys = props.keySet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        Iterator i = keys.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        while(i.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            String key = (String)i.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            Element entry = (Element)properties.appendChild(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                doc.createElement("entry"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            entry.setAttribute("key", key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            entry.appendChild(doc.createTextNode(props.getProperty(key)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        emitDocument(doc, os, encoding);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    static void emitDocument(Document doc, OutputStream os, String encoding)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        TransformerFactory tf = TransformerFactory.newInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        Transformer t = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            t = tf.newTransformer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            t.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, PROPS_DTD_URI);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            t.setOutputProperty(OutputKeys.INDENT, "yes");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            t.setOutputProperty(OutputKeys.METHOD, "xml");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            t.setOutputProperty(OutputKeys.ENCODING, encoding);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        } catch (TransformerConfigurationException tce) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            assert(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        DOMSource doms = new DOMSource(doc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        StreamResult sr = new StreamResult(os);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            t.transform(doms, sr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        } catch (TransformerException te) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            IOException ioe = new IOException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            ioe.initCause(te);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            throw ioe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    private static class Resolver implements EntityResolver {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        public InputSource resolveEntity(String pid, String sid)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            throws SAXException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            if (sid.equals(PROPS_DTD_URI)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                InputSource is;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                is = new InputSource(new StringReader(PROPS_DTD));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                is.setSystemId(PROPS_DTD_URI);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                return is;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            throw new SAXException("Invalid system identifier: " + sid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    private static class EH implements ErrorHandler {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        public void error(SAXParseException x) throws SAXException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            throw x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        public void fatalError(SAXParseException x) throws SAXException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            throw x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        public void warning(SAXParseException x) throws SAXException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            throw x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
}