annotate NGSrich_0.5.5/src/org/jdom/Document.java @ 0:89ad0a9cca52 default tip

Uploaded
author pfrommolt
date Mon, 21 Nov 2011 08:12:19 -0500
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
1 /*--
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
2
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
3 $Id: Document.java,v 1.85 2007/11/10 05:28:58 jhunter Exp $
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
4
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
5 Copyright (C) 2000-2007 Jason Hunter & Brett McLaughlin.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
6 All rights reserved.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
7
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
8 Redistribution and use in source and binary forms, with or without
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
9 modification, are permitted provided that the following conditions
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
10 are met:
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
11
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
12 1. Redistributions of source code must retain the above copyright
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
13 notice, this list of conditions, and the following disclaimer.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
14
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
15 2. Redistributions in binary form must reproduce the above copyright
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
16 notice, this list of conditions, and the disclaimer that follows
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
17 these conditions in the documentation and/or other materials
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
18 provided with the distribution.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
19
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
20 3. The name "JDOM" must not be used to endorse or promote products
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
21 derived from this software without prior written permission. For
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
22 written permission, please contact <request_AT_jdom_DOT_org>.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
23
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
24 4. Products derived from this software may not be called "JDOM", nor
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
25 may "JDOM" appear in their name, without prior written permission
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
26 from the JDOM Project Management <request_AT_jdom_DOT_org>.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
27
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
28 In addition, we request (but do not require) that you include in the
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
29 end-user documentation provided with the redistribution and/or in the
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
30 software itself an acknowledgement equivalent to the following:
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
31 "This product includes software developed by the
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
32 JDOM Project (http://www.jdom.org/)."
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
33 Alternatively, the acknowledgment may be graphical using the logos
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
34 available at http://www.jdom.org/images/logos.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
35
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
36 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
37 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
38 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
39 DISCLAIMED. IN NO EVENT SHALL THE JDOM AUTHORS OR THE PROJECT
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
40 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
41 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
42 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
43 USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
44 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
45 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
46 OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
47 SUCH DAMAGE.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
48
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
49 This software consists of voluntary contributions made by many
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
50 individuals on behalf of the JDOM Project and was originally
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
51 created by Jason Hunter <jhunter_AT_jdom_DOT_org> and
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
52 Brett McLaughlin <brett_AT_jdom_DOT_org>. For more information
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
53 on the JDOM Project, please see <http://www.jdom.org/>.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
54
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
55 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
56
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
57 package org.jdom;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
58
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
59 import java.util.*;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
60 import org.jdom.filter.*;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
61
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
62 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
63 * An XML document. Methods allow access to the root element as well as the
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
64 * {@link DocType} and other document-level information.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
65 *
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
66 * @version $Revision: 1.85 $, $Date: 2007/11/10 05:28:58 $
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
67 * @author Brett McLaughlin
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
68 * @author Jason Hunter
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
69 * @author Jools Enticknap
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
70 * @author Bradley S. Huffman
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
71 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
72 public class Document implements Parent {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
73
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
74 private static final String CVS_ID =
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
75 "@(#) $RCSfile: Document.java,v $ $Revision: 1.85 $ $Date: 2007/11/10 05:28:58 $ $Name: jdom_1_1_1 $";
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
76
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
77 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
78 * This document's content including comments, PIs, a possible
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
79 * DocType, and a root element.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
80 * Subclassers have to track content using their own
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
81 * mechanism.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
82 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
83 ContentList content = new ContentList(this);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
84
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
85 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
86 * See http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/core.html#baseURIs-Considerations
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
87 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
88 protected String baseURI = null;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
89
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
90 // Supports the setProperty/getProperty calls
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
91 private HashMap propertyMap = null;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
92
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
93 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
94 * Creates a new empty document. A document must have a root element,
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
95 * so this document will not be well-formed and accessor methods will
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
96 * throw an IllegalStateException if this document is accessed before a
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
97 * root element is added. This method is most useful for build tools.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
98 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
99 public Document() {}
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
100
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
101 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
102 * This will create a new <code>Document</code>,
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
103 * with the supplied <code>{@link Element}</code>
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
104 * as the root element, the supplied
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
105 * <code>{@link DocType}</code> declaration, and the specified
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
106 * base URI.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
107 *
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
108 * @param rootElement <code>Element</code> for document root.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
109 * @param docType <code>DocType</code> declaration.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
110 * @param baseURI the URI from which this doucment was loaded.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
111 * @throws IllegalAddException if the given docType object
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
112 * is already attached to a document or the given
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
113 * rootElement already has a parent
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
114 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
115 public Document(Element rootElement, DocType docType, String baseURI) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
116 if (rootElement != null) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
117 setRootElement(rootElement);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
118 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
119 if (docType != null) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
120 setDocType(docType);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
121 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
122 if (baseURI != null) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
123 setBaseURI(baseURI);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
124 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
125 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
126
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
127 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
128 * This will create a new <code>Document</code>,
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
129 * with the supplied <code>{@link Element}</code>
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
130 * as the root element and the supplied
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
131 * <code>{@link DocType}</code> declaration.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
132 *
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
133 * @param rootElement <code>Element</code> for document root.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
134 * @param docType <code>DocType</code> declaration.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
135 * @throws IllegalAddException if the given DocType object
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
136 * is already attached to a document or the given
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
137 * rootElement already has a parent
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
138 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
139 public Document(Element rootElement, DocType docType) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
140 this(rootElement, docType, null);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
141 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
142
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
143 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
144 * This will create a new <code>Document</code>,
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
145 * with the supplied <code>{@link Element}</code>
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
146 * as the root element, and no <code>{@link DocType}</code>
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
147 * declaration.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
148 *
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
149 * @param rootElement <code>Element</code> for document root
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
150 * @throws IllegalAddException if the given rootElement already has
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
151 * a parent.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
152 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
153 public Document(Element rootElement) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
154 this(rootElement, null, null);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
155 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
156
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
157 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
158 * This will create a new <code>Document</code>,
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
159 * with the supplied list of content, and a
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
160 * <code>{@link DocType}</code> declaration only if the content
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
161 * contains a DocType instance. A null list is treated the
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
162 * same as the no-arg constructor.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
163 *
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
164 * @param content <code>List</code> of starter content
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
165 * @throws IllegalAddException if the List contains more than
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
166 * one Element or objects of illegal types.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
167 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
168 public Document(List content) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
169 setContent(content);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
170 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
171
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
172 public int getContentSize() {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
173 return content.size();
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
174 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
175
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
176 public int indexOf(Content child) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
177 return content.indexOf(child);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
178 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
179
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
180 // /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
181 // * Starting at the given index (inclusive), return the index of
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
182 // * the first child matching the supplied filter, or -1
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
183 // * if none is found.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
184 // *
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
185 // * @return index of child, or -1 if none found.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
186 // */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
187 // private int indexOf(int start, Filter filter) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
188 // int size = getContentSize();
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
189 // for (int i = start; i < size; i++) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
190 // if (filter.matches(getContent(i))) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
191 // return i;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
192 // }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
193 // }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
194 // return -1;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
195 // }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
196
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
197 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
198 * This will return <code>true</code> if this document has a
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
199 * root element, <code>false</code> otherwise.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
200 *
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
201 * @return <code>true</code> if this document has a root element,
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
202 * <code>false</code> otherwise.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
203 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
204 public boolean hasRootElement() {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
205 return (content.indexOfFirstElement() < 0) ? false : true;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
206 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
207
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
208 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
209 * This will return the root <code>Element</code>
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
210 * for this <code>Document</code>
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
211 *
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
212 * @return <code>Element</code> - the document's root element
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
213 * @throws IllegalStateException if the root element hasn't been set
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
214 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
215 public Element getRootElement() {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
216 int index = content.indexOfFirstElement();
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
217 if (index < 0) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
218 throw new IllegalStateException("Root element not set");
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
219 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
220 return (Element) content.get(index);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
221 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
222
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
223 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
224 * This sets the root <code>{@link Element}</code> for the
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
225 * <code>Document</code>. If the document already has a root
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
226 * element, it is replaced.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
227 *
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
228 * @param rootElement <code>Element</code> to be new root.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
229 * @return <code>Document</code> - modified Document.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
230 * @throws IllegalAddException if the given rootElement already has
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
231 * a parent.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
232 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
233 public Document setRootElement(Element rootElement) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
234 int index = content.indexOfFirstElement();
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
235 if (index < 0) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
236 content.add(rootElement);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
237 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
238 else {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
239 content.set(index, rootElement);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
240 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
241 return this;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
242 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
243
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
244 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
245 * Detach the root <code>{@link Element}</code> from this document.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
246 *
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
247 * @return removed root <code>Element</code>
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
248 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
249 public Element detachRootElement() {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
250 int index = content.indexOfFirstElement();
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
251 if (index < 0)
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
252 return null;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
253 return (Element) removeContent(index);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
254 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
255
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
256 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
257 * This will return the <code>{@link DocType}</code>
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
258 * declaration for this <code>Document</code>, or
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
259 * <code>null</code> if none exists.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
260 *
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
261 * @return <code>DocType</code> - the DOCTYPE declaration.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
262 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
263 public DocType getDocType() {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
264 int index = content.indexOfDocType();
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
265 if (index < 0) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
266 return null;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
267 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
268 else {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
269 return (DocType) content.get(index);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
270 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
271 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
272
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
273 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
274 * This will set the <code>{@link DocType}</code>
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
275 * declaration for this <code>Document</code>. Note
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
276 * that a DocType can only be attached to one Document.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
277 * Attempting to set the DocType to a DocType object
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
278 * that already belongs to a Document will result in an
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
279 * IllegalAddException being thrown.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
280 *
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
281 * @param docType <code>DocType</code> declaration.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
282 * @return object on which the method was invoked
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
283 * @throws IllegalAddException if the given docType is
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
284 * already attached to a Document.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
285 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
286 public Document setDocType(DocType docType) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
287 if (docType == null) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
288 // Remove any existing doctype
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
289 int docTypeIndex = content.indexOfDocType();
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
290 if (docTypeIndex >= 0) content.remove(docTypeIndex);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
291 return this;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
292 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
293
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
294 if (docType.getParent() != null) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
295 throw new IllegalAddException(docType,
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
296 "The DocType already is attached to a document");
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
297 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
298
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
299 // Add DocType to head if new, replace old otherwise
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
300 int docTypeIndex = content.indexOfDocType();
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
301 if (docTypeIndex < 0) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
302 content.add(0, docType);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
303 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
304 else {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
305 content.set(docTypeIndex, docType);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
306 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
307
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
308 return this;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
309 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
310
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
311 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
312 * Appends the child to the end of the content list.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
313 *
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
314 * @param child child to append to end of content list
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
315 * @return the document on which the method was called
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
316 * @throws IllegalAddException if the given child already has a parent.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
317 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
318 public Document addContent(Content child) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
319 content.add(child);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
320 return this;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
321 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
322
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
323 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
324 * Appends all children in the given collection to the end of
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
325 * the content list. In event of an exception during add the
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
326 * original content will be unchanged and the objects in the supplied
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
327 * collection will be unaltered.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
328 *
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
329 * @param c collection to append
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
330 * @return the document on which the method was called
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
331 * @throws IllegalAddException if any item in the collection
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
332 * already has a parent or is of an illegal type.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
333 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
334 public Document addContent(Collection c) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
335 content.addAll(c);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
336 return this;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
337 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
338
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
339 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
340 * Inserts the child into the content list at the given index.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
341 *
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
342 * @param index location for adding the collection
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
343 * @param child child to insert
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
344 * @return the parent on which the method was called
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
345 * @throws IndexOutOfBoundsException if index is negative or beyond
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
346 * the current number of children
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
347 * @throws IllegalAddException if the given child already has a parent.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
348 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
349 public Document addContent(int index, Content child) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
350 content.add(index, child);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
351 return this;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
352 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
353
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
354 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
355 * Inserts the content in a collection into the content list
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
356 * at the given index. In event of an exception the original content
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
357 * will be unchanged and the objects in the supplied collection will be
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
358 * unaltered.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
359 *
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
360 * @param index location for adding the collection
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
361 * @param c collection to insert
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
362 * @return the parent on which the method was called
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
363 * @throws IndexOutOfBoundsException if index is negative or beyond
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
364 * the current number of children
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
365 * @throws IllegalAddException if any item in the collection
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
366 * already has a parent or is of an illegal type.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
367 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
368 public Document addContent(int index, Collection c) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
369 content.addAll(index, c);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
370 return this;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
371 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
372
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
373 public List cloneContent() {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
374 int size = getContentSize();
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
375 List list = new ArrayList(size);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
376 for (int i = 0; i < size; i++) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
377 Content child = getContent(i);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
378 list.add(child.clone());
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
379 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
380 return list;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
381 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
382
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
383 public Content getContent(int index) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
384 return (Content) content.get(index);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
385 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
386
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
387 // public Content getChild(Filter filter) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
388 // int i = indexOf(0, filter);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
389 // return (i < 0) ? null : getContent(i);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
390 // }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
391
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
392 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
393 * This will return all content for the <code>Document</code>.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
394 * The returned list is "live" in document order and changes to it
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
395 * affect the document's actual content.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
396 *
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
397 * <p>
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
398 * Sequential traversal through the List is best done with a Iterator
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
399 * since the underlying implement of List.size() may require walking the
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
400 * entire list.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
401 * </p>
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
402 *
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
403 * @return <code>List</code> - all Document content
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
404 * @throws IllegalStateException if the root element hasn't been set
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
405 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
406 public List getContent() {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
407 if (!hasRootElement())
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
408 throw new IllegalStateException("Root element not set");
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
409 return content;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
410 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
411
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
412 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
413 * Return a filtered view of this <code>Document</code>'s content.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
414 *
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
415 * <p>
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
416 * Sequential traversal through the List is best done with a Iterator
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
417 * since the underlying implement of List.size() may require walking the
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
418 * entire list.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
419 * </p>
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
420 *
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
421 * @param filter <code>Filter</code> to apply
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
422 * @return <code>List</code> - filtered Document content
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
423 * @throws IllegalStateException if the root element hasn't been set
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
424 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
425 public List getContent(Filter filter) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
426 if (!hasRootElement())
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
427 throw new IllegalStateException("Root element not set");
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
428 return content.getView(filter);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
429 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
430
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
431 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
432 * Removes all child content from this parent.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
433 *
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
434 * @return list of the old children detached from this parent
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
435 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
436 public List removeContent() {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
437 List old = new ArrayList(content);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
438 content.clear();
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
439 return old;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
440 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
441
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
442 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
443 * Remove all child content from this parent matching the supplied filter.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
444 *
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
445 * @param filter filter to select which content to remove
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
446 * @return list of the old children detached from this parent
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
447 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
448 public List removeContent(Filter filter) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
449 List old = new ArrayList();
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
450 Iterator itr = content.getView(filter).iterator();
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
451 while (itr.hasNext()) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
452 Content child = (Content) itr.next();
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
453 old.add(child);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
454 itr.remove();
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
455 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
456 return old;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
457 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
458
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
459 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
460 * This sets the content of the <code>Document</code>. The supplied
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
461 * List should contain only objects of type <code>Element</code>,
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
462 * <code>Comment</code>, and <code>ProcessingInstruction</code>.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
463 *
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
464 * <p>
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
465 * When all objects in the supplied List are legal and before the new
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
466 * content is added, all objects in the old content will have their
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
467 * parentage set to null (no parent) and the old content list will be
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
468 * cleared. This has the effect that any active list (previously obtained
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
469 * with a call to {@link #getContent}) will also
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
470 * change to reflect the new content. In addition, all objects in the
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
471 * supplied List will have their parentage set to this document, but the
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
472 * List itself will not be "live" and further removals and additions will
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
473 * have no effect on this document content. If the user wants to continue
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
474 * working with a "live" list, then a call to setContent should be
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
475 * followed by a call to {@link #getContent} to
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
476 * obtain a "live" version of the content.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
477 * </p>
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
478 *
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
479 * <p>
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
480 * Passing a null or empty List clears the existing content.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
481 * </p>
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
482 *
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
483 * <p>
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
484 * In event of an exception the original content will be unchanged and
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
485 * the objects in the supplied content will be unaltered.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
486 * </p>
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
487 *
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
488 * @param newContent <code>List</code> of content to set
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
489 * @return this document modified
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
490 * @throws IllegalAddException if the List contains objects of
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
491 * illegal types or with existing parentage.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
492 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
493 public Document setContent(Collection newContent) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
494 content.clearAndSet(newContent);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
495 return this;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
496 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
497
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
498 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
499 *
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
500 * <p>
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
501 * Sets the effective URI from which this document was loaded,
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
502 * and against which relative URLs in this document will be resolved.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
503 * </p>
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
504 *
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
505 * @param uri the base URI of this document
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
506 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
507 public final void setBaseURI(String uri) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
508 this.baseURI = uri; // XXX We don't check the URI
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
509 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
510
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
511 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
512 * <p>
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
513 * Returns the URI from which this document was loaded,
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
514 * or null if this is not known.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
515 * </p>
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
516 *
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
517 * @return the base URI of this document
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
518 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
519 public final String getBaseURI() {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
520 return baseURI;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
521 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
522
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
523 /*
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
524 * Replace the current child the given index with the supplied child.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
525 * <p>
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
526 * In event of an exception the original content will be unchanged and
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
527 * the supplied child will be unaltered.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
528 * </p>
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
529 *
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
530 * @param index - index of child to replace.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
531 * @param child - child to add.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
532 * @throws IllegalAddException if the supplied child is already attached
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
533 * or not legal content for this parent.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
534 * @throws IndexOutOfBoundsException if index is negative or greater
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
535 * than the current number of children.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
536 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
537 public Document setContent(int index, Content child) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
538 content.set(index, child);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
539 return this;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
540 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
541
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
542 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
543 * Replace the child at the given index whith the supplied
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
544 * collection.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
545 * <p>
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
546 * In event of an exception the original content will be unchanged and
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
547 * the content in the supplied collection will be unaltered.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
548 * </p>
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
549 *
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
550 * @param index - index of child to replace.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
551 * @param collection - collection of content to add.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
552 * @return object on which the method was invoked
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
553 * @throws IllegalAddException if the collection contains objects of
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
554 * illegal types.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
555 * @throws IndexOutOfBoundsException if index is negative or greater
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
556 * than the current number of children.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
557 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
558 public Document setContent(int index, Collection collection) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
559 content.remove(index);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
560 content.addAll(index, collection);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
561 return this;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
562 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
563
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
564 public boolean removeContent(Content child) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
565 return content.remove(child);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
566 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
567
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
568 public Content removeContent(int index) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
569 return (Content) content.remove(index);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
570 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
571
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
572 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
573 * Set this document's content to be the supplied child.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
574 * <p>
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
575 * If the supplied child is legal content for a Document and before
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
576 * it is added, all content in the current content list will
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
577 * be cleared and all current children will have their parentage set to
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
578 * null.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
579 * <p>
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
580 * This has the effect that any active list (previously obtained with
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
581 * a call to one of the {@link #getContent} methods will also change
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
582 * to reflect the new content. In addition, all content in the supplied
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
583 * collection will have their parentage set to this Document. If the user
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
584 * wants to continue working with a <b>"live"</b> list of this Document's
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
585 * child, then a call to setContent should be followed by a call to one
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
586 * of the {@link #getContent} methods to obtain a <b>"live"</b>
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
587 * version of the children.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
588 * <p>
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
589 * Passing a null child clears the existing content.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
590 * <p>
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
591 * In event of an exception the original content will be unchanged and
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
592 * the supplied child will be unaltered.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
593 *
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
594 * @param child new content to replace existing content
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
595 * @return the parent on which the method was called
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
596 * @throws IllegalAddException if the supplied child is already attached
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
597 * or not legal content for this parent
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
598 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
599 public Document setContent(Content child) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
600 content.clear();
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
601 content.add(child);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
602 return this;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
603 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
604
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
605 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
606 * This returns a <code>String</code> representation of the
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
607 * <code>Document</code>, suitable for debugging. If the XML
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
608 * representation of the <code>Document</code> is desired,
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
609 * {@link org.jdom.output.XMLOutputter#outputString(Document)}
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
610 * should be used.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
611 *
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
612 * @return <code>String</code> - information about the
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
613 * <code>Document</code>
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
614 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
615 public String toString() {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
616 StringBuffer stringForm = new StringBuffer()
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
617 .append("[Document: ");
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
618
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
619 DocType docType = getDocType();
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
620 if (docType != null) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
621 stringForm.append(docType.toString())
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
622 .append(", ");
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
623 } else {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
624 stringForm.append(" No DOCTYPE declaration, ");
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
625 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
626
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
627 Element rootElement = getRootElement();
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
628 if (rootElement != null) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
629 stringForm.append("Root is ")
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
630 .append(rootElement.toString());
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
631 } else {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
632 stringForm.append(" No root element"); // shouldn't happen
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
633 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
634
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
635 stringForm.append("]");
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
636
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
637 return stringForm.toString();
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
638 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
639
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
640 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
641 * This tests for equality of this <code>Document</code> to the supplied
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
642 * <code>Object</code>.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
643 *
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
644 * @param ob <code>Object</code> to compare to
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
645 * @return <code>boolean</code> whether the <code>Document</code> is
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
646 * equal to the supplied <code>Object</code>
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
647 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
648 public final boolean equals(Object ob) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
649 return (ob == this);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
650 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
651
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
652 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
653 * This returns the hash code for this <code>Document</code>.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
654 *
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
655 * @return <code>int</code> hash code
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
656 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
657 public final int hashCode() {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
658 return super.hashCode();
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
659 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
660
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
661 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
662 * This will return a deep clone of this <code>Document</code>.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
663 *
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
664 * @return <code>Object</code> clone of this <code>Document</code>
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
665 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
666 public Object clone() {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
667 Document doc = null;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
668
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
669 try {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
670 doc = (Document) super.clone();
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
671 } catch (CloneNotSupportedException ce) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
672 // Can't happen
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
673 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
674
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
675 // The clone has a reference to this object's content list, so
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
676 // owerwrite with a empty list
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
677 doc.content = new ContentList(doc);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
678
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
679 // Add the cloned content to clone
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
680
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
681 for (int i = 0; i < content.size(); i++) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
682 Object obj = content.get(i);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
683 if (obj instanceof Element) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
684 Element element = (Element)((Element)obj).clone();
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
685 doc.content.add(element);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
686 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
687 else if (obj instanceof Comment) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
688 Comment comment = (Comment)((Comment)obj).clone();
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
689 doc.content.add(comment);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
690 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
691 else if (obj instanceof ProcessingInstruction) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
692 ProcessingInstruction pi = (ProcessingInstruction)
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
693 ((ProcessingInstruction)obj).clone();
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
694 doc.content.add(pi);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
695 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
696 else if (obj instanceof DocType) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
697 DocType dt = (DocType) ((DocType)obj).clone();
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
698 doc.content.add(dt);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
699 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
700 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
701
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
702 return doc;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
703 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
704
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
705 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
706 * Returns an iterator that walks over all descendants in document order.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
707 *
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
708 * @return an iterator to walk descendants
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
709 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
710 public Iterator getDescendants() {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
711 return new DescendantIterator(this);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
712 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
713
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
714 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
715 * Returns an iterator that walks over all descendants in document order
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
716 * applying the Filter to return only elements that match the filter rule.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
717 * With filters you can match only Elements, only Comments, Elements or
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
718 * Comments, only Elements with a given name and/or prefix, and so on.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
719 *
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
720 * @param filter filter to select which descendants to see
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
721 * @return an iterator to walk descendants within a filter
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
722 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
723 public Iterator getDescendants(Filter filter) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
724 return new FilterIterator(new DescendantIterator(this), filter);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
725 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
726
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
727 public Parent getParent() {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
728 return null; // documents never have parents
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
729 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
730
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
731
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
732
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
733 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
734 * @see org.jdom.Parent#getDocument()
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
735 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
736 public Document getDocument() {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
737 return this;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
738 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
739
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
740 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
741 * Assigns an arbitrary object to be associated with this document under
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
742 * the given "id" string. Null values are permitted. Strings beginning
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
743 * with "http://www.jdom.org/ are reserved for JDOM use.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
744 *
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
745 * @param id the id of the stored object
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
746 * @param value the object to store
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
747 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
748 public void setProperty(String id, Object value) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
749 if (propertyMap == null) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
750 propertyMap = new HashMap();
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
751 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
752 propertyMap.put(id, value);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
753 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
754
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
755 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
756 * Returns the object associated with this document under the given "id"
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
757 * string, or null if there is no binding or if the binding explicitly
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
758 * stored a null value.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
759 *
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
760 * @param id the id of the stored object to return
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
761 * @return the object associated with the given id
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
762 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
763 public Object getProperty(String id) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
764 if (propertyMap == null) return null;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
765 return propertyMap.get(id);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
766 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
767 }