KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xpath > compiler > FunctionTable


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 /*
17  * $Id: FunctionTable.java,v 1.6 2004/02/17 04:32:48 minchau Exp $
18  */

19 package org.apache.xpath.compiler;
20
21 import org.apache.xpath.Expression;
22 import org.apache.xpath.functions.Function;
23
24 /**
25  * The function table for XPath.
26  */

27 public class FunctionTable
28 {
29
30   /** The 'current()' id. */
31   public static final int FUNC_CURRENT = 0;
32
33   /** The 'last()' id. */
34   public static final int FUNC_LAST = 1;
35
36   /** The 'position()' id. */
37   public static final int FUNC_POSITION = 2;
38
39   /** The 'count()' id. */
40   public static final int FUNC_COUNT = 3;
41
42   /** The 'id()' id. */
43   public static final int FUNC_ID = 4;
44
45   /** The 'key()' id (XSLT). */
46   public static final int FUNC_KEY = 5;
47
48   /** The 'local-name()' id. */
49   public static final int FUNC_LOCAL_PART = 7;
50
51   /** The 'namespace-uri()' id. */
52   public static final int FUNC_NAMESPACE = 8;
53
54   /** The 'name()' id. */
55   public static final int FUNC_QNAME = 9;
56
57   /** The 'generate-id()' id. */
58   public static final int FUNC_GENERATE_ID = 10;
59
60   /** The 'not()' id. */
61   public static final int FUNC_NOT = 11;
62
63   /** The 'true()' id. */
64   public static final int FUNC_TRUE = 12;
65
66   /** The 'false()' id. */
67   public static final int FUNC_FALSE = 13;
68
69   /** The 'boolean()' id. */
70   public static final int FUNC_BOOLEAN = 14;
71
72   /** The 'number()' id. */
73   public static final int FUNC_NUMBER = 15;
74
75   /** The 'floor()' id. */
76   public static final int FUNC_FLOOR = 16;
77
78   /** The 'ceiling()' id. */
79   public static final int FUNC_CEILING = 17;
80
81   /** The 'round()' id. */
82   public static final int FUNC_ROUND = 18;
83
84   /** The 'sum()' id. */
85   public static final int FUNC_SUM = 19;
86
87   /** The 'string()' id. */
88   public static final int FUNC_STRING = 20;
89
90   /** The 'starts-with()' id. */
91   public static final int FUNC_STARTS_WITH = 21;
92
93   /** The 'contains()' id. */
94   public static final int FUNC_CONTAINS = 22;
95
96   /** The 'substring-before()' id. */
97   public static final int FUNC_SUBSTRING_BEFORE = 23;
98
99   /** The 'substring-after()' id. */
100   public static final int FUNC_SUBSTRING_AFTER = 24;
101
102   /** The 'normalize-space()' id. */
103   public static final int FUNC_NORMALIZE_SPACE = 25;
104
105   /** The 'translate()' id. */
106   public static final int FUNC_TRANSLATE = 26;
107
108   /** The 'concat()' id. */
109   public static final int FUNC_CONCAT = 27;
110
111   /** The 'substring()' id. */
112   public static final int FUNC_SUBSTRING = 29;
113
114   /** The 'string-length()' id. */
115   public static final int FUNC_STRING_LENGTH = 30;
116
117   /** The 'system-property()' id. */
118   public static final int FUNC_SYSTEM_PROPERTY = 31;
119
120   /** The 'lang()' id. */
121   public static final int FUNC_LANG = 32;
122
123   /** The 'function-available()' id (XSLT). */
124   public static final int FUNC_EXT_FUNCTION_AVAILABLE = 33;
125
126   /** The 'element-available()' id (XSLT). */
127   public static final int FUNC_EXT_ELEM_AVAILABLE = 34;
128
129   /** The 'unparsed-entity-uri()' id (XSLT). */
130   public static final int FUNC_UNPARSED_ENTITY_URI = 36;
131
132   // Proprietary
133

134   /** The 'document-location()' id (Proprietary). */
135   public static final int FUNC_DOCLOCATION = 35;
136
137   /**
138    * The function table.
139    */

140   public static FuncLoader m_functions[];
141
142   /**
143    * Number of built in functions. Be sure to update this as
144    * built-in functions are added.
145    */

146   private static final int NUM_BUILT_IN_FUNCS = 37;
147
148   /**
149    * Number of built-in functions that may be added.
150    */

151   private static final int NUM_ALLOWABLE_ADDINS = 30;
152
153   /**
154    * The index to the next free function index.
155    */

156   static int m_funcNextFreeIndex = NUM_BUILT_IN_FUNCS;
157
158   static
159   {
160     m_functions = new FuncLoader[NUM_BUILT_IN_FUNCS + NUM_ALLOWABLE_ADDINS];
161     m_functions[FUNC_CURRENT] = new FuncLoader("FuncCurrent", FUNC_CURRENT);
162     m_functions[FUNC_LAST] = new FuncLoader("FuncLast", FUNC_LAST);
163     m_functions[FUNC_POSITION] = new FuncLoader("FuncPosition",
164                                                 FUNC_POSITION);
165     m_functions[FUNC_COUNT] = new FuncLoader("FuncCount", FUNC_COUNT);
166     m_functions[FUNC_ID] = new FuncLoader("FuncId", FUNC_ID);
167     m_functions[FUNC_KEY] =
168       new FuncLoader("org.apache.xalan.templates.FuncKey", FUNC_KEY);
169
170     // m_functions[FUNC_DOC] = new FuncDoc();
171
m_functions[FUNC_LOCAL_PART] = new FuncLoader("FuncLocalPart",
172             FUNC_LOCAL_PART);
173     m_functions[FUNC_NAMESPACE] = new FuncLoader("FuncNamespace",
174             FUNC_NAMESPACE);
175     m_functions[FUNC_QNAME] = new FuncLoader("FuncQname", FUNC_QNAME);
176     m_functions[FUNC_GENERATE_ID] = new FuncLoader("FuncGenerateId",
177             FUNC_GENERATE_ID);
178     m_functions[FUNC_NOT] = new FuncLoader("FuncNot", FUNC_NOT);
179     m_functions[FUNC_TRUE] = new FuncLoader("FuncTrue", FUNC_TRUE);
180     m_functions[FUNC_FALSE] = new FuncLoader("FuncFalse", FUNC_FALSE);
181     m_functions[FUNC_BOOLEAN] = new FuncLoader("FuncBoolean", FUNC_BOOLEAN);
182     m_functions[FUNC_LANG] = new FuncLoader("FuncLang", FUNC_LANG);
183     m_functions[FUNC_NUMBER] = new FuncLoader("FuncNumber", FUNC_NUMBER);
184     m_functions[FUNC_FLOOR] = new FuncLoader("FuncFloor", FUNC_FLOOR);
185     m_functions[FUNC_CEILING] = new FuncLoader("FuncCeiling", FUNC_CEILING);
186     m_functions[FUNC_ROUND] = new FuncLoader("FuncRound", FUNC_ROUND);
187     m_functions[FUNC_SUM] = new FuncLoader("FuncSum", FUNC_SUM);
188     m_functions[FUNC_STRING] = new FuncLoader("FuncString", FUNC_STRING);
189     m_functions[FUNC_STARTS_WITH] = new FuncLoader("FuncStartsWith",
190             FUNC_STARTS_WITH);
191     m_functions[FUNC_CONTAINS] = new FuncLoader("FuncContains",
192                                                 FUNC_CONTAINS);
193     m_functions[FUNC_SUBSTRING_BEFORE] = new FuncLoader("FuncSubstringBefore",
194             FUNC_SUBSTRING_BEFORE);
195     m_functions[FUNC_SUBSTRING_AFTER] = new FuncLoader("FuncSubstringAfter",
196             FUNC_SUBSTRING_AFTER);
197     m_functions[FUNC_NORMALIZE_SPACE] = new FuncLoader("FuncNormalizeSpace",
198             FUNC_NORMALIZE_SPACE);
199     m_functions[FUNC_TRANSLATE] = new FuncLoader("FuncTranslate",
200             FUNC_TRANSLATE);
201     m_functions[FUNC_CONCAT] = new FuncLoader("FuncConcat", FUNC_CONCAT);
202
203     //m_functions[FUNC_FORMAT_NUMBER] = new FuncFormatNumber();
204
m_functions[FUNC_SYSTEM_PROPERTY] = new FuncLoader("FuncSystemProperty",
205             FUNC_SYSTEM_PROPERTY);
206     m_functions[FUNC_EXT_FUNCTION_AVAILABLE] =
207       new FuncLoader("FuncExtFunctionAvailable", FUNC_EXT_FUNCTION_AVAILABLE);
208     m_functions[FUNC_EXT_ELEM_AVAILABLE] =
209       new FuncLoader("FuncExtElementAvailable", FUNC_EXT_ELEM_AVAILABLE);
210     m_functions[FUNC_SUBSTRING] = new FuncLoader("FuncSubstring",
211             FUNC_SUBSTRING);
212     m_functions[FUNC_STRING_LENGTH] = new FuncLoader("FuncStringLength",
213             FUNC_STRING_LENGTH);
214     m_functions[FUNC_DOCLOCATION] = new FuncLoader("FuncDoclocation",
215             FUNC_DOCLOCATION);
216     m_functions[FUNC_UNPARSED_ENTITY_URI] =
217       new FuncLoader("FuncUnparsedEntityURI", FUNC_UNPARSED_ENTITY_URI);
218   }
219
220   /**
221    * Obtain a new Function object from a function ID.
222    *
223    * @param which The function ID, which may correspond to one of the FUNC_XXX
224    * values found in {@link org.apache.xpath.compiler.FunctionTable}, but may
225    * be a value installed by an external module.
226    *
227    * @return a a new Function instance.
228    *
229    * @throws javax.xml.transform.TransformerException if ClassNotFoundException,
230    * IllegalAccessException, or InstantiationException is thrown.
231    */

232   public static Function getFunction(int which)
233           throws javax.xml.transform.TransformerException JavaDoc
234   {
235     return m_functions[which].getFunction();
236   }
237
238   /**
239    * Install a built-in function.
240    * @param name The unqualified name of the function.
241    * @param func A Implementation of an XPath Function object.
242    * @return the position of the function in the internal index.
243    */

244   public static int installFunction(String JavaDoc name, Expression func)
245   {
246
247     int funcIndex;
248     Object JavaDoc funcIndexObj = Keywords.m_functions.get(name);
249
250     if (null != funcIndexObj)
251     {
252       funcIndex = ((Integer JavaDoc) funcIndexObj).intValue();
253     }
254     else
255     {
256       funcIndex = m_funcNextFreeIndex;
257
258       m_funcNextFreeIndex++;
259
260       Keywords.m_functions.put(name, new Integer JavaDoc(funcIndex));
261     }
262
263     FuncLoader loader = new FuncLoader(func.getClass().getName(), funcIndex);
264
265     m_functions[funcIndex] = loader;
266
267     return funcIndex;
268   }
269
270   /**
271    * Install a function loader at a specific index.
272    * @param func A Implementation of an XPath Function object.
273    * @param which The function ID, which may correspond to one of the FUNC_XXX
274    * values found in {@link org.apache.xpath.compiler.FunctionTable}, but may
275    * be a value installed by an external module.
276    * @return the position of the function in the internal index.
277    */

278   public static void installFunction(Expression func, int funcIndex)
279   {
280
281     FuncLoader loader = new FuncLoader(func.getClass().getName(), funcIndex);
282
283     m_functions[funcIndex] = loader;
284   }
285 }
286
Popular Tags