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 com.sun.org.apache.xpath.internal.compiler;
  20. import com.sun.org.apache.xpath.internal.Expression;
  21. import com.sun.org.apache.xpath.internal.functions.Function;
  22. /**
  23. * The function table for XPath.
  24. */
  25. public class FunctionTable
  26. {
  27. /** The 'current()' id. */
  28. public static final int FUNC_CURRENT = 0;
  29. /** The 'last()' id. */
  30. public static final int FUNC_LAST = 1;
  31. /** The 'position()' id. */
  32. public static final int FUNC_POSITION = 2;
  33. /** The 'count()' id. */
  34. public static final int FUNC_COUNT = 3;
  35. /** The 'id()' id. */
  36. public static final int FUNC_ID = 4;
  37. /** The 'key()' id (XSLT). */
  38. public static final int FUNC_KEY = 5;
  39. /** The 'local-name()' id. */
  40. public static final int FUNC_LOCAL_PART = 7;
  41. /** The 'namespace-uri()' id. */
  42. public static final int FUNC_NAMESPACE = 8;
  43. /** The 'name()' id. */
  44. public static final int FUNC_QNAME = 9;
  45. /** The 'generate-id()' id. */
  46. public static final int FUNC_GENERATE_ID = 10;
  47. /** The 'not()' id. */
  48. public static final int FUNC_NOT = 11;
  49. /** The 'true()' id. */
  50. public static final int FUNC_TRUE = 12;
  51. /** The 'false()' id. */
  52. public static final int FUNC_FALSE = 13;
  53. /** The 'boolean()' id. */
  54. public static final int FUNC_BOOLEAN = 14;
  55. /** The 'number()' id. */
  56. public static final int FUNC_NUMBER = 15;
  57. /** The 'floor()' id. */
  58. public static final int FUNC_FLOOR = 16;
  59. /** The 'ceiling()' id. */
  60. public static final int FUNC_CEILING = 17;
  61. /** The 'round()' id. */
  62. public static final int FUNC_ROUND = 18;
  63. /** The 'sum()' id. */
  64. public static final int FUNC_SUM = 19;
  65. /** The 'string()' id. */
  66. public static final int FUNC_STRING = 20;
  67. /** The 'starts-with()' id. */
  68. public static final int FUNC_STARTS_WITH = 21;
  69. /** The 'contains()' id. */
  70. public static final int FUNC_CONTAINS = 22;
  71. /** The 'substring-before()' id. */
  72. public static final int FUNC_SUBSTRING_BEFORE = 23;
  73. /** The 'substring-after()' id. */
  74. public static final int FUNC_SUBSTRING_AFTER = 24;
  75. /** The 'normalize-space()' id. */
  76. public static final int FUNC_NORMALIZE_SPACE = 25;
  77. /** The 'translate()' id. */
  78. public static final int FUNC_TRANSLATE = 26;
  79. /** The 'concat()' id. */
  80. public static final int FUNC_CONCAT = 27;
  81. /** The 'substring()' id. */
  82. public static final int FUNC_SUBSTRING = 29;
  83. /** The 'string-length()' id. */
  84. public static final int FUNC_STRING_LENGTH = 30;
  85. /** The 'system-property()' id. */
  86. public static final int FUNC_SYSTEM_PROPERTY = 31;
  87. /** The 'lang()' id. */
  88. public static final int FUNC_LANG = 32;
  89. /** The 'function-available()' id (XSLT). */
  90. public static final int FUNC_EXT_FUNCTION_AVAILABLE = 33;
  91. /** The 'element-available()' id (XSLT). */
  92. public static final int FUNC_EXT_ELEM_AVAILABLE = 34;
  93. /** The 'unparsed-entity-uri()' id (XSLT). */
  94. public static final int FUNC_UNPARSED_ENTITY_URI = 36;
  95. // Proprietary
  96. /** The 'document-location()' id (Proprietary). */
  97. public static final int FUNC_DOCLOCATION = 35;
  98. /**
  99. * The function table.
  100. */
  101. private static FuncLoader m_functions[];
  102. /**
  103. * Number of built in functions. Be sure to update this as
  104. * built-in functions are added.
  105. */
  106. private static final int NUM_BUILT_IN_FUNCS = 37;
  107. /**
  108. * Number of built-in functions that may be added.
  109. */
  110. private static final int NUM_ALLOWABLE_ADDINS = 30;
  111. /**
  112. * The index to the next free function index.
  113. */
  114. static int m_funcNextFreeIndex = NUM_BUILT_IN_FUNCS;
  115. static
  116. {
  117. m_functions = new FuncLoader[NUM_BUILT_IN_FUNCS + NUM_ALLOWABLE_ADDINS];
  118. m_functions[FUNC_CURRENT] = new FuncLoader("FuncCurrent", FUNC_CURRENT);
  119. m_functions[FUNC_LAST] = new FuncLoader("FuncLast", FUNC_LAST);
  120. m_functions[FUNC_POSITION] = new FuncLoader("FuncPosition",
  121. FUNC_POSITION);
  122. m_functions[FUNC_COUNT] = new FuncLoader("FuncCount", FUNC_COUNT);
  123. m_functions[FUNC_ID] = new FuncLoader("FuncId", FUNC_ID);
  124. m_functions[FUNC_KEY] =
  125. new FuncLoader("com.sun.org.apache.xalan.internal.templates.FuncKey", FUNC_KEY);
  126. // m_functions[FUNC_DOC] = new FuncDoc();
  127. m_functions[FUNC_LOCAL_PART] = new FuncLoader("FuncLocalPart",
  128. FUNC_LOCAL_PART);
  129. m_functions[FUNC_NAMESPACE] = new FuncLoader("FuncNamespace",
  130. FUNC_NAMESPACE);
  131. m_functions[FUNC_QNAME] = new FuncLoader("FuncQname", FUNC_QNAME);
  132. m_functions[FUNC_GENERATE_ID] = new FuncLoader("FuncGenerateId",
  133. FUNC_GENERATE_ID);
  134. m_functions[FUNC_NOT] = new FuncLoader("FuncNot", FUNC_NOT);
  135. m_functions[FUNC_TRUE] = new FuncLoader("FuncTrue", FUNC_TRUE);
  136. m_functions[FUNC_FALSE] = new FuncLoader("FuncFalse", FUNC_FALSE);
  137. m_functions[FUNC_BOOLEAN] = new FuncLoader("FuncBoolean", FUNC_BOOLEAN);
  138. m_functions[FUNC_LANG] = new FuncLoader("FuncLang", FUNC_LANG);
  139. m_functions[FUNC_NUMBER] = new FuncLoader("FuncNumber", FUNC_NUMBER);
  140. m_functions[FUNC_FLOOR] = new FuncLoader("FuncFloor", FUNC_FLOOR);
  141. m_functions[FUNC_CEILING] = new FuncLoader("FuncCeiling", FUNC_CEILING);
  142. m_functions[FUNC_ROUND] = new FuncLoader("FuncRound", FUNC_ROUND);
  143. m_functions[FUNC_SUM] = new FuncLoader("FuncSum", FUNC_SUM);
  144. m_functions[FUNC_STRING] = new FuncLoader("FuncString", FUNC_STRING);
  145. m_functions[FUNC_STARTS_WITH] = new FuncLoader("FuncStartsWith",
  146. FUNC_STARTS_WITH);
  147. m_functions[FUNC_CONTAINS] = new FuncLoader("FuncContains",
  148. FUNC_CONTAINS);
  149. m_functions[FUNC_SUBSTRING_BEFORE] = new FuncLoader("FuncSubstringBefore",
  150. FUNC_SUBSTRING_BEFORE);
  151. m_functions[FUNC_SUBSTRING_AFTER] = new FuncLoader("FuncSubstringAfter",
  152. FUNC_SUBSTRING_AFTER);
  153. m_functions[FUNC_NORMALIZE_SPACE] = new FuncLoader("FuncNormalizeSpace",
  154. FUNC_NORMALIZE_SPACE);
  155. m_functions[FUNC_TRANSLATE] = new FuncLoader("FuncTranslate",
  156. FUNC_TRANSLATE);
  157. m_functions[FUNC_CONCAT] = new FuncLoader("FuncConcat", FUNC_CONCAT);
  158. //m_functions[FUNC_FORMAT_NUMBER] = new FuncFormatNumber();
  159. m_functions[FUNC_SYSTEM_PROPERTY] = new FuncLoader("FuncSystemProperty",
  160. FUNC_SYSTEM_PROPERTY);
  161. m_functions[FUNC_EXT_FUNCTION_AVAILABLE] =
  162. new FuncLoader("FuncExtFunctionAvailable", FUNC_EXT_FUNCTION_AVAILABLE);
  163. m_functions[FUNC_EXT_ELEM_AVAILABLE] =
  164. new FuncLoader("FuncExtElementAvailable", FUNC_EXT_ELEM_AVAILABLE);
  165. m_functions[FUNC_SUBSTRING] = new FuncLoader("FuncSubstring",
  166. FUNC_SUBSTRING);
  167. m_functions[FUNC_STRING_LENGTH] = new FuncLoader("FuncStringLength",
  168. FUNC_STRING_LENGTH);
  169. m_functions[FUNC_DOCLOCATION] = new FuncLoader("FuncDoclocation",
  170. FUNC_DOCLOCATION);
  171. m_functions[FUNC_UNPARSED_ENTITY_URI] =
  172. new FuncLoader("FuncUnparsedEntityURI", FUNC_UNPARSED_ENTITY_URI);
  173. }
  174. /**
  175. * Return the name of the a function in the static table. Needed to avoid
  176. * making the table publicly available.
  177. */
  178. static String getFunctionName(int funcID) {
  179. return m_functions[funcID].getName();
  180. }
  181. /**
  182. * Obtain a new Function object from a function ID.
  183. *
  184. * @param which The function ID, which may correspond to one of the FUNC_XXX
  185. * values found in {@link com.sun.org.apache.xpath.internal.compiler.FunctionTable}, but may
  186. * be a value installed by an external module.
  187. *
  188. * @return a a new Function instance.
  189. *
  190. * @throws javax.xml.transform.TransformerException if ClassNotFoundException,
  191. * IllegalAccessException, or InstantiationException is thrown.
  192. */
  193. public static Function getFunction(int which)
  194. throws javax.xml.transform.TransformerException
  195. {
  196. return m_functions[which].getFunction();
  197. }
  198. /**
  199. * Install a built-in function.
  200. * @param name The unqualified name of the function.
  201. * @param func A Implementation of an XPath Function object.
  202. * @return the position of the function in the internal index.
  203. */
  204. public static int installFunction(String name, Expression func)
  205. {
  206. int funcIndex;
  207. Object funcIndexObj = Keywords.m_functions.get(name);
  208. if (null != funcIndexObj)
  209. {
  210. funcIndex = ((Integer) funcIndexObj).intValue();
  211. }
  212. else
  213. {
  214. funcIndex = m_funcNextFreeIndex;
  215. m_funcNextFreeIndex++;
  216. Keywords.m_functions.put(name, new Integer(funcIndex));
  217. }
  218. FuncLoader loader = new FuncLoader(func.getClass().getName(), funcIndex);
  219. m_functions[funcIndex] = loader;
  220. return funcIndex;
  221. }
  222. /**
  223. * Install a function loader at a specific index.
  224. * @param func A Implementation of an XPath Function object.
  225. * @param which The function ID, which may correspond to one of the FUNC_XXX
  226. * values found in {@link com.sun.org.apache.xpath.internal.compiler.FunctionTable}, but may
  227. * be a value installed by an external module.
  228. * @return the position of the function in the internal index.
  229. */
  230. public static void installFunction(Expression func, int funcIndex)
  231. {
  232. FuncLoader loader = new FuncLoader(func.getClass().getName(), funcIndex);
  233. m_functions[funcIndex] = loader;
  234. }
  235. }