1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| package org.dom4j.tree; |
11 |
| |
12 |
| import java.util.Collections; |
13 |
| import java.util.Iterator; |
14 |
| import java.util.List; |
15 |
| |
16 |
| import org.dom4j.Document; |
17 |
| import org.dom4j.DocumentFactory; |
18 |
| import org.dom4j.DocumentType; |
19 |
| import org.dom4j.Element; |
20 |
| import org.dom4j.IllegalAddException; |
21 |
| import org.dom4j.Node; |
22 |
| import org.dom4j.ProcessingInstruction; |
23 |
| import org.xml.sax.EntityResolver; |
24 |
| |
25 |
| |
26 |
| |
27 |
| |
28 |
| |
29 |
| |
30 |
| |
31 |
| public class DefaultDocument extends AbstractDocument { |
32 |
| |
33 |
| protected static final List EMPTY_LIST = Collections.EMPTY_LIST; |
34 |
| protected static final Iterator EMPTY_ITERATOR = EMPTY_LIST.iterator(); |
35 |
| |
36 |
| |
37 |
| private String encoding; |
38 |
| |
39 |
| |
40 |
| private String name; |
41 |
| |
42 |
| |
43 |
| private Element rootElement; |
44 |
| |
45 |
| |
46 |
| private List content; |
47 |
| |
48 |
| |
49 |
| private DocumentType docType; |
50 |
| |
51 |
| |
52 |
| private DocumentFactory documentFactory = DocumentFactory.getInstance(); |
53 |
| |
54 |
| |
55 |
| private transient EntityResolver entityResolver; |
56 |
| |
57 |
| |
58 |
11996
| public DefaultDocument() {
|
59 |
| } |
60 |
| |
61 |
4
| public DefaultDocument(String name) {
|
62 |
4
| this.name = name;
|
63 |
| } |
64 |
| |
65 |
2
| public DefaultDocument(Element rootElement) {
|
66 |
2
| 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, DocumentType docType) {
|
79 |
0
| this.name = name;
|
80 |
0
| this.rootElement = rootElement;
|
81 |
0
| this.docType = docType;
|
82 |
| } |
83 |
| |
84 |
404
| public String getName() {
|
85 |
404
| return name;
|
86 |
| } |
87 |
| |
88 |
11658
| public void setName(String name) {
|
89 |
11658
| this.name = name;
|
90 |
| } |
91 |
| |
92 |
| |
93 |
24794
| public Element getRootElement() {
|
94 |
24794
| return rootElement;
|
95 |
| } |
96 |
| |
97 |
734
| public DocumentType getDocType() {
|
98 |
734
| return docType;
|
99 |
| } |
100 |
| |
101 |
22
| public void setDocType(DocumentType docType) {
|
102 |
22
| this.docType = docType;
|
103 |
| } |
104 |
| |
105 |
22
| public Document addDocType(String name, String publicId, String systemId) {
|
106 |
22
| setDocType( getDocumentFactory().createDocType( name, publicId, systemId ) );
|
107 |
22
| return this;
|
108 |
| } |
109 |
| |
110 |
16
| public String getXMLEncoding() {
|
111 |
16
| return encoding;
|
112 |
| } |
113 |
| |
114 |
11602
| public void setXMLEncoding(String encoding) {
|
115 |
11602
| this.encoding = encoding;
|
116 |
| } |
117 |
| |
118 |
4
| public EntityResolver getEntityResolver() {
|
119 |
4
| return entityResolver;
|
120 |
| } |
121 |
| |
122 |
11600
| public void setEntityResolver(EntityResolver entityResolver) {
|
123 |
11600
| this.entityResolver = entityResolver;
|
124 |
| } |
125 |
| |
126 |
16
| public Object clone() {
|
127 |
16
| DefaultDocument document = (DefaultDocument) super.clone();
|
128 |
16
| document.rootElement = null;
|
129 |
16
| document.content = null;
|
130 |
16
| document.appendContent(this);
|
131 |
16
| 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 |
0
| for ( int i = 0; i < size; i++ ) {
|
139 |
0
| Object object = source.get(i);
|
140 |
0
| if ( object instanceof ProcessingInstruction ) {
|
141 |
0
| answer.add( object );
|
142 |
| } |
143 |
| } |
144 |
0
| return answer;
|
145 |
| } |
146 |
| |
147 |
0
| public List processingInstructions(String target) {
|
148 |
0
| List source = contentList();
|
149 |
0
| List answer = createResultList();
|
150 |
0
| int size = source.size();
|
151 |
0
| for ( int i = 0; i < size; i++ ) {
|
152 |
0
| Object object = source.get(i);
|
153 |
0
| if ( object instanceof ProcessingInstruction ) {
|
154 |
0
| ProcessingInstruction pi = (ProcessingInstruction) object;
|
155 |
0
| if ( target.equals( pi.getName() ) ) {
|
156 |
0
| answer.add( pi );
|
157 |
| } |
158 |
| } |
159 |
| } |
160 |
0
| return answer;
|
161 |
| } |
162 |
| |
163 |
0
| public ProcessingInstruction processingInstruction(String target) {
|
164 |
0
| List source = contentList();
|
165 |
0
| int size = source.size();
|
166 |
0
| for ( int i = 0; i < size; i++ ) {
|
167 |
0
| Object object = source.get(i);
|
168 |
0
| if ( object instanceof ProcessingInstruction ) {
|
169 |
0
| ProcessingInstruction pi = (ProcessingInstruction) object;
|
170 |
0
| if ( target.equals( pi.getName() ) ) {
|
171 |
0
| return pi;
|
172 |
| } |
173 |
| } |
174 |
| } |
175 |
0
| return null;
|
176 |
| } |
177 |
| |
178 |
0
| public boolean removeProcessingInstruction(String target) {
|
179 |
0
| List source = contentList();
|
180 |
0
| for ( Iterator iter = source.iterator(); iter.hasNext(); ) {
|
181 |
0
| Object object = iter.next();
|
182 |
0
| if ( object instanceof ProcessingInstruction ) {
|
183 |
0
| ProcessingInstruction pi = (ProcessingInstruction) object;
|
184 |
0
| if ( target.equals( pi.getName() ) ) {
|
185 |
0
| iter.remove();
|
186 |
0
| return true;
|
187 |
| } |
188 |
| } |
189 |
| } |
190 |
0
| return false;
|
191 |
| } |
192 |
| |
193 |
| |
194 |
2
| public void setContent(List content) {
|
195 |
2
| rootElement = null;
|
196 |
2
| contentRemoved();
|
197 |
2
| if ( content instanceof ContentListFacade ) {
|
198 |
2
| content = ((ContentListFacade) content).getBackingList();
|
199 |
| } |
200 |
2
| if ( content == null ) {
|
201 |
0
| this.content = null;
|
202 |
| } |
203 |
| else { |
204 |
2
| int size = content.size();
|
205 |
2
| List newContent = createContentList( size );
|
206 |
2
| for ( int i = 0; i < size; i++ ) {
|
207 |
2
| Object object = content.get(i);
|
208 |
2
| if ( object instanceof Node ) {
|
209 |
2
| Node node = (Node) object;
|
210 |
2
| Document doc = node.getDocument();
|
211 |
2
| if ( doc != null && doc != this ) {
|
212 |
2
| node = (Node) node.clone();
|
213 |
| } |
214 |
2
| if ( node instanceof Element ) {
|
215 |
2
| if ( rootElement == null ) {
|
216 |
2
| rootElement = (Element) node;
|
217 |
| } |
218 |
| else { |
219 |
0
| throw new IllegalAddException( "A document may only contain one Element: " + content );
|
220 |
| } |
221 |
| } |
222 |
2
| newContent.add( node );
|
223 |
2
| childAdded( node );
|
224 |
| } |
225 |
| } |
226 |
2
| this.content = newContent;
|
227 |
| } |
228 |
| } |
229 |
| |
230 |
8
| public void clearContent() {
|
231 |
8
| contentRemoved();
|
232 |
8
| content = null;
|
233 |
8
| rootElement = null;
|
234 |
| } |
235 |
| |
236 |
| |
237 |
12006
| public void setDocumentFactory(DocumentFactory documentFactory) {
|
238 |
12006
| this.documentFactory = documentFactory;
|
239 |
| } |
240 |
| |
241 |
| |
242 |
| |
243 |
| |
244 |
14202
| protected List contentList() {
|
245 |
14202
| if (content == null) {
|
246 |
12024
| content = createContentList();
|
247 |
12024
| if (rootElement != null) {
|
248 |
2
| content.add( rootElement );
|
249 |
| } |
250 |
| } |
251 |
14202
| return content;
|
252 |
| } |
253 |
| |
254 |
| |
255 |
12102
| protected void addNode(Node node) {
|
256 |
12102
| if ( node != null ) {
|
257 |
12102
| Document document = node.getDocument();
|
258 |
12102
| if (document != null && document != this) {
|
259 |
| |
260 |
0
| String message = "The Node already has an existing document: " + document;
|
261 |
0
| throw new IllegalAddException(this, node, message);
|
262 |
| } |
263 |
12102
| contentList().add(node);
|
264 |
12102
| childAdded(node);
|
265 |
| } |
266 |
| } |
267 |
| |
268 |
0
| protected void addNode(int index, Node node) {
|
269 |
0
| if ( node != null ) {
|
270 |
0
| Document document = node.getDocument();
|
271 |
0
| if (document != null && document != this) {
|
272 |
| |
273 |
0
| String message = "The Node already has an existing document: " + document;
|
274 |
0
| throw new IllegalAddException(this, node, message);
|
275 |
| } |
276 |
0
| contentList().add(index, node);
|
277 |
0
| childAdded(node);
|
278 |
| } |
279 |
| } |
280 |
| |
281 |
4
| protected boolean removeNode(Node node) {
|
282 |
4
| if ( node == rootElement) {
|
283 |
4
| rootElement = null;
|
284 |
| } |
285 |
4
| if (contentList().remove(node)) {
|
286 |
4
| childRemoved(node);
|
287 |
4
| return true;
|
288 |
| } |
289 |
0
| return false;
|
290 |
| } |
291 |
| |
292 |
12016
| protected void rootElementAdded(Element element) {
|
293 |
12016
| this.rootElement = element;
|
294 |
12016
| element.setDocument(this);
|
295 |
| } |
296 |
| |
297 |
| |
298 |
13292
| protected DocumentFactory getDocumentFactory() {
|
299 |
13292
| return documentFactory;
|
300 |
| } |
301 |
| |
302 |
| } |
303 |
| |
304 |
| |
305 |
| |
306 |
| |
307 |
| |
308 |
| |
309 |
| |
310 |
| |
311 |
| |
312 |
| |
313 |
| |
314 |
| |
315 |
| |
316 |
| |
317 |
| |
318 |
| |
319 |
| |
320 |
| |
321 |
| |
322 |
| |
323 |
| |
324 |
| |
325 |
| |
326 |
| |
327 |
| |
328 |
| |
329 |
| |
330 |
| |
331 |
| |
332 |
| |
333 |
| |
334 |
| |
335 |
| |
336 |
| |
337 |
| |
338 |
| |
339 |
| |
340 |
| |
341 |
| |
342 |
| |
343 |
| |
344 |
| |
345 |
| |
346 |
| |
347 |
| |
348 |
| |
349 |
| |
350 |
| |