📄 keyindex.java
字号:
* {@link #gotoMark()}. */ public void setMark() { _markPosition = _position; } /** * Restores the current node remembered by {@link #setMark()}. */ public void gotoMark() { _position = _markPosition; } /** * Performs a comparison of the two heap nodes * * @param heapNode the heap node against which to compare * @return <code>true</code> if and only if the current node for * this heap node is before the current node of the * argument heap node in document order. */ public boolean isLessThan(HeapNode heapNode) { return _node < heapNode._node; } /** * <p>Sets context with respect to which this heap node is * evaluated.</p> * <p>This has no real effect on this kind of heap node. Instead, * the {@link KeyIndexIterator#setStartNode(int)} method should * create new instances of this class to represent the effect of * changing the context.</p> */ public HeapNode setStartNode(int node) { return this; } /** * Reset the heap node back to its beginning. */ public HeapNode reset() { _position = 0; return this; } } /** * Constructor used when the argument to <code>key</code> or * <code>id</code> is not a node set. * * @param keyValue the argument to <code>key</code> or <code>id</code> * cast to a <code>String</code> * @param isKeyIterator indicates whether the constructed iterator * represents a reference to <code>key</code> or * <code>id</code>. */ KeyIndexIterator(String keyValue, boolean isKeyIterator) { _isKeyIterator = isKeyIterator; _keyValue = keyValue; } /** * Constructor used when the argument to <code>key</code> or * <code>id</code> is a node set. * * @param keyValues the argument to <code>key</code> or <code>id</code> * @param isKeyIterator indicates whether the constructed iterator * represents a reference to <code>key</code> or * <code>id</code>. */ KeyIndexIterator(DTMAxisIterator keyValues, boolean isKeyIterator) { _keyValueIterator = keyValues; _isKeyIterator = isKeyIterator; } /** * Retrieve nodes for a particular key value or a particular id * argument value. * * @param root The root node of the document containing the context node * @param keyValue The key value of id string argument value * @return an {@link IntegerArray} of the resulting nodes */ protected IntegerArray lookupNodes(int root, String keyValue) { IntegerArray result = null; // Get mapping from key values/IDs to DTM nodes for this document Hashtable index = (Hashtable)_rootToIndexMap.get(new Integer(root)); if (!_isKeyIterator) { // For id function, tokenize argument as whitespace separated // list of values and look up nodes identified by each ID. final StringTokenizer values = new StringTokenizer(keyValue, " \n\t"); while (values.hasMoreElements()) { final String token = (String) values.nextElement(); IntegerArray nodes = null; // Does the ID map to any node in the document? if (index != null) { nodes = (IntegerArray) index.get(token); } // If input was from W3C DOM, use DOM's getElementById to do // the look-up. if (nodes == null && _enhancedDOM != null && _enhancedDOM.hasDOMSource()) { nodes = getDOMNodeById(token); } // If we found any nodes, merge them into the cumulative // result if (nodes != null) { if (result == null) { result = (IntegerArray)nodes.clone(); } else { result.merge(nodes); } } } } else if (index != null) { // For key function, map key value to nodes result = (IntegerArray) index.get(keyValue); } return result; } /** * Set context node for the iterator. This will cause the iterator * to reset itself, reevaluate arguments to the function, look up * nodes in the input and reinitialize its internal heap. * * @param node the context node * @return A {@link DTMAxisIterator} set to the start of the iteration. */ public DTMAxisIterator setStartNode(int node) { _startNode = node; // If the arugment to the function is a node set, set the // context node on it. if (_keyValueIterator != null) { _keyValueIterator = _keyValueIterator.setStartNode(node); } init(); return super.setStartNode(node); } /** * Get the next node in the iteration. * * @return The next node handle in the iteration, or END. */ public int next() { int nodeHandle; // If at most one key value or at most one string argument to id // resulted in nodes being returned, use the IntegerArray // stored at _nodes directly. This relies on the fact that the // IntegerArray never includes duplicate nodes and is always stored // in document order. if (_nodes != null) { if (_position < _nodes.cardinality()) { nodeHandle = returnNode(_nodes.at(_position)); } else { nodeHandle = DTMAxisIterator.END; } } else { nodeHandle = super.next(); } return nodeHandle; } /** * Resets the iterator to the last start node. * * @return A DTMAxisIterator, which may or may not be the same as this * iterator. */ public DTMAxisIterator reset() { if (_nodes == null) { init(); } else { super.reset(); } return resetPosition(); } /** * Evaluate the reference to the <code>key</code> or <code>id</code> * function with the context specified by {@link #setStartNode(int)} * and set up this iterator to iterate over the DTM nodes that are * to be returned. */ protected void init() { super.init(); _position = 0; // All nodes retrieved are in the same document int rootHandle = _dom.getAxisIterator(Axis.ROOT) .setStartNode(_startNode).next(); // Is the argument not a node set? if (_keyValueIterator == null) { // Look up nodes returned for the single string argument _nodes = lookupNodes(rootHandle, _keyValue); if (_nodes == null) { _nodes = EMPTY_NODES; } } else { DTMAxisIterator keyValues = _keyValueIterator.reset(); int retrievedKeyValueIdx = 0; boolean foundNodes = false; _nodes = null; // For each node in the node set argument, get the string value // and look up the nodes returned by key or id for that string // value. If at most one string value has nodes associated, // the nodes will be stored in _nodes; otherwise, the nodes // will be placed in a heap. for (int keyValueNode = keyValues.next(); keyValueNode != DTMAxisIterator.END; keyValueNode = keyValues.next()) { String keyValue = BasisLibrary.stringF(keyValueNode, _dom); IntegerArray nodes = lookupNodes(rootHandle, keyValue); if (nodes != null) { if (!foundNodes) { _nodes = nodes; foundNodes = true; } else { if (_nodes != null) { addHeapNode(new KeyIndexHeapNode(_nodes)); _nodes = null; } addHeapNode(new KeyIndexHeapNode(nodes)); } } } if (!foundNodes) { _nodes = EMPTY_NODES; } } } /** * Returns the number of nodes in this iterator. * * @return the number of nodes */ public int getLast() { // If nodes are stored in _nodes, take advantage of the fact that // there are no duplicates. Otherwise, fall back to the base heap // implementaiton and hope it does a good job with this. return (_nodes != null) ? _nodes.cardinality() : super.getLast(); } /** * Return the node at the given position. * * @param position The position * @return The node at the given position. */ public int getNodeByPosition(int position) { int node = DTMAxisIterator.END; // If nodes are stored in _nodes, take advantage of the fact that // there are no duplicates and they are stored in document order. // Otherwise, fall back to the base heap implementation to do a // good job with this. if (_nodes != null) { if (position > 0) { if (position <= _nodes.cardinality()) { _position = position; node = _nodes.at(position-1); } else { _position = _nodes.cardinality(); } } } else { node = super.getNodeByPosition(position); } return node; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -