/** * Creates a new Node named <code>name</code> and if a parent is supplied, adds * the newly created node as a child of the parent. * * @param parent the parent node or null if no parent * @param name the name of the node */ public Node(Node parent, Object name) { this(parent, name, new NodeList()); }
/** * Creates a new Node named <code>name</code> with * attributes specified in the <code>attributes</code> Map. If a parent is supplied, * the newly created node is added as a child of the parent. * * @param parent the parent node or null if no parent * @param name the name of the node * @param attributes a Map of name-value pairs */ public Node(Node parent, Object name, Map attributes) { this(parent, name, attributes, new NodeList()); }
/** * Returns a <code>List</code> of the nodes children. * * @return the nodes children */ public List children() { if (value == null) { return new NodeList(); } if (value instanceof List) { return (List) value; } // we're probably just a String List result = new NodeList(); result.add(value); return result; }
private List getDirectChildren() { List answer = new NodeList(); for (Iterator iter = InvokerHelper.asIterator(value); iter.hasNext(); ) { Object child = iter.next(); if (child instanceof Node) { Node childNode = (Node) child; answer.add(childNode); } else if (child instanceof String) { answer.add(child); } } return answer; }
/** * Provides a collection of all the nodes in the tree * using a depth-first traversal. * * @param preorder if false, a postorder depth-first traversal will be performed * @return the list of (depth-first) ordered nodes * @since 2.5.0 */ public List depthFirst(boolean preorder) { List answer = new NodeList(); if (preorder) answer.add(this); answer.addAll(depthFirstRest(preorder)); if (!preorder) answer.add(this); return answer; }
/** * Provides a collection of all the nodes in the tree * using a breadth-first traversal. * * @param preorder if false, a postorder breadth-first traversal will be performed * @return the list of (breadth-first) ordered nodes * @since 2.5.0 */ public List breadthFirst(boolean preorder) { List answer = new NodeList(); if (preorder) answer.add(this); answer.addAll(breadthFirstRest(preorder)); if (!preorder) answer.add(this); return answer; }
private List depthFirstRest(boolean preorder) { List answer = new NodeList(); for (Iterator iter = InvokerHelper.asIterator(value); iter.hasNext(); ) { Object child = iter.next(); if (child instanceof Node) { Node childNode = (Node) child; List children = childNode.depthFirstRest(preorder); if (preorder) answer.add(childNode); if (children.size() > 1 || (children.size() == 1 && !(children.get(0) instanceof String))) answer.addAll(children); if (!preorder) answer.add(childNode); } else if (child instanceof String) { answer.add(child); } } return answer; }
private static List getParentList(Node parent) { Object parentValue = parent.value(); List parentList; if (parentValue instanceof List) { parentList = (List) parentValue; } else { parentList = new NodeList(); parentList.add(parentValue); parent.setValue(parentList); } return parentList; }
protected Node createNode(Node parent, Object name, Map attributes) { return new CustomNode(parent, name, attributes, new NodeList()); } }
private List breadthFirstRest(boolean preorder) { List answer = new NodeList(); Stack stack = new Stack(); List nextLevelChildren = preorder ? getDirectChildren() : DefaultGroovyMethods.reverse(getDirectChildren()); while (!nextLevelChildren.isEmpty()) { List working = new NodeList(nextLevelChildren); nextLevelChildren = new NodeList(); for (Object child : working) { if (preorder) { answer.add(child); } else { stack.push(child); } if (child instanceof Node) { Node childNode = (Node) child; List children = childNode.getDirectChildren(); if (children.size() > 1 || (children.size() == 1 && !(children.get(0) instanceof String))) nextLevelChildren.addAll(preorder ? children : DefaultGroovyMethods.reverse(children)); } } } while (!stack.isEmpty()) { answer.add(stack.pop()); } return answer; }
/** * Provides lookup of elements by QName. * * @param name the name or shortcut key for nodes of interest * @return the nodes of interest which match name */ public NodeList getAt(QName name) { NodeList answer = new NodeList(); for (Object child : this) { if (child instanceof Node) { Node childNode = (Node) child; NodeList temp = childNode.getAt(name); answer.addAll(temp); } } return answer; }
private void breadthFirstRest(boolean preorder, int level, Closure c) { Stack<Tuple2<Object, Integer>> stack = new Stack<Tuple2<Object, Integer>>(); List nextLevelChildren = preorder ? getDirectChildren() : DefaultGroovyMethods.reverse(getDirectChildren()); while (!nextLevelChildren.isEmpty()) { List working = new NodeList(nextLevelChildren); nextLevelChildren = new NodeList(); for (Object child : working) { if (preorder) { callClosureForNode(c, child, level); } else { stack.push(new Tuple2<Object, Integer>(child, level)); } if (child instanceof Node) { Node childNode = (Node) child; List children = childNode.getDirectChildren(); if (children.size() > 1 || (children.size() == 1 && !(children.get(0) instanceof String))) nextLevelChildren.addAll(preorder ? children : DefaultGroovyMethods.reverse(children)); } } level++; } while (!stack.isEmpty()) { Tuple2<Object, Integer> next = stack.pop(); callClosureForNode(c, next.getFirst(), next.getSecond()); } }
/** * Provides lookup of elements by non-namespaced name. * * @param name the name or shortcut key for nodes of interest * @return the nodes of interest which match name */ public NodeList getAt(String name) { NodeList answer = new NodeList(); for (Object child : this) { if (child instanceof Node) { Node childNode = (Node) child; Object temp = childNode.get(name); if (temp instanceof Collection) { answer.addAll((Collection) temp); } else { answer.add(temp); } } } return answer; }
/** * Provides lookup of elements by QName. * * @param name the QName of interest * @return the nodes matching name */ public NodeList getAt(QName name) { NodeList answer = new NodeList(); for (Object child : children()) { if (child instanceof Node) { Node childNode = (Node) child; Object childNodeName = childNode.name(); if (name.matches(childNodeName)) { answer.add(childNode); } } } return answer; }
/** * Provides lookup of elements by name. * * @param name the name of interest * @return the nodes matching name */ private NodeList getByName(String name) { NodeList answer = new NodeList(); for (Object child : children()) { if (child instanceof Node) { Node childNode = (Node) child; Object childNodeName = childNode.name(); if (childNodeName instanceof QName) { QName qn = (QName) childNodeName; if (qn.matches(name)) { answer.add(childNode); } } else if (name.equals(childNodeName)) { answer.add(childNode); } } } return answer; }
/** * Creates a new NodeList containing the same elements as the * original (but cloned in the case of Nodes). * * @return the clone */ @Override public Object clone() { NodeList result = new NodeList(size()); for (int i = 0; i < size(); i++) { Object next = get(i); if (next instanceof Node) { Node n = (Node) next; result.add(n.clone()); } else { result.add(next); } } return result; }
/** * Provide a collection of all the nodes in the tree * using a depth first traversal. * * @return the list of (depth-first) ordered nodes */ public List depthFirst() { List answer = new NodeList(); answer.add(this); answer.addAll(depthFirstRest()); return answer; }
private List getDirectChildren() { List answer = new NodeList(); for (Iterator iter = InvokerHelper.asIterator(value); iter.hasNext();) { Object child = iter.next(); if (child instanceof Node) { Node childNode = (Node) child; answer.add(childNode); } } return answer; }
private List getParentList(Node parent) { Object parentValue = parent.value(); List parentList; if (parentValue instanceof List) { parentList = (List) parentValue; } else { parentList = new NodeList(); parentList.add(parentValue); parent.setValue(parentList); } return parentList; }