/** * Is the extension function available? */ public boolean functionAvailable(String ns, String funcName) throws javax.xml.transform.TransformerException { try { if ( funcName == null ) { String fmsg = XSLMessages.createXPATHMessage( XPATHErrorResources.ER_ARG_CANNOT_BE_NULL, new Object[] {"Function Name"} ); throw new NullPointerException ( fmsg ); } //Find the XPathFunction corresponding to namespace and funcName javax.xml.namespace.QName myQName = new QName( ns, funcName ); javax.xml.xpath.XPathFunction xpathFunction = resolver.resolveFunction ( myQName, 0 ); if ( xpathFunction == null ) { return false; } return true; } catch ( Exception e ) { return false; } }
private XPathFunction createPropertiesFunction() { return new XPathFunction() { @SuppressWarnings("rawtypes") public Object evaluate(List list) throws XPathFunctionException { if (!list.isEmpty()) { Object value = list.get(0); if (value != null) { String text = exchange.get().getContext().getTypeConverter().convertTo(String.class, value); try { // use the property placeholder resolver to lookup the property for us Object answer = exchange.get().getContext().resolvePropertyPlaceholders("{{" + text + "}}"); return answer; } catch (Exception e) { throw new XPathFunctionException(e); } } } return null; } }; }
private XPathFunction createSimpleFunction() { return new XPathFunction() { @SuppressWarnings("rawtypes") public Object evaluate(List list) throws XPathFunctionException { if (!list.isEmpty()) { Object value = list.get(0); if (value != null) { String text = exchange.get().getContext().getTypeConverter().convertTo(String.class, value); Language simple = exchange.get().getContext().resolveLanguage("simple"); Expression exp = simple.createExpression(text); Object answer = exp.evaluate(exchange.get(), Object.class); return answer; } } return null; } }; }
@Override public void endFunction() throws SAXPathException{ ArrayDeque params = popFrame(); javax.xml.namespace.QName name = (javax.xml.namespace.QName)params.pollFirst(); if(name.getNamespaceURI().length()==0) push(createFunction(name.getLocalPart(), params).simplify()); else{ int noOfParams = params.size(); XPathFunction function = functionResolver.resolveFunction(name, noOfParams); if(function==null) throw new SAXPathException("Unknown Function: "+name); FunctionCall functionCall = new FunctionCall(new Functions.UserFunction(name.getNamespaceURI(), name.getLocalPart(), function), noOfParams); for(int i=0; i<noOfParams; i++) functionCall.addMember(params.pollFirst(), i); push(functionCall); } }
/** * Look for a function in the registered XPathFunctionResolvers. * @param iri The URI of the function to find. * @return The requested XPathFunction, or <code>null</code> if not found. */ private XPathFunction findFunction(IRI iri, int argCount) { String label = iri.toString() + "/" + argCount; XPathFunction result = fnCache.get(label); if (result == null) { QName fnName = iri.getQName(); if (fnName == null) return null; for (XPathFunctionResolver resolver: FunctionResolverRegistry.getFunctionResolverRegistry()) { try { result = resolver.resolveFunction(fnName, argCount); if (result != null) { fnCache.put(label, result); break; } } catch (Exception e) { // this resolver is unable to handle the given QName result = null; } } } return result; }
public void init(Node ctx, final Map<String, Object> properties, final ReferenceResolver referenceResolver){ XPathFactory factory = XPathFactory.newInstance(); xpath = factory.newXPath(); xpath.setXPathVariableResolver(new XPathVariableResolver(){ @Override public Object resolveVariable(QName variableName) { return properties.get(variableName.getLocalPart()); } }); xpath.setXPathFunctionResolver(new XPathFunctionResolver(){ @Override public XPathFunction resolveFunction(QName name, int arity) { if (FUNC_RANDOM.equals(name.getLocalPart())){ return new XPathFunctionRandom(); } if (FUNC_EVALUATE.equals(name.getLocalPart())){ return new XPathFunctionEvaluate(referenceResolver); } return null; } }); this.ctx=ctx; }
@Override public XPathFunction resolveFunction(QName functionName, int arity) { if (functionName == null) { throw new NullPointerException("function name cannot be null"); } if (XmlNamespace.Signavio.getUri().equals(functionName.getNamespaceURI())) { if (FUNCTION_IS_EMPTY.equals(functionName.getLocalPart())) { return new SignavioIsEmptyFunction(); // } else if (<your function here>.equals(functionName.getLocalPart())) { } } return null; }
@Override public XPathFunction resolveFunction(final QName functionName, final int arity) { if (functionMap.containsKey(functionName)) { return new XPathFunction() { @Override public Object evaluate(List args) throws XPathFunctionException { return functionMap.get(functionName).evaluate(args); } }; } return originalResolver.resolveFunction(functionName, arity); }
@Override public XPathFunction resolveFunction(QName functionQName, int arity) { boolean enableDebug = false; String namespace = functionQName.getNamespaceURI(); if (StringUtils.isEmpty(namespace)) { namespace = MidPointConstants.NS_FUNC_BASIC; enableDebug = true; } else if (namespace.equals(MidPointConstants.NS_FUNC_BASIC)) { enableDebug = true; } FunctionLibrary lib = findLibrary(namespace); if (lib == null) { LOGGER.trace("Unknown namespace for function {} function with {} arguments", functionQName, arity); return null; } Object functionObject = null; if (lib.getXmlFunctions() != null) { functionObject = lib.getXmlFunctions(); } else { functionObject = lib.getGenericFunctions(); } String functionName = functionQName.getLocalPart(); LOGGER.trace("Resolving to {} function with {} arguments", functionName, arity); ReflectionXPathFunctionWrapper xPathFunction = new ReflectionXPathFunctionWrapper(functionObject, functionName, arity, enableDebug); return xPathFunction; }
/** * {@inheritDoc} */ public XPathFunction resolveFunction (QName name, int arity) { // Search up the view hierarchy for a matching function for (View view = View.this; view != null; view = view.parent) { Function function = view.functions.get (name); if (function != null) return (function.getInstance ()); } return (null); }
public XPathFunction resolveFunction(QName name, int arity) { String uri = name.getNamespaceURI(); if (XSL_NS.equals(uri) || uri == null || uri.length() == 0) { String localName = name.getLocalPart(); if ("document".equals(localName) && (arity == 1 || arity == 2)) { if (current == null) throw new RuntimeException("current is null"); return new DocumentFunction(getRootStylesheet(), current); } else if ("key".equals(localName) && (arity == 2)) return new KeyFunction(getRootStylesheet()); else if ("format-number".equals(localName) && (arity == 2 || arity == 3)) return new FormatNumberFunction(getRootStylesheet()); else if ("current".equals(localName) && (arity == 0)) return new CurrentFunction(getRootStylesheet()); else if ("unparsed-entity-uri".equals(localName) && (arity == 1)) return new UnparsedEntityUriFunction(); else if ("generate-id".equals(localName) && (arity == 1 || arity == 0)) return new GenerateIdFunction(); else if ("system-property".equals(localName) && (arity == 1)) return new SystemPropertyFunction(); else if ("element-available".equals(localName) && (arity == 1)) return new ElementAvailableFunction(new NamespaceProxy(current)); else if ("function-available".equals(localName) && (arity == 1)) return new FunctionAvailableFunction(new NamespaceProxy(current)); } return null; }
private XPathFunction createBodyFunction() { return new XPathFunction() { @SuppressWarnings("rawtypes") public Object evaluate(List list) throws XPathFunctionException { return exchange.get().getIn().getBody(); } }; }
private XPathFunction createHeaderFunction() { return new XPathFunction() { @SuppressWarnings("rawtypes") public Object evaluate(List list) throws XPathFunctionException { if (!list.isEmpty()) { Object value = list.get(0); if (value != null) { String text = exchange.get().getContext().getTypeConverter().convertTo(String.class, value); return exchange.get().getIn().getHeader(text); } } return null; } }; }
private XPathFunction createOutBodyFunction() { return new XPathFunction() { @SuppressWarnings("rawtypes") public Object evaluate(List list) throws XPathFunctionException { if (exchange.get() != null && exchange.get().hasOut()) { return exchange.get().getOut().getBody(); } return null; } }; }
private XPathFunction createOutHeaderFunction() { return new XPathFunction() { @SuppressWarnings("rawtypes") public Object evaluate(List list) throws XPathFunctionException { if (exchange.get() != null && !list.isEmpty()) { Object value = list.get(0); if (value != null) { String text = exchange.get().getContext().getTypeConverter().convertTo(String.class, value); return exchange.get().getOut().getHeader(text); } } return null; } }; }
@Override public XPathFunction resolveFunction(QName functionName, int arity) { if (eq(functionName.getLocalPart(), "inBlacklist") && arity == 1) { return new InBlacklistFunction(); } return null; }
@Override public XPathFunction resolveFunction(QName functionName, int arity) { if(functionName.equals(new QName("http://jlibs.googlecode.com", "reverse"))) return REVERSE; else return null; }
/** * Add a new function. * * @param aName * The qualified name of the function * @param nArity * The number of parameters of the function * @param aFunction * The function to be used. May not be <code>null</code>. * @return {@link EChange} */ @Nonnull public EChange addUniqueFunction (@Nonnull final QName aName, @Nonnegative final int nArity, @Nonnull final XPathFunction aFunction) { ValueEnforcer.notNull (aFunction, "Function"); final XPathFunctionKey aFunctionKey = new XPathFunctionKey (aName, nArity); if (m_aMap.containsKey (aFunctionKey)) return EChange.UNCHANGED; m_aMap.put (aFunctionKey, aFunction); return EChange.CHANGED; }
/** * Add all functions from the other function resolver into this resolver. * * @param aOther * The function resolver to import the functions from. May not be * <code>null</code>. * @param bOverwrite * if <code>true</code> existing functions will be overwritten with the * new functions, otherwise the old functions are kept. * @return {@link EChange} */ @Nonnull public EChange addAllFrom (@Nonnull final MapBasedXPathFunctionResolver aOther, final boolean bOverwrite) { ValueEnforcer.notNull (aOther, "Other"); EChange eChange = EChange.UNCHANGED; for (final Map.Entry <XPathFunctionKey, XPathFunction> aEntry : aOther.m_aMap.entrySet ()) if (bOverwrite || !m_aMap.containsKey (aEntry.getKey ())) { m_aMap.put (aEntry.getKey (), aEntry.getValue ()); eChange = EChange.CHANGED; } return eChange; }
/** * @return A mutable copy of all contained functions. Never <code>null</code> * but maybe empty. */ @Nonnull @ReturnsMutableCopy public ICommonsOrderedMap <XPathFunctionKey, XPathFunction> getAllFunctions () { return m_aMap.getClone (); }
/** * @see javax.xml.xpath.XPathFunctionResolver#resolveFunction(javax.xml.namespace.QName, int) */ public XPathFunction resolveFunction(QName functionName, int arity) { XPathFunction result = null; String namespace = functionName.getNamespaceURI(); if (namespace != null) { Map<String,XPathFunction> fnGroupMap = functionGroups.get(namespace); if (fnGroupMap != null) { result = fnGroupMap.get(functionName.getLocalPart() + "/" + arity); // fall back to multiple arity if (result == null) result = fnGroupMap.get(functionName.getLocalPart() + "/*"); } } return result; }
/** * A helper method to create a mapping of function names to their implementing classes, * and of namespaces to these mappings. * @param fnGroup A group of functions to be added into a single namespace. * This group also provides that namespace. */ protected final void addFunctionGroup(MulgaraFunctionGroup fnGroup) { // map the function names to the functions Map<String,XPathFunction> functionMap = new HashMap<String,XPathFunction>(); for (MulgaraFunction fn: fnGroup.getAllFunctions()) functionMap.put(fn.getName(), fn); // map the namespace to the name->function map functionGroups.put(fnGroup.getNamespace(), functionMap); }
public XPathFunction resolveFunction(QName functionName, int arity) { // not a real ewsolver, always return a default XPathFunction return new MyXPathFunction(); }