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.xalan.res.XSLMessages;
  59. import org.apache.xml.utils.ObjectVector;
  60. import org.apache.xpath.patterns.NodeTest;
  61. import org.apache.xpath.res.XPATHErrorResources;
  62. /**
  63. * This class represents the data structure basics of the XPath
  64. * object.
  65. */
  66. public class OpMap
  67. {
  68. /**
  69. * The current pattern string, for diagnostics purposes
  70. */
  71. protected String m_currentPattern;
  72. /**
  73. * Return the expression as a string for diagnostics.
  74. *
  75. * @return The expression string.
  76. */
  77. public String toString()
  78. {
  79. return m_currentPattern;
  80. }
  81. /**
  82. * Return the expression as a string for diagnostics.
  83. *
  84. * @return The expression string.
  85. */
  86. public String getPatternString()
  87. {
  88. return m_currentPattern;
  89. }
  90. /**
  91. * The starting size of the token queue.
  92. */
  93. static final int MAXTOKENQUEUESIZE = 500;
  94. /*
  95. * Amount to grow token queue when it becomes full
  96. */
  97. static final int BLOCKTOKENQUEUESIZE = 500;
  98. /**
  99. * TokenStack is the queue of used tokens. The current token is the token at the
  100. * end of the m_tokenQueue. The idea is that the queue can be marked and a sequence
  101. * of tokens can be reused.
  102. */
  103. ObjectVector m_tokenQueue = new ObjectVector(MAXTOKENQUEUESIZE, BLOCKTOKENQUEUESIZE);
  104. /**
  105. * Get the XPath as a list of tokens.
  106. *
  107. * @return ObjectVector of tokens.
  108. */
  109. public ObjectVector getTokenQueue()
  110. {
  111. return m_tokenQueue;
  112. }
  113. /**
  114. * Get the XPath as a list of tokens.
  115. *
  116. * @param pos index into token queue.
  117. *
  118. * @return The token, normally a string.
  119. */
  120. public Object getToken(int pos)
  121. {
  122. return m_tokenQueue.elementAt(pos);
  123. }
  124. /**
  125. * The current size of the token queue.
  126. */
  127. // public int m_tokenQueueSize = 0;
  128. /**
  129. * Get size of the token queue.
  130. *
  131. * @return The size of the token queue.
  132. */
  133. public int getTokenQueueSize()
  134. {
  135. return m_tokenQueue.size();
  136. }
  137. /**
  138. * An operations map is used instead of a proper parse tree. It contains
  139. * operations codes and indexes into the m_tokenQueue.
  140. * I use an array instead of a full parse tree in order to cut down
  141. * on the number of objects created.
  142. */
  143. OpMapVector m_opMap = null;
  144. /**
  145. * Get the opcode list that describes the XPath operations. It contains
  146. * operations codes and indexes into the m_tokenQueue.
  147. * I use an array instead of a full parse tree in order to cut down
  148. * on the number of objects created.
  149. *
  150. * @return An IntVector that is the opcode list that describes the XPath operations.
  151. */
  152. public OpMapVector getOpMap()
  153. {
  154. return m_opMap;
  155. }
  156. // Position indexes
  157. /**
  158. * The length is always the opcode position + 1.
  159. * Length is always expressed as the opcode+length bytes,
  160. * so it is always 2 or greater.
  161. */
  162. public static final int MAPINDEX_LENGTH = 1;
  163. /**
  164. * Replace the large arrays
  165. * with a small array.
  166. */
  167. void shrink()
  168. {
  169. int n = m_opMap.elementAt(MAPINDEX_LENGTH);
  170. m_opMap.setToSize(n + 4);
  171. m_opMap.setElementAt(0,n);
  172. m_opMap.setElementAt(0,n+1);
  173. m_opMap.setElementAt(0,n+2);
  174. n = m_tokenQueue.size();
  175. m_tokenQueue.setToSize(n + 4);
  176. m_tokenQueue.setElementAt(null,n);
  177. m_tokenQueue.setElementAt(null,n + 1);
  178. m_tokenQueue.setElementAt(null,n + 2);
  179. }
  180. /**
  181. * Given an operation position, return the current op.
  182. *
  183. * @param opPos index into op map.
  184. * @return the op that corresponds to the opPos argument.
  185. */
  186. public int getOp(int opPos)
  187. {
  188. return m_opMap.elementAt(opPos);
  189. }
  190. /**
  191. * Set the op at index to the given int.
  192. *
  193. * @param opPos index into op map.
  194. * @param value Value to set
  195. */
  196. public void setOp(int opPos, int value)
  197. {
  198. m_opMap.setElementAt(value,opPos);
  199. }
  200. /**
  201. * Given an operation position, return the end position, i.e. the
  202. * beginning of the next operation.
  203. *
  204. * @param opPos An op position of an operation for which there is a size
  205. * entry following.
  206. * @return position of next operation in m_opMap.
  207. */
  208. public int getNextOpPos(int opPos)
  209. {
  210. return opPos + m_opMap.elementAt(opPos + 1);
  211. }
  212. /**
  213. * Given a location step position, return the end position, i.e. the
  214. * beginning of the next step.
  215. *
  216. * @param opPos the position of a location step.
  217. * @return the position of the next location step.
  218. */
  219. public int getNextStepPos(int opPos)
  220. {
  221. int stepType = getOp(opPos);
  222. if ((stepType >= OpCodes.AXES_START_TYPES)
  223. && (stepType <= OpCodes.AXES_END_TYPES))
  224. {
  225. return getNextOpPos(opPos);
  226. }
  227. else if ((stepType >= OpCodes.FIRST_NODESET_OP)
  228. && (stepType <= OpCodes.LAST_NODESET_OP))
  229. {
  230. int newOpPos = getNextOpPos(opPos);
  231. while (OpCodes.OP_PREDICATE == getOp(newOpPos))
  232. {
  233. newOpPos = getNextOpPos(newOpPos);
  234. }
  235. stepType = getOp(newOpPos);
  236. if (!((stepType >= OpCodes.AXES_START_TYPES)
  237. && (stepType <= OpCodes.AXES_END_TYPES)))
  238. {
  239. return OpCodes.ENDOP;
  240. }
  241. return newOpPos;
  242. }
  243. else
  244. {
  245. throw new RuntimeException(
  246. XSLMessages.createXPATHMessage(XPATHErrorResources.ER_UNKNOWN_STEP, new Object[]{new Integer(stepType).toString()}));
  247. //"Programmer's assertion in getNextStepPos: unknown stepType: " + stepType);
  248. }
  249. }
  250. /**
  251. * Given an operation position, return the end position, i.e. the
  252. * beginning of the next operation.
  253. *
  254. * @param opMap The operations map.
  255. * @param opPos index to operation, for which there is a size entry following.
  256. * @return position of next operation in m_opMap.
  257. */
  258. public static int getNextOpPos(int[] opMap, int opPos)
  259. {
  260. return opPos + opMap[opPos + 1];
  261. }
  262. /**
  263. * Given an FROM_stepType position, return the position of the
  264. * first predicate, if there is one, or else this will point
  265. * to the end of the FROM_stepType.
  266. * Example:
  267. * int posOfPredicate = xpath.getNextOpPos(stepPos);
  268. * boolean hasPredicates =
  269. * OpCodes.OP_PREDICATE == xpath.getOp(posOfPredicate);
  270. *
  271. * @param opPos position of FROM_stepType op.
  272. * @return position of predicate in FROM_stepType structure.
  273. */
  274. public int getFirstPredicateOpPos(int opPos)
  275. throws javax.xml.transform.TransformerException
  276. {
  277. int stepType = m_opMap.elementAt(opPos);
  278. if ((stepType >= OpCodes.AXES_START_TYPES)
  279. && (stepType <= OpCodes.AXES_END_TYPES))
  280. {
  281. return opPos + m_opMap.elementAt(opPos + 2);
  282. }
  283. else if ((stepType >= OpCodes.FIRST_NODESET_OP)
  284. && (stepType <= OpCodes.LAST_NODESET_OP))
  285. {
  286. return opPos + m_opMap.elementAt(opPos + 1);
  287. }
  288. else if(-2 == stepType)
  289. {
  290. return -2;
  291. }
  292. else
  293. {
  294. error(org.apache.xpath.res.XPATHErrorResources.ER_UNKNOWN_OPCODE,
  295. new Object[]{ String.valueOf(stepType) }); //"ERROR! Unknown op code: "+m_opMap[opPos]);
  296. return -1;
  297. }
  298. }
  299. /**
  300. * Tell the user of an error, and probably throw an
  301. * exception.
  302. *
  303. * @param msg An error msgkey that corresponds to one of the constants found
  304. * in {@link org.apache.xpath.res.XPATHErrorResources}, which is
  305. * a key for a format string.
  306. * @param args An array of arguments represented in the format string, which
  307. * may be null.
  308. *
  309. * @throws TransformerException if the current ErrorListoner determines to
  310. * throw an exception.
  311. */
  312. public void error(String msg, Object[] args) throws javax.xml.transform.TransformerException
  313. {
  314. java.lang.String fmsg = org.apache.xalan.res.XSLMessages.createXPATHMessage(msg, args);
  315. throw new javax.xml.transform.TransformerException(fmsg);
  316. }
  317. /**
  318. * Go to the first child of a given operation.
  319. *
  320. * @param opPos position of operation.
  321. *
  322. * @return The position of the first child of the operation.
  323. */
  324. public static int getFirstChildPos(int opPos)
  325. {
  326. return opPos + 2;
  327. }
  328. /**
  329. * Get the length of an operation.
  330. *
  331. * @param opPos The position of the operation in the op map.
  332. *
  333. * @return The size of the operation.
  334. */
  335. public int getArgLength(int opPos)
  336. {
  337. return m_opMap.elementAt(opPos + MAPINDEX_LENGTH);
  338. }
  339. /**
  340. * Given a location step, get the length of that step.
  341. *
  342. * @param opPos Position of location step in op map.
  343. *
  344. * @return The length of the step.
  345. */
  346. public int getArgLengthOfStep(int opPos)
  347. {
  348. return m_opMap.elementAt(opPos + MAPINDEX_LENGTH + 1) - 3;
  349. }
  350. /**
  351. * Get the first child position of a given location step.
  352. *
  353. * @param opPos Position of location step in the location map.
  354. *
  355. * @return The first child position of the step.
  356. */
  357. public static int getFirstChildPosOfStep(int opPos)
  358. {
  359. return opPos + 3;
  360. }
  361. /**
  362. * Get the test type of the step, i.e. NODETYPE_XXX value.
  363. *
  364. * @param opPosOfStep The position of the FROM_XXX step.
  365. *
  366. * @return NODETYPE_XXX value.
  367. */
  368. public int getStepTestType(int opPosOfStep)
  369. {
  370. return m_opMap.elementAt(opPosOfStep + 3); // skip past op, len, len without predicates
  371. }
  372. /**
  373. * Get the namespace of the step.
  374. *
  375. * @param opPosOfStep The position of the FROM_XXX step.
  376. *
  377. * @return The step's namespace, NodeTest.WILD, or null for null namespace.
  378. */
  379. public String getStepNS(int opPosOfStep)
  380. {
  381. int argLenOfStep = getArgLengthOfStep(opPosOfStep);
  382. // System.out.println("getStepNS.argLenOfStep: "+argLenOfStep);
  383. if (argLenOfStep == 3)
  384. {
  385. int index = m_opMap.elementAt(opPosOfStep + 4);
  386. if (index >= 0)
  387. return (String) m_tokenQueue.elementAt(index);
  388. else if (OpCodes.ELEMWILDCARD == index)
  389. return NodeTest.WILD;
  390. else
  391. return null;
  392. }
  393. else
  394. return null;
  395. }
  396. /**
  397. * Get the local name of the step.
  398. * @param opPosOfStep The position of the FROM_XXX step.
  399. *
  400. * @return OpCodes.EMPTY, OpCodes.ELEMWILDCARD, or the local name.
  401. */
  402. public String getStepLocalName(int opPosOfStep)
  403. {
  404. int argLenOfStep = getArgLengthOfStep(opPosOfStep);
  405. // System.out.println("getStepLocalName.argLenOfStep: "+argLenOfStep);
  406. int index;
  407. switch (argLenOfStep)
  408. {
  409. case 0 :
  410. index = OpCodes.EMPTY;
  411. break;
  412. case 1 :
  413. index = OpCodes.ELEMWILDCARD;
  414. break;
  415. case 2 :
  416. index = m_opMap.elementAt(opPosOfStep + 4);
  417. break;
  418. case 3 :
  419. index = m_opMap.elementAt(opPosOfStep + 5);
  420. break;
  421. default :
  422. index = OpCodes.EMPTY;
  423. break; // Should assert error
  424. }
  425. // int index = (argLenOfStep == 3) ? m_opMap[opPosOfStep+5]
  426. // : ((argLenOfStep == 1) ? -3 : -2);
  427. if (index >= 0)
  428. return (String) m_tokenQueue.elementAt(index).toString();
  429. else if (OpCodes.ELEMWILDCARD == index)
  430. return NodeTest.WILD;
  431. else
  432. return null;
  433. }
  434. }