Java Code Examples for groovy.util.Node#attribute()

The following examples show how to use groovy.util.Node#attribute() . 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: ClasspathFile.java    From gradle-modules-plugin with MIT License 5 votes vote down vote up
/**
 * 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 2
Source File: ScoverageFunctionalTest.java    From gradle-scoverage with Apache License 2.0 5 votes vote down vote up
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 3
Source File: ClasspathFile.java    From gradle-modules-plugin with MIT License 3 votes vote down vote up
/**
 * Estimates whether given {@link Node} belongs to a JRE description.
 *
 * @param item
 *        a {@link Node} investigated whether it is of a certain kind
 *
 * @return true if given {@link Node} is kind of "con" and has an attribute "path" containing
 *         "JRE_CONTAINER",
 *         false otherwise
 */
/* package */ boolean isJre(final Node item) {
  // ... Note 1: In real usage (i.e. no test scenario) node has name "classpathentry".

  final Object path = item.attribute("path"); // might return null

  return isKindOf(item, "con") && (null != path) && path.toString().contains(NAME_JRE);
}
 
Example 4
Source File: ClasspathFile.java    From gradle-modules-plugin with MIT License 3 votes vote down vote up
/**
 * Estimates whether given {@link Node} is of certain kind.
 *
 * @param item
 *        a {@link Node} investigated whether it is of a certain kind
 *
 * @param kind
 *        type for which the given {@link Node} is checked
 *
 * @return true if the {@link Node} has attribute "kind" and the value of that attribute is
 *         equal to the given one in parameter {@code kind},
 *         false otherwise
 */
/* package */ boolean isKindOf(final Node item, final String kind) {
  // ... Note 1: In real usage (i.e. no test scenario) node has name "classpathentry".

  final Object attr = item.attribute("kind"); // might return null

  return kind.equals(attr);
}