Java Code Examples for java.util.Properties#loadFromXML()

The following examples show how to use java.util.Properties#loadFromXML() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ConcurrentLoadAndStoreXML.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
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;
}
 
Example 2
Source File: Main.java    From Gaalop with GNU Lesser General Public License v3.0 6 votes vote down vote up
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;
}
 
Example 3
Source File: ConcurrentLoadAndStoreXML.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
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;
}
 
Example 4
Source File: ConcurrentLoadAndStoreXML.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
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;
}
 
Example 5
Source File: FlowFileUnpackagerV1.java    From nifi with Apache License 2.0 6 votes vote down vote up
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;
    }
 
Example 6
Source File: ConcurrentLoadAndStoreXML.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
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;
}
 
Example 7
Source File: ConcurrentLoadAndStoreXML.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
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;
}
 
Example 8
Source File: CompatibilityTest.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
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());
    }
}
 
Example 9
Source File: LoadAndStoreXMLWithDefaults.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
@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;
}
 
Example 10
Source File: Operation.java    From development with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 11
Source File: CompatibilityTest.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
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());
    }
}
 
Example 12
Source File: PropertiesUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@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"));
}
 
Example 13
Source File: CompatibilityTest.java    From native-obfuscator with GNU General Public License v3.0 5 votes vote down vote up
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());
    }
}
 
Example 14
Source File: UniversalDecompressor.java    From dkpro-jwpl with Apache License 2.0 5 votes vote down vote up
/**
 * 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) {
	}
}
 
Example 15
Source File: MainFrame.java    From ios-image-util with MIT License 5 votes vote down vote up
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(); }
		}
	}
}
 
Example 16
Source File: LoadAndStoreXMLWithDefaults.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
@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;
}
 
Example 17
Source File: PropertiesLoaderUtils.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example 18
Source File: DefaultMetaInf.java    From jackrabbit-filevault with Apache License 2.0 5 votes vote down vote up
/**
 * <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);
}
 
Example 19
Source File: Decompressor.java    From Bytecoder with Apache License 2.0 4 votes vote down vote up
/**
 * 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;
}
 
Example 20
Source File: XMLResourceBundle.java    From sr201 with MIT License 4 votes vote down vote up
/**
 * 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);
}