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 org.dom4j.Element;
    11  import org.dom4j.Node;
    12
    13  /***
    14   * <p>
    15   * <code>FlyweightEntity</code> is a Flyweight pattern implementation of a
    16   * singly linked, read-only XML entity.
    17   * </p>
    18   *
    19   * <p>
    20   * This node could be shared across documents and elements though it does not
    21   * support the parent relationship.
    22   * </p>
    23   *
    24   * <p>
    25   * Often this node needs to be created and then the text content added later
    26   * (for example in SAX) so this implementation allows a call to {@link#setText}
    27   * providing the entity has no text already.
    28   * </p>
    29   *
    30   * @author <a href="mailto:jstrachan@apache.org">James Strachan </a>
    31   * @version $Revision: 1.6 $
    32   */
    33  public class FlyweightEntity extends AbstractEntity {
    34      /*** The name of the <code>Entity</code> */
    35      protected String name;
    36
    37      /*** The text of the <code>Entity</code> */
    38      protected String text;
    39
    40      /***
    41       * A default constructor for implementors to use.
    42       */
    43      protected FlyweightEntity() {
    44      }
    45
    46      /***
    47       * Creates the <code>Entity</code> with the specified name
    48       *
    49       * @param name
    50       *            is the name of the entity
    51       */
    52      public FlyweightEntity(String name) {
    53          this.name = name;
    54      }
    55
    56      /***
    57       * Creates the <code>Entity</code> with the specified name and text.
    58       *
    59       * @param name
    60       *            is the name of the entity
    61       * @param text
    62       *            is the text of the entity
    63       */
    64      public FlyweightEntity(String name, String text) {
    65          this.name = name;
    66          this.text = text;
    67      }
    68
    69      /***
    70       * DOCUMENT ME!
    71       *
    72       * @return the name of the entity
    73       */
    74      public String getName() {
    75          return name;
    76      }
    77
    78      /***
    79       * DOCUMENT ME!
    80       *
    81       * @return the text of the entity
    82       */
    83      public String getText() {
    84          return text;
    85      }
    86
    87      /***
    88       * sets the value of the entity if it is not defined yet otherwise an
    89       * <code>UnsupportedOperationException</code> is thrown as this class is
    90       * read only.
    91       *
    92       * @param text
    93       *            DOCUMENT ME!
    94       *
    95       * @throws UnsupportedOperationException
    96       *             DOCUMENT ME!
    97       */
    98      public void setText(String text) {
    99          if (this.text != null) {
    100             this.text = text;
    101         } else {
    102             throw new UnsupportedOperationException(
    103                     "This Entity is read-only. " + "It cannot be modified");
    104         }
    105     }
    106
    107     protected Node createXPathResult(Element parent) {
    108         return new DefaultEntity(parent, getName(), getText());
    109     }
    110 }
    111
    112 /*
    113  * Redistribution and use of this software and associated documentation
    114  * ("Software"), with or without modification, are permitted provided that the
    115  * following conditions are met:
    116  *
    117  * 1. Redistributions of source code must retain copyright statements and
    118  * notices. Redistributions must also contain a copy of this document.
    119  *
    120  * 2. Redistributions in binary form must reproduce the above copyright notice,
    121  * this list of conditions and the following disclaimer in the documentation
    122  * and/or other materials provided with the distribution.
    123  *
    124  * 3. The name "DOM4J" must not be used to endorse or promote products derived
    125  * from this Software without prior written permission of MetaStuff, Ltd. For
    126  * written permission, please contact dom4j-info@metastuff.com.
    127  *
    128  * 4. Products derived from this Software may not be called "DOM4J" nor may
    129  * "DOM4J" appear in their names without prior written permission of MetaStuff,
    130  * Ltd. DOM4J is a registered trademark of MetaStuff, Ltd.
    131  *
    132  * 5. Due credit should be given to the DOM4J Project - http://www.dom4j.org
    133  *
    134  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS ``AS IS'' AND
    135  * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    136  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    137  * ARE DISCLAIMED. IN NO EVENT SHALL METASTUFF, LTD. OR ITS CONTRIBUTORS BE
    138  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    139  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    140  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    141  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    142  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    143  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    144  * POSSIBILITY OF SUCH DAMAGE.
    145  *
    146  * Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved.
    147  */