Example usage for org.jdom2.xpath XPathFactory instance

List of usage examples for org.jdom2.xpath XPathFactory instance

Introduction

In this page you can find the example usage for org.jdom2.xpath XPathFactory instance.

Prototype

public static final XPathFactory instance() 

Source Link

Document

Obtain an instance of an XPathFactory using the default mechanisms to determine what XPathFactory implementation to use.

Usage

From source file:nl.colorize.util.xml.XPath.java

License:Apache License

/**
 * Creates an {@code XPath} instance from an XPath expression in text form.
 * @throws IllegalArgumentException if the expression is not valid XPath.
 *//*w  w  w.  j  a  v a  2s.  c  o  m*/
public static XPath parse(String expression) {
    XPathFactory xpathFactory = XPathFactory.instance();
    XPathExpression<Element> compiled = xpathFactory.compile(expression, Filters.element());
    return new XPath(compiled);
}

From source file:org.apache.marmotta.ldclient.provider.mediawiki.MediawikiProvider.java

License:Apache License

protected static Element queryElement(Document n, String query) {
    return XPathFactory.instance().compile(query, new ElementFilter()).evaluateFirst(n);
}

From source file:org.apache.marmotta.ldclient.provider.mediawiki.MediawikiProvider.java

License:Apache License

protected static List<Element> queryElements(Document n, String query) {
    return XPathFactory.instance().compile(query, new ElementFilter()).evaluate(n);
}

From source file:org.apache.marmotta.ldclient.provider.xml.mapping.XPathValueMapper.java

License:Apache License

protected XPathValueMapper(String xpath, Map<String, String> namespaces) {
    this.xpath = xpath;

    Set<Namespace> xnamespaces = new HashSet<Namespace>();
    if (namespaces != null) {
        for (Map.Entry<String, String> ns : namespaces.entrySet()) {
            xnamespaces.add(Namespace.getNamespace(ns.getKey(), ns.getValue()));
        }//w  ww . j a v  a  2s . c om
    }
    this.compiled = XPathFactory.instance().compile(xpath, Filters.fpassthrough(), null, xnamespaces);
}

From source file:org.apache.marmotta.ldclient.provider.xml.mapping.XPathValueMapper.java

License:Apache License

protected XPathValueMapper(String xpath, Collection<Namespace> namespaces) {
    this.xpath = xpath;
    this.compiled = XPathFactory.instance().compile(xpath, Filters.fpassthrough(), null, namespaces);
}

From source file:org.apache.marmotta.ldpath.model.functions.xml.XPathFunction.java

License:Apache License

private LinkedList<String> doFilter(String in, Set<String> xpaths) throws IOException {
    LinkedList<String> result = new LinkedList<String>();
    try {/*  ww  w .j a  va  2 s  .  c o m*/
        Document doc = new SAXBuilder(XMLReaders.NONVALIDATING).build(new StringReader(in));
        XMLOutputter out = new XMLOutputter();

        for (String xp : xpaths) {
            XPathExpression<Content> xpath = XPathFactory.instance().compile(xp, Filters.content());
            for (Content node : xpath.evaluate(doc)) {
                if (node instanceof Element) {
                    result.add(out.outputString((Element) node));
                } else if (node instanceof Text) {
                    result.add(out.outputString((Text) node));
                }
            }
        }
        return result;
    } catch (JDOMException xpe) {
        throw new IllegalArgumentException("error while processing xpath expressions: '" + xpaths + "'", xpe);
    }
}

From source file:org.apache.marmotta.ucuenca.wk.provider.orcid.ORCIDRawProvider.java

License:Apache License

protected static List<Element> queryElements(Document n, String query) {
    return XPathFactory.instance()
            .compile(query, new ElementFilter(), null,
                    Namespace.getNamespace("external-identifier",
                            "http://www.orcid.org/ns/external-identifier"),
                    Namespace.getNamespace("email", "http://www.orcid.org/ns/email"),
                    Namespace.getNamespace("other-name", "http://www.orcid.org/ns/other-name"),
                    Namespace.getNamespace("personal-details", "http://www.orcid.org/ns/personal-details"),
                    Namespace.getNamespace("person", "http://www.orcid.org/ns/person"),
                    Namespace.getNamespace("record", "http://www.orcid.org/ns/record"),
                    Namespace.getNamespace("search", "http://www.orcid.org/ns/search"),
                    Namespace.getNamespace("keyword", "http://www.orcid.org/ns/keyword"),
                    Namespace.getNamespace("employment", "http://www.orcid.org/ns/employment"),
                    Namespace.getNamespace("researcher-url", "http://www.orcid.org/ns/researcher-url"),
                    Namespace.getNamespace("activities", "http://www.orcid.org/ns/activities"),
                    Namespace.getNamespace("education", "http://www.orcid.org/ns/education"),
                    Namespace.getNamespace("work", "http://www.orcid.org/ns/work"),
                    Namespace.getNamespace("common", "http://www.orcid.org/ns/common"))
            .evaluate(n);//from   w w w. j  a  v a2  s .c  o  m
}

From source file:org.apache.marmotta.ucuenca.wk.provider.scopus.ScopusAuthorSearchProvider.java

License:Apache License

protected static List<Element> queryElements(Document n, String query) {
    return XPathFactory.instance().compile(query, new ElementFilter(), null, n.getNamespacesInherited())
            .evaluate(n);/*from  w ww  .java  2  s .c o  m*/
}

From source file:org.apache.wiki.util.XmlUtil.java

License:Apache License

/**
 * Parses the given XML file and returns the requested nodes. If there's an error accessing or parsing the file, an
 * empty list is returned.//from   w w  w  . j  a  va2  s  .  co  m
 * 
 * @param xml file to parse; matches all resources from classpath, filters repeated items.
 * @param requestedNodes requestd nodes on the xml file
 * @return the requested nodes of the XML file.
 */
public static List<Element> parse(String xml, String requestedNodes) {
    if (StringUtils.isNotEmpty(xml) && StringUtils.isNotEmpty(requestedNodes)) {
        Set<Element> readed = new HashSet<Element>();
        SAXBuilder builder = new SAXBuilder();
        try {
            Enumeration<URL> resources = XmlUtil.class.getClassLoader().getResources(xml);
            while (resources.hasMoreElements()) {
                URL resource = resources.nextElement();
                log.debug("reading " + resource.toString());
                Document doc = builder.build(resource);
                XPathFactory xpfac = XPathFactory.instance();
                XPathExpression<Element> xp = xpfac.compile(requestedNodes, Filters.element());
                readed.addAll(xp.evaluate(doc)); // filter out repeated items
            }
            return new ArrayList<Element>(readed);
        } catch (IOException ioe) {
            log.error("Couldn't load all " + xml + " resources", ioe);
        } catch (JDOMException jdome) {
            log.error("error parsing " + xml + " resources", jdome);
        }
    }
    return Collections.<Element>emptyList();
}

From source file:org.apache.wiki.util.XmlUtil.java

License:Apache License

/**
 * Parses the given stream and returns the requested nodes. If there's an error accessing or parsing the stream, an
 * empty list is returned./*from ww w .  ja  v a  2 s .com*/
 * 
 * @param xmlStream stream to parse.
 * @param requestedNodes requestd nodes on the xml stream.
 * @return the requested nodes of the XML stream.
 */
public static List<Element> parse(InputStream xmlStream, String requestedNodes) {
    if (xmlStream != null && StringUtils.isNotEmpty(requestedNodes)) {
        SAXBuilder builder = new SAXBuilder();
        try {
            Document doc = builder.build(xmlStream);
            XPathFactory xpfac = XPathFactory.instance();
            XPathExpression<Element> xp = xpfac.compile(requestedNodes, Filters.element());
            return xp.evaluate(doc);
        } catch (IOException ioe) {
            log.error("Couldn't load all " + xmlStream + " resources", ioe);
        } catch (JDOMException jdome) {
            log.error("error parsing " + xmlStream + " resources", jdome);
        }
    }
    return Collections.<Element>emptyList();
}