89 *
90 * @throws IOException if writing to the specified output stream
91 * results in an <tt>IOException</tt>.
92 * @throws BackingStoreException if preference data cannot be read from
93 * backing store.
94 * @throws IllegalStateException if this node (or an ancestor) has been
95 * removed with the {@link #removeNode()} method.
96 */
97 static void export(OutputStream os, final Preferences p, boolean subTree)
98 throws IOException, BackingStoreException {
99 if (((AbstractPreferences)p).isRemoved())
100 throw new IllegalStateException("Node has been removed");
101 Document doc = createPrefsDoc("preferences");
102 Element preferences = doc.getDocumentElement() ;
103 preferences.setAttribute("EXTERNAL_XML_VERSION", EXTERNAL_XML_VERSION);
104 Element xmlRoot = (Element)
105 preferences.appendChild(doc.createElement("root"));
106 xmlRoot.setAttribute("type", (p.isUserNode() ? "user" : "system"));
107
108 // Get bottom-up list of nodes from p to root, excluding root
109 List ancestors = new ArrayList();
110
111 for (Preferences kid = p, dad = kid.parent(); dad != null;
112 kid = dad, dad = kid.parent()) {
113 ancestors.add(kid);
114 }
115 Element e = xmlRoot;
116 for (int i=ancestors.size()-1; i >= 0; i--) {
117 e.appendChild(doc.createElement("map"));
118 e = (Element) e.appendChild(doc.createElement("node"));
119 e.setAttribute("name", ((Preferences)ancestors.get(i)).name());
120 }
121 putPreferencesInXml(e, doc, p, subTree);
122
123 writeDoc(doc, os);
124 }
125
126 /**
127 * Put the preferences in the specified Preferences node into the
128 * specified XML element which is assumed to represent a node
129 * in the specified XML document which is assumed to conform to
130 * PREFS_DTD. If subTree is true, create children of the specified
131 * XML node conforming to all of the children of the specified
132 * Preferences node and recurse.
133 *
134 * @throws BackingStoreException if it is not possible to read
135 * the preferences or children out of the specified
136 * preferences node.
137 */
138 private static void putPreferencesInXml(Element elt, Document doc,
139 Preferences prefs, boolean subTree) throws BackingStoreException
322 * (a map from a preferences document) into the specified
323 * preferences node.
324 */
325 private static void ImportPrefs(Preferences prefsNode, Element map) {
326 NodeList entries = map.getChildNodes();
327 for (int i=0, numEntries = entries.getLength(); i < numEntries; i++) {
328 Element entry = (Element) entries.item(i);
329 prefsNode.put(entry.getAttribute("key"),
330 entry.getAttribute("value"));
331 }
332 }
333
334 /**
335 * Export the specified Map<String,String> to a map document on
336 * the specified OutputStream as per the prefs DTD. This is used
337 * as the internal (undocumented) format for FileSystemPrefs.
338 *
339 * @throws IOException if writing to the specified output stream
340 * results in an <tt>IOException</tt>.
341 */
342 static void exportMap(OutputStream os, Map map) throws IOException {
343 Document doc = createPrefsDoc("map");
344 Element xmlMap = doc.getDocumentElement( ) ;
345 xmlMap.setAttribute("MAP_XML_VERSION", MAP_XML_VERSION);
346
347 for (Iterator i = map.entrySet().iterator(); i.hasNext(); ) {
348 Map.Entry e = (Map.Entry) i.next();
349 Element xe = (Element)
350 xmlMap.appendChild(doc.createElement("entry"));
351 xe.setAttribute("key", (String) e.getKey());
352 xe.setAttribute("value", (String) e.getValue());
353 }
354
355 writeDoc(doc, os);
356 }
357
358 /**
359 * Import Map from the specified input stream, which is assumed
360 * to contain a map document as per the prefs DTD. This is used
361 * as the internal (undocumented) format for FileSystemPrefs. The
362 * key-value pairs specified in the XML document will be put into
363 * the specified Map. (If this Map is empty, it will contain exactly
364 * the key-value pairs int the XML-document when this method returns.)
365 *
366 * @throws IOException if reading from the specified output stream
367 * results in an <tt>IOException</tt>.
368 * @throws InvalidPreferencesFormatException Data on input stream does not
369 * constitute a valid XML document with the mandated document type.
370 */
371 static void importMap(InputStream is, Map m)
372 throws IOException, InvalidPreferencesFormatException
373 {
374 try {
375 Document doc = loadPrefsDoc(is);
376 Element xmlMap = doc.getDocumentElement();
377 // check version
378 String mapVersion = xmlMap.getAttribute("MAP_XML_VERSION");
379 if (mapVersion.compareTo(MAP_XML_VERSION) > 0)
380 throw new InvalidPreferencesFormatException(
381 "Preferences map file format version " + mapVersion +
382 " is not supported. This java installation can read" +
383 " versions " + MAP_XML_VERSION + " or older. You may need" +
384 " to install a newer version of JDK.");
385
386 NodeList entries = xmlMap.getChildNodes();
387 for (int i=0, numEntries=entries.getLength(); i<numEntries; i++) {
388 Element entry = (Element) entries.item(i);
389 m.put(entry.getAttribute("key"), entry.getAttribute("value"));
390 }
391 } catch(SAXException e) {
|
89 *
90 * @throws IOException if writing to the specified output stream
91 * results in an <tt>IOException</tt>.
92 * @throws BackingStoreException if preference data cannot be read from
93 * backing store.
94 * @throws IllegalStateException if this node (or an ancestor) has been
95 * removed with the {@link #removeNode()} method.
96 */
97 static void export(OutputStream os, final Preferences p, boolean subTree)
98 throws IOException, BackingStoreException {
99 if (((AbstractPreferences)p).isRemoved())
100 throw new IllegalStateException("Node has been removed");
101 Document doc = createPrefsDoc("preferences");
102 Element preferences = doc.getDocumentElement() ;
103 preferences.setAttribute("EXTERNAL_XML_VERSION", EXTERNAL_XML_VERSION);
104 Element xmlRoot = (Element)
105 preferences.appendChild(doc.createElement("root"));
106 xmlRoot.setAttribute("type", (p.isUserNode() ? "user" : "system"));
107
108 // Get bottom-up list of nodes from p to root, excluding root
109 List<Preferences> ancestors = new ArrayList<>();
110
111 for (Preferences kid = p, dad = kid.parent(); dad != null;
112 kid = dad, dad = kid.parent()) {
113 ancestors.add(kid);
114 }
115 Element e = xmlRoot;
116 for (int i=ancestors.size()-1; i >= 0; i--) {
117 e.appendChild(doc.createElement("map"));
118 e = (Element) e.appendChild(doc.createElement("node"));
119 e.setAttribute("name", ancestors.get(i).name());
120 }
121 putPreferencesInXml(e, doc, p, subTree);
122
123 writeDoc(doc, os);
124 }
125
126 /**
127 * Put the preferences in the specified Preferences node into the
128 * specified XML element which is assumed to represent a node
129 * in the specified XML document which is assumed to conform to
130 * PREFS_DTD. If subTree is true, create children of the specified
131 * XML node conforming to all of the children of the specified
132 * Preferences node and recurse.
133 *
134 * @throws BackingStoreException if it is not possible to read
135 * the preferences or children out of the specified
136 * preferences node.
137 */
138 private static void putPreferencesInXml(Element elt, Document doc,
139 Preferences prefs, boolean subTree) throws BackingStoreException
322 * (a map from a preferences document) into the specified
323 * preferences node.
324 */
325 private static void ImportPrefs(Preferences prefsNode, Element map) {
326 NodeList entries = map.getChildNodes();
327 for (int i=0, numEntries = entries.getLength(); i < numEntries; i++) {
328 Element entry = (Element) entries.item(i);
329 prefsNode.put(entry.getAttribute("key"),
330 entry.getAttribute("value"));
331 }
332 }
333
334 /**
335 * Export the specified Map<String,String> to a map document on
336 * the specified OutputStream as per the prefs DTD. This is used
337 * as the internal (undocumented) format for FileSystemPrefs.
338 *
339 * @throws IOException if writing to the specified output stream
340 * results in an <tt>IOException</tt>.
341 */
342 static void exportMap(OutputStream os, Map<String, String> map) throws IOException {
343 Document doc = createPrefsDoc("map");
344 Element xmlMap = doc.getDocumentElement( ) ;
345 xmlMap.setAttribute("MAP_XML_VERSION", MAP_XML_VERSION);
346
347 for (Iterator<Map.Entry<String, String>> i = map.entrySet().iterator(); i.hasNext(); ) {
348 Map.Entry<String, String> e = i.next();
349 Element xe = (Element)
350 xmlMap.appendChild(doc.createElement("entry"));
351 xe.setAttribute("key", e.getKey());
352 xe.setAttribute("value", e.getValue());
353 }
354
355 writeDoc(doc, os);
356 }
357
358 /**
359 * Import Map from the specified input stream, which is assumed
360 * to contain a map document as per the prefs DTD. This is used
361 * as the internal (undocumented) format for FileSystemPrefs. The
362 * key-value pairs specified in the XML document will be put into
363 * the specified Map. (If this Map is empty, it will contain exactly
364 * the key-value pairs int the XML-document when this method returns.)
365 *
366 * @throws IOException if reading from the specified output stream
367 * results in an <tt>IOException</tt>.
368 * @throws InvalidPreferencesFormatException Data on input stream does not
369 * constitute a valid XML document with the mandated document type.
370 */
371 static void importMap(InputStream is, Map<String, String> m)
372 throws IOException, InvalidPreferencesFormatException
373 {
374 try {
375 Document doc = loadPrefsDoc(is);
376 Element xmlMap = doc.getDocumentElement();
377 // check version
378 String mapVersion = xmlMap.getAttribute("MAP_XML_VERSION");
379 if (mapVersion.compareTo(MAP_XML_VERSION) > 0)
380 throw new InvalidPreferencesFormatException(
381 "Preferences map file format version " + mapVersion +
382 " is not supported. This java installation can read" +
383 " versions " + MAP_XML_VERSION + " or older. You may need" +
384 " to install a newer version of JDK.");
385
386 NodeList entries = xmlMap.getChildNodes();
387 for (int i=0, numEntries=entries.getLength(); i<numEntries; i++) {
388 Element entry = (Element) entries.item(i);
389 m.put(entry.getAttribute("key"), entry.getAttribute("value"));
390 }
391 } catch(SAXException e) {
|