Example usage for javax.xml.xpath XPathFactory newXPath

List of usage examples for javax.xml.xpath XPathFactory newXPath

Introduction

In this page you can find the example usage for javax.xml.xpath XPathFactory newXPath.

Prototype

public abstract XPath newXPath();

Source Link

Document

Return a new XPath using the underlying object model determined when the XPathFactory was instantiated.

Usage

From source file:com.odoko.solrcli.actions.CrawlPostAction.java

/**
 * Gets all nodes matching an XPath/*from  w  w  w.  j  av  a2  s.c o  m*/
 */
public static NodeList getNodesFromXP(Node n, String xpath) throws XPathExpressionException {
  XPathFactory factory = XPathFactory.newInstance();
  XPath xp = factory.newXPath();
  XPathExpression expr = xp.compile(xpath);
  return (NodeList) expr.evaluate(n, XPathConstants.NODESET);
}

From source file:cz.mzk.editor.server.fedora.utils.FedoraUtils.java

/**
 * Gets the rdf pids./*  w  w  w . ja  va  2  s .  c o  m*/
 * 
 * @param pid
 *        the pid
 * @param relation
 *        the relation
 * @return the rdf pids
 */
public static ArrayList<String> getRdfPids(String pid, String relation) {
    ArrayList<String> pids = new ArrayList<String>();
    try {

        String command = configuration.getFedoraHost() + "/get/" + pid + "/" + RELS_EXT_STREAM;
        InputStream is = RESTHelper.get(command, configuration.getFedoraLogin(),
                configuration.getFedoraPassword(), true);
        Document contentDom = XMLUtils.parseDocument(is);
        XPathFactory factory = XPathFactory.newInstance();
        XPath xpath = factory.newXPath();
        String xPathStr = "/RDF/Description/" + relation;
        XPathExpression expr = xpath.compile(xPathStr);
        NodeList nodes = (NodeList) expr.evaluate(contentDom, XPathConstants.NODESET);
        for (int i = 0; i < nodes.getLength(); i++) {
            Node childnode = nodes.item(i);
            if (!childnode.getNodeName().contains("hasModel")) {
                pids.add(childnode.getNodeName() + " "
                        + childnode.getAttributes().getNamedItem("rdf:resource").getNodeValue().split("/")[1]);
            }
        }
    } catch (Exception e) {
        LOGGER.error(e.getMessage(), e);
    }
    return pids;
}

From source file:cz.mzk.editor.server.fedora.utils.FedoraUtils.java

/**
 * Find first page pid./*  w  w w.j  a  v a  2s .c o  m*/
 * 
 * @param pid
 *        the pid
 * @return the string
 */
public static String findFirstPagePid(String pid) {

    ArrayList<String> pids = new ArrayList<String>();
    try {
        String command = configuration.getFedoraHost() + "/get/" + pid + "/RELS-EXT";
        InputStream is = RESTHelper.get(command, configuration.getFedoraLogin(),
                configuration.getFedoraPassword(), true);
        Document contentDom = XMLUtils.parseDocument(is);
        XPathFactory factory = XPathFactory.newInstance();
        XPath xpath = factory.newXPath();
        XPathExpression expr = xpath.compile("/RDF/Description/*");
        NodeList nodes = (NodeList) expr.evaluate(contentDom, XPathConstants.NODESET);
        for (int i = 0; i < nodes.getLength(); i++) {
            Node childnode = nodes.item(i);
            String nodeName = childnode.getNodeName();
            if (nodeName.contains(FedoraRelationship.hasPage.getStringRepresentation())
                    || nodeName.contains(FedoraRelationship.isOnPage.getStringRepresentation())) {
                return childnode.getAttributes().getNamedItem("rdf:resource").getNodeValue().split("uuid:")[1];
            } else if (!nodeName.contains("hasModel") && childnode.hasAttributes()
                    && childnode.getAttributes().getNamedItem("rdf:resource") != null) {
                pids.add(childnode.getAttributes().getNamedItem("rdf:resource").getNodeValue().split("/")[1]);
            }
        }
        for (String relpid : pids) {
            return FedoraUtils.findFirstPagePid(relpid);
        }
    } catch (Exception e) {
        LOGGER.error(e.getMessage(), e);
    }
    return null;
}

From source file:com.alliander.osgp.platform.cucumber.RunXpathResult.java

public XpathResult runXPathExpression(final String response, final String path)
        throws ParserConfigurationException, SAXException, IOException, XPathExpressionException {

    final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    final DocumentBuilder builder = factory.newDocumentBuilder();
    final InputSource is = new InputSource();
    is.setCharacterStream(new StringReader(response));
    final Document doc = builder.parse(is);
    final XPathFactory xPathfactory = XPathFactory.newInstance();
    final XPath xpath = xPathfactory.newXPath();

    return new XpathResult(xpath.compile(path), doc);
}

From source file:org.apache.solr.kelvin.responseanalyzers.LegacyResponseAnalyzer.java

public void configure(JsonNode config) throws Exception {
    XPathFactory xPathfactory = XPathFactory.newInstance();
    XPath xpath = xPathfactory.newXPath();
    expr = xpath.compile("/response/federator/result/doc");
}

From source file:com.dianping.phoenix.dev.core.tools.scanner.ServiceMetaScanner.java

@Override
protected List<ServicePortEntry> doScan(Document doc) throws Exception {
    List<ServicePortEntry> resList = new ArrayList<ServicePortEntry>();
    XPathFactory factory = XPathFactory.newInstance();
    XPath xpath = factory.newXPath();
    XPathExpression expr = xpath.compile("//service");

    Object xmlRes = expr.evaluate(doc, XPathConstants.NODESET);

    NodeList nodes = (NodeList) xmlRes;

    for (int i = 0; i < nodes.getLength(); i++) {
        String serviceName = nodes.item(i).getAttributes().getNamedItem("name").getNodeValue();
        XPathExpression portExpr = xpath.compile("//service[@name='" + serviceName + "']/port/text()");
        Object portRes = portExpr.evaluate(doc, XPathConstants.NODESET);
        NodeList ports = (NodeList) portRes;
        int projectPort = -1;
        if (ports.getLength() >= 1) {
            projectPort = Integer.parseInt(ports.item(0).getNodeValue());
        }/* ww w  . ja v a2 s  . c o m*/

        if (projectPort > 0 && StringUtils.isNotBlank(serviceName)) {
            resList.add(new ServicePortEntry(serviceName, projectPort));
        }
    }

    return resList;
}

From source file:org.apache.solr.kelvin.responseanalyzers.XmlDoclistExtractorResponseAnalyzer.java

public void configure(JsonNode config) throws Exception {
    XPathFactory xPathfactory = XPathFactory.newInstance();
    XPath xpath = xPathfactory.newXPath();
    expr = xpath.compile("/response/result/doc");

}

From source file:com.dianping.maven.plugin.tools.misc.scanner.ProjectMetaScanner.java

@Override
protected List<ProjectPortEntry> doScan(Document doc) throws Exception {
    List<ProjectPortEntry> resList = new ArrayList<ProjectPortEntry>();
    XPathFactory factory = XPathFactory.newInstance();
    XPath xpath = factory.newXPath();
    XPathExpression expr = xpath.compile("//project");

    Object xmlRes = expr.evaluate(doc, XPathConstants.NODESET);

    NodeList nodes = (NodeList) xmlRes;

    for (int i = 0; i < nodes.getLength(); i++) {
        String projectName = nodes.item(i).getAttributes().getNamedItem("name").getNodeValue();
        XPathExpression portExpr = xpath.compile("//project[@name='" + projectName + "']/port/text()");
        Object portRes = portExpr.evaluate(doc, XPathConstants.NODESET);
        NodeList ports = (NodeList) portRes;
        int projectPort = -1;
        if (ports.getLength() >= 1) {
            projectPort = Integer.parseInt(ports.item(0).getNodeValue());
        }/*from  w  w  w.  j ava  2  s.co  m*/

        if (projectPort > 0 && StringUtils.isNotBlank(projectName)) {
            resList.add(new ProjectPortEntry(projectName, projectPort));
        }
    }

    return resList;
}

From source file:com.rackspace.api.clients.veracode.responses.AbstractXmlResponse.java

public AbstractXmlResponse(HttpResponse response) throws VeracodeApiException {
    DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = null;

    try {/*from  w  w w . ja v  a 2s.  com*/
        builder = domFactory.newDocumentBuilder();
    } catch (ParserConfigurationException e) {
        throw new RuntimeException("Xml Configuration Issue parsing response", e);
    }

    HttpEntity responseEntity = extractResponse(response);

    try {
        if (response.getStatusLine().getStatusCode() == 200) {
            this.doc = builder.parse(responseEntity.getContent());
            EntityUtils.consume(responseEntity);
        } else {
            String errorMessage = EntityUtils.toString(responseEntity);
            int errorCode = response.getStatusLine().getStatusCode();

            throw new VeracodeApiException("Error Code: " + errorCode + " Error Message: " + errorMessage);
        }
    } catch (SAXException e) {
        throw new VeracodeApiException("Could not Parse Response.", e);
    } catch (IOException e) {
        throw new VeracodeApiException("Could not Parse Response.", e);
    }

    XPathFactory factory = XPathFactory.newInstance();
    this.xpath = factory.newXPath();
}

From source file:org.ambraproject.search.service.DummySOLRMessageSender.java

private Node XPathSingleNodeQuery(Document dom, String statement) throws XPathExpressionException {
    XPathFactory factory = XPathFactory.newInstance();
    XPath xpath = factory.newXPath();
    XPathExpression expr = xpath.compile(statement);

    return (Node) expr.evaluate(dom, XPathConstants.NODE);
}