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: FlyweightEntity.java,v 1.1 2001/06/20 18:59:23 jstrachan Exp $
008 */
009
010package org.dom4j.tree;
011
012import org.dom4j.Element;
013import org.dom4j.Node;
014
015/** <p><code>FlyweightEntity</code> is a Flyweight pattern implementation
016  * of a singly linked, read-only XML entity.</p>
017  *
018  * <p>This node could be shared across documents and elements though 
019  * it does not support the parent relationship.</p>
020  *
021  * <p>Often this node needs to be created and then the text content added
022  * later (for example in SAX) so this implementation allows a call to 
023  * {@link #setText} providing the entity has no text already.
024  *
025  * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
026  * @version $Revision: 1.1 $
027  */
028public class FlyweightEntity extends AbstractEntity {
029
030    /** The name of the <code>Entity</code> */
031    protected String name;
032
033    /** The text of the <code>Entity</code> */
034    protected String text;
035
036    /** A default constructor for implementors to use.
037      */
038    protected FlyweightEntity() {
039    }
040
041    /** Creates the <code>Entity</code> with the specified name
042      *
043      * @param name is the name of the entity
044      */
045    public FlyweightEntity(String name) {
046        this.name = name;
047    }
048
049    /** Creates the <code>Entity</code> with the specified name
050      * and text.
051      *
052      * @param name is the name of the entity
053      * @param text is the text of the entity
054      */
055    public FlyweightEntity(String name,String text) {
056        this.name = name;
057        this.text = text;
058    }
059
060    /** @return the name of the entity
061      */
062    public String getName() {
063        return name;
064    }
065
066    /** @return the text of the entity
067      */
068    public String getText() {
069        return text;
070    }
071    
072    /** sets the value of the entity if it is not defined yet
073      * otherwise an <code>UnsupportedOperationException</code> is thrown
074      * as this class is read only.
075      */
076    public void setText(String text) {
077        if (this.text != null) {
078            this.text = text;
079        }
080        else {
081            throw new UnsupportedOperationException( 
082                "This Entity is read-only. It cannot be modified" 
083            );
084        }
085    }
086    
087    protected Node createXPathResult(Element parent) {
088        return new DefaultEntity( parent, getName(), getText() );
089    }
090}
091
092
093
094
095/*
096 * Redistribution and use of this software and associated documentation
097 * ("Software"), with or without modification, are permitted provided
098 * that the following conditions are met:
099 *
100 * 1. Redistributions of source code must retain copyright
101 *    statements and notices.  Redistributions must also contain a
102 *    copy of this document.
103 *
104 * 2. Redistributions in binary form must reproduce the
105 *    above copyright notice, this list of conditions and the
106 *    following disclaimer in the documentation and/or other
107 *    materials provided with the distribution.
108 *
109 * 3. The name "DOM4J" must not be used to endorse or promote
110 *    products derived from this Software without prior written
111 *    permission of MetaStuff, Ltd.  For written permission,
112 *    please contact dom4j-info@metastuff.com.
113 *
114 * 4. Products derived from this Software may not be called "DOM4J"
115 *    nor may "DOM4J" appear in their names without prior written
116 *    permission of MetaStuff, Ltd. DOM4J is a registered
117 *    trademark of MetaStuff, Ltd.
118 *
119 * 5. Due credit should be given to the DOM4J Project
120 *    (http://dom4j.org/).
121 *
122 * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS
123 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
124 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
125 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
126 * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
127 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
128 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
129 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
130 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
131 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
132 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
133 * OF THE POSSIBILITY OF SUCH DAMAGE.
134 *
135 * Copyright 2001 (C) MetaStuff, Ltd. All Rights Reserved.
136 *
137 * $Id: FlyweightEntity.java,v 1.1 2001/06/20 18:59:23 jstrachan Exp $
138 */