View Javadoc

1   /*
2    * $Header: /home/projects/jaxen/scm/jaxen/src/java/main/org/jaxen/XPathFunctionContext.java,v 1.22 2005/03/23 13:13:20 elharo Exp $
3    * $Revision: 1.22 $
4    * $Date: 2005/03/23 13:13:20 $
5    *
6    * ====================================================================
7    *
8    * Copyright (C) 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
13   * are met:
14   * 
15   * 1. Redistributions of source code must retain the above copyright
16   *    notice, this list of conditions, and the following disclaimer.
17   *
18   * 2. Redistributions in binary form must reproduce the above copyright
19   *    notice, this list of conditions, and the disclaimer that follows 
20   *    these conditions in the documentation and/or other materials 
21   *    provided with the distribution.
22   *
23   * 3. The name "Jaxen" must not be used to endorse or promote products
24   *    derived from this software without prior written permission.  For
25   *    written permission, please contact license@jaxen.org.
26   * 
27   * 4. Products derived from this software may not be called "Jaxen", nor
28   *    may "Jaxen" appear in their name, without prior written permission
29   *    from the Jaxen Project Management (pm@jaxen.org).
30   * 
31   * In addition, we request (but do not require) that you include in the 
32   * end-user documentation provided with the redistribution and/or in the 
33   * software itself an acknowledgement equivalent to the following:
34   *     "This product includes software developed by the
35   *      Jaxen Project (http://www.jaxen.org/)."
36   * Alternatively, the acknowledgment may be graphical using the logos 
37   * available at http://www.jaxen.org/
38   *
39   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
40   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
41   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
42   * DISCLAIMED.  IN NO EVENT SHALL THE Jaxen AUTHORS OR THE PROJECT
43   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
45   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
46   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
47   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
48   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
49   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50   * SUCH DAMAGE.
51   *
52   * ====================================================================
53   * This software consists of voluntary contributions made by many 
54   * individuals on behalf of the Jaxen Project and was originally 
55   * created by bob mcwhirter <bob@werken.com> and 
56   * James Strachan <jstrachan@apache.org>.  For more information on the 
57   * Jaxen Project, please see <http://www.jaxen.org/>.
58   * 
59   * $Id: XPathFunctionContext.java,v 1.22 2005/03/23 13:13:20 elharo Exp $
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, with 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 implements a <i>Singleton</i> pattern (see {@link #getInstance}),
110  *  as it is perfectly re-entrant and thread-safe.  If using the
111  *  singleton, it is inadvisable to call {@link #registerFunction(String, String, Function)}
112  *  as that will extend the global function context, affecting other
113  *  users of the singleton.  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  *
120  *  <ul>
121  *     <li>matrix-concat(..)</li>
122  *     <li>evaluate(..)</li>
123  *  </ul>
124  *
125  *  @see FunctionContext
126  *
127  *  @author <a href="mailto:bob@werken.com">bob mcwhirter</a>
128  */
129 public class XPathFunctionContext extends SimpleFunctionContext
130 {
131    /*** Singleton implementation.
132     */
133     private static class Singleton
134     {
135         /*** Singleton instance.
136          */
137         private static XPathFunctionContext instance = new XPathFunctionContext();
138     }
139 
140     /*** Retrieve the singleton instance.
141      *
142      *  @return the singleton instance
143      */
144     public static FunctionContext getInstance()
145     {
146         return Singleton.instance;
147     }
148 
149     /*** Construct.
150      *
151      *  <p>
152      *  Construct with all core XPath functions registered.
153      *  </p>
154      */
155     public XPathFunctionContext()
156     {
157         // XXX could this be a HotSpot????
158         registerFunction( null,  // namespace URI
159                           "boolean",
160                           new BooleanFunction() );
161 
162         registerFunction( null,  // namespace URI
163                           "ceiling",
164                           new CeilingFunction() );
165 
166         registerFunction( null,  // namespace URI
167                           "concat",
168                           new ConcatFunction() );
169 
170         registerFunction( null,  // namespace URI
171                           "contains",
172                           new ContainsFunction() );
173         
174         registerFunction( null,  // namespace URI
175                           "count",
176                           new CountFunction() );
177 
178         registerFunction( null,  // namespace URI
179                           "document",
180                           new DocumentFunction() );
181 
182         registerFunction( null,  // namespace URI
183                           "false",
184                           new FalseFunction() );
185 
186         registerFunction( null,  // namespace URI
187                           "floor",
188                           new FloorFunction() );
189 
190         registerFunction( null,  // namespace URI
191                           "id",
192                           new IdFunction() );
193 
194         registerFunction( null,  // namespace URI
195                           "lang",
196                           new LangFunction() );
197 
198         registerFunction( null,  // namespace URI
199                           "last",
200                           new LastFunction() );
201 
202         registerFunction( null,  // namespace URI
203                           "local-name",
204                           new LocalNameFunction() );
205 
206         registerFunction( null,  // namespace URI
207                           "name",
208                           new NameFunction() );
209 
210         registerFunction( null,  // namespace URI
211                           "namespace-uri",
212                           new NamespaceUriFunction() );
213 
214         registerFunction( null,  // namespace URI
215                           "normalize-space",
216                           new NormalizeSpaceFunction() );
217 
218         registerFunction( null,  // namespace URI
219                           "not",
220                           new NotFunction() );
221 
222         registerFunction( null,  // namespace URI
223                           "number",
224                           new NumberFunction() );
225 
226         registerFunction( null,  // namespace URI
227                           "position",
228                           new PositionFunction() );
229 
230         registerFunction( null,  // namespace URI
231                           "round",
232                           new RoundFunction() );
233 
234         registerFunction( null,  // namespace URI
235                           "starts-with",
236                           new StartsWithFunction() );
237 
238         registerFunction( null,  // namespace URI
239                           "string",
240                           new StringFunction() );
241 
242         registerFunction( null,  // namespace URI
243                           "string-length",
244                           new StringLengthFunction() );
245 
246         registerFunction( null,  // namespace URI
247                           "substring-after",
248                           new SubstringAfterFunction() );
249 
250         registerFunction( null,  // namespace URI
251                           "substring-before",
252                           new SubstringBeforeFunction() );
253 
254         registerFunction( null,  // namespace URI
255                           "substring",
256                           new SubstringFunction() );
257 
258         registerFunction( null,  // namespace URI
259                           "sum",
260                           new SumFunction() );
261 
262         registerFunction( null,  // namespace URI
263                           "true",
264                           new TrueFunction() );
265         
266         registerFunction( null,  // namespace URI
267                           "translate",
268                           new TranslateFunction() );
269         
270 
271         // register extension functions
272         // extension functions should go into a namespace, but which one?
273         // for now, keep them in default namespace to not break any code
274 
275         registerFunction( null,  // namespace URI
276                           "matrix-concat",
277                           new MatrixConcatFunction() );
278 
279         registerFunction( null,  // namespace URI
280                           "evaluate",
281                           new EvaluateFunction() );
282         
283         registerFunction( null,  // namespace URI
284                           "lower-case",
285                           new LowerFunction() );
286         
287         registerFunction( null,  // namespace URI
288                           "upper-case",
289                           new UpperFunction() );
290         
291         registerFunction( null,  // namespace URI
292                           "ends-with",
293                           new EndsWithFunction() );
294         
295     }
296 }