XmlSupport.java revision 2362
0N/A * Copyright (c) 2002, 2006, Oracle and/or its affiliates. All rights reserved. 0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 0N/A * This code is free software; you can redistribute it and/or modify it 0N/A * under the terms of the GNU General Public License version 2 only, as 0N/A * published by the Free Software Foundation. Oracle designates this 4872N/A * particular file as subject to the "Classpath" exception as provided 0N/A * by Oracle in the LICENSE file that accompanied this code. 0N/A * This code is distributed in the hope that it will be useful, but WITHOUT 0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 0N/A * version 2 for more details (a copy is included in the LICENSE file that 0N/A * accompanied this code). * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * XML Support for java.util.prefs. Methods to import and export preference * @author Josh Bloch and Mark Reinhold // The required DTD URI for exported preferences // The actual DTD corresponding to the URI "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
"<!-- DTD for preferences -->" +
"<!ELEMENT preferences (root) >" +
"<!ATTLIST preferences" +
" EXTERNAL_XML_VERSION CDATA \"0.0\" >" +
"<!ELEMENT root (map, node*) >" +
" type (system|user) #REQUIRED >" +
"<!ELEMENT node (map, node*) >" +
" name CDATA #REQUIRED >" +
"<!ELEMENT map (entry*) >" +
" MAP_XML_VERSION CDATA \"0.0\" >" +
"<!ELEMENT entry EMPTY >" +
" value CDATA #REQUIRED >" ;
* Version number for the format exported preferences files. * Version number for the internal map files. * Export the specified preferences node and, if subTree is true, all * subnodes, to the specified output stream. Preferences are exported as * an XML document conforming to the definition in the Preferences spec. * @throws IOException if writing to the specified output stream * results in an <tt>IOException</tt>. * @throws BackingStoreException if preference data cannot be read from * @throws IllegalStateException if this node (or an ancestor) has been * removed with the {@link #removeNode()} method. // Get bottom-up list of nodes from p to root, excluding root * Put the preferences in the specified Preferences node into the * specified XML element which is assumed to represent a node * in the specified XML document which is assumed to conform to * PREFS_DTD. If subTree is true, create children of the specified * XML node conforming to all of the children of the specified * Preferences node and recurse. * @throws BackingStoreException if it is not possible to read * the preferences or children out of the specified // Node is locked to export its contents and get a // copy of children, then lock is released, // and, if subTree = true, recursive calls are made on children // Check if this node was concurrently removed. If yes // remove it from XML Document and return. // Put map in xml element // NEXT STATEMENT THROWS NULL PTR EXC INSTEAD OF ASSERT FAIL // Recurse if appropriate /* Get a copy of kids while lock is held */ * Import preferences from the specified input stream, which is assumed * to contain an XML document in the format described in the Preferences * @throws IOException if reading from the specified output stream * results in an <tt>IOException</tt>. * @throws InvalidPreferencesFormatException Data on input stream does not * constitute a valid XML document with the mandated document type. "Exported preferences file format version " +
xmlVersion +
" is not supported. This java installation can read" +
" to install a newer version of JDK.");
* Create a new prefs XML document. * Load an XML document from specified input stream, which must * have the requisite DTD URI. * Write XML document to the specified output stream. //Ignore the IAE. Should not fail the writeout even the //transformer provider does not support "indent-number". //Transformer resets the "indent" info if the "result" is a StreamResult with //an OutputStream object embedded, creating a Writer object on top of that //OutputStream object however works. * Recursively traverse the specified preferences node and store * the described preferences into the system or current user * preferences tree, as appropriate. * We first lock the node, import its contents and get * child nodes. Then we unlock the node and go to children * Since some of the children might have been concurrently * deleted we check for this. //If removed, return silently // Import any preferences at this node * Import the preferences described by the specified XML element * (a map from a preferences document) into the specified * Export the specified Map<String,String> to a map document on * the specified OutputStream as per the prefs DTD. This is used * as the internal (undocumented) format for FileSystemPrefs. * @throws IOException if writing to the specified output stream * results in an <tt>IOException</tt>. * Import Map from the specified input stream, which is assumed * to contain a map document as per the prefs DTD. This is used * as the internal (undocumented) format for FileSystemPrefs. The * key-value pairs specified in the XML document will be put into * the specified Map. (If this Map is empty, it will contain exactly * the key-value pairs int the XML-document when this method returns.) * @throws IOException if reading from the specified output stream * results in an <tt>IOException</tt>. * @throws InvalidPreferencesFormatException Data on input stream does not * constitute a valid XML document with the mandated document type. "Preferences map file format version " +
mapVersion +
" is not supported. This java installation can read" +
" to install a newer version of JDK.");