Example usage for org.apache.commons.jxpath.functions MethodFunction MethodFunction

List of usage examples for org.apache.commons.jxpath.functions MethodFunction MethodFunction

Introduction

In this page you can find the example usage for org.apache.commons.jxpath.functions MethodFunction MethodFunction.

Prototype

public MethodFunction(Method method) 

Source Link

Document

Create a new MethodFunction.

Usage

From source file:org.chiba.xml.xforms.xpath.ExtensionFunctions.java

private MethodFunction getMethodFunction(Class clazz, String name, Object[] parameters) {
    Method method = MethodLookupUtils.lookupStaticMethod(clazz, name, parameters);
    if (method != null) {
        return new MethodFunction(method);
    }//from  w  w w. j  a v  a2  s. co m

    return null;
}

From source file:org.openvpms.component.system.common.jxpath.AbstractObjectFunctions.java

/**
 * Returns a Function, if any, for the specified namespace,
 * name and parameter types./*  w  w w .jav a  2 s  .c  om*/
 *
 * @param namespace if it is not the namespace specified in the constructor,
 *                  the method returns null
 * @param name      is a function name.
 * @return a MethodFunction, or null if there is no such function.
 */
public Function getFunction(String namespace, String name, Object[] parameters) {
    if (!namespace.equals(this.namespace)) {
        return null;
    }

    if (parameters == null) {
        parameters = EMPTY_ARRAY;
    }

    Method method = MethodLookupUtils.lookupStaticMethod(objectClass, name, parameters);
    if (method != null && Modifier.isStatic(method.getModifiers())) {
        // need to check static due to bug in MethodLookupUtils
        return new MethodFunction(method);
    }

    Object[] params = getParameters(parameters);
    method = MethodLookupUtils.lookupMethod(objectClass, name, params);
    if (method != null) {
        return new MethodFunction(method) {
            @Override
            public Object invoke(ExpressionContext context, Object[] parameters) {
                return super.invoke(context, getParameters(parameters));
            }
        };
    }

    return null;
}

From source file:org.xchain.framework.jxpath.GenericsWisePackageFunctions.java

public Function getFunction(String namespace, String name, Object[] parameters) {
    if ((namespace == null && this.namespace != null) //NOPMD
            || (namespace != null && !namespace.equals(this.namespace))) {
        return null;
    }/*from   w w  w  . j a va 2 s.  c  o m*/

    if (parameters == null) {
        parameters = EMPTY_ARRAY;
    }

    if (parameters.length >= 1) {
        Object target = TypeUtils.convert(parameters[0], Object.class);
        if (target != null) {
            Method method = MethodLookupUtils.lookupMethod(target.getClass(), name, parameters);
            if (method != null) {
                return new MethodFunction(method);
            }

            if (target instanceof NodeSet) {
                target = ((NodeSet) target).getPointers();
            }

            method = MethodLookupUtils.lookupMethod(target.getClass(), name, parameters);
            if (method != null) {
                return new MethodFunction(method);
            }

            if (target instanceof Collection) {
                Iterator iter = ((Collection) target).iterator();
                if (iter.hasNext()) {
                    target = iter.next();
                    if (target instanceof Pointer) {
                        target = ((Pointer) target).getValue();
                    }
                } else {
                    target = null;
                }
            }
        }
        if (target != null) {
            Method method = MethodLookupUtils.lookupMethod(target.getClass(), name, parameters);
            if (method != null) {
                return new MethodFunction(method);
            }
        }
    }

    String fullName = classPrefix + name;
    int inx = fullName.lastIndexOf('.');
    if (inx == -1) {
        return null;
    }

    String className = fullName.substring(0, inx);
    String methodName = fullName.substring(inx + 1);

    Class functionClass;
    try {
        functionClass = Class.forName(className, true, Thread.currentThread().getContextClassLoader());
    } catch (ClassNotFoundException ex) {
        throw new JXPathException(
                "Cannot invoke extension function " + (namespace != null ? namespace + ":" + name : name), ex);
    }

    if (methodName.equals("new")) {
        Constructor constructor = MethodLookupUtils.lookupConstructor(functionClass, parameters);
        if (constructor != null) {
            return new ConstructorFunction(constructor);
        }
    } else {
        Method method = MethodLookupUtils.lookupStaticMethod(functionClass, methodName, parameters);
        if (method != null) {
            return new MethodFunction(method);
        }
    }
    return null;
}

From source file:org.xchain.framework.jxpath.StandardFunctions.java

public Function getFunction(String namespace, String name, Object[] parameters) {
    if ((this.namespace == null && namespace != null)
            || (this.namespace != null && !this.namespace.equals(namespace))) {
        return null;
    }// ww  w. j  a  va2  s.c  o m

    if (parameters == null) {
        parameters = new Object[] {};
    }

    if ("string-join".equals(name)) {
        Method method = MethodLookupUtils.lookupStaticMethod(StandardFunctions.class, "stringJoin", parameters);

        if (method == null) {
            return packageFunctions.getFunction(namespace, name, parameters);
        }

        return new MethodFunction(method);
    }

    return packageFunctions.getFunction(namespace, name, parameters);
}

From source file:org.xchain.framework.lifecycle.NamespaceFunctionLibrary.java

public Function getFunction(String namespace, String name, Object[] parameters) {
    // make sure that we are requesting the correct namespace.
    if (namespace == null && this.namespace != null) {
        return null;
    } else if (!namespace.equals(this.namespace)) {
        return null;
    }/*from   www . j  av  a  2s  . c  om*/

    if (parameters == null) {
        parameters = EMPTY_ARRAY;
    }

    Set<Class> constructorClassSet = constructorClassMap.get(name);
    Constructor constructor;
    if (constructorClassSet != null) {
        for (Class constructorClass : constructorClassSet) {
            constructor = MethodLookupUtils.lookupConstructor(constructorClass, parameters);
            if (constructor != null) {
                return new ConstructorFunction(constructor);
            }
        }
    }

    Set<MethodInfo> methodInfoSet = staticMethodInfoMap.get(name);
    Method method;

    if (methodInfoSet != null) {
        for (MethodInfo methodInfo : methodInfoSet) {
            method = MethodLookupUtils.lookupStaticMethod(methodInfo.getMethodClass(),
                    methodInfo.getMethodName(), parameters);
            if (method != null) {
                return new MethodFunction(method);
            }
        }
    }

    methodInfoSet = instanceMethodInfoMap.get(name);

    if (methodInfoSet != null) {
        for (MethodInfo methodInfo : methodInfoSet) {
            Object[] instanceParameters = parameters;
            Method singletonAccessor = methodInfo.getSingletonAccessor();
            if (singletonAccessor != null) {
                try {
                    Object singleton = singletonAccessor.invoke(null, EMPTY_ARRAY);
                    instanceParameters = new Object[parameters.length + 1];
                    instanceParameters[0] = singleton;
                    for (int i = 0; i < parameters.length; i++) {
                        instanceParameters[i + 1] = parameters[i];
                    }
                    method = MethodLookupUtils.lookupMethod(methodInfo.getMethodClass(),
                            methodInfo.getMethodName(), instanceParameters);
                    if (method != null) {
                        return new SingletonMethodFunction(method, singleton);
                    }
                } catch (Exception e) {
                    if (log.isDebugEnabled()) {
                        log.debug("Could not find singleton for class '" + methodInfo.getMethodClass().getName()
                                + "'.", e);
                    }
                }
            } else {
                method = MethodLookupUtils.lookupMethod(methodInfo.getMethodClass(), methodInfo.getMethodName(),
                        instanceParameters);
                if (method != null) {
                    return new MethodFunction(method);
                }
            }
        }
    }

    return null;
}