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