Example usage for javax.xml.xpath XPath setNamespaceContext

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

Introduction

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

Prototype

public void setNamespaceContext(NamespaceContext nsContext);

Source Link

Document

Establish a namespace context.

Usage

From source file:org.apache.ode.bpel.rtrep.v1.xpath10.jaxp.JaxpXPath10ExpressionRuntime.java

private Object evaluate(OExpression cexp, EvaluationContext ctx, QName type) throws FaultException {
    try {//from www  . j a  v a 2  s . co m
        OXPath10Expression oxpath = (OXPath10Expression) cexp;
        __log.debug("JAXP runtime: evaluating " + oxpath.xpath);
        // use default XPath implementation
        XPathFactory xpf = XPathFactory.newInstance();
        __log.debug("JAXP runtime: XPathFactory impl = " + xpf.getClass());
        XPath xpe = xpf.newXPath();
        xpe.setXPathFunctionResolver(new JaxpFunctionResolver(ctx, oxpath));
        xpe.setXPathVariableResolver(new JaxpVariableResolver(ctx, oxpath));
        xpe.setNamespaceContext(oxpath.namespaceCtx);
        XPathExpression expr = xpe.compile(((OXPath10Expression) cexp).xpath);
        Object evalResult = expr
                .evaluate(ctx.getRootNode() == null ? DOMUtils.newDocument() : ctx.getRootNode(), type);
        if (evalResult != null && __log.isDebugEnabled()) {
            __log.debug("Expression " + cexp.toString() + " generated result " + evalResult + " - type="
                    + evalResult.getClass().getName());
            if (ctx.getRootNode() != null)
                __log.debug("Was using context node " + DOMUtils.domToString(ctx.getRootNode()));
        }
        return evalResult;
    } catch (XPathExpressionException e) {
        // Extracting the real cause from all this wrapping isn't a simple task
        Throwable cause = e.getCause() != null ? e.getCause() : e;
        throw new FaultException(cexp.getOwner().constants.qnSubLanguageExecutionFault, cause.getMessage(),
                cause);
    } catch (WrappedFaultException wre) {
        __log.debug("Could not evaluate expression because of ", wre);
        throw (FaultException) wre.getCause();
    } catch (Throwable t) {
        __log.debug("Could not evaluate expression because of ", t);
        throw new FaultException(cexp.getOwner().constants.qnSubLanguageExecutionFault, t.getMessage(), t);
    }

}

From source file:org.apache.ode.bpel.rtrep.v1.xpath20.XPath20ExpressionRuntime.java

private Object evaluate(OExpression cexp, EvaluationContext ctx, QName type) throws FaultException {
    try {//  w w  w.  ja  v a2s  .  c  o m
        OXPath20ExpressionBPEL20 oxpath20 = ((OXPath20ExpressionBPEL20) cexp);
        System.setProperty("javax.xml.xpath.XPathFactory:" + NamespaceConstant.OBJECT_MODEL_SAXON,
                "net.sf.saxon.xpath.XPathFactoryImpl");
        // JAXP based XPath 1.0 runtime does not work anymore after a XPath 2.0 has been evaluated if this is set.
        // System.setProperty("javax.xml.xpath.XPathFactory:"+XPathConstants.DOM_OBJECT_MODEL,
        //        "net.sf.saxon.xpath.XPathFactoryImpl");
        System.setProperty("javax.xml.xpath.XPathFactory:" + NamespaceConstant.OBJECT_MODEL_JDOM,
                "net.sf.saxon.xpath.XPathFactoryImpl");
        System.setProperty("javax.xml.xpath.XPathFactory:" + NamespaceConstant.OBJECT_MODEL_XOM,
                "net.sf.saxon.xpath.XPathFactoryImpl");
        System.setProperty("javax.xml.xpath.XPathFactory:" + NamespaceConstant.OBJECT_MODEL_DOM4J,
                "net.sf.saxon.xpath.XPathFactoryImpl");

        XPathFactory xpf = XPathFactory.newInstance(NamespaceConstant.OBJECT_MODEL_SAXON);
        JaxpFunctionResolver funcResolver = new JaxpFunctionResolver(ctx, oxpath20);
        JaxpVariableResolver varResolver = new JaxpVariableResolver(ctx, oxpath20,
                ((XPathFactoryImpl) xpf).getConfiguration());
        xpf.setXPathFunctionResolver(funcResolver);
        xpf.setXPathVariableResolver(varResolver);
        XPath xpe = xpf.newXPath();
        xpe.setNamespaceContext(oxpath20.namespaceCtx);
        XPathExpression expr = xpe.compile(((OXPath10Expression) cexp).xpath);
        Node contextNode = ctx.getRootNode() == null ? DOMUtils.newDocument() : ctx.getRootNode();
        Object evalResult = expr.evaluate(contextNode, type);
        if (evalResult != null && __log.isDebugEnabled()) {
            __log.debug("Expression " + cexp.toString() + " generated result " + evalResult + " - type="
                    + evalResult.getClass().getName());
            if (ctx.getRootNode() != null)
                __log.debug("Was using context node " + DOMUtils.domToString(ctx.getRootNode()));
        }
        return evalResult;
    } catch (XPathExpressionException e) {
        // Extracting the real cause from all this wrapping isn't a simple task
        Throwable cause = e.getCause() != null ? e.getCause() : e;
        if (cause instanceof DynamicError) {
            Throwable th = ((DynamicError) cause).getException();
            if (th != null) {
                cause = th;
                if (cause.getCause() != null)
                    cause = cause.getCause();
            }
        }
        throw new FaultException(cexp.getOwner().constants.qnSubLanguageExecutionFault, cause.getMessage(),
                cause);
    } catch (WrappedFaultException wre) {
        __log.debug("Could not evaluate expression because of ", wre);
        throw (FaultException) wre.getCause();
    } catch (Throwable t) {
        __log.debug("Could not evaluate expression because of ", t);
        throw new FaultException(cexp.getOwner().constants.qnSubLanguageExecutionFault, t.getMessage(), t);
    }
}

From source file:org.apache.ode.bpel.rtrep.v2.xpath20.XPath20ExpressionRuntime.java

private Object evaluate(OExpression cexp, EvaluationContext ctx, QName type) throws FaultException {
    try {//from  www  .ja v  a 2  s.  co m
        OXPath20ExpressionBPEL20 oxpath20 = ((OXPath20ExpressionBPEL20) cexp);
        System.setProperty("javax.xml.xpath.XPathFactory:" + NamespaceConstant.OBJECT_MODEL_SAXON,
                "net.sf.saxon.xpath.XPathFactoryImpl");
        // JAXP based XPath 1.0 runtime does not work anymore after a XPath 2.0 has been evaluated if this is set.
        // System.setProperty("javax.xml.xpath.XPathFactory:"+XPathConstants.DOM_OBJECT_MODEL,
        //        "net.sf.saxon.xpath.XPathFactoryImpl");
        System.setProperty("javax.xml.xpath.XPathFactory:" + NamespaceConstant.OBJECT_MODEL_JDOM,
                "net.sf.saxon.xpath.XPathFactoryImpl");
        System.setProperty("javax.xml.xpath.XPathFactory:" + NamespaceConstant.OBJECT_MODEL_XOM,
                "net.sf.saxon.xpath.XPathFactoryImpl");
        System.setProperty("javax.xml.xpath.XPathFactory:" + NamespaceConstant.OBJECT_MODEL_DOM4J,
                "net.sf.saxon.xpath.XPathFactoryImpl");

        XPathFactory xpf = XPathFactory.newInstance(NamespaceConstant.OBJECT_MODEL_SAXON);
        JaxpFunctionResolver funcResolver = new JaxpFunctionResolver(ctx, oxpath20);
        JaxpVariableResolver varResolver = new JaxpVariableResolver(ctx, oxpath20,
                ((XPathFactoryImpl) xpf).getConfiguration());
        xpf.setXPathFunctionResolver(funcResolver);
        xpf.setXPathVariableResolver(varResolver);
        XPath xpe = xpf.newXPath();
        xpe.setNamespaceContext(oxpath20.namespaceCtx);
        XPathExpression expr = xpe.compile(((OXPath10Expression) cexp).xpath);
        Node contextNode = ctx.getRootNode() == null ? DOMUtils.newDocument() : ctx.getRootNode();
        // Create step nodes in XPath in case it is incompletely instantiated 
        if (oxpath20.insertMissingData) {
            XPath20ExpressionModifier modifier = new XPath20ExpressionModifier(oxpath20.namespaceCtx,
                    ((XPathFactoryImpl) xpf).getConfiguration().getNamePool());
            Node temp = ctx.getRootNode();
            if (temp.getLocalName().equals("message") && temp.getNamespaceURI() == null
                    && temp.getFirstChild() != null && temp.getFirstChild().getFirstChild() != null) {
                modifier.insertMissingData(expr, temp.getFirstChild().getFirstChild());
            } else {
                modifier.insertMissingData(expr, temp);
            }
        }

        Object evalResult = expr.evaluate(contextNode, type);
        if (evalResult != null && __log.isDebugEnabled()) {
            __log.debug("Expression " + cexp.toString() + " generated result " + evalResult + " - type="
                    + evalResult.getClass().getName());
            if (ctx.getRootNode() != null)
                __log.debug("Was using context node " + DOMUtils.domToString(ctx.getRootNode()));
        }
        return evalResult;
    } catch (XPathExpressionException e) {
        // Extracting the real cause from all this wrapping isn't a simple task
        Throwable cause = e.getCause() != null ? e.getCause() : e;
        if (cause instanceof DynamicError) {
            Throwable th = ((DynamicError) cause).getException();
            if (th != null) {
                cause = th;
                if (cause.getCause() != null)
                    cause = cause.getCause();
            }
        }
        throw new FaultException(cexp.getOwner().constants.qnSubLanguageExecutionFault, cause.getMessage(),
                cause);
    } catch (WrappedFaultException wre) {
        __log.debug("Could not evaluate expression because of ", wre);
        throw (FaultException) wre.getCause();
    } catch (Throwable t) {
        __log.debug("Could not evaluate expression because of ", t);
        throw new FaultException(cexp.getOwner().constants.qnSubLanguageExecutionFault, t.getMessage(), t);
    }

}

From source file:org.apache.sling.testing.mock.osgi.OsgiMetadataUtil.java

private static NodeList queryNodes(Document metadata, String xpathQuery) {
    try {//from  www.j av  a 2  s. co m
        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);
    }
}

From source file:org.apache.sling.testing.mock.osgi.OsgiMetadataUtil.java

private static Node queryNode(Document metadata, String xpathQuery) {
    try {//from   ww w  . jav  a2s . c  o  m
        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);
    }
}

From source file:org.apereo.portal.xml.xpath.XPathExpressionFactory.java

@Override
public synchronized Object makeObject(Object key) throws Exception {
    final String expression = (String) key;

    final XPath xPath = xPathFactory.newXPath();
    if (this.namespaceContext != null) {
        xPath.setNamespaceContext(this.namespaceContext);
    }//w  w  w.jav  a2s  .c om
    if (this.variableResolver != null) {
        xPath.setXPathVariableResolver(this.variableResolver);
    }

    logger.debug("Compiling XPathExpression from: {}", expression);

    try {
        return xPath.compile(expression);
    } catch (XPathExpressionException e) {
        throw new RuntimeException("Failed to compile XPath expression '" + expression + "'", e);
    }
}

From source file:org.artifactory.webapp.wicket.util.DescriptionExtractor.java

private String executeQuery(String query) {
    try {//from   w  w w.ja  v a 2s .  c  o m
        XPathFactory xFactory = XPathFactory.newInstance();
        XPath xpath = xFactory.newXPath();
        xpath.setNamespaceContext(new SchemaNamespaceContext());
        XPathExpression expr = xpath.compile(query);
        Object description = expr.evaluate(doc, XPathConstants.STRING);
        return description.toString().trim();
    } catch (XPathExpressionException e) {
        throw new RuntimeException("Failed to execute xpath query: " + query, e);
    }
}

From source file:org.asimba.wa.integrationtest.saml2.model.Assertion.java

private void parseAttributes() {
    String xpathQuery = "/saml2p:Response/saml2:Assertion/saml2:AttributeStatement/saml2:Attribute";

    if (_responseDocument == null) {
        _logger.error("No document specified.");
        return;/*from   ww w  .j a  v a  2 s.c  o m*/
    }

    Map<String, String> attributeMap = new HashMap<>();

    XPath xpath = XPathFactory.newInstance().newXPath();
    xpath.setNamespaceContext(new SimpleSAMLNamespaceContext(null));

    try {
        NodeList nodes = (NodeList) xpath.compile(xpathQuery).evaluate(_responseDocument,
                XPathConstants.NODESET);
        if (nodes != null) {
            for (int i = 0; i < nodes.getLength(); i++) {
                // get value of @Name attribute
                Node n = nodes.item(i);

                String name = n.getAttributes().getNamedItem("Name").getNodeValue();

                // get the first AttributeValue childnode
                NodeList valueNodes = n.getChildNodes();
                for (int ci = 0; ci < valueNodes.getLength(); ci++) {
                    Node cn = valueNodes.item(ci);
                    String nodename = cn.getLocalName();
                    if ("AttributeValue".equals(nodename)) {
                        String value = cn.getTextContent().trim();
                        attributeMap.put(name, value);
                    }
                }
            }
        }
    } catch (XPathExpressionException e) {
        _logger.error("Exception when getting attributes: {}", e.getMessage(), e);
        return;
    }

    _responseAttributes = attributeMap;
}

From source file:org.asimba.wa.integrationtest.saml2.model.Assertion.java

public Node getAssertionNode() {
    if (_responseDocument == null)
        return null;

    String xpathQuery = "/saml2p:Response/saml2:Assertion";

    XPath xpath = XPathFactory.newInstance().newXPath();
    xpath.setNamespaceContext(new SimpleSAMLNamespaceContext(null));

    try {/*from  w  w  w. jav  a  2s  .  co  m*/
        return (Node) xpath.compile(xpathQuery).evaluate(_responseDocument, XPathConstants.NODE);
    } catch (XPathExpressionException e) {
        _logger.error("Exception when processing XPath Query: {}", e.getMessage(), e);
        return null;
    }

}

From source file:org.atricore.idbus.capabilities.sso.support.core.signature.JSR105SamlR2SignerImpl.java

protected NodeList evaluateXPath(Document doc, String expression) throws SamlR2SignatureException {
    XPathFactory factory = XPathFactory.newInstance();
    XPath xpath = factory.newXPath();
    xpath.setNamespaceContext(getNamespaceContext());

    NodeList nl;//from w w  w  .jav  a2s.com
    try {
        XPathExpression expr = xpath.compile(expression);

        nl = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
    } catch (XPathExpressionException e) {
        throw new SamlR2SignatureException(e);
    }

    return nl;
}