/** * Resolves the specified node regarding the parent flag. If the parent flag * is set, the node's parent is returned (if any); otherwise, the node * itself is returned. * * @param nd the node in question * @param parent the parent flag * @return the resolved node */ private static ConfigurationNode resolveParent(ConfigurationNode nd, boolean parent) { if (parent) { ConfigurationNode parentNode = nd.getParentNode(); return (parentNode != null) ? parentNode : nd; } return nd; } }
/** * Obtains the complete path from the specified target node. This method * navigates through the parent nodes until it finds the root node. * * @param node the target node * @return a list with the nodes of this path */ private List<ConfigurationNode> initPath(ConfigurationNode node) { List<ConfigurationNode> nodes = new ArrayList<ConfigurationNode>(); ConfigurationNode nd = node; while (nd != null) { nodes.add(nd); nd = nd.getParentNode(); } Collections.reverse(nodes); return nodes; }
current.getParentNode()) for (ConfigurationNode current = nd1.getParentNode(); current != null; current = current.getParentNode())
private void populateAlternateKeyMap(HierarchicalConfiguration config) { List<SubnodeConfiguration> configurationsAt = config.configurationsAt("/*/" + ALTERNATE_KEY); alternateKeysMap = new HashMap<>(configurationsAt.size()); for (SubnodeConfiguration node : configurationsAt) { String rootKey = node.getRootNode() .getParentNode().getName(); String[] alternateKeys = config.getStringArray("/" + rootKey + "/" + ALTERNATE_KEY); for (String token : alternateKeys) { alternateKeysMap.put(token, rootKey); } } }
/** * Adds a child node to this view node. The new child's parent node will be * saved. * * @param child the child node to be added */ @Override public void addChild(ConfigurationNode child) { ConfigurationNode parent = null; if (child != null) { parent = child.getParentNode(); super.addChild(child); child.setParentNode(parent); } else { throw new IllegalArgumentException("Child node must not be null!"); } }
/** * Adds an attribute to this view node. The new attribute's parent node will * be saved. * * @param attr the attribute node to be added */ @Override public void addAttribute(ConfigurationNode attr) { ConfigurationNode parent = null; if (attr != null) { parent = attr.getParentNode(); super.addAttribute(attr); attr.setParentNode(parent); } else { throw new IllegalArgumentException("Attribute node must not be null!"); } }
/** * Adds a child node to this view node. The new child's parent node will be * saved. * * @param child the child node to be added */ @Override public void addChild(ConfigurationNode child) { ConfigurationNode parent = null; if (child != null) { parent = child.getParentNode(); super.addChild(child); child.setParentNode(parent); } else { throw new IllegalArgumentException("Child node must not be null!"); } }
/** * Adds an attribute to this view node. The new attribute's parent node will * be saved. * * @param attr the attribute node to be added */ @Override public void addAttribute(ConfigurationNode attr) { ConfigurationNode parent = null; if (attr != null) { parent = attr.getParentNode(); super.addAttribute(attr); attr.setParentNode(parent); } else { throw new IllegalArgumentException("Attribute node must not be null!"); } }
/** * Returns a {@code TreeNodePath} object that was created by appending the * specified {@code ConfigurationNode} to this path. The new node becomes * the target node of the new {@code TreeNodePath} object. This method is * useful when navigating through a tree structure. The passed in node must * be a child node of the current target node. * * @param node the node to be appended to the path * @return the new {@code TreeNodePath} extended by the node * @throws IllegalArgumentException if the passed in node is <b>null</b> or * not a child node of the target node */ public TreeNodePath append(ConfigurationNode node) { if (node == null) { throw new IllegalArgumentException("Node must not be null!"); } if (node.getParentNode() != getTargetNode()) { throw new IllegalArgumentException( "Node is not a child of the target node!"); } List<ConfigurationNode> newNodes = new ArrayList<ConfigurationNode>(size() + 1); newNodes.addAll(nodes); newNodes.add(node); return new TreeNodePath(newNodes); }
/** * Determines the index of the specified child node. * * @param nd the node * @return the index of this node relative to its parent */ private static int indexForChild(ConfigurationNode nd) { ConfigurationNode parent = nd.getParentNode(); assert parent != null : "No parent node!"; List<?> children = parent.getChildren(nd.getName()); int index = 0; for (Object c : children) { if (c == nd) { break; } index++; } return index; } }
/** * Checks whether the given node is reserved. This method will take * further reserved attributes into account * * @param nd the node * @return a flag whether this node is reserved */ @Override protected boolean isReservedNode(ConfigurationNode nd) { if (super.isReservedNode(nd)) { return true; } return nd.isAttribute() && ((ATTR_ATNAME.equals(nd.getName()) && nd.getParentNode() .getAttributeCount(RESERVED_PREFIX + ATTR_ATNAME) == 0) || (ATTR_OPTIONALNAME .equals(nd.getName()) && nd.getParentNode() .getAttributeCount(RESERVED_PREFIX + ATTR_OPTIONALNAME) == 0)); }
/** * Checks whether the given node is reserved. This method will take * further reserved attributes into account * * @param nd the node * @return a flag whether this node is reserved */ @Override protected boolean isReservedNode(ConfigurationNode nd) { if (super.isReservedNode(nd)) { return true; } return nd.isAttribute() && ((ATTR_ATNAME.equals(nd.getName()) && nd.getParentNode() .getAttributeCount(RESERVED_PREFIX + ATTR_ATNAME) == 0) || (ATTR_OPTIONALNAME .equals(nd.getName()) && nd.getParentNode() .getAttributeCount(RESERVED_PREFIX + ATTR_OPTIONALNAME) == 0)); }
/** * Removes the specified node from this configuration. This method ensures * that parent nodes that become undefined by this operation are also * removed. * * @param node the node to be removed */ protected void removeNode(ConfigurationNode node) { ConfigurationNode parent = node.getParentNode(); if (parent != null) { parent.removeChild(node); if (!nodeDefined(parent)) { removeNode(parent); } } }
/** * Removes the specified node from this configuration. This method ensures * that parent nodes that become undefined by this operation are also * removed. * * @param node the node to be removed */ protected void removeNode(ConfigurationNode node) { ConfigurationNode parent = node.getParentNode(); if (parent != null) { parent.removeChild(node); if (!nodeDefined(parent)) { removeNode(parent); } } }
/** * Changes the name of a {@code ConfigurationNode}. This is a utility method * which is probably needed by each concrete tree model implementation. * Normally, the name of a configuration node cannot be changed if it is * part of a node hierarchy. Therefore, this method uses some tricks to * achieve this goal. The return value indicates if a change was done. If * the passed in new name equals the current name, nothing is changed, and * result is <b>false</b>. * * @param node the {@code ConfigurationNode} to be changed * @param newName the new name of this node * @return <b>true</b> if a name change was necessary, <b>false</b> * otherwise */ public boolean changeNodeName(ConfigurationNode node, String newName) { if (!StringUtils.equals(node.getName(), newName)) { ConfigurationNode parent = node.getParentNode(); node.setParentNode(null); node.setName(newName); node.setParentNode(parent); return true; } return false; }
/** * Determines the configuration that owns the specified node. * * @param node the node * @return the owning configuration */ private Configuration findSourceConfiguration(ConfigurationNode node) { synchronized (getReloadLock()) { ConfigurationNode root = null; ConfigurationNode current = node; // find the root node in this hierarchy while (current != null) { root = current; current = current.getParentNode(); } // Check with the root nodes of the child configurations for (ConfigData cd : configurations) { if (root == cd.getRootNode()) { return cd.getConfiguration(); } } } return this; }
/** * Determines the configuration that owns the specified node. * * @param node the node * @return the owning configuration */ private Configuration findSourceConfiguration(ConfigurationNode node) { synchronized (getReloadLock()) { ConfigurationNode root = null; ConfigurationNode current = node; // find the root node in this hierarchy while (current != null) { root = current; current = current.getParentNode(); } // Check with the root nodes of the child configurations for (ConfigData cd : configurations) { if (root == cd.getRootNode()) { return cd.getConfiguration(); } } } return this; }
/** * 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); } }
public void saveTo(Configuration conf) { boolean removed = false; List<HierarchicalConfiguration> fields = ((HierarchicalConfiguration) getConfig()).configurationsAt(PSCANS_KEY); for (HierarchicalConfiguration sub : fields) { if (isPluginConfiguration(sub)) { sub.getRootNode().getParentNode().removeChild(sub.getRootNode()); removed = true; break; } } boolean persistId = false; String entryKey = PSCANS_KEY + "(" + (removed ? fields.size() - 1 : fields.size()) + ")."; if (getAlertThreshold() != AlertThreshold.MEDIUM) { conf.setProperty(entryKey + ALERT_THRESHOLD_KEY, getAlertThreshold().name()); // For compatibility with older versions: conf.setProperty(entryKey + LEVEL_KEY, getAlertThreshold().name()); persistId = true; } if (!isEnabled()) { conf.setProperty(entryKey + ENABLED_KEY, Boolean.FALSE); persistId = true; } if (persistId) { conf.setProperty(entryKey + ID_KEY, getPluginId()); } }