1. /*
  2. * Copyright 2001-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: KeyIndex.java,v 1.13 2004/02/16 22:54:59 minchau Exp $
  18. */
  19. package com.sun.org.apache.xalan.internal.xsltc.dom;
  20. import java.util.StringTokenizer;
  21. import com.sun.org.apache.xalan.internal.xsltc.DOM;
  22. import com.sun.org.apache.xalan.internal.xsltc.DOMEnhancedForDTM;
  23. import com.sun.org.apache.xalan.internal.xsltc.runtime.Hashtable;
  24. import com.sun.org.apache.xalan.internal.xsltc.util.IntegerArray;
  25. import com.sun.org.apache.xml.internal.dtm.DTM;
  26. import com.sun.org.apache.xml.internal.dtm.DTMAxisIterator;
  27. import com.sun.org.apache.xml.internal.dtm.ref.DTMAxisIteratorBase;
  28. /**
  29. * @author Morten Jorgensen
  30. * @author Santiago Pericas-Geertsen
  31. */
  32. public class KeyIndex extends DTMAxisIteratorBase {
  33. /**
  34. * A mapping between values and nodesets.
  35. */
  36. private Hashtable _index = new Hashtable();
  37. /**
  38. * The node set associated to the current value passed
  39. * to lookupKey();
  40. */
  41. private IntegerArray _nodes = null;
  42. /**
  43. * The XSLTC DOM object if this KeyIndex is being used to implement the
  44. * id() function.
  45. */
  46. private DOM _dom;
  47. private DOMEnhancedForDTM _enhancedDOM;
  48. /**
  49. * Store position after call to setMark()
  50. */
  51. private int _markedPosition = 0;
  52. public KeyIndex(int dummy) {
  53. }
  54. public void setRestartable(boolean flag) {
  55. }
  56. /**
  57. * Adds a node to the node list for a given value. Nodes will
  58. * always be added in document order.
  59. */
  60. public void add(Object value, int node) {
  61. IntegerArray nodes;
  62. if ((nodes = (IntegerArray) _index.get(value)) == null) {
  63. _index.put(value, nodes = new IntegerArray());
  64. }
  65. nodes.add(node);
  66. }
  67. /**
  68. * Merge the current value's nodeset set by lookupKey() with _nodes.
  69. */
  70. public void merge(KeyIndex other) {
  71. if (other == null) return;
  72. if (other._nodes != null) {
  73. if (_nodes == null) {
  74. _nodes = other._nodes;
  75. }
  76. else {
  77. _nodes.merge(other._nodes);
  78. }
  79. }
  80. }
  81. /**
  82. * This method must be called by the code generated by the id() function
  83. * prior to returning the node iterator. The lookup code for key() and
  84. * id() differ in the way the lookup value can be whitespace separated
  85. * list of tokens for the id() function, but a single string for the
  86. * key() function.
  87. */
  88. public void lookupId(Object value) {
  89. // Clear _nodes array
  90. _nodes = null;
  91. final StringTokenizer values = new StringTokenizer((String) value);
  92. while (values.hasMoreElements()) {
  93. final String token = (String) values.nextElement();
  94. IntegerArray nodes = (IntegerArray) _index.get(token);
  95. if (nodes == null && _enhancedDOM != null
  96. && _enhancedDOM.hasDOMSource()) {
  97. nodes = getDOMNodeById(token);
  98. }
  99. if (nodes == null) continue;
  100. if (_nodes == null) {
  101. _nodes = nodes;
  102. }
  103. else {
  104. _nodes.merge(nodes);
  105. }
  106. }
  107. }
  108. /**
  109. * Return an IntegerArray for the DOM Node which has the given id.
  110. *
  111. * @param id The id
  112. * @return A IntegerArray representing the Node whose id is the given value.
  113. */
  114. public IntegerArray getDOMNodeById(String id) {
  115. IntegerArray nodes = null;
  116. if (_enhancedDOM != null) {
  117. int ident = _enhancedDOM.getElementById(id);
  118. if (ident != DTM.NULL) {
  119. nodes = new IntegerArray();
  120. _index.put(id, nodes);
  121. nodes.add(ident);
  122. }
  123. }
  124. return nodes;
  125. }
  126. /**
  127. * This method must be called by the code generated by the key() function
  128. * prior to returning the node iterator.
  129. */
  130. public void lookupKey(Object value) {
  131. _nodes = (IntegerArray) _index.get(value);
  132. _position = 0;
  133. }
  134. /**
  135. * Callers should not call next() after it returns END.
  136. */
  137. public int next() {
  138. if (_nodes == null) return DTMAxisIterator.END;
  139. return (_position < _nodes.cardinality()) ?
  140. _dom.getNodeHandle(_nodes.at(_position++)) : DTMAxisIterator.END;
  141. }
  142. public int containsID(int node, Object value) {
  143. final String string = (String)value;
  144. if (string.indexOf(' ') > -1) {
  145. final StringTokenizer values = new StringTokenizer(string);
  146. while (values.hasMoreElements()) {
  147. final String token = (String) values.nextElement();
  148. IntegerArray nodes = (IntegerArray) _index.get(token);
  149. if (nodes == null && _enhancedDOM != null
  150. && _enhancedDOM.hasDOMSource()) {
  151. nodes = getDOMNodeById(token);
  152. }
  153. if (nodes != null && nodes.indexOf(node) >= 0) {
  154. return 1;
  155. }
  156. }
  157. return 0;
  158. }
  159. else {
  160. IntegerArray nodes = (IntegerArray) _index.get(value);
  161. if (nodes == null && _enhancedDOM != null && _enhancedDOM.hasDOMSource()) {
  162. nodes = getDOMNodeById(string);
  163. }
  164. return (nodes != null && nodes.indexOf(node) >= 0) ? 1 : 0;
  165. }
  166. }
  167. public int containsKey(int node, Object value) {
  168. final IntegerArray nodes = (IntegerArray) _index.get(value);
  169. return (nodes != null && nodes.indexOf(node) >= 0) ? 1 : 0;
  170. }
  171. /**
  172. * Resets the iterator to the last start node.
  173. */
  174. public DTMAxisIterator reset() {
  175. _position = 0;
  176. return this;
  177. }
  178. /**
  179. * Returns the number of elements in this iterator.
  180. */
  181. public int getLast() {
  182. return (_nodes == null) ? 0 : _nodes.cardinality();
  183. }
  184. /**
  185. * Returns the position of the current node in the set.
  186. */
  187. public int getPosition() {
  188. return _position;
  189. }
  190. /**
  191. * Remembers the current node for the next call to gotoMark().
  192. */
  193. public void setMark() {
  194. _markedPosition = _position;
  195. }
  196. /**
  197. * Restores the current node remembered by setMark().
  198. */
  199. public void gotoMark() {
  200. _position = _markedPosition;
  201. }
  202. /**
  203. * Set start to END should 'close' the iterator,
  204. * i.e. subsequent call to next() should return END.
  205. */
  206. public DTMAxisIterator setStartNode(int start) {
  207. if (start == DTMAxisIterator.END) {
  208. _nodes = null;
  209. }
  210. else if (_nodes != null) {
  211. _position = 0;
  212. }
  213. return (DTMAxisIterator) this;
  214. }
  215. /**
  216. * Get start to END should 'close' the iterator,
  217. * i.e. subsequent call to next() should return END.
  218. */
  219. public int getStartNode() {
  220. return 0;
  221. }
  222. /**
  223. * True if this iterator has a reversed axis.
  224. */
  225. public boolean isReverse() {
  226. return(false);
  227. }
  228. /**
  229. * Returns a deep copy of this iterator.
  230. */
  231. public DTMAxisIterator cloneIterator() {
  232. KeyIndex other = new KeyIndex(0);
  233. other._index = _index;
  234. other._nodes = _nodes;
  235. other._position = _position;
  236. return (DTMAxisIterator) other;
  237. }
  238. public void setDom(DOM dom) {
  239. _dom = dom;
  240. if (dom instanceof DOMEnhancedForDTM) {
  241. _enhancedDOM = (DOMEnhancedForDTM)dom;
  242. }
  243. else if (dom instanceof DOMAdapter) {
  244. DOM idom = ((DOMAdapter)dom).getDOMImpl();
  245. if (idom instanceof DOMEnhancedForDTM) {
  246. _enhancedDOM = (DOMEnhancedForDTM)idom;
  247. }
  248. }
  249. }
  250. }