KickJava   Java API By Example, From Geeks To Geeks.

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


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: BaseElement.java,v 1.2 2003/06/10 16:18:36 per_nyfelt Exp $
8  */

9
10 package org.ozoneDB.xml.dom4j.o3impl;
11
12 import org.dom4j.*;
13
14 import java.util.List JavaDoc;
15
16 /** <p><code>BaseElement</code> is a useful base class for implemementation
17  * inheritence of an XML element.</p>
18  *
19  * @author <a HREF="mailto:jstrachan@apache.org">James Strachan</a>
20  * @version $Revision: 1.2 $
21  */

22 public class BaseElement extends AbstractElement {
23
24     /** The <code>QName</code> for this element */
25     private QName qname;
26
27     /** Stores the parent branch of this node which is either a Document
28      * if this element is the root element in a document, or another Element
29      * if it is a child of the root document, or null if it has not been added
30      * to a document yet.
31      */

32     private Branch parentBranch;
33
34     /** List of content nodes. */
35     protected List JavaDoc content;
36
37     /** list of attributes */
38     protected List JavaDoc attributes;
39
40
41     public BaseElement(String JavaDoc name) {
42         this.qname = getNodeFactory().createQName(name);
43     }
44
45     public BaseElement(QName qname) {
46         this.qname = qname;
47     }
48
49     public BaseElement(String JavaDoc name, Namespace namespace) {
50         this.qname = getNodeFactory().createQName(name, namespace);
51     }
52
53     public Element getParent() {
54         return (parentBranch instanceof Element)
55                 ? (Element) parentBranch : null;
56     }
57
58     public void setParent(Element parent) {
59         if (parentBranch instanceof Element || parent != null) {
60             parentBranch = parent;
61         }
62     }
63
64     public Document getDocument() {
65         if (parentBranch instanceof Document) {
66             return (Document) parentBranch;
67         } else if (parentBranch instanceof Element) {
68             Element parent = (Element) parentBranch;
69             return parent.getDocument();
70         }
71         return null;
72     }
73
74     public void setDocument(Document document) {
75         if (parentBranch instanceof Document || document != null) {
76             parentBranch = document;
77         }
78     }
79
80     public boolean supportsParent() {
81         return true;
82     }
83
84     public QName getQName() {
85         return qname;
86     }
87
88     public void setQName(QName qname) {
89         this.qname = qname;
90     }
91
92     public void clearContent() {
93         contentList().clear();
94     }
95
96     public void setContent(List JavaDoc content) {
97         this.content = content;
98         if (content instanceof ContentListFacade) {
99             this.content = ((ContentListFacade) content).getBackingList();
100         }
101     }
102
103     public void setAttributes(List JavaDoc attributes) {
104         this.attributes = attributes;
105         if (attributes instanceof ContentListFacade) {
106             this.attributes = ((ContentListFacade) attributes).getBackingList();
107         }
108     }
109
110
111     // Implementation methods
112
//-------------------------------------------------------------------------
113

114     protected List JavaDoc contentList() {
115         if (content == null) {
116             content = createContentList();
117         }
118         return content;
119     }
120
121     protected List JavaDoc attributeList() {
122         if (attributes == null) {
123             attributes = createAttributeList();
124         }
125         return attributes;
126     }
127
128     protected List JavaDoc attributeList(int size) {
129         if (attributes == null) {
130             attributes = createAttributeList(size);
131         }
132         return attributes;
133     }
134
135     protected void setAttributeList(List JavaDoc attributes) {
136         this.attributes = attributes;
137     }
138
139 }
140
141
142 /*
143  * Redistribution and use of this software and associated documentation
144  * ("Software"), with or without modification, are permitted provided
145  * that the following conditions are met:
146  *
147  * 1. Redistributions of source code must retain copyright
148  * statements and notices. Redistributions must also contain a
149  * copy of this document.
150  *
151  * 2. Redistributions in binary form must reproduce the
152  * above copyright notice, this list of conditions and the
153  * following disclaimer in the documentation and/or other
154  * materials provided with the distribution.
155  *
156  * 3. The name "DOM4J" must not be used to endorse or promote
157  * products derived from this Software without prior written
158  * permission of MetaStuff, Ltd. For written permission,
159  * please contact dom4j-info@metastuff.com.
160  *
161  * 4. Products derived from this Software may not be called "DOM4J"
162  * nor may "DOM4J" appear in their names without prior written
163  * permission of MetaStuff, Ltd. DOM4J is a registered
164  * trademark of MetaStuff, Ltd.
165  *
166  * 5. Due credit should be given to the DOM4J Project
167  * (http://dom4j.org/).
168  *
169  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS
170  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
171  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
172  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
173  * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
174  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
175  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
176  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
177  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
178  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
179  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
180  * OF THE POSSIBILITY OF SUCH DAMAGE.
181  *
182  * Copyright 2001 (C) MetaStuff, Ltd. All Rights Reserved.
183  *
184  * $Id: BaseElement.java,v 1.2 2003/06/10 16:18:36 per_nyfelt Exp $
185  */

186
Popular Tags