1 21 22 package nu.xom; 23 24 35 public class Document extends ParentNode { 36 37 49 public Document(Element root) { 50 _insertChild(root, 0); 51 } 52 53 54 63 public Document(Document doc) { 64 65 insertChild(doc.getRootElement().copy(), 0); 66 for (int i = 0; i < doc.getChildCount(); i++) { 67 Node child = doc.getChild(i); 68 if (!(child.isElement())) { 69 this.insertChild(child.copy(), i); 70 } 71 } 72 this.actualBaseURI = doc.actualBaseURI; 73 74 } 75 76 77 final void insertionAllowed(Node child, int position) { 78 79 if (child == null) { 80 throw new NullPointerException ( 81 "Tried to insert a null child in the document"); 82 } 83 else if (child.getParent() != null) { 84 throw new MultipleParentException("Child already has a parent."); 85 } 86 else if (child.isComment() || child.isProcessingInstruction()) { 87 return; 88 } 89 else if (child.isDocType()) { 90 if (position <= getRootPosition()) { 91 DocType oldDocType = getDocType(); 92 if (oldDocType != null) { 93 throw new IllegalAddException( 94 "Tried to insert a second DOCTYPE" 95 ); 96 } 97 return; 98 } 99 else { 100 throw new IllegalAddException( 101 "Cannot add a document type declaration " 102 + "after the root element" 103 ); 104 } 105 } 106 else if (child.isElement()) { 107 if (getChildCount() == 0) return; 108 else { 109 throw new IllegalAddException( 110 "Cannot add a second root element to a Document." 111 ); 112 } 113 } 114 else { 115 throw new IllegalAddException("Cannot add a " 116 + child.getClass().getName() + " to a Document."); 117 } 118 119 } 120 121 122 private int getRootPosition() { 123 124 for (int i = 0; ; i++) { 127 Node child = getChild(i); 128 if (child.isElement()) { 129 return i; 130 } 131 } 132 133 } 134 135 136 147 public final DocType getDocType() { 148 149 for (int i = 0; i < getChildCount(); i++) { 150 Node child = getChild(i); 151 if (child.isDocType()) { 152 return (DocType) child; 153 } 154 } 155 return null; 156 157 } 158 159 160 175 public void setDocType(DocType doctype) { 176 177 DocType oldDocType = getDocType(); 178 if (doctype == null) { 179 throw new NullPointerException ("Null DocType"); 180 } 181 else if (doctype == oldDocType) return; 182 else if (doctype.getParent() != null) { 183 throw new MultipleParentException("DocType belongs to another document"); 184 } 185 186 if (oldDocType == null) insertChild(doctype, 0); 187 else { 188 int position = indexOf(oldDocType); 189 children.remove(position); 190 children.add(position, doctype); 191 oldDocType.setParent(null); 192 doctype.setParent(this); 193 } 194 195 } 196 197 198 206 public final Element getRootElement() { 207 208 for (int i = 0; ; i++) { 211 Node child = getChild(i); 212 if (child.isElement()) { 213 return (Element) child; 214 } 215 } 216 217 } 218 219 220 230 public void setRootElement(Element root) { 231 232 Element oldRoot = this.getRootElement(); 233 if (root == oldRoot) return; 234 else if (root == null) { 235 throw new NullPointerException ("Root element cannot be null"); 236 } 237 else if (root.getParent() != null) { 238 throw new MultipleParentException(root.getQualifiedName() 239 + " already has a parent"); 240 } 241 242 fillInBaseURI(oldRoot); 243 int index = indexOf(oldRoot); 244 245 oldRoot.setParent(null); 246 children.remove(index); 247 children.add(index, root); 248 root.setParent(this); 249 250 } 251 252 253 266 public void setBaseURI(String URI) { 267 setActualBaseURI(URI); 268 } 269 270 271 281 public final String getBaseURI() { 282 return getActualBaseURI(); 283 } 284 285 286 304 public Node removeChild(int position) { 305 306 if (position == getRootPosition()) { 307 throw new WellformednessException( 308 "Cannot remove the root element" 309 ); 310 } 311 return super.removeChild(position); 312 313 } 314 315 316 332 public Node removeChild(Node child) { 333 334 if (child == getRootElement()) { 335 throw new WellformednessException( 336 "Cannot remove the root element"); 337 } 338 return super.removeChild(child); 339 340 } 341 342 343 365 public void replaceChild(Node oldChild, Node newChild) { 366 367 if (oldChild == getRootElement() 368 && newChild != null && newChild.isElement()) { 369 setRootElement((Element) newChild); 370 } 371 else if (oldChild == getDocType() 372 && newChild != null && newChild.isDocType()) { 373 setDocType((DocType) newChild); 374 } 375 else { 376 super.replaceChild(oldChild, newChild); 377 } 378 379 } 380 381 382 394 public final String getValue() { 395 return getRootElement().getValue(); 396 } 397 398 399 411 public final String toXML() { 412 413 StringBuffer result = new StringBuffer (); 414 415 result.append("<?xml version=\"1.0\"?>\n"); 417 418 for (int i = 0; i < getChildCount(); i++) { 420 result.append(getChild(i).toXML()); 421 result.append("\n"); 422 } 423 424 return result.toString(); 425 426 } 427 428 429 436 public Node copy() { 437 return new Document(this); 438 } 439 440 441 boolean isDocument() { 442 return true; 443 } 444 445 446 455 public final String toString() { 456 return "[" + getClass().getName() + ": " 457 + getRootElement().getQualifiedName() + "]"; 458 } 459 460 461 } | Popular Tags |