Example usage for org.apache.commons.configuration2.tree ImmutableNode getChildren

List of usage examples for org.apache.commons.configuration2.tree ImmutableNode getChildren

Introduction

In this page you can find the example usage for org.apache.commons.configuration2.tree ImmutableNode getChildren.

Prototype

public List<ImmutableNode> getChildren() 

Source Link

Document

Returns a list with the children of this node.

Usage

From source file:com.virtlink.commons.configuration2.jackson.JacksonConfiguration.java

/**
 * Gets a serialization object from a node.
 *
 * @param node The node.//from w  w w . java  2s. c  o m
 * @return The object representing the node.
 */
private Object fromNode(final ImmutableNode node) {
    if (!node.getChildren().isEmpty()) {
        final Map<String, List<ImmutableNode>> children = getChildrenWithName(node);

        final HashMap<String, Object> map = new HashMap<>();
        for (final Map.Entry<String, List<ImmutableNode>> entry : children.entrySet()) {
            assert !entry.getValue().isEmpty();
            if (entry.getValue().size() == 1) {
                // Just one node.
                final ImmutableNode child = entry.getValue().get(0);
                final Object childValue = fromNode(child);
                map.put(entry.getKey(), childValue);
            } else {
                // Multiple nodes.
                final ArrayList<Object> list = new ArrayList<>();
                for (final ImmutableNode child : entry.getValue()) {
                    final Object childValue = fromNode(child);
                    list.add(childValue);
                }
                map.put(entry.getKey(), list);
            }
        }
        return map;
    } else {
        return node.getValue();
    }
}

From source file:com.virtlink.commons.configuration2.jackson.JacksonConfiguration.java

private Map<String, List<ImmutableNode>> getChildrenWithName(final ImmutableNode node) {
    final Map<String, List<ImmutableNode>> children = new HashMap<>();
    for (final ImmutableNode child : node.getChildren()) {
        final String name = child.getNodeName();
        if (!children.containsKey(name)) {
            children.put(name, new ArrayList<ImmutableNode>());
        }/*from  w  ww. j a va 2  s  .  c om*/
        children.get(name).add(child);
    }
    return children;
}

From source file:com.gs.obevo.db.api.factory.DbEnvironmentXmlEnricherTest.java

private Object getNodeValue(ImmutableNode node) {
    if (node.getChildren().isEmpty()) {
        if (node.getAttributes() != null && !node.getAttributes().isEmpty()) {
            return constructMap(node);
        } else {/*ww w.  j  av  a 2  s  . c  om*/
            return node.getValue();
        }
    } else {
        return constructMap(node);
    }
}

From source file:com.gs.obevo.db.api.factory.DbEnvironmentXmlEnricherTest.java

private Map<String, Object> constructMap(ImmutableNode node) {
    final Map<String, Object> map = new HashMap<>(node.getChildren().size());
    map.putAll(node.getAttributes());//from   w ww .  ja  va  2 s.  c  o m

    MutableListMultimap<String, ImmutableNode> nodesByName = ListAdapter.adapt(node.getChildren())
            .groupBy(new Function<ImmutableNode, String>() {
                @Override
                public String valueOf(ImmutableNode immutableNode) {
                    return immutableNode.getNodeName();
                }
            });
    nodesByName.forEachKeyMultiValues(new Procedure2<String, Iterable<ImmutableNode>>() {
        @Override
        public void value(String nodeName, Iterable<ImmutableNode> nodeIterable) {
            //            System.out.println("At node " + cNode.getNodeName() + " and attrs " + cNode.getAttributes() + " and value " + cNode.getValue() + " and children " + cNode.getChildren());
            MutableList<ImmutableNode> nodes = CollectionAdapter.wrapList(nodeIterable);
            if (nodes.size() > 1) {
                map.put(nodeName, nodes.collect(new Function<ImmutableNode, Object>() {
                    @Override
                    public Object valueOf(ImmutableNode node1) {
                        return DbEnvironmentXmlEnricherTest.this.getNodeValue(node1);
                    }
                }));
            } else {
                map.put(nodeName, DbEnvironmentXmlEnricherTest.this.getNodeValue(nodes.getFirst()));
            }
        }
    });
    return map;
}