Package javax.xml.xpath

Examples of javax.xml.xpath.XPath


        return null;
    }

    private static NodeList queryNodes(Document metadata, String xpathQuery) {
        try {
            XPath xpath = XPATH_FACTORY.newXPath();
            xpath.setNamespaceContext(NAMESPACE_CONTEXT);
            return (NodeList) xpath.evaluate(xpathQuery, metadata, XPathConstants.NODESET);
        } catch (XPathExpressionException ex) {
            throw new RuntimeException("Error evaluating XPath: " + xpathQuery, ex);
        }
    }
View Full Code Here


        }
    }

    private static Node queryNode(Document metadata, String xpathQuery) {
        try {
            XPath xpath = XPATH_FACTORY.newXPath();
            xpath.setNamespaceContext(NAMESPACE_CONTEXT);
            return (Node) xpath.evaluate(xpathQuery, metadata, XPathConstants.NODE);
        } catch (XPathExpressionException ex) {
            throw new RuntimeException("Error evaluating XPath: " + xpathQuery, ex);
        }
    }
View Full Code Here

        }
    }

    private Object[] getMethodArguments(Element payloadElement, Method method) throws XPathExpressionException {
        Class<?>[] parameterTypes = method.getParameterTypes();
        XPath xpath = createXPath();
        Object[] args = new Object[parameterTypes.length];
        for (int i = 0; i < parameterTypes.length; i++) {
            String expression = getXPathParamAnnotation(method, i).value();
            QName conversionType;
            if (Boolean.class.isAssignableFrom(parameterTypes[i]) || Boolean.TYPE.isAssignableFrom(parameterTypes[i])) {
                conversionType = XPathConstants.BOOLEAN;
            }
            else
            if (Double.class.isAssignableFrom(parameterTypes[i]) || Double.TYPE.isAssignableFrom(parameterTypes[i])) {
                conversionType = XPathConstants.NUMBER;
            }
            else if (Node.class.isAssignableFrom(parameterTypes[i])) {
                conversionType = XPathConstants.NODE;
            }
            else if (NodeList.class.isAssignableFrom(parameterTypes[i])) {
                conversionType = XPathConstants.NODESET;
            }
            else if (String.class.isAssignableFrom(parameterTypes[i])) {
                conversionType = XPathConstants.STRING;
            }
            else {
                throw new IllegalArgumentException("Invalid parameter type [" + parameterTypes[i] + "]. " +
                        "Supported are: Boolean, Double, Node, NodeList, and String.");
            }
            args[i] = xpath.evaluate(expression, payloadElement, conversionType);
        }
        return args;
    }
View Full Code Here

        }
        return args;
    }

    private synchronized XPath createXPath() {
        XPath xpath = xpathFactory.newXPath();
        if (namespaces != null) {
            SimpleNamespaceContext namespaceContext = new SimpleNamespaceContext();
            namespaceContext.setBindings(namespaces);
            xpath.setNamespaceContext(namespaceContext);
        }
        return xpath;
    }
View Full Code Here

        if (evaluationReturnType == null) {
            evaluationReturnType = XPathConstants.STRING;
            useConversionService = true;
        }

        XPath xpath = createXPath();
        xpath.setNamespaceContext(NamespaceUtils.getNamespaceContext(parameter.getMethod()));

        Element rootElement = getRootElement(messageContext.getRequest().getPayloadSource());
        String expression = parameter.getParameterAnnotation(XPathParam.class).value();
        Object result = xpath.evaluate(expression, rootElement, evaluationReturnType);
        return useConversionService ? conversionService.convert(result, parameterType) : result;
    }
View Full Code Here

  public boolean isValid(C24Object c24) throws Exception {
    Document document = DocumentBuilderFactory.newInstance()
        .newDocumentBuilder()
        .parse(new ByteArrayInputStream(c24.getXml().getBytes()));

    XPath xpath = XPathFactory.newInstance().newXPath();
    XPathExpression xpathExpression = xpath.compile("//transaction/value");
    String result = xpathExpression.evaluate(document).toLowerCase();
   
    return result.contains("valid");
  }
View Full Code Here

    @SuppressWarnings("unchecked")
    public static XPathExpression getXPathExpression(XPathFilterParameterSpec xpathFilter) throws XPathExpressionException {

        XPathFactory factory = XPathFactory.newInstance();
        XPath xpath = factory.newXPath();
        if (xpathFilter.getNamespaceMap() != null) {
            xpath.setNamespaceContext(new XPathNamespaceContext(xpathFilter.getNamespaceMap()));
        }
        return xpath.compile(xpathFilter.getXPath());
    }
View Full Code Here

    @Override
    protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response)
            throws ServletException, IOException {

        try {
            XPath xpath = XPathFactory.newInstance().newXPath();

            String result = xpath.evaluate("/content/name", new InputSource(new StringReader(XML_INPUT)));

            response.setContentType("text/plain");
            response.getWriter().write(result);
        } catch (XPathExpressionException e) {
            throw new ServletException(e.getMessage(), e);
View Full Code Here

        throws XPathExpressionException, SAXException, IOException, ParserConfigurationException {
        Message mess = getMessage(mock);
        InputStream body = mess.getBody(InputStream.class);
        assertNotNull(body);
        XPathFactory xpathFactory = XPathFactory.newInstance();
        XPath xpath = xpathFactory.newXPath();
        NamespaceContext nc = new NamespaceContext() {

            @SuppressWarnings("rawtypes")
            @Override
            public Iterator getPrefixes(String namespaceURI) {
                return null;
            }

            @Override
            public String getPrefix(String namespaceURI) {
                return null;
            }

            @Override
            public String getNamespaceURI(String prefix) {
                return prefix2Namespace.get(prefix);
            }
        };
        xpath.setNamespaceContext(nc);
        XPathExpression expr = xpath.compile(xpathString);
        Object result = expr.evaluate(XmlSignatureHelper.newDocumentBuilder(true).parse(body), XPathConstants.NODE);
        assertNotNull("The xpath " + xpathString + " returned a null value", result);
        return result;
    }
View Full Code Here

    }

    public static Map<String, String> getScrProperties(String xmlLocation, String componentName) throws Exception {
        Map<String, String> result = new HashMap<String, String>();
        final Document dom = readXML(new File(xmlLocation));
        final XPath xPath = XPathFactory.newInstance(XPathFactory.DEFAULT_OBJECT_MODEL_URI, "com.sun.org.apache.xpath.internal.jaxp.XPathFactoryImpl", null).newXPath();
        xPath.setNamespaceContext(new NamespaceContext() {
            @Override
            public String getNamespaceURI(String prefix) {
                switch (prefix) {
                    case "scr":
                        try {
                            XPathExpression scrNamespace = xPath.compile("/*/namespace::*[name()='scr']");
                            Node node = (Node) scrNamespace.evaluate(dom, XPathConstants.NODE);
                            return node.getNodeValue();
                        } catch (XPathExpressionException e) {
                            // ignore
                            LOG.debug("Error evaluating xpath to obtain namespace prefix. This exception is ignored and using namespace: http://www.osgi.org/xmlns/scr/v1.1.0", e);

                        }
                        return "http://www.osgi.org/xmlns/scr/v1.1.0";
                    default:
                        // noop
                }
                return XMLConstants.NULL_NS_URI;
            }

            @Override
            public String getPrefix(String namespaceURI) {
                return null;
            }

            @Override
            public Iterator getPrefixes(String namespaceURI) {
                return null;
            }
        });

        String propertyListExpression = String.format("/components/scr:component[@name='%s']/property", componentName);
        XPathExpression propertyList = xPath.compile(propertyListExpression);
        XPathExpression propertyName = xPath.compile("@name");
        XPathExpression propertyValue = xPath.compile("@value");
        NodeList nodes = (NodeList) propertyList.evaluate(dom, XPathConstants.NODESET);
        for (int i = 0; i < nodes.getLength(); i++) {
            Node node = nodes.item(i);
            result.put((String) propertyName.evaluate(node, XPathConstants.STRING), (String) propertyValue.evaluate(node, XPathConstants.STRING));
        }
View Full Code Here

TOP

Related Classes of javax.xml.xpath.XPath

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.