Source Code Cross Referenced for DOMUtil.java in  » XML » xerces-2_9_1 » org » apache » xerces » util » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » XML » xerces 2_9_1 » org.apache.xerces.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Licensed to the Apache Software Foundation (ASF) under one or more
003:         * contributor license agreements.  See the NOTICE file distributed with
004:         * this work for additional information regarding copyright ownership.
005:         * The ASF licenses this file to You under the Apache License, Version 2.0
006:         * (the "License"); you may not use this file except in compliance with
007:         * the License.  You may obtain a copy of the License at
008:         * 
009:         *      http://www.apache.org/licenses/LICENSE-2.0
010:         * 
011:         * Unless required by applicable law or agreed to in writing, software
012:         * distributed under the License is distributed on an "AS IS" BASIS,
013:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014:         * See the License for the specific language governing permissions and
015:         * limitations under the License.
016:         */
017:
018:        package org.apache.xerces.util;
019:
020:        import java.util.Hashtable;
021:
022:        import org.apache.xerces.dom.AttrImpl;
023:        import org.apache.xerces.dom.DocumentImpl;
024:        import org.apache.xerces.impl.xs.opti.ElementImpl;
025:        import org.w3c.dom.Attr;
026:        import org.w3c.dom.DOMException;
027:        import org.w3c.dom.Document;
028:        import org.w3c.dom.Element;
029:        import org.w3c.dom.NamedNodeMap;
030:        import org.w3c.dom.Node;
031:        import org.w3c.dom.ls.LSException;
032:
033:        /**
034:         * Some useful utility methods.
035:         * This class was modified in Xerces2 with a view to abstracting as
036:         * much as possible away from the representation of the underlying
037:         * parsed structure (i.e., the DOM).  This was done so that, if Xerces
038:         * ever adopts an in-memory representation more efficient than the DOM
039:         * (such as a DTM), we should easily be able to convert our schema
040:         * parsing to utilize it.
041:         *
042:         * @version $Id: DOMUtil.java 447948 2006-09-19 20:20:53Z mrglavas $
043:         */
044:        public class DOMUtil {
045:
046:            //
047:            // Constructors
048:            //
049:
050:            /** This class cannot be instantiated. */
051:            protected DOMUtil() {
052:            }
053:
054:            //
055:            // Public static methods
056:            //
057:
058:            /**
059:             * Copies the source tree into the specified place in a destination
060:             * tree. The source node and its children are appended as children
061:             * of the destination node.
062:             * <p>
063:             * <em>Note:</em> This is an iterative implementation.
064:             */
065:            public static void copyInto(Node src, Node dest)
066:                    throws DOMException {
067:
068:                // get node factory
069:                Document factory = dest.getOwnerDocument();
070:                boolean domimpl = factory instanceof  DocumentImpl;
071:
072:                // placement variables
073:                Node start = src;
074:                Node parent = src;
075:                Node place = src;
076:
077:                // traverse source tree
078:                while (place != null) {
079:
080:                    // copy this node
081:                    Node node = null;
082:                    int type = place.getNodeType();
083:                    switch (type) {
084:                    case Node.CDATA_SECTION_NODE: {
085:                        node = factory.createCDATASection(place.getNodeValue());
086:                        break;
087:                    }
088:                    case Node.COMMENT_NODE: {
089:                        node = factory.createComment(place.getNodeValue());
090:                        break;
091:                    }
092:                    case Node.ELEMENT_NODE: {
093:                        Element element = factory.createElement(place
094:                                .getNodeName());
095:                        node = element;
096:                        NamedNodeMap attrs = place.getAttributes();
097:                        int attrCount = attrs.getLength();
098:                        for (int i = 0; i < attrCount; i++) {
099:                            Attr attr = (Attr) attrs.item(i);
100:                            String attrName = attr.getNodeName();
101:                            String attrValue = attr.getNodeValue();
102:                            element.setAttribute(attrName, attrValue);
103:                            if (domimpl && !attr.getSpecified()) {
104:                                ((AttrImpl) element.getAttributeNode(attrName))
105:                                        .setSpecified(false);
106:                            }
107:                        }
108:                        break;
109:                    }
110:                    case Node.ENTITY_REFERENCE_NODE: {
111:                        node = factory.createEntityReference(place
112:                                .getNodeName());
113:                        break;
114:                    }
115:                    case Node.PROCESSING_INSTRUCTION_NODE: {
116:                        node = factory.createProcessingInstruction(place
117:                                .getNodeName(), place.getNodeValue());
118:                        break;
119:                    }
120:                    case Node.TEXT_NODE: {
121:                        node = factory.createTextNode(place.getNodeValue());
122:                        break;
123:                    }
124:                    default: {
125:                        throw new IllegalArgumentException(
126:                                "can't copy node type, " + type + " ("
127:                                        + node.getNodeName() + ')');
128:                    }
129:                    }
130:                    dest.appendChild(node);
131:
132:                    // iterate over children
133:                    if (place.hasChildNodes()) {
134:                        parent = place;
135:                        place = place.getFirstChild();
136:                        dest = node;
137:                    }
138:
139:                    // advance
140:                    else {
141:                        place = place.getNextSibling();
142:                        while (place == null && parent != start) {
143:                            place = parent.getNextSibling();
144:                            parent = parent.getParentNode();
145:                            dest = dest.getParentNode();
146:                        }
147:                    }
148:
149:                }
150:
151:            } // copyInto(Node,Node)
152:
153:            /** Finds and returns the first child element node. */
154:            public static Element getFirstChildElement(Node parent) {
155:
156:                // search for node
157:                Node child = parent.getFirstChild();
158:                while (child != null) {
159:                    if (child.getNodeType() == Node.ELEMENT_NODE) {
160:                        return (Element) child;
161:                    }
162:                    child = child.getNextSibling();
163:                }
164:
165:                // not found
166:                return null;
167:
168:            } // getFirstChildElement(Node):Element
169:
170:            /** Finds and returns the first visible child element node. */
171:            public static Element getFirstVisibleChildElement(Node parent) {
172:
173:                // search for node
174:                Node child = parent.getFirstChild();
175:                while (child != null) {
176:                    if (child.getNodeType() == Node.ELEMENT_NODE
177:                            && !isHidden(child)) {
178:                        return (Element) child;
179:                    }
180:                    child = child.getNextSibling();
181:                }
182:
183:                // not found
184:                return null;
185:
186:            } // getFirstChildElement(Node):Element
187:
188:            /** Finds and returns the first visible child element node. */
189:            public static Element getFirstVisibleChildElement(Node parent,
190:                    Hashtable hiddenNodes) {
191:
192:                // search for node
193:                Node child = parent.getFirstChild();
194:                while (child != null) {
195:                    if (child.getNodeType() == Node.ELEMENT_NODE
196:                            && !isHidden(child, hiddenNodes)) {
197:                        return (Element) child;
198:                    }
199:                    child = child.getNextSibling();
200:                }
201:
202:                // not found
203:                return null;
204:
205:            } // getFirstChildElement(Node):Element
206:
207:            /** Finds and returns the last child element node. 
208:             *  Overload previous method for non-Xerces node impl.
209:             */
210:            public static Element getLastChildElement(Node parent) {
211:
212:                // search for node
213:                Node child = parent.getLastChild();
214:                while (child != null) {
215:                    if (child.getNodeType() == Node.ELEMENT_NODE) {
216:                        return (Element) child;
217:                    }
218:                    child = child.getPreviousSibling();
219:                }
220:
221:                // not found
222:                return null;
223:
224:            } // getLastChildElement(Node):Element
225:
226:            /** Finds and returns the last visible child element node. */
227:            public static Element getLastVisibleChildElement(Node parent) {
228:
229:                // search for node
230:                Node child = parent.getLastChild();
231:                while (child != null) {
232:                    if (child.getNodeType() == Node.ELEMENT_NODE
233:                            && !isHidden(child)) {
234:                        return (Element) child;
235:                    }
236:                    child = child.getPreviousSibling();
237:                }
238:
239:                // not found
240:                return null;
241:
242:            } // getLastChildElement(Node):Element
243:
244:            /** Finds and returns the last visible child element node. 
245:             *  Overload previous method for non-Xerces node impl
246:             */
247:            public static Element getLastVisibleChildElement(Node parent,
248:                    Hashtable hiddenNodes) {
249:
250:                // search for node
251:                Node child = parent.getLastChild();
252:                while (child != null) {
253:                    if (child.getNodeType() == Node.ELEMENT_NODE
254:                            && !isHidden(child, hiddenNodes)) {
255:                        return (Element) child;
256:                    }
257:                    child = child.getPreviousSibling();
258:                }
259:
260:                // not found
261:                return null;
262:
263:            } // getLastChildElement(Node):Element
264:
265:            /** Finds and returns the next sibling element node. */
266:            public static Element getNextSiblingElement(Node node) {
267:
268:                // search for node
269:                Node sibling = node.getNextSibling();
270:                while (sibling != null) {
271:                    if (sibling.getNodeType() == Node.ELEMENT_NODE) {
272:                        return (Element) sibling;
273:                    }
274:                    sibling = sibling.getNextSibling();
275:                }
276:
277:                // not found
278:                return null;
279:
280:            } // getNextSiblingElement(Node):Element
281:
282:            // get next visible (un-hidden) node.
283:            public static Element getNextVisibleSiblingElement(Node node) {
284:
285:                // search for node
286:                Node sibling = node.getNextSibling();
287:                while (sibling != null) {
288:                    if (sibling.getNodeType() == Node.ELEMENT_NODE
289:                            && !isHidden(sibling)) {
290:                        return (Element) sibling;
291:                    }
292:                    sibling = sibling.getNextSibling();
293:                }
294:
295:                // not found
296:                return null;
297:
298:            } // getNextSiblingdElement(Node):Element
299:
300:            // get next visible (un-hidden) node, overload previous method for non Xerces node impl
301:            public static Element getNextVisibleSiblingElement(Node node,
302:                    Hashtable hiddenNodes) {
303:
304:                // search for node
305:                Node sibling = node.getNextSibling();
306:                while (sibling != null) {
307:                    if (sibling.getNodeType() == Node.ELEMENT_NODE
308:                            && !isHidden(sibling, hiddenNodes)) {
309:                        return (Element) sibling;
310:                    }
311:                    sibling = sibling.getNextSibling();
312:                }
313:
314:                // not found
315:                return null;
316:
317:            } // getNextSiblingdElement(Node):Element
318:
319:            // set this Node as being hidden
320:            public static void setHidden(Node node) {
321:                if (node instanceof  org.apache.xerces.impl.xs.opti.NodeImpl)
322:                    ((org.apache.xerces.impl.xs.opti.NodeImpl) node)
323:                            .setReadOnly(true, false);
324:                else if (node instanceof  org.apache.xerces.dom.NodeImpl)
325:                    ((org.apache.xerces.dom.NodeImpl) node).setReadOnly(true,
326:                            false);
327:            } // setHidden(node):void
328:
329:            // set this Node as being hidden, overloaded method
330:            public static void setHidden(Node node, Hashtable hiddenNodes) {
331:                if (node instanceof  org.apache.xerces.impl.xs.opti.NodeImpl) {
332:                    ((org.apache.xerces.impl.xs.opti.NodeImpl) node)
333:                            .setReadOnly(true, false);
334:                } else {
335:                    hiddenNodes.put(node, "");
336:                }
337:            } // setHidden(node):void
338:
339:            // set this Node as being visible
340:            public static void setVisible(Node node) {
341:                if (node instanceof  org.apache.xerces.impl.xs.opti.NodeImpl)
342:                    ((org.apache.xerces.impl.xs.opti.NodeImpl) node)
343:                            .setReadOnly(false, false);
344:                else if (node instanceof  org.apache.xerces.dom.NodeImpl)
345:                    ((org.apache.xerces.dom.NodeImpl) node).setReadOnly(false,
346:                            false);
347:            } // setVisible(node):void   
348:
349:            // set this Node as being visible, overloaded method
350:            public static void setVisible(Node node, Hashtable hiddenNodes) {
351:                if (node instanceof  org.apache.xerces.impl.xs.opti.NodeImpl) {
352:                    ((org.apache.xerces.impl.xs.opti.NodeImpl) node)
353:                            .setReadOnly(false, false);
354:                } else {
355:                    hiddenNodes.remove(node);
356:                }
357:            } // setVisible(node):void
358:
359:            // is this node hidden?
360:            public static boolean isHidden(Node node) {
361:                if (node instanceof  org.apache.xerces.impl.xs.opti.NodeImpl)
362:                    return ((org.apache.xerces.impl.xs.opti.NodeImpl) node)
363:                            .getReadOnly();
364:                else if (node instanceof  org.apache.xerces.dom.NodeImpl)
365:                    return ((org.apache.xerces.dom.NodeImpl) node)
366:                            .getReadOnly();
367:                return false;
368:            } // isHidden(Node):boolean
369:
370:            // is this node hidden? overloaded method
371:            public static boolean isHidden(Node node, Hashtable hiddenNodes) {
372:                if (node instanceof  org.apache.xerces.impl.xs.opti.NodeImpl) {
373:                    return ((org.apache.xerces.impl.xs.opti.NodeImpl) node)
374:                            .getReadOnly();
375:                } else {
376:                    return hiddenNodes.containsKey(node);
377:                }
378:            } // isHidden(Node):boolean
379:
380:            /** Finds and returns the first child node with the given name. */
381:            public static Element getFirstChildElement(Node parent,
382:                    String elemName) {
383:
384:                // search for node
385:                Node child = parent.getFirstChild();
386:                while (child != null) {
387:                    if (child.getNodeType() == Node.ELEMENT_NODE) {
388:                        if (child.getNodeName().equals(elemName)) {
389:                            return (Element) child;
390:                        }
391:                    }
392:                    child = child.getNextSibling();
393:                }
394:
395:                // not found
396:                return null;
397:
398:            } // getFirstChildElement(Node,String):Element
399:
400:            /** Finds and returns the last child node with the given name. */
401:            public static Element getLastChildElement(Node parent,
402:                    String elemName) {
403:
404:                // search for node
405:                Node child = parent.getLastChild();
406:                while (child != null) {
407:                    if (child.getNodeType() == Node.ELEMENT_NODE) {
408:                        if (child.getNodeName().equals(elemName)) {
409:                            return (Element) child;
410:                        }
411:                    }
412:                    child = child.getPreviousSibling();
413:                }
414:
415:                // not found
416:                return null;
417:
418:            } // getLastChildElement(Node,String):Element
419:
420:            /** Finds and returns the next sibling node with the given name. */
421:            public static Element getNextSiblingElement(Node node,
422:                    String elemName) {
423:
424:                // search for node
425:                Node sibling = node.getNextSibling();
426:                while (sibling != null) {
427:                    if (sibling.getNodeType() == Node.ELEMENT_NODE) {
428:                        if (sibling.getNodeName().equals(elemName)) {
429:                            return (Element) sibling;
430:                        }
431:                    }
432:                    sibling = sibling.getNextSibling();
433:                }
434:
435:                // not found
436:                return null;
437:
438:            } // getNextSiblingdElement(Node,String):Element
439:
440:            /** Finds and returns the first child node with the given qualified name. */
441:            public static Element getFirstChildElementNS(Node parent,
442:                    String uri, String localpart) {
443:
444:                // search for node
445:                Node child = parent.getFirstChild();
446:                while (child != null) {
447:                    if (child.getNodeType() == Node.ELEMENT_NODE) {
448:                        String childURI = child.getNamespaceURI();
449:                        if (childURI != null && childURI.equals(uri)
450:                                && child.getLocalName().equals(localpart)) {
451:                            return (Element) child;
452:                        }
453:                    }
454:                    child = child.getNextSibling();
455:                }
456:
457:                // not found
458:                return null;
459:
460:            } // getFirstChildElementNS(Node,String,String):Element
461:
462:            /** Finds and returns the last child node with the given qualified name. */
463:            public static Element getLastChildElementNS(Node parent,
464:                    String uri, String localpart) {
465:
466:                // search for node
467:                Node child = parent.getLastChild();
468:                while (child != null) {
469:                    if (child.getNodeType() == Node.ELEMENT_NODE) {
470:                        String childURI = child.getNamespaceURI();
471:                        if (childURI != null && childURI.equals(uri)
472:                                && child.getLocalName().equals(localpart)) {
473:                            return (Element) child;
474:                        }
475:                    }
476:                    child = child.getPreviousSibling();
477:                }
478:
479:                // not found
480:                return null;
481:
482:            } // getLastChildElementNS(Node,String,String):Element
483:
484:            /** Finds and returns the next sibling node with the given qualified name. */
485:            public static Element getNextSiblingElementNS(Node node,
486:                    String uri, String localpart) {
487:
488:                // search for node
489:                Node sibling = node.getNextSibling();
490:                while (sibling != null) {
491:                    if (sibling.getNodeType() == Node.ELEMENT_NODE) {
492:                        String siblingURI = sibling.getNamespaceURI();
493:                        if (siblingURI != null && siblingURI.equals(uri)
494:                                && sibling.getLocalName().equals(localpart)) {
495:                            return (Element) sibling;
496:                        }
497:                    }
498:                    sibling = sibling.getNextSibling();
499:                }
500:
501:                // not found
502:                return null;
503:
504:            } // getNextSiblingdElementNS(Node,String,String):Element
505:
506:            /** Finds and returns the first child node with the given name. */
507:            public static Element getFirstChildElement(Node parent,
508:                    String elemNames[]) {
509:
510:                // search for node
511:                Node child = parent.getFirstChild();
512:                while (child != null) {
513:                    if (child.getNodeType() == Node.ELEMENT_NODE) {
514:                        for (int i = 0; i < elemNames.length; i++) {
515:                            if (child.getNodeName().equals(elemNames[i])) {
516:                                return (Element) child;
517:                            }
518:                        }
519:                    }
520:                    child = child.getNextSibling();
521:                }
522:
523:                // not found
524:                return null;
525:
526:            } // getFirstChildElement(Node,String[]):Element
527:
528:            /** Finds and returns the last child node with the given name. */
529:            public static Element getLastChildElement(Node parent,
530:                    String elemNames[]) {
531:
532:                // search for node
533:                Node child = parent.getLastChild();
534:                while (child != null) {
535:                    if (child.getNodeType() == Node.ELEMENT_NODE) {
536:                        for (int i = 0; i < elemNames.length; i++) {
537:                            if (child.getNodeName().equals(elemNames[i])) {
538:                                return (Element) child;
539:                            }
540:                        }
541:                    }
542:                    child = child.getPreviousSibling();
543:                }
544:
545:                // not found
546:                return null;
547:
548:            } // getLastChildElement(Node,String[]):Element
549:
550:            /** Finds and returns the next sibling node with the given name. */
551:            public static Element getNextSiblingElement(Node node,
552:                    String elemNames[]) {
553:
554:                // search for node
555:                Node sibling = node.getNextSibling();
556:                while (sibling != null) {
557:                    if (sibling.getNodeType() == Node.ELEMENT_NODE) {
558:                        for (int i = 0; i < elemNames.length; i++) {
559:                            if (sibling.getNodeName().equals(elemNames[i])) {
560:                                return (Element) sibling;
561:                            }
562:                        }
563:                    }
564:                    sibling = sibling.getNextSibling();
565:                }
566:
567:                // not found
568:                return null;
569:
570:            } // getNextSiblingdElement(Node,String[]):Element
571:
572:            /** Finds and returns the first child node with the given qualified name. */
573:            public static Element getFirstChildElementNS(Node parent,
574:                    String[][] elemNames) {
575:
576:                // search for node
577:                Node child = parent.getFirstChild();
578:                while (child != null) {
579:                    if (child.getNodeType() == Node.ELEMENT_NODE) {
580:                        for (int i = 0; i < elemNames.length; i++) {
581:                            String uri = child.getNamespaceURI();
582:                            if (uri != null
583:                                    && uri.equals(elemNames[i][0])
584:                                    && child.getLocalName().equals(
585:                                            elemNames[i][1])) {
586:                                return (Element) child;
587:                            }
588:                        }
589:                    }
590:                    child = child.getNextSibling();
591:                }
592:
593:                // not found
594:                return null;
595:
596:            } // getFirstChildElementNS(Node,String[][]):Element
597:
598:            /** Finds and returns the last child node with the given qualified name. */
599:            public static Element getLastChildElementNS(Node parent,
600:                    String[][] elemNames) {
601:
602:                // search for node
603:                Node child = parent.getLastChild();
604:                while (child != null) {
605:                    if (child.getNodeType() == Node.ELEMENT_NODE) {
606:                        for (int i = 0; i < elemNames.length; i++) {
607:                            String uri = child.getNamespaceURI();
608:                            if (uri != null
609:                                    && uri.equals(elemNames[i][0])
610:                                    && child.getLocalName().equals(
611:                                            elemNames[i][1])) {
612:                                return (Element) child;
613:                            }
614:                        }
615:                    }
616:                    child = child.getPreviousSibling();
617:                }
618:
619:                // not found
620:                return null;
621:
622:            } // getLastChildElementNS(Node,String[][]):Element
623:
624:            /** Finds and returns the next sibling node with the given qualified name. */
625:            public static Element getNextSiblingElementNS(Node node,
626:                    String[][] elemNames) {
627:
628:                // search for node
629:                Node sibling = node.getNextSibling();
630:                while (sibling != null) {
631:                    if (sibling.getNodeType() == Node.ELEMENT_NODE) {
632:                        for (int i = 0; i < elemNames.length; i++) {
633:                            String uri = sibling.getNamespaceURI();
634:                            if (uri != null
635:                                    && uri.equals(elemNames[i][0])
636:                                    && sibling.getLocalName().equals(
637:                                            elemNames[i][1])) {
638:                                return (Element) sibling;
639:                            }
640:                        }
641:                    }
642:                    sibling = sibling.getNextSibling();
643:                }
644:
645:                // not found
646:                return null;
647:
648:            } // getNextSiblingdElementNS(Node,String[][]):Element
649:
650:            /**
651:             * Finds and returns the first child node with the given name and
652:             * attribute name, value pair.
653:             */
654:            public static Element getFirstChildElement(Node parent,
655:                    String elemName, String attrName, String attrValue) {
656:
657:                // search for node
658:                Node child = parent.getFirstChild();
659:                while (child != null) {
660:                    if (child.getNodeType() == Node.ELEMENT_NODE) {
661:                        Element element = (Element) child;
662:                        if (element.getNodeName().equals(elemName)
663:                                && element.getAttribute(attrName).equals(
664:                                        attrValue)) {
665:                            return element;
666:                        }
667:                    }
668:                    child = child.getNextSibling();
669:                }
670:
671:                // not found
672:                return null;
673:
674:            } // getFirstChildElement(Node,String,String,String):Element
675:
676:            /**
677:             * Finds and returns the last child node with the given name and
678:             * attribute name, value pair.
679:             */
680:            public static Element getLastChildElement(Node parent,
681:                    String elemName, String attrName, String attrValue) {
682:
683:                // search for node
684:                Node child = parent.getLastChild();
685:                while (child != null) {
686:                    if (child.getNodeType() == Node.ELEMENT_NODE) {
687:                        Element element = (Element) child;
688:                        if (element.getNodeName().equals(elemName)
689:                                && element.getAttribute(attrName).equals(
690:                                        attrValue)) {
691:                            return element;
692:                        }
693:                    }
694:                    child = child.getPreviousSibling();
695:                }
696:
697:                // not found
698:                return null;
699:
700:            } // getLastChildElement(Node,String,String,String):Element
701:
702:            /**
703:             * Finds and returns the next sibling node with the given name and
704:             * attribute name, value pair. Since only elements have attributes,
705:             * the node returned will be of type Node.ELEMENT_NODE.
706:             */
707:            public static Element getNextSiblingElement(Node node,
708:                    String elemName, String attrName, String attrValue) {
709:
710:                // search for node
711:                Node sibling = node.getNextSibling();
712:                while (sibling != null) {
713:                    if (sibling.getNodeType() == Node.ELEMENT_NODE) {
714:                        Element element = (Element) sibling;
715:                        if (element.getNodeName().equals(elemName)
716:                                && element.getAttribute(attrName).equals(
717:                                        attrValue)) {
718:                            return element;
719:                        }
720:                    }
721:                    sibling = sibling.getNextSibling();
722:                }
723:
724:                // not found
725:                return null;
726:
727:            } // getNextSiblingElement(Node,String,String,String):Element
728:
729:            /**
730:             * Returns the concatenated child text of the specified node.
731:             * This method only looks at the immediate children of type
732:             * <code>Node.TEXT_NODE</code> or the children of any child
733:             * node that is of type <code>Node.CDATA_SECTION_NODE</code>
734:             * for the concatenation.
735:             *
736:             * @param node The node to look at.
737:             */
738:            public static String getChildText(Node node) {
739:
740:                // is there anything to do?
741:                if (node == null) {
742:                    return null;
743:                }
744:
745:                // concatenate children text
746:                StringBuffer str = new StringBuffer();
747:                Node child = node.getFirstChild();
748:                while (child != null) {
749:                    short type = child.getNodeType();
750:                    if (type == Node.TEXT_NODE) {
751:                        str.append(child.getNodeValue());
752:                    } else if (type == Node.CDATA_SECTION_NODE) {
753:                        str.append(getChildText(child));
754:                    }
755:                    child = child.getNextSibling();
756:                }
757:
758:                // return text value
759:                return str.toString();
760:
761:            } // getChildText(Node):String
762:
763:            // return the name of this element
764:            public static String getName(Node node) {
765:                return node.getNodeName();
766:            } // getLocalName(Element):  String
767:
768:            /** returns local name of this element if not null, otherwise
769:             returns the name of the node
770:             */
771:            public static String getLocalName(Node node) {
772:                String name = node.getLocalName();
773:                return (name != null) ? name : node.getNodeName();
774:            } // getLocalName(Element):  String
775:
776:            public static Element getParent(Element elem) {
777:                Node parent = elem.getParentNode();
778:                if (parent instanceof  Element)
779:                    return (Element) parent;
780:                return null;
781:            } // getParent(Element):Element
782:
783:            // get the Document of which this Node is a part
784:            public static Document getDocument(Node node) {
785:                return node.getOwnerDocument();
786:            } // getDocument(Node):Document
787:
788:            // return this Document's root node
789:            public static Element getRoot(Document doc) {
790:                return doc.getDocumentElement();
791:            } // getRoot(Document(:  Element
792:
793:            // some methods for handling attributes:
794:
795:            // return the right attribute node
796:            public static Attr getAttr(Element elem, String name) {
797:                return elem.getAttributeNode(name);
798:            } // getAttr(Element, String):Attr
799:
800:            // return the right attribute node
801:            public static Attr getAttrNS(Element elem, String nsUri,
802:                    String localName) {
803:                return elem.getAttributeNodeNS(nsUri, localName);
804:            } // getAttrNS(Element, String):Attr
805:
806:            // get all the attributes for an Element
807:            public static Attr[] getAttrs(Element elem) {
808:                NamedNodeMap attrMap = elem.getAttributes();
809:                Attr[] attrArray = new Attr[attrMap.getLength()];
810:                for (int i = 0; i < attrMap.getLength(); i++)
811:                    attrArray[i] = (Attr) attrMap.item(i);
812:                return attrArray;
813:            } // getAttrs(Element):  Attr[]
814:
815:            // get attribute's value
816:            public static String getValue(Attr attribute) {
817:                return attribute.getValue();
818:            } // getValue(Attr):String
819:
820:            // It is noteworthy that, because of the way the DOM specs
821:            // work, the next two methods return the empty string (not
822:            // null!) when the attribute with the specified name does not
823:            // exist on an element.  Beware!
824:
825:            // return the value of the attribute of the given element
826:            // with the given name
827:            public static String getAttrValue(Element elem, String name) {
828:                return elem.getAttribute(name);
829:            } // getAttr(Element, String):Attr
830:
831:            // return the value of the attribute of the given element
832:            // with the given name
833:            public static String getAttrValueNS(Element elem, String nsUri,
834:                    String localName) {
835:                return elem.getAttributeNS(nsUri, localName);
836:            } // getAttrValueNS(Element, String):Attr
837:
838:            // return the prefix
839:            public static String getPrefix(Node node) {
840:                return node.getPrefix();
841:            }
842:
843:            // return the namespace URI
844:            public static String getNamespaceURI(Node node) {
845:                return node.getNamespaceURI();
846:            }
847:
848:            // return annotation
849:            public static String getAnnotation(Node node) {
850:                if (node instanceof  ElementImpl) {
851:                    return ((ElementImpl) node).getAnnotation();
852:                }
853:                return null;
854:            }
855:
856:            // return synthetic annotation
857:            public static String getSyntheticAnnotation(Node node) {
858:                if (node instanceof  ElementImpl) {
859:                    return ((ElementImpl) node).getSyntheticAnnotation();
860:                }
861:                return null;
862:            }
863:
864:            /**
865:             * Creates a DOMException. On J2SE 1.4 and above the cause for the exception will be set.
866:             */
867:            public static DOMException createDOMException(short code,
868:                    Throwable cause) {
869:                DOMException de = new DOMException(code, cause != null ? cause
870:                        .getMessage() : null);
871:                if (cause != null
872:                        && ThrowableMethods.fgThrowableMethodsAvailable) {
873:                    try {
874:                        ThrowableMethods.fgThrowableInitCauseMethod.invoke(de,
875:                                new Object[] { cause });
876:                    }
877:                    // Something went wrong. There's not much we can do about it.
878:                    catch (Exception e) {
879:                    }
880:                }
881:                return de;
882:            }
883:
884:            /**
885:             * Creates an LSException. On J2SE 1.4 and above the cause for the exception will be set.
886:             */
887:            public static LSException createLSException(short code,
888:                    Throwable cause) {
889:                LSException lse = new LSException(code, cause != null ? cause
890:                        .getMessage() : null);
891:                if (cause != null
892:                        && ThrowableMethods.fgThrowableMethodsAvailable) {
893:                    try {
894:                        ThrowableMethods.fgThrowableInitCauseMethod.invoke(lse,
895:                                new Object[] { cause });
896:                    }
897:                    // Something went wrong. There's not much we can do about it.
898:                    catch (Exception e) {
899:                    }
900:                }
901:                return lse;
902:            }
903:
904:            /**
905:             * Holder of methods from java.lang.Throwable.
906:             */
907:            static class ThrowableMethods {
908:
909:                // Method: java.lang.Throwable.initCause(java.lang.Throwable)
910:                private static java.lang.reflect.Method fgThrowableInitCauseMethod = null;
911:
912:                // Flag indicating whether or not Throwable methods available.
913:                private static boolean fgThrowableMethodsAvailable = false;
914:
915:                private ThrowableMethods() {
916:                }
917:
918:                // Attempt to get methods for java.lang.Throwable on class initialization.
919:                static {
920:                    try {
921:                        fgThrowableInitCauseMethod = Throwable.class.getMethod(
922:                                "initCause", new Class[] { Throwable.class });
923:                        fgThrowableMethodsAvailable = true;
924:                    }
925:                    // ClassNotFoundException, NoSuchMethodException or SecurityException
926:                    // Whatever the case, we cannot use java.lang.Throwable.initCause(java.lang.Throwable).
927:                    catch (Exception exc) {
928:                        fgThrowableInitCauseMethod = null;
929:                        fgThrowableMethodsAvailable = false;
930:                    }
931:                }
932:            }
933:
934:        } // class DOMUtil
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.