Example usage for javax.xml.xpath XPath evaluate

List of usage examples for javax.xml.xpath XPath evaluate

Introduction

In this page you can find the example usage for javax.xml.xpath XPath evaluate.

Prototype

public Object evaluate(String expression, InputSource source, QName returnType) throws XPathExpressionException;

Source Link

Document

Evaluate an XPath expression in the context of the specified InputSource and return the result as the specified type.

Usage

From source file:pl.psnc.synat.wrdz.mdz.format.FileFormatVerifierBean.java

/**
 * Returns the nodes from the given xml document that match the given xpath expression.
 * //from  w  w w.ja va  2  s. c  o  m
 * @param document
 *            xml document to search
 * @param path
 *            xpath expression
 * @return matching nodes
 */
private NodeList xpath(Document document, String path) {
    XPathFactory factory = XPathFactory.newInstance();
    XPath xpath = factory.newXPath();
    xpath.setNamespaceContext(new NamespaceContextImpl(NS_PREFIX, NS_URI));

    try {
        return (NodeList) xpath.evaluate(path, document, XPathConstants.NODESET);
    } catch (XPathExpressionException e) {
        throw new RuntimeException("Incorrect XPath expression", e);
    }
}

From source file:com.tibco.businessworks6.sonar.plugin.source.XmlSource.java

/**
 * Return hard coded violations of all elements related to the xPathQuery evaluated on the input context
 * //from w w w . j  ava  2 s.com
 * @param rule         violated {@link Rule}
 * @param context      input context {@link Node}
 * @param xPathQuery    XPath query {@link String}
 * @param message      violations message {@link String}
 * 
 * @return List<Violation>
 */
public List<Violation> getViolationsHardCodedXPath(Rule rule, Node context, String xPathQuery, String message) {
    // Init violation 
    List<Violation> violations = new ArrayList<Violation>();
    try {
        XPath xpath = XPathFactory.newInstance().newXPath();
        NodeList nodesFound = (NodeList) xpath.evaluate(xPathQuery, context, XPathConstants.NODESET);
        int length = nodesFound.getLength();
        for (int i = 0; i < length; i++) {
            Node nodeFound = nodesFound.item(i);
            violations.addAll(
                    getViolationsHardCodedNode(rule, nodeFound, message + " (" + i + "/" + length + ")"));
        }
    } catch (Exception e) {
        LOGGER.error("context", e);
        violations.add(new DefaultViolation(rule, getLineForNode(context), message + " (not found)"));
    }
    return violations;
}

From source file:de.iteratec.iteraplan.businesslogic.exchange.common.informationflow.VisioInformationFlowTemplateParser.java

public void parseTemplate() {
    Document dom = parseXmlFile();

    XPath xPath = XPathFactory.newInstance().newXPath();
    String xPathFirstLevelApplicationNodesExpression = "/VisioDocument/Pages/Page[1]/"
            + APPLICATION_SHAPE_XPATH;//w  ww  . ja  v a  2  s.  com
    parsedNodes = Maps.newHashMap();

    try {
        NodeList firstLevelApplicationNodes = (NodeList) xPath
                .evaluate(xPathFirstLevelApplicationNodesExpression, dom, XPathConstants.NODESET);
        for (int i = 0; i < firstLevelApplicationNodes.getLength(); i++) {
            Node node = firstLevelApplicationNodes.item(i);
            parseNodeAndAddToMap(node, null);
        }
    } catch (XPathExpressionException e) {
        LOGGER.error("XPath-error during parsing of template '" + templateFile.getName() + "'.", e);
        throw new IteraplanTechnicalException(IteraplanErrorMessages.INTERNAL_ERROR);
    }

}

From source file:com.microsoft.alm.plugin.external.commands.Command.java

protected NodeList evaluateXPath(final String stdout, final String xpathQuery) {
    if (StringUtils.isEmpty(stdout)) {
        return null;
    }//  w  w w  .j  av a  2 s.  com

    // Skip over any lines (like WARNing lines) that come before the xml tag
    // Example:
    // WARN -- Unable to construct Telemetry Client
    // <?xml ...
    final InputSource xmlInput;
    final int xmlStart = stdout.indexOf(XML_PREFIX);
    if (xmlStart > 0) {
        xmlInput = new InputSource(new StringReader(stdout.substring(xmlStart)));
    } else {
        xmlInput = new InputSource(new StringReader(stdout));
    }

    final XPath xpath = XPathFactory.newInstance().newXPath();
    try {
        final Object result = xpath.evaluate(xpathQuery, xmlInput, XPathConstants.NODESET);
        if (result != null && result instanceof NodeList) {
            return (NodeList) result;
        }
    } catch (final XPathExpressionException inner) {
        throw new ToolParseFailureException(inner);
    }

    throw new ToolParseFailureException();
}

From source file:de.ingrid.iplug.opensearch.converter.IngridRSSConverter.java

/**
 * Return the hits coming from the response of an OS-Interface.
 * //from ww w  .java2 s . c o  m
 * @param doc
 *            is the converted response into a document structure
 * @param plugId
 * @param totalResults
 * @param groupedBy
 * @return
 * @throws XPathExpressionException
 */
private IngridHit[] getHits(Document doc, String plugId, int totalResults, String groupedBy)
        throws XPathExpressionException {
    XPath xpath = XPathFactory.newInstance().newXPath();
    NodeList nodes = (NodeList) xpath.evaluate("/rss/channel/item", doc, XPathConstants.NODESET);
    IngridHit[] hits = new IngridHit[nodes.getLength()];

    for (int i = 0; i < nodes.getLength(); i++) {
        IngridHit hit = new IngridHit(plugId, "0", 0, (float) 1.0);
        Node node = nodes.item(i);
        hit.put("title", getTitle(node));
        hit.put("url", getLink(node));
        hit.put("abstract", getAbstract(node));
        hit.put("no_of_hits", String.valueOf(totalResults));
        hit.setScore(getScore(node));

        // ingrid specific data
        setIngridHitDetail(hit, node, groupedBy);

        hits[i] = hit;
    }

    // now we have all original hits, let's manipulate the score
    normalizeRanking(hits);

    return hits;
}

From source file:net.javacrumbs.springws.test.util.DefaultXmlUtil.java

@SuppressWarnings("unchecked")
public boolean isSoap(Document document) {
    XPathFactory factory = XPathFactory.newInstance();
    XPath xpath = factory.newXPath();
    xpath.setNamespaceContext(SOAP_NAMESPACE_CONTEXT);
    try {/*from w w  w . j a  v  a2 s.com*/
        for (Iterator iterator = SOAP_NAMESPACE_CONTEXT.getBoundPrefixes(); iterator.hasNext();) {
            String prefix = (String) iterator.next();
            if ((Boolean) xpath.evaluate("/" + prefix + ":Envelope", document, XPathConstants.BOOLEAN)) {
                return true;
            }
        }
    } catch (XPathExpressionException e) {
        logger.warn(e);
    }
    return false;
}

From source file:eu.planets_project.tb.impl.services.EvaluationTestbedServiceTemplateImpl.java

/**
 * @param node a Node obtained by getAllEvalResultsRootNodes()
 * @return/* ww w.j av a2 s.c  om*/
 * @throws XPathExpressionException
 */
public String getEvalResultName(Node node) throws XPathExpressionException {
    XPath xpath = XPathFactory.newInstance().newXPath();
    String result = (String) xpath.evaluate(sXPathForBMGoalName, node, XPathConstants.STRING);
    return result;
}

From source file:eu.planets_project.tb.impl.services.EvaluationTestbedServiceTemplateImpl.java

/**
 * @param node a Node obtained by getAllEvalResultsRootNodes()
 * @return//ww w  . ja v a 2 s  .  c  om
 * @throws XPathExpressionException
 */
public String getEvalResultSrcValue(Node node) throws XPathExpressionException {
    XPath xpath = XPathFactory.newInstance().newXPath();
    String result = (String) xpath.evaluate(sSrcXpath, node, XPathConstants.STRING);
    return result;
}

From source file:eu.planets_project.tb.impl.services.EvaluationTestbedServiceTemplateImpl.java

/**
 * @param node a Node obtained by getAllEvalResultsRootNodes()
 * @return/*ww  w. jav a  2 s . c o  m*/
 * @throws XPathExpressionException
 */
public String getEvalResultTarValue(Node node) throws XPathExpressionException {
    XPath xpath = XPathFactory.newInstance().newXPath();
    String result = (String) xpath.evaluate(sTarXpath, node, XPathConstants.STRING);
    return result;
}

From source file:eu.planets_project.tb.impl.services.EvaluationTestbedServiceTemplateImpl.java

/**
 * @param node a Node obtained by getAllEvalResultsRootNodes()
 * @return//  www .j a va 2 s.  c  om
 * @throws XPathExpressionException
 */
public String getEvalResultCompStatus(Node node) throws XPathExpressionException {
    XPath xpath = XPathFactory.newInstance().newXPath();
    String result = (String) xpath.evaluate(sCompStatusXpath, node, XPathConstants.STRING);
    return result;
}