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