KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > test > xml > schema > core > lib > dom > parser > NodeIterator


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.test.xml.schema.core.lib.dom.parser;
21 import java.util.Collection JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.LinkedList JavaDoc;
24 import java.util.TreeMap JavaDoc;
25 import org.w3c.dom.Node JavaDoc;
26
27 /**
28  *
29  * @author ca@netbeans.org
30  */

31 public class NodeIterator {
32     private Iterator JavaDoc m_iterator;
33     private LinkedList JavaDoc<Node JavaDoc> m_trace = new LinkedList JavaDoc<Node JavaDoc>();
34     
35     /** Creates a new instance of NodeIterator */
36     public NodeIterator(TreeMap JavaDoc<String JavaDoc, Node JavaDoc> map) {
37         Collection JavaDoc c = map.values();
38         m_iterator = c.iterator();
39     }
40     
41     public Node JavaDoc next() {
42         boolean bSeekChildren = true;
43         
44         while (true) {
45             if (m_trace.size() == 0) {
46                 Node JavaDoc node = null;
47                 if (m_iterator.hasNext()) {
48                     node = (Node JavaDoc) m_iterator.next();
49                     if (node != null) {
50                         m_trace.add(node);
51                     }
52                 }
53                 return node;
54             } else {
55                 Node JavaDoc node = m_trace.getLast();
56                 Node JavaDoc nextNode = null;
57                 if (node.hasChildNodes() && bSeekChildren) {
58                     nextNode = node.getFirstChild();
59                     m_trace.add(nextNode);
60                 } else {
61                     if (m_trace.size() > 1) {
62                         nextNode = node.getNextSibling();
63                     }
64                     m_trace.removeLast();
65                     if (nextNode != null) {
66                         bSeekChildren = true;
67                         m_trace.add(nextNode);
68                     } else {
69                         bSeekChildren = false;
70                         continue;
71                     }
72                 }
73                 
74                 return nextNode;
75             }
76         }
77     }
78 }
79
Popular Tags