001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.xpath.pattern;
030:
031: import com.caucho.xpath.ExprEnvironment;
032: import com.caucho.xpath.XPathException;
033:
034: import org.w3c.dom.Node;
035:
036: import java.util.logging.Level;
037:
038: /**
039: * The merge iterator.
040: */
041: public class MergeIterator extends NodeIterator {
042: private NodeIterator _baseIterator;
043:
044: private SelectedNode[] _nodes = new SelectedNode[32];
045: private int _head;
046: private int _tail;
047:
048: /**
049: * Creates a merge iterator with a given base.
050: */
051: public MergeIterator(ExprEnvironment env, NodeIterator baseIterator)
052: throws XPathException {
053: super (env);
054:
055: _baseIterator = baseIterator;
056:
057: SelectedNode node;
058: loop: while ((node = baseIterator.nextSelectedNode()) != null) {
059: if (_tail == _nodes.length) {
060: SelectedNode[] newNodes = new SelectedNode[2 * _nodes.length];
061: System.arraycopy(_nodes, 0, newNodes, 0, _nodes.length);
062: _nodes = newNodes;
063: }
064:
065: int index = _tail;
066: for (; index > 0; index--) {
067: SelectedNode oldNode = _nodes[index - 1];
068:
069: int cmp = node.compareTo(oldNode);
070:
071: if (cmp > 0)
072: break;
073: else if (cmp == 0)
074: continue loop;
075: }
076:
077: for (int ptr = _tail++; index < ptr; ptr--)
078: _nodes[ptr] = _nodes[ptr - 1];
079:
080: _nodes[index] = node;
081: }
082: }
083:
084: /**
085: * True if there are more nodes.
086: */
087: public boolean hasNext() {
088: return _head < _tail;
089: }
090:
091: /**
092: * Returns the next node.
093: */
094: public Node nextNode() {
095: if (_head < _tail) {
096: _position = _head + 1;
097: return _nodes[_head++].getNode();
098: } else
099: return null;
100: }
101:
102: /**
103: * Returns the next selected.
104: */
105: public SelectedNode nextSelectedNode() {
106: if (_head < _tail) {
107: _position = _head + 1;
108: return _nodes[_head++];
109: } else
110: return null;
111: }
112:
113: /**
114: * Returns a clone
115: */
116: public Object clone() {
117: try {
118: MergeIterator clone = new MergeIterator(_env, _baseIterator);
119: clone._head = _head;
120: clone._tail = _tail;
121:
122: if (_nodes.length != clone._nodes.length)
123: clone._nodes = new SelectedNode[_nodes.length];
124:
125: System.arraycopy(_nodes, 0, clone._nodes, 0, _tail);
126:
127: return clone;
128: } catch (Exception e) {
129: log.log(Level.FINE, e.toString(), e);
130:
131: return null;
132: }
133: }
134: }
|