groovy.util.Node Java Examples
The following examples show how to use
groovy.util.Node.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TransformActivity.java From mdw with Apache License 2.0 | 6 votes |
private void setGPathParamValue(String varName, String varType, Object value) throws ActivityException { if (isOutputDocument(varName)) { VariableTranslator translator = getPackage().getTranslator(varType); DocumentReferenceTranslator docRefTranslator = (DocumentReferenceTranslator) translator; try { if (value instanceof Node) new XmlNodePrinter(new PrintWriter(outputDocumentWriter)).print((Node)value); Object doc = docRefTranslator.toObject(outputDocumentWriter.toString(), varType); super.setVariableValue(varName, varType, doc); } catch (Exception ex) { getLogger().error(ex.getMessage(), ex); throw new ActivityException(ex.getMessage(), ex); } } else { super.setVariableValue(varName, varType, value); } }
Example #2
Source File: XmlNodePrinter.java From groovy with Apache License 2.0 | 6 votes |
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(); }
Example #3
Source File: XmlNodePrinter.java From groovy with Apache License 2.0 | 6 votes |
protected void printNamespace(Object object, NamespaceContext ctx) { if (namespaceAware) { if (object instanceof Node) { printNamespace(((Node) object).name(), ctx); } else if (object instanceof QName) { QName qname = (QName) object; String namespaceUri = qname.getNamespaceURI(); if (namespaceUri != null) { String prefix = qname.getPrefix(); if (!ctx.isPrefixRegistered(prefix, namespaceUri)) { ctx.registerNamespacePrefix(prefix, namespaceUri); out.print(" "); out.print("xmlns"); if (prefix.length() > 0) { out.print(":"); out.print(prefix); } out.print("=" + quote); out.print(namespaceUri); out.print(quote); } } } } }
Example #4
Source File: XmlTransformer.java From pushfish-android with BSD 2-Clause "Simplified" License | 6 votes |
private void printNode(Node node, Writer writer) { final PrintWriter printWriter = new PrintWriter(writer); if (GUtil.isTrue(publicId)) { printWriter.format("<!DOCTYPE %s PUBLIC \"%s\" \"%s\">%n", node.name(), publicId, systemId); } IndentPrinter indentPrinter = new IndentPrinter(printWriter, indentation) { @Override public void println() { printWriter.println(); } }; XmlNodePrinter nodePrinter = new XmlNodePrinter(indentPrinter); nodePrinter.setPreserveWhitespace(true); nodePrinter.print(node); printWriter.flush(); }
Example #5
Source File: ClasspathFile.java From gradle-modules-plugin with MIT License | 6 votes |
/** * Adds a grand-child {@link Node} named "attribute" and * {@code name="attributeName"} and * {@code value="true"}. * * <p>The implementation searches for the first child named "attributes" and * adds to that child a {@link Node} with name "attribute" and attributes * {@code attributeName="true"}. * * @param item * where a new grand-children is added to * * @param attributeName * name of attribute */ /* package */ void addAttribute(final Node item, final String attributeName) { // ... Note 1: In real usage (i.e. no test scenario) item has name "classpathentry". final Map<String, String> map = new ConcurrentSkipListMap<>(); // NOPMD use concurrent map map.put("name", attributeName); map.put("value", "true"); // --- find first child named "attributes" item.children().stream() // loop over all children .filter(c -> c instanceof Node) // better safe than sorry .filter(c -> NAME_CHILD.equals(((Node)c).name())) // nodes with name "attributes" .findFirst() .ifPresentOrElse( // ... item has a child named "attributes" // => add appropriate child to that // Note: Intentionally the return value of method ".appendNode(...) is ignored. c -> ((Node) c).appendNode(NAME_GRAND, map), // ... item has no child named "attributes" // => add appropriate child to item new AddAttribute(item, map) ); // end ifPresentOrElse(...) }
Example #6
Source File: ClasspathFile.java From gradle-modules-plugin with MIT License | 6 votes |
/** * Configures appropriate action if project has task "eclipseClasspath". * * @param task * responsible for generating {@code .classpath} file */ public void configure(final Task task) { // LOGGER.quiet("configure, task: {}", task.getClass()); // --- add functionality for enhancing the content of ".classpath"-file // Note: For more information see the manual for the eclipse-plugin. final EclipseClasspath eclipseClasspath = ((GenerateEclipseClasspath) task).getClasspath(); eclipseClasspath.file( xmlFileContentMerger -> { xmlFileContentMerger.withXml( xmlProvider -> { final Node rootNode = xmlProvider.asNode(); // show content of .classpath file before improving LOGGER.debug("addAction: rootNode.before improving:{}", rootNode); // improve improveEclipseClasspathFile(rootNode); // show content of .classpath file after improving LOGGER.debug("addAction: rootNode.after improving:{}", rootNode); } // end xmlProvider's lambda expression ); } // end xmlFileContentMerger's lambda expression ); }
Example #7
Source File: XmlNodePrinter.java From groovy with Apache License 2.0 | 5 votes |
protected void printList(List list, NamespaceContext ctx) { out.incrementIndent(); for (Object value : list) { NamespaceContext context = new NamespaceContext(ctx); /* * If the current value is a node, recurse into that node. */ if (value instanceof Node) { print((Node) value, context); continue; } printSimpleItem(value); } out.decrementIndent(); }
Example #8
Source File: ClasspathFileTest.java From gradle-modules-plugin with MIT License | 5 votes |
/** * Test method for {@link ClasspathFile#putJreOnModulePath(Node)}. */ @Test void test_putJreOnModulePath__Node() { // Test strategy: // ... assertion 1: method isJre(Node) works as intended // ... assertion 2: method hasNoAttributeModule(Node) works as intended // ... assertion 3: method moveToModulePath(Node) works as intended // --- a. rootNode with a bunch of entries and entries differing slightly // --- a. rootNode with a bunch of entries and entries differing slightly final Node rootNode = new Node(null, "root"); // a.1: difference in name of item final Map<String, String> mapA1 = new ConcurrentSkipListMap<>(); mapA1.put("path", "prefix" + "JRE_CONTAINER" + "suffix"); mapA1.put("kind", "con"); rootNode.appendNode("classpathentry", mapA1); // ok rootNode.appendNode("classpathEntry", mapA1); // not classpathentry // --- improve insDut.putJreOnModulePath(rootNode); // --- check assertEquals( "root[attributes={}; value=[" // a.1, begin + "classpathentry[attributes={kind=con, path=prefixJRE_CONTAINERsuffix}; value=[" + "attributes[attributes={}; value=[" + "attribute[attributes={name=module, value=true}; value=[]]" + "]]]" + "], " + "classpathEntry[attributes={kind=con, path=prefixJRE_CONTAINERsuffix}; value=[]]" + "]]", rootNode.toString() ); }
Example #9
Source File: ClasspathFile.java From gradle-modules-plugin with MIT License | 5 votes |
/** * Estimates whether given {@code item} contains a value for a key named "module". * * @param item * {@link Node} for which the estimation is performed * * @return false if {@code item} has at least one child named "attributes" and that child has * at least one child named "attribute" containing an attribute named "module", * true otherwise */ /* package */ boolean hasNoAttributeModule(final Node item) { // ... Note 1: In real usage (i.e. no test scenario) item has name "classpathentry". return item.children().stream() // loop over all children .filter(c -> c instanceof Node) // better safe than sorry .filter(c -> NAME_CHILD.equals(((Node)c).name())) // child named "attributes" .filter(c -> hasAttributeNamed((Node)c, "module")) // grand-child with attribute "module" .findFirst() .isEmpty(); }
Example #10
Source File: ClasspathFile.java From gradle-modules-plugin with MIT License | 5 votes |
/** * Estimates whether given {@code item} contains a value for a key named "test". * * @param item * {@link Node} for which the estimation is performed * * @return false if {@code item} has at least one child named "attributes" and that child has * at least one child named "attribute" containing an attribute named "test", * true otherwise */ /* package */ boolean hasNoAttributeTest(final Node item) { // ... Note 1: In real usage (i.e. no test scenario) item has name "classpathentry". return item.children().stream() // loop over all children .filter(c -> c instanceof Node) // better safe than sorry .filter(c -> NAME_CHILD.equals(((Node)c).name())) // child named "attributes" .filter(c -> hasAttributeNamed((Node)c, "test")) // grand-child with attribute "test" .findFirst() .isEmpty(); }
Example #11
Source File: ScoverageFunctionalTest.java From gradle-scoverage with Apache License 2.0 | 5 votes |
private Double coverage(File reportDir, CoverageType coverageType) throws IOException, SAXException, ParseException { File reportFile = reportDir.toPath().resolve(coverageType.getFileName()).toFile(); Node xml = parser.parse(reportFile); Object attribute = xml.attribute(coverageType.getParamName()); double rawValue = NumberFormat.getInstance().parse(attribute.toString()).doubleValue(); return coverageType.normalize(rawValue) * 100.0; }
Example #12
Source File: ClasspathFile.java From gradle-modules-plugin with MIT License | 5 votes |
/** * Retrieves gradle scope from given {@code item}. * * <p>The following actions are performed: * <ol> * <li>Searches in the children of {@code item} for the first one named {@link #NAME_CHILD}. * <li>If such a child doesn't exist an empty {@link String} is returned. * <li>If such a child exists then in the children of that child for first one named * {@link #NAME_GRAND} and an attribute with name {@link #NAME_ATTRIBUTE} is searched. * <li>If such an attribute is present then its value is returned. * <li>If such an attribute is absent then an empty {@link String} is returned. * </ol> * * @param item * investigated for a gradle scope * * @return value of attribute {@link #NAME_ATTRIBUTE} or * an empty {@link String} if such an attribute is not present */ /* package */ String getGradleScope(final Node item) { final String empty = ""; // value if gradle scope is absent // ... Note 1: In real usage (i.e. no test scenario) item has name "classpathentry". final Optional<Node> oChild = item.children().stream() // loop over all children .filter(c -> c instanceof Node) // better safe than sorry .filter(c -> NAME_CHILD.equals(((Node)c).name())) // with name "attributes" .findFirst(); // first child named "attributes" if (oChild.isPresent()) { // ... child of type Node and name "attributes" is present // => search there for grand-child with gradle scope attribute final Optional<Node> oGrand = getAttributeNamed(oChild.get(), NAME_ATTRIBUTE); if (oGrand.isPresent()) { // ... grandChild of type Node named "attribute" with attribute named "gradle_used_by_scope" // => get its value (if there is one) final Node grand = oGrand.get(); final Object value = grand.attribute("value"); // returns null if value is absent return (null == value) ? empty : value.toString(); // avoid NullPointerException } // end if (oGrand present) // ... no appropriate grand-child present => return empty string } // end if (oChild present) // ... no appropriate child present => return empty string return empty; }
Example #13
Source File: XmlUtil.java From groovy with Apache License 2.0 | 5 votes |
private static String asString(Node node) { Writer sw = new StringBuilderWriter(); PrintWriter pw = new PrintWriter(sw); XmlNodePrinter nodePrinter = new XmlNodePrinter(pw); nodePrinter.setPreserveWhitespace(true); nodePrinter.print(node); return sw.toString(); }
Example #14
Source File: XmlTransformer.java From pushfish-android with BSD 2-Clause "Simplified" License | 5 votes |
public Node asNode() { if (node == null) { try { node = new XmlParser().parseText(toString()); } catch (Exception e) { throw UncheckedException.throwAsUncheckedException(e); } builder = null; element = null; } return node; }
Example #15
Source File: XmlTransformer.java From pushfish-android with BSD 2-Clause "Simplified" License | 5 votes |
private void removeEmptyTextNodes(org.w3c.dom.Node node) { org.w3c.dom.NodeList children = node.getChildNodes(); for (int i = 0; i < children.getLength(); i++) { org.w3c.dom.Node child = children.item(i); if (child.getNodeType() == org.w3c.dom.Node.TEXT_NODE && child.getNodeValue().trim().length() == 0) { node.removeChild(child); } else { removeEmptyTextNodes(child); } } }
Example #16
Source File: XmlNodePrinter.java From groovy with Apache License 2.0 | 5 votes |
private String getName(Object object) { if (object instanceof String) { return (String) object; } else if (object instanceof QName) { QName qname = (QName) object; if (!namespaceAware) { return qname.getLocalPart(); } return qname.getQualifiedName(); } else if (object instanceof Node) { Object name = ((Node) object).name(); return getName(name); } return object.toString(); }
Example #17
Source File: XmlTransformer.java From Pushjet-Android with BSD 2-Clause "Simplified" License | 5 votes |
public Node asNode() { if (node == null) { try { node = new XmlParser().parseText(toString()); } catch (Exception e) { throw UncheckedException.throwAsUncheckedException(e); } builder = null; element = null; } return node; }
Example #18
Source File: XmlTransformer.java From pushfish-android with BSD 2-Clause "Simplified" License | 5 votes |
public Node asNode() { if (node == null) { try { node = new XmlParser().parseText(toString()); } catch (Exception e) { throw UncheckedException.throwAsUncheckedException(e); } builder = null; element = null; } return node; }
Example #19
Source File: CuriostackRootPlugin.java From curiostack with MIT License | 5 votes |
private static Optional<Node> findChild(Node node, String name) { // Should work. @SuppressWarnings("unchecked") List<Node> children = (List<Node>) node.children(); return children.stream() .filter( n -> n.name().equals(name) || (n.name() instanceof QName && ((QName) n.name()).getLocalPart().equals(name))) .findFirst(); }
Example #20
Source File: XmlTransformer.java From Pushjet-Android with BSD 2-Clause "Simplified" License | 5 votes |
private void removeEmptyTextNodes(org.w3c.dom.Node node) { org.w3c.dom.NodeList children = node.getChildNodes(); for (int i = 0; i < children.getLength(); i++) { org.w3c.dom.Node child = children.item(i); if (child.getNodeType() == org.w3c.dom.Node.TEXT_NODE && child.getNodeValue().trim().length() == 0) { node.removeChild(child); } else { removeEmptyTextNodes(child); } } }
Example #21
Source File: CuriostackRootPlugin.java From curiostack with MIT License | 5 votes |
private static Node findOrCreateChild(Node parent, String type, String name) { Map<String, String> attributes = new HashMap<>(); attributes.put("name", name); return findChild( parent, node -> node.name().equals(type) && node.attribute("name").equals(name)) .orElseGet(() -> parent.appendNode(type, attributes)); }
Example #22
Source File: UtraceHostGeoLookup.java From brooklyn-server with Apache License 2.0 | 5 votes |
@Nullable private static Node getFirstChild(Node xml, String field) { if (xml==null) return null; NodeList nl = (NodeList)xml.get(field); if (nl==null || nl.isEmpty()) return null; return (Node)nl.get(0); }
Example #23
Source File: XmlTransformer.java From Pushjet-Android with BSD 2-Clause "Simplified" License | 5 votes |
private void removeEmptyTextNodes(org.w3c.dom.Node node) { org.w3c.dom.NodeList children = node.getChildNodes(); for (int i = 0; i < children.getLength(); i++) { org.w3c.dom.Node child = children.item(i); if (child.getNodeType() == org.w3c.dom.Node.TEXT_NODE && child.getNodeValue().trim().length() == 0) { node.removeChild(child); } else { removeEmptyTextNodes(child); } } }
Example #24
Source File: GroovyNodeTranslator.java From mdw with Apache License 2.0 | 5 votes |
public Object fromDomNode(org.w3c.dom.Node domNode) throws TranslationException { try { return toObject(DomHelper.toXml(domNode), null); } catch (Exception ex) { throw new TranslationException(ex.getMessage(), ex); } }
Example #25
Source File: XmlTransformer.java From Pushjet-Android with BSD 2-Clause "Simplified" License | 5 votes |
public Node asNode() { if (node == null) { try { node = new XmlParser().parseText(toString()); } catch (Exception e) { throw UncheckedException.throwAsUncheckedException(e); } builder = null; element = null; } return node; }
Example #26
Source File: XmlTransformer.java From pushfish-android with BSD 2-Clause "Simplified" License | 4 votes |
public XmlProviderImpl(Node original) { this.node = original; }
Example #27
Source File: ClasspathFileTest.java From gradle-modules-plugin with MIT License | 4 votes |
/** * Test method for {@link ClasspathFile#improveEclipseClasspathFile(Node)}. */ @Test void test_improveEclipseClasspathFile__Node() { // Test strategy: // ... assertion 1: method putJreOnModulePath(Node) works as intended // --- a. JRE, rootNode with a bunch of entries and entries differing slightly // --- b. Main, rootNode with a bunch of entries and entries differing slightly // --- c. Test, rootNode with a bunch of entries and entries differing slightly final Node root = new Node(null, "root"); // --- a. JRE, rootNode with a bunch of entries and entries differing slightly // a.1: difference in name of item final Map<String, String> mapA1 = new ConcurrentSkipListMap<>(); mapA1.put("path", "prefix" + "JRE_CONTAINER" + "suffix"); mapA1.put("kind", "con"); root.appendNode("classpathentry", mapA1); // ok root.appendNode("classpathEntry", mapA1); // not classpathentry // --- b. Main, rootNode with a bunch of entries and entries differing slightly // b.1: difference in name of item final Map<String, String> kind = Map.of("kind", "lib"); final Map<String, String> mapB1 = new ConcurrentSkipListMap<>(); mapB1.put("name", ClasspathFile.NAME_ATTRIBUTE); mapB1.put("value", "main"); root.appendNode("classpathentry", kind) // ok .appendNode(ClasspathFile.NAME_CHILD) .appendNode(ClasspathFile.NAME_GRAND, mapB1); root.appendNode("Classpathentry", kind) // not classpathentry .appendNode(ClasspathFile.NAME_CHILD) .appendNode(ClasspathFile.NAME_GRAND, mapB1); // --- c. Test, rootNode with a bunch of entries and entries differing slightly // c.1: difference in name of item final Map<String, String> mapC1 = new ConcurrentSkipListMap<>(); mapC1.put("name", ClasspathFile.NAME_ATTRIBUTE); mapC1.put("value", "test"); root.appendNode("classpathentry") // ok .appendNode(ClasspathFile.NAME_CHILD) .appendNode(ClasspathFile.NAME_GRAND, mapC1); root.appendNode("Classpathentry") // not classpathentry .appendNode(ClasspathFile.NAME_CHILD) .appendNode(ClasspathFile.NAME_GRAND, mapC1); // --- improve insDut.improveEclipseClasspathFile(root); // --- check assertEquals( "root[attributes={}; value=[" // a.1, begin + "classpathentry[attributes={kind=con, path=prefixJRE_CONTAINERsuffix}; value=[" + "attributes[attributes={}; value=[" + "attribute[attributes={name=module, value=true}; value=[]]" + "]]]" + "], " + "classpathEntry[attributes={kind=con, path=prefixJRE_CONTAINERsuffix}; value=[]], " // b.1, begin + "classpathentry[attributes={kind=lib}; value=[" + "attributes[attributes={}; value=[" + "attribute[attributes={name=gradle_used_by_scope, value=main}; value=[]], " + "attribute[attributes={name=module, value=true}; value=[]]" + "]]" + "]], " + "Classpathentry[attributes={kind=lib}; value=[" + "attributes[attributes={}; value=[" + "attribute[attributes={name=gradle_used_by_scope, value=main}; value=[]]" + "]]" + "]], " // c.1, begin + "classpathentry[attributes={}; value=[" + "attributes[attributes={}; value=[" + "attribute[attributes={name=gradle_used_by_scope, value=test}; value=[]], " + "attribute[attributes={name=test, value=true}; value=[]]" + "]]" + "]], " + "Classpathentry[attributes={}; value=[" + "attributes[attributes={}; value=[" + "attribute[attributes={name=gradle_used_by_scope, value=test}; value=[]]" + "]]" + "]]" // end + "]]", root.toString() ); }
Example #28
Source File: ClasspathFileTest.java From gradle-modules-plugin with MIT License | 4 votes |
/** * Test method for {@link ClasspathFile#markMain(groovy.util.Node)}. */ @Test void test_markMain__Node() { // ... assertion 1: method isKindOf(Node, String) works as expected // ... assertion 2: method getGradleScope(Node) works as expected // ... assertion 3: method hasNoAttributeModule(Node) works as expected // ... assertion 4: method addAttribute(Node, String) works as expected // => not much to check hereafter // Test strategy: // --- a. items with improper name are not changed // --- b. items with proper name are changed final Node root = new Node(null, "root"); final Map<String, String> kind = Map.of("kind", "lib"); final Map<String, String> map = new ConcurrentSkipListMap<>(); map.put("name", "gradle_used_by_scope"); map.put("value", "main,test"); // --- a. items with improper name are not changed root.appendNode("classPathentry", kind) // wrong capitalization .appendNode(ClasspathFile.NAME_CHILD) .appendNode(ClasspathFile.NAME_GRAND, map); // --- b. items with proper name are changed root.appendNode("classpathentry", kind) .appendNode(ClasspathFile.NAME_CHILD) .appendNode(ClasspathFile.NAME_GRAND, map); insDut.markMain(root); assertEquals( "root[attributes={}; value=[" + "classPathentry[attributes={kind=lib}; value=[" + "attributes[attributes={}; value=[" + "attribute[attributes={name=gradle_used_by_scope, value=main,test}; value=[]]" + "]]" + "]], " + "classpathentry[attributes={kind=lib}; value=[" + "attributes[attributes={}; value=[" + "attribute[attributes={name=gradle_used_by_scope, value=main,test}; value=[]], " + "attribute[attributes={name=module, value=true}; value=[]]" + "]]" + "]]" + "]]", root.toString() ); }
Example #29
Source File: XmlTransformer.java From pushfish-android with BSD 2-Clause "Simplified" License | 4 votes |
private XmlProviderImpl doTransform(Node original) { return doTransform(new XmlProviderImpl(original)); }
Example #30
Source File: XmlTransformer.java From pushfish-android with BSD 2-Clause "Simplified" License | 4 votes |
public void transform(Node original, Writer destination) { doTransform(original).writeTo(destination); }