List of usage examples for javax.xml.xpath XPath setXPathVariableResolver
public void setXPathVariableResolver(XPathVariableResolver resolver);
From source file:MainClass.java
public static void main(String[] args) throws Exception { SimpleDateFormat xmlDateFormat = new SimpleDateFormat("MM.dd.yy"); MapVariableResolver resolver = new MapVariableResolver(); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = dbf.newDocumentBuilder(); Document document = builder.parse(new File("t.xml")); XPathFactory factory = XPathFactory.newInstance(); XPath xPath = factory.newXPath(); xPath.setXPathVariableResolver(resolver); XPathExpression expression = xPath.compile("/schedule/show[@date=$date]/guest"); String formattedDate = xmlDateFormat.format(new Date(2006, 5, 14)); resolver.addVariable(null, "date", formattedDate); Element guest = (Element) expression.evaluate(document, XPathConstants.NODE); System.out.println(guest.getElementsByTagName("name").item(0).getTextContent()); }
From source file:XPathResolver.java
public static void main(String[] args) { XPathFactory factory = XPathFactory.newInstance(); XPath xpath = factory.newXPath(); // Set the NamespaceContext xpath.setNamespaceContext(new MyNamespaceContext()); // Set the function resolver xpath.setXPathFunctionResolver(new MyFunctionResolver()); // Set the variable resolver xpath.setXPathVariableResolver(new MyVariableResolver()); Object result = null;//from w w w . ja va 2 s.c om try { result = xpath.evaluate(EXPR, (Object) null, XPathConstants.NUMBER); } catch (Exception e) { e.printStackTrace(); } // The evaluation result is 9.0. System.out.println("The evaluation result: " + result); }
From source file:Main.java
/** * Create a new {@link XPath} with the passed variable resolver, function * resolver and namespace context./*from ww w . j a v a2 s .co m*/ * * @param aXPathFactory * The XPath factory object to use. May not be <code>null</code>. * @param aVariableResolver * Variable resolver to be used. May be <code>null</code>. * @param aFunctionResolver * Function resolver to be used. May be <code>null</code>. * @param aNamespaceContext * Namespace context to be used. May be <code>null</code>. * @return The created non-<code>null</code> {@link XPath} object */ @Nonnull public static XPath createNewXPath(@Nonnull final XPathFactory aXPathFactory, @Nullable final XPathVariableResolver aVariableResolver, @Nullable final XPathFunctionResolver aFunctionResolver, @Nullable final NamespaceContext aNamespaceContext) { if (aXPathFactory == null) throw new NullPointerException("XPathFactory"); final XPath aXPath = aXPathFactory.newXPath(); if (aVariableResolver != null) aXPath.setXPathVariableResolver(aVariableResolver); if (aFunctionResolver != null) aXPath.setXPathFunctionResolver(aFunctionResolver); if (aNamespaceContext != null) aXPath.setNamespaceContext(aNamespaceContext); return aXPath; }
From source file:com.jkoolcloud.tnt4j.streams.filters.XPathActivityExpressionFilter.java
@Override public boolean doFilter(ActivityInfo activityInfo) throws FilterException { if (activityInfo == null) { return false; }//from ww w. j a va 2 s .c o m Map<String, Object> valuesMap = new HashMap<>(); if (CollectionUtils.isNotEmpty(exprVars)) { Object fValue; String fieldName; for (String eVar : exprVars) { fieldName = eVar.substring(2, eVar.length() - 1); fValue = activityInfo.getFieldValue(fieldName); fieldName = placeHoldersMap.get(eVar); valuesMap.put(StringUtils.isEmpty(fieldName) ? eVar : fieldName, fValue); } } XPath xPath = StreamsXMLUtils.getStreamsXPath(); xPath.setXPathVariableResolver(new XPathActivityExpressionFilter.StreamsVariableResolver(valuesMap)); try { boolean match = "true".equals(xPath.evaluate(getExpression(), (Object) null)); // NON-NLS boolean filteredOut = isFilteredOut(getHandleType(), match); activityInfo.setFiltered(filteredOut); return filteredOut; } catch (Exception exc) { throw new FilterException(StreamsResources.getStringFormatted(StreamsResources.RESOURCE_BUNDLE_NAME, "ExpressionFilter.filtering.failed", filterExpression), exc); } }
From source file:com.jkoolcloud.tnt4j.streams.filters.XPathExpressionFilter.java
@Override public boolean doFilter(Object value, ActivityInfo ai) throws FilterException { Map<String, Object> valuesMap = new HashMap<>(); valuesMap.put(OWN_FIELD_VALUE_KEY, value); if (ai != null && CollectionUtils.isNotEmpty(exprVars)) { for (String eVar : exprVars) { Property eKV = resolveFieldKeyAndValue(eVar, ai); valuesMap.put(eKV.getKey(), eKV.getValue()); }//w ww .j a va 2s. c o m } XPath xPath = StreamsXMLUtils.getStreamsXPath(); xPath.setXPathVariableResolver(new StreamsVariableResolver(valuesMap)); try { boolean match = "true".equals(xPath.evaluate(getExpression(), (Object) null)); // NON-NLS logEvaluationResult(valuesMap, match); return isFilteredOut(getHandleType(), match); } catch (Exception exc) { throw new FilterException(StreamsResources.getStringFormatted(StreamsResources.RESOURCE_BUNDLE_NAME, "ExpressionFilter.filtering.failed", filterExpression), exc); } }
From source file:com.jkoolcloud.tnt4j.streams.transform.XPathTransformation.java
@Override public Object transform(Object value, ActivityInfo ai) throws TransformationException { Map<String, Object> valuesMap = new HashMap<>(); valuesMap.put(OWN_FIELD_VALUE_KEY, value); if (ai != null && CollectionUtils.isNotEmpty(exprVars)) { for (String eVar : exprVars) { Property eKV = resolveFieldKeyAndValue(eVar, ai); valuesMap.put(eKV.getKey(), eKV.getValue()); }//from w w w . j a va 2s . com } XPath xPath = StreamsXMLUtils.getStreamsXPath(); xPath.setXPathVariableResolver(new StreamsVariableResolver(valuesMap)); try { Object tValue = xPath.evaluate(getExpression(), (Object) null); logEvaluationResult(valuesMap, tValue); return tValue; } catch (Exception exc) { throw new TransformationException( StreamsResources.getStringFormatted(StreamsResources.RESOURCE_BUNDLE_NAME, "ValueTransformation.transformation.failed", getName(), getPhase()), exc); } }
From source file:org.apache.camel.builder.xml.XPathBuilder.java
protected XPathExpression createXPathExpression() throws XPathExpressionException, XPathFactoryConfigurationException { XPath xPath = getXPathFactory().newXPath(); xPath.setNamespaceContext(getNamespaceContext()); xPath.setXPathVariableResolver(getVariableResolver()); XPathFunctionResolver parentResolver = getFunctionResolver(); if (parentResolver == null) { parentResolver = xPath.getXPathFunctionResolver(); }/* w w w . j ava 2 s . co m*/ xPath.setXPathFunctionResolver(createDefaultFunctionResolver(parentResolver)); return xPath.compile(text); }
From source file:org.apache.ode.bpel.compiler.v1.xpath10.jaxp.JaxpXPath10ExpressionCompilerImpl.java
/** * Verifies validity of a xpath expression. *//* w w w . j a va 2 s . c o m*/ protected void doJaxpCompile(OXPath10Expression out, Expression source) throws CompilationException { String xpathStr; Node node = source.getExpression(); if (node == null) { throw new IllegalStateException("XPath string and xpath node are both null"); } xpathStr = node.getNodeValue(); xpathStr = xpathStr.trim(); if (xpathStr.length() == 0) { throw new CompilationException(__msgs.errXPathSyntax(xpathStr)); } try { __log.debug("JAXP compile: xpath = " + xpathStr); // use default XPath implementation XPathFactory xpf = XPathFactory.newInstance(); __log.debug("JAXP compile: XPathFactory impl = " + xpf.getClass()); XPath xpath = xpf.newXPath(); xpath.setXPathFunctionResolver( new JaxpFunctionResolver(_compilerContext, out, source.getNamespaceContext(), _bpelNsURI)); xpath.setXPathVariableResolver(new JaxpVariableResolver(_compilerContext, out)); xpath.setNamespaceContext(source.getNamespaceContext()); XPathExpression xpe = xpath.compile(xpathStr); // dummy evaluation to resolve variables and functions (hopefully complete...) xpe.evaluate(DOMUtils.newDocument()); out.xpath = xpathStr; } catch (XPathExpressionException e) { throw new CompilationException(__msgs.errUnexpectedCompilationError(e.getMessage()), e); } }
From source file:org.apache.ode.bpel.compiler.v1.xpath20.XPath20ExpressionCompilerImpl.java
private void doJaxpCompile(OXPath20ExpressionBPEL20 out, Expression source) throws CompilationException { String xpathStr;/* w w w. j av a 2 s .c o m*/ Node node = source.getExpression(); if (node == null) { throw new IllegalStateException("XPath string and xpath node are both null"); } if (node.getNodeType() != Node.TEXT_NODE) { throw new CompilationException(__msgs.errUnexpectedNodeTypeForXPath(DOMUtils.domToString(node))); } xpathStr = node.getNodeValue(); xpathStr = xpathStr.trim(); if (xpathStr.length() == 0) { throw new CompilationException(__msgs.warnXPath20Syntax(DOMUtils.domToString(node), "empty string")); } out.xpath = xpathStr; try { __log.debug("Compiling expression " + xpathStr); System.setProperty("javax.xml.xpath.XPathFactory:" + NamespaceConstant.OBJECT_MODEL_SAXON, "net.sf.saxon.xpath.XPathFactoryImpl"); XPathFactory xpf = XPathFactory.newInstance(NamespaceConstant.OBJECT_MODEL_SAXON); JaxpFunctionResolver funcResolver = new JaxpFunctionResolver(_compilerContext, out, source.getNamespaceContext(), _bpelNS); JaxpVariableResolver varResolver = new JaxpVariableResolver(_compilerContext, out); XPath xpe = xpf.newXPath(); xpe.setXPathFunctionResolver(funcResolver); xpe.setXPathVariableResolver(varResolver); xpe.setNamespaceContext(source.getNamespaceContext()); XPathExpression expr = xpe.compile(xpathStr); // evaluate the expression so as to initialize the variables try { expr.evaluate(node); } catch (XPathExpressionException xpee) { // swallow errors caused by uninitialized variable } for (String varExpr : extractVariableExprs(xpathStr)) { expr = xpe.compile(varExpr); try { expr.evaluate(node); } catch (XPathExpressionException xpee) { // swallow errors caused by uninitialized variable } } } catch (XPathFactoryConfigurationException xpfce) { __log.debug(xpfce); __log.info("Couldn't validate properly expression " + xpathStr); } catch (XPathExpressionException e) { __log.debug(e); __log.info("Couldn't validate properly expression " + xpathStr); } catch (WrappedResolverException wre) { if (wre._compilationMsg != null) throw new CompilationException(wre._compilationMsg, wre); if (wre.getCause() instanceof CompilationException) throw (CompilationException) wre.getCause(); throw wre; } }
From source file:org.apache.ode.bpel.elang.xpath20.compiler.XPath20ExpressionCompilerImpl.java
private void doJaxpCompile(OXPath20ExpressionBPEL20 out, Expression source) throws CompilationException { String xpathStr;// w w w. j av a 2s . c om Node node = source.getExpression(); if (node == null) { throw new CompilationException(__msgs.errEmptyExpression(source.getURI(), new QName(source.getElement().getNamespaceURI(), source.getElement().getNodeName()))); } if (node.getNodeType() != Node.TEXT_NODE && node.getNodeType() != Node.CDATA_SECTION_NODE) { throw new CompilationException(__msgs.errUnexpectedNodeTypeForXPath(DOMUtils.domToString(node))); } xpathStr = node.getNodeValue(); xpathStr = xpathStr.trim(); if (xpathStr.length() == 0) { throw new CompilationException(__msgs.warnXPath20Syntax(DOMUtils.domToString(node), "empty string")); } out.xpath = xpathStr; try { __log.debug("Compiling expression " + xpathStr); XPathFactory xpf = new XPathFactoryImpl(); JaxpFunctionResolver funcResolver = new JaxpFunctionResolver(_compilerContext, out, source.getNamespaceContext(), _bpelNS); JaxpVariableResolver varResolver = new JaxpVariableResolver(_compilerContext, out); XPath xpe = xpf.newXPath(); xpe.setXPathFunctionResolver(funcResolver); xpe.setXPathVariableResolver(varResolver); xpe.setNamespaceContext(source.getNamespaceContext()); XPathExpression expr = xpe.compile(xpathStr); // evaluate the expression so as to initialize the variables try { expr.evaluate(node); } catch (XPathExpressionException xpee) { // swallow errors caused by uninitialized variable } for (String varExpr : extractVariableExprs(xpathStr)) { expr = xpe.compile(varExpr); try { expr.evaluate(node); } catch (XPathExpressionException xpee) { // swallow errors caused by uninitialized variable } } for (String functionExpr : extractFunctionExprs(xpathStr)) { expr = xpe.compile(functionExpr); try { expr.evaluate(node); } catch (XPathExpressionException xpee) { // swallow errors caused by uninitialized variable } } } catch (XPathExpressionException e) { __log.debug(e); __log.info("Couldn't validate properly expression " + xpathStr); } catch (WrappedResolverException wre) { if (wre._compilationMsg != null) throw new CompilationException(wre._compilationMsg, wre); if (wre.getCause() instanceof CompilationException) throw (CompilationException) wre.getCause(); throw wre; } }