Clover coverage report - dom4j - 1.6.1
Coverage timestamp: ma mei 16 2005 14:23:01 GMT+01:00

file stats: | LOC: | 369 |  | Methods: | 29
| NCLOC: | 229 |  | Classes: | 1

Source file | Conditionals | Statements | Methods | TOTAL

DefaultDocument.java

30,8%

52,2%

72,4%

49,5%

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