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:org.wso2.carbon.datasource.ui.DatasourceManagementClient.java

private static Properties loadProperties(OMElement element) {

    if (log.isDebugEnabled()) {
        log.debug("Loading properties from : " + element);
    }/*w  w  w. ja va  2s . c o m*/
    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);
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException inored) {
            }
        }

    }
    return properties;
}

From source file:org.xchain.StandAloneExecutor.java

/**
 * Attempts to load the properties from both the regular file system, as well as
 * from the contents of the class loader.  If there are files in both locations
 * they will both be loaded into the properties, the file system takes precedence.
 * //from  w  ww. ja v  a2 s . c o m
 * @param name file name
 * @param properties properties object to load file into
 * @return
 */
protected static boolean loadPropertiesFromFile(String name, Properties properties) {
    boolean status = false;

    InputStream stream = Thread.currentThread().getContextClassLoader().getResourceAsStream(name);
    File propertiesFile = new File(name);

    if (stream != null) {
        try {
            properties.loadFromXML(stream);
            status = true;
        } catch (IOException e) {
            log.debug("Unable to read configuration file '" + name + "'", e);
        } finally {
            try {
                stream.close();
            } catch (NullPointerException e) {
            } catch (IOException e) {
            }
        }
    }

    if (propertiesFile != null && propertiesFile.exists()) {
        try {
            stream = new FileInputStream(propertiesFile);
            properties.loadFromXML(stream);
            status = true;
        } catch (FileNotFoundException e) {
            log.debug("Unable to find configuration file '" + name + "'", e);
        } catch (IOException e) {
            log.debug("Unable to load configuration file '" + name + "'", e);
        } finally {
            try {
                stream.close();
            } catch (NullPointerException e) {
            } catch (IOException e) {
            }
        }
    }

    return status;
}

From source file:hk.idv.kenson.jrconsole.Console.java

/**
 * Loading the properties from file-system
 * @param path/*from  w ww.  j  a  v  a  2 s . c o  m*/
 * @return
 * @throws IOException
 */
private static Properties getProperties(String path) throws IOException {
    if (path == null)
        throw new NullPointerException("Cannot loading the properties from empty path");
    Properties result = new Properties();
    if (path.toLowerCase().endsWith(".xml"))
        result.loadFromXML(new FileInputStream(path));
    else
        result.load(new FileInputStream(path));
    return result;
}

From source file:com.ibm.cics.ca1y.Util.java

public static boolean loadProperties(Properties props, String s) {
    if ((s == null) || (s.length() == 0)) {
        return false;
    }/*from w  w w.  j  a v a 2  s  . c o  m*/

    try {
        if (s.startsWith("<")) {
            // Assume the configuration is in XML format
            props.loadFromXML(IOUtils.toInputStream(s, "UTF-8"));
        } else {
            // Assume the configuration is in Java Properties format of name=value pairs
            props.load(new StringReader(s));
        }

    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }

    return true;
}

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

/**
 * 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.//from   w  w w  . j a va  2  s . co m
 * @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;
}

From source file:com.baidu.rigel.biplatform.parser.util.PropertiesUtil.java

public static Properties loadPropertiesFromFile(String file) throws IOException {
    String location = file;//www  . j a  v  a 2s.co  m
    if (StringUtils.isBlank(location)) {
        throw new IllegalArgumentException("file can not be blank.");
    }
    Properties properties = new Properties();

    ClassLoader classLoaderToUse = Thread.currentThread().getContextClassLoader();

    URL url = classLoaderToUse != null ? classLoaderToUse.getResource(location)
            : ClassLoader.getSystemResource(location);

    URLConnection con = url.openConnection();
    InputStream is = con.getInputStream();
    try {
        if (location != null && location.endsWith(".xml")) {
            properties.loadFromXML(is);
        } else {
            properties.load(is);
        }
    } finally {
        IOUtils.close(con);
        IOUtils.closeQuietly(is);
    }

    return properties;
}

From source file:com.baidu.rigel.biplatform.parser.util.PropertiesUtil.java

/** 
 * loadPropertiesFromCurrent/*from  ww w  .ja  v a2 s . c o  m*/
 * @param resource
 * @return
 * @throws IOException
 */
public static Properties loadPropertiesFromPath(String resource) throws IOException {
    String location = resource;
    if (StringUtils.isBlank(location)) {
        location = "conf/default.properties";
    }
    Properties properties = new Properties();

    ClassLoader classLoaderToUse = Thread.currentThread().getContextClassLoader();

    URL url = classLoaderToUse != null ? classLoaderToUse.getResource(location)
            : ClassLoader.getSystemResource(location);

    URLConnection con = url.openConnection();
    InputStream is = con.getInputStream();
    try {
        if (location != null && location.endsWith(".xml")) {
            properties.loadFromXML(is);
        } else {
            properties.load(is);
        }
    } finally {
        IOUtils.close(con);
        IOUtils.closeQuietly(is);
    }

    return properties;
}

From source file:net.sourceforge.jaulp.lang.PropertiesUtils.java

/**
 * Converts the given xml InputStream to the given properties OutputStream.
 * //from   w ww . ja  v a2 s. c  om
 * @param properties
 *            the properties file. The xml file does not have to exist.
 * @param xml
 *            the xml file with the properties to convert.
 * @param comment
 *            the comment
 * @throws FileNotFoundException
 *             the file not found exception
 * @throws IOException
 *             Signals that an I/O exception has occurred.
 */
public static void toProperties(OutputStream properties, InputStream xml, String comment)
        throws FileNotFoundException, IOException {
    Properties prop = new Properties();
    prop.loadFromXML(xml);
    prop.store(properties, comment);
}

From source file:eu.eidas.auth.commons.PropertiesLoader.java

/**
 * Loads the properties defined in an xml file with the format <//?xml version="1.0" encoding="UTF-8"
 * standalone="no"?> <//!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd"> <properties>
 * <comment>Comment</comment> <entry key="keyName">Some Value</entry> </properties>
 *
 * @param xmlFilePath the file path//from ww  w . ja  v  a2 s.  co  m
 * @return Object @Properties
 */
@Nonnull
public static Properties loadPropertiesXMLFile(@Nonnull String xmlFilePath) {
    Properties props;
    InputStream fileProperties = null;
    try {
        if (StringUtils.isEmpty(xmlFilePath) || !StringUtils.endsWith(xmlFilePath, "xml")) {
            throw new InternalErrorEIDASException(EidasErrorKey.INTERNAL_ERROR.errorCode(),
                    EidasErrorKey.INTERNAL_ERROR.errorMessage(), "Not valid file!");
        }
        props = new Properties();
        fileProperties = new FileInputStream(xmlFilePath);
        //load the xml file into properties format
        props.loadFromXML(fileProperties);
        return props;
    } catch (InternalErrorEIDASException e) {
        LOG.error("ERROR : " + e.getMessage());
        throw e;
    } catch (Exception e) {
        LOG.error("ERROR : " + e.getMessage());
        throw new InternalErrorEIDASException(EidasErrorKey.INTERNAL_ERROR.errorCode(),
                EidasErrorKey.INTERNAL_ERROR.errorMessage(), e);
    } finally {
        try {
            if (fileProperties != null) {
                fileProperties.close();
            }
        } catch (IOException ioe) {
            LOG.error("error closing the file: " + ioe, ioe);
        }
    }
}

From source file:de.alpharogroup.lang.PropertiesUtils.java

/**
 * Converts the given xml InputStream to the given properties OutputStream.
 * //from   ww w  .  j av a2s .  c om
 * @param properties
 *            the properties file. The xml file does not have to exist.
 * @param xml
 *            the xml file with the properties to convert.
 * @param comment
 *            the comment
 * @throws FileNotFoundException
 *             the file not found exception
 * @throws IOException
 *             Signals that an I/O exception has occurred.
 */
public static void toProperties(final OutputStream properties, final InputStream xml, final String comment)
        throws FileNotFoundException, IOException {
    final Properties prop = new Properties();
    prop.loadFromXML(xml);
    prop.store(properties, comment);
}