Reading of exported preferences yields the same results if done via DOM as well as nanoXML parser
1.1 --- a/src/share/classes/sun/util/xml/DefaultPrefsXmlSupport.java Wed Jun 24 17:29:29 2009 +0200
1.2 +++ b/src/share/classes/sun/util/xml/DefaultPrefsXmlSupport.java Wed Jun 24 17:50:25 2009 +0200
1.3 @@ -136,28 +136,25 @@
1.4 public void importPreferences(InputStream is)
1.5 throws IOException, InvalidPreferencesFormatException
1.6 {
1.7 - /*
1.8 try {
1.9 - Document doc = loadPrefsDoc(is);
1.10 - String xmlVersion =
1.11 - doc.getDocumentElement().getAttribute("EXTERNAL_XML_VERSION");
1.12 - if (xmlVersion.compareTo(EXTERNAL_XML_VERSION) > 0)
1.13 + XMLElement doc = new XMLElement();
1.14 + doc.parseFromReader(new InputStreamReader(is, "UTF-8"));
1.15 + String xmlVersion = (String)doc.getAttribute("EXTERNAL_XML_VERSION");
1.16 + if (xmlVersion.compareTo("1.0") > 0)
1.17 throw new InvalidPreferencesFormatException(
1.18 "Exported preferences file format version " + xmlVersion +
1.19 " is not supported. This java installation can read" +
1.20 - " versions " + EXTERNAL_XML_VERSION + " or older. You may need" +
1.21 + " versions " + "1.0" + " or older. You may need" +
1.22 " to install a newer version of JDK.");
1.23
1.24 - Element xmlRoot = (Element) doc.getDocumentElement().
1.25 - getChildNodes().item(0);
1.26 + XMLElement xmlRoot = (XMLElement) doc.getChildren().get(0);
1.27 Preferences prefsRoot =
1.28 (xmlRoot.getAttribute("type").equals("user") ?
1.29 Preferences.userRoot() : Preferences.systemRoot());
1.30 ImportSubtree(prefsRoot, xmlRoot);
1.31 - } catch(SAXException e) {
1.32 + } catch(XMLParseException e) {
1.33 throw new InvalidPreferencesFormatException(e);
1.34 }
1.35 - */
1.36 }
1.37
1.38 /**
1.39 @@ -174,27 +171,6 @@
1.40 }
1.41 }
1.42
1.43 - /**
1.44 - * Load an XML document from specified input stream, which must
1.45 - * have the requisite DTD URI.
1.46 - *
1.47 - private static Document loadPrefsDoc(InputStream in)
1.48 - throws SAXException, IOException
1.49 - {
1.50 - DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
1.51 - dbf.setIgnoringElementContentWhitespace(true);
1.52 - dbf.setValidating(true);
1.53 - dbf.setCoalescing(true);
1.54 - dbf.setIgnoringComments(true);
1.55 - try {
1.56 - DocumentBuilder db = dbf.newDocumentBuilder();
1.57 - db.setEntityResolver(new Resolver());
1.58 - db.setErrorHandler(new EH());
1.59 - return db.parse(new InputSource(in));
1.60 - } catch (ParserConfigurationException e) {
1.61 - throw new AssertionError(e);
1.62 - }
1.63 - }
1.64
1.65 /**
1.66 * Write XML document to the specified output stream.
1.67 @@ -227,50 +203,50 @@
1.68 * Recursively traverse the specified preferences node and store
1.69 * the described preferences into the system or current user
1.70 * preferences tree, as appropriate.
1.71 - *
1.72 - private static void ImportSubtree(Preferences prefsNode, Element xmlNode) {
1.73 - NodeList xmlKids = xmlNode.getChildNodes();
1.74 - int numXmlKids = xmlKids.getLength();
1.75 + */
1.76 + private static void ImportSubtree(Preferences prefsNode, XMLElement xmlNode) {
1.77 + Vector xmlKids = xmlNode.getChildren();
1.78 + int numXmlKids = xmlKids.size();
1.79 /*
1.80 * We first lock the node, import its contents and get
1.81 * child nodes. Then we unlock the node and go to children
1.82 * Since some of the children might have been concurrently
1.83 * deleted we check for this.
1.84 - * /
1.85 + */
1.86 Preferences[] prefsKids;
1.87 - /* Lock the node * /
1.88 + /* Lock the node */
1.89 synchronized (lock(prefsNode)) {
1.90 //If removed, return silently
1.91 if (isRemoved(prefsNode))
1.92 return;
1.93
1.94 // Import any preferences at this node
1.95 - Element firstXmlKid = (Element) xmlKids.item(0);
1.96 + XMLElement firstXmlKid = (XMLElement) xmlKids.get(0);
1.97 ImportPrefs(prefsNode, firstXmlKid);
1.98 prefsKids = new Preferences[numXmlKids - 1];
1.99
1.100 // Get involved children
1.101 for (int i=1; i < numXmlKids; i++) {
1.102 - Element xmlKid = (Element) xmlKids.item(i);
1.103 - prefsKids[i-1] = prefsNode.node(xmlKid.getAttribute("name"));
1.104 + XMLElement xmlKid = (XMLElement) xmlKids.get(i);
1.105 + prefsKids[i-1] = prefsNode.node((String)xmlKid.getAttribute("name"));
1.106 }
1.107 } // unlocked the node
1.108 // import children
1.109 for (int i=1; i < numXmlKids; i++)
1.110 - ImportSubtree(prefsKids[i-1], (Element)xmlKids.item(i));
1.111 + ImportSubtree(prefsKids[i-1], (XMLElement)xmlKids.get(i));
1.112 }
1.113
1.114 /**
1.115 * Import the preferences described by the specified XML element
1.116 * (a map from a preferences document) into the specified
1.117 * preferences node.
1.118 - * /
1.119 - private static void ImportPrefs(Preferences prefsNode, Element map) {
1.120 - NodeList entries = map.getChildNodes();
1.121 - for (int i=0, numEntries = entries.getLength(); i < numEntries; i++) {
1.122 - Element entry = (Element) entries.item(i);
1.123 - prefsNode.put(entry.getAttribute("key"),
1.124 - entry.getAttribute("value"));
1.125 + */
1.126 + private static void ImportPrefs(Preferences prefsNode, XMLElement map) {
1.127 + Vector entries = map.getChildren();
1.128 + for (int i=0, numEntries = entries.size(); i < numEntries; i++) {
1.129 + XMLElement entry = (XMLElement) entries.get(i);
1.130 + prefsNode.put((String)entry.getAttribute("key"),
1.131 + (String)entry.getAttribute("value"));
1.132 }
1.133 }
1.134
2.1 --- a/test/java/util/prefs/Preferences/XMLPreferencesTest.java Wed Jun 24 17:29:29 2009 +0200
2.2 +++ b/test/java/util/prefs/Preferences/XMLPreferencesTest.java Wed Jun 24 17:50:25 2009 +0200
2.3 @@ -28,6 +28,7 @@
2.4 import sun.util.xml.PrefsXmlSupport;
2.5
2.6 import java.io.ByteArrayOutputStream;
2.7 +import java.util.Arrays;
2.8 import java.util.prefs.Preferences;
2.9
2.10 /** Checks whether reading and writing via standard DOM and simplified API
2.11 @@ -74,24 +75,54 @@
2.12 }
2.13
2.14 public void testCompareInput() throws Exception {
2.15 - /*
2.16 - String text = "";
2.17 + Preferences.userRoot().node("test1/b/c").removeNode();
2.18 + Preferences.userRoot().node("test2/b/c").removeNode();
2.19
2.20 - Properties p1 = new Properties();
2.21 + String text = "<?xml version='1.0' encoding='UTF-8' standalone='no'?>" +
2.22 + "<!DOCTYPE preferences SYSTEM 'http://java.sun.com/dtd/preferences.dtd'>" +
2.23 + " <preferences EXTERNAL_XML_VERSION='1.0'>" +
2.24 + " <root type='user'>\n\n\n" +
2.25 + "<map/><node name='test1'><map/><node name='b'>" +
2.26 + "<map/><node name='c'><map>" +
2.27 + " <entry key='ahoj' value='simple'/>" +
2.28 + " <entry key='kuk' value='1'/>" +
2.29 + " <entry key='multi' value='true'/> \n" +
2.30 + "</map>" +
2.31 + "<node name='child'>" +
2.32 + "<map>" +
2.33 + "<entry key='data' value='false'/>" +
2.34 + "</map>" +
2.35 + "</node><node name='empty'><map/>" +
2.36 + "</node>" +
2.37 + "</node>" +
2.38 + "</node>" +
2.39 + "</node>" +
2.40 + "</root>" +
2.41 + "</preferences>";
2.42 +
2.43 {
2.44 ByteArrayInputStream is = new ByteArrayInputStream(text.getBytes("UTF-8"));
2.45 - FULL.load(p1, is);
2.46 + FULL.importPreferences(is);
2.47 }
2.48
2.49 - Properties p2 = new Properties();
2.50 {
2.51 + text = text.replace("'test1'", "'test2'");
2.52 ByteArrayInputStream is = new ByteArrayInputStream(text.getBytes("UTF-8"));
2.53 - SIMPLE.load(p2, is);
2.54 + SIMPLE.importPreferences(is);
2.55 }
2.56
2.57 - assert p1.equals(p2) : "P1: " + p1 + "\nP2: " + p2;
2.58 + assert Preferences.userRoot().nodeExists("test1/b/c") : "Node in user imported";
2.59 + assert Preferences.userRoot().nodeExists("test2/b/c") : "Node in system imported";
2.60 +
2.61 + Preferences u = Preferences.userRoot().node("test1/b/c");
2.62 + Preferences s = Preferences.userRoot().node("test2/b/c");
2.63 +
2.64 + assert Arrays.equals(u.keys(), s.keys()) : "Same keys in both nodes";
2.65 +
2.66 + for (String k : u.keys()) {
2.67 + assert u.get(k, "Neco").equals(s.get(k, "Cone")) : "Same values for " + k;
2.68 + }
2.69 System.err.println("OK: testCompareInput");
2.70 - */
2.71 }
2.72
2.73 }
2.74 \ No newline at end of file