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:org.yawlfoundation.yawl.util.DOMUtil.java

public static String selectNodeText(Node node, String expression) throws XPathExpressionException {
    XPathFactory factory = XPathFactory.newInstance();
    XPath path = factory.newXPath();
    Object result = path.evaluate(expression, node, XPathConstants.NODE);
    return getNodeText((Node) result);
}

From source file:org.yawlfoundation.yawl.util.DOMUtil.java

public static NodeList selectNodeList(Node node, String expression) throws XPathExpressionException {
    XPathFactory factory = XPathFactory.newInstance();
    XPath path = factory.newXPath();
    Object result = path.evaluate(expression, node, XPathConstants.NODESET);
    return (NodeList) result;
}

From source file:org.zaproxy.zap.extension.saml.SAMLMessage.java

/** Get the saml attributes from the saml message which are also in the configured list */
private void buildAttributeMap() {
    // xpath initialization
    XPathFactory xFactory = XPathFactory.newInstance();
    XPath xpath = xFactory.newXPath();
    Set<Attribute> allAttributes = SAMLConfiguration.getInstance().getAvailableAttributes();
    for (Attribute attribute : allAttributes) {
        try {//from w  w w.  j  a v  a2  s.co m
            XPathExpression expression = xpath.compile(attribute.getxPath());
            Node node = (Node) expression.evaluate(xmlDocument, XPathConstants.NODE);
            if (node != null) { // the attributes that aren't available will be giving null
                // values
                String value;
                if (node instanceof Element) {
                    value = node.getTextContent();
                } else if (node instanceof Attr) {
                    value = ((Attr) node).getValue();
                } else {
                    value = node.getNodeValue();
                }
                if (value != null && !"".equals(value)) {
                    Attribute newAttrib = attribute.createCopy();
                    newAttrib.setValue(value);
                    attributeMap.put(attribute.getName(), newAttrib);
                }
            }
        } catch (XPathExpressionException e) {
            log.warn(attribute.getxPath() + " is not a valid XPath", e);
        }
    }
}

From source file:org.zaproxy.zap.extension.saml.SAMLMessage.java

/** Update XML document with any attributes that were changed */
private void updateXMLDocument() {
    XPathFactory xFactory = XPathFactory.newInstance();
    XPath xpath = xFactory.newXPath();
    for (Attribute attribute : attributeMap.values()) {
        try {//  w  ww.j  a va2 s .  c o m
            Node node = (Node) xpath.compile(attribute.getxPath()).evaluate(xmlDocument, XPathConstants.NODE);
            if (node != null) { // the attributes that aren't available will be giving null
                // values
                if (node instanceof Element) {
                    node.setTextContent(attribute.getValue().toString());
                } else if (node instanceof Attr) {
                    ((Attr) node).setValue(attribute.getValue().toString());
                } else {
                    node.setNodeValue(attribute.getValue().toString());
                }
            }
        } catch (XPathExpressionException e) {
            log.warn(attribute.getxPath() + " is not a valid XPath", e);
        }
    }
    if (SAMLConfiguration.getInstance().getXSWEnabled()) {
        try {
            NodeList nodeList = (NodeList) xpath.compile("/Response//Signature").evaluate(xmlDocument,
                    XPathConstants.NODESET);
            for (int i = 0; i < nodeList.getLength(); i++) {
                Node item = nodeList.item(i);
                if (item instanceof Element) {
                    item.getParentNode().removeChild(item);
                }
            }
        } catch (XPathExpressionException e) {
            log.warn("'/Response//Signature' is not a valid XPath", e);
        }
    }
}

From source file:smartrics.rest.fitnesse.fixture.support.Tools.java

public static XPathExpression toExpression(Map<String, String> ns, String xpathExpression) {
    try {//  w  ww .  j  a  v  a  2s  . co  m
        XPathFactory xpathFactory = XPathFactory.newInstance();
        XPath xpath = xpathFactory.newXPath();
        if (ns.size() > 0) {
            xpath.setNamespaceContext(toNsContext(ns));
        }
        XPathExpression expr = xpath.compile(xpathExpression);
        return expr;
    } catch (XPathExpressionException e) {
        throw new IllegalArgumentException("xPath expression can not be compiled: " + xpathExpression, e);
    }
}

From source file:test.framework.TestBase.java

/**
 * Return the child of the node selected by the xPath.
 * // w  w w .j  a  v a2  s . c o m
 * @param node The node.
 * @param xPath The xPath expression.
 * @return The child of the node selected by the xPath.
 * @throws TransformerException If anything fails.
 */
protected static Node selectSingleNode(final Node node, final String xpathExpression)
        throws TransformerException {
    XPathFactory factory = XPathFactory.newInstance();
    XPath xPath = factory.newXPath();
    try {
        return (Node) xPath.evaluate(xpathExpression, node, XPathConstants.NODE);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:test.framework.TestBase.java

/**
 * Return the list of children of the node selected by the xPath.
 * //from   w w w.ja v  a  2  s . com
 * @param node The node.
 * @param xPath The xPath expression.
 * @return The list of children of the node selected by the xPath.
 * @throws TransformerException If anything fails.
 */
protected static NodeList selectNodeList(final Node node, final String xpathExpression)
        throws TransformerException {
    XPathFactory factory = XPathFactory.newInstance();
    XPath xPath = factory.newXPath();
    try {
        return (NodeList) xPath.evaluate(xpathExpression, node, XPathConstants.NODESET);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:ubic.gemma.core.loader.entrez.pubmed.PubMedXMLParser.java

private Node processRecord(BibliographicReference bibRef, Node record) {
    Node article = null;//from w ww. j  a v a2  s. c o  m

    NodeList recordNodes = record.getChildNodes();
    for (int p = 0; p < recordNodes.getLength(); p++) {
        Node item = recordNodes.item(p);
        if (!(item instanceof Element)) {
            continue;
        }
        String name = item.getNodeName();
        switch (name) {
        case "Article":
            article = item;
            break;
        case "ChemicalList":
            bibRef.setChemicals(this.extractChemicals(item));
            break;
        case "MeshHeadingList":
            this.processMESH(item, bibRef);
            break;
        case "KeywordList":
            bibRef.setKeywords(this.extractKeywords(item));
            break;
        case "MedlineJournalInfo": {
            NodeList jNodes = item.getChildNodes();
            for (int q = 0; q < jNodes.getLength(); q++) {
                Node jitem = jNodes.item(q);
                if (!(jitem instanceof Element)) {
                    continue;
                }
                if (jitem.getNodeName().equals("MedlineTA")) {
                    bibRef.setPublication(XMLUtils.getTextValue((Element) jitem));
                }
            }
            break;
        }
        case "PMID":
            this.processAccession(bibRef, item);
            break;
        case "CommentsCorrectionsList":
            NodeList jNodes = item.getChildNodes();
            for (int q = 0; q < jNodes.getLength(); q++) {
                Node jitem = jNodes.item(q);
                if (!(jitem instanceof Element)) {
                    continue;
                }
                Node reftype = jitem.getAttributes().getNamedItem("RefType");

                if (reftype == null)
                    continue;

                String reftypeName = ((Attr) reftype).getValue();
                PubMedXMLParser.log.debug(reftypeName);
                if (reftypeName.equals("RetractionIn")) {

                    try {
                        XPathFactory xf = XPathFactory.newInstance();
                        XPath xpath = xf.newXPath();
                        XPathExpression xgds = xpath.compile("RefSource/text()");
                        String ref = (String) xgds.evaluate(jitem, XPathConstants.STRING);

                        xgds = xpath.compile("PMID/text()");
                        String pmid = (String) xgds.evaluate(jitem, XPathConstants.STRING);

                        String description = "Retracted [In: " + ref + " PMID=" + pmid + "]";
                        bibRef.setDescription(description);
                    } catch (XPathExpressionException e) {
                        PubMedXMLParser.log.warn(
                                "Error while trying to get details of the retraction: " + e.getMessage(), e);
                    }
                    /*
                     * Such papers also have <PublicationType>Retracted Publication</PublicationType>
                     */
                }

            }

            break;
        default:
            log.warn("Unrecognized node name " + name);
        }
    }
    return article;
}

From source file:ubic.gemma.core.loader.expression.geo.DatasetCombiner.java

/**
 * @param seriesAccession series accession
 * @return GDSs that correspond to the given series. It will be empty if there is no GDS matching.
 *///from  www .  j  av  a2 s.  c  o m
public static Collection<String> findGDSforGSE(String seriesAccession) {

    Collection<String> associatedDatasetAccessions = new HashSet<>();
    try {
        String details = EutilFetch.fetch("gds", seriesAccession, 100);
        if (details.equalsIgnoreCase("no results")) {
            return associatedDatasetAccessions;
        }
        XPathFactory xf = XPathFactory.newInstance();
        XPath xpath = xf.newXPath();

        /*
         * Get all Items of type GDS that are from a DocSum with an Item entryType of GDS.
         */
        XPathExpression xgds = xpath.compile(
                "/eSummaryResult/DocSum[Item/@Name=\"entryType\" and (Item=\"GDS\")]/Item[@Name=\"GDS\"][1]/text()");

        DocumentBuilder builder = DatasetCombiner.factory.newDocumentBuilder();

        /*
         * Bug 2690. There must be a better way.
         */
        details = details.replaceAll("encoding=\"UTF-8\"", "");
        try (StringInputStream sis = new StringInputStream(StringUtils.trim(details))) {

            Document document = builder.parse(sis);

            NodeList result = (NodeList) xgds.evaluate(document, XPathConstants.NODESET);
            for (int i = 0; i < result.getLength(); i++) {
                String nodeValue = result.item(i).getNodeValue();
                // if ( nodeValue.contains( ";" ) ) continue; //
                associatedDatasetAccessions.add("GDS" + nodeValue);
            }

            return associatedDatasetAccessions;

        }

    } catch (IOException e) {
        throw new RuntimeException("Could not parse XML data from remote server", e);
    } catch (ParserConfigurationException | SAXException | XPathExpressionException e) {
        throw new RuntimeException("XML parsing error of remote data", e);
    }
}