| 1 9 10 package org.ozoneDB.xml.dom4j.o3impl; 11 12 import org.dom4j.*; 13 14 import java.util.ArrayList ; 15 import java.util.Iterator ; 16 import java.util.List ; 17 18 24 25 public class DefaultElement extends AbstractElement { 26 27 28 29 private static final NodeFactory NODE_FACTORY = DocumentFactory.getInstance(); 30 31 32 33 private QName qname; 34 35 40 41 private Branch parentBranch; 42 43 46 47 private Object content; 48 49 50 51 private Object attributes; 52 53 public DefaultElement(String name) { 54 55 this(NODE_FACTORY.createQName(name)); 56 57 } 58 59 public DefaultElement(QName qname) { 60 61 this.qname = qname; 62 63 } 64 65 public DefaultElement(QName qname, int attributeCount) { 66 67 this.qname = qname; 68 69 if (attributeCount > 1) { 70 71 this.attributes = new ArrayList (attributeCount); 72 73 } 74 75 } 76 77 public DefaultElement(String name, Namespace namespace) { 78 79 this(NODE_FACTORY.createQName(name, namespace)); 80 81 } 82 83 public Element getParent() { 84 85 return (parentBranch instanceof Element) ? (Element) parentBranch : null; 86 87 } 88 89 public void setParent(Element parent) { 90 91 if (parentBranch instanceof Element || parent != null) { 92 93 parentBranch = parent; 94 95 } 96 97 } 98 99 public Document getDocument() { 100 101 if (parentBranch instanceof Document) { 102 103 return (Document) parentBranch; 104 105 } else if (parentBranch instanceof Element) { 106 107 Element parent = (Element) parentBranch; 108 109 return parent.getDocument(); 110 111 } 112 113 return null; 114 115 } 116 117 public void setDocument(Document document) { 118 119 if (parentBranch instanceof Document || document != null) { 120 121 parentBranch = document; 122 123 } 124 125 } 126 127 public boolean supportsParent() { 128 129 return true; 130 131 } 132 133 public QName getQName() { 134 135 return qname; 136 137 } 138 139 public void setQName(QName qname) { 140 141 this.qname = qname; 142 143 } 144 145 public String getText() { 146 147 if (content instanceof List ) { 148 149 return super.getText(); 150 151 } else { 152 153 if (content != null) { 154 155 return getContentAsText(content); 156 157 } else { 158 159 return ""; 160 161 } 162 163 } 164 165 } 166 167 public String getStringValue() { 168 169 if (content instanceof List ) { 170 171 List list = (List ) content; 172 173 int size = list.size(); 174 175 if (size > 0) { 176 177 if (size == 1) { 178 179 181 return getContentAsStringValue(list.get(0)); 182 183 } else { 184 185 StringBuffer buffer = new StringBuffer (); 186 187 for (int i = 0; i < size; i++) { 188 189 Object node = list.get(i); 190 191 String string = getContentAsStringValue(node); 192 193 if (string.length() > 0) { 194 195 if (USE_STRINGVALUE_SEPARATOR) { 196 197 if (buffer.length() > 0) { 198 199 buffer.append(' '); 200 201 } 202 203 } 204 205 buffer.append(string); 206 207 } 208 209 } 210 211 return buffer.toString(); 212 213 } 214 215 } 216 217 } else { 218 219 if (content != null) { 220 221 return getContentAsStringValue(content); 222 223 } 224 225 } 226 227 return ""; 228 229 } 230 231 public Object clone() { 232 233 DefaultElement answer = (DefaultElement) super.clone(); 234 235 if (answer != this) { 236 237 answer.content = null; 238 239 answer.attributes = null; 240 241 answer.appendAttributes(this); 242 243 answer.appendContent(this); 244 245 } 246 247 return answer; 248 249 } 250 251 public Namespace getNamespaceForPrefix(String prefix) { 252 if (prefix == null) { 253 prefix = ""; 254 } 255 if (prefix.equals(getNamespacePrefix())) { 256 return getNamespace(); 257 } else if (prefix.equals("xml")) { 258 return getNodeFactory().getXmlNameSpace(); 259 } else { 261 if (content instanceof List ) { 262 List list = (List ) content; 263 int size = list.size(); 264 for (int i = 0; i < size; i++) { 265 Object object = list.get(i); 266 if (object instanceof Namespace) { 267 Namespace namespace = (Namespace) object; 268 if (prefix.equals(namespace.getPrefix())) { 269 return namespace; 270 } 271 } 272 } 273 } else if (content instanceof Namespace) { 274 Namespace namespace = (Namespace) content; 275 if (prefix.equals(namespace.getPrefix())) { 276 return namespace; 277 } 278 } 279 } 280 Element parent = getParent(); 281 if (parent != null) { 282 Namespace answer = parent.getNamespaceForPrefix(prefix); 283 if (answer != null) { 284 return answer; 285 } 286 } 287 if (prefix == null || prefix.length() <= 0) { 288 return getNodeFactory().getNoNamespace(); 289 } 291 return null; 292 } 293 294 public Namespace getNamespaceForURI(String uri) { 295 if (uri == null || uri.length() <= 0) { 296 return getNodeFactory().getNoNamespace(); 297 } else if (uri.equals(getNamespaceURI())) { 299 return getNamespace(); 300 } else { 301 if (content instanceof List ) { 302 List list = (List ) content; 303 int size = list.size(); 304 for (int i = 0; i < size; i++) { 305 Object object = list.get(i); 306 if (object instanceof Namespace) { 307 Namespace namespace = (Namespace) object; 308 if (uri.equals(namespace.getURI())) { 309 return namespace; 310 } 311 } 312 } 313 } else if (content instanceof Namespace) { 314 Namespace namespace = (Namespace) content; 315 if (uri.equals(namespace.getURI())) { 316 return namespace; 317 } 318 } 319 Element parent = getParent(); 320 if (parent != null) { 321 return parent.getNamespaceForURI(uri); 322 } 323 return null; 324 } 325 } 326 327 public List declaredNamespaces() { 328 BackedList answer = createResultList(); 329 if (getNamespaceURI().length() > 0) { 330 answer.addLocal(getNamespace()); 331 } 332 if (content instanceof List ) { 333 List list = (List ) content; 334 int size = list.size(); 335 for (int i = 0; i < size; i++) { 336 Object object = list.get(i); 337 if (object instanceof Namespace) { 338 answer.addLocal(object); 339 } 340 } 341 } else { 342 if (content instanceof Namespace) { 343 answer.addLocal(content); 344 } 345 } 346 return answer; 347 } 348 349 public List additionalNamespaces() { 350 if (content instanceof List ) { 351 List list = (List ) content; 352 int size = list.size(); 353 BackedList answer = createResultList(); 354 for (int i = 0; i < size; i++) { 355 Object object = list.get(i); 356 if (object instanceof Namespace) { 357 Namespace namespace = (Namespace) object; 358 answer.addLocal(namespace); 359 } 360 } 361 return answer; 362 } else { 363 if (content instanceof Namespace) { 364 Namespace namespace = (Namespace) content; 365 return createSingleResultList(namespace); 366 } else { 367 return createEmptyList(); 368 } 369 } 370 } 371 372 public List additionalNamespaces(String defaultNamespaceURI) { 373 if (content instanceof List ) { 374 List list = (List ) content; 375 BackedList answer = createResultList(); 376 int size = list.size(); 377 for (int i = 0; i < size; i++) { 378 Object object = list.get(i); 379 if (object instanceof Namespace) { 380 Namespace namespace = (Namespace) object; 381 if (!defaultNamespaceURI.equals(namespace.getURI())) { 382 answer.addLocal(namespace); 383 } 384 } 385 } 386 return answer; 387 } else { 388 if (content instanceof Namespace) { 389 Namespace namespace = (Namespace) content; 390 if (!defaultNamespaceURI.equals(namespace.getURI())) { 391 return createSingleResultList(namespace); 392 } 393 } 394 } 395 return createEmptyList(); 396 } 397 398 public List processingInstructions() { 400 if (content instanceof List ) { 401 List list = (List ) content; 402 BackedList answer = createResultList(); 403 int size = list.size(); 404 for (int i = 0; i < size; i++) { 405 Object object = list.get(i); 406 if (object instanceof ProcessingInstruction) { 407 answer.addLocal(object); 408 } 409 } 410 return answer; 411 } else { 412 if (content instanceof ProcessingInstruction) { 413 return createSingleResultList(content); 414 } 415 return createEmptyList(); 416 } 417 } 418 419 public List processingInstructions(String target) { 420 if (content instanceof List ) { 421 List list = (List ) content; 422 BackedList answer = createResultList(); 423 int size = list.size(); 424 for (int i = 0; i < size; i++) { 425 Object object = list.get(i); 426 if (object instanceof ProcessingInstruction) { 427 ProcessingInstruction pi = (ProcessingInstruction) object; 428 if (target.equals(pi.getName())) { 429 answer.addLocal(pi); 430 } 431 } 432 } 433 return answer; 434 } else { 435 if (content instanceof ProcessingInstruction) { 436 ProcessingInstruction pi = (ProcessingInstruction) content; 437 if (target.equals(pi.getName())) { 438 return createSingleResultList(pi); 439 } 440 } 441 return createEmptyList(); 442 } 443 } 444 445 public ProcessingInstruction processingInstruction(String target) { 446 if (content instanceof List ) { 447 List list = (List ) content; 448 int size = list.size(); 449 for (int i = 0; i < size; i++) { 450 Object object = list.get(i); 451 if (object instanceof ProcessingInstruction) { 452 ProcessingInstruction pi = (ProcessingInstruction) object; 453 if (target.equals(pi.getName())) { 454 return pi; 455 } 456 } 457 } 458 } else { 459 if (content instanceof ProcessingInstruction) { 460 ProcessingInstruction pi = (ProcessingInstruction) content; 461 if (target.equals(pi.getName())) { 462 return pi; 463 } 464 } 465 } 466 return null; 467 } 468 469 public boolean removeProcessingInstruction(String target) { 470 if (content instanceof List ) { 471 List list = (List ) content; 472 for (Iterator iter = list.iterator(); iter.hasNext();) { 473 Object object = iter.next(); 474 if (object instanceof ProcessingInstruction) { 475 ProcessingInstruction pi = (ProcessingInstruction) object; 476 if (target.equals(pi.getName())) { 477 iter.remove(); 478 return true; 479 } 480 } 481 } 482 } else { 483 if (content instanceof ProcessingInstruction) { 484 ProcessingInstruction pi = (ProcessingInstruction) content; 485 if (target.equals(pi.getName())) { 486 content = null; 487 return true; 488 } 489 } 490 } 491 return false; 492 } 493 494 public Element element(String name) { 495 if (content instanceof List ) { 496 List list = (List ) content; 497 int size = list.size(); 498 for (int i = 0; i < size; i++) { 499 Object object = list.get(i); 500 if (object instanceof Element) { 501 Element element = (Element) object; 502 if (name.equals(element.getName())) { 503 return element; 504 } 505 } 506 } 507 } else { 508 if (content instanceof Element) { 509 Element element = (Element) content; 510 if (name.equals(element.getName())) { 511 return element; 512 } 513 } 514 } 515 return null; 516 } 517 518 public Element element(QName qName) { 519 if (content instanceof List ) { 520 List list = (List ) content; 521 int size = list.size(); 522 for (int i = 0; i < size; i++) { 523 Object object = list.get(i); 524 if (object instanceof Element) { 525 Element element = (Element) object; 526 if (qName.equals(element.getQName())) { 527 return element; 528 } 529 } 530 } 531 } else { 532 if (content instanceof Element) { 533 Element element = (Element) content; 534 if (qName.equals(element.getQName())) { 535 return element; 536 } 537 } 538 } 539 return null; 540 } 541 542 public Element element(String name, Namespace namespace) { 543 return element(getNodeFactory().createQName(name, namespace)); 544 } 545 546 public List elements() { 547 if (content instanceof List ) { 548 List list = (List ) content; 549 BackedList answer = createResultList(); 550 int size = list.size(); 551 for (int i = 0; i < size; i++) { 552 Object object = list.get(i); 553 if (object instanceof Element) { 554 answer.addLocal(object); 555 } 556 } 557 return answer; 558 } else { 559 if (content instanceof Element) { 560 Element element = (Element) content; 561 return createSingleResultList(element); 562 } 563 return createEmptyList(); 564 } 565 } 566 567 public List elements(String name) { 568 if (content instanceof List ) { 569 List list = (List ) content; 570 BackedList answer = createResultList(); 571 int size = list.size(); 572 for (int i = 0; i < size; i++) { 573 Object object = list.get(i); 574 if (object instanceof Element) { 575 Element element = (Element) object; 576 if (name.equals(element.getName())) { 577 answer.addLocal(element); 578 } 579 } 580 } 581 return answer; 582 } else { 583 if (content instanceof Element) { 584 Element element = (Element) content; 585 if (name.equals(element.getName())) { 586 return createSingleResultList(element); 587 } 588 } 589 return createEmptyList(); 590 } 591 } 592 593 public List elements(QName qName) { 594 if (content instanceof List ) { 595 List list = (List ) content; 596 BackedList answer = createResultList(); 597 int size = list.size(); 598 for (int i = 0; i < size; i++) { 599 Object object = list.get(i); 600 if (object instanceof Element) { 601 Element element = (Element) object; 602 if (qName.equals(element.getQName())) { 603 answer.addLocal(element); 604 } 605 } 606 } 607 return answer; 608 } else { 609 if (content instanceof Element) { 610 Element element = (Element) content; 611 if (qName.equals(element.getQName())) { 612 return createSingleResultList(element); 613 } 614 } 615 return createEmptyList(); 616 } 617 } 618 619 public List elements(String name, Namespace namespace) { 620 return elements(getNodeFactory().createQName(name, namespace)); 621 } 622 623 public Iterator elementIterator() { 624 if (content instanceof List ) { 625 List list = (List ) content; 626 return new ElementIterator(list.iterator()); 627 } else { 628 if (content instanceof Element) { 629 Element element = (Element) content; 630 return createSingleIterator(element); 631 } 632 return EMPTY_ITERATOR; 633 } 634 } 635 636 public Iterator elementIterator(String name) { 637 if (content instanceof List ) { 638 List list = (List ) content; 639 return new ElementNameIterator(list.iterator(), name); 640 } else { 641 if (content instanceof Element) { 642 Element element = (Element) content; 643 if (name.equals(element.getName())) { 644 return createSingleIterator(element); 645 } 646 } 647 return EMPTY_ITERATOR; 648 } 649 } 650 651 public Iterator elementIterator(QName qName) { 652 if (content instanceof List ) { 653 List list = (List ) content; 654 return new ElementQNameIterator(list.iterator(), qName); 655 } else { 656 if (content instanceof Element) { 657 Element element = (Element) content; 658 if (qName.equals(element.getQName())) { 659 return createSingleIterator(element); 660 } 661 } 662 return EMPTY_ITERATOR; 663 } 664 } 665 666 public Iterator elementIterator(String name, Namespace namespace) { 667 return elementIterator(getNodeFactory().createQName(name, namespace)); 668 } 669 670 public void setContent(List content) { 671 if (content instanceof ContentListFacade) { 672 content = ((ContentListFacade) content).getBackingList(); 673 } 674 if (content == null) { 675 this.content = null; 676 } else { 677 int size = content.size(); 678 List newContent = createContentList(size); 679 for (int i = 0; i < size; i++) { 680 Object object = content.get(i); 681 if (object instanceof Node) { 682 Node node = (Node) object; 683 Element parent = node.getParent(); 684 if (parent != null && parent != this) { 685 node = (Node) node.clone(); 686 } 687 newContent.add(node); 688 childAdded(node); 689 } else if (object != null) { 690 String text = object.toString(); 691 Node node = getNodeFactory().createText(text); 692 newContent.add(node); 693 childAdded(node); 694 } 695 } 696 contentRemoved(); 697 this.content = newContent; 698 } 699 } 700 701 public void clearContent() { 702 if (content != null) { 703 contentRemoved(); 704 } 705 content = null; 706 } 707 708 public Node node(int index) { 709 if (index >= 0) { 710 Object node = content; 711 if (content instanceof List ) { 712 List list = (List ) content; 713 if (index >= list.size()) { 714 System.out.println("index " + index + " is bigger than the contained list size " + list.size() + ", returning null"); 716 return null; 717 } 718 node = list.get(index); 719 } 720 if (node != null) { 721 if (node instanceof Node) { 722 return (Node) node; 723 } else { 724 return new DefaultText(node.toString()); 725 } 726 } 727 } 728 System.out.println("DefaultElement.node() - index is " + index + ", returning null"); 730 return null; 731 } 732 733 public int indexOf(Node node) { 734 if (content instanceof List ) { 735 List list = (List ) content; 736 return list.indexOf(node); 737 } else { 738 return (content != null && content.equals(node)) ? 0 : -1; 739 } 740 } 741 742 public int nodeCount() { 743 if (content instanceof List ) { 744 List list = (List ) content; 745 return list.size(); 746 } else { 747 return (content != null) ? 1 : 0; 748 } 749 } 750 751 public Iterator nodeIterator() { 752 if (content instanceof List ) { 753 List list = (List ) content; 754 return list.iterator(); 755 } else { 756 if (content != null) { 757 return createSingleIterator(content); 758 } else { 759 return EMPTY_ITERATOR; 760 } 761 } 762 } 763 764 public List attributes() { 765 return new ContentListFacade(this, attributeList()); 766 } 767 768 public void setAttributes(List attributes) { 769 this.attributes = attributes; 770 if (attributes instanceof ContentListFacade) { 771 this.attributes = ((ContentListFacade) attributes).getBackingList(); 772 } 773 } 774 775 public Iterator attributeIterator() { 776 if (attributes instanceof List ) { 777 List list = (List ) attributes; 778 return list.iterator(); 779 } else if (attributes != null) { 780 return createSingleIterator(attributes); 781 782 } else { 783 784 return EMPTY_ITERATOR; 785 786 } 787 788 } 789 790 public Attribute attribute(int index) { 791 792 if (attributes instanceof List ) { 793 794 List list = (List ) attributes; 795 796 return (Attribute) list.get(index); 797 798 } else if (attributes != null && index == 0) { 799 800 return (Attribute) attributes; 801 802 } else { 803 804 return null; 805 806 } 807 808 } 809 810 public int attributeCount() { 811 812 if (attributes instanceof List ) { 813 814 List list = (List ) attributes; 815 816 return list.size(); 817 818 } else { 819 820 return (attributes != null) ? 1 : 0; 821 822 } 823 824 } 825 826 public Attribute attribute(String name) { 827 828 if (attributes instanceof List ) { 829 830 List list = (List ) attributes; 831 832 int size = list.size(); 833 834 for (int i = 0; i < size; i++) { 835 836 Attribute attribute = (Attribute) list.get(i); 837 838 if (name.equals(attribute.getName())) { 839 840 return attribute; 841 842 } 843 844 } 845 846 } else if (attributes != null) { 847 848 Attribute attribute = (Attribute) attributes; 849 850 if (name.equals(attribute.getName())) { 851 852 return attribute; 853 854 } 855 856 } 857 858 return null; 859 860 } 861 862 public Attribute attribute(QName qName) { 863 864 if (attributes instanceof List ) { 865 866 List list = (List ) attributes; 867 868 int size = list.size(); 869 870 for (int i = 0; i < size; i++) { 871 872 Attribute attribute = (Attribute) list.get(i); 873 874 if (qName.equals(attribute.getQName())) { 875 876 return attribute; 877 878 } 879 880 } 881 882 } else if (attributes != null) { 883 884 Attribute attribute = (Attribute) attributes; 885 886 if (qName.equals(attribute.getQName())) { 887 888 return attribute; 889 890 } 891 892 } 893 894 return null; 895 896 } 897 898 public Attribute attribute(String name, Namespace namespace) { 899 900 return attribute(getNodeFactory().createQName(name, namespace)); 901 902 } 903 904 public void add(Attribute attribute) { 905 if (attribute.getParent() != null) { 906 String message = 907 "The Attribute already has an existing parent \"" 908 + attribute.getParent().getQualifiedName() 909 + "\""; 910 throw new IllegalAddException(this, attribute, message); 911 } 912 if (attribute.getValue() == null) { 913 Attribute oldAttribute = attribute(attribute.getQName()); 917 if (oldAttribute != null) { 918 remove(oldAttribute); 919 } 920 } else { 921 if (attributes == null) { 922 attributes = attribute; 923 } else { 924 attributeList().add(attribute); 925 } 926 childAdded(attribute); 927 } 928 } 929 930 public boolean remove(Attribute attribute) { 931 boolean answer = false; 932 if (attributes instanceof List ) { 933 List list = (List ) attributes; 934 answer = list.remove(attribute); 935 if (!answer) { 936 Attribute copy = attribute(attribute.getQName()); 938 if (copy != null) { 939 list.remove(copy); 940 answer = true; 941 } 942 } 943 } else if (attributes != null) { 944 if (attribute.equals(attributes)) { 945 attributes = null; 946 answer = true; 947 } else { 948 Attribute other = (Attribute) attributes; 950 if (attribute.getQName().equals(other.getQName())) { 951 attributes = null; 952 answer = true; 953 } 954 } 955 } 956 if (answer) { 957 childRemoved(attribute); 958 } 959 return answer; 960 } 961 962 protected void addNewNode(Node node) { 965 if (content == null) { 966 content = node; 967 } else { 968 if (content instanceof List ) { 969 List list = (List ) content; 970 list.add(node); 971 } else { 972 List list = createContentList(); 973 list.add(content); 974 list.add(node); 975 content = list; 976 } 977 } 978 childAdded(node); 979 } 980 981 protected boolean removeNode(Node node) { 982 983 boolean answer = false; 984 985 if (content != null) { 986 987 if (content == node) { 988 989 content = null; 990 991 answer = true; 992 993 } else if (content instanceof List ) { 994 995 List list = (List ) content; 996 997 answer = list.remove(node); 998 999 } 1000 1001 } 1002 1003 if (answer) { 1004 1005 childRemoved(node); 1006 1007 } 1008 1009 return answer; 1010 1011 } 1012 1013 protected List contentList() { 1014 1015 if (content instanceof List ) { 1016 1017 return (List ) content; 1018 1019 } else { 1020 1021 List list = createContentList(); 1022 1023 if (content != null) { 1024 1025 list.add(content); 1026 1027 } 1028 1029 content = list; 1030 1031 return list; 1032 1033 } 1034 1035 } 1036 1037 protected List attributeList() { 1038 1039 if (attributes instanceof List ) { 1040 1041 return (List ) attributes; 1042 1043 } else if (attributes != null) { 1044 1045 List list = createAttributeList(); 1046 1047 list.add(attributes); 1048 1049 attributes = list; 1050 1051 return list; 1052 1053 } else { 1054 1055 List list = createAttributeList(); 1056 1057 attributes = list; 1058 1059 return list; 1060 1061 } 1062 1063 } 1064 1065 protected List attributeList(int size) { 1066 1067 if (attributes instanceof List ) { 1068 1069 return (List ) attributes; 1070 1071 } else if (attributes != null) { 1072 1073 List list = createAttributeList(size); 1074 1075 list.add(attributes); 1076 1077 attributes = list; 1078 1079 return list; 1080 1081 } else { 1082 1083 List list = createAttributeList(size); 1084 1085 attributes = list; 1086 1087 return list; 1088 1089 } 1090 1091 } 1092 1093 protected void setAttributeList(List attributes) { 1094 1095 this.attributes = attributes; 1096 1097 } 1098 1099} 1100 1101 1145 | Popular Tags |