KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ozoneDB > xml > dom4j > o3impl > AbstractDocument


1 /*
2  * Copyright 2001 (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: AbstractDocument.java,v 1.2 2003/06/10 16:18:35 per_nyfelt Exp $
8  */

9
10 package org.ozoneDB.xml.dom4j.o3impl;
11
12 import org.dom4j.*;
13 import org.dom4j.io.XMLWriter;
14
15 import java.io.IOException JavaDoc;
16 import java.io.StringWriter JavaDoc;
17 import java.io.Writer JavaDoc;
18 import java.util.Iterator JavaDoc;
19 import java.util.List JavaDoc;
20 import java.util.Map JavaDoc;
21
22 /** <p><code>AbstractDocument</code> is an abstract base class for
23  * tree implementors to use for implementation inheritence.</p>
24  *
25  * @author <a HREF="mailto:jstrachan@apache.org">James Strachan</a>
26  * @version $Revision: 1.2 $
27  */

28 public abstract class AbstractDocument extends AbstractBranch implements Document {
29
30     public AbstractDocument() {
31     }
32
33     public short getNodeType() {
34         return DOCUMENT_NODE;
35     }
36
37     public String JavaDoc getPath(Element context) {
38         return "/";
39     }
40
41     public String JavaDoc getUniquePath(Element context) {
42         return "/";
43     }
44
45     public Document getDocument() {
46         return this;
47     }
48
49     public String JavaDoc getStringValue() {
50         Element root = getRootElement();
51         return (root != null) ? root.getStringValue() : "";
52     }
53
54     public String JavaDoc asXML() {
55         try {
56             StringWriter JavaDoc out = new StringWriter JavaDoc();
57             XMLWriter writer = new XMLWriter(out, outputFormat);
58             writer.write(this);
59             return out.toString();
60         } catch (IOException JavaDoc e) {
61             throw new RuntimeException JavaDoc("Wierd IOException while generating textual representation: " + e.getMessage());
62         }
63     }
64
65     public void write(Writer out) throws IOException JavaDoc {
66         XMLWriter writer = new XMLWriter(out, outputFormat);
67         writer.write(this);
68     }
69
70     /** <p><code>accept</code> method is the <code>Visitor Pattern</code> method.
71      * </p>
72      *
73      * @param visitor <code>Visitor</code> is the visitor.
74      */

75     public void accept(Visitor visitor) {
76         visitor.visit(this);
77
78         DocumentType docType = getDocType();
79         if (docType != null) {
80             visitor.visit(docType);
81         }
82
83         // visit content
84
List JavaDoc content = content();
85         if (content != null) {
86             for (Iterator JavaDoc iter = content.iterator(); iter.hasNext();) {
87                 Object JavaDoc object = iter.next();
88                 if (object instanceof String JavaDoc) {
89                     Text text = getNodeFactory().createText((String JavaDoc) object);
90                     visitor.visit(text);
91                 } else {
92                     Node node = (Node) object;
93                     node.accept(visitor);
94                 }
95             }
96         }
97     }
98
99     public String JavaDoc toString() {
100         return super.toString() + " [Document: name " + getName() + "]";
101     }
102
103     public void normalize() {
104         Element element = getRootElement();
105         if (element != null) {
106             element.normalize();
107         }
108     }
109
110     public Document addComment(String JavaDoc comment) {
111         Comment node = getNodeFactory().createComment(comment);
112         add(node);
113         return this;
114     }
115
116     public Document addProcessingInstruction(String JavaDoc target, String JavaDoc data) {
117         ProcessingInstruction node = getNodeFactory().createProcessingInstruction(target, data);
118         add(node);
119         return this;
120     }
121
122     public Document addProcessingInstruction(String JavaDoc target, Map JavaDoc data) {
123         ProcessingInstruction node = getNodeFactory().createProcessingInstruction(target, data);
124         add(node);
125         return this;
126     }
127
128     public Element addElement(String JavaDoc name) {
129         checkAddElementAllowed();
130         Element node = super.addElement(name);
131         rootElementAdded(node);
132         return node;
133     }
134
135     public Element addElement(String JavaDoc qualifiedName, String JavaDoc namespaceURI) {
136         checkAddElementAllowed();
137         Element node = super.addElement(qualifiedName, namespaceURI);
138         rootElementAdded(node);
139         return node;
140     }
141
142     public Element addElement(QName qName) {
143         checkAddElementAllowed();
144         Element node = super.addElement(qName);
145         rootElementAdded(node);
146         return node;
147     }
148
149     public void setRootElement(Element rootElement) {
150         clearContent();
151         if (rootElement != null) {
152             super.add(rootElement);
153             rootElementAdded(rootElement);
154         }
155     }
156
157     public void add(Element element) {
158         checkAddElementAllowed();
159         super.add(element);
160         rootElementAdded(element);
161     }
162
163     public boolean remove(Element element) {
164         boolean answer = super.remove(element);
165         Element root = getRootElement();
166         if (root != null && answer) {
167             setRootElement(null);
168         }
169         element.setDocument(null);
170         return answer;
171     }
172
173     public Node asXPathResult(Element parent) {
174         return this;
175     }
176
177
178     protected void childAdded(Node node) {
179         if (node != null) {
180             node.setDocument(this);
181         }
182     }
183
184     protected void childRemoved(Node node) {
185         if (node != null) {
186             node.setDocument(null);
187         }
188     }
189
190     protected void checkAddElementAllowed() {
191         Element root = getRootElement();
192         if (root != null) {
193             throw new IllegalAddException(
194                     this,
195                     root,
196                     "Cannot add another element to this Document as it already has "
197                     + " a root element of: " + root.getQualifiedName()
198             );
199         }
200     }
201
202     /** Called to set the root element variable */
203     protected abstract void rootElementAdded(Element rootElement);
204
205 }
206
207
208 /*
209  * Redistribution and use of this software and associated documentation
210  * ("Software"), with or without modification, are permitted provided
211  * that the following conditions are met:
212  *
213  * 1. Redistributions of source code must retain copyright
214  * statements and notices. Redistributions must also contain a
215  * copy of this document.
216  *
217  * 2. Redistributions in binary form must reproduce the
218  * above copyright notice, this list of conditions and the
219  * following disclaimer in the documentation and/or other
220  * materials provided with the distribution.
221  *
222  * 3. The name "DOM4J" must not be used to endorse or promote
223  * products derived from this Software without prior written
224  * permission of MetaStuff, Ltd. For written permission,
225  * please contact dom4j-info@metastuff.com.
226  *
227  * 4. Products derived from this Software may not be called "DOM4J"
228  * nor may "DOM4J" appear in their names without prior written
229  * permission of MetaStuff, Ltd. DOM4J is a registered
230  * trademark of MetaStuff, Ltd.
231  *
232  * 5. Due credit should be given to the DOM4J Project
233  * (http://dom4j.org/).
234  *
235  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS
236  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
237  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
238  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
239  * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
240  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
241  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
242  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
243  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
244  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
245  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
246  * OF THE POSSIBILITY OF SUCH DAMAGE.
247  *
248  * Copyright 2001 (C) MetaStuff, Ltd. All Rights Reserved.
249  *
250  * $Id: AbstractDocument.java,v 1.2 2003/06/10 16:18:35 per_nyfelt Exp $
251  */

252
Popular Tags