Java Code Examples for org.jdom2.xpath.XPath#addNamespace()

The following examples show how to use org.jdom2.xpath.XPath#addNamespace() . 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: VersionsValidatorTest.java    From wildfly-camel with Apache License 2.0 6 votes vote down vote up
@Test
public void testVersions() throws Exception {

    XPath xpath = XPath.newInstance("/ns:project/ns:properties");
    xpath.addNamespace(Namespace.getNamespace("ns", "http://maven.apache.org/POM/4.0.0"));
    Element node = (Element) xpath.selectSingleNode(wfcRoot);
    for (Object child : node.getChildren()) {
        String wfcKey = ((Element) child).getName();
        String wfcVal = ((Element) child).getText();
        String targetVal = getTargetValue(wfcKey);
        if (targetVal != null) {
            if (!targetVal.equals(wfcVal) && !targetVal.startsWith(wfcVal + ".redhat")) {
                problems.add(wfcKey + ": " + wfcVal + " => " + targetVal);
            }
        }
    }
    for (String line : problems) {
        System.err.println(line);
    }
    Assert.assertEquals("Mapping problems", Collections.emptyList(), problems);
}
 
Example 2
Source File: VersionsValidatorTest.java    From wildfly-camel with Apache License 2.0 6 votes vote down vote up
public String getTargetValue(String wfcKey) throws JDOMException {

        Element rootNode;
        if (wfcKey.startsWith("version.camel.")) {
            rootNode = camelRoot;
        } else if (wfcKey.startsWith("version.wildfly.")) {
            rootNode = wfRoot;
        } else {
            return null;
        }

        String targetKey = mapping.get(wfcKey);
        if (targetKey == null) {
            problems.add("Cannot find mapping for: " + wfcKey);
            return null;
        }
        XPath xpath = XPath.newInstance("/ns:project/ns:properties/ns:" + targetKey);
        xpath.addNamespace(Namespace.getNamespace("ns", "http://maven.apache.org/POM/4.0.0"));
        Element propNode = (Element) xpath.selectSingleNode(rootNode);
        if (propNode == null) {
            problems.add("Cannot obtain target property: " + targetKey);
            return null;
        }
        return propNode.getText();
    }
 
Example 3
Source File: NcmlParserUtil.java    From tds with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static List getNcMLElements(String path, Document doc) {

    // XPath doesn't support default namespaces, so we add nc as a prefix for the tags within the namespace!!!
    if (!path.startsWith(NS_PREFIX_ON_TAG) && !path.startsWith("/"))
      path = NS_PREFIX_ON_TAG + path;

    Pattern pattern = Pattern.compile("/\\w");
    Matcher matcher = pattern.matcher(path);

    StringBuilder sb = new StringBuilder();
    int currentChar = 0;
    while (matcher.find()) {

      sb.append(path.substring(currentChar, matcher.start() - currentChar + 1));
      if (!sb.toString().endsWith("/"))
        sb.append("/");
      sb.append(NS_PREFIX_ON_TAG);
      currentChar = matcher.start() + 1;
    }

    sb.append(path.substring(currentChar, path.length()));

    XPath xpath;
    try {

      xpath = XPath.newInstance(sb.toString());
      xpath.addNamespace(NS_PREFIX, doc.getRootElement().getNamespaceURI());
      return xpath.selectNodes(doc);

    } catch (JDOMException e) {

      e.printStackTrace();
    }

    return null;

  }
 
Example 4
Source File: ConsistentDatesTest.java    From tds with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
@Ignore("WMS not working")
public void checkWMSDates() throws JDOMException, IOException {
  String endpoint = TestOnLocalServer.withHttpPath(
      "/wms/cdmUnitTest/ncss/climatology/PF5_SST_Climatology_Monthly_1985_2001.nc?service=WMS&version=1.3.0&request=GetCapabilities");
  byte[] result = TestOnLocalServer.getContent(endpoint, 200, ContentType.xml);
  Reader in = new StringReader(new String(result, StandardCharsets.UTF_8));
  SAXBuilder sb = new SAXBuilder();
  Document doc = sb.build(in);

  if (show) {
    XMLOutputter fmt = new XMLOutputter(Format.getPrettyFormat());
    fmt.output(doc, System.out);
  }

  XPath xPath = XPath.newInstance("//wms:Dimension");
  xPath.addNamespace("wms", doc.getRootElement().getNamespaceURI());
  Element dimNode = (Element) xPath.selectSingleNode(doc);
  // List<String> content = Arrays.asList(dimNode.getText().trim().split(","));
  List<String> content = new ArrayList<>();
  for (String d : Arrays.asList(dimNode.getText().trim().split(","))) {
    // System.out.printf("Date= %s%n", d);
    CalendarDate cd = CalendarDate.parseISOformat(null, d);
    content.add(cd.toString());
  }

  assertEquals(expectedDatesAsDateTime, content);
}
 
Example 5
Source File: TestUCFPackage.java    From incubator-taverna-language with Apache License 2.0 5 votes vote down vote up
protected Object xpathSelectElement(Element element, String xpath) throws JDOMException {
	XPath x = XPath.newInstance(xpath);
	x.addNamespace(MANIFEST_NS);
	x.addNamespace(CONTAINER_NS);
	x.addNamespace(EXAMPLE_NS);
	return x.selectSingleNode(element);
}
 
Example 6
Source File: TestRDFXMLSerializer.java    From incubator-taverna-language with Apache License 2.0 5 votes vote down vote up
protected Object xpathSelectElement(Element element, String xpath) throws JDOMException {
	XPath x = XPath.newInstance(xpath);
	x.addNamespace(SCUFL2_NS);
	x.addNamespace(RDF_NS);
	x.addNamespace(RDSF_NS);
	x.addNamespace(BEANSHELL_NS);
	//x.addNamespace(XML_NS);

	return x.selectSingleNode(element);
}