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; }
private void appendNodes(Closure c) { List tail = getTail(); for (Node child : buildChildrenFromClosure(c)) { parent().appendNode(child.name(), child.attributes(), child.value()); } parent().children().addAll(tail); }
/** * Replaces the current node with the supplied node. * * @param n the new Node * @return the original now replaced node */ public Node replaceNode(Node n) { if (parent() == null) { throw new UnsupportedOperationException("Replacing the root node is not supported"); } List tail = getTail(); parent().appendNode(n.name(), n.attributes(), n.value()); parent().children().addAll(tail); getParentList(parent()).remove(this); this.setParent(null); return this; }
public void print(Node node) { out.printIndent(); printName(node); Map attributes = node.attributes(); boolean hasAttributes = attributes != null && !attributes.isEmpty(); if (hasAttributes) { printAttributes(attributes); } Object value = node.value(); if (value instanceof List) { if (!hasAttributes) { out.print("()"); } printList((List) value); } else { if (value instanceof String) { out.print("('"); out.print((String) value); out.println("')"); } else { out.println("()"); } } out.flush(); }
private void merge(Properties props, Node node, String prefix) { assert props != null; assert node != null; assert prefix != null; String name = prefix + node.name(); Object value = node.value(); if (value instanceof String) { props.setProperty(name, String.valueOf(value)); } Map attrs = node.attributes(); for (Object key : attrs.keySet()) { props.setProperty(name + "." + key, String.valueOf(attrs.get(key))); } for (Object child : node.children()) { if (child instanceof Node) { merge(props, (Node) child, name + "."); } } } }
Object value = node.value(); if (value instanceof List) { printName(node, ctx, true, isListOfSimple((List) value));
private Xpp3Dom nodeToXpp3(final Node node) { Xpp3Dom dom = new Xpp3Dom((String) node.name()); Object value = node.value(); if (value instanceof String) { dom.setValue(String.valueOf(value)); } Map attrs = node.attributes(); for (Object key : attrs.keySet()) { dom.setAttribute(String.valueOf(key), String.valueOf(attrs.get(key))); } for (Object child : node.children()) { if (child instanceof Node) { dom.addChild(nodeToXpp3((Node) child)); } } return dom; } }
@Override public boolean onNodeChildren(FactoryBuilderSupport builder, Object node, Closure content) { Properties props = (Properties) node; NodeBuilder nodes = new NodeBuilder() { @Override protected void setClosureDelegate(final Closure c, final Object o) { c.setDelegate(this); c.setResolveStrategy(Closure.DELEGATE_FIRST); } @Override public void setProperty(final String name, final Object value) { this.invokeMethod(name, value); } }; content.setDelegate(nodes); content.setResolveStrategy(Closure.DELEGATE_FIRST); Node root = (Node) nodes.invokeMethod(getName(), content); for (Node child : (List<Node>) root.value()) { merge(props, child, ""); } return false; }
public Node breadthFirst(Node T, Node searchNode){ Queue queue = new Queue(); queue.queue(T); while (!queue.isEmpty()) { Node curNode = queue.dequeue(); if (curNode == null) continue; if (curNode.value().equals(searchNode.value()) { return curNode; } queue.queue(curNode.left); queue.queue(curNode.right); } return null; //or throw exception not found }
private void convertNode(Node node, HierarchicalStreamWriter writer) { writer.startNode(node.name().toString()); if (node.value() instanceof Collection) { for (Object subNode : (Collection) node.value()) { convertNode((Node) subNode, writer); } } else { writer.setValue(node.value().toString()); } writer.endNode(); }
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; }
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; }
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; }
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; }
private void convertNode(Node node, HierarchicalStreamWriter writer) { writer.startNode(node.name().toString()); writeNodeAttributes(node, writer); if (node.value() instanceof Collection) { for (Object subNode : (Collection) node.value()) { convertNode((Node) subNode, writer); } } else { writer.setValue(node.value().toString()); } writer.endNode(); }
private void merge(Properties props, Node node, String prefix) { assert props != null; assert node != null; assert prefix != null; String name = prefix + node.name(); Object value = node.value(); if (value instanceof String) { props.setProperty(name, String.valueOf(value)); } Map attrs = node.attributes(); for (Object key : attrs.keySet()) { props.setProperty(name + "." + key, String.valueOf(attrs.get(key))); } for (Object child : node.children()) { if (child instanceof Node) { merge(props, (Node) child, name + "."); } } } }
private Node appendNodes(Closure c) { List list = parent().children(); int afterIndex = list.indexOf(this); List leftOvers = new ArrayList(list.subList(afterIndex + 1, list.size())); list.subList(afterIndex + 1, list.size()).clear(); Node lastAppended = null; for (Node child : buildChildrenFromClosure(c)) { lastAppended = parent().appendNode(child.name(), child.attributes(), child.value()); } parent().children().addAll(leftOvers); return lastAppended; }
private Xpp3Dom nodeToXpp3(final Node node) { Xpp3Dom dom = new Xpp3Dom((String) node.name()); Object value = node.value(); if (value instanceof String) { dom.setValue(String.valueOf(value)); } Map attrs = node.attributes(); for (Object key : attrs.keySet()) { dom.setAttribute(String.valueOf(key), String.valueOf(attrs.get(key))); } for (Object child : node.children()) { if (child instanceof Node) { dom.addChild(nodeToXpp3((Node) child)); } } return dom; } }
/** * 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); } } }
@Override public boolean onNodeChildren(FactoryBuilderSupport builder, Object node, Closure content) { Properties props = (Properties) node; NodeBuilder nodes = new NodeBuilder() { @Override protected void setClosureDelegate(final Closure c, final Object o) { c.setDelegate(this); c.setResolveStrategy(Closure.DELEGATE_FIRST); } @Override public void setProperty(final String name, final Object value) { this.invokeMethod(name, value); } }; content.setDelegate(nodes); content.setResolveStrategy(Closure.DELEGATE_FIRST); Node root = (Node) nodes.invokeMethod(getName(), content); for (Node child : (List<Node>) root.value()) { merge(props, child, ""); } return false; }