KickJava   Java API By Example, From Geeks To Geeks.

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


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  * The merge iterator.
40  */

41 public class MergeIterator extends NodeIterator {
42   private NodeIterator _baseIterator;
43
44   private SelectedNode []_nodes = new SelectedNode[32];
45   private int _head;
46   private int _tail;
47
48   /**
49    * Creates a merge iterator with a given base.
50    */

51   public MergeIterator(ExprEnvironment env, NodeIterator baseIterator)
52     throws XPathException
53   {
54     super(env);
55
56     _baseIterator = baseIterator;
57
58     SelectedNode node;
59     loop:
60     while ((node = baseIterator.nextSelectedNode()) != null) {
61       if (_tail == _nodes.length) {
62         SelectedNode []newNodes = new SelectedNode[2 * _nodes.length];
63         System.arraycopy(_nodes, 0, newNodes, 0, _nodes.length);
64         _nodes = newNodes;
65       }
66
67       int index = _tail;
68       for (; index > 0; index--) {
69         SelectedNode oldNode = _nodes[index - 1];
70
71         int cmp = node.compareTo(oldNode);
72
73         if (cmp > 0)
74           break;
75         else if (cmp == 0)
76           continue loop;
77       }
78
79       for (int ptr = _tail++; index < ptr; ptr--)
80         _nodes[ptr] = _nodes[ptr - 1];
81           
82       _nodes[index] = node;
83     }
84   }
85
86   /**
87    * True if there are more nodes.
88    */

89   public boolean hasNext()
90   {
91     return _head < _tail;
92   }
93
94   /**
95    * Returns the next node.
96    */

97   public Node JavaDoc nextNode()
98   {
99     if (_head < _tail) {
100       _position = _head + 1;
101       return _nodes[_head++].getNode();
102     }
103     else
104       return null;
105   }
106
107   /**
108    * Returns the next selected.
109    */

110   public SelectedNode nextSelectedNode()
111   {
112     if (_head < _tail) {
113       _position = _head + 1;
114       return _nodes[_head++];
115     }
116     else
117       return null;
118   }
119
120   /**
121    * Returns a clone
122    */

123   public Object JavaDoc clone()
124   {
125     try {
126       MergeIterator clone = new MergeIterator(_env, _baseIterator);
127       clone._head = _head;
128       clone._tail = _tail;
129
130       if (_nodes.length != clone._nodes.length)
131         clone._nodes = new SelectedNode[_nodes.length];
132
133       System.arraycopy(_nodes, 0, clone._nodes, 0, _tail);
134
135       return clone;
136     } catch (Exception JavaDoc e) {
137       log.log(Level.FINE, e.toString(), e);
138
139       return null;
140     }
141   }
142 }
143
144
Popular Tags