2
* Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved.
4
* This software is open source.
5
* See the bottom of this file for the licence.
8
package org.dom4j.tree;
10
import java.io.IOException;
11
import java.io.StringWriter;
12
import java.io.Writer;
13
import java.util.Iterator;
14
import java.util.List;
17
import org.dom4j.Comment;
18
import org.dom4j.Document;
19
import org.dom4j.DocumentType;
20
import org.dom4j.Element;
21
import org.dom4j.IllegalAddException;
22
import org.dom4j.Node;
23
import org.dom4j.ProcessingInstruction;
24
import org.dom4j.QName;
25
import org.dom4j.Text;
26
import org.dom4j.Visitor;
27
import org.dom4j.io.OutputFormat;
28
import org.dom4j.io.XMLWriter;
32
* <code>AbstractDocument</code> is an abstract base class for tree
33
* implementors to use for implementation inheritence.
36
* @author <a href="mailto:jstrachan@apache.org">James Strachan </a>
37
* @version $Revision: 1.33 $
39
public abstract class AbstractDocument extends AbstractBranch implements
42
/** The encoding of this document as stated in the XML declaration */
43
protected String encoding;
45
public AbstractDocument() {
48
public short getNodeType() {
52
public String getPath(Element context) {
56
public String getUniquePath(Element context) {
60
public Document getDocument() {
64
public String getXMLEncoding() {
68
public String getStringValue() {
69
Element root = getRootElement();
71
return (root != null) ? root.getStringValue() : "";
74
public String asXML() {
75
OutputFormat format = new OutputFormat();
76
format.setEncoding(encoding);
79
StringWriter out = new StringWriter();
80
XMLWriter writer = new XMLWriter(out, format);
84
return out.toString();
85
} catch (IOException e) {
86
throw new RuntimeException("IOException while generating textual "
87
+ "representation: " + e.getMessage());
91
public void write(Writer out) throws IOException {
92
OutputFormat format = new OutputFormat();
93
format.setEncoding(encoding);
95
XMLWriter writer = new XMLWriter(out, format);
101
* <code>accept</code> method is the <code>Visitor Pattern</code>
106
* <code>Visitor</code> is the visitor.
108
public void accept(Visitor visitor) {
111
DocumentType docType = getDocType();
113
if (docType != null) {
114
visitor.visit(docType);
118
List content = content();
120
if (content != null) {
121
for (Iterator iter = content.iterator(); iter.hasNext();) {
122
Object object = iter.next();
124
if (object instanceof String) {
125
Text text = getDocumentFactory()
126
.createText((String) object);
129
Node node = (Node) object;
130
node.accept(visitor);
136
public String toString() {
137
return super.toString() + " [Document: name " + getName() + "]";
140
public void normalize() {
141
Element element = getRootElement();
143
if (element != null) {
148
public Document addComment(String comment) {
149
Comment node = getDocumentFactory().createComment(comment);
155
public Document addProcessingInstruction(String target, String data) {
156
ProcessingInstruction node = getDocumentFactory()
157
.createProcessingInstruction(target, data);
163
public Document addProcessingInstruction(String target, Map data) {
164
ProcessingInstruction node = getDocumentFactory()
165
.createProcessingInstruction(target, data);
171
public Element addElement(String name) {
172
Element element = getDocumentFactory().createElement(name);
178
public Element addElement(String qualifiedName, String namespaceURI) {
179
Element element = getDocumentFactory().createElement(qualifiedName,
186
public Element addElement(QName qName) {
187
Element element = getDocumentFactory().createElement(qName);
193
public void setRootElement(Element rootElement) {
196
if (rootElement != null) {
197
super.add(rootElement);
198
rootElementAdded(rootElement);
202
public void add(Element element) {
203
checkAddElementAllowed(element);
205
rootElementAdded(element);
208
public boolean remove(Element element) {
209
boolean answer = super.remove(element);
210
Element root = getRootElement();
212
if ((root != null) && answer) {
213
setRootElement(null);
216
element.setDocument(null);
221
public Node asXPathResult(Element parent) {
225
protected void childAdded(Node node) {
227
node.setDocument(this);
231
protected void childRemoved(Node node) {
233
node.setDocument(null);
237
protected void checkAddElementAllowed(Element element) {
238
Element root = getRootElement();
241
throw new IllegalAddException(this, element,
242
"Cannot add another element to this "
243
+ "Document as it already has a root "
244
+ "element of: " + root.getQualifiedName());
249
* Called to set the root element variable
254
protected abstract void rootElementAdded(Element rootElement);
256
public void setXMLEncoding(String enc) {
262
* Redistribution and use of this software and associated documentation
263
* ("Software"), with or without modification, are permitted provided that the
264
* following conditions are met:
266
* 1. Redistributions of source code must retain copyright statements and
267
* notices. Redistributions must also contain a copy of this document.
269
* 2. Redistributions in binary form must reproduce the above copyright notice,
270
* this list of conditions and the following disclaimer in the documentation
271
* and/or other materials provided with the distribution.
273
* 3. The name "DOM4J" must not be used to endorse or promote products derived
274
* from this Software without prior written permission of MetaStuff, Ltd. For
275
* written permission, please contact dom4j-info@metastuff.com.
277
* 4. Products derived from this Software may not be called "DOM4J" nor may
278
* "DOM4J" appear in their names without prior written permission of MetaStuff,
279
* Ltd. DOM4J is a registered trademark of MetaStuff, Ltd.
281
* 5. Due credit should be given to the DOM4J Project - http://www.dom4j.org
283
* THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS ``AS IS'' AND
284
* ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
285
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
286
* ARE DISCLAIMED. IN NO EVENT SHALL METASTUFF, LTD. OR ITS CONTRIBUTORS BE
287
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
288
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
289
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
290
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
291
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
292
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
293
* POSSIBILITY OF SUCH DAMAGE.
295
* Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved.