KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > xpath > pattern > UnionIterator


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.xpath.pattern;
30
31 import com.caucho.xpath.ExprEnvironment;
32 import com.caucho.xpath.XPathException;
33
34 import org.w3c.dom.Node JavaDoc;
35
36 import java.util.logging.Level JavaDoc;
37
38 /**
39  * Uses the axis to select new nodes.
40  */

41 public class UnionIterator extends NodeIterator {
42   private NodeIterator _leftIter;
43   private NodeIterator _rightIter;
44
45   private Node JavaDoc _node;
46   
47   /**
48    * Creates the new AxisIterator.
49    *
50    * @param leftIter the left iterator
51    * @param rightIter the right iterator
52    */

53   public UnionIterator(ExprEnvironment env,
54                        NodeIterator leftIter, NodeIterator rightIter)
55     throws XPathException
56   {
57     super(env);
58     
59     _leftIter = leftIter;
60     _rightIter = rightIter;
61
62     _node = leftIter.nextNode();
63     if (_node == null) {
64       _leftIter = null;
65       _node = _rightIter.nextNode();
66     }
67   }
68   
69   /**
70    * True if there's more data.
71    */

72   public boolean hasNext()
73   {
74     return _node != null;
75   }
76   
77   /**
78    * Returns the next selected node.
79    */

80   public Node JavaDoc nextNode()
81     throws XPathException
82   {
83     Node JavaDoc next = _node;
84
85     if (next == null)
86       return null;
87
88     if (_leftIter != null) {
89       _node = _leftIter.nextNode();
90       if (_node == null) {
91         _leftIter = null;
92         _node = _rightIter.nextNode();
93       }
94     }
95     else
96       _node = _rightIter.nextNode();
97     
98     return next;
99   }
100
101   /**
102    * Returns a clone of the iterator.
103    */

104   public Object JavaDoc clone()
105   {
106     try {
107       UnionIterator clone;
108       clone = new UnionIterator(_env,
109                                 (NodeIterator) _leftIter.clone(),
110                                 (NodeIterator) _rightIter.clone());
111
112       clone._node = _node;
113       clone._position = _position;
114
115       return clone;
116     } catch (Exception JavaDoc e) {
117       log.log(Level.FINE, e.toString(), e);
118       
119       return null;
120     }
121   }
122 }
123
Popular Tags