Example usage for org.apache.commons.configuration HierarchicalConfiguration getRoot

List of usage examples for org.apache.commons.configuration HierarchicalConfiguration getRoot

Introduction

In this page you can find the example usage for org.apache.commons.configuration HierarchicalConfiguration getRoot.

Prototype

public Node getRoot() 

Source Link

Document

Returns the root node of this hierarchical configuration.

Usage

From source file:com.aol.advertising.qiao.util.XmlConfigUtil.java

/**
 * Parse xml configuration and return a list of attributes belonging to one
 * or more elements with the specific key. It returns a list preserving the
 * order of elements in xml.//from  w ww  .  ja v  a 2  s .co m
 *
 * @param config
 * @param key
 *            identifies xml elements with the specific tag
 * @return
 */
public static List<Map<String, Object>> parseMultiNodesAttributes(HierarchicalConfiguration config,
        String key) {
    List<Map<String, Object>> result = null;

    // get a list of subnode configurations for all configuration nodes
    // selected by the given key, e.g. "node.inbound.listener"
    List<?> list = config.configurationsAt(key);
    if (list != null) {
        result = new ArrayList<Map<String, Object>>();
        for (Iterator<?> iter = list.iterator(); iter.hasNext();) {
            HierarchicalConfiguration sub = (HierarchicalConfiguration) iter.next();
            Node root = sub.getRoot();

            Map<String, Object> attributes = new HashMap<String, Object>(root.getAttributeCount());

            // get attribute nodes
            List<?> attrs = root.getAttributes();
            for (Object attr : attrs) {
                ConfigurationNode nd = (ConfigurationNode) attr;
                attributes.put(nd.getName(), nd.getValue());
            }
            result.add(attributes);
        }
    }

    return result;
}

From source file:com.vmware.qe.framework.datadriven.impl.supplier.XMLDataParser.java

/**
 * This method appends config2 with config1 This method is required since Configuration.append()
 * will not maintain hierarchical structure
 * /*  w w  w.j  av a2  s .c o  m*/
 * @param config1
 * @param config2
 */
public static void append(HierarchicalConfiguration config1, HierarchicalConfiguration config2)
        throws Exception {
    HierarchicalConfiguration clonedConfig2 = (HierarchicalConfiguration) config2.clone();
    List<ConfigurationNode> nodes = clonedConfig2.getRootNode().getChildren();
    for (ConfigurationNode configurationNode : nodes) {
        config1.getRoot().addChild(configurationNode);
    }
}

From source file:net.continuumsecurity.Config.java

public List<String> getSpiderUrls() {
    List<String> spiderUrls = new ArrayList<>();
    for (HierarchicalConfiguration ignoreUrl : getXml().configurationsAt("scanner.spiderUrl")) {
        spiderUrls.add(ignoreUrl.getRoot().getValue().toString());
    }/*from  w  w w . java 2s  .  c om*/
    return spiderUrls;
}

From source file:net.continuumsecurity.Config.java

public List<String> getIgnoreUrls() {
    List<String> ignoreUrls = new ArrayList<>();
    for (HierarchicalConfiguration ignoreUrl : getXml().configurationsAt("scanner.ignoreUrl")) {
        ignoreUrls.add(ignoreUrl.getRoot().getValue().toString());
        System.out.println(ignoreUrl);
    }/*  ww  w . j  ava 2  s .com*/
    return ignoreUrls;
}

From source file:net.jradius.example.LocalUsersHandler.java

public void setConfig(ConfigurationItem cfg) {
    super.setConfig(cfg);
    HierarchicalConfiguration.Node root = cfg.getRoot();
    HierarchicalConfiguration xmlCfg = cfg.getXMLConfig();

    /*/*  w  ww .  j a va  2s .c  om*/
     * Look for <users> ... </users> in the configuration
     */
    List usersList = root.getChildren("users");
    HierarchicalConfiguration.Node node;

    for (Iterator l = usersList.iterator(); l.hasNext();) {
        /*
         * Iterate the <user> ... </user> blocks
         */
        node = (HierarchicalConfiguration.Node) l.next();
        List children = node.getChildren("user");
        for (Iterator i = children.iterator(); i.hasNext();) {
            node = (HierarchicalConfiguration.Node) i.next();
            root = xmlCfg.getRoot();
            xmlCfg.setRoot(node);

            LocalUser user = new LocalUser();

            /*
             * A user is defined in the configuration with the following XML syntax example:
             * 
             * <users>
             *   <user username="test" password="test">
             *       Reply-Message = Hello test user!
             *   </user>
             * </users>
             * 
             * The contents of the <user>...</user> block are the attributes to use in the
             * AccessAccept reply.
             */
            user.username = xmlCfg.getString("[@username]");
            user.realm = xmlCfg.getString("[@realm]");
            user.password = xmlCfg.getString("[@password]");
            Object v = node.getValue();

            if (v != null) {
                user.attributes = v.toString();
            }

            RadiusLog.debug("        -> Configured local user: " + user.getUserName());
            users.put(user.getUserName(), user);
            xmlCfg.setRoot(root);
        }
    }
}

From source file:de.uni_rostock.goodod.tools.Configuration.java

private HierarchicalConfiguration getConfigMap(String args[]) {

    CombinedConfiguration cfg = new CombinedConfiguration();
    HierarchicalConfiguration envCfg = new CombinedConfiguration();
    String repoRoot = System.getenv("GOODOD_REPO_ROOT");
    boolean helpMode = false;

    envCfg.addProperty("repositoryRoot", repoRoot);
    if (null == args) {
        return cfg;
    }/*from   w ww . ja v a  2  s.c  om*/
    GnuParser cmdLineParser = new GnuParser();
    CommandLine cmdLine = null;
    try {
        // parse the command line arguments
        cmdLine = cmdLineParser.parse(options, args);
    } catch (ParseException exception) {
        logger.fatal("Could not validate command-line.", exception);
        System.exit(1);
    }

    if (cmdLine.hasOption('c')) {
        envCfg.addProperty("configFile", cmdLine.getOptionValue('c'));
    }
    if (cmdLine.hasOption('t')) {
        envCfg.addProperty("threadCount", cmdLine.getOptionObject('t'));
    }

    if (cmdLine.hasOption('s')) {
        envCfg.addProperty("similarity", cmdLine.getOptionValue('s'));
    }
    if (cmdLine.hasOption('h')) {
        envCfg.addProperty("helpMode", true);
        helpMode = true;
    }
    if (cmdLine.hasOption('d')) {
        envCfg.addProperty("debug", true);
    }
    if (cmdLine.hasOption('1')) {
        envCfg.addProperty("one-way", true);
    }
    //Fetch the remaining arguments, but alas, commons-cli is not generics aware
    @SuppressWarnings("unchecked")
    List<String> argList = cmdLine.getArgList();
    HierarchicalConfiguration testConfig = null;
    try {
        if (argList.isEmpty() && (false == helpMode)) {
            logger.fatal("No test specification provided.");
            System.exit(1);
        } else if (1 == argList.size()) {
            File testFile = new File(argList.get(0));
            testConfig = readTestConfig(testFile);
            assert (null != testConfig);
            envCfg.addProperty("testFile", testFile.toString());

        } else if (false == helpMode) {
            /*
             *  For > 1 file, we assume that both are ontologies and we
             *  construct ourselves a test case configuration for them.
             */
            testConfig = new HierarchicalConfiguration();
            String ontologyA = argList.get(0);
            String ontologyB = argList.get(1);
            testConfig.addProperty("testName", "Comparison of " + ontologyA + " and " + ontologyB);
            testConfig.addProperty("notInRepository", true);
            Node studentOntologies = new Node("studentOntologies");
            Node groupA = new Node("groupA", Collections.singletonList(ontologyA));
            Node groupB = new Node("groupB", Collections.singletonList(ontologyB));
            studentOntologies.addChild(groupA);
            studentOntologies.addChild(groupB);
            testConfig.getRoot().addChild(studentOntologies);
            if (2 < argList.size()) {
                logger.warn("Ignoring extra arguments to comparison between individual ontologies.");
            }
            envCfg.addProperty("testFile", "unknown.plist");
        }
    } catch (Throwable t) {
        logger.fatal("Could not load test configuration.", t);
        System.exit(1);
    }
    cfg.addConfiguration(envCfg, "environment");
    if (false == helpMode) {
        cfg.addConfiguration(testConfig, "TestSubTree", "testDescription");
    }
    return cfg;
}

From source file:org.ambraproject.queue.MessageServiceImpl.java

/**
 * If there is content inside <syndication><message> configuration append it to message.
 *
 * @param configuration Sub configuration under "message" tag.
 * @return XML code snippet.// ww  w.ja  v  a 2 s .  c om
 */
private static String createAdditionalBodyFromConfiguration(HierarchicalConfiguration configuration) {
    if (configuration == null || configuration.isEmpty()) {
        return null;
    }
    Visitor visitor = new Visitor();
    configuration.getRoot().visit(visitor, new ConfigurationKey(""));
    return visitor.body.toString();
}

From source file:org.apache.james.fetchmail.FetchMail.java

/**
 * Method configure parses and validates the Configuration data and creates
 * a new <code>ParsedConfiguration</code>, an <code>Account</code> for each
 * configured static account and a// w w  w . j  a v  a 2  s .com
 * <code>ParsedDynamicAccountParameters</code> for each dynamic account.
 *
 * @see org.apache.james.lifecycle.api.Configurable#configure(HierarchicalConfiguration)
 */
public void configure(HierarchicalConfiguration configuration) throws ConfigurationException {
    // Set any Session parameters passed in the Configuration
    setSessionParameters(configuration);

    // Create the ParsedConfiguration used in the delegation chain
    ParsedConfiguration parsedConfiguration = new ParsedConfiguration(configuration, logger, getLocalUsers(),
            getDNSService(), getDomainList(), getMailQueue());

    setParsedConfiguration(parsedConfiguration);

    // Setup the Accounts
    List<HierarchicalConfiguration> allAccounts = configuration.configurationsAt("accounts");
    if (allAccounts.size() < 1)
        throw new ConfigurationException("Missing <accounts> section.");
    if (allAccounts.size() > 1)
        throw new ConfigurationException("Too many <accounts> sections, there must be exactly one");
    HierarchicalConfiguration accounts = allAccounts.get(0);

    if (!accounts.getKeys().hasNext())
        throw new ConfigurationException("Missing <account> section.");

    int i = 0;
    // Create an Account for every configured account
    for (ConfigurationNode accountsChild : accounts.getRoot().getChildren()) {

        String accountsChildName = accountsChild.getName();

        List<HierarchicalConfiguration> accountsChildConfig = accounts.configurationsAt(accountsChildName);
        HierarchicalConfiguration conf = accountsChildConfig.get(i);

        if ("alllocal".equals(accountsChildName)) {
            // <allLocal> is dynamic, save the parameters for accounts to
            // be created when the task is triggered
            getParsedDynamicAccountParameters().add(new ParsedDynamicAccountParameters(i, conf));
            continue;
        }

        if ("account".equals(accountsChildName)) {
            // Create an Account for the named user and
            // add it to the list of static accounts
            getStaticAccounts().add(new Account(i, parsedConfiguration, conf.getString("[@user]"),
                    conf.getString("[@password]"), conf.getString("[@recipient]"),
                    conf.getBoolean("[@ignorercpt-header]"), conf.getString("[@customrcpt-header]", ""),
                    getSession()));
            continue;
        }

        throw new ConfigurationException("Illegal token: <" + accountsChildName + "> in <accounts>");
    }
    i++;
}