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.apache.cxf.systest.rest.RestClientServerBookTest.java

@Test
public void testGetBookWrappedUsingURL() throws Exception {
    String endpointAddress = "http://localhost:9080/xmlwrapped/books/123";
    URL url = new URL(endpointAddress);
    InputStream in = url.openStream();
    assertNotNull(in);/*www.  ja  v a 2 s.c o m*/

    Map<String, String> ns = new HashMap<String, String>();
    ns.put("a1", "http://book.acme.com");
    ns.put("a2", "http://book.customer.cxf.apache.org/");
    Document doc = XMLUtils.parse(in);
    XPathUtils xp = new XPathUtils(ns);
    assertTrue(xp.isExist("/a2:getBookResponse", doc.getDocumentElement(), XPathConstants.NODE));
    assertTrue(xp.isExist("/a2:getBookResponse/a2:Book", doc.getDocumentElement(), XPathConstants.NODE));
    assertTrue(xp.isExist("/a2:getBookResponse/a2:Book/a1:id", doc.getDocumentElement(), XPathConstants.NODE));
    assertEquals("123",
            xp.getValue("/a2:getBookResponse/a2:Book/a1:id", doc.getDocumentElement(), XPathConstants.STRING));
    assertEquals("CXF in Action", xp.getValue("/a2:getBookResponse/a2:Book/a1:name", doc.getDocumentElement(),
            XPathConstants.STRING));

}

From source file:org.apache.geode.management.internal.configuration.utils.XmlUtils.java

public static Element querySingleElement(Node node, String searchString, final XPathContext xPathContext)
        throws XPathExpressionException {
    XPath xpath = XPathFactory.newInstance().newXPath();
    xpath.setNamespaceContext(xPathContext);
    Object result = xpath.evaluate(searchString, node, XPathConstants.NODE);
    try {/*from  w w  w . j a  va  2s. c  o  m*/
        return (Element) result;
    } catch (ClassCastException e) {
        throw new XPathExpressionException("Not an org.w3c.dom.Element: " + result);
    }
}

From source file:org.apache.hadoop.gateway.deploy.DeploymentFactoryFuncTest.java

private Node node(Node scope, String expression) throws XPathExpressionException {
    return (Node) XPathFactory.newInstance().newXPath().compile(expression).evaluate(scope,
            XPathConstants.NODE);
}

From source file:org.apache.hadoop.gateway.filter.rewrite.impl.xml.XmlFilterReader.java

private void processBufferedElement(Level level, UrlRewriteFilterGroupDescriptor config)
        throws XPathExpressionException {
    for (UrlRewriteFilterPathDescriptor selector : config.getSelectors()) {
        if (selector instanceof UrlRewriteFilterApplyDescriptor) {
            XPathExpression path = (XPathExpression) selector.compiledPath(XPATH_COMPILER);
            Object node = path.evaluate(level.scopeNode, XPathConstants.NODE);
            if (node != null) {
                UrlRewriteFilterApplyDescriptor apply = (UrlRewriteFilterApplyDescriptor) selector;
                if (node instanceof Element) {
                    Element element = (Element) node;
                    String value = element.getTextContent();
                    value = filterText(extractQName(element), value, apply.rule());
                    element.setTextContent(value);
                } else if (node instanceof Text) {
                    Text text = (Text) node;
                    String value = text.getWholeText();
                    value = filterText(extractQName(text.getParentNode()), value, apply.rule());
                    text.replaceWholeText(value);
                } else if (node instanceof Attr) {
                    Attr attr = (Attr) node;
                    String value = attr.getValue();
                    value = filterAttribute(extractQName(attr.getOwnerElement()), extractQName(attr), value,
                            apply.rule());
                    attr.setValue(value);
                } else {
                    throw new IllegalArgumentException(RES.unexpectedSelectedNodeType(node));
                }// w w w .  ja v  a 2 s.  c  om
            }
        } else if (selector instanceof UrlRewriteFilterDetectDescriptor) {
            XPathExpression path = (XPathExpression) selector.compiledPath(XPATH_COMPILER);
            Object node = path.evaluate(level.scopeNode, XPathConstants.NODE);
            if (node != null) {
                UrlRewriteFilterDetectDescriptor detect = (UrlRewriteFilterDetectDescriptor) selector;
                String value = null;
                if (node instanceof Element) {
                    Element element = (Element) node;
                    value = element.getTextContent();
                } else if (node instanceof Text) {
                    Text text = (Text) node;
                    value = text.getWholeText();
                } else if (node instanceof Attr) {
                    Attr attr = (Attr) node;
                    value = attr.getValue();
                } else {
                    throw new IllegalArgumentException(RES.unexpectedSelectedNodeType(node));
                }
                if (detect.compiledValue(REGEX_COMPILER).matcher(value).matches()) {
                    processBufferedElement(level, detect);
                }
            }
        } else {
            throw new IllegalArgumentException(RES.unexpectedRewritePathSelector(selector));
        }
    }
}

From source file:org.apache.hadoop.gateway.filter.rewrite.impl.xml.XmlFilterReader.java

protected UrlRewriteFilterPathDescriptor pickFirstMatchingPath(Level level) {
    UrlRewriteFilterPathDescriptor match = null;
    if (level.scopeConfig != null) {
        for (UrlRewriteFilterPathDescriptor selector : level.scopeConfig.getSelectors()) {
            try {
                XPathExpression path = (XPathExpression) selector.compiledPath(XPATH_COMPILER);
                Object node = path.evaluate(level.scopeNode, XPathConstants.NODE);
                if (node != null) {
                    match = selector;//w ww.  j av  a  2s  .co m
                    break;
                }
            } catch (XPathExpressionException e) {
                throw new IllegalArgumentException(selector.path(), e);
            }
        }
    }
    return match;
}

From source file:org.apache.hise.runtime.Task.java

static Map<String, Node> findInputParts(TaskDefinition taskDefinition, Node requestXml) {
    Map<String, Node> inputParts = new HashMap<String, Node>();

    Operation operation = taskDefinition.getPortType()
            .getOperation(taskDefinition.getTaskInterface().getOperation(), null, null);
    if (operation == null) {
        LogFactory.getLog(Task.class).error("Operation: " + taskDefinition.getTaskInterface().getOperation()
                + " not found in port type definition.");
        return inputParts;
    }//ww  w .  j a v a 2  s  .c  om

    Map<String, Part> partsMap = operation.getInput().getMessage().getParts();
    Node messagePart = null;
    for (Part part : partsMap.values()) {
        String name = part.getName();
        QName element = part.getElementName();
        QName type = part.getTypeName();
        Element root = (Element) requestXml;
        XPath xPath = XPathFactory.newInstance().newXPath();
        try {
            if (element != null) {
                Map namespaceMap = new HashMap(1);
                namespaceMap.put("prefix", element.getNamespaceURI());
                NamespaceContext nc = new NamespaceMap(namespaceMap);
                xPath.setNamespaceContext(nc);
                messagePart = (Node) xPath.evaluate("prefix:" + element.getLocalPart(), root,
                        XPathConstants.NODE);
            } else if (type != null) {
                messagePart = (Node) xPath.evaluate("child::*/" + part.getName(), root, XPathConstants.NODE);
            }
        } catch (XPathExpressionException ex) {
            LogFactory.getLog(Task.class).error("Can not get message part.", ex);
        }
        inputParts.put(name, messagePart);
    }

    return inputParts;
}

From source file:org.apache.nifi.minifi.bootstrap.util.ConfigTransformerTest.java

@Test
public void testNullQueuePrioritizerNotWritten() throws ConfigurationChangeException, XPathExpressionException {
    ConfigTransformer.addConnection(config, new ConnectionSchema(Collections.emptyMap()),
            new ParentGroupIdResolver(
                    new ProcessGroupSchema(Collections.emptyMap(), ConfigSchema.TOP_LEVEL_NAME)));
    XPath xpath = xPathFactory.newXPath();
    String expression = "connection/queuePrioritizerClass";
    assertNull(xpath.evaluate(expression, config, XPathConstants.NODE));
}

From source file:org.apache.nifi.minifi.bootstrap.util.ConfigTransformerTest.java

@Test
public void testEmptyQueuePrioritizerNotWritten()
        throws ConfigurationChangeException, XPathExpressionException {
    Map<String, Object> map = new HashMap<>();
    map.put(ConnectionSchema.QUEUE_PRIORITIZER_CLASS_KEY, "");

    ConfigTransformer.addConnection(config, new ConnectionSchema(map), new ParentGroupIdResolver(
            new ProcessGroupSchema(Collections.emptyMap(), ConfigSchema.TOP_LEVEL_NAME)));
    XPath xpath = xPathFactory.newXPath();
    String expression = "connection/queuePrioritizerClass";
    assertNull(xpath.evaluate(expression, config, XPathConstants.NODE));
}

From source file:org.apache.nifi.minifi.bootstrap.util.ConfigTransformerTest.java

public void testConfigFileTransform(String configFile) throws Exception {
    ConfigSchema configSchema = SchemaLoader.loadConfigSchemaFromYaml(
            ConfigTransformerTest.class.getClassLoader().getResourceAsStream(configFile));

    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    ConfigTransformer.writeFlowXmlFile(configSchema, outputStream);
    Document document = documentBuilder.parse(new ByteArrayInputStream(outputStream.toByteArray()));

    testProcessGroup((Element) xPathFactory.newXPath().evaluate("flowController/rootGroup", document,
            XPathConstants.NODE), configSchema.getProcessGroupSchema());
}

From source file:org.apache.ode.bpel.elang.xquery10.runtime.XQuery10ExpressionRuntime.java

/**
 * Cast XQuery sequence into an opaque list
 *
 * @param type type//  w  w w  .  j  a va  2s  . co m
 * @param result result
 *
 * @return value
 *
 * @throws XQException XQException
 */
private Object getResultValue(QName type, XQResultSequence result) throws XQException {
    Document document = DOMUtils.newDocument();
    Object resultValue = null;
    if (XPathConstants.NODESET.equals(type)) {
        List list = new ArrayList();

        while (result.next()) {
            Object itemValue = getItemValue(result.getItem());
            if (itemValue instanceof Document) {
                itemValue = DOMUtils.cloneNode(document, ((Document) itemValue).getDocumentElement());
            } else if (itemValue instanceof Node) {
                itemValue = DOMUtils.cloneNode(document, (Node) itemValue);
            }

            if (itemValue != null) {
                list.add(itemValue);
            }
        }

        resultValue = list;
    } else if (XPathConstants.NODE.equals(type)) {
        XQItem item = null;
        if (result.count() > 0) {
            result.first();
            if (result.isOnItem()) {
                item = result.getItem();
            }
        }
        if (item != null) {
            resultValue = getItemValue(item);
            if (resultValue instanceof Node) {
                resultValue = DOMUtils.cloneNode(document, (Node) resultValue);
            }
        }
    } else if (XPathConstants.STRING.equals(type)) {
        resultValue = result.getSequenceAsString(new Properties());
    } else if (XPathConstants.NUMBER.equals(type)) {
        resultValue = result.getSequenceAsString(new Properties());
        resultValue = Integer.parseInt((String) resultValue);
    } else if (XPathConstants.BOOLEAN.equals(type)) {
        resultValue = result.getSequenceAsString(new Properties());
        resultValue = Boolean.parseBoolean((String) resultValue);
    }
    return resultValue;
}