/** * 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; }
/** * Creates a new node as a child of the current node. * * @param name the name of the new node * @param attributes the attributes of the new node * @param value the value of the new node * @return the newly created <code>Node</code> */ public Node appendNode(Object name, Map attributes, Object value) { return new Node(this, name, attributes, value); }
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); }
/** * Adds sibling nodes (defined using builder-style notation via a Closure) after the current node. * * @param c A Closure defining the new sibling nodes to add using builder-style notation. */ public void plus(Closure c) { if (parent() == null) { throw new UnsupportedOperationException("Adding sibling nodes to the root node is not supported"); } appendNodes(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); } } }
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 + "."); } } } }
/** * Provides lookup of elements by non-namespaced name * * @param key the name (or shortcut key) of the node(s) of interest * @return the nodes which match key */ public Object get(String key) { if (key != null && key.charAt(0) == '@') { String attributeName = key.substring(1); return attributes().get(attributeName); } if ("..".equals(key)) { return parent(); } if ("*".equals(key)) { return children(); } if ("**".equals(key)) { return depthFirst(); } return getByName(key); }
/** * 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; }
@SuppressWarnings("unchecked") @Override protected void applyOnce(Project project) { extension = project.getExtensions().create(ResourceFiltersExtension.NAME, ResourceFiltersExtension.class); EclipseProjectPlugin.modifyEclipseProject(project, eclipseModel -> { eclipseModel.getProject().getFile().getXmlTransformer().addAction(xmlProvider -> { Node rootNode = (Node) xmlProvider.asNode(); // remove the old filteredResources List<Node> toRemove = ((List<Node>) rootNode.children()).stream() .filter(Objects::nonNull) .filter(node -> FILTERED_RESOURCES.equals(node.name())) .collect(Collectors.toList()); toRemove.forEach(rootNode::remove); // now add ours Node filteredResources = rootNode.appendNode(FILTERED_RESOURCES); for (ResourceFilter toExclude : extension.filters) { toExclude.appendToFilteredResources(filteredResources); } }); }); }
/** Creates an XML node representing all the repos in this model. */ private Node sourceNode(Node parent) { Node source = new Node(parent, "source"); @SuppressWarnings("unchecked") BiConsumer<Iterable<String>, Consumer<Map<String, String>>> addRepos = (urls, repoAttributes) -> { for (String url : urls) { Node repository = source.appendNode("repository"); repository.attributes().put("location", url); repoAttributes.accept(repository.attributes()); } }; addRepos.accept(repos, Consumers.doNothing()); addRepos.accept(metadataRepos, repoAttr -> repoAttr.put("kind", "metadata")); addRepos.accept(artifactRepos, repoAttr -> repoAttr.put("kind", "artifact")); return source; }
Iterator<Node> classpathEntries = classpathNode.children().iterator(); while (classpathEntries.hasNext()) { Node entry = classpathEntries.next(); String path = (String) entry.attributes().get("path"); if (path != null && !path.isEmpty()) { if (path.endsWith(".jar")) { Node entry = classpathNode.appendNode("classpathentry"); entry.attributes().put("combineaccessrules", "true"); entry.attributes().put("exported", "true"); entry.attributes().put("kind", "src"); entry.attributes().put("path", "/" + projectDep); }; classpathEntries = classpathNode.children().iterator(); while (classpathEntries.hasNext()) { Node entry = classpathEntries.next(); String path = (String) entry.attributes().get("path"); if (path != null && path.endsWith(jar)) { classpathEntries.remove();
@Override public Object getAttribute(final Object object, final String attribute) { Node n = (Node) object; return n.get("@" + attribute); }
@Override public void setAttribute(final Object object, final String attribute, final Object newValue) { for (Object o : (NodeList) object) { Node node = (Node) o; node.attributes().put(attribute, newValue); } }
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(); }
protected void printName(Node node, NamespaceContext ctx, boolean begin, boolean preserve) { if (node == null) { throw new NullPointerException("Node must not be null."); } Object name = node.name(); if (name == null) { throw new NullPointerException("Name must not be null."); } if (!preserve || begin) printLineBegin(); out.print("<"); if (!begin) { out.print("/"); } out.print(getName(node)); if (ctx != null) { printNamespace(node, ctx); } if (begin) { printNameAttributes(node.attributes(), ctx); } out.print(">"); if (!preserve || !begin) printLineEnd(); }
protected boolean printSpecialNode(Node node) { Object name = node.name(); if (name != null && name instanceof QName) { QName qn = (QName) name; // check uri and for legacy cases just check prefix name (not recommended) if (qn.getNamespaceURI().equals("http://groovy.codehaus.org/2005/gsp") || qn.getPrefix().equals("gsp")) { String s = qn.getLocalPart(); if (s.length() == 0) { throw new RuntimeException("No local part after 'gsp:' given in node " + node); } printGroovyTag(s, node.text()); return true; } } return false; } }
protected void printName(Node node) { Object name = node.name(); if (name != null) { out.print(name.toString()); } else { out.print("null"); } }
/** * Converts the text of this GPathResult to a Double object. * * @return the GPathResult, converted to a <code>Double</code> */ public Double toDouble() { if(textIsEmptyOrNull()){ return null; } return StringGroovyMethods.toDouble((CharSequence)text()); }