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