protected Object saveHierarchy(final ConfigurationNode parentNode) { if (parentNode.getChildrenCount() == 0) return parentNode.getValue(); if (parentNode.getChildrenCount("item") == parentNode.getChildrenCount()) { return parentNode.getChildren().stream().map(this::saveHierarchy).collect(Collectors.toList()); } else { final Map<String, Object> map = new LinkedHashMap<>(); for (ConfigurationNode childNode : parentNode.getChildren()) { String nodeName = childNode.getName(); if (this.xmlCompatibility && childNode.getAttributes("name").size() > 0) nodeName = String.valueOf(childNode.getAttributes("name").get(0).getValue()); map.put(nodeName, saveHierarchy(childNode)); } return map; } } }
List<HierarchicalConfiguration> nodes = config.configurationsAt("attrs/attr"); for(HierarchicalConfiguration c : nodes ) { ConfigurationNode node = c.getRootNode(); System.out.println( "name:" + ((ConfigurationNode) node.getAttributes("name").get(0)).getValue() + " VAL:" + ((ConfigurationNode) node.getAttributes("val").get(0)).getValue()); }
/** * Get the very first attribute with the given name from the configuration node. * A null is returned if there is no such value. * * @param node * @param name * @return */ public static String getFirstAttribute(ConfigurationNode node, String name) { List list = node.getAttributes(name); if (list.isEmpty()) { return null; } ConfigurationNode cn = (ConfigurationNode) list.get(0); return cn.getValue().toString(); }
/** * Adds all attribute nodes of the given source node to this view node. * * @param source the source node */ public void appendAttributes(ConfigurationNode source) { if (source != null) { for (ConfigurationNode attr : source.getAttributes()) { addAttribute(attr); } } }
/** * Adds all attribute nodes of the given source node to this view node. * * @param source the source node */ public void appendAttributes(ConfigurationNode source) { if (source != null) { for (ConfigurationNode attr : source.getAttributes()) { addAttribute(attr); } } }
/** * Get an attribute for this type. * * @param key * @return */ public String getTypeAttribute(String key) { List list = getTypeConfig().getAttributes(key); if (list.isEmpty()) { return null; } DefaultConfigurationNode node = (DefaultConfigurationNode) list.get(0); return node.getValue().toString(); }
/** * Gets the attribute with the given key. This slaps "[@" and "]" around the key to conform wit * how Apache resolves these. * * @param key * @return */ public String getAttribute(String key) { // return getConfig().getString(toAttrKey(key)); List list = getConfig().getAttributes(key); if (list.isEmpty()) { return null; } DefaultConfigurationNode node = (DefaultConfigurationNode) list.get(0); return node.getValue().toString(); }
/** * Determines which attributes are selected based on the passed in node * name. * @param node the current node * @param name the name of the selected attribute * @return a list with the selected attributes */ protected List<ConfigurationNode> createSubNodeList(ConfigurationNode node, QName name) { if (name.getPrefix() != null) { // namespace prefixes are not supported return Collections.emptyList(); } List<ConfigurationNode> result = new ArrayList<ConfigurationNode>(); if (!WILDCARD.equals(name.getName())) { result.addAll(node.getAttributes(name.getName())); } else { result.addAll(node.getAttributes()); } return result; } }
protected Object saveHierarchy(final ConfigurationNode parentNode) { if (parentNode.getChildrenCount() == 0) return parentNode.getValue(); if (parentNode.getChildrenCount("item") == parentNode.getChildrenCount()) { return parentNode.getChildren().stream().map(this::saveHierarchy).collect(Collectors.toList()); } else { final Map<String, Object> map = new LinkedHashMap<>(); for (ConfigurationNode childNode : parentNode.getChildren()) { String nodeName = childNode.getName(); if (this.xmlCompatibility && childNode.getAttributes("name").size() > 0) nodeName = String.valueOf(childNode.getAttributes("name").get(0).getValue()); map.put(nodeName, saveHierarchy(childNode)); } return map; } } }
protected Object saveHierarchy(final ConfigurationNode parentNode) { if (parentNode.getChildrenCount() == 0) return parentNode.getValue(); if (parentNode.getChildrenCount("item") == parentNode.getChildrenCount()) { return parentNode.getChildren().stream().map(this::saveHierarchy).collect(Collectors.toList()); } else { final Map<String, Object> map = new LinkedHashMap<>(); for (ConfigurationNode childNode : parentNode.getChildren()) { String nodeName = childNode.getName(); if (this.xmlCompatibility && childNode.getAttributes("name").size() > 0) nodeName = String.valueOf(childNode.getAttributes("name").get(0).getValue()); map.put(nodeName, saveHierarchy(childNode)); } return map; } } }
/** * Returns a map with the bean's (simple) properties. The properties are * collected from all attribute nodes, which are not reserved. * * @return a map with the bean's properties */ public Map<String, Object> getBeanProperties() { Map<String, Object> props = new HashMap<String, Object>(); for (ConfigurationNode attr : getNode().getAttributes()) { if (!isReservedNode(attr)) { props.put(attr.getName(), interpolate(attr .getValue())); } } return props; }
/** * Returns a map with the bean's (simple) properties. The properties are * collected from all attribute nodes, which are not reserved. * * @return a map with the bean's properties */ public Map<String, Object> getBeanProperties() { Map<String, Object> props = new HashMap<String, Object>(); for (ConfigurationNode attr : getNode().getAttributes()) { if (!isReservedNode(attr)) { props.put(attr.getName(), interpolate(attr .getValue())); } } return props; }
/** * Handles the attributes during a combination process. First all attributes * of the first node will be added to the result. Then all attributes of the * second node, which are not contained in the first node, will also be * added. * * @param result the resulting node * @param node1 the first node * @param node2 the second node */ protected void addAttributes(ViewNode result, ConfigurationNode node1, ConfigurationNode node2) { result.appendAttributes(node1); for (ConfigurationNode attr : node2.getAttributes()) { if (node1.getAttributeCount(attr.getName()) == 0) { result.addAttribute(attr); } } }
/** * Handles the attributes during a combination process. First all attributes * of the first node will be added to the result. Then all attributes of the * second node, which are not contained in the first node, will also be * added. * * @param result the resulting node * @param node1 the first node * @param node2 the second node */ protected void addAttributes(ViewNode result, ConfigurationNode node1, ConfigurationNode node2) { result.appendAttributes(node1); for (ConfigurationNode attr : node2.getAttributes()) { if (node1.getAttributeCount(attr.getName()) == 0) { result.addAttribute(attr); } } }
/** * Handles the attributes during a combination process. First all attributes * of the first node will be added to the result. Then all attributes of the * second node, which are not contained in the first node, will also be * added. * * @param result the resulting node * @param node1 the first node * @param node2 the second node */ protected void addAttributes(ViewNode result, ConfigurationNode node1, ConfigurationNode node2) { result.appendAttributes(node1); for (ConfigurationNode attr : node2.getAttributes()) { if (node1.getAttributeCount(attr.getName()) == 0) { result.addAttribute(attr); } } }
/** * Handles the attributes during a combination process. First all attributes * of the first node will be added to the result. Then all attributes of the * second node, which are not contained in the first node, will also be * added. * * @param result the resulting node * @param node1 the first node * @param node2 the second node */ protected void addAttributes(ViewNode result, ConfigurationNode node1, ConfigurationNode node2) { result.appendAttributes(node1); for (ConfigurationNode attr : node2.getAttributes()) { if (node1.getAttributeCount(attr.getName()) == 0) { result.addAttribute(attr); } } }
/** * Recursive helper method for evaluating a key. This method processes all * facets of a configuration key, traverses the tree of properties and * fetches the the nodes of all matching properties. * * @param keyPart the configuration key iterator * @param node the actual node * @param nodes here the found nodes are stored */ protected void findNodesForKey(DefaultConfigurationKey.KeyIterator keyPart, ConfigurationNode node, Collection<ConfigurationNode> nodes) { if (!keyPart.hasNext()) { nodes.add(node); } else { String key = keyPart.nextKey(false); if (keyPart.isPropertyKey()) { processSubNodes(keyPart, node.getChildren(key), nodes); } if (keyPart.isAttribute()) { processSubNodes(keyPart, node.getAttributes(key), nodes); } } }
/** * Creates a new instance of {@code Node} based on the given * source node. All properties of the source node, including its * children and attributes, will be copied. * * @param src the node to be copied */ public Node(ConfigurationNode src) { this(src.getName(), src.getValue()); setReference(src.getReference()); for (ConfigurationNode nd : src.getChildren()) { // Don't change the parent node ConfigurationNode parent = nd.getParentNode(); addChild(nd); nd.setParentNode(parent); } for (ConfigurationNode nd : src.getAttributes()) { // Don't change the parent node ConfigurationNode parent = nd.getParentNode(); addAttribute(nd); nd.setParentNode(parent); } }
/** * Creates a new instance of {@code Node} based on the given * source node. All properties of the source node, including its * children and attributes, will be copied. * * @param src the node to be copied */ public Node(ConfigurationNode src) { this(src.getName(), src.getValue()); setReference(src.getReference()); for (ConfigurationNode nd : src.getChildren()) { // Don't change the parent node ConfigurationNode parent = nd.getParentNode(); addChild(nd); nd.setParentNode(parent); } for (ConfigurationNode nd : src.getAttributes()) { // Don't change the parent node ConfigurationNode parent = nd.getParentNode(); addAttribute(nd); nd.setParentNode(parent); } }
/** * Converts the specified node into a {@code XMLNode} if necessary. * This is required for nodes that are directly added, e.g. by * {@code addNodes()}. If the passed in node is already an instance * of {@code XMLNode}, it is directly returned, and conversion * stops. Otherwise a new {@code XMLNode} is created, and the * children are also converted. * * @param node the node to be converted * @return the converted node */ private XMLNode convertToXMLNode(ConfigurationNode node) { if (node instanceof XMLNode) { return (XMLNode) node; } XMLNode nd = (XMLNode) createNode(node.getName()); nd.setValue(node.getValue()); nd.setAttribute(node.isAttribute()); for (ConfigurationNode child : node.getChildren()) { nd.addChild(convertToXMLNode(child)); } for (ConfigurationNode attr : node.getAttributes()) { nd.addAttribute(convertToXMLNode(attr)); } return nd; }