1 /*
2 * Copyright 2001-2004 (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: FlyweightEntity.java,v 1.4 2004/06/25 08:03:41 maartenc Exp $
8 */
9
10 package org.dom4j.tree;
11
12 import org.dom4j.Element;
13 import org.dom4j.Node;
14
15 /*** <p><code>FlyweightEntity</code> is a Flyweight pattern implementation
16 * of a singly linked, read-only XML entity.</p>
17 *
18 * <p>This node could be shared across documents and elements though
19 * it does not support the parent relationship.</p>
20 *
21 * <p>Often this node needs to be created and then the text content added
22 * later (for example in SAX) so this implementation allows a call to
23 * {@link #setText} providing the entity has no text already.
24 *
25 * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
26 * @version $Revision: 1.4 $
27 */
28 public class FlyweightEntity extends AbstractEntity {
29
30 /*** The name of the <code>Entity</code> */
31 protected String name;
32
33 /*** The text of the <code>Entity</code> */
34 protected String text;
35
36 /*** A default constructor for implementors to use.
37 */
38 protected FlyweightEntity() {
39 }
40
41 /*** Creates the <code>Entity</code> with the specified name
42 *
43 * @param name is the name of the entity
44 */
45 public FlyweightEntity(String name) {
46 this.name = name;
47 }
48
49 /*** Creates the <code>Entity</code> with the specified name
50 * and text.
51 *
52 * @param name is the name of the entity
53 * @param text is the text of the entity
54 */
55 public FlyweightEntity(String name,String text) {
56 this.name = name;
57 this.text = text;
58 }
59
60 /*** @return the name of the entity
61 */
62 public String getName() {
63 return name;
64 }
65
66 /*** @return the text of the entity
67 */
68 public String getText() {
69 return text;
70 }
71
72 /*** sets the value of the entity if it is not defined yet
73 * otherwise an <code>UnsupportedOperationException</code> is thrown
74 * as this class is read only.
75 */
76 public void setText(String text) {
77 if (this.text != null) {
78 this.text = text;
79 }
80 else {
81 throw new UnsupportedOperationException(
82 "This Entity is read-only. It cannot be modified"
83 );
84 }
85 }
86
87 protected Node createXPathResult(Element parent) {
88 return new DefaultEntity( parent, getName(), getText() );
89 }
90 }
91
92
93
94
95 /*
96 * Redistribution and use of this software and associated documentation
97 * ("Software"), with or without modification, are permitted provided
98 * that the following conditions are met:
99 *
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://www.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-2004 (C) MetaStuff, Ltd. All Rights Reserved.
136 *
137 * $Id: FlyweightEntity.java,v 1.4 2004/06/25 08:03:41 maartenc Exp $
138 */