1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.jxpath;
18
19 import java.lang.reflect.Constructor;
20 import java.lang.reflect.Method;
21 import java.util.Collection;
22 import java.util.Collections;
23 import java.util.Iterator;
24 import java.util.Set;
25
26 import org.apache.commons.jxpath.functions.ConstructorFunction;
27 import org.apache.commons.jxpath.functions.MethodFunction;
28 import org.apache.commons.jxpath.util.ClassLoaderUtil;
29 import org.apache.commons.jxpath.util.MethodLookupUtils;
30 import org.apache.commons.jxpath.util.TypeUtils;
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71 public class PackageFunctions implements Functions {
72 private String classPrefix;
73 private String namespace;
74 private static final Object[] EMPTY_ARRAY = new Object[0];
75
76
77
78
79
80
81 public PackageFunctions(String classPrefix, String namespace) {
82 this.classPrefix = classPrefix;
83 this.namespace = namespace;
84 }
85
86
87
88
89
90 public Set getUsedNamespaces() {
91 return Collections.singleton(namespace);
92 }
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115 public Function getFunction(
116 String namespace,
117 String name,
118 Object[] parameters) {
119 if ((namespace == null && this.namespace != null)
120 || (namespace != null && !namespace.equals(this.namespace))) {
121 return null;
122 }
123
124 if (parameters == null) {
125 parameters = EMPTY_ARRAY;
126 }
127
128 if (parameters.length >= 1) {
129 Object target = TypeUtils.convert(parameters[0], Object.class);
130 if (target != null) {
131 Method method =
132 MethodLookupUtils.lookupMethod(
133 target.getClass(),
134 name,
135 parameters);
136 if (method != null) {
137 return new MethodFunction(method);
138 }
139
140 if (target instanceof NodeSet) {
141 target = ((NodeSet) target).getPointers();
142 }
143
144 method =
145 MethodLookupUtils.lookupMethod(
146 target.getClass(),
147 name,
148 parameters);
149 if (method != null) {
150 return new MethodFunction(method);
151 }
152
153 if (target instanceof Collection) {
154 Iterator iter = ((Collection) target).iterator();
155 if (iter.hasNext()) {
156 target = iter.next();
157 if (target instanceof Pointer) {
158 target = ((Pointer) target).getValue();
159 }
160 }
161 else {
162 target = null;
163 }
164 }
165 }
166 if (target != null) {
167 Method method =
168 MethodLookupUtils.lookupMethod(
169 target.getClass(),
170 name,
171 parameters);
172 if (method != null) {
173 return new MethodFunction(method);
174 }
175 }
176 }
177
178 String fullName = classPrefix + name;
179 int inx = fullName.lastIndexOf('.');
180 if (inx == -1) {
181 return null;
182 }
183
184 String className = fullName.substring(0, inx);
185 String methodName = fullName.substring(inx + 1);
186
187 Class functionClass;
188 try {
189 functionClass = ClassLoaderUtil.getClass(className, true);
190 }
191 catch (ClassNotFoundException ex) {
192 throw new JXPathException(
193 "Cannot invoke extension function "
194 + (namespace != null ? namespace + ":" + name : name),
195 ex);
196 }
197
198 if (methodName.equals("new")) {
199 Constructor constructor =
200 MethodLookupUtils.lookupConstructor(functionClass, parameters);
201 if (constructor != null) {
202 return new ConstructorFunction(constructor);
203 }
204 }
205 else {
206 Method method =
207 MethodLookupUtils.lookupStaticMethod(
208 functionClass,
209 methodName,
210 parameters);
211 if (method != null) {
212 return new MethodFunction(method);
213 }
214 }
215 return null;
216 }
217 }