下面列出了怎么用java.util.prefs.InvalidPreferencesFormatException的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* Import preferences from the specified input stream, which is assumed to
* contain an XML document in the format described in the Preferences spec.
*
* @throws IOException
* if reading from the specified output stream results in an
* <tt>IOException</tt>.
* @throws InvalidPreferencesFormatException
* Data on input stream does not constitute a valid XML document
* with the mandated document type.
*/
static void importPreferences(InputStream is) throws IOException, InvalidPreferencesFormatException {
try {
Document doc = loadPrefsDoc(is);
String xmlVersion = doc.getDocumentElement().getAttribute("EXTERNAL_XML_VERSION");
if (xmlVersion.compareTo(EXTERNAL_XML_VERSION) > 0)
throw new InvalidPreferencesFormatException("Exported preferences file format version " + xmlVersion
+ " is not supported. This java installation can read" + " versions " + EXTERNAL_XML_VERSION
+ " or older. You may need" + " to install a newer version of JDK.");
Element xmlRoot = (Element) doc.getDocumentElement().getChildNodes().item(0);
Preferences prefsRoot = (xmlRoot.getAttribute("type").equals("user") ? Preferences.userRoot()
: Preferences.systemRoot());
ImportSubtree(prefsRoot, xmlRoot);
} catch (SAXException | BackingStoreException e) {
throw new InvalidPreferencesFormatException(e);
}
}
/**
* Import Map from the specified input stream, which is assumed to contain a map
* document as per the prefs DTD. This is used as the internal (undocumented)
* format for FileSystemPrefs. The key-value pairs specified in the XML document
* will be put into the specified Map. (If this Map is empty, it will contain
* exactly the key-value pairs int the XML-document when this method returns.)
*
* @throws IOException
* if reading from the specified output stream results in an
* <tt>IOException</tt>.
* @throws InvalidPreferencesFormatException
* Data on input stream does not constitute a valid XML document
* with the mandated document type.
*/
static void importMap(InputStream is, Map m) throws IOException, InvalidPreferencesFormatException {
try {
Document doc = loadPrefsDoc(is);
Element xmlMap = doc.getDocumentElement();
// check version
String mapVersion = xmlMap.getAttribute("MAP_XML_VERSION");
if (mapVersion.compareTo(MAP_XML_VERSION) > 0)
throw new InvalidPreferencesFormatException("Preferences map file format version " + mapVersion
+ " is not supported. This java installation can read" + " versions " + MAP_XML_VERSION
+ " or older. You may need" + " to install a newer version of JDK.");
NodeList entries = xmlMap.getChildNodes();
for (int i = 0, numEntries = entries.getLength(); i < numEntries; i++) {
Element entry = (Element) entries.item(i);
m.put(entry.getAttribute("key"), entry.getAttribute("value"));
}
} catch (SAXException e) {
throw new InvalidPreferencesFormatException(e);
}
}
static void importPreferences(InputStream is)
throws IOException, InvalidPreferencesFormatException
{
try {
Document doc = loadPrefsDoc(is);
String xmlVersion =
doc.getDocumentElement().getAttribute("EXTERNAL_XML_VERSION");
if (xmlVersion.compareTo(EXTERNAL_XML_VERSION) > 0)
throw new InvalidPreferencesFormatException(
"Exported preferences file format version " + xmlVersion +
" is not supported. This java installation can read" +
" versions " + EXTERNAL_XML_VERSION + " or older. You may need" +
" to install a newer version of JDK.");
Element xmlRoot = (Element) doc.getDocumentElement().
getChildNodes().item(0);
Preferences prefsRoot =
(xmlRoot.getAttribute("type").equals("user") ?
Preferences.userRoot() : Preferences.systemRoot());
ImportSubtree(prefsRoot, xmlRoot);
} catch(SAXException e) {
throw new InvalidPreferencesFormatException(e);
}
}
static void importMap(InputStream is, Map m)
throws IOException, InvalidPreferencesFormatException
{
try {
Document doc = loadPrefsDoc(is);
Element xmlMap = doc.getDocumentElement();
String mapVersion = xmlMap.getAttribute("MAP_XML_VERSION");
if (mapVersion.compareTo(MAP_XML_VERSION) > 0)
throw new InvalidPreferencesFormatException(
"Preferences map file format version " + mapVersion +
" is not supported. This java installation can read" +
" versions " + MAP_XML_VERSION + " or older. You may need" +
" to install a newer version of JDK.");
NodeList entries = xmlMap.getChildNodes();
for (int i=0, numEntries=entries.getLength(); i<numEntries; i++) {
Element entry = (Element) entries.item(i);
m.put(entry.getAttribute("key"), entry.getAttribute("value"));
}
} catch(SAXException e) {
throw new InvalidPreferencesFormatException(e);
}
}
static void importPreferences(InputStream is)
throws IOException, InvalidPreferencesFormatException
{
try {
Document doc = loadPrefsDoc(is);
String xmlVersion =
doc.getDocumentElement().getAttribute("EXTERNAL_XML_VERSION");
if (xmlVersion.compareTo(EXTERNAL_XML_VERSION) > 0)
throw new InvalidPreferencesFormatException(
"Exported preferences file format version " + xmlVersion +
" is not supported. This java installation can read" +
" versions " + EXTERNAL_XML_VERSION + " or older. You may need" +
" to install a newer version of JDK.");
Element xmlRoot = (Element) doc.getDocumentElement().
getChildNodes().item(0);
Preferences prefsRoot =
(xmlRoot.getAttribute("type").equals("user") ?
Preferences.userRoot() : Preferences.systemRoot());
ImportSubtree(prefsRoot, xmlRoot);
} catch(SAXException e) {
throw new InvalidPreferencesFormatException(e);
}
}
static void importMap(InputStream is, Map m)
throws IOException, InvalidPreferencesFormatException
{
try {
Document doc = loadPrefsDoc(is);
Element xmlMap = doc.getDocumentElement();
String mapVersion = xmlMap.getAttribute("MAP_XML_VERSION");
if (mapVersion.compareTo(MAP_XML_VERSION) > 0)
throw new InvalidPreferencesFormatException(
"Preferences map file format version " + mapVersion +
" is not supported. This java installation can read" +
" versions " + MAP_XML_VERSION + " or older. You may need" +
" to install a newer version of JDK.");
NodeList entries = xmlMap.getChildNodes();
for (int i=0, numEntries=entries.getLength(); i<numEntries; i++) {
Element entry = (Element) entries.item(i);
m.put(entry.getAttribute("key"), entry.getAttribute("value"));
}
} catch(SAXException e) {
throw new InvalidPreferencesFormatException(e);
}
}
@Test
// @Ignore("This doesn't actually assert that the substitute and export features work.")
public void testExport() throws IOException, BackingStoreException, InvalidPreferencesFormatException {
Preferences userRoot = Preferences.userRoot();
Preferences fromNode = userRoot.node("SemperUbi");
assert fromNode instanceof PreferencesExt : "Factory not set = " + userRoot.getClass().getName();
ByteArrayOutputStream os = new ByteArrayOutputStream(10000);
fromNode.exportNode(os);
String xml = new String(os.toByteArray());
xml = substitute(xml, "SemperUbi", "SubUbi");
ByteArrayInputStream is = new ByteArrayInputStream(xml.getBytes());
Preferences.importPreferences(is);
userRoot.exportSubtree(System.out);
}
public static void applyPreferencesFrom(final String path, final Map<String, Object> modelValues) {
// DEBUG.OUT("Apply preferences from " + path);
try (final FileInputStream is = new FileInputStream(path);) {
store.importPreferences(is);
reloadPreferences(modelValues);
} catch (final IOException | InvalidPreferencesFormatException e) {
e.printStackTrace();
}
}
/**
*
* @throws IOException
* @throws InvalidPreferencesFormatException
*/
public static void InitializeConfiguration() throws IOException, InvalidPreferencesFormatException {
myPreferences.put("URL_EARTHTIMEORG", URL_EARTHTIMEORG);
myPreferences.put("URI_UPB_PUBLIC_DATA", URI_UPB_PUBLIC_DATA);
myPreferences.put("URI_AliquotXMLSchema", URI_AliquotXMLSchema);
myPreferences.put("URI_AliquotUThXMLSchema", URI_AliquotUThXMLSchema);
myPreferences.put("URI_UPbReduxFractionXMLSchemaURL", URI_UPbReduxFractionXMLSchemaURL);
myPreferences.put("URI_AnalysisFractionXMLSchemaURL", URI_AnalysisFractionXMLSchemaURL);
myPreferences.put("URI_InitialPbModelXMLSchema", URI_InitialPbModelXMLSchema);
myPreferences.put("URI_InitialPbModelETXMLSchema", URI_InitialPbModelETXMLSchema);
myPreferences.put("URI_PbBlankXMLSchema", URI_PbBlankXMLSchema);
myPreferences.put("URI_PbBlankICModelXMLSchema", URI_PbBlankICModelXMLSchema);
myPreferences.put("URI_MineralStandardModelXMLSchemaURL", URI_MineralStandardModelXMLSchemaURL);
myPreferences.put("URI_MineralStandardUPbModelXMLSchemaURL", URI_MineralStandardUPbModelXMLSchemaURL);
myPreferences.put("URI_PhysicalConstantsXMLSchema", URI_PhysicalConstantsXMLSchema);
myPreferences.put("URI_PhysicalConstantsModelXMLSchema", URI_PhysicalConstantsModelXMLSchema);
myPreferences.put("URI_TracerXMLSchema", URI_TracerXMLSchema);
myPreferences.put("URI_TracerUPbModelXMLSchema", URI_TracerUPbModelXMLSchema);
myPreferences.put("URI_EARTHTIME_XMLTracers", URI_EARTHTIME_XMLTracers);
myPreferences.put("URI_ValueModelXMLSchema", URI_ValueModelXMLSchema);
myPreferences.put("URI_SampleMetaDataXMLSchema", URI_SampleMetaDataXMLSchema);
myPreferences.put("URI_ReportSettingsXMLSchema", URI_ReportSettingsXMLSchema);
myPreferences.put("URI_ReduxMatrixXMLSchemaURL", URI_ReduxMatrixXMLSchemaURL);
myPreferences.put("URI_DetritalUraniumAndThoriumModelXMLSchema", URI_DetritalUraniumAndThoriumModelXMLSchema);
}