# HG changeset patch # User Jaroslav Tulach # Date 1245858625 -7200 # Node ID 601d21ee9aa6082e925f479464b514dad8ea2d75 # Parent 24b6c30fbf71a1743ab78504415a1c013985558f Reading of exported preferences yields the same results if done via DOM as well as nanoXML parser diff -r 24b6c30fbf71 -r 601d21ee9aa6 src/share/classes/sun/util/xml/DefaultPrefsXmlSupport.java --- a/src/share/classes/sun/util/xml/DefaultPrefsXmlSupport.java Wed Jun 24 17:29:29 2009 +0200 +++ b/src/share/classes/sun/util/xml/DefaultPrefsXmlSupport.java Wed Jun 24 17:50:25 2009 +0200 @@ -136,28 +136,25 @@ public void importPreferences(InputStream is) throws IOException, InvalidPreferencesFormatException { - /* try { - Document doc = loadPrefsDoc(is); - String xmlVersion = - doc.getDocumentElement().getAttribute("EXTERNAL_XML_VERSION"); - if (xmlVersion.compareTo(EXTERNAL_XML_VERSION) > 0) + XMLElement doc = new XMLElement(); + doc.parseFromReader(new InputStreamReader(is, "UTF-8")); + String xmlVersion = (String)doc.getAttribute("EXTERNAL_XML_VERSION"); + if (xmlVersion.compareTo("1.0") > 0) throw new InvalidPreferencesFormatException( "Exported preferences file format version " + xmlVersion + " is not supported. This java installation can read" + - " versions " + EXTERNAL_XML_VERSION + " or older. You may need" + + " versions " + "1.0" + " or older. You may need" + " to install a newer version of JDK."); - Element xmlRoot = (Element) doc.getDocumentElement(). - getChildNodes().item(0); + XMLElement xmlRoot = (XMLElement) doc.getChildren().get(0); Preferences prefsRoot = (xmlRoot.getAttribute("type").equals("user") ? Preferences.userRoot() : Preferences.systemRoot()); ImportSubtree(prefsRoot, xmlRoot); - } catch(SAXException e) { + } catch(XMLParseException e) { throw new InvalidPreferencesFormatException(e); } - */ } /** @@ -174,27 +171,6 @@ } } - /** - * Load an XML document from specified input stream, which must - * have the requisite DTD URI. - * - private static Document loadPrefsDoc(InputStream in) - throws SAXException, IOException - { - DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); - dbf.setIgnoringElementContentWhitespace(true); - dbf.setValidating(true); - dbf.setCoalescing(true); - dbf.setIgnoringComments(true); - try { - DocumentBuilder db = dbf.newDocumentBuilder(); - db.setEntityResolver(new Resolver()); - db.setErrorHandler(new EH()); - return db.parse(new InputSource(in)); - } catch (ParserConfigurationException e) { - throw new AssertionError(e); - } - } /** * Write XML document to the specified output stream. @@ -227,50 +203,50 @@ * Recursively traverse the specified preferences node and store * the described preferences into the system or current user * preferences tree, as appropriate. - * - private static void ImportSubtree(Preferences prefsNode, Element xmlNode) { - NodeList xmlKids = xmlNode.getChildNodes(); - int numXmlKids = xmlKids.getLength(); + */ + private static void ImportSubtree(Preferences prefsNode, XMLElement xmlNode) { + Vector xmlKids = xmlNode.getChildren(); + int numXmlKids = xmlKids.size(); /* * 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. - * / + */ Preferences[] prefsKids; - /* Lock the node * / + /* Lock the node */ synchronized (lock(prefsNode)) { //If removed, return silently if (isRemoved(prefsNode)) return; // Import any preferences at this node - Element firstXmlKid = (Element) xmlKids.item(0); + XMLElement firstXmlKid = (XMLElement) xmlKids.get(0); ImportPrefs(prefsNode, firstXmlKid); prefsKids = new Preferences[numXmlKids - 1]; // Get involved children for (int i=1; i < numXmlKids; i++) { - Element xmlKid = (Element) xmlKids.item(i); - prefsKids[i-1] = prefsNode.node(xmlKid.getAttribute("name")); + XMLElement xmlKid = (XMLElement) xmlKids.get(i); + prefsKids[i-1] = prefsNode.node((String)xmlKid.getAttribute("name")); } } // unlocked the node // import children for (int i=1; i < numXmlKids; i++) - ImportSubtree(prefsKids[i-1], (Element)xmlKids.item(i)); + ImportSubtree(prefsKids[i-1], (XMLElement)xmlKids.get(i)); } /** * Import the preferences described by the specified XML element * (a map from a preferences document) into the specified * preferences node. - * / - private static void ImportPrefs(Preferences prefsNode, Element map) { - NodeList entries = map.getChildNodes(); - for (int i=0, numEntries = entries.getLength(); i < numEntries; i++) { - Element entry = (Element) entries.item(i); - prefsNode.put(entry.getAttribute("key"), - entry.getAttribute("value")); + */ + private static void ImportPrefs(Preferences prefsNode, XMLElement map) { + Vector entries = map.getChildren(); + for (int i=0, numEntries = entries.size(); i < numEntries; i++) { + XMLElement entry = (XMLElement) entries.get(i); + prefsNode.put((String)entry.getAttribute("key"), + (String)entry.getAttribute("value")); } } diff -r 24b6c30fbf71 -r 601d21ee9aa6 test/java/util/prefs/Preferences/XMLPreferencesTest.java --- a/test/java/util/prefs/Preferences/XMLPreferencesTest.java Wed Jun 24 17:29:29 2009 +0200 +++ b/test/java/util/prefs/Preferences/XMLPreferencesTest.java Wed Jun 24 17:50:25 2009 +0200 @@ -28,6 +28,7 @@ import sun.util.xml.PrefsXmlSupport; import java.io.ByteArrayOutputStream; +import java.util.Arrays; import java.util.prefs.Preferences; /** Checks whether reading and writing via standard DOM and simplified API @@ -74,24 +75,54 @@ } public void testCompareInput() throws Exception { - /* - String text = ""; + Preferences.userRoot().node("test1/b/c").removeNode(); + Preferences.userRoot().node("test2/b/c").removeNode(); - Properties p1 = new Properties(); + String text = "" + + "" + + " " + + " \n\n\n" + + "" + + "" + + " " + + " " + + " \n" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + ""; + { ByteArrayInputStream is = new ByteArrayInputStream(text.getBytes("UTF-8")); - FULL.load(p1, is); + FULL.importPreferences(is); } - Properties p2 = new Properties(); { + text = text.replace("'test1'", "'test2'"); ByteArrayInputStream is = new ByteArrayInputStream(text.getBytes("UTF-8")); - SIMPLE.load(p2, is); + SIMPLE.importPreferences(is); } - assert p1.equals(p2) : "P1: " + p1 + "\nP2: " + p2; + assert Preferences.userRoot().nodeExists("test1/b/c") : "Node in user imported"; + assert Preferences.userRoot().nodeExists("test2/b/c") : "Node in system imported"; + + Preferences u = Preferences.userRoot().node("test1/b/c"); + Preferences s = Preferences.userRoot().node("test2/b/c"); + + assert Arrays.equals(u.keys(), s.keys()) : "Same keys in both nodes"; + + for (String k : u.keys()) { + assert u.get(k, "Neco").equals(s.get(k, "Cone")) : "Same values for " + k; + } System.err.println("OK: testCompareInput"); - */ } } \ No newline at end of file