src/share/classes/sun/util/xml/DefaultPrefsXmlSupport.java
branchxml-sax-and-dom-2
changeset 1264 601d21ee9aa6
parent 1263 24b6c30fbf71
     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