1 23 24 package org.enhydra.xml.xmlc.dom.generic; 25 26 import java.util.Enumeration ; 27 import java.util.NoSuchElementException ; 28 29 import org.w3c.dom.DocumentType ; 30 import org.w3c.dom.NamedNodeMap ; 31 import org.w3c.dom.Node ; 32 33 38 public class ContainedNodeEnum implements Enumeration { 39 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 51 private Node fParent; 52 53 54 private boolean fIsDocType; 55 56 57 private int fFilterSet; 58 59 60 private int fNextNodeType = NONE; 61 62 63 private Node fNextNode; 64 65 66 private NamedNodeMap fNextNodeMap; 67 private int fNextNodeIdx; 68 69 70 public ContainedNodeEnum(Node parent, 71 int filterSet) { 72 fParent = parent; 73 fIsDocType = (fParent instanceof DocumentType ); 74 fFilterSet = filterSet; 75 76 advance(); 78 } 79 80 81 public ContainedNodeEnum(Node parent) { 82 this(parent, ALL); 83 } 84 85 86 private boolean accepts(int type) { 87 return ((type & fFilterSet) != 0); 88 } 89 90 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; } 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; } 118 } 119 if (fIsDocType && (fNextNodeType <= ENTITIES) && accepts(ENTITIES)) { 120 if (fNextNodeType != ENTITIES) { 121 fNextNodeMap = ((DocumentType )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; } 132 } 133 if (fIsDocType && (fNextNodeType <= NOTATIONS) && accepts(NOTATIONS)) { 134 if (fNextNodeType != NOTATIONS) { 135 fNextNodeMap = ((DocumentType )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; } 146 } 147 fNextNodeType = NONE; 149 } 150 151 152 public boolean hasMoreElements() { 153 return (fNextNodeType != NONE); 154 } 155 156 157 public Node nextNode() throws NoSuchElementException { 158 if (fNextNodeType == NONE) { 159 return null; 160 } else { 161 Node node = fNextNode; 162 advance(); 163 return node; 164 } 165 } 166 167 168 public Object nextElement() throws NoSuchElementException { 169 if (fNextNodeType == NONE) { 170 throw new NoSuchElementException ("no more contained nodes"); 171 } 172 return nextNode(); 173 } 174 } 175 | Popular Tags |