1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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 package org.jaxen;
64
65 import org.jaxen.function.BooleanFunction;
66 import org.jaxen.function.CeilingFunction;
67 import org.jaxen.function.ConcatFunction;
68 import org.jaxen.function.ContainsFunction;
69 import org.jaxen.function.CountFunction;
70 import org.jaxen.function.FalseFunction;
71 import org.jaxen.function.FloorFunction;
72 import org.jaxen.function.IdFunction;
73 import org.jaxen.function.LangFunction;
74 import org.jaxen.function.LastFunction;
75 import org.jaxen.function.LocalNameFunction;
76 import org.jaxen.function.NameFunction;
77 import org.jaxen.function.NamespaceUriFunction;
78 import org.jaxen.function.NormalizeSpaceFunction;
79 import org.jaxen.function.NotFunction;
80 import org.jaxen.function.NumberFunction;
81 import org.jaxen.function.PositionFunction;
82 import org.jaxen.function.RoundFunction;
83 import org.jaxen.function.StartsWithFunction;
84 import org.jaxen.function.StringFunction;
85 import org.jaxen.function.StringLengthFunction;
86 import org.jaxen.function.SubstringAfterFunction;
87 import org.jaxen.function.SubstringBeforeFunction;
88 import org.jaxen.function.SubstringFunction;
89 import org.jaxen.function.SumFunction;
90 import org.jaxen.function.TranslateFunction;
91 import org.jaxen.function.TrueFunction;
92 import org.jaxen.function.ext.EndsWithFunction;
93 import org.jaxen.function.ext.EvaluateFunction;
94 import org.jaxen.function.ext.LowerFunction;
95 import org.jaxen.function.ext.MatrixConcatFunction;
96 import org.jaxen.function.ext.UpperFunction;
97 import org.jaxen.function.xslt.DocumentFunction;
98
99 /*** A <code>FunctionContext</code> implementing the core XPath
100 * function library, plus Jaxen extensions.
101 *
102 * <p>
103 * The core XPath function library is provided through this
104 * implementation of <code>FunctionContext</code>. Additionally,
105 * extension functions have been provided, as enumerated below.
106 * </p>
107 *
108 * <p>
109 * This class is re-entrant and thread-safe. If using the
110 * default instance, it is inadvisable to call
111 * {@link #registerFunction(String, String, Function)}
112 * as that will extend the global function context, affecting other
113 * users. But that's your call, really, now isn't
114 * it? That may be what you really want to do.
115 * </p>
116 *
117 * <p>
118 * Extension functions:
119 * </p>
120 *
121 * <ul>
122 * <li>matrix-concat(..)</li>
123 * <li>evaluate(..)</li>
124 * <li>upper-case(..)</li>
125 * <li>lower-case(..)</li>
126 * <li>ends-with(..)</li>
127 * </ul>
128 *
129 * @see FunctionContext
130 * @see org.jaxen.function
131 * @see org.jaxen.function.xslt
132 * @see org.jaxen.function.ext
133 *
134 * @author <a href="mailto:bob@werken.com">bob mcwhirter</a>
135 */
136 public class XPathFunctionContext extends SimpleFunctionContext
137 {
138 private static XPathFunctionContext instance = new XPathFunctionContext();
139
140 /*** Retrieve the default function context
141 *
142 * @return the default function context
143 */
144 public static FunctionContext getInstance()
145 {
146 return instance;
147 }
148
149 /*** Create a new XPath function context.
150 * All core XPath and Jaxen extension functions are registered.
151 */
152 public XPathFunctionContext()
153 {
154 this(true);
155 }
156
157 /*** Create a new XPath function context.
158 * All core XPath functions are registered.
159 *
160 * @param includeExtensionFunctions if true extension functions are included;
161 * if false, they aren't.
162 */
163 public XPathFunctionContext(boolean includeExtensionFunctions)
164 {
165 registerXPathFunctions();
166 if (includeExtensionFunctions) {
167 registerXSLTFunctions();
168 registerExtensionFunctions();
169 }
170 }
171
172 private void registerXPathFunctions() {
173
174 registerFunction( null,
175 "boolean",
176 new BooleanFunction() );
177
178 registerFunction( null,
179 "ceiling",
180 new CeilingFunction() );
181
182 registerFunction( null,
183 "concat",
184 new ConcatFunction() );
185
186 registerFunction( null,
187 "contains",
188 new ContainsFunction() );
189
190 registerFunction( null,
191 "count",
192 new CountFunction() );
193
194 registerFunction( null,
195 "false",
196 new FalseFunction() );
197
198 registerFunction( null,
199 "floor",
200 new FloorFunction() );
201
202 registerFunction( null,
203 "id",
204 new IdFunction() );
205
206 registerFunction( null,
207 "lang",
208 new LangFunction() );
209
210 registerFunction( null,
211 "last",
212 new LastFunction() );
213
214 registerFunction( null,
215 "local-name",
216 new LocalNameFunction() );
217
218 registerFunction( null,
219 "name",
220 new NameFunction() );
221
222 registerFunction( null,
223 "namespace-uri",
224 new NamespaceUriFunction() );
225
226 registerFunction( null,
227 "normalize-space",
228 new NormalizeSpaceFunction() );
229
230 registerFunction( null,
231 "not",
232 new NotFunction() );
233
234 registerFunction( null,
235 "number",
236 new NumberFunction() );
237
238 registerFunction( null,
239 "position",
240 new PositionFunction() );
241
242 registerFunction( null,
243 "round",
244 new RoundFunction() );
245
246 registerFunction( null,
247 "starts-with",
248 new StartsWithFunction() );
249
250 registerFunction( null,
251 "string",
252 new StringFunction() );
253
254 registerFunction( null,
255 "string-length",
256 new StringLengthFunction() );
257
258 registerFunction( null,
259 "substring-after",
260 new SubstringAfterFunction() );
261
262 registerFunction( null,
263 "substring-before",
264 new SubstringBeforeFunction() );
265
266 registerFunction( null,
267 "substring",
268 new SubstringFunction() );
269
270 registerFunction( null,
271 "sum",
272 new SumFunction() );
273
274 registerFunction( null,
275 "true",
276 new TrueFunction() );
277
278 registerFunction( null,
279 "translate",
280 new TranslateFunction() );
281 }
282
283 private void registerXSLTFunctions() {
284
285
286 registerFunction( null,
287 "document",
288 new DocumentFunction() );
289 }
290
291 private void registerExtensionFunctions() {
292
293
294
295 registerFunction( null,
296 "matrix-concat",
297 new MatrixConcatFunction() );
298
299 registerFunction( null,
300 "evaluate",
301 new EvaluateFunction() );
302
303 registerFunction( null,
304 "lower-case",
305 new LowerFunction() );
306
307 registerFunction( null,
308 "upper-case",
309 new UpperFunction() );
310
311 registerFunction( null,
312 "ends-with",
313 new EndsWithFunction() );
314 }
315
316
317 }