nu.xom.Node Java Examples

The following examples show how to use nu.xom.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: FreemarkerTest.java    From cloud-opensource-java with Apache License 2.0 6 votes vote down vote up
@Test
public void testVersionIndex()
    throws IOException, TemplateException, URISyntaxException, ParsingException {
  Path output =
      DashboardMain.generateVersionIndex(
          "com.google.cloud",
          "libraries-bom",
          ImmutableList.of("1.0.0", "2.0.0", "2.1.0-SNAPSHOT"));
  Assert.assertTrue(
      output.endsWith(Paths.get("target", "com.google.cloud", "libraries-bom", "index.html")));
  Assert.assertTrue(Files.isRegularFile(output));

  Document document = builder.build(output.toFile());
  Nodes links = document.query("//a/@href");
  Assert.assertEquals(3, links.size());
  Node snapshotLink = links.get(2);
  // 2.1.0-SNAPSHOT has directory 'snapshot'
  Assert.assertEquals("snapshot/index.html", snapshotLink.getValue());
}
 
Example #2
Source File: DashboardTest.java    From cloud-opensource-java with Apache License 2.0 6 votes vote down vote up
@Test
public void testDashboard_recommendedCoordinates() {
  Nodes recommendedListItem = dashboard.query("//ul[@id='recommended']/li");
  Assert.assertTrue(recommendedListItem.size() > 100);

  // fails if these are not valid Maven coordinates
  for (Node node : recommendedListItem) {
    new DefaultArtifact(node.getValue());
  }

  ImmutableList<String> coordinateList =
      Streams.stream(recommendedListItem).map(Node::getValue).collect(toImmutableList());
  
  ArrayList<String> sorted = new ArrayList<>(coordinateList);
  Comparator<String> comparator = new SortWithoutVersion();
  Collections.sort(sorted, comparator);

  for (int i = 0; i < sorted.size(); i++) {
    Assert.assertEquals(
        "Coordinates are not sorted: ", sorted.get(i), coordinateList.get(i));
  }
}
 
Example #3
Source File: XPathProcessor.java    From GeoTriples with Apache License 2.0 6 votes vote down vote up
public String execute(Node node, String expression)
		throws SaxonApiException {

	Processor proc = new Processor(false);
	XPathCompiler xpath = proc.newXPathCompiler();
	DocumentBuilder builder = proc.newDocumentBuilder();

	String fileName = getClass().getResource(
			map.getLogicalSource().getIdentifier()).getFile();

	XdmNode doc = builder.build(new File(fileName));
	String expre = replace(node, expression);
	expression = expression.replaceAll(
			"\\{" + expression.split("\\{")[1].split("\\}")[0] + "\\}", "'"
					+ expre + "'");

	XPathSelector selector = xpath.compile(expression).load();
	selector.setContextItem(doc);

	// Evaluate the expression.
	Object result = selector.evaluate();

	return result.toString();

}
 
Example #4
Source File: DashboardTest.java    From cloud-opensource-java with Apache License 2.0 5 votes vote down vote up
@Test // https://github.com/GoogleCloudPlatform/cloud-opensource-java/issues/617
public void testPlural() {
  Nodes statisticItems = dashboard.query("//section[@class='statistics']/div/div");
  for (Node node : statisticItems) {
    Node h2 = node.query("h2").get(0);
    Node span = node.query("span").get(0);
    int errorCount = Integer.parseInt(h2.getValue());
    if (errorCount == 1) {
      String message = span.getValue();
      Assert.assertEquals("Has Linkage Errors", message);
    }
  }
  
  Assert.assertFalse(dashboard.toXML().contains("1 HAVE"));
}
 
Example #5
Source File: XOMTreeBuilder.java    From caja with Apache License 2.0 5 votes vote down vote up
@Override protected void insertFosterParentedChild(Element child,
        Element table, Element stackParent) throws SAXException {
    try {
        Node parent = table.getParent();
        if (parent != null) { // always an element if not null
            ((ParentNode)parent).insertChild(child, indexOfTable(table, stackParent));
            cachedTableIndex++;
        } else {
            stackParent.appendChild(child);
        }
    } catch (XMLException e) {
        fatal(e);
    }
}
 
Example #6
Source File: XOMTreeBuilder.java    From caja with Apache License 2.0 5 votes vote down vote up
@Override protected void insertFosterParentedCharacters(String text,
        Element table, Element stackParent) throws SAXException {
    try {
        Node child = nodeFactory.makeText(text);
        Node parent = table.getParent();
        if (parent != null) { // always an element if not null
            ((ParentNode)parent).insertChild(child, indexOfTable(table, stackParent));
            cachedTableIndex++;
        } else {
            stackParent.appendChild(child);
        }            
    } catch (XMLException e) {
        fatal(e);
    }
}
 
Example #7
Source File: XPathProcessor.java    From GeoTriples with Apache License 2.0 5 votes vote down vote up
@Override
public List<Object> extractValueFromNode(Object node, String expression) {
	if (expression.contains(Config.GEOTRIPLES_AUTO_ID)
			&& expression.split("/").length > 1) { // this is when
													// referencing an
													// element and ask or
													// the GeoTriplesID
		// used in rr:template "mpla/GeoTriplesID"
		return extractValueFromNode((NodeItem) node,
				expression.replaceAll("/*" + Config.GEOTRIPLES_AUTO_ID, ""));
	}
	return extractValueFromNode((Node) (((NodeItem) node).xml), expression);
}
 
Example #8
Source File: XPathProcessor.java    From GeoTriples with Apache License 2.0 5 votes vote down vote up
public WorkerPerformOnNode(RMLPerformer performer, Resource subject,
		Node n, SesameDataSet dataset, TriplesMap parentTriplesMap,
		TriplesMap map, List<Statement> statements2) {
	this.performer = performer;
	this.subject = subject;
	this.n = n;
	this.dataset = dataset;
	this.parentTriplesMap = parentTriplesMap;
	this.map = map;
	this.statements=statements2;
}
 
Example #9
Source File: XmlUtils.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
public static String getXmlDefinition(Document doc, String entityType, String entityName) {
  Node node = getNode(doc, entityType, entityName);
  if (node != null) {
    return node.toXML();
  } else {
    return null;
  }
}
 
Example #10
Source File: XmlUtils.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
public static Node getNode(Document doc, String entityType, String entityName) {
  String query = createQueryStringByName(entityType, entityName);
  Nodes nodes = doc.query(query);
  if (nodes != null && nodes.size() > 0) {
    return nodes.get(0);
  } 
  return null;
}
 
Example #11
Source File: XmlUtils.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
public static String getXmlDefinition(Document doc, String entityType, String entityName) {
  Node node = getNode(doc, entityType, entityName);
  if (node != null) {
    return node.toXML();
  } else {
    return null;
  }
}
 
Example #12
Source File: XmlUtils.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
public static Node getNode(Document doc, String entityType, String entityName) {
  String query = createQueryStringByName(entityType, entityName);
  Nodes nodes = doc.query(query);
  if (nodes != null && nodes.size() > 0) {
    return nodes.get(0);
  } 
  return null;
}
 
Example #13
Source File: XomReader.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public String getValue() {
    // currentElement.getValue() not used as this includes text of child elements, which we don't want.
    StringBuffer result = new StringBuffer();
    int childCount = currentElement.getChildCount();
    for(int i = 0; i < childCount; i++) {
        Node child = currentElement.getChild(i);
        if (child instanceof Text) {
            Text text = (Text) child;
            result.append(text.getValue());
        }
    }
    return result.toString();
}
 
Example #14
Source File: DashboardTest.java    From cloud-opensource-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testLinkageReports() {
  Nodes reports = details.query("//p[@class='jar-linkage-report']");
  // appengine-api-sdk, shown as first item in linkage errors, has these errors
  Truth.assertThat(trimAndCollapseWhiteSpace(reports.get(0).getValue()))
      .isEqualTo(
          "91 target classes causing linkage errors referenced from 501 source classes.");

  Nodes dependencyPaths = details.query("//p[@class='linkage-check-dependency-paths']");
  Node dependencyPathMessageOnProblem = dependencyPaths.get(dependencyPaths.size() - 4);
  Assert.assertEquals(
      "The following paths contain com.google.guava:guava-jdk5:13.0:",
      trimAndCollapseWhiteSpace(dependencyPathMessageOnProblem.getValue()));

  Node dependencyPathMessageOnSource = dependencyPaths.get(dependencyPaths.size() - 3);
  Assert.assertEquals(
      "The following paths contain com.google.guava:guava:27.1-android:",
      trimAndCollapseWhiteSpace(dependencyPathMessageOnSource.getValue()));

  Nodes nodesWithPathsSummary = details.query("//p[@class='linkage-check-dependency-paths']");
  Truth.assertWithMessage("The dashboard should not show repetitive dependency paths")
      .that(nodesWithPathsSummary)
      .comparingElementsUsing(
          Correspondence.<Node, String>transforming(
              node -> trimAndCollapseWhiteSpace(node.getValue()), "has text"))
      .contains(
          "Dependency path 'commons-logging:commons-logging > javax.servlet:servlet-api' exists"
              + " in all 1337 dependency paths. Example path:"
              + " com.google.http-client:google-http-client:1.29.1 (compile) /"
              + " org.apache.httpcomponents:httpclient:4.5.5 (compile) /"
              + " commons-logging:commons-logging:1.2 (compile) / javax.servlet:servlet-api:2.3"
              + " (provided, optional)");
}
 
Example #15
Source File: DashboardTest.java    From cloud-opensource-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testDashboard_statisticBox() {
  Nodes artifactCount =
      dashboard.query("//div[@class='statistic-item statistic-item-green']/h2");
  Assert.assertTrue(artifactCount.size() > 0);
  for (Node artifactCountElement : artifactCount) {
    String value = artifactCountElement.getValue().trim();
    Assert.assertTrue(value, Integer.parseInt(value) > 0);
  }
}
 
Example #16
Source File: XmlUtils.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
public static void deleteNode(Document doc , String entityType, String entityName) {
  Node node = getNode(doc, entityType, entityName);
  if (node != null) {
    node.detach();
  }
}
 
Example #17
Source File: XPathProcessor.java    From GeoTriples with Apache License 2.0 4 votes vote down vote up
private String replace(Node node, String expression) {
	Object expre = extractValueFromNode(node,
			expression.split("\\{")[1].split("\\}")[0]).get(0);
	log.info("[XPathProcessor:execute] expre " + expre);
	return expre.toString();
}
 
Example #18
Source File: XmlUtils.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
public static void deleteNode(Document doc , String entityType, String entityName) {
  Node node = getNode(doc, entityType, entityName);
  if (node != null) {
    node.detach();
  }
}
 
Example #19
Source File: XOMBuilder.java    From GeoTriples with Apache License 2.0 4 votes vote down vote up
@Override
protected void removeFromParent(Object node) {
    ((Node) curNode).detach();
}