View Javadoc

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