Example usage for java.util Properties loadFromXML

List of usage examples for java.util Properties loadFromXML

Introduction

In this page you can find the example usage for java.util Properties loadFromXML.

Prototype

public synchronized void loadFromXML(InputStream in) throws IOException, InvalidPropertiesFormatException 

Source Link

Document

Loads all of the properties represented by the XML document on the specified input stream into this properties table.

Usage

From source file:com.squid.kraken.v4.auth.KrakenClientConfig.java

private static void load(InputStream in, Properties target) {
    Properties properties = new Properties();
    try {/*w  ww .  j a v a 2  s  .co  m*/
        properties.loadFromXML(in);
        for (Object key : properties.keySet()) {
            Object value = properties.get(key);
            target.put(key, value);
            logger.debug(key + ":" + value);
        }
    } catch (Exception e) {
        logger.warn("Could not load Kraken config file for stream : " + in, e);
    }

}

From source file:org.exoplatform.platform.common.software.register.Utils.java

public static String readFromFile(String key, String fileLocation) {
    if (fileLocation == null || fileLocation.isEmpty() || !new File(fileLocation).exists()) {
        throw new IllegalArgumentException("Illegal file Location parameter: " + fileLocation);
    }//from  ww w .  j a  v a  2s. co m
    try {
        Properties properties = new Properties();
        InputStream inputStream = new FileInputStream(fileLocation);
        properties.loadFromXML(inputStream);
        inputStream.close();
        return (String) properties.get(key);
    } catch (Exception exception) {
        throw new RuntimeException(exception);
    }
}

From source file:org.duracloud.common.util.ApplicationConfig.java

public static Properties getPropsFromXmlResource(String resourceName) {
    Properties props = new Properties();
    AutoCloseInputStream in = new AutoCloseInputStream(
            ApplicationConfig.class.getClassLoader().getResourceAsStream(resourceName));
    try {/*  w w w  .  java2  s.c o  m*/
        props.loadFromXML(in);
    } catch (Exception e) {
        String error = "Unable to find resource: '" + resourceName + "': " + e.getMessage();
        throw new RuntimeException(error);
    }
    return props;
}

From source file:org.capatect.restatic.core.model.ResLocale.java

private static Properties loadProperties(final File resourceBundle) {
    BufferedInputStream bis = null;
    try {/* w  ww  .jav a 2 s  . c om*/
        bis = new BufferedInputStream(new FileInputStream(resourceBundle));
        Properties properties = new Properties();
        if (isXmlResourceBundle(resourceBundle)) {
            properties.loadFromXML(bis);
        } else {
            properties.load(bis);
        }
        return properties;
    } catch (FileNotFoundException e) {
        throw new ParseException(String.format("Parsing of %s failed.", resourceBundle.getAbsolutePath()), e,
                resourceBundle.getAbsolutePath());
    } catch (IOException e) {
        throw new ParseException(String.format("Parsing of %s failed.", resourceBundle.getAbsolutePath()), e,
                resourceBundle.getAbsolutePath());
    } finally {
        closeInputStream(bis);
    }
}

From source file:ro.agrade.jira.qanda.utils.ResourceUtils.java

/**
 * Gets the configuration resource, as properties file. You should read the
 * corresponding standard documentation, and check the DTD found at:
 * <pre>/*from   www.  ja v  a  2s. co m*/
 * &lt;!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd"&gt;
 * </pre>
 * 
 * @return the properties
 * @param resFileName file path, URL, or internal resource denominator.
 * @param loaderClazz the loader class
 * @throws java.util.InvalidPropertiesFormatException if the XML format is 
 * invalid
 * @throws IOException if the properties cannot be loaded, for whatever 
 * reason
 */
public static Properties getAsXMLProperties(String resFileName, Class<?> loaderClazz) throws IOException {
    InputStream is = null;

    try {
        is = getAsInputStream(resFileName, loaderClazz);
        Properties p = new Properties();
        p.loadFromXML(is);
        return p;
    } finally {
        if (is != null)
            try {
                is.close();
            } catch (IOException ex) {
            }
    }
}

From source file:eionet.cr.harvest.util.MediaTypeToDcmiTypeConverter.java

/**
 * Loads the mimetypes from the file and puts them into mimeToRdfMap.
 *///from w  w  w  .  j a v a  2 s .  c  om
private static void initialize() {

    mappings = new LinkedHashMap<String, String>();

    InputStream inputStream = null;
    Properties properties = new Properties();
    try {
        inputStream = MediaTypeToDcmiTypeConverter.class.getClassLoader()
                .getResourceAsStream(MAPPINGS_FILENAME);
        properties.loadFromXML(inputStream);
    } catch (IOException e) {
        LOGGER.error("Failed to load XML-formatted properties from " + MAPPINGS_FILENAME, e);
    } finally {
        if (inputStream != null) {
            IOUtils.closeQuietly(inputStream);
        }
    }

    if (!properties.isEmpty()) {

        for (Map.Entry entry : properties.entrySet()) {

            String rdfType = entry.getKey().toString();
            String[] mediaTypes = entry.getValue().toString().split("\\s+");

            if (!StringUtils.isBlank(rdfType) && mediaTypes != null && mediaTypes.length > 0) {

                for (int i = 0; i < mediaTypes.length; i++) {

                    if (!StringUtils.isBlank(mediaTypes[i])) {

                        mappings.put(mediaTypes[i].trim(), rdfType.trim());
                    }
                }
            }
        }
    }
}

From source file:org.oscarehr.util.MiscUtils.java

License:asdf

public static Properties xmlByteArrayToProperties(byte[] b) throws IOException {
    Properties p = new Properties();

    ByteArrayInputStream is = new ByteArrayInputStream(b);
    p.loadFromXML(is);

    return (p);//from  w w w.j  a  va  2s . c o  m
}

From source file:org.exoplatform.platform.common.software.register.Utils.java

public static void writeToFile(String key, String value, String fileLocation) {
    if (fileLocation == null || fileLocation.isEmpty()) {
        throw new IllegalArgumentException("Illegal empty file Location parameter.");
    }/*w  w w .  j a  v a 2  s  .c  om*/
    InputStream inputStream = null;
    OutputStream outputStream = null;
    try {
        Properties properties = new Properties();
        File file = new File(fileLocation);
        if (file.exists()) {
            inputStream = new FileInputStream(fileLocation);
            properties.loadFromXML(inputStream);
            inputStream.close();
        } else {
            verifyAndCreateParentFolder(fileLocation);
        }
        properties.put(key, value);
        outputStream = new FileOutputStream(fileLocation);
        properties.storeToXML(outputStream, "");
        outputStream.close();
    } catch (Exception exception) {
        if (outputStream != null) {
            try {
                outputStream.close();
            } catch (IOException ioException) {
                LOG.error("Error during close outputStream ", ioException);
            }
        }
        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (IOException ioException) {
                LOG.error("Error during close inputStream ", ioException);
            }
        }
    }
}

From source file:org.wso2.carbon.datasource.MiscellaneousHelper.java

public static Properties loadProperties(OMElement element) {

    if (log.isDebugEnabled()) {
        log.debug("Loading properties from : " + element);
    }/*from  w  w w .  j a  v a2s.c  om*/
    String xml = "<!DOCTYPE properties   [\n" + "\n" + "<!ELEMENT properties ( comment?, entry* ) >\n" + "\n"
            + "<!ATTLIST properties version CDATA #FIXED \"1.0\">\n" + "\n" + "<!ELEMENT comment (#PCDATA) >\n"
            + "\n" + "<!ELEMENT entry (#PCDATA) >\n" + "\n" + "<!ATTLIST entry key CDATA #REQUIRED>\n" + "]>"
            + element.toString();
    final Properties properties = new Properties();
    InputStream in = null;
    try {
        in = new ByteArrayInputStream(xml.getBytes());
        properties.loadFromXML(in);
        return properties;
    } catch (IOException e) {
        handleException("IOError loading properties from : " + element, e);
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException ignored) {
            }
        }

    }
    return properties;
}

From source file:com.sfxie.extension.spring4.properties.PropertiesLoaderUtils.java

/**
 * Fill the given properties from the given resource (in ISO-8859-1 encoding).
 * @param props the Properties instance to fill
 * @param resource the resource to load from
 * @throws IOException if loading failed
 *//*from www.  j av  a  2 s  .c om*/
public static void fillProperties(Properties props, Resource resource) throws IOException {
    InputStream is = resource.getInputStream();
    try {
        String filename = resource.getFilename();
        if (filename != null && filename.endsWith(XML_FILE_EXTENSION)) {
            props.loadFromXML(is);
        } else {
            props.load(is);
        }
    } finally {
        is.close();
    }
}