1 /* 2 * Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package java.util.prefs; 27 28 import java.io.InputStream; 29 import java.io.IOException; 30 import java.io.OutputStream; 31 import java.security.AccessController; 32 import java.security.Permission; 33 import java.security.PrivilegedAction; 34 import java.util.Iterator; 35 import java.util.ServiceLoader; 36 import java.util.ServiceConfigurationError; 37 38 // These imports needed only as a workaround for a JavaDoc bug 39 import java.lang.RuntimePermission; 40 import java.lang.Integer; 41 import java.lang.Long; 42 import java.lang.Float; 43 import java.lang.Double; 44 45 /** 46 * A node in a hierarchical collection of preference data. This class 47 * allows applications to store and retrieve user and system 48 * preference and configuration data. This data is stored 49 * persistently in an implementation-dependent backing store. Typical 50 * implementations include flat files, OS-specific registries, 51 * directory servers and SQL databases. The user of this class needn't 52 * be concerned with details of the backing store. 53 * 54 * <p>There are two separate trees of preference nodes, one for user 55 * preferences and one for system preferences. Each user has a separate user 56 * preference tree, and all users in a given system share the same system 57 * preference tree. The precise description of "user" and "system" will vary 58 * from implementation to implementation. Typical information stored in the 59 * user preference tree might include font choice, color choice, or preferred 60 * window location and size for a particular application. Typical information 61 * stored in the system preference tree might include installation 62 * configuration data for an application. 63 * 64 * <p>Nodes in a preference tree are named in a similar fashion to 65 * directories in a hierarchical file system. Every node in a preference 66 * tree has a <i>node name</i> (which is not necessarily unique), 67 * a unique <i>absolute path name</i>, and a path name <i>relative</i> to each 68 * ancestor including itself. 69 * 70 * <p>The root node has a node name of the empty string (""). Every other 71 * node has an arbitrary node name, specified at the time it is created. The 72 * only restrictions on this name are that it cannot be the empty string, and 73 * it cannot contain the slash character ('/'). 74 * 75 * <p>The root node has an absolute path name of <tt>"/"</tt>. Children of 76 * the root node have absolute path names of <tt>"/" + </tt><i><node 77 * name></i>. All other nodes have absolute path names of <i><parent's 78 * absolute path name></i><tt> + "/" + </tt><i><node name></i>. 79 * Note that all absolute path names begin with the slash character. 80 * 81 * <p>A node <i>n</i>'s path name relative to its ancestor <i>a</i> 82 * is simply the string that must be appended to <i>a</i>'s absolute path name 83 * in order to form <i>n</i>'s absolute path name, with the initial slash 84 * character (if present) removed. Note that: 85 * <ul> 86 * <li>No relative path names begin with the slash character. 87 * <li>Every node's path name relative to itself is the empty string. 88 * <li>Every node's path name relative to its parent is its node name (except 89 * for the root node, which does not have a parent). 90 * <li>Every node's path name relative to the root is its absolute path name 91 * with the initial slash character removed. 92 * </ul> 93 * 94 * <p>Note finally that: 95 * <ul> 96 * <li>No path name contains multiple consecutive slash characters. 97 * <li>No path name with the exception of the root's absolute path name 98 * ends in the slash character. 99 * <li>Any string that conforms to these two rules is a valid path name. 100 * </ul> 101 * 102 * <p>All of the methods that modify preferences data are permitted to operate 103 * asynchronously; they may return immediately, and changes will eventually 104 * propagate to the persistent backing store with an implementation-dependent 105 * delay. The <tt>flush</tt> method may be used to synchronously force 106 * updates to the backing store. Normal termination of the Java Virtual 107 * Machine will <i>not</i> result in the loss of pending updates -- an explicit 108 * <tt>flush</tt> invocation is <i>not</i> required upon termination to ensure 109 * that pending updates are made persistent. 110 * 111 * <p>All of the methods that read preferences from a <tt>Preferences</tt> 112 * object require the invoker to provide a default value. The default value is 113 * returned if no value has been previously set <i>or if the backing store is 114 * unavailable</i>. The intent is to allow applications to operate, albeit 115 * with slightly degraded functionality, even if the backing store becomes 116 * unavailable. Several methods, like <tt>flush</tt>, have semantics that 117 * prevent them from operating if the backing store is unavailable. Ordinary 118 * applications should have no need to invoke any of these methods, which can 119 * be identified by the fact that they are declared to throw {@link 120 * BackingStoreException}. 121 * 122 * <p>The methods in this class may be invoked concurrently by multiple threads 123 * in a single JVM without the need for external synchronization, and the 124 * results will be equivalent to some serial execution. If this class is used 125 * concurrently <i>by multiple JVMs</i> that store their preference data in 126 * the same backing store, the data store will not be corrupted, but no 127 * other guarantees are made concerning the consistency of the preference 128 * data. 129 * 130 * <p>This class contains an export/import facility, allowing preferences 131 * to be "exported" to an XML document, and XML documents representing 132 * preferences to be "imported" back into the system. This facility 133 * may be used to back up all or part of a preference tree, and 134 * subsequently restore from the backup. 135 * 136 * <p>The XML document has the following DOCTYPE declaration: 137 * <pre>{@code 138 * <!DOCTYPE preferences SYSTEM "http://java.sun.com/dtd/preferences.dtd"> 139 * }</pre> 140 * Note that the system URI (http://java.sun.com/dtd/preferences.dtd) is 141 * <i>not</i> accessed when exporting or importing preferences; it merely 142 * serves as a string to uniquely identify the DTD, which is: 143 * <pre>{@code 144 * <?xml version="1.0" encoding="UTF-8"?> 145 * 146 * <!-- DTD for a Preferences tree. --> 147 * 148 * <!-- The preferences element is at the root of an XML document 149 * representing a Preferences tree. --> 150 * <!ELEMENT preferences (root)> 151 * 152 * <!-- The preferences element contains an optional version attribute, 153 * which specifies version of DTD. --> 154 * <!ATTLIST preferences EXTERNAL_XML_VERSION CDATA "0.0" > 155 * 156 * <!-- The root element has a map representing the root's preferences 157 * (if any), and one node for each child of the root (if any). --> 158 * <!ELEMENT root (map, node*) > 159 * 160 * <!-- Additionally, the root contains a type attribute, which 161 * specifies whether it's the system or user root. --> 162 * <!ATTLIST root 163 * type (system|user) #REQUIRED > 164 * 165 * <!-- Each node has a map representing its preferences (if any), 166 * and one node for each child (if any). --> 167 * <!ELEMENT node (map, node*) > 168 * 169 * <!-- Additionally, each node has a name attribute --> 170 * <!ATTLIST node 171 * name CDATA #REQUIRED > 172 * 173 * <!-- A map represents the preferences stored at a node (if any). --> 174 * <!ELEMENT map (entry*) > 175 * 176 * <!-- An entry represents a single preference, which is simply 177 * a key-value pair. --> 178 * <!ELEMENT entry EMPTY > 179 * <!ATTLIST entry 180 * key CDATA #REQUIRED 181 * value CDATA #REQUIRED > 182 * }</pre> 183 * 184 * Every <tt>Preferences</tt> implementation must have an associated {@link 185 * PreferencesFactory} implementation. Every Java(TM) SE implementation must provide 186 * some means of specifying which <tt>PreferencesFactory</tt> implementation 187 * is used to generate the root preferences nodes. This allows the 188 * administrator to replace the default preferences implementation with an 189 * alternative implementation. 190 * 191 * <p>Implementation note: In Sun's JRE, the <tt>PreferencesFactory</tt> 192 * implementation is located as follows: 193 * 194 * <ol> 195 * 196 * <li><p>If the system property 197 * <tt>java.util.prefs.PreferencesFactory</tt> is defined, then it is 198 * taken to be the fully-qualified name of a class implementing the 199 * <tt>PreferencesFactory</tt> interface. The class is loaded and 200 * instantiated; if this process fails then an unspecified error is 201 * thrown.</p></li> 202 * 203 * <li><p> If a <tt>PreferencesFactory</tt> implementation class file 204 * has been installed in a jar file that is visible to the 205 * {@link java.lang.ClassLoader#getSystemClassLoader system class loader}, 206 * and that jar file contains a provider-configuration file named 207 * <tt>java.util.prefs.PreferencesFactory</tt> in the resource 208 * directory <tt>META-INF/services</tt>, then the first class name 209 * specified in that file is taken. If more than one such jar file is 210 * provided, the first one found will be used. The class is loaded 211 * and instantiated; if this process fails then an unspecified error 212 * is thrown. </p></li> 213 * 214 * <li><p>Finally, if neither the above-mentioned system property nor 215 * an extension jar file is provided, then the system-wide default 216 * <tt>PreferencesFactory</tt> implementation for the underlying 217 * platform is loaded and instantiated.</p></li> 218 * 219 * </ol> 220 * 221 * @author Josh Bloch 222 * @since 1.4 223 */ 224 public abstract class Preferences { 225 226 private static final PreferencesFactory factory = factory(); 227 228 private static PreferencesFactory factory() { 229 // 1. Try user-specified system property 230 String factoryName = AccessController.doPrivileged( 231 new PrivilegedAction<String>() { 232 public String run() { 233 return System.getProperty( 234 "java.util.prefs.PreferencesFactory");}}); 235 if (factoryName != null) { 236 // FIXME: This code should be run in a doPrivileged and 237 // not use the context classloader, to avoid being 238 // dependent on the invoking thread. 239 // Checking AllPermission also seems wrong. 240 try { 241 return (PreferencesFactory) 242 Class.forName(factoryName, false, 243 ClassLoader.getSystemClassLoader()) 244 .newInstance(); 245 } catch (Exception ex) { 246 try { 247 // workaround for javaws, plugin, 248 // load factory class using non-system classloader 249 SecurityManager sm = System.getSecurityManager(); 250 if (sm != null) { 251 sm.checkPermission(new java.security.AllPermission()); 252 } 253 return (PreferencesFactory) 254 Class.forName(factoryName, false, 255 Thread.currentThread() 256 .getContextClassLoader()) 257 .newInstance(); 258 } catch (Exception e) { 259 throw new InternalError( 260 "Can't instantiate Preferences factory " 261 + factoryName, e); 262 } 263 } 264 } 265 266 return AccessController.doPrivileged( 267 new PrivilegedAction<PreferencesFactory>() { 268 public PreferencesFactory run() { 269 return factory1();}}); 270 } 271 272 private static PreferencesFactory factory1() { 273 // 2. Try service provider interface 274 Iterator<PreferencesFactory> itr = ServiceLoader 275 .load(PreferencesFactory.class, ClassLoader.getSystemClassLoader()) 276 .iterator(); 277 278 // choose first provider instance 279 while (itr.hasNext()) { 280 try { 281 return itr.next(); 282 } catch (ServiceConfigurationError sce) { 283 if (sce.getCause() instanceof SecurityException) { 284 // Ignore the security exception, try the next provider 285 continue; 286 } 287 throw sce; 288 } 289 } 290 291 // 3. Use platform-specific system-wide default 292 String osName = System.getProperty("os.name"); 293 String platformFactory; 294 if (osName.startsWith("Windows")) { 295 platformFactory = "java.util.prefs.WindowsPreferencesFactory"; 296 } else if (osName.contains("OS X")) { 297 platformFactory = "java.util.prefs.MacOSXPreferencesFactory"; 298 } else { 299 platformFactory = "java.util.prefs.FileSystemPreferencesFactory"; 300 } 301 try { 302 return (PreferencesFactory) 303 Class.forName(platformFactory, false, 304 Preferences.class.getClassLoader()).newInstance(); 305 } catch (Exception e) { 306 throw new InternalError( 307 "Can't instantiate platform default Preferences factory " 308 + platformFactory, e); 309 } 310 } 311 312 /** 313 * Maximum length of string allowed as a key (80 characters). 314 */ 315 public static final int MAX_KEY_LENGTH = 80; 316 317 /** 318 * Maximum length of string allowed as a value (8192 characters). 319 */ 320 public static final int MAX_VALUE_LENGTH = 8*1024; 321 322 /** 323 * Maximum length of a node name (80 characters). 324 */ 325 public static final int MAX_NAME_LENGTH = 80; 326 327 /** 328 * Returns the preference node from the calling user's preference tree 329 * that is associated (by convention) with the specified class's package. 330 * The convention is as follows: the absolute path name of the node is the 331 * fully qualified package name, preceded by a slash (<tt>'/'</tt>), and 332 * with each period (<tt>'.'</tt>) replaced by a slash. For example the 333 * absolute path name of the node associated with the class 334 * <tt>com.acme.widget.Foo</tt> is <tt>/com/acme/widget</tt>. 335 * 336 * <p>This convention does not apply to the unnamed package, whose 337 * associated preference node is <tt><unnamed></tt>. This node 338 * is not intended for long term use, but for convenience in the early 339 * development of programs that do not yet belong to a package, and 340 * for "throwaway" programs. <i>Valuable data should not be stored 341 * at this node as it is shared by all programs that use it.</i> 342 * 343 * <p>A class <tt>Foo</tt> wishing to access preferences pertaining to its 344 * package can obtain a preference node as follows: <pre> 345 * static Preferences prefs = Preferences.userNodeForPackage(Foo.class); 346 * </pre> 347 * This idiom obviates the need for using a string to describe the 348 * preferences node and decreases the likelihood of a run-time failure. 349 * (If the class name is misspelled, it will typically result in a 350 * compile-time error.) 351 * 352 * <p>Invoking this method will result in the creation of the returned 353 * node and its ancestors if they do not already exist. If the returned 354 * node did not exist prior to this call, this node and any ancestors that 355 * were created by this call are not guaranteed to become permanent until 356 * the <tt>flush</tt> method is called on the returned node (or one of its 357 * ancestors or descendants). 358 * 359 * @param c the class for whose package a user preference node is desired. 360 * @return the user preference node associated with the package of which 361 * <tt>c</tt> is a member. 362 * @throws NullPointerException if <tt>c</tt> is <tt>null</tt>. 363 * @throws SecurityException if a security manager is present and 364 * it denies <tt>RuntimePermission("preferences")</tt>. 365 * @see RuntimePermission 366 */ 367 public static Preferences userNodeForPackage(Class<?> c) { 368 return userRoot().node(nodeName(c)); 369 } 370 371 /** 372 * Returns the preference node from the system preference tree that is 373 * associated (by convention) with the specified class's package. The 374 * convention is as follows: the absolute path name of the node is the 375 * fully qualified package name, preceded by a slash (<tt>'/'</tt>), and 376 * with each period (<tt>'.'</tt>) replaced by a slash. For example the 377 * absolute path name of the node associated with the class 378 * <tt>com.acme.widget.Foo</tt> is <tt>/com/acme/widget</tt>. 379 * 380 * <p>This convention does not apply to the unnamed package, whose 381 * associated preference node is <tt><unnamed></tt>. This node 382 * is not intended for long term use, but for convenience in the early 383 * development of programs that do not yet belong to a package, and 384 * for "throwaway" programs. <i>Valuable data should not be stored 385 * at this node as it is shared by all programs that use it.</i> 386 * 387 * <p>A class <tt>Foo</tt> wishing to access preferences pertaining to its 388 * package can obtain a preference node as follows: <pre> 389 * static Preferences prefs = Preferences.systemNodeForPackage(Foo.class); 390 * </pre> 391 * This idiom obviates the need for using a string to describe the 392 * preferences node and decreases the likelihood of a run-time failure. 393 * (If the class name is misspelled, it will typically result in a 394 * compile-time error.) 395 * 396 * <p>Invoking this method will result in the creation of the returned 397 * node and its ancestors if they do not already exist. If the returned 398 * node did not exist prior to this call, this node and any ancestors that 399 * were created by this call are not guaranteed to become permanent until 400 * the <tt>flush</tt> method is called on the returned node (or one of its 401 * ancestors or descendants). 402 * 403 * @param c the class for whose package a system preference node is desired. 404 * @return the system preference node associated with the package of which 405 * <tt>c</tt> is a member. 406 * @throws NullPointerException if <tt>c</tt> is <tt>null</tt>. 407 * @throws SecurityException if a security manager is present and 408 * it denies <tt>RuntimePermission("preferences")</tt>. 409 * @see RuntimePermission 410 */ 411 public static Preferences systemNodeForPackage(Class<?> c) { 412 return systemRoot().node(nodeName(c)); 413 } 414 415 /** 416 * Returns the absolute path name of the node corresponding to the package 417 * of the specified object. 418 * 419 * @throws IllegalArgumentException if the package has node preferences 420 * node associated with it. 421 */ 422 private static String nodeName(Class<?> c) { 423 if (c.isArray()) 424 throw new IllegalArgumentException( 425 "Arrays have no associated preferences node."); 426 String className = c.getName(); 427 int pkgEndIndex = className.lastIndexOf('.'); 428 if (pkgEndIndex < 0) 429 return "/<unnamed>"; 430 String packageName = className.substring(0, pkgEndIndex); 431 return "/" + packageName.replace('.', '/'); 432 } 433 434 /** 435 * This permission object represents the permission required to get 436 * access to the user or system root (which in turn allows for all 437 * other operations). 438 */ 439 private static Permission prefsPerm = new RuntimePermission("preferences"); 440 441 /** 442 * Returns the root preference node for the calling user. 443 * 444 * @return the root preference node for the calling user. 445 * @throws SecurityException If a security manager is present and 446 * it denies <tt>RuntimePermission("preferences")</tt>. 447 * @see RuntimePermission 448 */ 449 public static Preferences userRoot() { 450 SecurityManager security = System.getSecurityManager(); 451 if (security != null) 452 security.checkPermission(prefsPerm); 453 454 return factory.userRoot(); 455 } 456 457 /** 458 * Returns the root preference node for the system. 459 * 460 * @return the root preference node for the system. 461 * @throws SecurityException If a security manager is present and 462 * it denies <tt>RuntimePermission("preferences")</tt>. 463 * @see RuntimePermission 464 */ 465 public static Preferences systemRoot() { 466 SecurityManager security = System.getSecurityManager(); 467 if (security != null) 468 security.checkPermission(prefsPerm); 469 470 return factory.systemRoot(); 471 } 472 473 /** 474 * Sole constructor. (For invocation by subclass constructors, typically 475 * implicit.) 476 */ 477 protected Preferences() { 478 } 479 480 /** 481 * Associates the specified value with the specified key in this 482 * preference node. 483 * 484 * @param key key with which the specified value is to be associated. 485 * @param value value to be associated with the specified key. 486 * @throws NullPointerException if key or value is <tt>null</tt>. 487 * @throws IllegalArgumentException if <tt>key.length()</tt> exceeds 488 * <tt>MAX_KEY_LENGTH</tt> or if <tt>value.length</tt> exceeds 489 * <tt>MAX_VALUE_LENGTH</tt>. 490 * @throws IllegalStateException if this node (or an ancestor) has been 491 * removed with the {@link #removeNode()} method. 492 * @throws IllegalArgumentException if either the key or the value contain 493 * the null control character, code point U+0000. 494 */ 495 public abstract void put(String key, String value); 496 497 /** 498 * Returns the value associated with the specified key in this preference 499 * node. Returns the specified default if there is no value associated 500 * with the key, or the backing store is inaccessible. 501 * 502 * <p>Some implementations may store default values in their backing 503 * stores. If there is no value associated with the specified key 504 * but there is such a <i>stored default</i>, it is returned in 505 * preference to the specified default. 506 * 507 * @param key key whose associated value is to be returned. 508 * @param def the value to be returned in the event that this 509 * preference node has no value associated with <tt>key</tt>. 510 * @return the value associated with <tt>key</tt>, or <tt>def</tt> 511 * if no value is associated with <tt>key</tt>, or the backing 512 * store is inaccessible. 513 * @throws IllegalStateException if this node (or an ancestor) has been 514 * removed with the {@link #removeNode()} method. 515 * @throws NullPointerException if <tt>key</tt> is <tt>null</tt>. (A 516 * <tt>null</tt> value for <tt>def</tt> <i>is</i> permitted.) 517 */ 518 public abstract String get(String key, String def); 519 520 /** 521 * Removes the value associated with the specified key in this preference 522 * node, if any. 523 * 524 * <p>If this implementation supports <i>stored defaults</i>, and there is 525 * such a default for the specified preference, the stored default will be 526 * "exposed" by this call, in the sense that it will be returned 527 * by a succeeding call to <tt>get</tt>. 528 * 529 * @param key key whose mapping is to be removed from the preference node. 530 * @throws NullPointerException if <tt>key</tt> is <tt>null</tt>. 531 * @throws IllegalStateException if this node (or an ancestor) has been 532 * removed with the {@link #removeNode()} method. 533 */ 534 public abstract void remove(String key); 535 536 /** 537 * Removes all of the preferences (key-value associations) in this 538 * preference node. This call has no effect on any descendants 539 * of this node. 540 * 541 * <p>If this implementation supports <i>stored defaults</i>, and this 542 * node in the preferences hierarchy contains any such defaults, 543 * the stored defaults will be "exposed" by this call, in the sense that 544 * they will be returned by succeeding calls to <tt>get</tt>. 545 * 546 * @throws BackingStoreException if this operation cannot be completed 547 * due to a failure in the backing store, or inability to 548 * communicate with it. 549 * @throws IllegalStateException if this node (or an ancestor) has been 550 * removed with the {@link #removeNode()} method. 551 * @see #removeNode() 552 */ 553 public abstract void clear() throws BackingStoreException; 554 555 /** 556 * Associates a string representing the specified int value with the 557 * specified key in this preference node. The associated string is the 558 * one that would be returned if the int value were passed to 559 * {@link Integer#toString(int)}. This method is intended for use in 560 * conjunction with {@link #getInt}. 561 * 562 * @param key key with which the string form of value is to be associated. 563 * @param value value whose string form is to be associated with key. 564 * @throws NullPointerException if <tt>key</tt> is <tt>null</tt>. 565 * @throws IllegalArgumentException if <tt>key.length()</tt> exceeds 566 * <tt>MAX_KEY_LENGTH</tt>. 567 * @throws IllegalStateException if this node (or an ancestor) has been 568 * removed with the {@link #removeNode()} method. 569 * @see #getInt(String,int) 570 */ 571 public abstract void putInt(String key, int value); 572 573 /** 574 * Returns the int value represented by the string associated with the 575 * specified key in this preference node. The string is converted to 576 * an integer as by {@link Integer#parseInt(String)}. Returns the 577 * specified default if there is no value associated with the key, 578 * the backing store is inaccessible, or if 579 * <tt>Integer.parseInt(String)</tt> would throw a {@link 580 * NumberFormatException} if the associated value were passed. This 581 * method is intended for use in conjunction with {@link #putInt}. 582 * 583 * <p>If the implementation supports <i>stored defaults</i> and such a 584 * default exists, is accessible, and could be converted to an int 585 * with <tt>Integer.parseInt</tt>, this int is returned in preference to 586 * the specified default. 587 * 588 * @param key key whose associated value is to be returned as an int. 589 * @param def the value to be returned in the event that this 590 * preference node has no value associated with <tt>key</tt> 591 * or the associated value cannot be interpreted as an int, 592 * or the backing store is inaccessible. 593 * @return the int value represented by the string associated with 594 * <tt>key</tt> in this preference node, or <tt>def</tt> if the 595 * associated value does not exist or cannot be interpreted as 596 * an int. 597 * @throws IllegalStateException if this node (or an ancestor) has been 598 * removed with the {@link #removeNode()} method. 599 * @throws NullPointerException if <tt>key</tt> is <tt>null</tt>. 600 * @see #putInt(String,int) 601 * @see #get(String,String) 602 */ 603 public abstract int getInt(String key, int def); 604 605 /** 606 * Associates a string representing the specified long value with the 607 * specified key in this preference node. The associated string is the 608 * one that would be returned if the long value were passed to 609 * {@link Long#toString(long)}. This method is intended for use in 610 * conjunction with {@link #getLong}. 611 * 612 * @param key key with which the string form of value is to be associated. 613 * @param value value whose string form is to be associated with key. 614 * @throws NullPointerException if <tt>key</tt> is <tt>null</tt>. 615 * @throws IllegalArgumentException if <tt>key.length()</tt> exceeds 616 * <tt>MAX_KEY_LENGTH</tt>. 617 * @throws IllegalStateException if this node (or an ancestor) has been 618 * removed with the {@link #removeNode()} method. 619 * @see #getLong(String,long) 620 */ 621 public abstract void putLong(String key, long value); 622 623 /** 624 * Returns the long value represented by the string associated with the 625 * specified key in this preference node. The string is converted to 626 * a long as by {@link Long#parseLong(String)}. Returns the 627 * specified default if there is no value associated with the key, 628 * the backing store is inaccessible, or if 629 * <tt>Long.parseLong(String)</tt> would throw a {@link 630 * NumberFormatException} if the associated value were passed. This 631 * method is intended for use in conjunction with {@link #putLong}. 632 * 633 * <p>If the implementation supports <i>stored defaults</i> and such a 634 * default exists, is accessible, and could be converted to a long 635 * with <tt>Long.parseLong</tt>, this long is returned in preference to 636 * the specified default. 637 * 638 * @param key key whose associated value is to be returned as a long. 639 * @param def the value to be returned in the event that this 640 * preference node has no value associated with <tt>key</tt> 641 * or the associated value cannot be interpreted as a long, 642 * or the backing store is inaccessible. 643 * @return the long value represented by the string associated with 644 * <tt>key</tt> in this preference node, or <tt>def</tt> if the 645 * associated value does not exist or cannot be interpreted as 646 * a long. 647 * @throws IllegalStateException if this node (or an ancestor) has been 648 * removed with the {@link #removeNode()} method. 649 * @throws NullPointerException if <tt>key</tt> is <tt>null</tt>. 650 * @see #putLong(String,long) 651 * @see #get(String,String) 652 */ 653 public abstract long getLong(String key, long def); 654 655 /** 656 * Associates a string representing the specified boolean value with the 657 * specified key in this preference node. The associated string is 658 * <tt>"true"</tt> if the value is true, and <tt>"false"</tt> if it is 659 * false. This method is intended for use in conjunction with 660 * {@link #getBoolean}. 661 * 662 * @param key key with which the string form of value is to be associated. 663 * @param value value whose string form is to be associated with key. 664 * @throws NullPointerException if <tt>key</tt> is <tt>null</tt>. 665 * @throws IllegalArgumentException if <tt>key.length()</tt> exceeds 666 * <tt>MAX_KEY_LENGTH</tt>. 667 * @throws IllegalStateException if this node (or an ancestor) has been 668 * removed with the {@link #removeNode()} method. 669 * @see #getBoolean(String,boolean) 670 * @see #get(String,String) 671 */ 672 public abstract void putBoolean(String key, boolean value); 673 674 /** 675 * Returns the boolean value represented by the string associated with the 676 * specified key in this preference node. Valid strings 677 * are <tt>"true"</tt>, which represents true, and <tt>"false"</tt>, which 678 * represents false. Case is ignored, so, for example, <tt>"TRUE"</tt> 679 * and <tt>"False"</tt> are also valid. This method is intended for use in 680 * conjunction with {@link #putBoolean}. 681 * 682 * <p>Returns the specified default if there is no value 683 * associated with the key, the backing store is inaccessible, or if the 684 * associated value is something other than <tt>"true"</tt> or 685 * <tt>"false"</tt>, ignoring case. 686 * 687 * <p>If the implementation supports <i>stored defaults</i> and such a 688 * default exists and is accessible, it is used in preference to the 689 * specified default, unless the stored default is something other than 690 * <tt>"true"</tt> or <tt>"false"</tt>, ignoring case, in which case the 691 * specified default is used. 692 * 693 * @param key key whose associated value is to be returned as a boolean. 694 * @param def the value to be returned in the event that this 695 * preference node has no value associated with <tt>key</tt> 696 * or the associated value cannot be interpreted as a boolean, 697 * or the backing store is inaccessible. 698 * @return the boolean value represented by the string associated with 699 * <tt>key</tt> in this preference node, or <tt>def</tt> if the 700 * associated value does not exist or cannot be interpreted as 701 * a boolean. 702 * @throws IllegalStateException if this node (or an ancestor) has been 703 * removed with the {@link #removeNode()} method. 704 * @throws NullPointerException if <tt>key</tt> is <tt>null</tt>. 705 * @see #get(String,String) 706 * @see #putBoolean(String,boolean) 707 */ 708 public abstract boolean getBoolean(String key, boolean def); 709 710 /** 711 * Associates a string representing the specified float value with the 712 * specified key in this preference node. The associated string is the 713 * one that would be returned if the float value were passed to 714 * {@link Float#toString(float)}. This method is intended for use in 715 * conjunction with {@link #getFloat}. 716 * 717 * @param key key with which the string form of value is to be associated. 718 * @param value value whose string form is to be associated with key. 719 * @throws NullPointerException if <tt>key</tt> is <tt>null</tt>. 720 * @throws IllegalArgumentException if <tt>key.length()</tt> exceeds 721 * <tt>MAX_KEY_LENGTH</tt>. 722 * @throws IllegalStateException if this node (or an ancestor) has been 723 * removed with the {@link #removeNode()} method. 724 * @see #getFloat(String,float) 725 */ 726 public abstract void putFloat(String key, float value); 727 728 /** 729 * Returns the float value represented by the string associated with the 730 * specified key in this preference node. The string is converted to an 731 * integer as by {@link Float#parseFloat(String)}. Returns the specified 732 * default if there is no value associated with the key, the backing store 733 * is inaccessible, or if <tt>Float.parseFloat(String)</tt> would throw a 734 * {@link NumberFormatException} if the associated value were passed. 735 * This method is intended for use in conjunction with {@link #putFloat}. 736 * 737 * <p>If the implementation supports <i>stored defaults</i> and such a 738 * default exists, is accessible, and could be converted to a float 739 * with <tt>Float.parseFloat</tt>, this float is returned in preference to 740 * the specified default. 741 * 742 * @param key key whose associated value is to be returned as a float. 743 * @param def the value to be returned in the event that this 744 * preference node has no value associated with <tt>key</tt> 745 * or the associated value cannot be interpreted as a float, 746 * or the backing store is inaccessible. 747 * @return the float value represented by the string associated with 748 * <tt>key</tt> in this preference node, or <tt>def</tt> if the 749 * associated value does not exist or cannot be interpreted as 750 * a float. 751 * @throws IllegalStateException if this node (or an ancestor) has been 752 * removed with the {@link #removeNode()} method. 753 * @throws NullPointerException if <tt>key</tt> is <tt>null</tt>. 754 * @see #putFloat(String,float) 755 * @see #get(String,String) 756 */ 757 public abstract float getFloat(String key, float def); 758 759 /** 760 * Associates a string representing the specified double value with the 761 * specified key in this preference node. The associated string is the 762 * one that would be returned if the double value were passed to 763 * {@link Double#toString(double)}. This method is intended for use in 764 * conjunction with {@link #getDouble}. 765 * 766 * @param key key with which the string form of value is to be associated. 767 * @param value value whose string form is to be associated with key. 768 * @throws NullPointerException if <tt>key</tt> is <tt>null</tt>. 769 * @throws IllegalArgumentException if <tt>key.length()</tt> exceeds 770 * <tt>MAX_KEY_LENGTH</tt>. 771 * @throws IllegalStateException if this node (or an ancestor) has been 772 * removed with the {@link #removeNode()} method. 773 * @see #getDouble(String,double) 774 */ 775 public abstract void putDouble(String key, double value); 776 777 /** 778 * Returns the double value represented by the string associated with the 779 * specified key in this preference node. The string is converted to an 780 * integer as by {@link Double#parseDouble(String)}. Returns the specified 781 * default if there is no value associated with the key, the backing store 782 * is inaccessible, or if <tt>Double.parseDouble(String)</tt> would throw a 783 * {@link NumberFormatException} if the associated value were passed. 784 * This method is intended for use in conjunction with {@link #putDouble}. 785 * 786 * <p>If the implementation supports <i>stored defaults</i> and such a 787 * default exists, is accessible, and could be converted to a double 788 * with <tt>Double.parseDouble</tt>, this double is returned in preference 789 * to the specified default. 790 * 791 * @param key key whose associated value is to be returned as a double. 792 * @param def the value to be returned in the event that this 793 * preference node has no value associated with <tt>key</tt> 794 * or the associated value cannot be interpreted as a double, 795 * or the backing store is inaccessible. 796 * @return the double value represented by the string associated with 797 * <tt>key</tt> in this preference node, or <tt>def</tt> if the 798 * associated value does not exist or cannot be interpreted as 799 * a double. 800 * @throws IllegalStateException if this node (or an ancestor) has been 801 * removed with the {@link #removeNode()} method. 802 * @throws NullPointerException if <tt>key</tt> is <tt>null</tt>. 803 * @see #putDouble(String,double) 804 * @see #get(String,String) 805 */ 806 public abstract double getDouble(String key, double def); 807 808 /** 809 * Associates a string representing the specified byte array with the 810 * specified key in this preference node. The associated string is 811 * the <i>Base64</i> encoding of the byte array, as defined in <a 812 * href=http://www.ietf.org/rfc/rfc2045.txt>RFC 2045</a>, Section 6.8, 813 * with one minor change: the string will consist solely of characters 814 * from the <i>Base64 Alphabet</i>; it will not contain any newline 815 * characters. Note that the maximum length of the byte array is limited 816 * to three quarters of <tt>MAX_VALUE_LENGTH</tt> so that the length 817 * of the Base64 encoded String does not exceed <tt>MAX_VALUE_LENGTH</tt>. 818 * This method is intended for use in conjunction with 819 * {@link #getByteArray}. 820 * 821 * @param key key with which the string form of value is to be associated. 822 * @param value value whose string form is to be associated with key. 823 * @throws NullPointerException if key or value is <tt>null</tt>. 824 * @throws IllegalArgumentException if key.length() exceeds MAX_KEY_LENGTH 825 * or if value.length exceeds MAX_VALUE_LENGTH*3/4. 826 * @throws IllegalStateException if this node (or an ancestor) has been 827 * removed with the {@link #removeNode()} method. 828 * @see #getByteArray(String,byte[]) 829 * @see #get(String,String) 830 */ 831 public abstract void putByteArray(String key, byte[] value); 832 833 /** 834 * Returns the byte array value represented by the string associated with 835 * the specified key in this preference node. Valid strings are 836 * <i>Base64</i> encoded binary data, as defined in <a 837 * href=http://www.ietf.org/rfc/rfc2045.txt>RFC 2045</a>, Section 6.8, 838 * with one minor change: the string must consist solely of characters 839 * from the <i>Base64 Alphabet</i>; no newline characters or 840 * extraneous characters are permitted. This method is intended for use 841 * in conjunction with {@link #putByteArray}. 842 * 843 * <p>Returns the specified default if there is no value 844 * associated with the key, the backing store is inaccessible, or if the 845 * associated value is not a valid Base64 encoded byte array 846 * (as defined above). 847 * 848 * <p>If the implementation supports <i>stored defaults</i> and such a 849 * default exists and is accessible, it is used in preference to the 850 * specified default, unless the stored default is not a valid Base64 851 * encoded byte array (as defined above), in which case the 852 * specified default is used. 853 * 854 * @param key key whose associated value is to be returned as a byte array. 855 * @param def the value to be returned in the event that this 856 * preference node has no value associated with <tt>key</tt> 857 * or the associated value cannot be interpreted as a byte array, 858 * or the backing store is inaccessible. 859 * @return the byte array value represented by the string associated with 860 * <tt>key</tt> in this preference node, or <tt>def</tt> if the 861 * associated value does not exist or cannot be interpreted as 862 * a byte array. 863 * @throws IllegalStateException if this node (or an ancestor) has been 864 * removed with the {@link #removeNode()} method. 865 * @throws NullPointerException if <tt>key</tt> is <tt>null</tt>. (A 866 * <tt>null</tt> value for <tt>def</tt> <i>is</i> permitted.) 867 * @see #get(String,String) 868 * @see #putByteArray(String,byte[]) 869 */ 870 public abstract byte[] getByteArray(String key, byte[] def); 871 872 /** 873 * Returns all of the keys that have an associated value in this 874 * preference node. (The returned array will be of size zero if 875 * this node has no preferences.) 876 * 877 * <p>If the implementation supports <i>stored defaults</i> and there 878 * are any such defaults at this node that have not been overridden, 879 * by explicit preferences, the defaults are returned in the array in 880 * addition to any explicit preferences. 881 * 882 * @return an array of the keys that have an associated value in this 883 * preference node. 884 * @throws BackingStoreException if this operation cannot be completed 885 * due to a failure in the backing store, or inability to 886 * communicate with it. 887 * @throws IllegalStateException if this node (or an ancestor) has been 888 * removed with the {@link #removeNode()} method. 889 */ 890 public abstract String[] keys() throws BackingStoreException; 891 892 /** 893 * Returns the names of the children of this preference node, relative to 894 * this node. (The returned array will be of size zero if this node has 895 * no children.) 896 * 897 * @return the names of the children of this preference node. 898 * @throws BackingStoreException if this operation cannot be completed 899 * due to a failure in the backing store, or inability to 900 * communicate with it. 901 * @throws IllegalStateException if this node (or an ancestor) has been 902 * removed with the {@link #removeNode()} method. 903 */ 904 public abstract String[] childrenNames() throws BackingStoreException; 905 906 /** 907 * Returns the parent of this preference node, or <tt>null</tt> if this is 908 * the root. 909 * 910 * @return the parent of this preference node. 911 * @throws IllegalStateException if this node (or an ancestor) has been 912 * removed with the {@link #removeNode()} method. 913 */ 914 public abstract Preferences parent(); 915 916 /** 917 * Returns the named preference node in the same tree as this node, 918 * creating it and any of its ancestors if they do not already exist. 919 * Accepts a relative or absolute path name. Relative path names 920 * (which do not begin with the slash character <tt>('/')</tt>) are 921 * interpreted relative to this preference node. 922 * 923 * <p>If the returned node did not exist prior to this call, this node and 924 * any ancestors that were created by this call are not guaranteed 925 * to become permanent until the <tt>flush</tt> method is called on 926 * the returned node (or one of its ancestors or descendants). 927 * 928 * @param pathName the path name of the preference node to return. 929 * @return the specified preference node. 930 * @throws IllegalArgumentException if the path name is invalid (i.e., 931 * it contains multiple consecutive slash characters, or ends 932 * with a slash character and is more than one character long). 933 * @throws NullPointerException if path name is <tt>null</tt>. 934 * @throws IllegalStateException if this node (or an ancestor) has been 935 * removed with the {@link #removeNode()} method. 936 * @see #flush() 937 */ 938 public abstract Preferences node(String pathName); 939 940 /** 941 * Returns true if the named preference node exists in the same tree 942 * as this node. Relative path names (which do not begin with the slash 943 * character <tt>('/')</tt>) are interpreted relative to this preference 944 * node. 945 * 946 * <p>If this node (or an ancestor) has already been removed with the 947 * {@link #removeNode()} method, it <i>is</i> legal to invoke this method, 948 * but only with the path name <tt>""</tt>; the invocation will return 949 * <tt>false</tt>. Thus, the idiom <tt>p.nodeExists("")</tt> may be 950 * used to test whether <tt>p</tt> has been removed. 951 * 952 * @param pathName the path name of the node whose existence 953 * is to be checked. 954 * @return true if the specified node exists. 955 * @throws BackingStoreException if this operation cannot be completed 956 * due to a failure in the backing store, or inability to 957 * communicate with it. 958 * @throws IllegalArgumentException if the path name is invalid (i.e., 959 * it contains multiple consecutive slash characters, or ends 960 * with a slash character and is more than one character long). 961 * @throws NullPointerException if path name is <tt>null</tt>. 962 * @throws IllegalStateException if this node (or an ancestor) has been 963 * removed with the {@link #removeNode()} method and 964 * <tt>pathName</tt> is not the empty string (<tt>""</tt>). 965 */ 966 public abstract boolean nodeExists(String pathName) 967 throws BackingStoreException; 968 969 /** 970 * Removes this preference node and all of its descendants, invalidating 971 * any preferences contained in the removed nodes. Once a node has been 972 * removed, attempting any method other than {@link #name()}, 973 * {@link #absolutePath()}, {@link #isUserNode()}, {@link #flush()} or 974 * {@link #node(String) nodeExists("")} on the corresponding 975 * <tt>Preferences</tt> instance will fail with an 976 * <tt>IllegalStateException</tt>. (The methods defined on {@link Object} 977 * can still be invoked on a node after it has been removed; they will not 978 * throw <tt>IllegalStateException</tt>.) 979 * 980 * <p>The removal is not guaranteed to be persistent until the 981 * <tt>flush</tt> method is called on this node (or an ancestor). 982 * 983 * <p>If this implementation supports <i>stored defaults</i>, removing a 984 * node exposes any stored defaults at or below this node. Thus, a 985 * subsequent call to <tt>nodeExists</tt> on this node's path name may 986 * return <tt>true</tt>, and a subsequent call to <tt>node</tt> on this 987 * path name may return a (different) <tt>Preferences</tt> instance 988 * representing a non-empty collection of preferences and/or children. 989 * 990 * @throws BackingStoreException if this operation cannot be completed 991 * due to a failure in the backing store, or inability to 992 * communicate with it. 993 * @throws IllegalStateException if this node (or an ancestor) has already 994 * been removed with the {@link #removeNode()} method. 995 * @throws UnsupportedOperationException if this method is invoked on 996 * the root node. 997 * @see #flush() 998 */ 999 public abstract void removeNode() throws BackingStoreException; 1000 1001 /** 1002 * Returns this preference node's name, relative to its parent. 1003 * 1004 * @return this preference node's name, relative to its parent. 1005 */ 1006 public abstract String name(); 1007 1008 /** 1009 * Returns this preference node's absolute path name. 1010 * 1011 * @return this preference node's absolute path name. 1012 */ 1013 public abstract String absolutePath(); 1014 1015 /** 1016 * Returns <tt>true</tt> if this preference node is in the user 1017 * preference tree, <tt>false</tt> if it's in the system preference tree. 1018 * 1019 * @return <tt>true</tt> if this preference node is in the user 1020 * preference tree, <tt>false</tt> if it's in the system 1021 * preference tree. 1022 */ 1023 public abstract boolean isUserNode(); 1024 1025 /** 1026 * Returns a string representation of this preferences node, 1027 * as if computed by the expression:<tt>(this.isUserNode() ? "User" : 1028 * "System") + " Preference Node: " + this.absolutePath()</tt>. 1029 */ 1030 public abstract String toString(); 1031 1032 /** 1033 * Forces any changes in the contents of this preference node and its 1034 * descendants to the persistent store. Once this method returns 1035 * successfully, it is safe to assume that all changes made in the 1036 * subtree rooted at this node prior to the method invocation have become 1037 * permanent. 1038 * 1039 * <p>Implementations are free to flush changes into the persistent store 1040 * at any time. They do not need to wait for this method to be called. 1041 * 1042 * <p>When a flush occurs on a newly created node, it is made persistent, 1043 * as are any ancestors (and descendants) that have yet to be made 1044 * persistent. Note however that any preference value changes in 1045 * ancestors are <i>not</i> guaranteed to be made persistent. 1046 * 1047 * <p> If this method is invoked on a node that has been removed with 1048 * the {@link #removeNode()} method, flushSpi() is invoked on this node, 1049 * but not on others. 1050 * 1051 * @throws BackingStoreException if this operation cannot be completed 1052 * due to a failure in the backing store, or inability to 1053 * communicate with it. 1054 * @see #sync() 1055 */ 1056 public abstract void flush() throws BackingStoreException; 1057 1058 /** 1059 * Ensures that future reads from this preference node and its 1060 * descendants reflect any changes that were committed to the persistent 1061 * store (from any VM) prior to the <tt>sync</tt> invocation. As a 1062 * side-effect, forces any changes in the contents of this preference node 1063 * and its descendants to the persistent store, as if the <tt>flush</tt> 1064 * method had been invoked on this node. 1065 * 1066 * @throws BackingStoreException if this operation cannot be completed 1067 * due to a failure in the backing store, or inability to 1068 * communicate with it. 1069 * @throws IllegalStateException if this node (or an ancestor) has been 1070 * removed with the {@link #removeNode()} method. 1071 * @see #flush() 1072 */ 1073 public abstract void sync() throws BackingStoreException; 1074 1075 /** 1076 * Registers the specified listener to receive <i>preference change 1077 * events</i> for this preference node. A preference change event is 1078 * generated when a preference is added to this node, removed from this 1079 * node, or when the value associated with a preference is changed. 1080 * (Preference change events are <i>not</i> generated by the {@link 1081 * #removeNode()} method, which generates a <i>node change event</i>. 1082 * Preference change events <i>are</i> generated by the <tt>clear</tt> 1083 * method.) 1084 * 1085 * <p>Events are only guaranteed for changes made within the same JVM 1086 * as the registered listener, though some implementations may generate 1087 * events for changes made outside this JVM. Events may be generated 1088 * before the changes have been made persistent. Events are not generated 1089 * when preferences are modified in descendants of this node; a caller 1090 * desiring such events must register with each descendant. 1091 * 1092 * @param pcl The preference change listener to add. 1093 * @throws NullPointerException if <tt>pcl</tt> is null. 1094 * @throws IllegalStateException if this node (or an ancestor) has been 1095 * removed with the {@link #removeNode()} method. 1096 * @see #removePreferenceChangeListener(PreferenceChangeListener) 1097 * @see #addNodeChangeListener(NodeChangeListener) 1098 */ 1099 public abstract void addPreferenceChangeListener( 1100 PreferenceChangeListener pcl); 1101 1102 /** 1103 * Removes the specified preference change listener, so it no longer 1104 * receives preference change events. 1105 * 1106 * @param pcl The preference change listener to remove. 1107 * @throws IllegalArgumentException if <tt>pcl</tt> was not a registered 1108 * preference change listener on this node. 1109 * @throws IllegalStateException if this node (or an ancestor) has been 1110 * removed with the {@link #removeNode()} method. 1111 * @see #addPreferenceChangeListener(PreferenceChangeListener) 1112 */ 1113 public abstract void removePreferenceChangeListener( 1114 PreferenceChangeListener pcl); 1115 1116 /** 1117 * Registers the specified listener to receive <i>node change events</i> 1118 * for this node. A node change event is generated when a child node is 1119 * added to or removed from this node. (A single {@link #removeNode()} 1120 * invocation results in multiple <i>node change events</i>, one for every 1121 * node in the subtree rooted at the removed node.) 1122 * 1123 * <p>Events are only guaranteed for changes made within the same JVM 1124 * as the registered listener, though some implementations may generate 1125 * events for changes made outside this JVM. Events may be generated 1126 * before the changes have become permanent. Events are not generated 1127 * when indirect descendants of this node are added or removed; a 1128 * caller desiring such events must register with each descendant. 1129 * 1130 * <p>Few guarantees can be made regarding node creation. Because nodes 1131 * are created implicitly upon access, it may not be feasible for an 1132 * implementation to determine whether a child node existed in the backing 1133 * store prior to access (for example, because the backing store is 1134 * unreachable or cached information is out of date). Under these 1135 * circumstances, implementations are neither required to generate node 1136 * change events nor prohibited from doing so. 1137 * 1138 * @param ncl The <tt>NodeChangeListener</tt> to add. 1139 * @throws NullPointerException if <tt>ncl</tt> is null. 1140 * @throws IllegalStateException if this node (or an ancestor) has been 1141 * removed with the {@link #removeNode()} method. 1142 * @see #removeNodeChangeListener(NodeChangeListener) 1143 * @see #addPreferenceChangeListener(PreferenceChangeListener) 1144 */ 1145 public abstract void addNodeChangeListener(NodeChangeListener ncl); 1146 1147 /** 1148 * Removes the specified <tt>NodeChangeListener</tt>, so it no longer 1149 * receives change events. 1150 * 1151 * @param ncl The <tt>NodeChangeListener</tt> to remove. 1152 * @throws IllegalArgumentException if <tt>ncl</tt> was not a registered 1153 * <tt>NodeChangeListener</tt> on this node. 1154 * @throws IllegalStateException if this node (or an ancestor) has been 1155 * removed with the {@link #removeNode()} method. 1156 * @see #addNodeChangeListener(NodeChangeListener) 1157 */ 1158 public abstract void removeNodeChangeListener(NodeChangeListener ncl); 1159 1160 /** 1161 * Emits on the specified output stream an XML document representing all 1162 * of the preferences contained in this node (but not its descendants). 1163 * This XML document is, in effect, an offline backup of the node. 1164 * 1165 * <p>The XML document will have the following DOCTYPE declaration: 1166 * <pre>{@code 1167 * <!DOCTYPE preferences SYSTEM "http://java.sun.com/dtd/preferences.dtd"> 1168 * }</pre> 1169 * The UTF-8 character encoding will be used. 1170 * 1171 * <p>This method is an exception to the general rule that the results of 1172 * concurrently executing multiple methods in this class yields 1173 * results equivalent to some serial execution. If the preferences 1174 * at this node are modified concurrently with an invocation of this 1175 * method, the exported preferences comprise a "fuzzy snapshot" of the 1176 * preferences contained in the node; some of the concurrent modifications 1177 * may be reflected in the exported data while others may not. 1178 * 1179 * @param os the output stream on which to emit the XML document. 1180 * @throws IOException if writing to the specified output stream 1181 * results in an <tt>IOException</tt>. 1182 * @throws BackingStoreException if preference data cannot be read from 1183 * backing store. 1184 * @see #importPreferences(InputStream) 1185 * @throws IllegalStateException if this node (or an ancestor) has been 1186 * removed with the {@link #removeNode()} method. 1187 */ 1188 public abstract void exportNode(OutputStream os) 1189 throws IOException, BackingStoreException; 1190 1191 /** 1192 * Emits an XML document representing all of the preferences contained 1193 * in this node and all of its descendants. This XML document is, in 1194 * effect, an offline backup of the subtree rooted at the node. 1195 * 1196 * <p>The XML document will have the following DOCTYPE declaration: 1197 * <pre>{@code 1198 * <!DOCTYPE preferences SYSTEM "http://java.sun.com/dtd/preferences.dtd"> 1199 * }</pre> 1200 * The UTF-8 character encoding will be used. 1201 * 1202 * <p>This method is an exception to the general rule that the results of 1203 * concurrently executing multiple methods in this class yields 1204 * results equivalent to some serial execution. If the preferences 1205 * or nodes in the subtree rooted at this node are modified concurrently 1206 * with an invocation of this method, the exported preferences comprise a 1207 * "fuzzy snapshot" of the subtree; some of the concurrent modifications 1208 * may be reflected in the exported data while others may not. 1209 * 1210 * @param os the output stream on which to emit the XML document. 1211 * @throws IOException if writing to the specified output stream 1212 * results in an <tt>IOException</tt>. 1213 * @throws BackingStoreException if preference data cannot be read from 1214 * backing store. 1215 * @throws IllegalStateException if this node (or an ancestor) has been 1216 * removed with the {@link #removeNode()} method. 1217 * @see #importPreferences(InputStream) 1218 * @see #exportNode(OutputStream) 1219 */ 1220 public abstract void exportSubtree(OutputStream os) 1221 throws IOException, BackingStoreException; 1222 1223 /** 1224 * Imports all of the preferences represented by the XML document on the 1225 * specified input stream. The document may represent user preferences or 1226 * system preferences. If it represents user preferences, the preferences 1227 * will be imported into the calling user's preference tree (even if they 1228 * originally came from a different user's preference tree). If any of 1229 * the preferences described by the document inhabit preference nodes that 1230 * do not exist, the nodes will be created. 1231 * 1232 * <p>The XML document must have the following DOCTYPE declaration: 1233 * <pre>{@code 1234 * <!DOCTYPE preferences SYSTEM "http://java.sun.com/dtd/preferences.dtd"> 1235 * }</pre> 1236 * (This method is designed for use in conjunction with 1237 * {@link #exportNode(OutputStream)} and 1238 * {@link #exportSubtree(OutputStream)}. 1239 * 1240 * <p>This method is an exception to the general rule that the results of 1241 * concurrently executing multiple methods in this class yields 1242 * results equivalent to some serial execution. The method behaves 1243 * as if implemented on top of the other public methods in this class, 1244 * notably {@link #node(String)} and {@link #put(String, String)}. 1245 * 1246 * @param is the input stream from which to read the XML document. 1247 * @throws IOException if reading from the specified input stream 1248 * results in an <tt>IOException</tt>. 1249 * @throws InvalidPreferencesFormatException Data on input stream does not 1250 * constitute a valid XML document with the mandated document type. 1251 * @throws SecurityException If a security manager is present and 1252 * it denies <tt>RuntimePermission("preferences")</tt>. 1253 * @see RuntimePermission 1254 */ 1255 public static void importPreferences(InputStream is) 1256 throws IOException, InvalidPreferencesFormatException 1257 { 1258 XmlSupport.importPreferences(is); 1259 } 1260 }