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: * Uses the axis to select new nodes.
040: */
041: public class UnionIterator extends NodeIterator {
042: private NodeIterator _leftIter;
043: private NodeIterator _rightIter;
044:
045: private Node _node;
046:
047: /**
048: * Creates the new AxisIterator.
049: *
050: * @param leftIter the left iterator
051: * @param rightIter the right iterator
052: */
053: public UnionIterator(ExprEnvironment env, NodeIterator leftIter,
054: NodeIterator rightIter) throws XPathException {
055: super (env);
056:
057: _leftIter = leftIter;
058: _rightIter = rightIter;
059:
060: _node = leftIter.nextNode();
061: if (_node == null) {
062: _leftIter = null;
063: _node = _rightIter.nextNode();
064: }
065: }
066:
067: /**
068: * True if there's more data.
069: */
070: public boolean hasNext() {
071: return _node != null;
072: }
073:
074: /**
075: * Returns the next selected node.
076: */
077: public Node nextNode() throws XPathException {
078: Node next = _node;
079:
080: if (next == null)
081: return null;
082:
083: if (_leftIter != null) {
084: _node = _leftIter.nextNode();
085: if (_node == null) {
086: _leftIter = null;
087: _node = _rightIter.nextNode();
088: }
089: } else
090: _node = _rightIter.nextNode();
091:
092: return next;
093: }
094:
095: /**
096: * Returns a clone of the iterator.
097: */
098: public Object clone() {
099: try {
100: UnionIterator clone;
101: clone = new UnionIterator(_env, (NodeIterator) _leftIter
102: .clone(), (NodeIterator) _rightIter.clone());
103:
104: clone._node = _node;
105: clone._position = _position;
106:
107: return clone;
108: } catch (Exception e) {
109: log.log(Level.FINE, e.toString(), e);
110:
111: return null;
112: }
113: }
114: }
|