KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ozoneDB > xml > dom4j > o3impl > DefaultElement


1 /*
2  * Copyright 2001 (C) MetaStuff, Ltd. All Rights Reserved.
3  *
4  * This software is open source.
5  * See the bottom of this file for the licence.
6  *
7  * $Id: DefaultElement.java,v 1.1 2003/11/02 18:10:03 per_nyfelt Exp $
8  */

9
10 package org.ozoneDB.xml.dom4j.o3impl;
11
12 import org.dom4j.*;
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.List JavaDoc;
17
18 /** <p><code>DefaultElement</code> is the default DOM4J default implementation
19  * of an XML element.</p>
20  *
21  * @author <a HREF="mailto:jstrachan@apache.org">James Strachan</a>
22  * @version $Revision: 1.1 $
23  */

24
25 public class DefaultElement extends AbstractElement {
26
27     /** The <code>NodeFactory</code> instance used by default */
28
29     private static final NodeFactory NODE_FACTORY = DocumentFactory.getInstance();
30
31     /** The <code>QName</code> for this element */
32
33     private QName qname;
34
35     /** Stores the parent branch of this node which is either a Document
36      * if this element is the root element in a document, or another Element
37      * if it is a child of the root document, or null if it has not been added
38      * to a document yet.
39      */

40
41     private Branch parentBranch;
42
43     /** Stores null for no content, a Node for a single content node
44      * or a List for multiple content nodes.
45      * The List will be lazily constructed when required. */

46
47     private Object JavaDoc content;
48
49     /** Lazily constructes list of attributes */
50
51     private Object JavaDoc attributes;
52
53     public DefaultElement(String JavaDoc 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 JavaDoc(attributeCount);
72
73         }
74
75     }
76
77     public DefaultElement(String JavaDoc 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 JavaDoc getText() {
146
147         if (content instanceof List JavaDoc) {
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 JavaDoc getStringValue() {
168
169         if (content instanceof List JavaDoc) {
170
171             List JavaDoc list = (List JavaDoc) content;
172
173             int size = list.size();
174
175             if (size > 0) {
176
177                 if (size == 1) {
178
179                     // optimised to avoid StringBuffer creation
180

181                     return getContentAsStringValue(list.get(0));
182
183                 } else {
184
185                     StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
186
187                     for (int i = 0; i < size; i++) {
188
189                         Object JavaDoc node = list.get(i);
190
191                         String JavaDoc 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 JavaDoc 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 JavaDoc 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             //return AbstractNamespace.XML_NAMESPACE;
260
} else {
261             if (content instanceof List JavaDoc) {
262                 List JavaDoc list = (List JavaDoc) content;
263                 int size = list.size();
264                 for (int i = 0; i < size; i++) {
265                     Object JavaDoc 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             //return AbstractNamespace.NO_NAMESPACE;
290
}
291         return null;
292     }
293
294     public Namespace getNamespaceForURI(String JavaDoc uri) {
295         if (uri == null || uri.length() <= 0) {
296             return getNodeFactory().getNoNamespace();
297             //return AbstractNamespace.NO_NAMESPACE;
298
} else if (uri.equals(getNamespaceURI())) {
299             return getNamespace();
300         } else {
301             if (content instanceof List JavaDoc) {
302                 List JavaDoc list = (List JavaDoc) content;
303                 int size = list.size();
304                 for (int i = 0; i < size; i++) {
305                     Object JavaDoc 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 JavaDoc declaredNamespaces() {
328         BackedList answer = createResultList();
329         if (getNamespaceURI().length() > 0) {
330             answer.addLocal(getNamespace());
331         }
332         if (content instanceof List JavaDoc) {
333             List JavaDoc list = (List JavaDoc) content;
334             int size = list.size();
335             for (int i = 0; i < size; i++) {
336                 Object JavaDoc 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 JavaDoc additionalNamespaces() {
350         if (content instanceof List JavaDoc) {
351             List JavaDoc list = (List JavaDoc) content;
352             int size = list.size();
353             BackedList answer = createResultList();
354             for (int i = 0; i < size; i++) {
355                 Object JavaDoc 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 JavaDoc additionalNamespaces(String JavaDoc defaultNamespaceURI) {
373         if (content instanceof List JavaDoc) {
374             List JavaDoc list = (List JavaDoc) content;
375             BackedList answer = createResultList();
376             int size = list.size();
377             for (int i = 0; i < size; i++) {
378                 Object JavaDoc 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     // Processing instruction API
399
public List JavaDoc processingInstructions() {
400         if (content instanceof List JavaDoc) {
401             List JavaDoc list = (List JavaDoc) content;
402             BackedList answer = createResultList();
403             int size = list.size();
404             for (int i = 0; i < size; i++) {
405                 Object JavaDoc 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 JavaDoc processingInstructions(String JavaDoc target) {
420         if (content instanceof List JavaDoc) {
421             List JavaDoc list = (List JavaDoc) content;
422             BackedList answer = createResultList();
423             int size = list.size();
424             for (int i = 0; i < size; i++) {
425                 Object JavaDoc 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 JavaDoc target) {
446         if (content instanceof List JavaDoc) {
447             List JavaDoc list = (List JavaDoc) content;
448             int size = list.size();
449             for (int i = 0; i < size; i++) {
450                 Object JavaDoc 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 JavaDoc target) {
470         if (content instanceof List JavaDoc) {
471             List JavaDoc list = (List JavaDoc) content;
472             for (Iterator JavaDoc iter = list.iterator(); iter.hasNext();) {
473                 Object JavaDoc 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 JavaDoc name) {
495         if (content instanceof List JavaDoc) {
496             List JavaDoc list = (List JavaDoc) content;
497             int size = list.size();
498             for (int i = 0; i < size; i++) {
499                 Object JavaDoc 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 JavaDoc) {
520             List JavaDoc list = (List JavaDoc) content;
521             int size = list.size();
522             for (int i = 0; i < size; i++) {
523                 Object JavaDoc 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 JavaDoc name, Namespace namespace) {
543         return element(getNodeFactory().createQName(name, namespace));
544     }
545
546     public List JavaDoc elements() {
547         if (content instanceof List JavaDoc) {
548             List JavaDoc list = (List JavaDoc) content;
549             BackedList answer = createResultList();
550             int size = list.size();
551             for (int i = 0; i < size; i++) {
552                 Object JavaDoc 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 JavaDoc elements(String JavaDoc name) {
568         if (content instanceof List JavaDoc) {
569             List JavaDoc list = (List JavaDoc) content;
570             BackedList answer = createResultList();
571             int size = list.size();
572             for (int i = 0; i < size; i++) {
573                 Object JavaDoc 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 JavaDoc elements(QName qName) {
594         if (content instanceof List JavaDoc) {
595             List JavaDoc list = (List JavaDoc) content;
596             BackedList answer = createResultList();
597             int size = list.size();
598             for (int i = 0; i < size; i++) {
599                 Object JavaDoc 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 JavaDoc elements(String JavaDoc name, Namespace namespace) {
620         return elements(getNodeFactory().createQName(name, namespace));
621     }
622
623     public Iterator JavaDoc elementIterator() {
624         if (content instanceof List JavaDoc) {
625             List JavaDoc list = (List JavaDoc) 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 JavaDoc elementIterator(String JavaDoc name) {
637         if (content instanceof List JavaDoc) {
638             List JavaDoc list = (List JavaDoc) 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 JavaDoc elementIterator(QName qName) {
652         if (content instanceof List JavaDoc) {
653             List JavaDoc list = (List JavaDoc) 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 JavaDoc elementIterator(String JavaDoc name, Namespace namespace) {
667         return elementIterator(getNodeFactory().createQName(name, namespace));
668     }
669
670     public void setContent(List JavaDoc 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 JavaDoc newContent = createContentList(size);
679             for (int i = 0; i < size; i++) {
680                 Object JavaDoc 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 JavaDoc 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 JavaDoc node = content;
711             if (content instanceof List JavaDoc) {
712                 List JavaDoc list = (List JavaDoc) content;
713                 if (index >= list.size()) {
714                     // fixme remove println when TEstContent is working again
715
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         // fixme remove println when TEstContent is working again
729
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 JavaDoc) {
735             List JavaDoc list = (List JavaDoc) 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 JavaDoc) {
744             List JavaDoc list = (List JavaDoc) content;
745             return list.size();
746         } else {
747             return (content != null) ? 1 : 0;
748         }
749     }
750
751     public Iterator JavaDoc nodeIterator() {
752         if (content instanceof List JavaDoc) {
753             List JavaDoc list = (List JavaDoc) 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 JavaDoc attributes() {
765         return new ContentListFacade(this, attributeList());
766     }
767
768     public void setAttributes(List JavaDoc attributes) {
769         this.attributes = attributes;
770         if (attributes instanceof ContentListFacade) {
771             this.attributes = ((ContentListFacade) attributes).getBackingList();
772         }
773     }
774
775     public Iterator JavaDoc attributeIterator() {
776         if (attributes instanceof List JavaDoc) {
777             List JavaDoc list = (List JavaDoc) 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 JavaDoc) {
793
794             List JavaDoc list = (List JavaDoc) 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 JavaDoc) {
813
814             List JavaDoc list = (List JavaDoc) 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 JavaDoc name) {
827
828         if (attributes instanceof List JavaDoc) {
829
830             List JavaDoc list = (List JavaDoc) 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 JavaDoc) {
865
866             List JavaDoc list = (List JavaDoc) 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 JavaDoc 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 JavaDoc 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             // try remove a previous attribute with the same
914
// name since adding an attribute with a null value
915
// is equivalent to removing it.
916
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 JavaDoc) {
933             List JavaDoc list = (List JavaDoc) attributes;
934             answer = list.remove(attribute);
935             if (!answer) {
936                 // we may have a copy of the attribute
937
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                 // we may have a copy of the attribute
949
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     // Implementation methods
963
//-------------------------------------------------------------------------
964
protected void addNewNode(Node node) {
965         if (content == null) {
966             content = node;
967         } else {
968             if (content instanceof List JavaDoc) {
969                 List JavaDoc list = (List JavaDoc) content;
970                 list.add(node);
971             } else {
972                 List JavaDoc 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 JavaDoc) {
994
995                 List JavaDoc list = (List JavaDoc) 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 JavaDoc contentList() {
1014
1015        if (content instanceof List JavaDoc) {
1016
1017            return (List JavaDoc) content;
1018
1019        } else {
1020
1021            List JavaDoc 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 JavaDoc attributeList() {
1038
1039        if (attributes instanceof List JavaDoc) {
1040
1041            return (List JavaDoc) attributes;
1042
1043        } else if (attributes != null) {
1044
1045            List JavaDoc list = createAttributeList();
1046
1047            list.add(attributes);
1048
1049            attributes = list;
1050
1051            return list;
1052
1053        } else {
1054
1055            List JavaDoc list = createAttributeList();
1056
1057            attributes = list;
1058
1059            return list;
1060
1061        }
1062
1063    }
1064
1065    protected List JavaDoc attributeList(int size) {
1066
1067        if (attributes instanceof List JavaDoc) {
1068
1069            return (List JavaDoc) attributes;
1070
1071        } else if (attributes != null) {
1072
1073            List JavaDoc list = createAttributeList(size);
1074
1075            list.add(attributes);
1076
1077            attributes = list;
1078
1079            return list;
1080
1081        } else {
1082
1083            List JavaDoc list = createAttributeList(size);
1084
1085            attributes = list;
1086
1087            return list;
1088
1089        }
1090
1091    }
1092
1093    protected void setAttributeList(List JavaDoc attributes) {
1094
1095        this.attributes = attributes;
1096
1097    }
1098
1099}
1100
1101/*
1102 * Redistribution and use of this software and associated documentation
1103 * ("Software"), with or without modification, are permitted provided
1104 * that the following conditions are met:
1105 *
1106 * 1. Redistributions of source code must retain copyright
1107 * statements and notices. Redistributions must also contain a
1108 * copy of this document.
1109 *
1110 * 2. Redistributions in binary form must reproduce the
1111 * above copyright notice, this list of conditions and the
1112 * following disclaimer in the documentation and/or other
1113 * materials provided with the distribution.
1114 *
1115 * 3. The name "DOM4J" must not be used to endorse or promote
1116 * products derived from this Software without prior written
1117 * permission of MetaStuff, Ltd. For written permission,
1118 * please contact dom4j-info@metastuff.com.
1119 *
1120 * 4. Products derived from this Software may not be called "DOM4J"
1121 * nor may "DOM4J" appear in their names without prior written
1122 * permission of MetaStuff, Ltd. DOM4J is a registered
1123 * trademark of MetaStuff, Ltd.
1124 *
1125 * 5. Due credit should be given to the DOM4J Project
1126 * (http://dom4j.org/).
1127 *
1128 * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS
1129 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
1130 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
1131 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
1132 * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
1133 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
1134 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
1135 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1136 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
1137 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
1138 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
1139 * OF THE POSSIBILITY OF SUCH DAMAGE.
1140 *
1141 * Copyright 2001 (C) MetaStuff, Ltd. All Rights Reserved.
1142 *
1143 * $Id: DefaultElement.java,v 1.1 2003/11/02 18:10:03 per_nyfelt Exp $
1144 */

1145
Popular Tags