Example usage for org.jdom2.xpath XPathBuilder compileWith

List of usage examples for org.jdom2.xpath XPathBuilder compileWith

Introduction

In this page you can find the example usage for org.jdom2.xpath XPathBuilder compileWith.

Prototype

public XPathExpression<T> compileWith(XPathFactory factory) 

Source Link

Document

Compile an XPathExpression using the details currently stored in the XPathBuilder.

Usage

From source file:ca.nrc.cadc.vosi.avail.CheckWebService.java

License:Open Source License

void checkReturnedXml(String strXml) throws CheckException {
    Document doc;//from w ww .j a v a 2s  . com
    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);
    }
}

From source file:com.c4om.utils.xmlutils.XMLLibsShortcuts.java

License:Apache License

/**
 * Simple method that performs a XPath query over elements
 * @param <T> The type of the XPath result.
 * @param path the path//from w  w w  . jav a  2  s. co m
 * @param context a context node
 * @param filter an appropiate filter. Its type will be the one of the result
 * @param namespaces namespaces made available to XPath engine
 * @return the list of results
 */
public static <T> List<T> performJAXENXPath(String path, Object context, Filter<T> filter,
        Collection<Namespace> namespaces) {
    XPathBuilder<T> builder = new XPathBuilder<>(path, filter);
    if (namespaces != null) {
        builder.setNamespaces(namespaces);
    }
    XPathExpression<T> xpathExpression = builder.compileWith(XPATH_FACTORY);
    List<T> xpathResults = xpathExpression.evaluate(context);
    return xpathResults;
}