Example usage for javax.xml.xpath XPathConstants NODE

List of usage examples for javax.xml.xpath XPathConstants NODE

Introduction

In this page you can find the example usage for javax.xml.xpath XPathConstants NODE.

Prototype

QName NODE

To view the source code for javax.xml.xpath XPathConstants NODE.

Click Source Link

Document

The XPath 1.0 NodeSet data type.

Usage

From source file:org.wso2.carbon.humantask.core.api.rendering.HTRenderingApiImpl.java

/**
 * Function to update xml DOM element according to the xpath expression with given string value
 *
 * @param xmlElement DOM element representing the xml
 * @param xPathExpression xpath expression
 * @param value string that need to replace in the xml
 * @param referanceDoc reference Document which represents original ht rendering element, which used to resolve
 *                     namespaces mentioned in the xpath expression
 * @return//from   w ww .  j  a  v  a2 s .  c om
 * @throws XPathExpressionException
 */
private Element updateXmlByXpath(Element xmlElement, String xPathExpression, String value,
        Document referanceDoc) throws XPathExpressionException {
    if (xPathExpression.length() > 0) {
        //Evaluate XPath
        XPath xPath = XPathFactory.newInstance().newXPath();
        NamespaceResolver nsResolver = new NamespaceResolver(referanceDoc);
        xPath.setNamespaceContext(nsResolver);

        Node result = (Node) xPath.evaluate(xPathExpression, xmlElement, XPathConstants.NODE);
        result.setTextContent(value);
    }

    return xmlElement;
}

From source file:org.wso2.carbon.registry.indexing.utils.RxtUnboundedDataLoadUtils.java

/**
 * This method is used to get unbounded field values in a rxt.
 *
 * @param rxtContent rxt configuration//from   ww  w  .  jav a2 s .  c  om
 * @return list of unbounded filed values.
 * @throws RegistryException
 */
public static RxtUnboundedEntryBean getRxtUnboundedEntries(String rxtContent) throws RegistryException {
    RxtUnboundedEntryBean rxtUnboundedEntryBean = new RxtUnboundedEntryBean();
    String mediaType;
    List<String> fields = new ArrayList<>();

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder;
    Document doc;
    try (ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(rxtContent.getBytes())) {
        builder = factory.newDocumentBuilder();
        if (builder != null) {
            doc = builder.parse(byteArrayInputStream);
            XPathFactory xPathfactory = XPathFactory.newInstance();
            XPath xpath = xPathfactory.newXPath();
            //Get media Type
            XPathExpression expToGetMediaType = xpath.compile(SolrConstants.RXT_ROOT_XPATH);
            Node rxtRootNode = (Node) expToGetMediaType.evaluate(doc, XPathConstants.NODE);
            mediaType = rxtRootNode.getAttributes().getNamedItem(SolrConstants.WORD_TYPE).getTextContent();
            rxtUnboundedEntryBean.setMediaType(mediaType);

            if (StringUtils.isNotEmpty(mediaType)) {
                XPathExpression expr1 = xpath.compile(SolrConstants.UNBOUNDED_TABLE_XPATH);
                NodeList n1 = (NodeList) expr1.evaluate(doc, XPathConstants.NODESET);
                XPathExpression expr2 = xpath.compile(SolrConstants.UNBOUNDED_FIELD_XPATH);
                NodeList n2 = (NodeList) expr2.evaluate(doc, XPathConstants.NODESET);

                // Stop the iteration if no unbounded table entries were found in rxt configuration.
                if (n1.getLength() != 0 || n2.getLength() != 0) {
                    // Add unbounded table values
                    for (int i = 0; i < n1.getLength(); i++) {
                        Node n = n1.item(i);
                        String tableName = n.getAttributes().getNamedItem(SolrConstants.WORD_NAME)
                                .getTextContent();
                        String expr2Text = SolrConstants.UNBOUNDED_TABLE_XPATH_PREFIX + tableName
                                + SolrConstants.UNBOUNDED_TABLE_XPATH_SUFFIX;
                        XPathExpression expr3 = xpath.compile(expr2Text);
                        NodeList n3 = (NodeList) expr3.evaluate(doc, XPathConstants.NODESET);

                        if (n3.getLength() > 0) {
                            for (int j = 0; j < n3.getLength(); j++) {
                                Node node = n3.item(j);
                                fields.add(toCamelCase(tableName) + SolrConstants.UNDERSCORE
                                        + node.getTextContent());
                            }
                        }
                    }
                    // Add unbounded option test field values.
                    for (int k = 0; k < n2.getLength(); k++) {
                        Node node = n2.item(k);
                        String tableName = node.getParentNode().getAttributes()
                                .getNamedItem(SolrConstants.WORD_NAME).getTextContent();
                        if (tableName != null) {
                            fields.add(toCamelCase(tableName) + SolrConstants.UNDERSCORE_ENTRY);
                        }
                    }
                }
            }
        }
        rxtUnboundedEntryBean.setFields(fields);
    } catch (ParserConfigurationException | SAXException | XPathExpressionException | IOException e) {
        throw new RegistryException("Failed to read rxt configuration and filter the unbounded fields", e);
    }
    return rxtUnboundedEntryBean;
}

From source file:org.wso2.pwprovider.DefaultPasswordProvider.java

private String getDataFromConfigFile(File fileName, String xpath) {

    String nodeValue = null;//  w  w w  .  j  av  a2  s.c  o m
    try {
        DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
        Document doc = docBuilder.parse(fileName);

        XPathFactory xpf = XPathFactory.newInstance();
        XPath xp = xpf.newXPath();
        XPathExpression xpathEx = xp.compile(xpath);

        Node text = (Node) xpathEx.evaluate(doc.getDocumentElement(), XPathConstants.NODE);
        nodeValue = text.getTextContent();
    } catch (Exception e) {
        handleException("Error reading key store data from key-password.xml file ", e);
    }
    return nodeValue;
}

From source file:org.wso2.pwprovider.DefaultPasswordProvider.java

private String getPrimaryKeyData(String xpath) {

    String carbonConfigFile = carbonHome + File.separator + "repository" + File.separator + "conf"
            + File.separator + "carbon.xml";
    String nodeValue = null;/*from   w  w w . j  av a2s  . c om*/
    try {
        DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
        Document doc = docBuilder.parse(carbonConfigFile);

        XPathFactory xpf = XPathFactory.newInstance();
        XPath xp = xpf.newXPath();
        XPathExpression xpathEx = xp.compile(xpath);

        Node text = (Node) xpathEx.evaluate(doc.getDocumentElement(), XPathConstants.NODE);
        nodeValue = text.getTextContent();
    } catch (Exception e) {
        handleException("Error reading primary key store data from carbon.xml file ", e);
    }
    return nodeValue;
}

From source file:org.xdi.service.XmlService.java

public String getNodeValue(Document xmlDocument, String xPathExpression, String attributeName)
        throws XPathExpressionException {
    XPath xPath = XPathFactory.newInstance().newXPath();
    XPathExpression formXPathExpression = xPath.compile(xPathExpression);

    if (StringHelper.isEmpty(attributeName)) {
        String nodeValue = (String) formXPathExpression.evaluate(xmlDocument, XPathConstants.STRING);

        return nodeValue;
    }/*  ww w . j  a  va 2s. c  o  m*/

    Node node = ((Node) formXPathExpression.evaluate(xmlDocument, XPathConstants.NODE));
    if (node == null) {
        return null;
    }

    Node attributeNode = node.getAttributes().getNamedItem(attributeName);
    if (attributeNode == null) {
        return null;
    }

    return attributeNode.getNodeValue();
}

From source file:org.xframium.device.factory.DeviceWebDriver.java

public WebElement findElement(By by) {
    if (by instanceof ByXPath) {
        if (cachingEnabled && cachedDocument == null)
            cacheData();/* ww  w.  j ava2 s. c o  m*/

        if (cachingEnabled && cachedDocument != null) {
            try {
                XPath xPath = xPathFactory.newXPath();
                String path = by.toString();
                path = path.substring(path.indexOf(": ") + 2);
                Node node = (Node) xPath.evaluate(path, cachedDocument, XPathConstants.NODE);

                if (node != null)
                    return new CachedWebElement(this, webDriver, by, node);
                else
                    cachedDocument = null;
            } catch (Exception e) {
                log.warn("Error reading from cache ", e);
                cachingEnabled = false;
                cachedDocument = null;
            }
        }
    }
    return new MorelandWebElement(this, webDriver.findElement(by));
}

From source file:org.xframium.utility.WebServiceClientUtil.java

private static void processResult(Responce result, ResponceDetails responceDetails,
        Map<String, String> contextMap) throws Exception {
    if (CONTENT_XML.equalsIgnoreCase(responceDetails.type)) {
        ////from  w  w  w.j  a va2s .  com
        // In this case, result.payload is an XML document and the paths in
        // responceDetails.parameters are XPATH
        // expressions
        //

        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        Document document = dBuilder.parse(new ByteArrayInputStream(result.payload.getBytes()));
        XPath xPath = xPathFactory.newXPath();

        Iterator<ResponceVariable> params = responceDetails.parameters.iterator();
        while (params.hasNext()) {
            ResponceVariable param = params.next();

            Node node = (Node) xPath.evaluate(param.path, document, XPathConstants.NODE);

            if (node == null) {
                throw new ScriptConfigurationException("No result found. Check xpath expression " + param.path);
            }

            String value = node.getTextContent();

            contextMap.put(param.name, value);
        }
    } else if (CONTENT_JSON.equalsIgnoreCase(responceDetails.type)) {
        //
        // In this case, result.payload is an JSON document and the paths in
        // responceDetails.parameters are JASONPATH
        // (https://github.com/jayway/JsonPath) expressions
        //

        DocumentContext ctx = JsonPath.parse(result.payload);

        Iterator<ResponceVariable> params = responceDetails.parameters.iterator();
        while (params.hasNext()) {
            ResponceVariable param = params.next();

            /* String value = ctx.read( param.path );
                    
             contextMap.put( param.name, value );*/

            JSONArray value = (JSONArray) ctx.read(param.path);

            contextMap.put(param.name, value.toJSONString());
        }
    }
}

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

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

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.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 ww. j  a  v  a  2 s.  c o 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);
        }
    }
}