1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
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 |
| |
26 |
| |
27 |
| |
28 |
| |
29 |
| |
30 |
| |
31 |
| |
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 |
| |
39 |
| private String name; |
40 |
| |
41 |
| |
42 |
| private Element rootElement; |
43 |
| |
44 |
| |
45 |
| |
46 |
| |
47 |
| private List content; |
48 |
| |
49 |
| |
50 |
| private DocumentType docType; |
51 |
| |
52 |
| |
53 |
| private DocumentFactory documentFactory = DocumentFactory.getInstance(); |
54 |
| |
55 |
| |
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 |
| |
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 |
| |
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 |
| |
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 |
| |
336 |
| |
337 |
| |
338 |
| |
339 |
| |
340 |
| |
341 |
| |
342 |
| |
343 |
| |
344 |
| |
345 |
| |
346 |
| |
347 |
| |
348 |
| |
349 |
| |
350 |
| |
351 |
| |
352 |
| |
353 |
| |
354 |
| |
355 |
| |
356 |
| |
357 |
| |
358 |
| |
359 |
| |
360 |
| |
361 |
| |
362 |
| |
363 |
| |
364 |
| |
365 |
| |
366 |
| |
367 |
| |
368 |
| |
369 |
| |