View Javadoc

1   /*
2    * $Header: /home/projects/jaxen/scm/jaxen/src/java/main/org/jaxen/XPathFunctionContext.java,v 1.29 2006/11/08 13:59:38 elharo Exp $
3    * $Revision: 1.29 $
4    * $Date: 2006/11/08 13:59:38 $
5    *
6    * ====================================================================
7    *
8    * Copyright 2000-2002 bob mcwhirter & James Strachan.
9    * All rights reserved.
10   *
11   * Redistribution and use in source and binary forms, with or without
12   * modification, are permitted provided that the following conditions are
13   * met:
14   * 
15   *   * Redistributions of source code must retain the above copyright
16   *     notice, this list of conditions and the following disclaimer.
17   * 
18   *   * Redistributions in binary form must reproduce the above copyright
19   *     notice, this list of conditions and the following disclaimer in the
20   *     documentation and/or other materials provided with the distribution.
21   * 
22   *   * Neither the name of the Jaxen Project nor the names of its
23   *     contributors may be used to endorse or promote products derived 
24   *     from this software without specific prior written permission.
25   * 
26   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
27   * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28   * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
29   * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
30   * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31   * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32   * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33   * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34   * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35   * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36   * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37   *
38   * ====================================================================
39   * This software consists of voluntary contributions made by many 
40   * individuals on behalf of the Jaxen Project and was originally 
41   * created by bob mcwhirter <bob@werken.com> and 
42   * James Strachan <jstrachan@apache.org>.  For more information on the 
43   * Jaxen Project, please see <http://www.jaxen.org/>.
44   * 
45   * $Id: XPathFunctionContext.java,v 1.29 2006/11/08 13:59:38 elharo Exp $
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,  // namespace URI
158                           "boolean",
159                           new BooleanFunction() );
160 
161         registerFunction( null,  // namespace URI
162                           "ceiling",
163                           new CeilingFunction() );
164 
165         registerFunction( null,  // namespace URI
166                           "concat",
167                           new ConcatFunction() );
168 
169         registerFunction( null,  // namespace URI
170                           "contains",
171                           new ContainsFunction() );
172         
173         registerFunction( null,  // namespace URI
174                           "count",
175                           new CountFunction() );
176 
177         registerFunction( null,  // namespace URI
178                           "false",
179                           new FalseFunction() );
180 
181         registerFunction( null,  // namespace URI
182                           "floor",
183                           new FloorFunction() );
184 
185         registerFunction( null,  // namespace URI
186                           "id",
187                           new IdFunction() );
188 
189         registerFunction( null,  // namespace URI
190                           "lang",
191                           new LangFunction() );
192 
193         registerFunction( null,  // namespace URI
194                           "last",
195                           new LastFunction() );
196 
197         registerFunction( null,  // namespace URI
198                           "local-name",
199                           new LocalNameFunction() );
200 
201         registerFunction( null,  // namespace URI
202                           "name",
203                           new NameFunction() );
204 
205         registerFunction( null,  // namespace URI
206                           "namespace-uri",
207                           new NamespaceUriFunction() );
208 
209         registerFunction( null,  // namespace URI
210                           "normalize-space",
211                           new NormalizeSpaceFunction() );
212 
213         registerFunction( null,  // namespace URI
214                           "not",
215                           new NotFunction() );
216 
217         registerFunction( null,  // namespace URI
218                           "number",
219                           new NumberFunction() );
220 
221         registerFunction( null,  // namespace URI
222                           "position",
223                           new PositionFunction() );
224 
225         registerFunction( null,  // namespace URI
226                           "round",
227                           new RoundFunction() );
228 
229         registerFunction( null,  // namespace URI
230                           "starts-with",
231                           new StartsWithFunction() );
232 
233         registerFunction( null,  // namespace URI
234                           "string",
235                           new StringFunction() );
236 
237         registerFunction( null,  // namespace URI
238                           "string-length",
239                           new StringLengthFunction() );
240 
241         registerFunction( null,  // namespace URI
242                           "substring-after",
243                           new SubstringAfterFunction() );
244 
245         registerFunction( null,  // namespace URI
246                           "substring-before",
247                           new SubstringBeforeFunction() );
248 
249         registerFunction( null,  // namespace URI
250                           "substring",
251                           new SubstringFunction() );
252 
253         registerFunction( null,  // namespace URI
254                           "sum",
255                           new SumFunction() );
256 
257         registerFunction( null,  // namespace URI
258                           "true",
259                           new TrueFunction() );
260         
261         registerFunction( null,  // namespace URI
262                           "translate",
263                           new TranslateFunction() );
264     }
265 
266     private void registerXSLTFunctions() {
267 
268         // extension functions defined in XSLT
269         registerFunction( null,  // namespace URI
270                           "document",
271                           new DocumentFunction() );
272     }
273 
274     private void registerExtensionFunctions() {
275         // extension functions should go into a namespace, but which one?
276         // for now, keep them in default namespace to not break any code
277 
278         registerFunction( null,  // namespace URI
279                           "evaluate",
280                           new EvaluateFunction() );
281         
282         registerFunction( null,  // namespace URI
283                           "lower-case",
284                           new LowerFunction() );
285         
286         registerFunction( null,  // namespace URI
287                           "upper-case",
288                           new UpperFunction() );
289         
290         registerFunction( null,  // namespace URI
291                           "ends-with",
292                           new EndsWithFunction() );
293     }
294 
295     
296 }