KickJava   Java API By Example, From Geeks To Geeks.

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


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: DefaultDocument.java,v 1.2 2003/06/10 16:18:36 per_nyfelt Exp $
8  */

9
10 package org.ozoneDB.xml.dom4j.o3impl;
11
12 import org.dom4j.*;
13 import org.xml.sax.EntityResolver JavaDoc;
14
15 import java.util.Collections JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.List JavaDoc;
18
19 /** <p><code>DefaultDocument</code> is the default DOM4J default implementation
20  * of an XML document.</p>
21  *
22  * @author <a HREF="mailto:jstrachan@apache.org">James Strachan</a>
23  * @version $Revision: 1.2 $
24  */

25 public class DefaultDocument extends AbstractDocument {
26
27     protected static final List JavaDoc EMPTY_LIST = Collections.EMPTY_LIST;
28     protected static final Iterator JavaDoc EMPTY_ITERATOR = EMPTY_LIST.iterator();
29
30     /** The name of the document */
31     private String JavaDoc name;
32
33     /** The root element of this document */
34     private Element rootElement;
35
36     /** Store the contents of the document as a lazily created <code>List</code> */
37     private List JavaDoc content;
38
39     /** The document type for this document */
40     private DocumentType docType;
41
42     /** The resolver of URIs */
43     private transient EntityResolver JavaDoc entityResolver;
44
45     public DefaultDocument() {
46     }
47
48     public DefaultDocument(String JavaDoc name) {
49         this.name = name;
50     }
51
52     public DefaultDocument(Element rootElement) {
53         this.rootElement = rootElement;
54     }
55
56     public DefaultDocument(DocumentType docType) {
57         this.docType = docType;
58     }
59
60     public DefaultDocument(Element rootElement, DocumentType docType) {
61         this.rootElement = rootElement;
62         this.docType = docType;
63     }
64
65     public DefaultDocument(String JavaDoc name, Element rootElement, DocumentType docType) {
66         this.name = name;
67         this.rootElement = rootElement;
68         this.docType = docType;
69     }
70
71     public String JavaDoc getName() {
72         return name;
73     }
74
75     public void setName(String JavaDoc name) {
76         this.name = name;
77     }
78
79
80     public Element getRootElement() {
81         return rootElement;
82     }
83
84     public DocumentType getDocType() {
85         return docType;
86     }
87
88     public void setDocType(DocumentType docType) {
89         this.docType = docType;
90     }
91
92     public Document addDocType(String JavaDoc name, String JavaDoc publicId, String JavaDoc systemId) {
93         setDocType(getNodeFactory().createDocType(name, publicId, systemId));
94         return this;
95     }
96
97     public EntityResolver JavaDoc getEntityResolver() {
98         return entityResolver;
99     }
100
101     public void setEntityResolver(EntityResolver JavaDoc entityResolver) {
102         this.entityResolver = entityResolver;
103     }
104
105     public Object JavaDoc clone() {
106         DefaultDocument document = (DefaultDocument) super.clone();
107         document.rootElement = null;
108         document.content = null;
109         document.appendContent(this);
110         return document;
111     }
112
113     public List JavaDoc processingInstructions() {
114         List JavaDoc source = contentList();
115         List JavaDoc answer = createResultList();
116         int size = source.size();
117         for (int i = 0; i < size; i++) {
118             Object JavaDoc object = source.get(i);
119             if (object instanceof ProcessingInstruction) {
120                 answer.add(object);
121             }
122         }
123         return answer;
124     }
125
126     public List JavaDoc processingInstructions(String JavaDoc target) {
127         List JavaDoc source = contentList();
128         List JavaDoc answer = createResultList();
129         int size = source.size();
130         for (int i = 0; i < size; i++) {
131             Object JavaDoc object = source.get(i);
132             if (object instanceof ProcessingInstruction) {
133                 ProcessingInstruction pi = (ProcessingInstruction) object;
134                 if (target.equals(pi.getName())) {
135                     answer.add(pi);
136                 }
137             }
138         }
139         return answer;
140     }
141
142     public ProcessingInstruction processingInstruction(String JavaDoc target) {
143         List JavaDoc source = contentList();
144         int size = source.size();
145         for (int i = 0; i < size; i++) {
146             Object JavaDoc object = source.get(i);
147             if (object instanceof ProcessingInstruction) {
148                 ProcessingInstruction pi = (ProcessingInstruction) object;
149                 if (target.equals(pi.getName())) {
150                     return pi;
151                 }
152             }
153         }
154         return null;
155     }
156
157     public boolean removeProcessingInstruction(String JavaDoc target) {
158         List JavaDoc source = contentList();
159         for (Iterator JavaDoc iter = source.iterator(); iter.hasNext();) {
160             Object JavaDoc object = iter.next();
161             if (object instanceof ProcessingInstruction) {
162                 ProcessingInstruction pi = (ProcessingInstruction) object;
163                 if (target.equals(pi.getName())) {
164                     iter.remove();
165                     return true;
166                 }
167             }
168         }
169         return false;
170     }
171
172
173     public void setContent(List JavaDoc content) {
174         rootElement = null;
175         contentRemoved();
176         if (content instanceof ContentListFacade) {
177             content = ((ContentListFacade) content).getBackingList();
178         }
179         if (content == null) {
180             this.content = null;
181         } else {
182             int size = content.size();
183             List JavaDoc newContent = createContentList(size);
184             for (int i = 0; i < size; i++) {
185                 Object JavaDoc object = content.get(i);
186                 if (object instanceof Node) {
187                     Node node = (Node) object;
188                     Document doc = node.getDocument();
189                     if (doc != null && doc != this) {
190                         node = (Node) node.clone();
191                     }
192                     if (node instanceof Element) {
193                         if (rootElement == null) {
194                             rootElement = (Element) node;
195                         } else {
196                             throw new IllegalAddException("A document may only contain one Element: " + content);
197                         }
198                     }
199                     newContent.add(node);
200                     childAdded(node);
201                 }
202             }
203             this.content = newContent;
204         }
205     }
206
207     public void clearContent() {
208         contentRemoved();
209         content = null;
210         rootElement = null;
211     }
212
213
214     // Implementation methods
215
//-------------------------------------------------------------------------
216
protected List JavaDoc contentList() {
217         if (content == null) {
218             content = createContentList();
219             if (rootElement != null) {
220                 content.add(rootElement);
221             }
222         }
223         return content;
224     }
225
226
227     protected void addNode(Node node) {
228         if (node != null) {
229             Document document = node.getDocument();
230             if (document != null && document != this) {
231                 // XXX: could clone here
232
String JavaDoc message = "The Node already has an existing document: " + document;
233                 throw new IllegalAddException(this, node, message);
234             }
235             contentList().add(node);
236             childAdded(node);
237         }
238     }
239
240     protected void addNode(int index, Node node) {
241         if (node != null) {
242             Document document = node.getDocument();
243             if (document != null && document != this) {
244                 // XXX: could clone here
245
String JavaDoc message = "The Node already has an existing document: " + document;
246                 throw new IllegalAddException(this, node, message);
247             }
248             contentList().add(index, node);
249             childAdded(node);
250         }
251     }
252
253     protected boolean removeNode(Node node) {
254         if (node == rootElement) {
255             rootElement = null;
256         }
257         if (contentList().remove(node)) {
258             childRemoved(node);
259             return true;
260         }
261         return false;
262     }
263
264     protected void rootElementAdded(Element element) {
265         this.rootElement = element;
266         element.setDocument(this);
267     }
268
269 }
270
271
272 /*
273  * Redistribution and use of this software and associated documentation
274  * ("Software"), with or without modification, are permitted provided
275  * that the following conditions are met:
276  *
277  * 1. Redistributions of source code must retain copyright
278  * statements and notices. Redistributions must also contain a
279  * copy of this document.
280  *
281  * 2. Redistributions in binary form must reproduce the
282  * above copyright notice, this list of conditions and the
283  * following disclaimer in the documentation and/or other
284  * materials provided with the distribution.
285  *
286  * 3. The name "DOM4J" must not be used to endorse or promote
287  * products derived from this Software without prior written
288  * permission of MetaStuff, Ltd. For written permission,
289  * please contact dom4j-info@metastuff.com.
290  *
291  * 4. Products derived from this Software may not be called "DOM4J"
292  * nor may "DOM4J" appear in their names without prior written
293  * permission of MetaStuff, Ltd. DOM4J is a registered
294  * trademark of MetaStuff, Ltd.
295  *
296  * 5. Due credit should be given to the DOM4J Project
297  * (http://dom4j.org/).
298  *
299  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS
300  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
301  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
302  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
303  * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
304  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
305  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
306  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
307  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
308  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
309  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
310  * OF THE POSSIBILITY OF SUCH DAMAGE.
311  *
312  * Copyright 2001 (C) MetaStuff, Ltd. All Rights Reserved.
313  *
314  * $Id: DefaultDocument.java,v 1.2 2003/06/10 16:18:36 per_nyfelt Exp $
315  */

316
Popular Tags