001/*
002 * Copyright 2001 (C) MetaStuff, Ltd. All Rights Reserved.
003 * 
004 * This software is open source. 
005 * See the bottom of this file for the licence.
006 * 
007 * $Id: BaseElement.java,v 1.3 2002/02/01 13:04:32 jstrachan Exp $
008 */
009
010package org.dom4j.tree;
011
012import java.io.IOException;
013import java.io.StringWriter;
014import java.io.PrintWriter;
015import java.util.ArrayList;
016import java.util.Collections;
017import java.util.HashMap;
018import java.util.Iterator;
019import java.util.List;
020import java.util.Map;
021import java.util.StringTokenizer;
022
023import org.dom4j.Attribute;
024import org.dom4j.Branch;
025import org.dom4j.CDATA;
026import org.dom4j.CharacterData;
027import org.dom4j.Comment;
028import org.dom4j.Document;
029import org.dom4j.Element;
030import org.dom4j.Entity;
031import org.dom4j.IllegalAddException;
032import org.dom4j.Node;
033import org.dom4j.Namespace;
034import org.dom4j.QName;
035import org.dom4j.ProcessingInstruction;
036import org.dom4j.Text;
037
038/** <p><code>BaseElement</code> is a useful base class for implemementation
039  * inheritence of an XML element.</p>
040  *
041  * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
042  * @version $Revision: 1.3 $
043  */
044public class BaseElement extends AbstractElement {
045
046    /** The <code>QName</code> for this element */
047    private QName qname;
048    
049    /** Stores the parent branch of this node which is either a Document 
050      * if this element is the root element in a document, or another Element 
051      * if it is a child of the root document, or null if it has not been added
052      * to a document yet. 
053       */
054    private Branch parentBranch;
055
056    /** List of content nodes. */
057    protected List content;
058    
059    /** list of attributes */
060    protected List attributes;
061
062    
063    
064    public BaseElement(String name) { 
065        this.qname = getDocumentFactory().createQName(name);
066    }
067
068    public BaseElement(QName qname) { 
069        this.qname = qname;
070    }
071
072    public BaseElement(String name,Namespace namespace) { 
073        this.qname = getDocumentFactory().createQName(name, namespace);
074    }
075
076    public Element getParent() {
077        return ( parentBranch instanceof Element ) 
078            ? (Element) parentBranch : null;
079    }
080
081    public void setParent(Element parent) {
082        if ( parentBranch instanceof Element || parent != null ) {
083            parentBranch = parent;
084        }
085    }
086
087    public Document getDocument() {
088        if ( parentBranch instanceof Document ) {
089            return (Document) parentBranch;
090        }
091        else if ( parentBranch instanceof Element ) {
092            Element parent = (Element) parentBranch;
093            return parent.getDocument();
094        }
095        return null;
096    }
097    
098    public void setDocument(Document document) {
099        if ( parentBranch instanceof Document || document != null ) {
100            parentBranch = document;
101        }
102    }
103    
104    public boolean supportsParent() {
105        return true;
106    }
107
108    public QName getQName() {
109        return qname;
110    }
111    
112    public void setQName(QName qname) {
113        this.qname = qname;
114    }
115
116    public void clearContent() {
117        contentList().clear();
118    }
119    
120    public void setContent(List content) {
121        this.content = content;
122        if ( content instanceof ContentListFacade ) {
123            this.content = ((ContentListFacade) content).getBackingList();
124        }
125    }
126    
127    public void setAttributes(List attributes) {
128        this.attributes = attributes;
129        if ( attributes instanceof ContentListFacade ) {
130            this.attributes = ((ContentListFacade) attributes).getBackingList();
131        }
132    }
133    
134    
135    // Implementation methods
136    //-------------------------------------------------------------------------    
137
138    protected List contentList() {
139        if ( content == null ) {
140            content = createContentList();
141        }
142        return content;
143    }
144
145    protected List attributeList() {
146        if ( attributes == null ) {
147            attributes = createAttributeList();
148        }
149        return attributes;
150    }
151    
152    protected List attributeList(int size) {
153        if ( attributes == null ) {
154            attributes = createAttributeList(size);
155        }
156        return attributes;
157    }
158    
159    protected void setAttributeList(List attributes) {
160        this.attributes = attributes;
161    }
162
163}
164
165
166
167
168/*
169 * Redistribution and use of this software and associated documentation
170 * ("Software"), with or without modification, are permitted provided
171 * that the following conditions are met:
172 *
173 * 1. Redistributions of source code must retain copyright
174 *    statements and notices.  Redistributions must also contain a
175 *    copy of this document.
176 *
177 * 2. Redistributions in binary form must reproduce the
178 *    above copyright notice, this list of conditions and the
179 *    following disclaimer in the documentation and/or other
180 *    materials provided with the distribution.
181 *
182 * 3. The name "DOM4J" must not be used to endorse or promote
183 *    products derived from this Software without prior written
184 *    permission of MetaStuff, Ltd.  For written permission,
185 *    please contact dom4j-info@metastuff.com.
186 *
187 * 4. Products derived from this Software may not be called "DOM4J"
188 *    nor may "DOM4J" appear in their names without prior written
189 *    permission of MetaStuff, Ltd. DOM4J is a registered
190 *    trademark of MetaStuff, Ltd.
191 *
192 * 5. Due credit should be given to the DOM4J Project
193 *    (http://dom4j.org/).
194 *
195 * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS
196 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
197 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
198 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
199 * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
200 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
201 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
202 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
203 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
204 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
205 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
206 * OF THE POSSIBILITY OF SUCH DAMAGE.
207 *
208 * Copyright 2001 (C) MetaStuff, Ltd. All Rights Reserved.
209 *
210 * $Id: BaseElement.java,v 1.3 2002/02/01 13:04:32 jstrachan Exp $
211 */