List of usage examples for org.jdom2.xpath XPathBuilder setNamespace
public boolean setNamespace(Namespace namespace)
From source file:ca.nrc.cadc.vosi.avail.CheckWebService.java
License:Open Source License
void checkReturnedXml(String strXml) throws CheckException { Document doc;// w w w .j a v a 2s . c o m String xpathStr; try { StringReader reader = new StringReader(strXml); doc = XmlUtil.buildDocument(reader, schemaMap); //get namespace and/or prefix from Document, then create xpath based on the prefix String nsp = doc.getRootElement().getNamespacePrefix(); //Namespace Prefix if (nsp != null && nsp.length() > 0) nsp = nsp + ":"; else nsp = ""; xpathStr = "/" + nsp + "availability/" + nsp + "available"; XPathBuilder<Element> builder = new XPathBuilder<Element>(xpathStr, Filters.element()); Namespace ns = Namespace.getNamespace(VOSI.NS_PREFIX, VOSI.AVAILABILITY_NS_URI); builder.setNamespace(ns); XPathExpression<Element> xpath = builder.compileWith(XPathFactory.instance()); Element eleAvail = xpath.evaluateFirst(doc); log.debug(eleAvail); String textAvail = eleAvail.getText(); // TODO: is this is actually valid? is the content not constrained by the schema? if (textAvail == null) throw new CheckException(wsURL + " output is invalid: no content in <available> element", null); if (!textAvail.equalsIgnoreCase("true")) { xpathStr = "/" + nsp + "availability/" + nsp + "note"; builder = new XPathBuilder<Element>(xpathStr, Filters.element()); builder.setNamespace(ns); xpath = builder.compileWith(XPathFactory.instance()); Element eleNotes = xpath.evaluateFirst(doc); String textNotes = eleNotes.getText(); throw new CheckException("service " + wsURL + " is not available, reported reason: " + textNotes, null); } } catch (IOException e) { // probably an incorrect setup or bug in the checks throw new RuntimeException("failed to test " + wsURL, e); } catch (JDOMException e) { throw new CheckException(wsURL + " output is invalid", e); } }