jdk/src/share/classes/java/util/prefs/XmlSupport.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 2002-2006 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.prefs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import javax.xml.parsers.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import javax.xml.transform.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import javax.xml.transform.dom.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import javax.xml.transform.stream.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import org.xml.sax.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import org.w3c.dom.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * XML Support for java.util.prefs. Methods to import and export preference
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * nodes and subtrees.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * @author  Josh Bloch and Mark Reinhold
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * @see     Preferences
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * @since   1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
class XmlSupport {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    // The required DTD URI for exported preferences
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    private static final String PREFS_DTD_URI =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        "http://java.sun.com/dtd/preferences.dtd";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    // The actual DTD corresponding to the URI
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private static final String PREFS_DTD =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        "<!-- DTD for preferences -->"               +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        "<!ELEMENT preferences (root) >"             +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        "<!ATTLIST preferences"                      +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        " EXTERNAL_XML_VERSION CDATA \"0.0\"  >"     +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        "<!ELEMENT root (map, node*) >"              +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        "<!ATTLIST root"                             +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        "          type (system|user) #REQUIRED >"   +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        "<!ELEMENT node (map, node*) >"              +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        "<!ATTLIST node"                             +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        "          name CDATA #REQUIRED >"           +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        "<!ELEMENT map (entry*) >"                   +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        "<!ATTLIST map"                              +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        "  MAP_XML_VERSION CDATA \"0.0\"  >"         +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        "<!ELEMENT entry EMPTY >"                    +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        "<!ATTLIST entry"                            +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        "          key CDATA #REQUIRED"              +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        "          value CDATA #REQUIRED >"          ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * Version number for the format exported preferences files.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    private static final String EXTERNAL_XML_VERSION = "1.0";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * Version number for the internal map files.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    private static final String MAP_XML_VERSION = "1.0";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * Export the specified preferences node and, if subTree is true, all
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * subnodes, to the specified output stream.  Preferences are exported as
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * an XML document conforming to the definition in the Preferences spec.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * @throws IOException if writing to the specified output stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     *         results in an <tt>IOException</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * @throws BackingStoreException if preference data cannot be read from
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     *         backing store.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * @throws IllegalStateException if this node (or an ancestor) has been
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     *         removed with the {@link #removeNode()} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    static void export(OutputStream os, final Preferences p, boolean subTree)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        throws IOException, BackingStoreException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        if (((AbstractPreferences)p).isRemoved())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            throw new IllegalStateException("Node has been removed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        Document doc = createPrefsDoc("preferences");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        Element preferences =  doc.getDocumentElement() ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        preferences.setAttribute("EXTERNAL_XML_VERSION", EXTERNAL_XML_VERSION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        Element xmlRoot =  (Element)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        preferences.appendChild(doc.createElement("root"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        xmlRoot.setAttribute("type", (p.isUserNode() ? "user" : "system"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        // Get bottom-up list of nodes from p to root, excluding root
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        List ancestors = new ArrayList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        for (Preferences kid = p, dad = kid.parent(); dad != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                                   kid = dad, dad = kid.parent()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            ancestors.add(kid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        Element e = xmlRoot;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        for (int i=ancestors.size()-1; i >= 0; i--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            e.appendChild(doc.createElement("map"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            e = (Element) e.appendChild(doc.createElement("node"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            e.setAttribute("name", ((Preferences)ancestors.get(i)).name());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        putPreferencesInXml(e, doc, p, subTree);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        writeDoc(doc, os);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * Put the preferences in the specified Preferences node into the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * specified XML element which is assumed to represent a node
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * in the specified XML document which is assumed to conform to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * PREFS_DTD.  If subTree is true, create children of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * XML node conforming to all of the children of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * Preferences node and recurse.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * @throws BackingStoreException if it is not possible to read
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     *         the preferences or children out of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     *         preferences node.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    private static void putPreferencesInXml(Element elt, Document doc,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
               Preferences prefs, boolean subTree) throws BackingStoreException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        Preferences[] kidsCopy = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        String[] kidNames = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        // Node is locked to export its contents and get a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        // copy of children, then lock is released,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        // and, if subTree = true, recursive calls are made on children
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        synchronized (((AbstractPreferences)prefs).lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            // Check if this node was concurrently removed. If yes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            // remove it from XML Document and return.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            if (((AbstractPreferences)prefs).isRemoved()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                elt.getParentNode().removeChild(elt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            // Put map in xml element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            String[] keys = prefs.keys();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            Element map = (Element) elt.appendChild(doc.createElement("map"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            for (int i=0; i<keys.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                Element entry = (Element)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                    map.appendChild(doc.createElement("entry"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                entry.setAttribute("key", keys[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                // NEXT STATEMENT THROWS NULL PTR EXC INSTEAD OF ASSERT FAIL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                entry.setAttribute("value", prefs.get(keys[i], null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            // Recurse if appropriate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            if (subTree) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                /* Get a copy of kids while lock is held */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                kidNames = prefs.childrenNames();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                kidsCopy = new Preferences[kidNames.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                for (int i = 0; i <  kidNames.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                    kidsCopy[i] = prefs.node(kidNames[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            // release lock
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        if (subTree) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            for (int i=0; i < kidNames.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                Element xmlKid = (Element)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                    elt.appendChild(doc.createElement("node"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                xmlKid.setAttribute("name", kidNames[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                putPreferencesInXml(xmlKid, doc, kidsCopy[i], subTree);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * Import preferences from the specified input stream, which is assumed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * to contain an XML document in the format described in the Preferences
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * spec.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * @throws IOException if reading from the specified output stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     *         results in an <tt>IOException</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * @throws InvalidPreferencesFormatException Data on input stream does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     *         constitute a valid XML document with the mandated document type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    static void importPreferences(InputStream is)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        throws IOException, InvalidPreferencesFormatException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            Document doc = loadPrefsDoc(is);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            String xmlVersion =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                doc.getDocumentElement().getAttribute("EXTERNAL_XML_VERSION");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            if (xmlVersion.compareTo(EXTERNAL_XML_VERSION) > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                throw new InvalidPreferencesFormatException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                "Exported preferences file format version " + xmlVersion +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                " is not supported. This java installation can read" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                " versions " + EXTERNAL_XML_VERSION + " or older. You may need" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                " to install a newer version of JDK.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            Element xmlRoot = (Element) doc.getDocumentElement().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                                               getChildNodes().item(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            Preferences prefsRoot =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                (xmlRoot.getAttribute("type").equals("user") ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                            Preferences.userRoot() : Preferences.systemRoot());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            ImportSubtree(prefsRoot, xmlRoot);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        } catch(SAXException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            throw new InvalidPreferencesFormatException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * Create a new prefs XML document.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    private static Document createPrefsDoc( String qname ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            DOMImplementation di = DocumentBuilderFactory.newInstance().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                newDocumentBuilder().getDOMImplementation();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            DocumentType dt = di.createDocumentType(qname, null, PREFS_DTD_URI);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            return di.createDocument(null, qname, dt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        } catch(ParserConfigurationException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            throw new AssertionError(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * Load an XML document from specified input stream, which must
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * have the requisite DTD URI.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    private static Document loadPrefsDoc(InputStream in)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        throws SAXException, IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        dbf.setIgnoringElementContentWhitespace(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        dbf.setValidating(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        dbf.setCoalescing(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        dbf.setIgnoringComments(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            DocumentBuilder db = dbf.newDocumentBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            db.setEntityResolver(new Resolver());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            db.setErrorHandler(new EH());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            return db.parse(new InputSource(in));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        } catch (ParserConfigurationException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            throw new AssertionError(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * Write XML document to the specified output stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    private static final void writeDoc(Document doc, OutputStream out)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            TransformerFactory tf = TransformerFactory.newInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                tf.setAttribute("indent-number", new Integer(2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            } catch (IllegalArgumentException iae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                //Ignore the IAE. Should not fail the writeout even the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                //transformer provider does not support "indent-number".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            Transformer t = tf.newTransformer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            t.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, doc.getDoctype().getSystemId());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            t.setOutputProperty(OutputKeys.INDENT, "yes");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            //Transformer resets the "indent" info if the "result" is a StreamResult with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            //an OutputStream object embedded, creating a Writer object on top of that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            //OutputStream object however works.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            t.transform(new DOMSource(doc),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                        new StreamResult(new BufferedWriter(new OutputStreamWriter(out, "UTF-8"))));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        } catch(TransformerException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            throw new AssertionError(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * Recursively traverse the specified preferences node and store
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * the described preferences into the system or current user
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * preferences tree, as appropriate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    private static void ImportSubtree(Preferences prefsNode, Element xmlNode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        NodeList xmlKids = xmlNode.getChildNodes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        int numXmlKids = xmlKids.getLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
         * We first lock the node, import its contents and get
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
         * child nodes. Then we unlock the node and go to children
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
         * Since some of the children might have been concurrently
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
         * deleted we check for this.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        Preferences[] prefsKids;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        /* Lock the node */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        synchronized (((AbstractPreferences)prefsNode).lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            //If removed, return silently
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            if (((AbstractPreferences)prefsNode).isRemoved())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            // Import any preferences at this node
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            Element firstXmlKid = (Element) xmlKids.item(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            ImportPrefs(prefsNode, firstXmlKid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            prefsKids = new Preferences[numXmlKids - 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            // Get involved children
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            for (int i=1; i < numXmlKids; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                Element xmlKid = (Element) xmlKids.item(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                prefsKids[i-1] = prefsNode.node(xmlKid.getAttribute("name"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        } // unlocked the node
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        // import children
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        for (int i=1; i < numXmlKids; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            ImportSubtree(prefsKids[i-1], (Element)xmlKids.item(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * Import the preferences described by the specified XML element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * (a map from a preferences document) into the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * preferences node.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    private static void ImportPrefs(Preferences prefsNode, Element map) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        NodeList entries = map.getChildNodes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        for (int i=0, numEntries = entries.getLength(); i < numEntries; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            Element entry = (Element) entries.item(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            prefsNode.put(entry.getAttribute("key"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                          entry.getAttribute("value"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * Export the specified Map<String,String> to a map document on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * the specified OutputStream as per the prefs DTD.  This is used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * as the internal (undocumented) format for FileSystemPrefs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * @throws IOException if writing to the specified output stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     *         results in an <tt>IOException</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    static void exportMap(OutputStream os, Map map) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        Document doc = createPrefsDoc("map");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        Element xmlMap = doc.getDocumentElement( ) ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        xmlMap.setAttribute("MAP_XML_VERSION", MAP_XML_VERSION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        for (Iterator i = map.entrySet().iterator(); i.hasNext(); ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            Map.Entry e = (Map.Entry) i.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            Element xe = (Element)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                xmlMap.appendChild(doc.createElement("entry"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            xe.setAttribute("key",   (String) e.getKey());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
            xe.setAttribute("value", (String) e.getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        writeDoc(doc, os);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * Import Map from the specified input stream, which is assumed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * to contain a map document as per the prefs DTD.  This is used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * as the internal (undocumented) format for FileSystemPrefs.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     * key-value pairs specified in the XML document will be put into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * the specified Map.  (If this Map is empty, it will contain exactly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * the key-value pairs int the XML-document when this method returns.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * @throws IOException if reading from the specified output stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     *         results in an <tt>IOException</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * @throws InvalidPreferencesFormatException Data on input stream does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     *         constitute a valid XML document with the mandated document type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    static void importMap(InputStream is, Map m)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        throws IOException, InvalidPreferencesFormatException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            Document doc = loadPrefsDoc(is);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            Element xmlMap = doc.getDocumentElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            // check version
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            String mapVersion = xmlMap.getAttribute("MAP_XML_VERSION");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            if (mapVersion.compareTo(MAP_XML_VERSION) > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                throw new InvalidPreferencesFormatException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                "Preferences map file format version " + mapVersion +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                " is not supported. This java installation can read" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                " versions " + MAP_XML_VERSION + " or older. You may need" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                " to install a newer version of JDK.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            NodeList entries = xmlMap.getChildNodes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
            for (int i=0, numEntries=entries.getLength(); i<numEntries; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                Element entry = (Element) entries.item(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                m.put(entry.getAttribute("key"), entry.getAttribute("value"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        } catch(SAXException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
            throw new InvalidPreferencesFormatException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
    private static class Resolver implements EntityResolver {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        public InputSource resolveEntity(String pid, String sid)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
            throws SAXException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
            if (sid.equals(PREFS_DTD_URI)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                InputSource is;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                is = new InputSource(new StringReader(PREFS_DTD));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                is.setSystemId(PREFS_DTD_URI);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                return is;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
            throw new SAXException("Invalid system identifier: " + sid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
    private static class EH implements ErrorHandler {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        public void error(SAXParseException x) throws SAXException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
            throw x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        public void fatalError(SAXParseException x) throws SAXException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
            throw x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        public void warning(SAXParseException x) throws SAXException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
            throw x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
}