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