Name: nt126004 Date: 12/11/2001
P:\com_lgc\common>java -version
java version "1.4.0-beta3"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-beta3-b84)
Java HotSpot(TM) Client VM (build 1.4.0-beta3-b84, mixed mode)
1. Reproduce the problem
Start regedit.exe, and add a String value named "NEW" (without the quotes
around; with or without a value, it doesn't matter) in a registry key under
\\HKEY_CURRENT_USER\Software\JavaSoft\Prefs\ somewhere, in this example
\\HKEY_CURRENT_USER\Software\JavaSoft\Prefs\com\lgc\jax\util\prefs. Export in
XML this preferences node using exportSubtree or exportNode. The fact that the
value name uses an invalid "encoding" apparently makes the XML serialization
fail. Using the correct encoding for the value name "/N/E/W" fixes the problem.
Even though this is not standard usage of the Preferences API, since it's a
Windows-specific usage, and one modifies the BackingStore independently, this
should not fail the XML serialization. Either the value should be ignored, or
at the very least of more explicit exception should be thrown.
3.4. Exact Error Messages
Exception in thread "main" java.lang.NullPointerException
at org.apache.crimson.tree.AttributeNode.writeChildrenXml
(AttributeNode.java:270)
at org.apache.crimson.tree.AttributeNode.writeXml
(AttributeNode.java:260)
at org.apache.crimson.tree.AttributeSet.writeXml(AttributeSet.java:528)
at org.apache.crimson.tree.ElementNode2.writeXml(ElementNode2.java:319)
at org.apache.crimson.tree.ParentNode.writeChildrenXml
(ParentNode.java:162)
at org.apache.crimson.tree.ElementNode2.writeXml(ElementNode2.java:330)
at org.apache.crimson.tree.ParentNode.writeChildrenXml
(ParentNode.java:162)
at org.apache.crimson.tree.ElementNode2.writeXml(ElementNode2.java:330)
at org.apache.crimson.tree.ParentNode.writeChildrenXml
(ParentNode.java:162)
at org.apache.crimson.tree.ElementNode2.writeXml(ElementNode2.java:330)
at org.apache.crimson.tree.ParentNode.writeChildrenXml
(ParentNode.java:162)
at org.apache.crimson.tree.ElementNode2.writeXml(ElementNode2.java:330)
at org.apache.crimson.tree.ParentNode.writeChildrenXml
(ParentNode.java:162)
at org.apache.crimson.tree.ElementNode2.writeXml(ElementNode2.java:330)
at org.apache.crimson.tree.ParentNode.writeChildrenXml
(ParentNode.java:162)
at org.apache.crimson.tree.ElementNode2.writeXml(ElementNode2.java:330)
at org.apache.crimson.tree.ParentNode.writeChildrenXml
(ParentNode.java:162)
at org.apache.crimson.tree.ElementNode2.writeXml(ElementNode2.java:330)
at org.apache.crimson.tree.ParentNode.writeChildrenXml
(ParentNode.java:162)
at org.apache.crimson.tree.ElementNode2.writeXml(ElementNode2.java:330)
at org.apache.crimson.tree.ParentNode.writeChildrenXml
(ParentNode.java:162)
at org.apache.crimson.tree.ElementNode2.writeXml(ElementNode2.java:330)
at org.apache.crimson.tree.XmlDocument.writeChildrenXml
(XmlDocument.java:599)
at org.apache.crimson.tree.XmlDocument.write(XmlDocument.java:517)
at org.apache.crimson.tree.XmlDocument.write(XmlDocument.java:376)
at java.util.prefs.XmlSupport.export(XmlSupport.java:107)
at java.util.prefs.AbstractPreferences.exportSubtree
(AbstractPreferences.java:1589)
at com.lgc.jax.util.prefs.PreferencesExample.exportPrefs(Unknown Source)
at com.lgc.jax.util.prefs.PreferencesExample.main(Unknown Source)
2.
package com.lgc.jax.util.prefs;
import java.io.IOException;
import java.io.OutputStream;
import java.io.FileOutputStream;
import java.io.BufferedOutputStream;
import java.io.InputStream;
import java.io.FileInputStream;
import java.io.BufferedInputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.prefs.Preferences;
import java.util.prefs.BackingStoreException;
import java.util.prefs.InvalidPreferencesFormatException;
/**
* Example class experimenting with new Preferences API from JDK 1.4
*
* @author <a href="mailto:###@###.###">Dominique Devienne</a>
* @version Dev 2001 - Copyright (c) 2001, Landmark Graphics Corp.
*/
public class PreferencesExample {
private static final String CN = PreferencesExample.class.getName();
// Obtain a suitable logger.
private static Logger LOG = Logger.getLogger(CN);
/**
* Main entry point.
*/
public static void main(String[] args)
throws BackingStoreException,
InvalidPreferencesFormatException,
IOException {
LOG.entering(CN, "main");
LOG.fine("Doing stuff in class "+CN);
if (args.length!=1) {
LOG.severe("Invalid command line");
LOG.exiting(CN, "main", new Integer(1));
System.exit(1);
}
if (args[0].equals("-list")) {
list(Preferences.systemRoot());
list(Preferences.userRoot());
list(preferences());
}
else if (args[0].equals("-clear")) {
preferences().clear();
}
else if (args[0].equals("-removeNode")) {
preferences().removeNode();
}
else if (args[0].equals("-add")) {
preferences().put("EXTRA", "extra value");
}
else if (args[0].equals("-rem")) {
preferences().remove("EXTRA");
}
else if (args[0].equals("-put")) {
put();
}
else if (args[0].equals("-get")) {
get();
}
else if (args[0].equals("-import")) {
importPrefs(preferences());
}
else if (args[0].equals("-export")) {
exportPrefs(preferences(), true);
}
else {
LOG.severe("Unknown command line switch "+args[0]);
LOG.exiting(CN, "main", new Integer(2));
System.exit(2);
}
LOG.fine("done!");
LOG.exiting(CN, "main", null);
}
static Preferences preferences() {
LOG.entering(CN, "preferences");
Class self = PreferencesExample.class;
Preferences prefs = Preferences.userNodeForPackage(self);
assert prefs != null;
LOG.exiting(CN, "preferences");
return prefs;
}
public static void put() {
LOG.entering(CN, "put");
Preferences prefs = preferences();
prefs.put("string - 1", "aString");
prefs.put("string - 2", "two");
prefs.putBoolean("boolean - true", true);
prefs.putBoolean("boolean - false", false);
prefs.putInt("int - -1", -1);
prefs.putInt("int - 0", 0);
prefs.putInt("int - 9999", 9999);
prefs.putLong("long - MIN", Long.MIN_VALUE);
prefs.putLong("long - 0", 0);
prefs.putLong("long - MAX", Long.MAX_VALUE);
prefs.putFloat("float - PI", 3.14159267f);
prefs.putDouble("double - PI", Math.PI);
LOG.exiting(CN, "put", null);
}
public static void get() {
LOG.exiting(CN, "get", null);
Preferences prefs = preferences();
String name = null;
String s1 = prefs.get(name = "string - 1", null);
LOG.info(name+"="+s1);
String s2 = prefs.get(name = "string - 2", null);
LOG.info(name+"="+s2);
boolean b1 = prefs.getBoolean(name = "boolean - true", false);
LOG.info(name+"="+b1);
boolean b2 = prefs.getBoolean(name = "boolean - false", true);
LOG.info(name+"="+b2);
int i1 = prefs.getInt(name = "int - -1", -9);
LOG.info(name+"="+i1);
int i2 = prefs.getInt(name = "int - 0", -9);
LOG.info(name+"="+i2);
int i3 = prefs.getInt(name = "int - 9999", -9);
LOG.info(name+"="+i3);
long l1 = prefs.getLong(name = "long - MIN", -9);
LOG.info(name+"="+l1);
long l2 = prefs.getLong(name = "long - 0", -9);
LOG.info(name+"="+l2);
long l3 = prefs.getLong(name = "long - MAX", -9);
LOG.info(name+"="+l3);
float f1 = prefs.getFloat(name = "float - PI", 0.0f);
LOG.info(name+"="+f1);
double d1 = prefs.getDouble(name = "double - PI", 0.0);
LOG.info(name+"="+d1);
LOG.exiting(CN, "get", null);
}
public static void list(Preferences prefs)
throws BackingStoreException {
LOG.entering(CN, "list");
String[] keyes = prefs.keys();
assert keyes != null; // 0-length array if no keyes!
LOG.info("~~~~~~~~~~");
LOG.info(prefs+" has "+keyes.length+" values");
for (int i=0; i<keyes.length; ++i) {
String name = keyes[i];
String value = prefs.get(name, null);
LOG.info("\t"+name+"="+value);
}
String[] children = prefs.childrenNames();
assert children != null; // 0-length array if no children!
LOG.info(prefs+" has "+children.length+" children");
for (int i=0; i<children.length; ++i) {
String name = children[i];
LOG.info("\t"+name);
}
LOG.exiting(CN, "list", null);
}
public static void exportPrefs(Preferences prefs, boolean recurse)
throws BackingStoreException,
IOException {
OutputStream toClose = null; // ensure we always close the right class
try {
FileOutputStream fos = new FileOutputStream(prefs.name()+".xml");
toClose = fos;
BufferedOutputStream bos = new BufferedOutputStream(fos);
toClose = bos;
if (recurse) {
prefs.exportSubtree(bos);
}
else {
prefs.exportNode(bos);
}
}
finally {
if (toClose != null) {
toClose.close();
}
}
}
public static void importPrefs(Preferences prefs)
throws BackingStoreException,
InvalidPreferencesFormatException,
IOException {
InputStream toClose = null; // ensure we always close the right class
try {
FileInputStream fis = new FileInputStream(prefs.name()+".xml");
toClose = fis;
BufferedInputStream bis = new BufferedInputStream(fis);
toClose = bis;
Preferences.importPreferences(bis);
}
finally {
if (toClose != null) {
toClose.close();
}
}
}
} // END class PreferencesExample
(Review ID: 136838)
======================================================================