| career | drupal | java | mac | mysql | perl | scala | uml | unix  

Commons JXPath example source code file (

This example Commons JXPath source code file ( is included in the "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Commons JXPath tags/keywords

classfunctions, classfunctions, empty_array, function, functions, method, methodfunction, methodfunction, methodlookuputils, object, object, reflection, set, string, string, util

The Commons JXPath source code

 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * See the License for the specific language governing permissions and
 * limitations under the License.
package org.apache.commons.jxpath;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.Collections;
import java.util.Set;

import org.apache.commons.jxpath.functions.ConstructorFunction;
import org.apache.commons.jxpath.functions.MethodFunction;
import org.apache.commons.jxpath.util.MethodLookupUtils;

 * Extension functions provided by a Java class.
 * Let's say we declared a ClassFunction like this:
 * <blockquote>
 *     new ClassFunctions(Integer.class, "int")
 * </pre>
 * We can now use XPaths like:
 * <dl>
 *  <dt>"int:new(3)"
 *  <dd>Equivalent to new Integer(3)
 *  <dt>"int:getInteger('foo')"
 *  <dd>Equivalent to Integer.getInteger("foo")
 *  <dt>"int:floatValue(int:new(4))"
 *  <dd>Equivalent to new Integer(4).floatValue()
 * </dl>
 * <p>
 * If the first argument of a method is {@link ExpressionContext}, the
 * expression context in which the function is evaluated is passed to
 * the method.
 * @author Dmitri Plotnikov
 * @version $Revision: 652845 $ $Date: 2008-05-02 12:46:46 -0500 (Fri, 02 May 2008) $
public class ClassFunctions implements Functions {
    private static final Object[] EMPTY_ARRAY = new Object[0];

    private Class functionClass;
    private String namespace;

     * Create a new ClassFunctions.
     * @param functionClass Class providing the functions
     * @param namespace assigned ns
    public ClassFunctions(Class functionClass, String namespace) {
        this.functionClass = functionClass;
        this.namespace = namespace;

     * Returns a set of one namespace - the one specified in the constructor.
     * @return a singleton
    public Set getUsedNamespaces() {
        return Collections.singleton(namespace);

     * Returns a {@link Function}, if any, for the specified namespace,
     * name and parameter types.
     * @param namespace if it is not the namespace specified in the constructor,
     *     the method returns null
     * @param name is a function name or "new" for a constructor.
     * @param parameters Object[] of parameters
     * @return a MethodFunction, a ConstructorFunction or null if there is no
     *      such function.
    public Function getFunction(
        String namespace,
        String name,
        Object[] parameters) {
        if (namespace == null) {
            if (this.namespace != null) {
                return null;
        else if (!namespace.equals(this.namespace)) {
            return null;

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

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

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

        return null;

Other Commons JXPath examples (source code examples)

Here is a short list of links related to this Commons JXPath source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller


new blog posts


Copyright 1998-2021 Alvin Alexander,
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.