|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| package org.dom4j.dom; |
|
11 |
| |
|
12 |
| import java.util.List; |
|
13 |
| |
|
14 |
| import org.dom4j.Branch; |
|
15 |
| import org.dom4j.CharacterData; |
|
16 |
| import org.dom4j.Document; |
|
17 |
| import org.dom4j.DocumentType; |
|
18 |
| import org.dom4j.Element; |
|
19 |
| import org.dom4j.Node; |
|
20 |
| import org.w3c.dom.DOMException; |
|
21 |
| import org.w3c.dom.NamedNodeMap; |
|
22 |
| import org.w3c.dom.NodeList; |
|
23 |
| |
|
24 |
| |
|
25 |
| |
|
26 |
| |
|
27 |
| |
|
28 |
| |
|
29 |
| |
|
30 |
| public class DOMNodeHelper { |
|
31 |
| |
|
32 |
| public static final NodeList EMPTY_NODE_LIST = new EmptyNodeList(); |
|
33 |
| |
|
34 |
| public static class EmptyNodeList implements NodeList { |
|
35 |
0
| public org.w3c.dom.Node item(int index) {
|
|
36 |
0
| return null;
|
|
37 |
| } |
|
38 |
0
| public int getLength() {
|
|
39 |
0
| return 0;
|
|
40 |
| } |
|
41 |
| } |
|
42 |
| |
|
43 |
| |
|
44 |
| |
|
45 |
| |
|
46 |
0
| public static boolean supports(Node node, String feature, String version) {
|
|
47 |
0
| return false;
|
|
48 |
| } |
|
49 |
| |
|
50 |
0
| public static String getNamespaceURI(Node node) {
|
|
51 |
0
| return null;
|
|
52 |
| } |
|
53 |
| |
|
54 |
19548
| public static String getPrefix(Node node) {
|
|
55 |
19548
| return null;
|
|
56 |
| } |
|
57 |
| |
|
58 |
0
| public static String getLocalName(Node node) {
|
|
59 |
0
| return null;
|
|
60 |
| } |
|
61 |
| |
|
62 |
0
| public static void setPrefix(Node node, String prefix) throws DOMException {
|
|
63 |
0
| notSupported();
|
|
64 |
| } |
|
65 |
| |
|
66 |
132
| public static String getNodeValue(Node node) throws DOMException {
|
|
67 |
132
| return node.getText();
|
|
68 |
| } |
|
69 |
| |
|
70 |
0
| public static void setNodeValue(Node node, String nodeValue) throws DOMException {
|
|
71 |
0
| node.setText(nodeValue);
|
|
72 |
| } |
|
73 |
| |
|
74 |
58
| public static org.w3c.dom.Node getParentNode(Node node) {
|
|
75 |
58
| return asDOMNode( node.getParent() );
|
|
76 |
| } |
|
77 |
| |
|
78 |
0
| public static NodeList getChildNodes(Node node) {
|
|
79 |
0
| return EMPTY_NODE_LIST;
|
|
80 |
| } |
|
81 |
| |
|
82 |
0
| public static org.w3c.dom.Node getFirstChild(Node node) {
|
|
83 |
0
| return null;
|
|
84 |
| } |
|
85 |
| |
|
86 |
0
| public static org.w3c.dom.Node getLastChild(Node node) {
|
|
87 |
0
| return null;
|
|
88 |
| } |
|
89 |
| |
|
90 |
0
| public static org.w3c.dom.Node getPreviousSibling(Node node) {
|
|
91 |
0
| Element parent = node.getParent();
|
|
92 |
0
| if ( parent != null ) {
|
|
93 |
0
| int index = parent.indexOf( node );
|
|
94 |
0
| if ( index > 0 ) {
|
|
95 |
0
| Node previous = parent.node(index - 1);
|
|
96 |
0
| return asDOMNode( previous );
|
|
97 |
| } |
|
98 |
| } |
|
99 |
0
| return null;
|
|
100 |
| } |
|
101 |
| |
|
102 |
0
| public static org.w3c.dom.Node getNextSibling(Node node) {
|
|
103 |
0
| Element parent = node.getParent();
|
|
104 |
0
| if ( parent != null ) {
|
|
105 |
0
| int index = parent.indexOf( node );
|
|
106 |
0
| if ( index >= 0 ) {
|
|
107 |
0
| if ( ++index < parent.nodeCount() ) {
|
|
108 |
0
| Node next = parent.node(index);
|
|
109 |
0
| return asDOMNode( next );
|
|
110 |
| } |
|
111 |
| } |
|
112 |
| } |
|
113 |
0
| return null;
|
|
114 |
| } |
|
115 |
| |
|
116 |
0
| public static NamedNodeMap getAttributes(Node node) {
|
|
117 |
0
| return null;
|
|
118 |
| } |
|
119 |
| |
|
120 |
0
| public static org.w3c.dom.Document getOwnerDocument(Node node) {
|
|
121 |
0
| return asDOMDocument( node.getDocument() );
|
|
122 |
| } |
|
123 |
| |
|
124 |
0
| public static org.w3c.dom.Node insertBefore(
|
|
125 |
| Node node, |
|
126 |
| org.w3c.dom.Node newChild, |
|
127 |
| org.w3c.dom.Node refChild |
|
128 |
| ) throws DOMException { |
|
129 |
0
| if ( node instanceof Branch ) {
|
|
130 |
0
| Branch branch = (Branch) node;
|
|
131 |
0
| List list = branch.content();
|
|
132 |
0
| int index = list.indexOf(refChild);
|
|
133 |
0
| if ( index < 0 ) {
|
|
134 |
0
| branch.add((Node) newChild);
|
|
135 |
| } |
|
136 |
| else { |
|
137 |
0
| list.add(index, newChild);
|
|
138 |
| } |
|
139 |
0
| return newChild;
|
|
140 |
| } |
|
141 |
| else { |
|
142 |
0
| throw new DOMException( DOMException.HIERARCHY_REQUEST_ERR, "Children not allowed for this node: " + node );
|
|
143 |
| } |
|
144 |
| } |
|
145 |
| |
|
146 |
4
| public static org.w3c.dom.Node replaceChild(
|
|
147 |
| Node node, |
|
148 |
| org.w3c.dom.Node newChild, |
|
149 |
| org.w3c.dom.Node oldChild |
|
150 |
| ) throws DOMException { |
|
151 |
4
| if ( node instanceof Branch ) {
|
|
152 |
4
| Branch branch = (Branch) node;
|
|
153 |
4
| List list = branch.content();
|
|
154 |
4
| int index = list.indexOf(oldChild);
|
|
155 |
4
| if ( index < 0 ) {
|
|
156 |
2
| throw new DOMException( DOMException.NOT_FOUND_ERR, "Tried to replace a non existing child for node: " + node );
|
|
157 |
| } |
|
158 |
2
| list.set(index, newChild);
|
|
159 |
2
| return oldChild;
|
|
160 |
| } |
|
161 |
| else { |
|
162 |
0
| throw new DOMException( DOMException.HIERARCHY_REQUEST_ERR, "Children not allowed for this node: " + node );
|
|
163 |
| } |
|
164 |
| } |
|
165 |
| |
|
166 |
0
| public static org.w3c.dom.Node removeChild(
|
|
167 |
| Node node, |
|
168 |
| org.w3c.dom.Node oldChild |
|
169 |
| ) throws DOMException { |
|
170 |
0
| if ( node instanceof Branch ) {
|
|
171 |
0
| Branch branch = (Branch) node;
|
|
172 |
0
| branch.remove((Node) oldChild);
|
|
173 |
0
| return oldChild;
|
|
174 |
| } |
|
175 |
0
| throw new DOMException( DOMException.HIERARCHY_REQUEST_ERR, "Children not allowed for this node: " + node );
|
|
176 |
| } |
|
177 |
| |
|
178 |
58
| public static org.w3c.dom.Node appendChild(
|
|
179 |
| Node node, |
|
180 |
| org.w3c.dom.Node newChild |
|
181 |
| ) throws DOMException { |
|
182 |
58
| if ( node instanceof Branch ) {
|
|
183 |
58
| Branch branch = (Branch) node;
|
|
184 |
58
| org.w3c.dom.Node previousParent = newChild.getParentNode();
|
|
185 |
58
| if (previousParent != null) {
|
|
186 |
0
| previousParent.removeChild(newChild);
|
|
187 |
| } |
|
188 |
58
| branch.add( (Node) newChild );
|
|
189 |
58
| return newChild;
|
|
190 |
| } |
|
191 |
0
| throw new DOMException( DOMException.HIERARCHY_REQUEST_ERR, "Children not allowed for this node: " + node );
|
|
192 |
| } |
|
193 |
| |
|
194 |
0
| public static boolean hasChildNodes(Node node) {
|
|
195 |
0
| return false;
|
|
196 |
| } |
|
197 |
| |
|
198 |
0
| public static org.w3c.dom.Node cloneNode(Node node, boolean deep) {
|
|
199 |
0
| return asDOMNode( (Node) node.clone() );
|
|
200 |
| } |
|
201 |
| |
|
202 |
0
| public static void normalize(Node node) {
|
|
203 |
0
| notSupported();
|
|
204 |
| } |
|
205 |
| |
|
206 |
0
| public static boolean isSupported(Node node, String feature, String version) {
|
|
207 |
0
| return false;
|
|
208 |
| } |
|
209 |
| |
|
210 |
0
| public static boolean hasAttributes(Node node) {
|
|
211 |
0
| if (node != null && node instanceof Element) {
|
|
212 |
0
| return ((Element) node).attributeCount() > 0;
|
|
213 |
| } else { |
|
214 |
0
| return false;
|
|
215 |
| } |
|
216 |
| } |
|
217 |
| |
|
218 |
| |
|
219 |
| |
|
220 |
| |
|
221 |
| |
|
222 |
0
| public static String getData(CharacterData charData) throws DOMException {
|
|
223 |
0
| return charData.getText();
|
|
224 |
| } |
|
225 |
| |
|
226 |
0
| public static void setData(CharacterData charData, String data) throws DOMException {
|
|
227 |
0
| charData.setText(data);
|
|
228 |
| } |
|
229 |
| |
|
230 |
0
| public static int getLength(CharacterData charData) {
|
|
231 |
0
| String text = charData.getText();
|
|
232 |
0
| return ( text != null ) ? text.length() : 0;
|
|
233 |
| } |
|
234 |
| |
|
235 |
0
| public static String substringData(
|
|
236 |
| CharacterData charData, int offset, int count |
|
237 |
| ) throws DOMException { |
|
238 |
0
| if (count < 0) {
|
|
239 |
0
| throw new DOMException(
|
|
240 |
| DOMException.INDEX_SIZE_ERR, |
|
241 |
| "Illegal value for count: " + count |
|
242 |
| ); |
|
243 |
| } |
|
244 |
0
| String text = charData.getText();
|
|
245 |
0
| int length = ( text != null ) ? text.length() : 0;
|
|
246 |
0
| if ( offset < 0 || offset >= length ) {
|
|
247 |
0
| throw new DOMException(
|
|
248 |
| DOMException.INDEX_SIZE_ERR, |
|
249 |
| "No text at offset: " + offset |
|
250 |
| ); |
|
251 |
| } |
|
252 |
0
| if (offset + count > length) {
|
|
253 |
0
| return text.substring(offset);
|
|
254 |
| } |
|
255 |
0
| return text.substring( offset, offset + count );
|
|
256 |
| } |
|
257 |
| |
|
258 |
0
| public static void appendData(
|
|
259 |
| CharacterData charData, String arg |
|
260 |
| ) throws DOMException { |
|
261 |
0
| if ( charData.isReadOnly() ) {
|
|
262 |
0
| throw new DOMException(
|
|
263 |
| DOMException.NO_MODIFICATION_ALLOWED_ERR, |
|
264 |
| "CharacterData node is read only: " + charData |
|
265 |
| ); |
|
266 |
| } |
|
267 |
| else { |
|
268 |
0
| String text = charData.getText();
|
|
269 |
0
| if ( text == null ) {
|
|
270 |
0
| charData.setText( text );
|
|
271 |
| } |
|
272 |
| else { |
|
273 |
0
| charData.setText( text + arg );
|
|
274 |
| } |
|
275 |
| } |
|
276 |
| } |
|
277 |
| |
|
278 |
0
| public static void insertData(CharacterData charData, int offset, String arg) throws DOMException {
|
|
279 |
0
| if ( charData.isReadOnly() ) {
|
|
280 |
0
| throw new DOMException(
|
|
281 |
| DOMException.NO_MODIFICATION_ALLOWED_ERR, |
|
282 |
| "CharacterData node is read only: " + charData |
|
283 |
| ); |
|
284 |
| } |
|
285 |
| else { |
|
286 |
0
| String text = charData.getText();
|
|
287 |
0
| if ( text == null ) {
|
|
288 |
0
| charData.setText( arg );
|
|
289 |
| } |
|
290 |
| else { |
|
291 |
0
| int length = text.length();
|
|
292 |
0
| if ( offset < 0 || offset > length ) {
|
|
293 |
0
| throw new DOMException(
|
|
294 |
| DOMException.INDEX_SIZE_ERR, |
|
295 |
| "No text at offset: " + offset |
|
296 |
| ); |
|
297 |
| } |
|
298 |
| else { |
|
299 |
0
| StringBuffer buffer = new StringBuffer( text );
|
|
300 |
0
| buffer.insert( offset, arg );
|
|
301 |
0
| charData.setText( buffer.toString() );
|
|
302 |
| } |
|
303 |
| } |
|
304 |
| } |
|
305 |
| } |
|
306 |
| |
|
307 |
0
| public static void deleteData(CharacterData charData, int offset, int count) throws DOMException {
|
|
308 |
0
| if ( charData.isReadOnly() ) {
|
|
309 |
0
| throw new DOMException(
|
|
310 |
| DOMException.NO_MODIFICATION_ALLOWED_ERR, |
|
311 |
| "CharacterData node is read only: " + charData |
|
312 |
| ); |
|
313 |
| } |
|
314 |
| else { |
|
315 |
0
| if (count < 0) {
|
|
316 |
0
| throw new DOMException(
|
|
317 |
| DOMException.INDEX_SIZE_ERR, |
|
318 |
| "Illegal value for count: " + count |
|
319 |
| ); |
|
320 |
| } |
|
321 |
0
| String text = charData.getText();
|
|
322 |
0
| if ( text != null ) {
|
|
323 |
0
| int length = text.length();
|
|
324 |
0
| if ( offset < 0 || offset >= length ) {
|
|
325 |
0
| throw new DOMException(
|
|
326 |
| DOMException.INDEX_SIZE_ERR, |
|
327 |
| "No text at offset: " + offset |
|
328 |
| ); |
|
329 |
| } |
|
330 |
| else { |
|
331 |
0
| StringBuffer buffer = new StringBuffer( text );
|
|
332 |
0
| buffer.delete( offset, offset + count );
|
|
333 |
0
| charData.setText( buffer.toString() );
|
|
334 |
| } |
|
335 |
| } |
|
336 |
| } |
|
337 |
| } |
|
338 |
| |
|
339 |
0
| public static void replaceData(
|
|
340 |
| CharacterData charData, int offset, int count, String arg |
|
341 |
| ) throws DOMException { |
|
342 |
0
| if ( charData.isReadOnly() ) {
|
|
343 |
0
| throw new DOMException(
|
|
344 |
| DOMException.NO_MODIFICATION_ALLOWED_ERR, |
|
345 |
| "CharacterData node is read only: " + charData |
|
346 |
| ); |
|
347 |
| } |
|
348 |
| else { |
|
349 |
0
| if (count < 0) {
|
|
350 |
0
| throw new DOMException(
|
|
351 |
| DOMException.INDEX_SIZE_ERR, |
|
352 |
| "Illegal value for count: " + count |
|
353 |
| ); |
|
354 |
| } |
|
355 |
0
| String text = charData.getText();
|
|
356 |
0
| if ( text != null ) {
|
|
357 |
0
| int length = text.length();
|
|
358 |
0
| if ( offset < 0 || offset >= length ) {
|
|
359 |
0
| throw new DOMException(
|
|
360 |
| DOMException.INDEX_SIZE_ERR, |
|
361 |
| "No text at offset: " + offset |
|
362 |
| ); |
|
363 |
| } |
|
364 |
| else { |
|
365 |
0
| StringBuffer buffer = new StringBuffer( text );
|
|
366 |
0
| buffer.replace( offset, offset + count, arg );
|
|
367 |
0
| charData.setText( buffer.toString() );
|
|
368 |
| } |
|
369 |
| } |
|
370 |
| } |
|
371 |
| } |
|
372 |
| |
|
373 |
| |
|
374 |
| |
|
375 |
| |
|
376 |
| |
|
377 |
0
| public static void appendElementsByTagName(
|
|
378 |
| List list, Branch parent, String name |
|
379 |
| ) { |
|
380 |
0
| final boolean isStar = "*".equals(name);
|
|
381 |
0
| for ( int i = 0, size = parent.nodeCount(); i < size; i++ ) {
|
|
382 |
0
| Node node = parent.node(i);
|
|
383 |
0
| if ( node instanceof Element ) {
|
|
384 |
0
| Element element = (Element) node;
|
|
385 |
0
| if ( isStar || name.equals( element.getName() ) ) {
|
|
386 |
0
| list.add( element );
|
|
387 |
| } |
|
388 |
0
| appendElementsByTagName(list, element, name);
|
|
389 |
| } |
|
390 |
| } |
|
391 |
| } |
|
392 |
| |
|
393 |
0
| public static void appendElementsByTagNameNS(
|
|
394 |
| List list, Branch parent, String namespaceURI, String localName |
|
395 |
| ) { |
|
396 |
0
| final boolean isStarNS = "*".equals(namespaceURI);
|
|
397 |
0
| final boolean isStarName = "*".equals(localName);
|
|
398 |
0
| for ( int i = 0, size = parent.nodeCount(); i < size; i++ ) {
|
|
399 |
0
| Node node = parent.node(i);
|
|
400 |
0
| if ( node instanceof Element ) {
|
|
401 |
0
| Element element = (Element) node;
|
|
402 |
0
| if ( ( (isStarNS ||
|
|
403 |
| ((namespaceURI == null || namespaceURI.length() == 0) && |
|
404 |
| (element.getNamespaceURI() == null || element.getNamespaceURI().length() == 0)) || |
|
405 |
| (namespaceURI != null && namespaceURI.equals( element.getNamespaceURI() )))) |
|
406 |
| && (isStarName || localName.equals( element.getName() )) ) { |
|
407 |
0
| list.add( element );
|
|
408 |
| } |
|
409 |
0
| appendElementsByTagNameNS(list, element, namespaceURI, localName);
|
|
410 |
| } |
|
411 |
| } |
|
412 |
| } |
|
413 |
| |
|
414 |
| |
|
415 |
| |
|
416 |
| |
|
417 |
| |
|
418 |
68
| public static NodeList createNodeList( final List list ) {
|
|
419 |
68
| return new NodeList() {
|
|
420 |
210
| public org.w3c.dom.Node item(int index) {
|
|
421 |
210
| if (index >= getLength()) {
|
|
422 |
| |
|
423 |
| |
|
424 |
| |
|
425 |
| |
|
426 |
| |
|
427 |
0
| return null;
|
|
428 |
| } else { |
|
429 |
210
| return DOMNodeHelper.asDOMNode( (Node) list.get( index ) );
|
|
430 |
| } |
|
431 |
| } |
|
432 |
276
| public int getLength() {
|
|
433 |
276
| return list.size();
|
|
434 |
| } |
|
435 |
| }; |
|
436 |
| } |
|
437 |
| |
|
438 |
268
| public static org.w3c.dom.Node asDOMNode(Node node) {
|
|
439 |
268
| if ( node == null ) {
|
|
440 |
58
| return null;
|
|
441 |
| } |
|
442 |
210
| if ( node instanceof org.w3c.dom.Node ) {
|
|
443 |
210
| return (org.w3c.dom.Node) node;
|
|
444 |
| } |
|
445 |
| else { |
|
446 |
| |
|
447 |
0
| System.out.println( "Cannot convert: " + node + " into a W3C DOM Node");
|
|
448 |
0
| notSupported();
|
|
449 |
0
| return null;
|
|
450 |
| } |
|
451 |
| } |
|
452 |
| |
|
453 |
0
| public static org.w3c.dom.Document asDOMDocument(Document document) {
|
|
454 |
0
| if ( document == null ) {
|
|
455 |
0
| return null;
|
|
456 |
| } |
|
457 |
0
| if ( document instanceof org.w3c.dom.Document ) {
|
|
458 |
0
| return (org.w3c.dom.Document) document;
|
|
459 |
| } |
|
460 |
| else { |
|
461 |
| |
|
462 |
0
| notSupported();
|
|
463 |
0
| return null;
|
|
464 |
| } |
|
465 |
| } |
|
466 |
| |
|
467 |
0
| public static org.w3c.dom.DocumentType asDOMDocumentType(DocumentType documentType) {
|
|
468 |
0
| if ( documentType == null ) {
|
|
469 |
0
| return null;
|
|
470 |
| } |
|
471 |
0
| if ( documentType instanceof org.w3c.dom.DocumentType ) {
|
|
472 |
0
| return (org.w3c.dom.DocumentType) documentType;
|
|
473 |
| } |
|
474 |
| else { |
|
475 |
| |
|
476 |
0
| notSupported();
|
|
477 |
0
| return null;
|
|
478 |
| } |
|
479 |
| } |
|
480 |
| |
|
481 |
0
| public static org.w3c.dom.Text asDOMText(CharacterData text) {
|
|
482 |
0
| if ( text == null ) {
|
|
483 |
0
| return null;
|
|
484 |
| } |
|
485 |
0
| if ( text instanceof org.w3c.dom.Text ) {
|
|
486 |
0
| return (org.w3c.dom.Text) text;
|
|
487 |
| } |
|
488 |
| else { |
|
489 |
| |
|
490 |
0
| notSupported();
|
|
491 |
0
| return null;
|
|
492 |
| } |
|
493 |
| } |
|
494 |
| |
|
495 |
6
| public static org.w3c.dom.Element asDOMElement(Node element) {
|
|
496 |
6
| if ( element == null ) {
|
|
497 |
0
| return null;
|
|
498 |
| } |
|
499 |
6
| if ( element instanceof org.w3c.dom.Element ) {
|
|
500 |
6
| return (org.w3c.dom.Element) element;
|
|
501 |
| } |
|
502 |
| else { |
|
503 |
| |
|
504 |
0
| notSupported();
|
|
505 |
0
| return null;
|
|
506 |
| } |
|
507 |
| } |
|
508 |
| |
|
509 |
0
| public static org.w3c.dom.Attr asDOMAttr(Node attribute) {
|
|
510 |
0
| if ( attribute == null ) {
|
|
511 |
0
| return null;
|
|
512 |
| } |
|
513 |
0
| if ( attribute instanceof org.w3c.dom.Attr ) {
|
|
514 |
0
| return (org.w3c.dom.Attr) attribute;
|
|
515 |
| } |
|
516 |
| else { |
|
517 |
| |
|
518 |
0
| notSupported();
|
|
519 |
0
| return null;
|
|
520 |
| } |
|
521 |
| } |
|
522 |
| |
|
523 |
| |
|
524 |
| |
|
525 |
0
| public static void notSupported() {
|
|
526 |
0
| throw new DOMException( DOMException.NOT_SUPPORTED_ERR, "Not supported yet");
|
|
527 |
| } |
|
528 |
| |
|
529 |
| } |
|
530 |
| |
|
531 |
| |
|
532 |
| |
|
533 |
| |
|
534 |
| |
|
535 |
| |
|
536 |
| |
|
537 |
| |
|
538 |
| |
|
539 |
| |
|
540 |
| |
|
541 |
| |
|
542 |
| |
|
543 |
| |
|
544 |
| |
|
545 |
| |
|
546 |
| |
|
547 |
| |
|
548 |
| |
|
549 |
| |
|
550 |
| |
|
551 |
| |
|
552 |
| |
|
553 |
| |
|
554 |
| |
|
555 |
| |
|
556 |
| |
|
557 |
| |
|
558 |
| |
|
559 |
| |
|
560 |
| |
|
561 |
| |
|
562 |
| |
|
563 |
| |
|
564 |
| |
|
565 |
| |
|
566 |
| |
|
567 |
| |
|
568 |
| |
|
569 |
| |
|
570 |
| |
|
571 |
| |
|
572 |
| |
|
573 |
| |
|
574 |
| |
|
575 |
| |
|
576 |
| |
|
577 |
| |