下面列出了java.util.Properties#loadFromXML ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public Void call() throws IOException {
while (!done) {
// store as XML format
ByteArrayOutputStream out = new ByteArrayOutputStream();
props.storeToXML(out, null, "UTF-8");
// load from XML format
Properties p = new Properties();
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
p.loadFromXML(in);
// check that the properties are as expected
if (!p.equals(props))
throw new RuntimeException("Properties not equal");
}
return null;
}
private PluginConfigurator loadConfig1() {
Properties config = new Properties();
if (new File(CONFIG_FILENAME).exists()) {
try {
FileInputStream input = new FileInputStream(CONFIG_FILENAME);
try {
config.loadFromXML(input);
} finally {
input.close();
}
} catch (IOException e) {
log.error("Unable to load configuration file " + CONFIG_FILENAME, e);
}
log.debug("Configuration loaded: " + config);
}
PluginConfigurator configurator = new PluginConfigurator(config);
configurator.configureAll(null);
return configurator;
}
public Void call() throws IOException {
while (!done) {
// store as XML format
ByteArrayOutputStream out = new ByteArrayOutputStream();
props.storeToXML(out, null, "UTF-8");
// load from XML format
Properties p = new Properties();
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
p.loadFromXML(in);
// check that the properties are as expected
if (!p.equals(props))
throw new RuntimeException("Properties not equal");
}
return null;
}
public Void call() throws IOException {
while (!done) {
// store as XML format
ByteArrayOutputStream out = new ByteArrayOutputStream();
props.storeToXML(out, null, "UTF-8");
// load from XML format
Properties p = new Properties();
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
p.loadFromXML(in);
// check that the properties are as expected
if (!p.equals(props))
throw new RuntimeException("Properties not equal");
}
return null;
}
protected Map<String, String> getAttributes(final TarArchiveInputStream stream) throws IOException {
final Properties props = new Properties();
props.loadFromXML(new NonCloseableInputStream(stream));
final Map<String, String> result = new HashMap<>();
for (final Entry<Object, Object> entry : props.entrySet()) {
final Object keyObject = entry.getKey();
final Object valueObject = entry.getValue();
if (!(keyObject instanceof String)) {
throw new IOException("Flow file attributes object contains key of type "
+ keyObject.getClass().getCanonicalName()
+ " but expected java.lang.String");
} else if (!(keyObject instanceof String)) {
throw new IOException("Flow file attributes object contains value of type "
+ keyObject.getClass().getCanonicalName()
+ " but expected java.lang.String");
}
final String key = (String) keyObject;
final String value = (String) valueObject;
result.put(key, value);
}
return result;
}
public Void call() throws IOException {
while (!done) {
// store as XML format
ByteArrayOutputStream out = new ByteArrayOutputStream();
props.storeToXML(out, null, "UTF-8");
// load from XML format
Properties p = new Properties();
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
p.loadFromXML(in);
// check that the properties are as expected
if (!p.equals(props))
throw new RuntimeException("Properties not equal");
}
return null;
}
public Void call() throws IOException {
while (!done) {
// store as XML format
ByteArrayOutputStream out = new ByteArrayOutputStream();
props.storeToXML(out, null, "UTF-8");
// load from XML format
Properties p = new Properties();
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
p.loadFromXML(in);
// check that the properties are as expected
if (!p.equals(props))
throw new RuntimeException("Properties not equal");
}
return null;
}
/**
* <p>The specified stream remains open after this method returns.
* @param in
* @param systemId
* @throws IOException
*/
public void loadProperties(@NotNull InputStream in, @NotNull String systemId)
throws IOException {
Properties props = new Properties();
// prevent the input stream from being closed for achieving a consistent behaviour
props.loadFromXML(new CloseShieldInputStream(in));
setProperties(props);
log.trace("Loaded properties from {}.", systemId);
}
static void loadPropertyFile(String filename) {
try (InputStream in = new FileInputStream(filename)) {
Properties prop = new Properties();
prop.loadFromXML(in);
verifyProperites(prop);
} catch (IOException ex) {
fail(ex.getMessage());
}
}
/**
* Load all properties from the specified class path resource
* (in ISO-8859-1 encoding), using the given class loader.
* <p>Merges properties if more than one resource of the same name
* found in the class path.
* @param resourceName the name of the class path resource
* @param classLoader the ClassLoader to use for loading
* (or {@code null} to use the default class loader)
* @return the populated Properties instance
* @throws IOException if loading failed
*/
public static Properties loadAllProperties(String resourceName, ClassLoader classLoader) throws IOException {
Assert.notNull(resourceName, "Resource name must not be null");
ClassLoader classLoaderToUse = classLoader;
if (classLoaderToUse == null) {
classLoaderToUse = ClassUtils.getDefaultClassLoader();
}
Enumeration<URL> urls = (classLoaderToUse != null ? classLoaderToUse.getResources(resourceName) :
ClassLoader.getSystemResources(resourceName));
Properties props = new Properties();
while (urls.hasMoreElements()) {
URL url = urls.nextElement();
URLConnection con = url.openConnection();
ResourceUtils.useCachesIfNecessary(con);
InputStream is = con.getInputStream();
try {
if (resourceName != null && resourceName.endsWith(XML_FILE_EXTENSION)) {
props.loadFromXML(is);
}
else {
props.load(is);
}
}
finally {
is.close();
}
}
return props;
}
@Override
public Properties loadFromXML(String xml, Properties defaults)
throws IOException {
final ByteArrayInputStream bais = new ByteArrayInputStream(xml.getBytes("UTF-8"));
Properties p = new Properties(defaults);
p.loadFromXML(bais);
return p;
}
public void loadProperties(File f, boolean system, boolean forceNew) {
if (f == null || !f.exists()) {
alert("[" + f.getAbsolutePath() + "] " + getResource("error.not.exists", "is not exists."));
return;
}
BufferedInputStream bin = null;
try {
Properties props = new Properties();
if (system) {
props.load(bin = new BufferedInputStream(new FileInputStream(f)));
if (IOSImageUtil.isExecutableJarFile(this)) {
this.setCheckForUpdatesOnStartUp(IOSImageUtil.getBoolProperty(props, "system.check.for.updates.on.startup", true));
}
if (forceNew) {
props.put("system.last.properties.file", "");
}
} else {
props.loadFromXML(bin = new BufferedInputStream(new FileInputStream(f)));
}
this.applyProperties(props, system);
if (system) {
applySystemProperties(props);
} else {
this.setPropertiesFile(f);
}
} catch (Throwable t) {
handleThrowable(t);
} finally {
if (bin != null) {
try { bin.close(); } catch (Exception ex) { ex.printStackTrace(); }
}
}
}
/**
* Load the properties for external utilities from a XML file
*/
private void loadExternal() {
Properties properties = new Properties();
try {
properties.loadFromXML(new FileInputStream(PROPERTIES_PATH));
for (String key : properties.stringPropertyNames()) {
externalSupport.put(key, properties.getProperty(key));
}
} catch (IOException ignore) {
}
}
static void loadPropertyFile(String filename) {
try (InputStream in = new FileInputStream(filename)) {
Properties prop = new Properties();
prop.loadFromXML(in);
verifyProperites(prop);
} catch (IOException ex) {
fail(ex.getMessage());
}
}
@Test
public void givenPropertyValue_whenXMLPropertiesFileLoaded_thenCorrect() throws IOException {
String rootPath = Thread.currentThread().getContextClassLoader().getResource("").getPath();
String iconConfigPath = rootPath + "icons.xml";
Properties iconProps = new Properties();
iconProps.loadFromXML(new FileInputStream(iconConfigPath));
assertEquals("icon1.jpg", iconProps.getProperty("fileIcon"));
}
static void loadPropertyFile(String filename) {
try (InputStream in = new FileInputStream(filename)) {
Properties prop = new Properties();
prop.loadFromXML(in);
verifyProperites(prop);
} catch (IOException ex) {
fail(ex.getMessage());
}
}
Properties convertXMLToProperties(String xmlString) {
Properties properties = new Properties();
try (InputStream in = new ByteArrayInputStream(xmlString.getBytes())) {
properties.loadFromXML(in);
} catch (IOException e) {
LOGGER.logError(Log4jLogger.SYSTEM_LOG, e,
LogMessageIdentifier.ERROR);
}
return properties;
}
@Override
public Properties loadFromXML(String xml, Properties defaults)
throws IOException {
final ByteArrayInputStream bais = new ByteArrayInputStream(xml.getBytes("UTF-8"));
Properties p = new Properties(defaults);
p.loadFromXML(bais);
return p;
}
/**
* Decompress a resource.
* @param order Byte order.
* @param provider Strings provider
* @param content The resource content to uncompress.
* @return A fully uncompressed resource.
* @throws IOException
*/
public byte[] decompressResource(ByteOrder order, StringsProvider provider,
byte[] content) throws IOException {
Objects.requireNonNull(order);
Objects.requireNonNull(provider);
Objects.requireNonNull(content);
CompressedResourceHeader header;
do {
header = CompressedResourceHeader.readFromResource(order, content);
if (header != null) {
ResourceDecompressor decompressor =
pluginsCache.get(header.getDecompressorNameOffset());
if (decompressor == null) {
String pluginName =
provider.getString(header.getDecompressorNameOffset());
if (pluginName == null) {
throw new IOException("Plugin name not found");
}
String storedContent = header.getStoredContent(provider);
Properties props = new Properties();
if (storedContent != null) {
try (ByteArrayInputStream stream
= new ByteArrayInputStream(storedContent.
getBytes(StandardCharsets.UTF_8));) {
props.loadFromXML(stream);
}
}
decompressor = ResourceDecompressorRepository.
newResourceDecompressor(props, pluginName);
if (decompressor == null) {
throw new IOException("Plugin not found: " + pluginName);
}
pluginsCache.put(header.getDecompressorNameOffset(), decompressor);
}
try {
content = decompressor.decompress(provider, content,
CompressedResourceHeader.getSize(), header.getUncompressedSize());
} catch (Exception ex) {
throw new IOException(ex);
}
}
} while (header != null);
return content;
}
/**
* Create a new instance and load the translations from the given input
* stream.
*/
XMLResourceBundle(final InputStream stream) throws IOException {
props = new Properties();
props.loadFromXML(stream);
}