View Javadoc

1   /*
2    * Copyright 2001-2004 (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: BaseElement.java,v 1.7 2004/06/25 08:03:41 maartenc Exp $
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     // Implementation methods
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  * Redistribution and use of this software and associated documentation
152  * ("Software"), with or without modification, are permitted provided
153  * that the following conditions are met:
154  *
155  * 1. Redistributions of source code must retain copyright
156  *    statements and notices.  Redistributions must also contain a
157  *    copy of this document.
158  *
159  * 2. Redistributions in binary form must reproduce the
160  *    above copyright notice, this list of conditions and the
161  *    following disclaimer in the documentation and/or other
162  *    materials provided with the distribution.
163  *
164  * 3. The name "DOM4J" must not be used to endorse or promote
165  *    products derived from this Software without prior written
166  *    permission of MetaStuff, Ltd.  For written permission,
167  *    please contact dom4j-info@metastuff.com.
168  *
169  * 4. Products derived from this Software may not be called "DOM4J"
170  *    nor may "DOM4J" appear in their names without prior written
171  *    permission of MetaStuff, Ltd. DOM4J is a registered
172  *    trademark of MetaStuff, Ltd.
173  *
174  * 5. Due credit should be given to the DOM4J Project - 
175  *    http://www.dom4j.org
176  *
177  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS
178  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
179  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
180  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
181  * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
182  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
183  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
184  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
185  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
186  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
187  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
188  * OF THE POSSIBILITY OF SUCH DAMAGE.
189  *
190  * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved.
191  *
192  * $Id: BaseElement.java,v 1.7 2004/06/25 08:03:41 maartenc Exp $
193  */