Preferences.java revision 2683a45f92104cafe8acaf18e7211c67324a0fe6
1/* 2 * Copyright (c) 2000, 2012, 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 26package java.util.prefs; 27 28import java.io.InputStream; 29import java.io.IOException; 30import java.io.OutputStream; 31import java.security.AccessController; 32import java.security.Permission; 33import java.security.PrivilegedAction; 34import java.util.Iterator; 35import java.util.ServiceLoader; 36import java.util.ServiceConfigurationError; 37 38// These imports needed only as a workaround for a JavaDoc bug 39import java.lang.RuntimePermission; 40import java.lang.Integer; 41import java.lang.Long; 42import java.lang.Float; 43import 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> 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> 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 */ 224public abstract class Preferences { 225 226 // Android-changed: Not final for testing. 227 private static PreferencesFactory factory = factory(); 228 229 // Android-changed: Collapse factory / factory1 into a single function, remove 230 // references to AccessController and hacks to use the context class-loader. 231 // Also remove references to the OS specific preferences implementation. 232 private static PreferencesFactory factory() { 233 // 1. Try user-specified system property 234 String factoryName = System.getProperty("java.util.prefs.PreferencesFactory"); 235 if (factoryName != null) { 236 try { 237 return (PreferencesFactory) 238 Class.forName(factoryName, false, 239 ClassLoader.getSystemClassLoader()) 240 .newInstance(); 241 } catch (Exception ex) { 242 InternalError error = new InternalError( 243 "Can't instantiate Preferences factory " + factoryName); 244 error.initCause(ex); 245 throw error; 246 } 247 } 248 249 // 2. Try service provider interface 250 Iterator<PreferencesFactory> itr = ServiceLoader 251 .load(PreferencesFactory.class, ClassLoader.getSystemClassLoader()) 252 .iterator(); 253 254 // choose first provider instance 255 while (itr.hasNext()) { 256 try { 257 return itr.next(); 258 } catch (ServiceConfigurationError sce) { 259 throw sce; 260 } 261 } 262 263 // Android-changed: Remove support for OS Specific configuration options and 264 // just return a hard coded default. 265 return new FilePreferencesFactoryImpl(); 266 } 267 268 /** 269 * @hide for testing only. 270 */ 271 // Android-changed: Allow this to be set for testing. 272 public static PreferencesFactory setPreferencesFactory(PreferencesFactory pf) { 273 PreferencesFactory previous = factory; 274 factory = pf; 275 return previous; 276 } 277 278 /** 279 * Maximum length of string allowed as a key (80 characters). 280 */ 281 public static final int MAX_KEY_LENGTH = 80; 282 283 /** 284 * Maximum length of string allowed as a value (8192 characters). 285 */ 286 public static final int MAX_VALUE_LENGTH = 8*1024; 287 288 /** 289 * Maximum length of a node name (80 characters). 290 */ 291 public static final int MAX_NAME_LENGTH = 80; 292 293 /** 294 * Returns the preference node from the calling user's preference tree 295 * that is associated (by convention) with the specified class's package. 296 * The convention is as follows: the absolute path name of the node is the 297 * fully qualified package name, preceded by a slash (<tt>'/'</tt>), and 298 * with each period (<tt>'.'</tt>) replaced by a slash. For example the 299 * absolute path name of the node associated with the class 300 * <tt>com.acme.widget.Foo</tt> is <tt>/com/acme/widget</tt>. 301 * 302 * <p>This convention does not apply to the unnamed package, whose 303 * associated preference node is <tt><unnamed></tt>. This node 304 * is not intended for long term use, but for convenience in the early 305 * development of programs that do not yet belong to a package, and 306 * for "throwaway" programs. <i>Valuable data should not be stored 307 * at this node as it is shared by all programs that use it.</i> 308 * 309 * <p>A class <tt>Foo</tt> wishing to access preferences pertaining to its 310 * package can obtain a preference node as follows: <pre> 311 * static Preferences prefs = Preferences.userNodeForPackage(Foo.class); 312 * </pre> 313 * This idiom obviates the need for using a string to describe the 314 * preferences node and decreases the likelihood of a run-time failure. 315 * (If the class name is misspelled, it will typically result in a 316 * compile-time error.) 317 * 318 * <p>Invoking this method will result in the creation of the returned 319 * node and its ancestors if they do not already exist. If the returned 320 * node did not exist prior to this call, this node and any ancestors that 321 * were created by this call are not guaranteed to become permanent until 322 * the <tt>flush</tt> method is called on the returned node (or one of its 323 * ancestors or descendants). 324 * 325 * @param c the class for whose package a user preference node is desired. 326 * @return the user preference node associated with the package of which 327 * <tt>c</tt> is a member. 328 * @throws NullPointerException if <tt>c</tt> is <tt>null</tt>. 329 * @throws SecurityException if a security manager is present and 330 * it denies <tt>RuntimePermission("preferences")</tt>. 331 * @see RuntimePermission 332 */ 333 public static Preferences userNodeForPackage(Class<?> c) { 334 return userRoot().node(nodeName(c)); 335 } 336 337 /** 338 * Returns the preference node from the system preference tree that is 339 * associated (by convention) with the specified class's package. The 340 * convention is as follows: the absolute path name of the node is the 341 * fully qualified package name, preceded by a slash (<tt>'/'</tt>), and 342 * with each period (<tt>'.'</tt>) replaced by a slash. For example the 343 * absolute path name of the node associated with the class 344 * <tt>com.acme.widget.Foo</tt> is <tt>/com/acme/widget</tt>. 345 * 346 * <p>This convention does not apply to the unnamed package, whose 347 * associated preference node is <tt><unnamed></tt>. This node 348 * is not intended for long term use, but for convenience in the early 349 * development of programs that do not yet belong to a package, and 350 * for "throwaway" programs. <i>Valuable data should not be stored 351 * at this node as it is shared by all programs that use it.</i> 352 * 353 * <p>A class <tt>Foo</tt> wishing to access preferences pertaining to its 354 * package can obtain a preference node as follows: <pre> 355 * static Preferences prefs = Preferences.systemNodeForPackage(Foo.class); 356 * </pre> 357 * This idiom obviates the need for using a string to describe the 358 * preferences node and decreases the likelihood of a run-time failure. 359 * (If the class name is misspelled, it will typically result in a 360 * compile-time error.) 361 * 362 * <p>Invoking this method will result in the creation of the returned 363 * node and its ancestors if they do not already exist. If the returned 364 * node did not exist prior to this call, this node and any ancestors that 365 * were created by this call are not guaranteed to become permanent until 366 * the <tt>flush</tt> method is called on the returned node (or one of its 367 * ancestors or descendants). 368 * 369 * @param c the class for whose package a system preference node is desired. 370 * @return the system preference node associated with the package of which 371 * <tt>c</tt> is a member. 372 * @throws NullPointerException if <tt>c</tt> is <tt>null</tt>. 373 * @throws SecurityException if a security manager is present and 374 * it denies <tt>RuntimePermission("preferences")</tt>. 375 * @see RuntimePermission 376 */ 377 public static Preferences systemNodeForPackage(Class<?> c) { 378 return systemRoot().node(nodeName(c)); 379 } 380 381 /** 382 * Returns the absolute path name of the node corresponding to the package 383 * of the specified object. 384 * 385 * @throws IllegalArgumentException if the package has node preferences 386 * node associated with it. 387 */ 388 private static String nodeName(Class c) { 389 if (c.isArray()) 390 throw new IllegalArgumentException( 391 "Arrays have no associated preferences node."); 392 String className = c.getName(); 393 int pkgEndIndex = className.lastIndexOf('.'); 394 if (pkgEndIndex < 0) 395 return "/<unnamed>"; 396 String packageName = className.substring(0, pkgEndIndex); 397 return "/" + packageName.replace('.', '/'); 398 } 399 400 /** 401 * This permission object represents the permission required to get 402 * access to the user or system root (which in turn allows for all 403 * other operations). 404 */ 405 private static Permission prefsPerm = new RuntimePermission("preferences"); 406 407 /** 408 * Returns the root preference node for the calling user. 409 * 410 * @return the root preference node for the calling user. 411 * @throws SecurityException If a security manager is present and 412 * it denies <tt>RuntimePermission("preferences")</tt>. 413 * @see RuntimePermission 414 */ 415 public static Preferences userRoot() { 416 SecurityManager security = System.getSecurityManager(); 417 if (security != null) 418 security.checkPermission(prefsPerm); 419 420 return factory.userRoot(); 421 } 422 423 /** 424 * Returns the root preference node for the system. 425 * 426 * @return the root preference node for the system. 427 * @throws SecurityException If a security manager is present and 428 * it denies <tt>RuntimePermission("preferences")</tt>. 429 * @see RuntimePermission 430 */ 431 public static Preferences systemRoot() { 432 SecurityManager security = System.getSecurityManager(); 433 if (security != null) 434 security.checkPermission(prefsPerm); 435 436 return factory.systemRoot(); 437 } 438 439 /** 440 * Sole constructor. (For invocation by subclass constructors, typically 441 * implicit.) 442 */ 443 protected Preferences() { 444 } 445 446 /** 447 * Associates the specified value with the specified key in this 448 * preference node. 449 * 450 * @param key key with which the specified value is to be associated. 451 * @param value value to be associated with the specified key. 452 * @throws NullPointerException if key or value is <tt>null</tt>. 453 * @throws IllegalArgumentException if <tt>key.length()</tt> exceeds 454 * <tt>MAX_KEY_LENGTH</tt> or if <tt>value.length</tt> exceeds 455 * <tt>MAX_VALUE_LENGTH</tt>. 456 * @throws IllegalStateException if this node (or an ancestor) has been 457 * removed with the {@link #removeNode()} method. 458 */ 459 public abstract void put(String key, String value); 460 461 /** 462 * Returns the value associated with the specified key in this preference 463 * node. Returns the specified default if there is no value associated 464 * with the key, or the backing store is inaccessible. 465 * 466 * <p>Some implementations may store default values in their backing 467 * stores. If there is no value associated with the specified key 468 * but there is such a <i>stored default</i>, it is returned in 469 * preference to the specified default. 470 * 471 * @param key key whose associated value is to be returned. 472 * @param def the value to be returned in the event that this 473 * preference node has no value associated with <tt>key</tt>. 474 * @return the value associated with <tt>key</tt>, or <tt>def</tt> 475 * if no value is associated with <tt>key</tt>, or the backing 476 * store is inaccessible. 477 * @throws IllegalStateException if this node (or an ancestor) has been 478 * removed with the {@link #removeNode()} method. 479 * @throws NullPointerException if <tt>key</tt> is <tt>null</tt>. (A 480 * <tt>null</tt> value for <tt>def</tt> <i>is</i> permitted.) 481 */ 482 public abstract String get(String key, String def); 483 484 /** 485 * Removes the value associated with the specified key in this preference 486 * node, if any. 487 * 488 * <p>If this implementation supports <i>stored defaults</i>, and there is 489 * such a default for the specified preference, the stored default will be 490 * "exposed" by this call, in the sense that it will be returned 491 * by a succeeding call to <tt>get</tt>. 492 * 493 * @param key key whose mapping is to be removed from the preference node. 494 * @throws NullPointerException if <tt>key</tt> is <tt>null</tt>. 495 * @throws IllegalStateException if this node (or an ancestor) has been 496 * removed with the {@link #removeNode()} method. 497 */ 498 public abstract void remove(String key); 499 500 /** 501 * Removes all of the preferences (key-value associations) in this 502 * preference node. This call has no effect on any descendants 503 * of this node. 504 * 505 * <p>If this implementation supports <i>stored defaults</i>, and this 506 * node in the preferences hierarchy contains any such defaults, 507 * the stored defaults will be "exposed" by this call, in the sense that 508 * they will be returned by succeeding calls to <tt>get</tt>. 509 * 510 * @throws BackingStoreException if this operation cannot be completed 511 * due to a failure in the backing store, or inability to 512 * communicate with it. 513 * @throws IllegalStateException if this node (or an ancestor) has been 514 * removed with the {@link #removeNode()} method. 515 * @see #removeNode() 516 */ 517 public abstract void clear() throws BackingStoreException; 518 519 /** 520 * Associates a string representing the specified int value with the 521 * specified key in this preference node. The associated string is the 522 * one that would be returned if the int value were passed to 523 * {@link Integer#toString(int)}. This method is intended for use in 524 * conjunction with {@link #getInt}. 525 * 526 * @param key key with which the string form of value is to be associated. 527 * @param value value whose string form is to be associated with key. 528 * @throws NullPointerException if <tt>key</tt> is <tt>null</tt>. 529 * @throws IllegalArgumentException if <tt>key.length()</tt> exceeds 530 * <tt>MAX_KEY_LENGTH</tt>. 531 * @throws IllegalStateException if this node (or an ancestor) has been 532 * removed with the {@link #removeNode()} method. 533 * @see #getInt(String,int) 534 */ 535 public abstract void putInt(String key, int value); 536 537 /** 538 * Returns the int value represented by the string associated with the 539 * specified key in this preference node. The string is converted to 540 * an integer as by {@link Integer#parseInt(String)}. Returns the 541 * specified default if there is no value associated with the key, 542 * the backing store is inaccessible, or if 543 * <tt>Integer.parseInt(String)</tt> would throw a {@link 544 * NumberFormatException} if the associated value were passed. This 545 * method is intended for use in conjunction with {@link #putInt}. 546 * 547 * <p>If the implementation supports <i>stored defaults</i> and such a 548 * default exists, is accessible, and could be converted to an int 549 * with <tt>Integer.parseInt</tt>, this int is returned in preference to 550 * the specified default. 551 * 552 * @param key key whose associated value is to be returned as an int. 553 * @param def the value to be returned in the event that this 554 * preference node has no value associated with <tt>key</tt> 555 * or the associated value cannot be interpreted as an int, 556 * or the backing store is inaccessible. 557 * @return the int value represented by the string associated with 558 * <tt>key</tt> in this preference node, or <tt>def</tt> if the 559 * associated value does not exist or cannot be interpreted as 560 * an int. 561 * @throws IllegalStateException if this node (or an ancestor) has been 562 * removed with the {@link #removeNode()} method. 563 * @throws NullPointerException if <tt>key</tt> is <tt>null</tt>. 564 * @see #putInt(String,int) 565 * @see #get(String,String) 566 */ 567 public abstract int getInt(String key, int def); 568 569 /** 570 * Associates a string representing the specified long value with the 571 * specified key in this preference node. The associated string is the 572 * one that would be returned if the long value were passed to 573 * {@link Long#toString(long)}. This method is intended for use in 574 * conjunction with {@link #getLong}. 575 * 576 * @param key key with which the string form of value is to be associated. 577 * @param value value whose string form is to be associated with key. 578 * @throws NullPointerException if <tt>key</tt> is <tt>null</tt>. 579 * @throws IllegalArgumentException if <tt>key.length()</tt> exceeds 580 * <tt>MAX_KEY_LENGTH</tt>. 581 * @throws IllegalStateException if this node (or an ancestor) has been 582 * removed with the {@link #removeNode()} method. 583 * @see #getLong(String,long) 584 */ 585 public abstract void putLong(String key, long value); 586 587 /** 588 * Returns the long value represented by the string associated with the 589 * specified key in this preference node. The string is converted to 590 * a long as by {@link Long#parseLong(String)}. Returns the 591 * specified default if there is no value associated with the key, 592 * the backing store is inaccessible, or if 593 * <tt>Long.parseLong(String)</tt> would throw a {@link 594 * NumberFormatException} if the associated value were passed. This 595 * method is intended for use in conjunction with {@link #putLong}. 596 * 597 * <p>If the implementation supports <i>stored defaults</i> and such a 598 * default exists, is accessible, and could be converted to a long 599 * with <tt>Long.parseLong</tt>, this long is returned in preference to 600 * the specified default. 601 * 602 * @param key key whose associated value is to be returned as a long. 603 * @param def the value to be returned in the event that this 604 * preference node has no value associated with <tt>key</tt> 605 * or the associated value cannot be interpreted as a long, 606 * or the backing store is inaccessible. 607 * @return the long value represented by the string associated with 608 * <tt>key</tt> in this preference node, or <tt>def</tt> if the 609 * associated value does not exist or cannot be interpreted as 610 * a long. 611 * @throws IllegalStateException if this node (or an ancestor) has been 612 * removed with the {@link #removeNode()} method. 613 * @throws NullPointerException if <tt>key</tt> is <tt>null</tt>. 614 * @see #putLong(String,long) 615 * @see #get(String,String) 616 */ 617 public abstract long getLong(String key, long def); 618 619 /** 620 * Associates a string representing the specified boolean value with the 621 * specified key in this preference node. The associated string is 622 * <tt>"true"</tt> if the value is true, and <tt>"false"</tt> if it is 623 * false. This method is intended for use in conjunction with 624 * {@link #getBoolean}. 625 * 626 * @param key key with which the string form of value is to be associated. 627 * @param value value whose string form is to be associated with key. 628 * @throws NullPointerException if <tt>key</tt> is <tt>null</tt>. 629 * @throws IllegalArgumentException if <tt>key.length()</tt> exceeds 630 * <tt>MAX_KEY_LENGTH</tt>. 631 * @throws IllegalStateException if this node (or an ancestor) has been 632 * removed with the {@link #removeNode()} method. 633 * @see #getBoolean(String,boolean) 634 * @see #get(String,String) 635 */ 636 public abstract void putBoolean(String key, boolean value); 637 638 /** 639 * Returns the boolean value represented by the string associated with the 640 * specified key in this preference node. Valid strings 641 * are <tt>"true"</tt>, which represents true, and <tt>"false"</tt>, which 642 * represents false. Case is ignored, so, for example, <tt>"TRUE"</tt> 643 * and <tt>"False"</tt> are also valid. This method is intended for use in 644 * conjunction with {@link #putBoolean}. 645 * 646 * <p>Returns the specified default if there is no value 647 * associated with the key, the backing store is inaccessible, or if the 648 * associated value is something other than <tt>"true"</tt> or 649 * <tt>"false"</tt>, ignoring case. 650 * 651 * <p>If the implementation supports <i>stored defaults</i> and such a 652 * default exists and is accessible, it is used in preference to the 653 * specified default, unless the stored default is something other than 654 * <tt>"true"</tt> or <tt>"false"</tt>, ignoring case, in which case the 655 * specified default is used. 656 * 657 * @param key key whose associated value is to be returned as a boolean. 658 * @param def the value to be returned in the event that this 659 * preference node has no value associated with <tt>key</tt> 660 * or the associated value cannot be interpreted as a boolean, 661 * or the backing store is inaccessible. 662 * @return the boolean value represented by the string associated with 663 * <tt>key</tt> in this preference node, or <tt>def</tt> if the 664 * associated value does not exist or cannot be interpreted as 665 * a boolean. 666 * @throws IllegalStateException if this node (or an ancestor) has been 667 * removed with the {@link #removeNode()} method. 668 * @throws NullPointerException if <tt>key</tt> is <tt>null</tt>. 669 * @see #get(String,String) 670 * @see #putBoolean(String,boolean) 671 */ 672 public abstract boolean getBoolean(String key, boolean def); 673 674 /** 675 * Associates a string representing the specified float value with the 676 * specified key in this preference node. The associated string is the 677 * one that would be returned if the float value were passed to 678 * {@link Float#toString(float)}. This method is intended for use in 679 * conjunction with {@link #getFloat}. 680 * 681 * @param key key with which the string form of value is to be associated. 682 * @param value value whose string form is to be associated with key. 683 * @throws NullPointerException if <tt>key</tt> is <tt>null</tt>. 684 * @throws IllegalArgumentException if <tt>key.length()</tt> exceeds 685 * <tt>MAX_KEY_LENGTH</tt>. 686 * @throws IllegalStateException if this node (or an ancestor) has been 687 * removed with the {@link #removeNode()} method. 688 * @see #getFloat(String,float) 689 */ 690 public abstract void putFloat(String key, float value); 691 692 /** 693 * Returns the float value represented by the string associated with the 694 * specified key in this preference node. The string is converted to an 695 * integer as by {@link Float#parseFloat(String)}. Returns the specified 696 * default if there is no value associated with the key, the backing store 697 * is inaccessible, or if <tt>Float.parseFloat(String)</tt> would throw a 698 * {@link NumberFormatException} if the associated value were passed. 699 * This method is intended for use in conjunction with {@link #putFloat}. 700 * 701 * <p>If the implementation supports <i>stored defaults</i> and such a 702 * default exists, is accessible, and could be converted to a float 703 * with <tt>Float.parseFloat</tt>, this float is returned in preference to 704 * the specified default. 705 * 706 * @param key key whose associated value is to be returned as a float. 707 * @param def the value to be returned in the event that this 708 * preference node has no value associated with <tt>key</tt> 709 * or the associated value cannot be interpreted as a float, 710 * or the backing store is inaccessible. 711 * @return the float value represented by the string associated with 712 * <tt>key</tt> in this preference node, or <tt>def</tt> if the 713 * associated value does not exist or cannot be interpreted as 714 * a float. 715 * @throws IllegalStateException if this node (or an ancestor) has been 716 * removed with the {@link #removeNode()} method. 717 * @throws NullPointerException if <tt>key</tt> is <tt>null</tt>. 718 * @see #putFloat(String,float) 719 * @see #get(String,String) 720 */ 721 public abstract float getFloat(String key, float def); 722 723 /** 724 * Associates a string representing the specified double value with the 725 * specified key in this preference node. The associated string is the 726 * one that would be returned if the double value were passed to 727 * {@link Double#toString(double)}. This method is intended for use in 728 * conjunction with {@link #getDouble}. 729 * 730 * @param key key with which the string form of value is to be associated. 731 * @param value value whose string form is to be associated with key. 732 * @throws NullPointerException if <tt>key</tt> is <tt>null</tt>. 733 * @throws IllegalArgumentException if <tt>key.length()</tt> exceeds 734 * <tt>MAX_KEY_LENGTH</tt>. 735 * @throws IllegalStateException if this node (or an ancestor) has been 736 * removed with the {@link #removeNode()} method. 737 * @see #getDouble(String,double) 738 */ 739 public abstract void putDouble(String key, double value); 740 741 /** 742 * Returns the double value represented by the string associated with the 743 * specified key in this preference node. The string is converted to an 744 * integer as by {@link Double#parseDouble(String)}. Returns the specified 745 * default if there is no value associated with the key, the backing store 746 * is inaccessible, or if <tt>Double.parseDouble(String)</tt> would throw a 747 * {@link NumberFormatException} if the associated value were passed. 748 * This method is intended for use in conjunction with {@link #putDouble}. 749 * 750 * <p>If the implementation supports <i>stored defaults</i> and such a 751 * default exists, is accessible, and could be converted to a double 752 * with <tt>Double.parseDouble</tt>, this double is returned in preference 753 * to the specified default. 754 * 755 * @param key key whose associated value is to be returned as a double. 756 * @param def the value to be returned in the event that this 757 * preference node has no value associated with <tt>key</tt> 758 * or the associated value cannot be interpreted as a double, 759 * or the backing store is inaccessible. 760 * @return the double value represented by the string associated with 761 * <tt>key</tt> in this preference node, or <tt>def</tt> if the 762 * associated value does not exist or cannot be interpreted as 763 * a double. 764 * @throws IllegalStateException if this node (or an ancestor) has been 765 * removed with the {@link #removeNode()} method. 766 * @throws NullPointerException if <tt>key</tt> is <tt>null</tt>. 767 * @see #putDouble(String,double) 768 * @see #get(String,String) 769 */ 770 public abstract double getDouble(String key, double def); 771 772 /** 773 * Associates a string representing the specified byte array with the 774 * specified key in this preference node. The associated string is 775 * the <i>Base64</i> encoding of the byte array, as defined in <a 776 * href=http://www.ietf.org/rfc/rfc2045.txt>RFC 2045</a>, Section 6.8, 777 * with one minor change: the string will consist solely of characters 778 * from the <i>Base64 Alphabet</i>; it will not contain any newline 779 * characters. Note that the maximum length of the byte array is limited 780 * to three quarters of <tt>MAX_VALUE_LENGTH</tt> so that the length 781 * of the Base64 encoded String does not exceed <tt>MAX_VALUE_LENGTH</tt>. 782 * This method is intended for use in conjunction with 783 * {@link #getByteArray}. 784 * 785 * @param key key with which the string form of value is to be associated. 786 * @param value value whose string form is to be associated with key. 787 * @throws NullPointerException if key or value is <tt>null</tt>. 788 * @throws IllegalArgumentException if key.length() exceeds MAX_KEY_LENGTH 789 * or if value.length exceeds MAX_VALUE_LENGTH*3/4. 790 * @throws IllegalStateException if this node (or an ancestor) has been 791 * removed with the {@link #removeNode()} method. 792 * @see #getByteArray(String,byte[]) 793 * @see #get(String,String) 794 */ 795 public abstract void putByteArray(String key, byte[] value); 796 797 /** 798 * Returns the byte array value represented by the string associated with 799 * the specified key in this preference node. Valid strings are 800 * <i>Base64</i> encoded binary data, as defined in <a 801 * href=http://www.ietf.org/rfc/rfc2045.txt>RFC 2045</a>, Section 6.8, 802 * with one minor change: the string must consist solely of characters 803 * from the <i>Base64 Alphabet</i>; no newline characters or 804 * extraneous characters are permitted. This method is intended for use 805 * in conjunction with {@link #putByteArray}. 806 * 807 * <p>Returns the specified default if there is no value 808 * associated with the key, the backing store is inaccessible, or if the 809 * associated value is not a valid Base64 encoded byte array 810 * (as defined above). 811 * 812 * <p>If the implementation supports <i>stored defaults</i> and such a 813 * default exists and is accessible, it is used in preference to the 814 * specified default, unless the stored default is not a valid Base64 815 * encoded byte array (as defined above), in which case the 816 * specified default is used. 817 * 818 * @param key key whose associated value is to be returned as a byte array. 819 * @param def the value to be returned in the event that this 820 * preference node has no value associated with <tt>key</tt> 821 * or the associated value cannot be interpreted as a byte array, 822 * or the backing store is inaccessible. 823 * @return the byte array value represented by the string associated with 824 * <tt>key</tt> in this preference node, or <tt>def</tt> if the 825 * associated value does not exist or cannot be interpreted as 826 * a byte array. 827 * @throws IllegalStateException if this node (or an ancestor) has been 828 * removed with the {@link #removeNode()} method. 829 * @throws NullPointerException if <tt>key</tt> is <tt>null</tt>. (A 830 * <tt>null</tt> value for <tt>def</tt> <i>is</i> permitted.) 831 * @see #get(String,String) 832 * @see #putByteArray(String,byte[]) 833 */ 834 public abstract byte[] getByteArray(String key, byte[] def); 835 836 /** 837 * Returns all of the keys that have an associated value in this 838 * preference node. (The returned array will be of size zero if 839 * this node has no preferences.) 840 * 841 * <p>If the implementation supports <i>stored defaults</i> and there 842 * are any such defaults at this node that have not been overridden, 843 * by explicit preferences, the defaults are returned in the array in 844 * addition to any explicit preferences. 845 * 846 * @return an array of the keys that have an associated value in this 847 * preference node. 848 * @throws BackingStoreException if this operation cannot be completed 849 * due to a failure in the backing store, or inability to 850 * communicate with it. 851 * @throws IllegalStateException if this node (or an ancestor) has been 852 * removed with the {@link #removeNode()} method. 853 */ 854 public abstract String[] keys() throws BackingStoreException; 855 856 /** 857 * Returns the names of the children of this preference node, relative to 858 * this node. (The returned array will be of size zero if this node has 859 * no children.) 860 * 861 * @return the names of the children of this preference node. 862 * @throws BackingStoreException if this operation cannot be completed 863 * due to a failure in the backing store, or inability to 864 * communicate with it. 865 * @throws IllegalStateException if this node (or an ancestor) has been 866 * removed with the {@link #removeNode()} method. 867 */ 868 public abstract String[] childrenNames() throws BackingStoreException; 869 870 /** 871 * Returns the parent of this preference node, or <tt>null</tt> if this is 872 * the root. 873 * 874 * @return the parent of this preference node. 875 * @throws IllegalStateException if this node (or an ancestor) has been 876 * removed with the {@link #removeNode()} method. 877 */ 878 public abstract Preferences parent(); 879 880 /** 881 * Returns the named preference node in the same tree as this node, 882 * creating it and any of its ancestors if they do not already exist. 883 * Accepts a relative or absolute path name. Relative path names 884 * (which do not begin with the slash character <tt>('/')</tt>) are 885 * interpreted relative to this preference node. 886 * 887 * <p>If the returned node did not exist prior to this call, this node and 888 * any ancestors that were created by this call are not guaranteed 889 * to become permanent until the <tt>flush</tt> method is called on 890 * the returned node (or one of its ancestors or descendants). 891 * 892 * @param pathName the path name of the preference node to return. 893 * @return the specified preference node. 894 * @throws IllegalArgumentException if the path name is invalid (i.e., 895 * it contains multiple consecutive slash characters, or ends 896 * with a slash character and is more than one character long). 897 * @throws NullPointerException if path name is <tt>null</tt>. 898 * @throws IllegalStateException if this node (or an ancestor) has been 899 * removed with the {@link #removeNode()} method. 900 * @see #flush() 901 */ 902 public abstract Preferences node(String pathName); 903 904 /** 905 * Returns true if the named preference node exists in the same tree 906 * as this node. Relative path names (which do not begin with the slash 907 * character <tt>('/')</tt>) are interpreted relative to this preference 908 * node. 909 * 910 * <p>If this node (or an ancestor) has already been removed with the 911 * {@link #removeNode()} method, it <i>is</i> legal to invoke this method, 912 * but only with the path name <tt>""</tt>; the invocation will return 913 * <tt>false</tt>. Thus, the idiom <tt>p.nodeExists("")</tt> may be 914 * used to test whether <tt>p</tt> has been removed. 915 * 916 * @param pathName the path name of the node whose existence 917 * is to be checked. 918 * @return true if the specified node exists. 919 * @throws BackingStoreException if this operation cannot be completed 920 * due to a failure in the backing store, or inability to 921 * communicate with it. 922 * @throws IllegalArgumentException if the path name is invalid (i.e., 923 * it contains multiple consecutive slash characters, or ends 924 * with a slash character and is more than one character long). 925 * @throws NullPointerException if path name is <tt>null</tt>. 926 * @throws IllegalStateException if this node (or an ancestor) has been 927 * removed with the {@link #removeNode()} method and 928 * <tt>pathName</tt> is not the empty string (<tt>""</tt>). 929 */ 930 public abstract boolean nodeExists(String pathName) 931 throws BackingStoreException; 932 933 /** 934 * Removes this preference node and all of its descendants, invalidating 935 * any preferences contained in the removed nodes. Once a node has been 936 * removed, attempting any method other than {@link #name()}, 937 * {@link #absolutePath()}, {@link #isUserNode()}, {@link #flush()} or 938 * {@link #node(String) nodeExists("")} on the corresponding 939 * <tt>Preferences</tt> instance will fail with an 940 * <tt>IllegalStateException</tt>. (The methods defined on {@link Object} 941 * can still be invoked on a node after it has been removed; they will not 942 * throw <tt>IllegalStateException</tt>.) 943 * 944 * <p>The removal is not guaranteed to be persistent until the 945 * <tt>flush</tt> method is called on this node (or an ancestor). 946 * 947 * <p>If this implementation supports <i>stored defaults</i>, removing a 948 * node exposes any stored defaults at or below this node. Thus, a 949 * subsequent call to <tt>nodeExists</tt> on this node's path name may 950 * return <tt>true</tt>, and a subsequent call to <tt>node</tt> on this 951 * path name may return a (different) <tt>Preferences</tt> instance 952 * representing a non-empty collection of preferences and/or children. 953 * 954 * @throws BackingStoreException if this operation cannot be completed 955 * due to a failure in the backing store, or inability to 956 * communicate with it. 957 * @throws IllegalStateException if this node (or an ancestor) has already 958 * been removed with the {@link #removeNode()} method. 959 * @throws UnsupportedOperationException if this method is invoked on 960 * the root node. 961 * @see #flush() 962 */ 963 public abstract void removeNode() throws BackingStoreException; 964 965 /** 966 * Returns this preference node's name, relative to its parent. 967 * 968 * @return this preference node's name, relative to its parent. 969 */ 970 public abstract String name(); 971 972 /** 973 * Returns this preference node's absolute path name. 974 * 975 * @return this preference node's absolute path name. 976 */ 977 public abstract String absolutePath(); 978 979 /** 980 * Returns <tt>true</tt> if this preference node is in the user 981 * preference tree, <tt>false</tt> if it's in the system preference tree. 982 * 983 * @return <tt>true</tt> if this preference node is in the user 984 * preference tree, <tt>false</tt> if it's in the system 985 * preference tree. 986 */ 987 public abstract boolean isUserNode(); 988 989 /** 990 * Returns a string representation of this preferences node, 991 * as if computed by the expression:<tt>(this.isUserNode() ? "User" : 992 * "System") + " Preference Node: " + this.absolutePath()</tt>. 993 */ 994 public abstract String toString(); 995 996 /** 997 * Forces any changes in the contents of this preference node and its 998 * descendants to the persistent store. Once this method returns 999 * successfully, it is safe to assume that all changes made in the 1000 * subtree rooted at this node prior to the method invocation have become 1001 * permanent. 1002 * 1003 * <p>Implementations are free to flush changes into the persistent store 1004 * at any time. They do not need to wait for this method to be called. 1005 * 1006 * <p>When a flush occurs on a newly created node, it is made persistent, 1007 * as are any ancestors (and descendants) that have yet to be made 1008 * persistent. Note however that any preference value changes in 1009 * ancestors are <i>not</i> guaranteed to be made persistent. 1010 * 1011 * <p> If this method is invoked on a node that has been removed with 1012 * the {@link #removeNode()} method, flushSpi() is invoked on this node, 1013 * but not on others. 1014 * 1015 * @throws BackingStoreException if this operation cannot be completed 1016 * due to a failure in the backing store, or inability to 1017 * communicate with it. 1018 * @see #sync() 1019 */ 1020 public abstract void flush() throws BackingStoreException; 1021 1022 /** 1023 * Ensures that future reads from this preference node and its 1024 * descendants reflect any changes that were committed to the persistent 1025 * store (from any VM) prior to the <tt>sync</tt> invocation. As a 1026 * side-effect, forces any changes in the contents of this preference node 1027 * and its descendants to the persistent store, as if the <tt>flush</tt> 1028 * method had been invoked on this node. 1029 * 1030 * @throws BackingStoreException if this operation cannot be completed 1031 * due to a failure in the backing store, or inability to 1032 * communicate with it. 1033 * @throws IllegalStateException if this node (or an ancestor) has been 1034 * removed with the {@link #removeNode()} method. 1035 * @see #flush() 1036 */ 1037 public abstract void sync() throws BackingStoreException; 1038 1039 /** 1040 * Registers the specified listener to receive <i>preference change 1041 * events</i> for this preference node. A preference change event is 1042 * generated when a preference is added to this node, removed from this 1043 * node, or when the value associated with a preference is changed. 1044 * (Preference change events are <i>not</i> generated by the {@link 1045 * #removeNode()} method, which generates a <i>node change event</i>. 1046 * Preference change events <i>are</i> generated by the <tt>clear</tt> 1047 * method.) 1048 * 1049 * <p>Events are only guaranteed for changes made within the same JVM 1050 * as the registered listener, though some implementations may generate 1051 * events for changes made outside this JVM. Events may be generated 1052 * before the changes have been made persistent. Events are not generated 1053 * when preferences are modified in descendants of this node; a caller 1054 * desiring such events must register with each descendant. 1055 * 1056 * @param pcl The preference change listener to add. 1057 * @throws NullPointerException if <tt>pcl</tt> is null. 1058 * @throws IllegalStateException if this node (or an ancestor) has been 1059 * removed with the {@link #removeNode()} method. 1060 * @see #removePreferenceChangeListener(PreferenceChangeListener) 1061 * @see #addNodeChangeListener(NodeChangeListener) 1062 */ 1063 public abstract void addPreferenceChangeListener( 1064 PreferenceChangeListener pcl); 1065 1066 /** 1067 * Removes the specified preference change listener, so it no longer 1068 * receives preference change events. 1069 * 1070 * @param pcl The preference change listener to remove. 1071 * @throws IllegalArgumentException if <tt>pcl</tt> was not a registered 1072 * preference change listener on this node. 1073 * @throws IllegalStateException if this node (or an ancestor) has been 1074 * removed with the {@link #removeNode()} method. 1075 * @see #addPreferenceChangeListener(PreferenceChangeListener) 1076 */ 1077 public abstract void removePreferenceChangeListener( 1078 PreferenceChangeListener pcl); 1079 1080 /** 1081 * Registers the specified listener to receive <i>node change events</i> 1082 * for this node. A node change event is generated when a child node is 1083 * added to or removed from this node. (A single {@link #removeNode()} 1084 * invocation results in multiple <i>node change events</i>, one for every 1085 * node in the subtree rooted at the removed node.) 1086 * 1087 * <p>Events are only guaranteed for changes made within the same JVM 1088 * as the registered listener, though some implementations may generate 1089 * events for changes made outside this JVM. Events may be generated 1090 * before the changes have become permanent. Events are not generated 1091 * when indirect descendants of this node are added or removed; a 1092 * caller desiring such events must register with each descendant. 1093 * 1094 * <p>Few guarantees can be made regarding node creation. Because nodes 1095 * are created implicitly upon access, it may not be feasible for an 1096 * implementation to determine whether a child node existed in the backing 1097 * store prior to access (for example, because the backing store is 1098 * unreachable or cached information is out of date). Under these 1099 * circumstances, implementations are neither required to generate node 1100 * change events nor prohibited from doing so. 1101 * 1102 * @param ncl The <tt>NodeChangeListener</tt> to add. 1103 * @throws NullPointerException if <tt>ncl</tt> is null. 1104 * @throws IllegalStateException if this node (or an ancestor) has been 1105 * removed with the {@link #removeNode()} method. 1106 * @see #removeNodeChangeListener(NodeChangeListener) 1107 * @see #addPreferenceChangeListener(PreferenceChangeListener) 1108 */ 1109 public abstract void addNodeChangeListener(NodeChangeListener ncl); 1110 1111 /** 1112 * Removes the specified <tt>NodeChangeListener</tt>, so it no longer 1113 * receives change events. 1114 * 1115 * @param ncl The <tt>NodeChangeListener</tt> to remove. 1116 * @throws IllegalArgumentException if <tt>ncl</tt> was not a registered 1117 * <tt>NodeChangeListener</tt> on this node. 1118 * @throws IllegalStateException if this node (or an ancestor) has been 1119 * removed with the {@link #removeNode()} method. 1120 * @see #addNodeChangeListener(NodeChangeListener) 1121 */ 1122 public abstract void removeNodeChangeListener(NodeChangeListener ncl); 1123 1124 /** 1125 * Emits on the specified output stream an XML document representing all 1126 * of the preferences contained in this node (but not its descendants). 1127 * This XML document is, in effect, an offline backup of the node. 1128 * 1129 * <p>The XML document will have the following DOCTYPE declaration: 1130 * <pre> 1131 * <!DOCTYPE preferences SYSTEM "http://java.sun.com/dtd/preferences.dtd"> 1132 * </pre> 1133 * The UTF-8 character encoding will be used. 1134 * 1135 * <p>This method is an exception to the general rule that the results of 1136 * concurrently executing multiple methods in this class yields 1137 * results equivalent to some serial execution. If the preferences 1138 * at this node are modified concurrently with an invocation of this 1139 * method, the exported preferences comprise a "fuzzy snapshot" of the 1140 * preferences contained in the node; some of the concurrent modifications 1141 * may be reflected in the exported data while others may not. 1142 * 1143 * @param os the output stream on which to emit the XML document. 1144 * @throws IOException if writing to the specified output stream 1145 * results in an <tt>IOException</tt>. 1146 * @throws BackingStoreException if preference data cannot be read from 1147 * backing store. 1148 * @see #importPreferences(InputStream) 1149 * @throws IllegalStateException if this node (or an ancestor) has been 1150 * removed with the {@link #removeNode()} method. 1151 */ 1152 public abstract void exportNode(OutputStream os) 1153 throws IOException, BackingStoreException; 1154 1155 /** 1156 * Emits an XML document representing all of the preferences contained 1157 * in this node and all of its descendants. This XML document is, in 1158 * effect, an offline backup of the subtree rooted at the node. 1159 * 1160 * <p>The XML document will have the following DOCTYPE declaration: 1161 * <pre> 1162 * <!DOCTYPE preferences SYSTEM "http://java.sun.com/dtd/preferences.dtd"> 1163 * </pre> 1164 * The UTF-8 character encoding will be used. 1165 * 1166 * <p>This method is an exception to the general rule that the results of 1167 * concurrently executing multiple methods in this class yields 1168 * results equivalent to some serial execution. If the preferences 1169 * or nodes in the subtree rooted at this node are modified concurrently 1170 * with an invocation of this method, the exported preferences comprise a 1171 * "fuzzy snapshot" of the subtree; some of the concurrent modifications 1172 * may be reflected in the exported data while others may not. 1173 * 1174 * @param os the output stream on which to emit the XML document. 1175 * @throws IOException if writing to the specified output stream 1176 * results in an <tt>IOException</tt>. 1177 * @throws BackingStoreException if preference data cannot be read from 1178 * backing store. 1179 * @throws IllegalStateException if this node (or an ancestor) has been 1180 * removed with the {@link #removeNode()} method. 1181 * @see #importPreferences(InputStream) 1182 * @see #exportNode(OutputStream) 1183 */ 1184 public abstract void exportSubtree(OutputStream os) 1185 throws IOException, BackingStoreException; 1186 1187 /** 1188 * Imports all of the preferences represented by the XML document on the 1189 * specified input stream. The document may represent user preferences or 1190 * system preferences. If it represents user preferences, the preferences 1191 * will be imported into the calling user's preference tree (even if they 1192 * originally came from a different user's preference tree). If any of 1193 * the preferences described by the document inhabit preference nodes that 1194 * do not exist, the nodes will be created. 1195 * 1196 * <p>The XML document must have the following DOCTYPE declaration: 1197 * <pre> 1198 * <!DOCTYPE preferences SYSTEM "http://java.sun.com/dtd/preferences.dtd"> 1199 * </pre> 1200 * (This method is designed for use in conjunction with 1201 * {@link #exportNode(OutputStream)} and 1202 * {@link #exportSubtree(OutputStream)}. 1203 * 1204 * <p>This method is an exception to the general rule that the results of 1205 * concurrently executing multiple methods in this class yields 1206 * results equivalent to some serial execution. The method behaves 1207 * as if implemented on top of the other public methods in this class, 1208 * notably {@link #node(String)} and {@link #put(String, String)}. 1209 * 1210 * @param is the input stream from which to read the XML document. 1211 * @throws IOException if reading from the specified input stream 1212 * results in an <tt>IOException</tt>. 1213 * @throws InvalidPreferencesFormatException Data on input stream does not 1214 * constitute a valid XML document with the mandated document type. 1215 * @throws SecurityException If a security manager is present and 1216 * it denies <tt>RuntimePermission("preferences")</tt>. 1217 * @see RuntimePermission 1218 */ 1219 public static void importPreferences(InputStream is) 1220 throws IOException, InvalidPreferencesFormatException 1221 { 1222 XmlSupport.importPreferences(is); 1223 } 1224} 1225