|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| package org.dom4j.tree; |
|
11 |
| |
|
12 |
| import java.io.ByteArrayOutputStream; |
|
13 |
| import java.io.IOException; |
|
14 |
| import java.io.Writer; |
|
15 |
| import java.util.Iterator; |
|
16 |
| import java.util.List; |
|
17 |
| import java.util.Map; |
|
18 |
| |
|
19 |
| import org.dom4j.Comment; |
|
20 |
| import org.dom4j.Document; |
|
21 |
| import org.dom4j.DocumentType; |
|
22 |
| import org.dom4j.Element; |
|
23 |
| import org.dom4j.IllegalAddException; |
|
24 |
| import org.dom4j.Node; |
|
25 |
| import org.dom4j.ProcessingInstruction; |
|
26 |
| import org.dom4j.QName; |
|
27 |
| import org.dom4j.Text; |
|
28 |
| import org.dom4j.Visitor; |
|
29 |
| import org.dom4j.io.XMLWriter; |
|
30 |
| |
|
31 |
| |
|
32 |
| |
|
33 |
| |
|
34 |
| |
|
35 |
| |
|
36 |
| |
|
37 |
| public abstract class AbstractDocument extends AbstractBranch implements Document { |
|
38 |
| |
|
39 |
12002
| public AbstractDocument() {
|
|
40 |
| } |
|
41 |
| |
|
42 |
54
| public short getNodeType() {
|
|
43 |
54
| return DOCUMENT_NODE;
|
|
44 |
| } |
|
45 |
| |
|
46 |
2
| public String getPath(Element context) {
|
|
47 |
2
| return "/";
|
|
48 |
| } |
|
49 |
| |
|
50 |
2
| public String getUniquePath(Element context) {
|
|
51 |
2
| return "/";
|
|
52 |
| } |
|
53 |
| |
|
54 |
0
| public Document getDocument() {
|
|
55 |
0
| return this;
|
|
56 |
| } |
|
57 |
| |
|
58 |
0
| public String getXMLEncoding() {
|
|
59 |
0
| return null;
|
|
60 |
| } |
|
61 |
| |
|
62 |
0
| public String getStringValue() {
|
|
63 |
0
| Element root = getRootElement();
|
|
64 |
0
| return ( root != null ) ? root.getStringValue() : "";
|
|
65 |
| } |
|
66 |
| |
|
67 |
96
| public String asXML() {
|
|
68 |
96
| try {
|
|
69 |
96
| ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
70 |
96
| XMLWriter writer = new XMLWriter(out, outputFormat);
|
|
71 |
96
| writer.write(this);
|
|
72 |
96
| return out.toString();
|
|
73 |
| } |
|
74 |
| catch (IOException e) { |
|
75 |
0
| throw new RuntimeException("IOException while generating textual representation: " + e.getMessage());
|
|
76 |
| } |
|
77 |
| } |
|
78 |
| |
|
79 |
0
| public void write(Writer out) throws IOException {
|
|
80 |
0
| XMLWriter writer = new XMLWriter( out, outputFormat );
|
|
81 |
0
| writer.write(this);
|
|
82 |
| } |
|
83 |
| |
|
84 |
| |
|
85 |
| |
|
86 |
| |
|
87 |
| |
|
88 |
| |
|
89 |
0
| public void accept(Visitor visitor) {
|
|
90 |
0
| visitor.visit(this);
|
|
91 |
| |
|
92 |
0
| DocumentType docType = getDocType();
|
|
93 |
0
| if ( docType != null ) {
|
|
94 |
0
| visitor.visit( docType );
|
|
95 |
| } |
|
96 |
| |
|
97 |
| |
|
98 |
0
| List content = content();
|
|
99 |
0
| if (content != null) {
|
|
100 |
0
| for ( Iterator iter = content.iterator(); iter.hasNext(); ) {
|
|
101 |
0
| Object object = iter.next();
|
|
102 |
0
| if (object instanceof String) {
|
|
103 |
0
| Text text = getDocumentFactory().createText((String) object);
|
|
104 |
0
| visitor.visit(text);
|
|
105 |
| } |
|
106 |
| else { |
|
107 |
0
| Node node = (Node) object;
|
|
108 |
0
| node.accept(visitor);
|
|
109 |
| } |
|
110 |
| } |
|
111 |
| } |
|
112 |
| } |
|
113 |
| |
|
114 |
302
| public String toString() {
|
|
115 |
302
| return super.toString() + " [Document: name " + getName() + "]";
|
|
116 |
| } |
|
117 |
| |
|
118 |
208
| public void normalize() {
|
|
119 |
208
| Element element = getRootElement();
|
|
120 |
208
| if ( element != null ) {
|
|
121 |
208
| element.normalize();
|
|
122 |
| } |
|
123 |
| } |
|
124 |
| |
|
125 |
44
| public Document addComment(String comment) {
|
|
126 |
44
| Comment node = getDocumentFactory().createComment( comment );
|
|
127 |
44
| add( node );
|
|
128 |
44
| return this;
|
|
129 |
| } |
|
130 |
| |
|
131 |
42
| public Document addProcessingInstruction(String target, String data) {
|
|
132 |
42
| ProcessingInstruction node = getDocumentFactory().createProcessingInstruction( target, data );
|
|
133 |
42
| add( node );
|
|
134 |
42
| return this;
|
|
135 |
| } |
|
136 |
| |
|
137 |
0
| public Document addProcessingInstruction(String target, Map data) {
|
|
138 |
0
| ProcessingInstruction node = getDocumentFactory().createProcessingInstruction( target, data );
|
|
139 |
0
| add( node );
|
|
140 |
0
| return this;
|
|
141 |
| } |
|
142 |
| |
|
143 |
338
| public Element addElement(String name) {
|
|
144 |
338
| Element element = getDocumentFactory().createElement(name);
|
|
145 |
338
| add(element);
|
|
146 |
338
| return element;
|
|
147 |
| } |
|
148 |
| |
|
149 |
4
| public Element addElement(String qualifiedName, String namespaceURI) {
|
|
150 |
4
| Element element = getDocumentFactory().createElement(qualifiedName, namespaceURI);
|
|
151 |
4
| add(element);
|
|
152 |
4
| return element;
|
|
153 |
| } |
|
154 |
| |
|
155 |
11642
| public Element addElement(QName qName) {
|
|
156 |
11642
| Element element = getDocumentFactory().createElement(qName);
|
|
157 |
11642
| add(element);
|
|
158 |
11642
| return element;
|
|
159 |
| } |
|
160 |
| |
|
161 |
6
| public void setRootElement(Element rootElement) {
|
|
162 |
6
| clearContent();
|
|
163 |
6
| if ( rootElement != null ) {
|
|
164 |
6
| super.add(rootElement);
|
|
165 |
6
| rootElementAdded(rootElement);
|
|
166 |
| } |
|
167 |
| } |
|
168 |
| |
|
169 |
12012
| public void add(Element element) {
|
|
170 |
12012
| checkAddElementAllowed(element);
|
|
171 |
12010
| super.add(element);
|
|
172 |
12010
| rootElementAdded(element);
|
|
173 |
| } |
|
174 |
| |
|
175 |
4
| public boolean remove(Element element) {
|
|
176 |
4
| boolean answer = super.remove(element);
|
|
177 |
4
| Element root = getRootElement();
|
|
178 |
4
| if ( root != null && answer ) {
|
|
179 |
0
| setRootElement(null);
|
|
180 |
| } |
|
181 |
4
| element.setDocument(null);
|
|
182 |
4
| return answer;
|
|
183 |
| } |
|
184 |
| |
|
185 |
0
| public Node asXPathResult(Element parent) {
|
|
186 |
0
| return this;
|
|
187 |
| } |
|
188 |
| |
|
189 |
12104
| protected void childAdded(Node node) {
|
|
190 |
12104
| if (node != null ) {
|
|
191 |
12104
| node.setDocument(this);
|
|
192 |
| } |
|
193 |
| } |
|
194 |
| |
|
195 |
8
| protected void childRemoved(Node node) {
|
|
196 |
8
| if ( node != null ) {
|
|
197 |
8
| node.setDocument(null);
|
|
198 |
| } |
|
199 |
| } |
|
200 |
| |
|
201 |
12012
| protected void checkAddElementAllowed(Element element) {
|
|
202 |
12012
| Element root = getRootElement();
|
|
203 |
12012
| if ( root != null ) {
|
|
204 |
2
| throw new IllegalAddException(
|
|
205 |
| this, |
|
206 |
| element, |
|
207 |
| "Cannot add another element to this Document as it already has " |
|
208 |
| + " a root element of: " + root.getQualifiedName() |
|
209 |
| ); |
|
210 |
| } |
|
211 |
| } |
|
212 |
| |
|
213 |
| |
|
214 |
| protected abstract void rootElementAdded(Element rootElement); |
|
215 |
| |
|
216 |
| } |
|
217 |
| |
|
218 |
| |
|
219 |
| |
|
220 |
| |
|
221 |
| |
|
222 |
| |
|
223 |
| |
|
224 |
| |
|
225 |
| |
|
226 |
| |
|
227 |
| |
|
228 |
| |
|
229 |
| |
|
230 |
| |
|
231 |
| |
|
232 |
| |
|
233 |
| |
|
234 |
| |
|
235 |
| |
|
236 |
| |
|
237 |
| |
|
238 |
| |
|
239 |
| |
|
240 |
| |
|
241 |
| |
|
242 |
| |
|
243 |
| |
|
244 |
| |
|
245 |
| |
|
246 |
| |
|
247 |
| |
|
248 |
| |
|
249 |
| |
|
250 |
| |
|
251 |
| |
|
252 |
| |
|
253 |
| |
|
254 |
| |
|
255 |
| |
|
256 |
| |
|
257 |
| |
|
258 |
| |
|
259 |
| |
|
260 |
| |
|
261 |
| |
|
262 |
| |
|
263 |
| |
|
264 |
| |