Example usage for org.apache.commons.configuration XMLConfiguration load

List of usage examples for org.apache.commons.configuration XMLConfiguration load

Introduction

In this page you can find the example usage for org.apache.commons.configuration XMLConfiguration load.

Prototype

private void load(InputSource source) throws ConfigurationException 

Source Link

Document

Loads a configuration file from the specified input source.

Usage

From source file:net.sf.tweety.cli.plugins.CliMain.java

/**
 * This method is meant to load the tweety plugin pathes on startup
 * //from   ww w .  ja  v a2  s .co  m
 * @return an object with one or more pluginpathes
 * @throws ConfigurationException
 */
public static Map<String, String> configCLI() throws ConfigurationException, FileNotFoundException {

    System.out.println("Initialize CLI...");

    // TODO : exception handling for empty/erroneous configuration
    Map<String, String> loadablePlugins = new HashMap<String, String>();

    XMLConfiguration tweetyXmlConfig = new XMLConfiguration();
    File in = new File(TWEETY_CLI_DEFAULT_CONFIG);
    try {
        System.out.print("Loading Configuration...");
        String inPath = in.getAbsolutePath();
        tweetyXmlConfig
                .setBasePath(inPath.substring(0, inPath.length() - TWEETY_CLI_DEFAULT_CONFIG.length() - 1));
        tweetyXmlConfig.load(in);
        System.out.print("success.\n");
    } catch (NullPointerException e) {
        e.printStackTrace();
    }

    // map ueber "plugins.plugin" mit keys ()
    // TODO: Verhalten bei leeren Feldern pruefen
    // TODO: Verhalten bei einem einzelnen Eintrag prfen
    Iterator<String> it = tweetyXmlConfig.getKeys("plugin");

    // // TODO fix the casts!
    // if (it.hasNext()) {
    //
    // String pluginPath = (String) tweetyXmlConfig.getProperty(it.next()
    // .toString());
    //
    // String pluginName = (String) tweetyXmlConfig.getProperty(it.next()
    // .toString());
    //
    // // for (int i = 0; i < pluginPath.size(); i++) {
    // // System.out.println(pluginName.get(i) + pluginPath.get(i));
    // loadablePlugins.put(pluginName, pluginPath);
    // }
    // }
    System.out.print("Getting Plugins...");
    // TODO fix the casts!
    if (it.hasNext()) {
        @SuppressWarnings("unchecked")
        ArrayList<String> pluginPath = (ArrayList<String>) tweetyXmlConfig.getProperty(it.next());
        @SuppressWarnings("unchecked")
        ArrayList<String> pluginName = (ArrayList<String>) tweetyXmlConfig.getProperty(it.next());

        for (int i = 0; i < pluginPath.size(); i++) {
            // System.out.println(pluginName.get(i) + pluginPath.get(i));
            loadablePlugins.put(pluginName.get(i), pluginPath.get(i));
        }
    }
    System.out.print("done.\n");
    System.out.println("CLI initialized");
    return loadablePlugins;
}

From source file:com.example.TestClass.java

@Test
public void testWithNoComma() throws Exception {
    String source = "<configuration><key0></key0><key1></key1><key2></key2><key3></key3></configuration>";
    XMLConfiguration config = new XMLConfiguration();

    config.load(new StringReader(source));
    checkConfiguration(config);/*w  w  w .  j  a v a  2 s.co m*/
}

From source file:com.example.TestClass.java

@Test
public void testWithOnlyComma() throws Exception {
    String source = "<configuration><key0></key0><key1>,</key1><key2></key2><key3></key3></configuration>";
    XMLConfiguration config = new XMLConfiguration();

    config.load(new StringReader(source));
    checkConfiguration(config);/*  ww  w. java 2s .  co m*/
}

From source file:com.example.TestClass.java

@Test
public void testWithCommaSeparatedList() throws Exception {
    String source = "<configuration><key0></key0><key1>a,b</key1><key2></key2><key3></key3></configuration>";
    XMLConfiguration config = new XMLConfiguration();

    config.load(new StringReader(source));
    checkConfiguration(config);/*ww w  .j  a va 2  s . c  o  m*/
}

From source file:com.example.TestClass.java

@Test
public void testWithSeparatingWhitespace() throws Exception {
    String source = "<configuration><key0></key0><key1>,</key1> <key2></key2><key3></key3></configuration>";
    XMLConfiguration config = new XMLConfiguration();

    config.load(new StringReader(source));
    checkConfiguration(config);/*from  ww w. ja v a2  s .c  om*/
}

From source file:com.example.TestClass.java

@Test
public void testWithSeparatingNonWhitespace() throws Exception {
    String source = "<configuration><key0></key0><key1>,</key1>A<key2></key2><key3></key3></configuration>";
    XMLConfiguration config = new XMLConfiguration();

    config.load(new StringReader(source));
    checkConfiguration(config);/*from   www.jav a  2 s. co  m*/
}

From source file:com.example.TestClass.java

@Test
public void testWithOnlyCommaWithStringBuilder() throws Exception {
    StringBuilder sourceBuilder = new StringBuilder("<configuration>");
    sourceBuilder.append("<key0></key0>");
    sourceBuilder.append("<key1>,</key1>");
    sourceBuilder.append("<key2></key2>");
    sourceBuilder.append("<key3></key3>");
    sourceBuilder.append("</configuration>");
    XMLConfiguration config = new XMLConfiguration();

    config.load(new StringReader(sourceBuilder.toString()));
    checkConfiguration(config);//from   w w  w  .  j a  v  a2  s.  c o m
}

From source file:com.example.TestClass.java

@Test
public void testWithOnlyCommaWithoutDelimiterParsing() throws Exception {
    String source = "<configuration><key0></key0><key1>,</key1><key2></key2><key3></key3></configuration>";
    XMLConfiguration config = new XMLConfiguration();
    config.setDelimiterParsingDisabled(true);

    config.load(new StringReader(source));
    checkConfiguration(config);/*  w  ww  . j av  a  2s . c o m*/
}

From source file:com.example.TestClass.java

@Test
public void testWithOnlyCommaWithStringBuilderWithoutDelimiterParsing() throws Exception {
    StringBuilder sourceBuilder = new StringBuilder("<configuration>");
    sourceBuilder.append("<key0></key0>");
    sourceBuilder.append("<key1>,</key1>");
    sourceBuilder.append("<key2></key2>");
    sourceBuilder.append("<key3></key3>");
    sourceBuilder.append("</configuration>");
    XMLConfiguration config = new XMLConfiguration();
    config.setDelimiterParsingDisabled(true);

    config.load(new StringReader(sourceBuilder.toString()));
    checkConfiguration(config);//from w  w w  . ja  va 2s  . c o  m
}

From source file:com.epam.cme.mdp3.core.cfg.Configuration.java

/**
 * Loads and parse CME MDP Configuration.
 *
 * @param uri URI to CME MDP Configuration (config.xml, usually available on CME FTP)
 * @throws ConfigurationException if failed to parse configuration file
 * @throws MalformedURLException  if URI to Configuration is malformed
 *//*from  w  ww  .  j ava  2  s. c  om*/
private void load(final URI uri) throws ConfigurationException, MalformedURLException {
    // todo: if to implement the same via standard JAXB then dep to apache commons configuration will be not required
    final XMLConfiguration configuration = new XMLConfiguration();
    configuration.setDelimiterParsingDisabled(true);
    configuration.load(uri.toURL());
    for (final HierarchicalConfiguration channelCfg : configuration.configurationsAt("channel")) {
        final ChannelCfg channel = new ChannelCfg(channelCfg.getString("[@id]"),
                channelCfg.getString("[@label]"));

        for (final HierarchicalConfiguration connCfg : channelCfg.configurationsAt("connections.connection")) {
            final String id = connCfg.getString("[@id]");
            final FeedType type = FeedType.valueOf(connCfg.getString("type[@feed-type]"));
            final TransportProtocol protocol = TransportProtocol
                    .valueOf(connCfg.getString("protocol").substring(0, 3));
            final Feed feed = Feed.valueOf(connCfg.getString("feed"));
            final String ip = connCfg.getString("ip");
            final int port = connCfg.getInt("port");
            final List<String> hostIPs = Arrays.asList(connCfg.getStringArray("host-ip"));

            final ConnectionCfg connection = new ConnectionCfg(feed, id, type, protocol, ip, hostIPs, port);
            channel.addConnection(connection);
            connCfgs.put(connection.getId(), connection);
        }
        channelCfgs.put(channel.getId(), channel);
    }
}