1
2
3
4
5
6
7
8
9
10 package org.dom4j.tree;
11
12 import java.io.ByteArrayOutputStream;
13 import java.io.IOException;
14 import java.io.Writer;
15 import java.util.Iterator;
16 import java.util.List;
17 import java.util.Map;
18
19 import org.dom4j.Comment;
20 import org.dom4j.Document;
21 import org.dom4j.DocumentType;
22 import org.dom4j.Element;
23 import org.dom4j.IllegalAddException;
24 import org.dom4j.Node;
25 import org.dom4j.ProcessingInstruction;
26 import org.dom4j.QName;
27 import org.dom4j.Text;
28 import org.dom4j.Visitor;
29 import org.dom4j.io.XMLWriter;
30
31 /*** <p><code>AbstractDocument</code> is an abstract base class for
32 * tree implementors to use for implementation inheritence.</p>
33 *
34 * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
35 * @version $Revision: 1.29 $
36 */
37 public abstract class AbstractDocument extends AbstractBranch implements Document {
38
39 public AbstractDocument() {
40 }
41
42 public short getNodeType() {
43 return DOCUMENT_NODE;
44 }
45
46 public String getPath(Element context) {
47 return "/";
48 }
49
50 public String getUniquePath(Element context) {
51 return "/";
52 }
53
54 public Document getDocument() {
55 return this;
56 }
57
58 public String getXMLEncoding() {
59 return null;
60 }
61
62 public String getStringValue() {
63 Element root = getRootElement();
64 return ( root != null ) ? root.getStringValue() : "";
65 }
66
67 public String asXML() {
68 try {
69 ByteArrayOutputStream out = new ByteArrayOutputStream();
70 XMLWriter writer = new XMLWriter(out, outputFormat);
71 writer.write(this);
72 return out.toString();
73 }
74 catch (IOException e) {
75 throw new RuntimeException("IOException while generating textual representation: " + e.getMessage());
76 }
77 }
78
79 public void write(Writer out) throws IOException {
80 XMLWriter writer = new XMLWriter( out, outputFormat );
81 writer.write(this);
82 }
83
84 /*** <p><code>accept</code> method is the <code>Visitor Pattern</code> method.
85 * </p>
86 *
87 * @param visitor <code>Visitor</code> is the visitor.
88 */
89 public void accept(Visitor visitor) {
90 visitor.visit(this);
91
92 DocumentType docType = getDocType();
93 if ( docType != null ) {
94 visitor.visit( docType );
95 }
96
97
98 List content = content();
99 if (content != null) {
100 for ( Iterator iter = content.iterator(); iter.hasNext(); ) {
101 Object object = iter.next();
102 if (object instanceof String) {
103 Text text = getDocumentFactory().createText((String) object);
104 visitor.visit(text);
105 }
106 else {
107 Node node = (Node) object;
108 node.accept(visitor);
109 }
110 }
111 }
112 }
113
114 public String toString() {
115 return super.toString() + " [Document: name " + getName() + "]";
116 }
117
118 public void normalize() {
119 Element element = getRootElement();
120 if ( element != null ) {
121 element.normalize();
122 }
123 }
124
125 public Document addComment(String comment) {
126 Comment node = getDocumentFactory().createComment( comment );
127 add( node );
128 return this;
129 }
130
131 public Document addProcessingInstruction(String target, String data) {
132 ProcessingInstruction node = getDocumentFactory().createProcessingInstruction( target, data );
133 add( node );
134 return this;
135 }
136
137 public Document addProcessingInstruction(String target, Map data) {
138 ProcessingInstruction node = getDocumentFactory().createProcessingInstruction( target, data );
139 add( node );
140 return this;
141 }
142
143 public Element addElement(String name) {
144 Element element = getDocumentFactory().createElement(name);
145 add(element);
146 return element;
147 }
148
149 public Element addElement(String qualifiedName, String namespaceURI) {
150 Element element = getDocumentFactory().createElement(qualifiedName, namespaceURI);
151 add(element);
152 return element;
153 }
154
155 public Element addElement(QName qName) {
156 Element element = getDocumentFactory().createElement(qName);
157 add(element);
158 return element;
159 }
160
161 public void setRootElement(Element rootElement) {
162 clearContent();
163 if ( rootElement != null ) {
164 super.add(rootElement);
165 rootElementAdded(rootElement);
166 }
167 }
168
169 public void add(Element element) {
170 checkAddElementAllowed(element);
171 super.add(element);
172 rootElementAdded(element);
173 }
174
175 public boolean remove(Element element) {
176 boolean answer = super.remove(element);
177 Element root = getRootElement();
178 if ( root != null && answer ) {
179 setRootElement(null);
180 }
181 element.setDocument(null);
182 return answer;
183 }
184
185 public Node asXPathResult(Element parent) {
186 return this;
187 }
188
189 protected void childAdded(Node node) {
190 if (node != null ) {
191 node.setDocument(this);
192 }
193 }
194
195 protected void childRemoved(Node node) {
196 if ( node != null ) {
197 node.setDocument(null);
198 }
199 }
200
201 protected void checkAddElementAllowed(Element element) {
202 Element root = getRootElement();
203 if ( root != null ) {
204 throw new IllegalAddException(
205 this,
206 element,
207 "Cannot add another element to this Document as it already has "
208 + " a root element of: " + root.getQualifiedName()
209 );
210 }
211 }
212
213 /*** Called to set the root element variable */
214 protected abstract void rootElementAdded(Element rootElement);
215
216 }
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264