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