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 java.util.Hashtable;
  59. /**
  60. * <meta name="usage" content="internal"/>
  61. * Table of strings to operation code lookups.
  62. */
  63. public class Keywords
  64. {
  65. /** Table of keywords to opcode associations. */
  66. static Hashtable m_keywords = new Hashtable();
  67. /** Table of axes names to opcode associations. */
  68. static Hashtable m_axisnames = new Hashtable();
  69. /** Table of function name to function ID associations. */
  70. static Hashtable m_functions = new Hashtable();
  71. /** Table of node type strings to opcode associations. */
  72. static Hashtable m_nodetypes = new Hashtable();
  73. /** ancestor axes string. */
  74. private static final String FROM_ANCESTORS_STRING = "ancestor";
  75. /** ancestor-or-self axes string. */
  76. private static final String FROM_ANCESTORS_OR_SELF_STRING =
  77. "ancestor-or-self";
  78. /** attribute axes string. */
  79. private static final String FROM_ATTRIBUTES_STRING = "attribute";
  80. /** child axes string. */
  81. private static final String FROM_CHILDREN_STRING = "child";
  82. /** descendant-or-self axes string. */
  83. private static final String FROM_DESCENDANTS_STRING = "descendant";
  84. /** ancestor axes string. */
  85. private static final String FROM_DESCENDANTS_OR_SELF_STRING =
  86. "descendant-or-self";
  87. /** following axes string. */
  88. private static final String FROM_FOLLOWING_STRING = "following";
  89. /** following-sibling axes string. */
  90. private static final String FROM_FOLLOWING_SIBLINGS_STRING =
  91. "following-sibling";
  92. /** parent axes string. */
  93. private static final String FROM_PARENT_STRING = "parent";
  94. /** preceding axes string. */
  95. private static final String FROM_PRECEDING_STRING = "preceding";
  96. /** preceding-sibling axes string. */
  97. private static final String FROM_PRECEDING_SIBLINGS_STRING =
  98. "preceding-sibling";
  99. /** self axes string. */
  100. private static final String FROM_SELF_STRING = "self";
  101. /** namespace axes string. */
  102. private static final String FROM_NAMESPACE_STRING = "namespace";
  103. /** self axes abreviated string. */
  104. private static final String FROM_SELF_ABBREVIATED_STRING = ".";
  105. /** comment node test string. */
  106. private static final String NODETYPE_COMMENT_STRING = "comment";
  107. /** text node test string. */
  108. private static final String NODETYPE_TEXT_STRING = "text";
  109. /** processing-instruction node test string. */
  110. private static final String NODETYPE_PI_STRING = "processing-instruction";
  111. /** Any node test string. */
  112. private static final String NODETYPE_NODE_STRING = "node";
  113. /** Wildcard element string. */
  114. private static final String NODETYPE_ANYELEMENT_STRING = "*";
  115. /** current function string. */
  116. private static final String FUNC_CURRENT_STRING = "current";
  117. /** last function string. */
  118. private static final String FUNC_LAST_STRING = "last";
  119. /** position function string. */
  120. private static final String FUNC_POSITION_STRING = "position";
  121. /** count function string. */
  122. private static final String FUNC_COUNT_STRING = "count";
  123. /** id function string. */
  124. static final String FUNC_ID_STRING = "id";
  125. /** key function string (XSLT). */
  126. public static final String FUNC_KEY_STRING = "key";
  127. /** local-name function string. */
  128. private static final String FUNC_LOCAL_PART_STRING = "local-name";
  129. /** namespace-uri function string. */
  130. private static final String FUNC_NAMESPACE_STRING = "namespace-uri";
  131. /** name function string. */
  132. private static final String FUNC_NAME_STRING = "name";
  133. /** generate-id function string (XSLT). */
  134. private static final String FUNC_GENERATE_ID_STRING = "generate-id";
  135. /** not function string. */
  136. private static final String FUNC_NOT_STRING = "not";
  137. /** true function string. */
  138. private static final String FUNC_TRUE_STRING = "true";
  139. /** false function string. */
  140. private static final String FUNC_FALSE_STRING = "false";
  141. /** boolean function string. */
  142. private static final String FUNC_BOOLEAN_STRING = "boolean";
  143. /** lang function string. */
  144. private static final String FUNC_LANG_STRING = "lang";
  145. /** number function string. */
  146. private static final String FUNC_NUMBER_STRING = "number";
  147. /** floor function string. */
  148. private static final String FUNC_FLOOR_STRING = "floor";
  149. /** ceiling function string. */
  150. private static final String FUNC_CEILING_STRING = "ceiling";
  151. /** round function string. */
  152. private static final String FUNC_ROUND_STRING = "round";
  153. /** sum function string. */
  154. private static final String FUNC_SUM_STRING = "sum";
  155. /** string function string. */
  156. private static final String FUNC_STRING_STRING = "string";
  157. /** starts-with function string. */
  158. private static final String FUNC_STARTS_WITH_STRING = "starts-with";
  159. /** contains function string. */
  160. private static final String FUNC_CONTAINS_STRING = "contains";
  161. /** substring-before function string. */
  162. private static final String FUNC_SUBSTRING_BEFORE_STRING =
  163. "substring-before";
  164. /** substring-after function string. */
  165. private static final String FUNC_SUBSTRING_AFTER_STRING = "substring-after";
  166. /** normalize-space function string. */
  167. private static final String FUNC_NORMALIZE_SPACE_STRING = "normalize-space";
  168. /** translate function string. */
  169. private static final String FUNC_TRANSLATE_STRING = "translate";
  170. /** concat function string. */
  171. private static final String FUNC_CONCAT_STRING = "concat";
  172. /** system-property function string. */
  173. private static final String FUNC_SYSTEM_PROPERTY_STRING = "system-property";
  174. /** function-available function string (XSLT). */
  175. private static final String FUNC_EXT_FUNCTION_AVAILABLE_STRING =
  176. "function-available";
  177. /** element-available function string (XSLT). */
  178. private static final String FUNC_EXT_ELEM_AVAILABLE_STRING =
  179. "element-available";
  180. /** substring function string. */
  181. private static final String FUNC_SUBSTRING_STRING = "substring";
  182. /** string-length function string. */
  183. private static final String FUNC_STRING_LENGTH_STRING = "string-length";
  184. /** unparsed-entity-uri function string (XSLT). */
  185. private static final String FUNC_UNPARSED_ENTITY_URI_STRING =
  186. "unparsed-entity-uri";
  187. // Proprietary, built in functions
  188. /** current function string (Proprietary). */
  189. private static final String FUNC_DOCLOCATION_STRING = "document-location";
  190. static
  191. {
  192. m_axisnames.put(FROM_ANCESTORS_STRING,
  193. new Integer(OpCodes.FROM_ANCESTORS));
  194. m_axisnames.put(FROM_ANCESTORS_OR_SELF_STRING,
  195. new Integer(OpCodes.FROM_ANCESTORS_OR_SELF));
  196. m_axisnames.put(FROM_ATTRIBUTES_STRING,
  197. new Integer(OpCodes.FROM_ATTRIBUTES));
  198. m_axisnames.put(FROM_CHILDREN_STRING,
  199. new Integer(OpCodes.FROM_CHILDREN));
  200. m_axisnames.put(FROM_DESCENDANTS_STRING,
  201. new Integer(OpCodes.FROM_DESCENDANTS));
  202. m_axisnames.put(FROM_DESCENDANTS_OR_SELF_STRING,
  203. new Integer(OpCodes.FROM_DESCENDANTS_OR_SELF));
  204. m_axisnames.put(FROM_FOLLOWING_STRING,
  205. new Integer(OpCodes.FROM_FOLLOWING));
  206. m_axisnames.put(FROM_FOLLOWING_SIBLINGS_STRING,
  207. new Integer(OpCodes.FROM_FOLLOWING_SIBLINGS));
  208. m_axisnames.put(FROM_PARENT_STRING,
  209. new Integer(OpCodes.FROM_PARENT));
  210. m_axisnames.put(FROM_PRECEDING_STRING,
  211. new Integer(OpCodes.FROM_PRECEDING));
  212. m_axisnames.put(FROM_PRECEDING_SIBLINGS_STRING,
  213. new Integer(OpCodes.FROM_PRECEDING_SIBLINGS));
  214. m_axisnames.put(FROM_SELF_STRING,
  215. new Integer(OpCodes.FROM_SELF));
  216. m_axisnames.put(FROM_NAMESPACE_STRING,
  217. new Integer(OpCodes.FROM_NAMESPACE));
  218. m_nodetypes.put(NODETYPE_COMMENT_STRING,
  219. new Integer(OpCodes.NODETYPE_COMMENT));
  220. m_nodetypes.put(NODETYPE_TEXT_STRING,
  221. new Integer(OpCodes.NODETYPE_TEXT));
  222. m_nodetypes.put(NODETYPE_PI_STRING,
  223. new Integer(OpCodes.NODETYPE_PI));
  224. m_nodetypes.put(NODETYPE_NODE_STRING,
  225. new Integer(OpCodes.NODETYPE_NODE));
  226. m_nodetypes.put(NODETYPE_ANYELEMENT_STRING,
  227. new Integer(OpCodes.NODETYPE_ANYELEMENT));
  228. m_keywords.put(FROM_SELF_ABBREVIATED_STRING,
  229. new Integer(OpCodes.FROM_SELF));
  230. m_keywords.put(FUNC_ID_STRING,
  231. new Integer(FunctionTable.FUNC_ID));
  232. m_keywords.put(FUNC_KEY_STRING,
  233. new Integer(FunctionTable.FUNC_KEY));
  234. m_functions.put(FUNC_CURRENT_STRING,
  235. new Integer(FunctionTable.FUNC_CURRENT));
  236. m_functions.put(FUNC_LAST_STRING,
  237. new Integer(FunctionTable.FUNC_LAST));
  238. m_functions.put(FUNC_POSITION_STRING,
  239. new Integer(FunctionTable.FUNC_POSITION));
  240. m_functions.put(FUNC_COUNT_STRING,
  241. new Integer(FunctionTable.FUNC_COUNT));
  242. m_functions.put(FUNC_ID_STRING,
  243. new Integer(FunctionTable.FUNC_ID));
  244. m_functions.put(FUNC_KEY_STRING,
  245. new Integer(FunctionTable.FUNC_KEY));
  246. m_functions.put(FUNC_LOCAL_PART_STRING,
  247. new Integer(FunctionTable.FUNC_LOCAL_PART));
  248. m_functions.put(FUNC_NAMESPACE_STRING,
  249. new Integer(FunctionTable.FUNC_NAMESPACE));
  250. m_functions.put(FUNC_NAME_STRING,
  251. new Integer(FunctionTable.FUNC_QNAME));
  252. m_functions.put(FUNC_GENERATE_ID_STRING,
  253. new Integer(FunctionTable.FUNC_GENERATE_ID));
  254. m_functions.put(FUNC_NOT_STRING,
  255. new Integer(FunctionTable.FUNC_NOT));
  256. m_functions.put(FUNC_TRUE_STRING,
  257. new Integer(FunctionTable.FUNC_TRUE));
  258. m_functions.put(FUNC_FALSE_STRING,
  259. new Integer(FunctionTable.FUNC_FALSE));
  260. m_functions.put(FUNC_BOOLEAN_STRING,
  261. new Integer(FunctionTable.FUNC_BOOLEAN));
  262. m_functions.put(FUNC_LANG_STRING,
  263. new Integer(FunctionTable.FUNC_LANG));
  264. m_functions.put(FUNC_NUMBER_STRING,
  265. new Integer(FunctionTable.FUNC_NUMBER));
  266. m_functions.put(FUNC_FLOOR_STRING,
  267. new Integer(FunctionTable.FUNC_FLOOR));
  268. m_functions.put(FUNC_CEILING_STRING,
  269. new Integer(FunctionTable.FUNC_CEILING));
  270. m_functions.put(FUNC_ROUND_STRING,
  271. new Integer(FunctionTable.FUNC_ROUND));
  272. m_functions.put(FUNC_SUM_STRING,
  273. new Integer(FunctionTable.FUNC_SUM));
  274. m_functions.put(FUNC_STRING_STRING,
  275. new Integer(FunctionTable.FUNC_STRING));
  276. m_functions.put(FUNC_STARTS_WITH_STRING,
  277. new Integer(FunctionTable.FUNC_STARTS_WITH));
  278. m_functions.put(FUNC_CONTAINS_STRING,
  279. new Integer(FunctionTable.FUNC_CONTAINS));
  280. m_functions.put(FUNC_SUBSTRING_BEFORE_STRING,
  281. new Integer(FunctionTable.FUNC_SUBSTRING_BEFORE));
  282. m_functions.put(FUNC_SUBSTRING_AFTER_STRING,
  283. new Integer(FunctionTable.FUNC_SUBSTRING_AFTER));
  284. m_functions.put(FUNC_NORMALIZE_SPACE_STRING,
  285. new Integer(FunctionTable.FUNC_NORMALIZE_SPACE));
  286. m_functions.put(FUNC_TRANSLATE_STRING,
  287. new Integer(FunctionTable.FUNC_TRANSLATE));
  288. m_functions.put(FUNC_CONCAT_STRING,
  289. new Integer(FunctionTable.FUNC_CONCAT));
  290. //m_functions.put(FUNC_FORMAT_NUMBER_STRING, new Integer(FunctionTable.FUNC_FORMAT_NUMBER));
  291. m_functions.put(FUNC_SYSTEM_PROPERTY_STRING,
  292. new Integer(FunctionTable.FUNC_SYSTEM_PROPERTY));
  293. m_functions.put(FUNC_EXT_FUNCTION_AVAILABLE_STRING,
  294. new Integer(FunctionTable.FUNC_EXT_FUNCTION_AVAILABLE));
  295. m_functions.put(FUNC_EXT_ELEM_AVAILABLE_STRING,
  296. new Integer(FunctionTable.FUNC_EXT_ELEM_AVAILABLE));
  297. m_functions.put(FUNC_SUBSTRING_STRING,
  298. new Integer(FunctionTable.FUNC_SUBSTRING));
  299. m_functions.put(FUNC_STRING_LENGTH_STRING,
  300. new Integer(FunctionTable.FUNC_STRING_LENGTH));
  301. m_functions.put(FUNC_UNPARSED_ENTITY_URI_STRING,
  302. new Integer(FunctionTable.FUNC_UNPARSED_ENTITY_URI));
  303. // These aren't really functions.
  304. m_functions.put(NODETYPE_COMMENT_STRING,
  305. new Integer(OpCodes.NODETYPE_COMMENT));
  306. m_functions.put(NODETYPE_TEXT_STRING,
  307. new Integer(OpCodes.NODETYPE_TEXT));
  308. m_functions.put(NODETYPE_PI_STRING,
  309. new Integer(OpCodes.NODETYPE_PI));
  310. m_functions.put(NODETYPE_NODE_STRING,
  311. new Integer(OpCodes.NODETYPE_NODE));
  312. m_functions.put(FUNC_DOCLOCATION_STRING,
  313. new Integer(FunctionTable.FUNC_DOCLOCATION));
  314. }
  315. /**
  316. * Tell if a built-in, non-namespaced function is available.
  317. *
  318. * @param methName The local name of the function.
  319. *
  320. * @return True if the function can be executed.
  321. */
  322. public static boolean functionAvailable(String methName)
  323. {
  324. try
  325. {
  326. Object tblEntry = m_functions.get(methName);
  327. if (null == tblEntry)
  328. return false;
  329. int funcType = ((Integer) tblEntry).intValue();
  330. switch (funcType)
  331. {
  332. case OpCodes.NODETYPE_COMMENT :
  333. case OpCodes.NODETYPE_TEXT :
  334. case OpCodes.NODETYPE_PI :
  335. case OpCodes.NODETYPE_NODE :
  336. return false; // These look like functions but they're NodeTests.
  337. default :
  338. return true;
  339. }
  340. }
  341. catch (Exception e)
  342. {
  343. return false;
  344. }
  345. }
  346. }