List of usage examples for javax.xml.xpath XPathFactory setXPathVariableResolver
public abstract void setXPathVariableResolver(XPathVariableResolver resolver);
Establish a default variable resolver.
Any XPath
objects constructed from this factory will use the specified resolver by default.
A NullPointerException
is thrown if resolver
is null
.
From source file:net.javacrumbs.springws.test.common.XPathExpressionEvaluator.java
public String evaluateExpression(Document document, String expression, URI uri, NamespaceContext namespaceContext) { XPathFactory factory = XPathFactory.newInstance(); factory.setXPathVariableResolver(new WsTestXPathVariableResolver(uri)); try {//from w w w .jav a 2s.co m XPath xpath = factory.newXPath(); if (namespaceContext != null) { xpath.setNamespaceContext(namespaceContext); } String result = xpath.evaluate(expression, document); logger.debug("Expression \"" + expression + "\" resolved to \"" + result + "\""); return result; } catch (XPathExpressionException e) { throw new ExpressionResolverException( "Could not evaluate XPath expression \"" + expression + "\" : \"" + e.getMessage() + "\"", e); } }
From source file:org.jasig.springframework.security.portlet.authentication.PortletXmlMappableAttributesRetriever.java
/** * Loads the portlet.xml file using the configured <tt>ResourceLoader</tt> and * parses the role-name elements from it, using these as the set of <tt>mappableAttributes</tt>. *//* w ww . j a v a 2 s. co m*/ public void afterPropertiesSet() throws Exception { Resource portletXml = resourceLoader.getResource("/WEB-INF/portlet.xml"); Document doc = getDocument(portletXml.getInputStream()); final XPathExpression roleNamesExpression; if (portletConfig == null) { final XPathFactory xPathFactory = XPathFactory.newInstance(); final XPath xPath = xPathFactory.newXPath(); roleNamesExpression = xPath.compile("/portlet-app/portlet/security-role-ref/role-name"); } else { final XPathFactory xPathFactory = XPathFactory.newInstance(); xPathFactory.setXPathVariableResolver(new XPathVariableResolver() { @Override public Object resolveVariable(QName variableName) { if ("portletName".equals(variableName.getLocalPart())) { return portletConfig.getPortletName(); } return null; } }); final XPath xPath = xPathFactory.newXPath(); roleNamesExpression = xPath .compile("/portlet-app/portlet[portlet-name=$portletName]/security-role-ref/role-name"); } final NodeList securityRoles = (NodeList) roleNamesExpression.evaluate(doc, XPathConstants.NODESET); final Set<String> roleNames = new HashSet<String>(); for (int i = 0; i < securityRoles.getLength(); i++) { Element secRoleElt = (Element) securityRoles.item(i); String roleName = secRoleElt.getTextContent().trim(); roleNames.add(roleName); logger.info("Retrieved role-name '" + roleName + "' from portlet.xml"); } if (roleNames.isEmpty()) { logger.info("No security-role-ref elements found in " + portletXml + (portletConfig == null ? "" : " for portlet " + portletConfig.getPortletName())); } mappableAttributes = Collections.unmodifiableSet(roleNames); }
From source file:org.apache.ode.bpel.rtrep.v1.xpath20.XPath20ExpressionRuntime.java
private Object evaluate(OExpression cexp, EvaluationContext ctx, QName type) throws FaultException { try {/* ww w. ja va 2 s . 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 {/* 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); } }