1 56 57 package org.jdom; 58 59 import java.util.*; 60 import org.jdom.filter.*; 61 62 72 public class Document implements Parent { 73 74 private static final String CVS_ID = 75 "@(#) $RCSfile: Document.java,v $ $Revision: 1.84 $ $Date: 2004/08/31 21:47:51 $ $Name: $"; 76 77 83 ContentList content = new ContentList(this); 84 85 88 protected String baseURI = null; 89 90 private HashMap propertyMap = null; 92 93 99 public Document() {} 100 101 115 public Document(Element rootElement, DocType docType, String baseURI) { 116 if (rootElement != null) { 117 setRootElement(rootElement); 118 } 119 if (docType != null) { 120 setDocType(docType); 121 } 122 if (baseURI != null) { 123 setBaseURI(baseURI); 124 } 125 } 126 127 139 public Document(Element rootElement, DocType docType) { 140 this(rootElement, docType, null); 141 } 142 143 153 public Document(Element rootElement) { 154 this(rootElement, null, null); 155 } 156 157 168 public Document(List content) { 169 setContent(content); 170 } 171 172 public int getContentSize() { 173 return content.size(); 174 } 175 176 public int indexOf(Content child) { 177 return content.indexOf(child); 178 } 179 180 197 204 public boolean hasRootElement() { 205 return (content.indexOfFirstElement() < 0) ? false : true; 206 } 207 208 215 public Element getRootElement() { 216 int index = content.indexOfFirstElement(); 217 if (index < 0) { 218 throw new IllegalStateException ("Root element not set"); 219 } 220 return (Element) content.get(index); 221 } 222 223 233 public Document setRootElement(Element rootElement) { 234 int index = content.indexOfFirstElement(); 235 if (index < 0) { 236 content.add(rootElement); 237 } 238 else { 239 content.set(index, rootElement); 240 } 241 return this; 242 } 243 244 249 public Element detachRootElement() { 250 int index = content.indexOfFirstElement(); 251 if (index < 0) 252 return null; 253 return (Element) removeContent(index); 254 } 255 256 263 public DocType getDocType() { 264 int index = content.indexOfDocType(); 265 if (index < 0) { 266 return null; 267 } 268 else { 269 return (DocType) content.get(index); 270 } 271 } 272 273 286 public Document setDocType(DocType docType) { 287 if (docType == null) { 288 int docTypeIndex = content.indexOfDocType(); 290 if (docTypeIndex >= 0) content.remove(docTypeIndex); 291 return this; 292 } 293 294 if (docType.getParent() != null) { 295 throw new IllegalAddException(docType, 296 "The DocType already is attached to a document"); 297 } 298 299 int docTypeIndex = content.indexOfDocType(); 301 if (docTypeIndex < 0) { 302 content.add(0, docType); 303 } 304 else { 305 content.set(docTypeIndex, docType); 306 } 307 308 return this; 309 } 310 311 318 public Document addContent(Content child) { 319 content.add(child); 320 return this; 321 } 322 323 334 public Document addContent(Collection c) { 335 content.addAll(c); 336 return this; 337 } 338 339 349 public Document addContent(int index, Content child) { 350 content.add(index, child); 351 return this; 352 } 353 354 368 public Document addContent(int index, Collection c) { 369 content.addAll(index, c); 370 return this; 371 } 372 373 public List cloneContent() { 374 int size = getContentSize(); 375 List list = new ArrayList(size); 376 for (int i = 0; i < size; i++) { 377 Content child = getContent(i); 378 list.add(child.clone()); 379 } 380 return list; 381 } 382 383 public Content getContent(int index) { 384 return (Content) content.get(index); 385 } 386 387 392 406 public List getContent() { 407 if (!hasRootElement()) 408 throw new IllegalStateException ("Root element not set"); 409 return content; 410 } 411 412 425 public List getContent(Filter filter) { 426 if (!hasRootElement()) 427 throw new IllegalStateException ("Root element not set"); 428 return content.getView(filter); 429 } 430 431 436 public List removeContent() { 437 List old = new ArrayList(content); 438 content.clear(); 439 return old; 440 } 441 442 448 public List removeContent(Filter filter) { 449 List old = new ArrayList(); 450 Iterator itr = content.getView(filter).iterator(); 451 while (itr.hasNext()) { 452 Content child = (Content) itr.next(); 453 old.add(child); 454 itr.remove(); 455 } 456 return old; 457 } 458 459 493 public Document setContent(Collection newContent) { 494 content.clearAndSet(newContent); 495 return this; 496 } 497 498 507 public final void setBaseURI(String uri) { 508 this.baseURI = uri; } 510 511 519 public final String getBaseURI() { 520 return baseURI; 521 } 522 523 537 public Document setContent(int index, Content child) { 538 content.set(index, child); 539 return this; 540 } 541 542 558 public Document setContent(int index, Collection collection) { 559 content.remove(index); 560 content.addAll(index, collection); 561 return this; 562 } 563 564 public boolean removeContent(Content child) { 565 return content.remove(child); 566 } 567 568 public Content removeContent(int index) { 569 return (Content) content.remove(index); 570 } 571 572 599 public Document setContent(Content child) { 600 content.clear(); 601 content.add(child); 602 return this; 603 } 604 605 615 public String toString() { 616 StringBuffer stringForm = new StringBuffer () 617 .append("[Document: "); 618 619 DocType docType = getDocType(); 620 if (docType != null) { 621 stringForm.append(docType.toString()) 622 .append(", "); 623 } else { 624 stringForm.append(" No DOCTYPE declaration, "); 625 } 626 627 Element rootElement = getRootElement(); 628 if (rootElement != null) { 629 stringForm.append("Root is ") 630 .append(rootElement.toString()); 631 } else { 632 stringForm.append(" No root element"); } 634 635 stringForm.append("]"); 636 637 return stringForm.toString(); 638 } 639 640 648 public final boolean equals(Object ob) { 649 return (ob == this); 650 } 651 652 657 public final int hashCode() { 658 return super.hashCode(); 659 } 660 661 666 public Object clone() { 667 Document doc = null; 668 669 try { 670 doc = (Document) super.clone(); 671 } catch (CloneNotSupportedException ce) { 672 } 674 675 doc.content = new ContentList(doc); 678 679 681 for (int i = 0; i < content.size(); i++) { 682 Object obj = content.get(i); 683 if (obj instanceof Element) { 684 Element element = (Element)((Element)obj).clone(); 685 doc.content.add(element); 686 } 687 else if (obj instanceof Comment) { 688 Comment comment = (Comment)((Comment)obj).clone(); 689 doc.content.add(comment); 690 } 691 else if (obj instanceof ProcessingInstruction) { 692 ProcessingInstruction pi = (ProcessingInstruction) 693 ((ProcessingInstruction)obj).clone(); 694 doc.content.add(pi); 695 } 696 else if (obj instanceof DocType) { 697 DocType dt = (DocType) ((DocType)obj).clone(); 698 doc.content.add(dt); 699 } 700 } 701 702 return doc; 703 } 704 705 710 public Iterator getDescendants() { 711 return new DescendantIterator(this); 712 } 713 714 723 public Iterator getDescendants(Filter filter) { 724 return new FilterIterator(new DescendantIterator(this), filter); 725 } 726 727 public Parent getParent() { 728 return null; } 730 731 732 733 736 public Document getDocument() { 737 return this; 738 } 739 740 748 public void setProperty(String id, Object value) { 749 if (propertyMap == null) { 750 propertyMap = new HashMap(); 751 } 752 propertyMap.put(id, value); 753 } 754 755 763 public Object getProperty(String id) { 764 if (propertyMap == null) return null; 765 return propertyMap.get(id); 766 } 767 } 768 | Popular Tags |