1 16 package org.apache.commons.jxpath; 17 18 import java.lang.reflect.Constructor ; 19 import java.lang.reflect.Method ; 20 import java.util.Collection ; 21 import java.util.Collections ; 22 import java.util.Iterator ; 23 import java.util.Set ; 24 25 import org.apache.commons.jxpath.functions.ConstructorFunction; 26 import org.apache.commons.jxpath.functions.MethodFunction; 27 import org.apache.commons.jxpath.util.MethodLookupUtils; 28 import org.apache.commons.jxpath.util.TypeUtils; 29 30 70 public class PackageFunctions implements Functions { 71 private String classPrefix; 72 private String namespace; 73 private static final Object [] EMPTY_ARRAY = new Object [0]; 74 75 public PackageFunctions(String classPrefix, String namespace) { 76 this.classPrefix = classPrefix; 77 this.namespace = namespace; 78 } 79 80 83 public Set getUsedNamespaces() { 84 return Collections.singleton(namespace); 85 } 86 87 108 public Function getFunction( 109 String namespace, 110 String name, 111 Object [] parameters) 112 { 113 if ((namespace == null && this.namespace != null) 114 || (namespace != null && !namespace.equals(this.namespace))) { 115 return null; 116 } 117 118 if (parameters == null) { 119 parameters = EMPTY_ARRAY; 120 } 121 122 if (parameters.length >= 1) { 123 Object target = TypeUtils.convert(parameters[0], Object .class); 124 if (target != null) { 125 Method method = 126 MethodLookupUtils.lookupMethod( 127 target.getClass(), 128 name, 129 parameters); 130 if (method != null) { 131 return new MethodFunction(method); 132 } 133 134 if (target instanceof NodeSet) { 135 target = ((NodeSet) target).getPointers(); 136 } 137 138 method = 139 MethodLookupUtils.lookupMethod( 140 target.getClass(), 141 name, 142 parameters); 143 if (method != null) { 144 return new MethodFunction(method); 145 } 146 147 if (target instanceof Collection ) { 148 Iterator iter = ((Collection ) target).iterator(); 149 if (iter.hasNext()) { 150 target = iter.next(); 151 if (target instanceof Pointer) { 152 target = ((Pointer) target).getValue(); 153 } 154 } 155 else { 156 target = null; 157 } 158 } 159 } 160 if (target != null) { 161 Method method = 162 MethodLookupUtils.lookupMethod( 163 target.getClass(), 164 name, 165 parameters); 166 if (method != null) { 167 return new MethodFunction(method); 168 } 169 } 170 } 171 172 String fullName = classPrefix + name; 173 int inx = fullName.lastIndexOf('.'); 174 if (inx == -1) { 175 return null; 176 } 177 178 String className = fullName.substring(0, inx); 179 String methodName = fullName.substring(inx + 1); 180 181 Class functionClass; 182 try { 183 functionClass = Class.forName(className); 184 } 185 catch (ClassNotFoundException ex) { 186 throw new JXPathException( 187 "Cannot invoke extension function " 188 + (namespace != null ? namespace + ":" + name : name), 189 ex); 190 } 191 192 if (methodName.equals("new")) { 193 Constructor constructor = 194 MethodLookupUtils.lookupConstructor(functionClass, parameters); 195 if (constructor != null) { 196 return new ConstructorFunction(constructor); 197 } 198 } 199 else { 200 Method method = 201 MethodLookupUtils.lookupStaticMethod( 202 functionClass, 203 methodName, 204 parameters); 205 if (method != null) { 206 return new MethodFunction(method); 207 } 208 } 209 return null; 210 } 211 } | Popular Tags |