1 20 21 25 26 package org.ozoneDB.xml.dom; 27 28 import org.w3c.dom.*; 29 import org.w3c.dom.html.*; 30 32 33 64 public class CollectionImpl implements HTMLCollection { 65 66 67 73 public final int getLength() { 74 return getLength( getTopLevel() ); 76 } 77 78 79 87 public final Node item( int index ) { 88 if (index < 0) { 89 throw new IllegalArgumentException ( "Argument 'index' is negative." ); 90 } 91 return item( getTopLevel(), new CollectionIndex( index ) ); 93 } 94 95 96 105 public final Node namedItem( String name ) { 106 if (name == null) { 107 throw new NullPointerException ( "Argument 'name' is null." ); 108 } 109 return namedItem( getTopLevel(), name ); 111 } 112 113 114 119 private Element getTopLevel() { 120 return _topLevel; 121 } 122 123 124 132 private int getLength( Element topLevel ) { 133 int length; 134 Node node; 135 136 synchronized (topLevel) { 137 length = 0; 140 node = topLevel.getFirstChild(); 141 while (node != null) { 142 if (node instanceof Element) { 145 if (collectionMatch( (Element)node, null )) { 146 ++length; 147 } 148 if (recurse()) { 149 length += getLength( (Element)node ); 150 } 151 } 152 node = node.getNextSibling(); 153 } 154 } 155 return length; 156 } 157 158 159 175 private Node item( Element topLevel, CollectionIndex index ) { 176 Node node; 177 Node result; 178 179 synchronized (topLevel) { 180 node = topLevel.getFirstChild(); 184 while (node != null) { 185 if (node instanceof Element) { 190 if (collectionMatch( (Element)node, null )) { 191 if (index.isZero()) { 192 return node; 193 } 194 index.decrement(); 195 } 196 if (recurse()) { 197 result = item( (Element)node, index ); 198 if (result != null) { 199 return result; 200 } 201 } 202 } 203 node = node.getNextSibling(); 204 } 205 } 206 return null; 207 } 208 209 210 218 private Node namedItem( Element topLevel, String name ) { 219 Node node; 220 Node result; 221 synchronized (topLevel) { 222 node = topLevel.getFirstChild(); 224 while (node != null) { 225 if (node instanceof Element) { 230 if (collectionMatch( (Element)node, name )) { 231 return node; 232 } 233 if (recurse()) { 234 result = namedItem( (Element)node, name ); 235 if (result != null) { 236 return result; 237 } 238 } 239 } 240 241 node = node.getNextSibling(); 242 } 243 return node; 244 } 245 } 246 247 248 256 protected boolean recurse() { 257 return true; 258 } 259 260 261 271 protected boolean collectionMatch( Element elem, String name ) { 272 boolean match; 273 synchronized (elem) { 274 match = elem.getTagName().equals( _lookForTag ); 275 if (match && name != null) { 276 match = name.equals( elem.getAttribute( "id" ) ); 277 } 278 } 279 return match; 280 } 281 282 283 290 public CollectionImpl( Element topLevel, String lookFor ) { 291 if (topLevel == null) { 292 throw new NullPointerException ( "Argument 'topLevel' is null." ); 293 } 294 if (lookFor == null || lookFor.length() == 0) { 295 throw new NullPointerException ( "Argument 'lookFor' is null or an empty string." ); 296 } 297 _topLevel = topLevel; 298 _lookForTag = lookFor; 299 } 300 301 302 308 public CollectionImpl( Element topLevel ) { 309 if (topLevel == null) { 310 throw new NullPointerException ( "Argument 'topLevel' is null." ); 311 } 312 _topLevel = topLevel; 313 } 314 315 318 private Element _topLevel; 319 324 private String _lookForTag; 325 326 } 327 328 329 338 class CollectionIndex { 339 340 341 346 int getIndex() { 347 return _index; 348 } 349 350 351 354 void decrement() { 355 --_index; 356 } 357 358 359 364 boolean isZero() { 365 return _index <= 0; 366 } 367 368 369 375 CollectionIndex( int index ) { 376 _index = index; 377 } 378 379 382 private int _index; 383 384 } 385 | Popular Tags |