KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > util > xml > SimpleXMLPathIterator


1 /*****************************************************************************
2  * Source code information
3  * -----------------------
4  * Original author Ian Dickinson, HP Labs Bristol
5  * Author email ian.dickinson@hp.com
6  * Package Jena 2
7  * Web http://sourceforge.net/projects/jena/
8  * Created 04-Dec-2003
9  * Filename $RCSfile: SimpleXMLPathIterator.java,v $
10  * Revision $Revision: 1.5 $
11  * Release status $State: Exp $
12  *
13  * Last modified on $Date: 2005/02/21 12:20:41 $
14  * by $Author: andy_seaborne $
15  *
16  * (c) Copyright 2001, 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
17  * [See end of file]
18  *****************************************************************************/

19
20 // Package
21
///////////////
22
package com.hp.hpl.jena.util.xml;
23
24
25 // Imports
26
///////////////
27
import java.util.*;
28
29 import org.w3c.dom.*;
30
31
32 /**
33  * <p>
34  * An iterator over all of the results of evaluating a simple XML path.
35  * </p>
36  *
37  * @author Ian Dickinson, HP Labs (<a HREF="mailto:Ian.Dickinson@hp.com" >email</a>)
38  * @version CVS $Id: SimpleXMLPathIterator.java,v 1.5 2005/02/21 12:20:41 andy_seaborne Exp $
39  */

40 public class SimpleXMLPathIterator
41     implements Iterator
42 {
43     // Constants
44
//////////////////////////////////
45

46     // Static variables
47
//////////////////////////////////
48

49     // Instance variables
50
//////////////////////////////////
51

52     /** The stack of iterators we use to search down the paths */
53     protected List m_stack;
54     
55     /** The simple path we are evaluating */
56     protected SimpleXMLPath m_path;
57     
58     /** Flag for whether the latest evaluation of the path has been prepared */
59     protected boolean m_prepared = false;
60     
61     /** The value of the final iterator on the path */
62     protected Object JavaDoc m_result;
63     
64     /** The length of the path */
65     protected int m_len;
66     
67     // Constructors
68
//////////////////////////////////
69

70     public SimpleXMLPathIterator( SimpleXMLPath path, Node node ) {
71         m_path = path;
72         m_len = path.getPathComponents().size();
73         m_stack = new ArrayList( m_len );
74         
75         // put the first stage on the stack
76
m_stack.add( path.getPathComponent( 0 ).getAll( node ) );
77         prepare();
78     }
79     
80     
81     // External signature methods
82
//////////////////////////////////
83

84
85     /**
86      * Not supported.
87      */

88     public void remove() {
89         throw new UnsupportedOperationException JavaDoc( "Cannot remove from SimpleXMLPathIterator" );
90     }
91
92     /**
93      * Answer true if there is at least one more value
94      */

95     public boolean hasNext() {
96         prepare();
97         return m_result != null;
98     }
99
100     /**
101      * Answer the next value in the iterator
102      */

103     public Object JavaDoc next() {
104         prepare();
105         
106         if (m_result == null) {
107             throw new NoSuchElementException( "No more values on this SimpleXMLPath" );
108         }
109         
110         m_prepared = false;
111         return m_result;
112     }
113
114
115     // Internal implementation methods
116
//////////////////////////////////
117

118     /** Prepare the next value in the sequence */
119     protected void prepare() {
120         if (!m_prepared) {
121             evaluate();
122             m_prepared = true;
123         }
124     }
125     
126     /**
127      * <p>Evaluate the next traversal through the path, terminating either with a
128      * valid result in m_result, or a failure to find any (more) paths, and m_result
129      * is null.</p>
130      *
131      */

132     protected void evaluate() {
133         // search for a route through to the end of the path
134
int i = 0;
135         m_result = null;
136         
137         // find the tidemark
138
for (; i < min(m_len, m_stack.size()) && (m_stack.get(i) != null); i++);
139         i--;
140         
141         while (i >= 0 && i < min(m_len, m_stack.size())) {
142             Iterator j = (Iterator) m_stack.get( i );
143             
144             if (j == null) {
145                 // go back a stage
146
i--;
147             }
148             else if (!j.hasNext()) {
149                 // finished iterator
150
m_stack.add( i, null );
151                 m_result = null;
152                 i--;
153             }
154             else {
155                 // there is a valid stage here
156
m_result = j.next();
157                 i++;
158                 
159                 if (i < m_len) {
160                     // advance the stack along the path
161
m_stack.add( i, m_path.getPathComponent( i ).getAll( (Node) m_result ) );
162                 }
163             }
164         }
165     }
166
167
168     /** Answer the minimum of two ints */
169     private int min( int x, int y ) {
170         return (x < y) ? x : y;
171     }
172     
173     //==============================================================================
174
// Inner class definitions
175
//==============================================================================
176

177 }
178
179
180 /*
181  * (c) Copyright 2001, 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
182  * All rights reserved.
183  *
184  * Redistribution and use in source and binary forms, with or without
185  * modification, are permitted provided that the following conditions
186  * are met:
187  * 1. Redistributions of source code must retain the above copyright
188  * notice, this list of conditions and the following disclaimer.
189  * 2. Redistributions in binary form must reproduce the above copyright
190  * notice, this list of conditions and the following disclaimer in the
191  * documentation and/or other materials provided with the distribution.
192  * 3. The name of the author may not be used to endorse or promote products
193  * derived from this software without specific prior written permission.
194  *
195  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
196  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
197  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
198  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
199  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
200  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
201  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
202  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
203  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
204  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
205  */

206
Popular Tags