|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| package org.dom4j.io; |
|
11 |
| |
|
12 |
| import java.util.ArrayList; |
|
13 |
| import java.util.List; |
|
14 |
| |
|
15 |
| import org.dom4j.Branch; |
|
16 |
| import org.dom4j.Document; |
|
17 |
| import org.dom4j.DocumentFactory; |
|
18 |
| import org.dom4j.Element; |
|
19 |
| import org.dom4j.Namespace; |
|
20 |
| import org.dom4j.QName; |
|
21 |
| import org.dom4j.tree.NamespaceStack; |
|
22 |
| |
|
23 |
| |
|
24 |
| |
|
25 |
| |
|
26 |
| |
|
27 |
| |
|
28 |
| |
|
29 |
| public class DOMReader { |
|
30 |
| |
|
31 |
| |
|
32 |
| private DocumentFactory factory; |
|
33 |
| |
|
34 |
| |
|
35 |
| private NamespaceStack namespaceStack; |
|
36 |
| |
|
37 |
| |
|
38 |
40
| public DOMReader() {
|
|
39 |
40
| this.factory = DocumentFactory.getInstance();
|
|
40 |
40
| this.namespaceStack = new NamespaceStack(factory);
|
|
41 |
| } |
|
42 |
| |
|
43 |
0
| public DOMReader(DocumentFactory factory) {
|
|
44 |
0
| this.factory = factory;
|
|
45 |
0
| this.namespaceStack = new NamespaceStack(factory);
|
|
46 |
| } |
|
47 |
| |
|
48 |
| |
|
49 |
| |
|
50 |
40
| public DocumentFactory getDocumentFactory() {
|
|
51 |
40
| return factory;
|
|
52 |
| } |
|
53 |
| |
|
54 |
| |
|
55 |
| |
|
56 |
| |
|
57 |
| |
|
58 |
| |
|
59 |
| |
|
60 |
0
| public void setDocumentFactory(DocumentFactory factory) {
|
|
61 |
0
| this.factory = factory;
|
|
62 |
0
| this.namespaceStack.setDocumentFactory(factory);
|
|
63 |
| } |
|
64 |
| |
|
65 |
40
| public Document read(org.w3c.dom.Document domDocument) {
|
|
66 |
40
| if ( domDocument instanceof Document ) {
|
|
67 |
0
| return (Document) domDocument;
|
|
68 |
| } |
|
69 |
40
| Document document = createDocument();
|
|
70 |
| |
|
71 |
40
| clearNamespaceStack();
|
|
72 |
| |
|
73 |
40
| org.w3c.dom.NodeList nodeList = domDocument.getChildNodes();
|
|
74 |
40
| for ( int i = 0, size = nodeList.getLength(); i < size; i++ ) {
|
|
75 |
52
| readTree( nodeList.item(i), document );
|
|
76 |
| } |
|
77 |
40
| return document;
|
|
78 |
| } |
|
79 |
| |
|
80 |
| |
|
81 |
| |
|
82 |
28180
| protected void readTree(org.w3c.dom.Node node, Branch current) {
|
|
83 |
28180
| Element element = null;
|
|
84 |
28180
| Document document = null;
|
|
85 |
28180
| if ( current instanceof Element ) {
|
|
86 |
28128
| element = (Element) current;
|
|
87 |
| } |
|
88 |
| else { |
|
89 |
52
| document = (Document) current;
|
|
90 |
| } |
|
91 |
28180
| switch (node.getNodeType()) {
|
|
92 |
10238
| case org.w3c.dom.Node.ELEMENT_NODE:
|
|
93 |
10238
| readElement(node, current);
|
|
94 |
10238
| break;
|
|
95 |
| |
|
96 |
4
| case org.w3c.dom.Node.PROCESSING_INSTRUCTION_NODE:
|
|
97 |
4
| if ( current instanceof Element ) {
|
|
98 |
0
| ((Element) current).addProcessingInstruction(
|
|
99 |
| node.getNodeName(), node.getNodeValue() |
|
100 |
| ); |
|
101 |
| } |
|
102 |
| else { |
|
103 |
4
| ((Document) current).addProcessingInstruction(
|
|
104 |
| node.getNodeName(), node.getNodeValue() |
|
105 |
| ); |
|
106 |
| } |
|
107 |
4
| break;
|
|
108 |
| |
|
109 |
140
| case org.w3c.dom.Node.COMMENT_NODE:
|
|
110 |
140
| if ( current instanceof Element ) {
|
|
111 |
132
| ((Element) current).addComment( node.getNodeValue() );
|
|
112 |
| } |
|
113 |
| else { |
|
114 |
8
| ((Document) current).addComment( node.getNodeValue() );
|
|
115 |
| } |
|
116 |
140
| break;
|
|
117 |
| |
|
118 |
0
| case org.w3c.dom.Node.DOCUMENT_TYPE_NODE:
|
|
119 |
0
| org.w3c.dom.DocumentType domDocType
|
|
120 |
| = (org.w3c.dom.DocumentType) node; |
|
121 |
| |
|
122 |
0
| document.addDocType(
|
|
123 |
| domDocType.getName(), |
|
124 |
| domDocType.getPublicId(), |
|
125 |
| domDocType.getSystemId() |
|
126 |
| ); |
|
127 |
0
| break;
|
|
128 |
| |
|
129 |
17738
| case org.w3c.dom.Node.TEXT_NODE:
|
|
130 |
17738
| element.addText( node.getNodeValue() );
|
|
131 |
17738
| break;
|
|
132 |
| |
|
133 |
60
| case org.w3c.dom.Node.CDATA_SECTION_NODE:
|
|
134 |
60
| element.addCDATA( node.getNodeValue() );
|
|
135 |
60
| break;
|
|
136 |
| |
|
137 |
| |
|
138 |
0
| case org.w3c.dom.Node.ENTITY_REFERENCE_NODE: {
|
|
139 |
| |
|
140 |
0
| org.w3c.dom.Node firstChild = node.getFirstChild();
|
|
141 |
0
| if ( firstChild != null ) {
|
|
142 |
0
| element.addEntity(
|
|
143 |
| node.getNodeName(), |
|
144 |
| firstChild.getNodeValue() |
|
145 |
| ); |
|
146 |
| } |
|
147 |
| else { |
|
148 |
0
| element.addEntity( node.getNodeName(), "" );
|
|
149 |
| } |
|
150 |
| } |
|
151 |
0
| break;
|
|
152 |
| |
|
153 |
0
| case org.w3c.dom.Node.ENTITY_NODE:
|
|
154 |
0
| element.addEntity(
|
|
155 |
| node.getNodeName(), |
|
156 |
| node.getNodeValue() |
|
157 |
| ); |
|
158 |
0
| break;
|
|
159 |
| |
|
160 |
0
| default:
|
|
161 |
0
| System.out.println( "WARNING: Unknown DOM node type: " + node.getNodeType() );
|
|
162 |
| } |
|
163 |
| } |
|
164 |
| |
|
165 |
10238
| protected void readElement(org.w3c.dom.Node node, Branch current) {
|
|
166 |
10238
| int previouslyDeclaredNamespaces = namespaceStack.size();
|
|
167 |
| |
|
168 |
10238
| String namespaceUri = node.getNamespaceURI();
|
|
169 |
10238
| String elementPrefix = node.getPrefix();
|
|
170 |
10238
| if (elementPrefix == null) {
|
|
171 |
9502
| elementPrefix = "";
|
|
172 |
| } |
|
173 |
| |
|
174 |
10238
| org.w3c.dom.NamedNodeMap attributeList = node.getAttributes();
|
|
175 |
10238
| if (( attributeList != null ) && ( namespaceUri == null )) {
|
|
176 |
| |
|
177 |
9372
| org.w3c.dom.Node attribute = attributeList.getNamedItem( "xmlns" );
|
|
178 |
9372
| if ( attribute != null ) {
|
|
179 |
4
| namespaceUri = attribute.getNodeValue();
|
|
180 |
4
| elementPrefix = "";
|
|
181 |
| } |
|
182 |
| } |
|
183 |
| |
|
184 |
10238
| QName qName = namespaceStack.getQName( namespaceUri, node.getLocalName(), node.getNodeName() );
|
|
185 |
10238
| Element element = current.addElement(qName);
|
|
186 |
| |
|
187 |
10238
| if ( attributeList != null ) {
|
|
188 |
10238
| int size = attributeList.getLength();
|
|
189 |
10238
| List attributes = new ArrayList(size);
|
|
190 |
10238
| for ( int i = 0; i < size; i++ ) {
|
|
191 |
5658
| org.w3c.dom.Node attribute = attributeList.item(i);
|
|
192 |
| |
|
193 |
| |
|
194 |
5658
| String name = attribute.getNodeName();
|
|
195 |
5658
| if (name.startsWith("xmlns")) {
|
|
196 |
104
| String prefix = getPrefix(name);
|
|
197 |
104
| String uri = attribute.getNodeValue();
|
|
198 |
| |
|
199 |
| |
|
200 |
104
| Namespace namespace = namespaceStack.addNamespace( prefix, uri );
|
|
201 |
104
| element.add( namespace );
|
|
202 |
| |
|
203 |
| } |
|
204 |
| else { |
|
205 |
5554
| attributes.add( attribute );
|
|
206 |
| } |
|
207 |
| } |
|
208 |
| |
|
209 |
| |
|
210 |
10238
| size = attributes.size();
|
|
211 |
10238
| for ( int i = 0; i < size; i++ ) {
|
|
212 |
5554
| org.w3c.dom.Node attribute = (org.w3c.dom.Node) attributes.get(i);
|
|
213 |
5554
| QName attributeQName = namespaceStack.getQName(
|
|
214 |
| attribute.getNamespaceURI(), |
|
215 |
| attribute.getLocalName(), |
|
216 |
| attribute.getNodeName() |
|
217 |
| ); |
|
218 |
5554
| element.addAttribute( attributeQName, attribute.getNodeValue() );
|
|
219 |
| } |
|
220 |
| } |
|
221 |
| |
|
222 |
| |
|
223 |
10238
| org.w3c.dom.NodeList children = node.getChildNodes();
|
|
224 |
10238
| for ( int i = 0, size = children.getLength(); i < size; i++ ) {
|
|
225 |
28128
| org.w3c.dom.Node child = children.item(i);
|
|
226 |
28128
| readTree( child, element );
|
|
227 |
| } |
|
228 |
| |
|
229 |
| |
|
230 |
10238
| while (namespaceStack.size() > previouslyDeclaredNamespaces) {
|
|
231 |
104
| namespaceStack.pop();
|
|
232 |
| } |
|
233 |
| } |
|
234 |
| |
|
235 |
0
| protected Namespace getNamespace(String prefix, String uri) {
|
|
236 |
0
| return getDocumentFactory().createNamespace(prefix, uri);
|
|
237 |
| } |
|
238 |
| |
|
239 |
40
| protected Document createDocument() {
|
|
240 |
40
| return getDocumentFactory().createDocument();
|
|
241 |
| } |
|
242 |
| |
|
243 |
40
| protected void clearNamespaceStack() {
|
|
244 |
40
| namespaceStack.clear();
|
|
245 |
40
| if ( ! namespaceStack.contains( Namespace.XML_NAMESPACE ) ) {
|
|
246 |
40
| namespaceStack.push( Namespace.XML_NAMESPACE );
|
|
247 |
| } |
|
248 |
| } |
|
249 |
| |
|
250 |
104
| private String getPrefix(String xmlnsDecl) {
|
|
251 |
104
| int index = xmlnsDecl.indexOf(':', 5);
|
|
252 |
104
| if (index != -1) {
|
|
253 |
60
| return xmlnsDecl.substring(index + 1);
|
|
254 |
| } else { |
|
255 |
44
| return "";
|
|
256 |
| } |
|
257 |
| } |
|
258 |
| } |
|
259 |
| |
|
260 |
| |
|
261 |
| |
|
262 |
| |
|
263 |
| |
|
264 |
| |
|
265 |
| |
|
266 |
| |
|
267 |
| |
|
268 |
| |
|
269 |
| |
|
270 |
| |
|
271 |
| |
|
272 |
| |
|
273 |
| |
|
274 |
| |
|
275 |
| |
|
276 |
| |
|
277 |
| |
|
278 |
| |
|
279 |
| |
|
280 |
| |
|
281 |
| |
|
282 |
| |
|
283 |
| |
|
284 |
| |
|
285 |
| |
|
286 |
| |
|
287 |
| |
|
288 |
| |
|
289 |
| |
|
290 |
| |
|
291 |
| |
|
292 |
| |
|
293 |
| |
|
294 |
| |
|
295 |
| |
|
296 |
| |
|
297 |
| |
|
298 |
| |
|
299 |
| |
|
300 |
| |
|
301 |
| |
|
302 |
| |
|
303 |
| |
|
304 |
| |
|
305 |
| |
|
306 |
| |