1
2
3
4
5
6
7
8
9
10 package org.dom4j.tree;
11
12 import java.util.List;
13
14 import org.dom4j.Branch;
15 import org.dom4j.Document;
16 import org.dom4j.Element;
17 import org.dom4j.Namespace;
18 import org.dom4j.QName;
19
20 /*** <p><code>BaseElement</code> is a useful base class for implemementation
21 * inheritence of an XML element.</p>
22 *
23 * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
24 * @version $Revision: 1.7 $
25 */
26 public class BaseElement extends AbstractElement {
27
28 /*** The <code>QName</code> for this element */
29 private QName qname;
30
31 /*** Stores the parent branch of this node which is either a Document
32 * if this element is the root element in a document, or another Element
33 * if it is a child of the root document, or null if it has not been added
34 * to a document yet.
35 */
36 private Branch parentBranch;
37
38 /*** List of content nodes. */
39 protected List content;
40
41 /*** list of attributes */
42 protected List attributes;
43
44
45
46 public BaseElement(String name) {
47 this.qname = getDocumentFactory().createQName(name);
48 }
49
50 public BaseElement(QName qname) {
51 this.qname = qname;
52 }
53
54 public BaseElement(String name,Namespace namespace) {
55 this.qname = getDocumentFactory().createQName(name, namespace);
56 }
57
58 public Element getParent() {
59 return ( parentBranch instanceof Element )
60 ? (Element) parentBranch : null;
61 }
62
63 public void setParent(Element parent) {
64 if ( parentBranch instanceof Element || parent != null ) {
65 parentBranch = parent;
66 }
67 }
68
69 public Document getDocument() {
70 if ( parentBranch instanceof Document ) {
71 return (Document) parentBranch;
72 }
73 else if ( parentBranch instanceof Element ) {
74 Element parent = (Element) parentBranch;
75 return parent.getDocument();
76 }
77 return null;
78 }
79
80 public void setDocument(Document document) {
81 if ( parentBranch instanceof Document || document != null ) {
82 parentBranch = document;
83 }
84 }
85
86 public boolean supportsParent() {
87 return true;
88 }
89
90 public QName getQName() {
91 return qname;
92 }
93
94 public void setQName(QName qname) {
95 this.qname = qname;
96 }
97
98 public void clearContent() {
99 contentList().clear();
100 }
101
102 public void setContent(List content) {
103 this.content = content;
104 if ( content instanceof ContentListFacade ) {
105 this.content = ((ContentListFacade) content).getBackingList();
106 }
107 }
108
109 public void setAttributes(List attributes) {
110 this.attributes = attributes;
111 if ( attributes instanceof ContentListFacade ) {
112 this.attributes = ((ContentListFacade) attributes).getBackingList();
113 }
114 }
115
116
117
118
119
120 protected List contentList() {
121 if ( content == null ) {
122 content = createContentList();
123 }
124 return content;
125 }
126
127 protected List attributeList() {
128 if ( attributes == null ) {
129 attributes = createAttributeList();
130 }
131 return attributes;
132 }
133
134 protected List attributeList(int size) {
135 if ( attributes == null ) {
136 attributes = createAttributeList(size);
137 }
138 return attributes;
139 }
140
141 protected void setAttributeList(List attributes) {
142 this.attributes = attributes;
143 }
144
145 }
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193