1 21 22 package nu.xom; 23 24 import java.util.ArrayList ; 25 26 27 48 public abstract class ParentNode extends Node { 49 50 ArrayList children; 51 String actualBaseURI; 52 53 61 ParentNode() {} 62 63 64 72 public int getChildCount() { 73 if (children == null) return 0; 74 return children.size(); 75 } 76 77 78 107 public void insertChild(Node child, int position) { 108 _insertChild(child, position); 109 } 110 111 112 final void _insertChild(Node child, int position) { 115 insertionAllowed(child, position); 116 fastInsertChild(child, position); 117 } 118 119 120 void fastInsertChild(Node child, int position) { 121 if (children == null) children = new ArrayList (1); 122 children.add(position, child); 123 child.setParent(this); 124 } 125 126 127 abstract void insertionAllowed(Node child, int position); 128 129 130 143 public void appendChild(Node child) { 144 insertChild(child, getChildCount()); 145 } 146 147 148 162 public Node getChild(int position) { 163 164 if (children == null) { 165 throw new IndexOutOfBoundsException ( 166 "This node has no children" 167 ); 168 } 169 return (Node) children.get(position); 170 171 } 172 173 174 176 195 public int indexOf(Node child) { 196 197 if (children == null) return -1; 198 199 211 return children.indexOf(child); 212 213 } 214 215 216 230 public Node removeChild(int position) { 231 232 if (children == null) { 233 throw new IndexOutOfBoundsException ( 234 "This node has no children" 235 ); 236 } 237 Node removed = (Node) children.get(position); 238 if (removed.isElement()) fillInBaseURI((Element) removed); 242 children.remove(position); 243 removed.setParent(null); 244 245 return removed; 246 247 } 248 249 250 void fillInBaseURI(Element removed) { 251 252 ParentNode parent = removed; 253 String actualBaseURI = ""; 254 while (parent != null && actualBaseURI.equals("")) { 255 actualBaseURI = parent.getActualBaseURI(); 256 parent = parent.getParent(); 257 } 258 removed.setActualBaseURI(actualBaseURI); 259 260 } 261 262 263 275 public Node removeChild(Node child) { 276 277 if (children == null) { 278 throw new NoSuchChildException( 279 "Child does not belong to this node" 280 ); 281 } 282 int position = children.indexOf(child); 284 if (position == -1) { 285 throw new NoSuchChildException( 286 "Child does not belong to this node" 287 ); 288 } 289 if (child.isElement()) fillInBaseURI((Element) child); 290 children.remove(position); 291 292 child.setParent(null); 293 294 return child; 295 296 } 297 298 299 317 public void replaceChild(Node oldChild, Node newChild) { 318 319 if (oldChild == null) { 320 throw new NullPointerException ( 321 "Tried to replace null child" 322 ); 323 } 324 if (newChild == null) { 325 throw new NullPointerException ( 326 "Tried to replace child with null" 327 ); 328 } 329 if (children == null) { 330 throw new NoSuchChildException( 331 "Reference node is not a child of this node." 332 ); 333 } 334 int position = children.indexOf(oldChild); 335 if (position == -1) { 336 throw new NoSuchChildException( 337 "Reference node is not a child of this node." 338 ); 339 } 340 341 if (oldChild == newChild) return; 342 343 insertionAllowed(newChild, position); 344 removeChild(position); 345 insertChild(newChild, position); 346 347 } 348 349 350 394 public abstract void setBaseURI(String URI); 395 396 397 String getActualBaseURI() { 398 if (actualBaseURI == null) return ""; 399 return actualBaseURI; 400 } 401 402 403 void setActualBaseURI(String uri) { 404 if (uri == null) uri = ""; 405 if (!"".equals(uri)) Verifier.checkAbsoluteURI(uri); 406 actualBaseURI = uri; 407 } 408 409 410 final String findActualBaseURI() { 411 412 ParentNode current = this; 413 while (true) { 414 String actualBase = current.getActualBaseURI(); 415 ParentNode parent = current.getParent(); 416 417 if (parent == null) return actualBase; 418 419 if ("".equals(actualBase)) { 420 current = parent; 421 continue; 422 } 423 424 return actualBase; 427 } 428 429 } 430 431 432 } 433 | Popular Tags |