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