1
2
3
4
5
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147