KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > xml > xmlc > dom > generic > ContainedNodeEnum


1 /*
2  * Enhydra Java Application Server Project
3  *
4  * The contents of this file are subject to the Enhydra Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License on
7  * the Enhydra web site ( http://www.enhydra.org/ ).
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
11  * the License for the specific terms governing rights and limitations
12  * under the License.
13  *
14  * The Initial Developer of the Enhydra Application Server is Lutris
15  * Technologies, Inc. The Enhydra Application Server and portions created
16  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
17  * All Rights Reserved.
18  *
19  * Contributor(s):
20  *
21  * $Id: ContainedNodeEnum.java,v 1.2 2005/01/26 08:29:24 jkjome Exp $
22  */

23
24 package org.enhydra.xml.xmlc.dom.generic;
25
26 import java.util.Enumeration JavaDoc;
27 import java.util.NoSuchElementException JavaDoc;
28
29 import org.w3c.dom.DocumentType JavaDoc;
30 import org.w3c.dom.NamedNodeMap JavaDoc;
31 import org.w3c.dom.Node JavaDoc;
32
33 /**
34  * Enumeration over the nodes contained by another node. This provides a way
35  * of find traversing standard children, attributes and document type entity
36  * and notation nodes in the same way.
37  */

38 public class ContainedNodeEnum implements Enumeration JavaDoc {
39     /**
40      * Types of contained nodes to enumerate. This is both a bit set
41      * and values that define the order contained nodes are traversed.
42      */

43     public static final int NONE = 0x0;
44     public static final int CHILDREN = 0x1;
45     public static final int ATTRIBUTES = 0x2;
46     public static final int ENTITIES = 0x4;
47     public static final int NOTATIONS = 0x8;
48     public static final int ALL = 0xF;
49
50     /** Node who's children are being enumerated */
51     private Node JavaDoc fParent;
52
53     /** Is this a DocumentType node */
54     private boolean fIsDocType;
55
56     /** Filter on types of nodes to process. */
57     private int fFilterSet;
58
59     /** Flag indicating the type of next contained node. */
60     private int fNextNodeType = NONE;
61
62     /** Next contained node */
63     private Node JavaDoc fNextNode;
64
65     /** Node map and index for other than children */
66     private NamedNodeMap JavaDoc fNextNodeMap;
67     private int fNextNodeIdx;
68
69     /** Constructor with filter for node types */
70     public ContainedNodeEnum(Node JavaDoc parent,
71                              int filterSet) {
72         fParent = parent;
73         fIsDocType = (fParent instanceof DocumentType JavaDoc);
74         fFilterSet = filterSet;
75         
76         // Find the first contained node
77
advance();
78     }
79
80     /** Constructor for enumerating all nodes types */
81     public ContainedNodeEnum(Node JavaDoc parent) {
82         this(parent, ALL);
83     }
84
85     /** Does the filter accept the specified type */
86     private boolean accepts(int type) {
87         return ((type & fFilterSet) != 0);
88     }
89
90     /**
91      * Advance to the next contained node. Also handles initialization.
92      */

93     private void advance() {
94         if ((fNextNodeType <= CHILDREN) && accepts(CHILDREN)) {
95             if (fNextNodeType != CHILDREN) {
96                 fNextNode = fParent.getFirstChild();
97             } else {
98                 fNextNode = fNextNode.getNextSibling();
99             }
100             if (fNextNode != null) {
101                 fNextNodeType = CHILDREN;
102                 return; // done
103
}
104         }
105         if ((fNextNodeType <= ATTRIBUTES) && accepts(ATTRIBUTES)) {
106             if (fNextNodeType != ATTRIBUTES) {
107                 fNextNodeMap = fParent.getAttributes();
108                 fNextNodeIdx = 0;
109             } else {
110                 fNextNodeIdx++;
111             }
112             if ((fNextNodeMap != null)
113                 && (fNextNodeIdx < fNextNodeMap.getLength())) {
114                 fNextNodeType = ATTRIBUTES;
115                 fNextNode = fNextNodeMap.item(fNextNodeIdx);
116                 return; // done
117
}
118         }
119         if (fIsDocType && (fNextNodeType <= ENTITIES) && accepts(ENTITIES)) {
120             if (fNextNodeType != ENTITIES) {
121                 fNextNodeMap = ((DocumentType JavaDoc)fParent).getEntities();
122                 fNextNodeIdx = 0;
123             } else {
124                 fNextNodeIdx++;
125             }
126             if ((fNextNodeMap != null)
127                 && (fNextNodeIdx < fNextNodeMap.getLength())) {
128                 fNextNodeType = ENTITIES;
129                 fNextNode = fNextNodeMap.item(fNextNodeIdx);
130                 return; // done
131
}
132         }
133         if (fIsDocType && (fNextNodeType <= NOTATIONS) && accepts(NOTATIONS)) {
134             if (fNextNodeType != NOTATIONS) {
135                 fNextNodeMap = ((DocumentType JavaDoc)fParent).getNotations();
136                 fNextNodeIdx = 0;
137             } else {
138                 fNextNodeIdx++;
139             }
140             if ((fNextNodeMap != null)
141                 && (fNextNodeIdx < fNextNodeMap.getLength())) {
142                 fNextNodeType = NOTATIONS;
143                 fNextNode = fNextNodeMap.item(fNextNodeIdx);
144                 return; // done
145
}
146         }
147         // that' all folks...
148
fNextNodeType = NONE;
149     }
150
151     /** Tests if this enumeration contains more elements. */
152     public boolean hasMoreElements() {
153         return (fNextNodeType != NONE);
154     }
155
156     /** Returns the next node of this enumeration, or null if no more. */
157     public Node JavaDoc nextNode() throws NoSuchElementException JavaDoc {
158         if (fNextNodeType == NONE) {
159             return null;
160         } else {
161             Node JavaDoc node = fNextNode;
162             advance();
163             return node;
164         }
165     }
166     
167     /** Returns the next element of this enumeration */
168     public Object JavaDoc nextElement() throws NoSuchElementException JavaDoc {
169         if (fNextNodeType == NONE) {
170             throw new NoSuchElementException JavaDoc("no more contained nodes");
171         }
172         return nextNode();
173     }
174 }
175
Popular Tags