1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. *
  5. * Copyright (c) 1999 The Apache Software Foundation. All rights
  6. * reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * 3. The end-user documentation included with the redistribution,
  21. * if any, must include the following acknowledgment:
  22. * "This product includes software developed by the
  23. * Apache Software Foundation (http://www.apache.org/)."
  24. * Alternately, this acknowledgment may appear in the software itself,
  25. * if and wherever such third-party acknowledgments normally appear.
  26. *
  27. * 4. The names "Xalan" and "Apache Software Foundation" must
  28. * not be used to endorse or promote products derived from this
  29. * software without prior written permission. For written
  30. * permission, please contact apache@apache.org.
  31. *
  32. * 5. Products derived from this software may not be called "Apache",
  33. * nor may "Apache" appear in their name, without prior written
  34. * permission of the Apache Software Foundation.
  35. *
  36. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  37. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  38. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  39. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  40. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  41. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  42. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  43. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  44. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  45. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  46. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  47. * SUCH DAMAGE.
  48. * ====================================================================
  49. *
  50. * This software consists of voluntary contributions made by many
  51. * individuals on behalf of the Apache Software Foundation and was
  52. * originally based on software copyright (c) 1999, Lotus
  53. * Development Corporation., http://www.lotus.com. For more
  54. * information on the Apache Software Foundation, please see
  55. * <http://www.apache.org/>.
  56. */
  57. package org.apache.xpath.compiler;
  58. import org.apache.xpath.Expression;
  59. import org.apache.xpath.functions.Function;
  60. /**
  61. * The function table for XPath.
  62. */
  63. public class FunctionTable
  64. {
  65. /** The 'current()' id. */
  66. public static final int FUNC_CURRENT = 0;
  67. /** The 'last()' id. */
  68. public static final int FUNC_LAST = 1;
  69. /** The 'position()' id. */
  70. public static final int FUNC_POSITION = 2;
  71. /** The 'count()' id. */
  72. public static final int FUNC_COUNT = 3;
  73. /** The 'id()' id. */
  74. public static final int FUNC_ID = 4;
  75. /** The 'key()' id (XSLT). */
  76. public static final int FUNC_KEY = 5;
  77. /** The 'local-name()' id. */
  78. public static final int FUNC_LOCAL_PART = 7;
  79. /** The 'namespace-uri()' id. */
  80. public static final int FUNC_NAMESPACE = 8;
  81. /** The 'name()' id. */
  82. public static final int FUNC_QNAME = 9;
  83. /** The 'generate-id()' id. */
  84. public static final int FUNC_GENERATE_ID = 10;
  85. /** The 'not()' id. */
  86. public static final int FUNC_NOT = 11;
  87. /** The 'true()' id. */
  88. public static final int FUNC_TRUE = 12;
  89. /** The 'false()' id. */
  90. public static final int FUNC_FALSE = 13;
  91. /** The 'boolean()' id. */
  92. public static final int FUNC_BOOLEAN = 14;
  93. /** The 'number()' id. */
  94. public static final int FUNC_NUMBER = 15;
  95. /** The 'floor()' id. */
  96. public static final int FUNC_FLOOR = 16;
  97. /** The 'ceiling()' id. */
  98. public static final int FUNC_CEILING = 17;
  99. /** The 'round()' id. */
  100. public static final int FUNC_ROUND = 18;
  101. /** The 'sum()' id. */
  102. public static final int FUNC_SUM = 19;
  103. /** The 'string()' id. */
  104. public static final int FUNC_STRING = 20;
  105. /** The 'starts-with()' id. */
  106. public static final int FUNC_STARTS_WITH = 21;
  107. /** The 'contains()' id. */
  108. public static final int FUNC_CONTAINS = 22;
  109. /** The 'substring-before()' id. */
  110. public static final int FUNC_SUBSTRING_BEFORE = 23;
  111. /** The 'substring-after()' id. */
  112. public static final int FUNC_SUBSTRING_AFTER = 24;
  113. /** The 'normalize-space()' id. */
  114. public static final int FUNC_NORMALIZE_SPACE = 25;
  115. /** The 'translate()' id. */
  116. public static final int FUNC_TRANSLATE = 26;
  117. /** The 'concat()' id. */
  118. public static final int FUNC_CONCAT = 27;
  119. /** The 'substring()' id. */
  120. public static final int FUNC_SUBSTRING = 29;
  121. /** The 'string-length()' id. */
  122. public static final int FUNC_STRING_LENGTH = 30;
  123. /** The 'system-property()' id. */
  124. public static final int FUNC_SYSTEM_PROPERTY = 31;
  125. /** The 'lang()' id. */
  126. public static final int FUNC_LANG = 32;
  127. /** The 'function-available()' id (XSLT). */
  128. public static final int FUNC_EXT_FUNCTION_AVAILABLE = 33;
  129. /** The 'element-available()' id (XSLT). */
  130. public static final int FUNC_EXT_ELEM_AVAILABLE = 34;
  131. /** The 'unparsed-entity-uri()' id (XSLT). */
  132. public static final int FUNC_UNPARSED_ENTITY_URI = 36;
  133. // Proprietary
  134. /** The 'document-location()' id (Proprietary). */
  135. public static final int FUNC_DOCLOCATION = 35;
  136. /**
  137. * The function table.
  138. */
  139. public static FuncLoader m_functions[];
  140. /**
  141. * Number of built in functions. Be sure to update this as
  142. * built-in functions are added.
  143. */
  144. private static final int NUM_BUILT_IN_FUNCS = 37;
  145. /**
  146. * Number of built-in functions that may be added.
  147. */
  148. private static final int NUM_ALLOWABLE_ADDINS = 30;
  149. /**
  150. * The index to the next free function index.
  151. */
  152. static int m_funcNextFreeIndex = NUM_BUILT_IN_FUNCS;
  153. static
  154. {
  155. m_functions = new FuncLoader[NUM_BUILT_IN_FUNCS + NUM_ALLOWABLE_ADDINS];
  156. m_functions[FUNC_CURRENT] = new FuncLoader("FuncCurrent", FUNC_CURRENT);
  157. m_functions[FUNC_LAST] = new FuncLoader("FuncLast", FUNC_LAST);
  158. m_functions[FUNC_POSITION] = new FuncLoader("FuncPosition",
  159. FUNC_POSITION);
  160. m_functions[FUNC_COUNT] = new FuncLoader("FuncCount", FUNC_COUNT);
  161. m_functions[FUNC_ID] = new FuncLoader("FuncId", FUNC_ID);
  162. m_functions[FUNC_KEY] =
  163. new FuncLoader("org.apache.xalan.templates.FuncKey", FUNC_KEY);
  164. // m_functions[FUNC_DOC] = new FuncDoc();
  165. m_functions[FUNC_LOCAL_PART] = new FuncLoader("FuncLocalPart",
  166. FUNC_LOCAL_PART);
  167. m_functions[FUNC_NAMESPACE] = new FuncLoader("FuncNamespace",
  168. FUNC_NAMESPACE);
  169. m_functions[FUNC_QNAME] = new FuncLoader("FuncQname", FUNC_QNAME);
  170. m_functions[FUNC_GENERATE_ID] = new FuncLoader("FuncGenerateId",
  171. FUNC_GENERATE_ID);
  172. m_functions[FUNC_NOT] = new FuncLoader("FuncNot", FUNC_NOT);
  173. m_functions[FUNC_TRUE] = new FuncLoader("FuncTrue", FUNC_TRUE);
  174. m_functions[FUNC_FALSE] = new FuncLoader("FuncFalse", FUNC_FALSE);
  175. m_functions[FUNC_BOOLEAN] = new FuncLoader("FuncBoolean", FUNC_BOOLEAN);
  176. m_functions[FUNC_LANG] = new FuncLoader("FuncLang", FUNC_LANG);
  177. m_functions[FUNC_NUMBER] = new FuncLoader("FuncNumber", FUNC_NUMBER);
  178. m_functions[FUNC_FLOOR] = new FuncLoader("FuncFloor", FUNC_FLOOR);
  179. m_functions[FUNC_CEILING] = new FuncLoader("FuncCeiling", FUNC_CEILING);
  180. m_functions[FUNC_ROUND] = new FuncLoader("FuncRound", FUNC_ROUND);
  181. m_functions[FUNC_SUM] = new FuncLoader("FuncSum", FUNC_SUM);
  182. m_functions[FUNC_STRING] = new FuncLoader("FuncString", FUNC_STRING);
  183. m_functions[FUNC_STARTS_WITH] = new FuncLoader("FuncStartsWith",
  184. FUNC_STARTS_WITH);
  185. m_functions[FUNC_CONTAINS] = new FuncLoader("FuncContains",
  186. FUNC_CONTAINS);
  187. m_functions[FUNC_SUBSTRING_BEFORE] = new FuncLoader("FuncSubstringBefore",
  188. FUNC_SUBSTRING_BEFORE);
  189. m_functions[FUNC_SUBSTRING_AFTER] = new FuncLoader("FuncSubstringAfter",
  190. FUNC_SUBSTRING_AFTER);
  191. m_functions[FUNC_NORMALIZE_SPACE] = new FuncLoader("FuncNormalizeSpace",
  192. FUNC_NORMALIZE_SPACE);
  193. m_functions[FUNC_TRANSLATE] = new FuncLoader("FuncTranslate",
  194. FUNC_TRANSLATE);
  195. m_functions[FUNC_CONCAT] = new FuncLoader("FuncConcat", FUNC_CONCAT);
  196. //m_functions[FUNC_FORMAT_NUMBER] = new FuncFormatNumber();
  197. m_functions[FUNC_SYSTEM_PROPERTY] = new FuncLoader("FuncSystemProperty",
  198. FUNC_SYSTEM_PROPERTY);
  199. m_functions[FUNC_EXT_FUNCTION_AVAILABLE] =
  200. new FuncLoader("FuncExtFunctionAvailable", FUNC_EXT_FUNCTION_AVAILABLE);
  201. m_functions[FUNC_EXT_ELEM_AVAILABLE] =
  202. new FuncLoader("FuncExtElementAvailable", FUNC_EXT_ELEM_AVAILABLE);
  203. m_functions[FUNC_SUBSTRING] = new FuncLoader("FuncSubstring",
  204. FUNC_SUBSTRING);
  205. m_functions[FUNC_STRING_LENGTH] = new FuncLoader("FuncStringLength",
  206. FUNC_STRING_LENGTH);
  207. m_functions[FUNC_DOCLOCATION] = new FuncLoader("FuncDoclocation",
  208. FUNC_DOCLOCATION);
  209. m_functions[FUNC_UNPARSED_ENTITY_URI] =
  210. new FuncLoader("FuncUnparsedEntityURI", FUNC_UNPARSED_ENTITY_URI);
  211. }
  212. /**
  213. * Obtain a new Function object from a function ID.
  214. *
  215. * @param which The function ID, which may correspond to one of the FUNC_XXX
  216. * values found in {@link org.apache.xpath.compiler.FunctionTable}, but may
  217. * be a value installed by an external module.
  218. *
  219. * @return a a new Function instance.
  220. *
  221. * @throws javax.xml.transform.TransformerException if ClassNotFoundException,
  222. * IllegalAccessException, or InstantiationException is thrown.
  223. */
  224. public static Function getFunction(int which)
  225. throws javax.xml.transform.TransformerException
  226. {
  227. return m_functions[which].getFunction();
  228. }
  229. /**
  230. * Install a built-in function.
  231. * @param name The unqualified name of the function.
  232. * @param func A Implementation of an XPath Function object.
  233. * @return the position of the function in the internal index.
  234. */
  235. public static int installFunction(String name, Expression func)
  236. {
  237. int funcIndex;
  238. Object funcIndexObj = Keywords.m_functions.get(name);
  239. if (null != funcIndexObj)
  240. {
  241. funcIndex = ((Integer) funcIndexObj).intValue();
  242. }
  243. else
  244. {
  245. funcIndex = m_funcNextFreeIndex;
  246. m_funcNextFreeIndex++;
  247. Keywords.m_functions.put(name, new Integer(funcIndex));
  248. }
  249. FuncLoader loader = new FuncLoader(func.getClass().getName(), funcIndex);
  250. m_functions[funcIndex] = loader;
  251. return funcIndex;
  252. }
  253. /**
  254. * Install a function loader at a specific index.
  255. * @param func A Implementation of an XPath Function object.
  256. * @param which The function ID, which may correspond to one of the FUNC_XXX
  257. * values found in {@link org.apache.xpath.compiler.FunctionTable}, but may
  258. * be a value installed by an external module.
  259. * @return the position of the function in the internal index.
  260. */
  261. public static void installFunction(Expression func, int funcIndex)
  262. {
  263. FuncLoader loader = new FuncLoader(func.getClass().getName(), funcIndex);
  264. m_functions[funcIndex] = loader;
  265. }
  266. }