public Node replaceNode(Closure c) { if (size() <= 0 || size() > 1) { throw new GroovyRuntimeException( "replaceNode() can only be used to replace a single node, but was applied to " + size() + " nodes"); } return ((Node)get(0)).replaceNode(c); }
public Node replaceNode(Closure c) { if (size() <= 0 || size() > 1) { throw new GroovyRuntimeException("replaceNode() can only be used to replace a single node."); } return ((Node)get(0)).replaceNode(c); }
/** * Apply unique ids for all nodes. * Remove '_' prefix from nodes (workaround for clashes with method names). * * @param root current node * @param topId id to apply */ private static void prepareTree(Node root, String topId) { root.attributes().put(NID_ATTR, topId); String name = (String) root.name(); if (name.startsWith("_")) { Node replace = new Node(null, name.substring(1)); replace.setValue(root.value()); root.replaceNode(replace); } int pos = 0; if (!isLeaf(root)) { List<Node> list = root.children(); for (int i = 0, size = list.size(); i < size; i++) { pos += 1; prepareTree(list.get(i), topId + "_" + pos); } } }
/** * Searching for the longest existing path in original xml to correctly merge xmls without tags duplication. * Method called recursively (indirectly). * * @param root original xml node * @param child child to search path for */ private static void mergeChild(Node root, Node child) { // check if parent xml contains child tag Node target = findPath((NodeList) root.get((String) child.name()), child); if (target != null) { if (isLeaf(child)) { // replacing current node value target.replaceNode(child); associate(root, child); } else { // recursive merge merge(target, child.children()); } } else { // no tag with child name exist appending entire subtree root.append(child); associate(root, child); } }