Source Code Cross Referenced for XMLElement.java in  » Parser » NanoXML » net » n3 » nanoxml » 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 » Parser » NanoXML » net.n3.nanoxml 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /* XMLElement.java                                                 NanoXML/Java
002:         *
003:         * $Revision: 1.5 $
004:         * $Date: 2002/02/06 18:50:12 $
005:         * $Name: RELEASE_2_2_1 $
006:         *
007:         * This file is part of NanoXML 2 for Java.
008:         * Copyright (C) 2000-2002 Marc De Scheemaecker, All Rights Reserved.
009:         *
010:         * This software is provided 'as-is', without any express or implied warranty.
011:         * In no event will the authors be held liable for any damages arising from the
012:         * use of this software.
013:         *
014:         * Permission is granted to anyone to use this software for any purpose,
015:         * including commercial applications, and to alter it and redistribute it
016:         * freely, subject to the following restrictions:
017:         *
018:         *  1. The origin of this software must not be misrepresented; you must not
019:         *     claim that you wrote the original software. If you use this software in
020:         *     a product, an acknowledgment in the product documentation would be
021:         *     appreciated but is not required.
022:         *
023:         *  2. Altered source versions must be plainly marked as such, and must not be
024:         *     misrepresented as being the original software.
025:         *
026:         *  3. This notice may not be removed or altered from any source distribution.
027:         */
028:
029:        package net.n3.nanoxml;
030:
031:        import java.io.Serializable;
032:        import java.util.Enumeration;
033:        import java.util.Hashtable;
034:        import java.util.Properties;
035:        import java.util.Vector;
036:
037:        /**
038:         * XMLElement is an XML element. The standard NanoXML builder generates a
039:         * tree of such elements.
040:         *
041:         * @see net.n3.nanoxml.StdXMLBuilder
042:         *
043:         * @author Marc De Scheemaecker
044:         * @version $Name: RELEASE_2_2_1 $, $Revision: 1.5 $
045:         */
046:        public class XMLElement implements  IXMLElement, Serializable {
047:
048:            /**
049:             * Necessary for serialization.
050:             */
051:            static final long serialVersionUID = -2383376380548624920L;
052:
053:            /**
054:             * No line number defined.
055:             */
056:            public static final int NO_LINE = -1;
057:
058:            /**
059:             * The parent element.
060:             */
061:            private IXMLElement parent;
062:
063:            /**
064:             * The attributes of the element.
065:             */
066:            private Vector attributes;
067:
068:            /**
069:             * The child elements.
070:             */
071:            private Vector children;
072:
073:            /**
074:             * The name of the element.
075:             */
076:            private String name;
077:
078:            /**
079:             * The full name of the element.
080:             */
081:            private String fullName;
082:
083:            /**
084:             * The namespace URI.
085:             */
086:            private String namespace;
087:
088:            /**
089:             * The content of the element.
090:             */
091:            private String content;
092:
093:            /**
094:             * The system ID of the source data where this element is located.
095:             */
096:            private String systemID;
097:
098:            /**
099:             * The line in the source data where this element starts.
100:             */
101:            private int lineNr;
102:
103:            /**
104:             * Creates an empty element to be used for #PCDATA content.
105:             */
106:            public XMLElement() {
107:                this (null, null, null, NO_LINE);
108:            }
109:
110:            /**
111:             * Creates an empty element.
112:             *
113:             * @param fullName the name of the element.
114:             */
115:            public XMLElement(String fullName) {
116:                this (fullName, null, null, NO_LINE);
117:            }
118:
119:            /**
120:             * Creates an empty element.
121:             *
122:             * @param fullName the name of the element.
123:             * @param systemID the system ID of the XML data where the element starts.
124:             * @param lineNr   the line in the XML data where the element starts.
125:             */
126:            public XMLElement(String fullName, String systemID, int lineNr) {
127:                this (fullName, null, systemID, lineNr);
128:            }
129:
130:            /**
131:             * Creates an empty element.
132:             *
133:             * @param fullName  the full name of the element
134:             * @param namespace the namespace URI.
135:             */
136:            public XMLElement(String fullName, String namespace) {
137:                this (fullName, namespace, null, NO_LINE);
138:            }
139:
140:            /**
141:             * Creates an empty element.
142:             *
143:             * @param fullName  the full name of the element
144:             * @param namespace the namespace URI.
145:             * @param systemID  the system ID of the XML data where the element starts.
146:             * @param lineNr    the line in the XML data where the element starts.
147:             */
148:            public XMLElement(String fullName, String namespace,
149:                    String systemID, int lineNr) {
150:                this .attributes = new Vector();
151:                this .children = new Vector(8);
152:                this .fullName = fullName;
153:                if (namespace == null) {
154:                    this .name = fullName;
155:                } else {
156:                    int index = fullName.indexOf(':');
157:                    if (index >= 0) {
158:                        this .name = fullName.substring(index + 1);
159:                    } else {
160:                        this .name = fullName;
161:                    }
162:                }
163:                this .namespace = namespace;
164:                this .content = null;
165:                this .lineNr = lineNr;
166:                this .systemID = systemID;
167:                this .parent = null;
168:            }
169:
170:            /**
171:             * Creates an element to be used for #PCDATA content.
172:             */
173:            public IXMLElement createPCDataElement() {
174:                return new XMLElement();
175:            }
176:
177:            /**
178:             * Creates an empty element.
179:             *
180:             * @param fullName the name of the element.
181:             */
182:            public IXMLElement createElement(String fullName) {
183:                return new XMLElement(fullName);
184:            }
185:
186:            /**
187:             * Creates an empty element.
188:             *
189:             * @param fullName the name of the element.
190:             * @param systemID the system ID of the XML data where the element starts.
191:             * @param lineNr   the line in the XML data where the element starts.
192:             */
193:            public IXMLElement createElement(String fullName, String systemID,
194:                    int lineNr) {
195:                return new XMLElement(fullName, systemID, lineNr);
196:            }
197:
198:            /**
199:             * Creates an empty element.
200:             *
201:             * @param fullName  the full name of the element
202:             * @param namespace the namespace URI.
203:             */
204:            public IXMLElement createElement(String fullName, String namespace) {
205:                return new XMLElement(fullName, namespace);
206:            }
207:
208:            /**
209:             * Creates an empty element.
210:             *
211:             * @param fullName  the full name of the element
212:             * @param namespace the namespace URI.
213:             * @param systemID  the system ID of the XML data where the element starts.
214:             * @param lineNr    the line in the XML data where the element starts.
215:             */
216:            public IXMLElement createElement(String fullName, String namespace,
217:                    String systemID, int lineNr) {
218:                return new XMLElement(fullName, namespace, systemID, lineNr);
219:            }
220:
221:            /**
222:             * Cleans up the object when it's destroyed.
223:             */
224:            protected void finalize() throws Throwable {
225:                this .attributes.clear();
226:                this .attributes = null;
227:                this .children = null;
228:                this .fullName = null;
229:                this .name = null;
230:                this .namespace = null;
231:                this .content = null;
232:                this .systemID = null;
233:                this .parent = null;
234:                super .finalize();
235:            }
236:
237:            /**
238:             * Returns the parent element. This method returns null for the root
239:             * element.
240:             */
241:            public IXMLElement getParent() {
242:                return this .parent;
243:            }
244:
245:            /**
246:             * Returns the full name (i.e. the name including an eventual namespace
247:             * prefix) of the element.
248:             *
249:             * @return the name, or null if the element only contains #PCDATA.
250:             */
251:            public String getFullName() {
252:                return this .fullName;
253:            }
254:
255:            /**
256:             * Returns the name of the element.
257:             *
258:             * @return the name, or null if the element only contains #PCDATA.
259:             */
260:            public String getName() {
261:                return this .name;
262:            }
263:
264:            /**
265:             * Returns the namespace of the element.
266:             *
267:             * @return the namespace, or null if no namespace is associated with the
268:             *         element.
269:             */
270:            public String getNamespace() {
271:                return this .namespace;
272:            }
273:
274:            /**
275:             * Sets the full name. This method also sets the short name and clears the
276:             * namespace URI.
277:             *
278:             * @param name the non-null name.
279:             */
280:            public void setName(String name) {
281:                this .name = name;
282:                this .fullName = name;
283:                this .namespace = null;
284:            }
285:
286:            /**
287:             * Sets the name.
288:             *
289:             * @param fullName  the non-null full name.
290:             * @param namespace the namespace URI, which may be null.
291:             */
292:            public void setName(String fullName, String namespace) {
293:                int index = fullName.indexOf(':');
294:                if ((namespace == null) || (index < 0)) {
295:                    this .name = fullName;
296:                } else {
297:                    this .name = fullName.substring(index + 1);
298:                }
299:                this .fullName = fullName;
300:                this .namespace = namespace;
301:            }
302:
303:            /**
304:             * Adds a child element.
305:             *
306:             * @param child the non-null child to add.
307:             */
308:            public void addChild(IXMLElement child) {
309:                if (child == null) {
310:                    throw new IllegalArgumentException("child must not be null");
311:                }
312:                if ((child.getName() == null) && (!this .children.isEmpty())) {
313:                    IXMLElement lastChild = (IXMLElement) this .children
314:                            .lastElement();
315:
316:                    if (lastChild.getName() == null) {
317:                        lastChild.setContent(lastChild.getContent()
318:                                + child.getContent());
319:                        return;
320:                    }
321:                }
322:                ((XMLElement) child).parent = this ;
323:                this .children.addElement(child);
324:            }
325:
326:            /**
327:             * Inserts a child element.
328:             *
329:             * @param child the non-null child to add.
330:             * @param index where to put the child.
331:             */
332:            public void insertChild(IXMLElement child, int index) {
333:                if (child == null) {
334:                    throw new IllegalArgumentException("child must not be null");
335:                }
336:                if ((child.getName() == null) && (!this .children.isEmpty())) {
337:                    IXMLElement lastChild = (IXMLElement) this .children
338:                            .lastElement();
339:                    if (lastChild.getName() == null) {
340:                        lastChild.setContent(lastChild.getContent()
341:                                + child.getContent());
342:                        return;
343:                    }
344:                }
345:                ((XMLElement) child).parent = this ;
346:                this .children.insertElementAt(child, index);
347:            }
348:
349:            /**
350:             * Removes a child element.
351:             *
352:             * @param child the non-null child to remove.
353:             */
354:            public void removeChild(IXMLElement child) {
355:                if (child == null) {
356:                    throw new IllegalArgumentException("child must not be null");
357:                }
358:                this .children.removeElement(child);
359:            }
360:
361:            /**
362:             * Removes the child located at a certain index.
363:             *
364:             * @param index the index of the child, where the first child has index 0.
365:             */
366:            public void removeChildAtIndex(int index) {
367:                this .children.removeElementAt(index);
368:            }
369:
370:            /**
371:             * Returns an enumeration of all child elements.
372:             *
373:             * @return the non-null enumeration
374:             */
375:            public Enumeration enumerateChildren() {
376:                return this .children.elements();
377:            }
378:
379:            /**
380:             * Returns whether the element is a leaf element.
381:             *
382:             * @return true if the element has no children.
383:             */
384:            public boolean isLeaf() {
385:                return this .children.isEmpty();
386:            }
387:
388:            /**
389:             * Returns whether the element has children.
390:             *
391:             * @return true if the element has children.
392:             */
393:            public boolean hasChildren() {
394:                return (!this .children.isEmpty());
395:            }
396:
397:            /**
398:             * Returns the number of children.
399:             *
400:             * @return the count.
401:             */
402:            public int getChildrenCount() {
403:                return this .children.size();
404:            }
405:
406:            /**
407:             * Returns a vector containing all the child elements.
408:             *
409:             * @return the vector.
410:             */
411:            public Vector getChildren() {
412:                return this .children;
413:            }
414:
415:            /**
416:             * Returns the child at a specific index.
417:             *
418:             * @param index the index of the child
419:             *
420:             * @return the non-null child
421:             *
422:             * @throws java.lang.ArrayIndexOutOfBoundsException
423:             *		if the index is out of bounds.
424:             */
425:            public IXMLElement getChildAtIndex(int index)
426:                    throws ArrayIndexOutOfBoundsException {
427:                return (IXMLElement) this .children.elementAt(index);
428:            }
429:
430:            /**
431:             * Searches a child element.
432:             *
433:             * @param name the full name of the child to search for.
434:             *
435:             * @return the child element, or null if no such child was found.
436:             */
437:            public IXMLElement getFirstChildNamed(String name) {
438:        Enumeration enum = this .children.elements();
439:        while (enum.hasMoreElements()) {
440:            IXMLElement child = (IXMLElement) enum.nextElement();
441:            String childName = child.getFullName();
442:            if ((childName != null) && childName.equals(name)) {
443:                return child;
444:            }
445:        }
446:        return null;
447:    }
448:
449:            /**
450:             * Searches a child element.
451:             *
452:             * @param name      the name of the child to search for.
453:             * @param namespace the namespace, which may be null.
454:             *
455:             * @return the child element, or null if no such child was found.
456:             */
457:            public IXMLElement getFirstChildNamed(String name,
458:                                          String namespace) {
459:        Enumeration enum = this .children.elements();
460:        while (enum.hasMoreElements()) {
461:            IXMLElement child = (IXMLElement) enum.nextElement();
462:            String str = child.getName();
463:            boolean found = (str != null) && (str.equals(name));
464:            str = child.getNamespace();
465:            if (str == null) {
466:                found &= (name == null);
467:            } else {
468:                found &= str.equals(namespace);
469:            }
470:            if (found) {
471:                return child;
472:            }
473:        }
474:        return null;
475:    }
476:
477:            /**
478:             * Returns a vector of all child elements named <I>name</I>.
479:             *
480:             * @param name the full name of the children to search for.
481:             *
482:             * @return the non-null vector of child elements.
483:             */
484:            public Vector getChildrenNamed(String name) {
485:        Vector result = new Vector(this .children.size());
486:        Enumeration enum = this .children.elements();
487:        while (enum.hasMoreElements()) {
488:            IXMLElement child = (IXMLElement) enum.nextElement();
489:            String childName = child.getFullName();
490:            if ((childName != null) && childName.equals(name)) {
491:                result.addElement(child);
492:            }
493:        }
494:        return result;
495:    }
496:
497:            /**
498:             * Returns a vector of all child elements named <I>name</I>.
499:             *
500:             * @param name      the name of the children to search for.
501:             * @param namespace the namespace, which may be null.
502:             *
503:             * @return the non-null vector of child elements.
504:             */
505:            public Vector getChildrenNamed(String name,
506:                                   String namespace) {
507:        Vector result = new Vector(this .children.size());
508:        Enumeration enum = this .children.elements();
509:        while (enum.hasMoreElements()) {
510:            IXMLElement child = (IXMLElement) enum.nextElement();
511:            String str = child.getName();
512:            boolean found = (str != null) && (str.equals(name));
513:            str = child.getNamespace();
514:            if (str == null) {
515:                found &= (name == null);
516:            } else {
517:                found &= str.equals(namespace);
518:            }
519:
520:            if (found) {
521:                result.addElement(child);
522:            }
523:        }
524:        return result;
525:    }
526:
527:            /**
528:             * Searches an attribute.
529:             *
530:             * @param fullName the non-null full name of the attribute.
531:             *
532:             * @return the attribute, or null if the attribute does not exist.
533:             */
534:            private XMLAttribute findAttribute(String fullName) {
535:        Enumeration enum = this .attributes.elements();
536:        while (enum.hasMoreElements()) {
537:            XMLAttribute attr = (XMLAttribute) enum.nextElement();
538:            if (attr.getFullName().equals(fullName)) {
539:                return attr;
540:            }
541:        }
542:        return null;
543:    }
544:
545:            /**
546:             * Searches an attribute.
547:             *
548:             * @param name the non-null short name of the attribute.
549:             * @param namespace the name space, which may be null.
550:             *
551:             * @return the attribute, or null if the attribute does not exist.
552:             */
553:            private XMLAttribute findAttribute(String name,
554:                                       String namespace) {
555:        Enumeration enum = this .attributes.elements();
556:        while (enum.hasMoreElements()) {
557:            XMLAttribute attr = (XMLAttribute) enum.nextElement();
558:            boolean found = attr.getName().equals(name);
559:            if (namespace == null) {
560:                found &= (attr.getNamespace() == null);
561:            } else {
562:                found &= namespace.equals(attr.getNamespace());
563:            }
564:
565:            if (found) {
566:                return attr;
567:            }
568:        }
569:        return null;
570:    }
571:
572:            /**
573:             * Returns the number of attributes.
574:             */
575:            public int getAttributeCount() {
576:                return this .attributes.size();
577:            }
578:
579:            /**
580:             * @deprecated As of NanoXML/Java 2.1, replaced by
581:             *             {@link #getAttribute(java.lang.String,java.lang.String)}
582:             * Returns the value of an attribute.
583:             *
584:             * @param name the non-null name of the attribute.
585:             *
586:             * @return the value, or null if the attribute does not exist.
587:             */
588:            public String getAttribute(String name) {
589:                return this .getAttribute(name, null);
590:            }
591:
592:            /**
593:             * Returns the value of an attribute.
594:             *
595:             * @param name the non-null full name of the attribute.
596:             * @param defaultValue the default value of the attribute.
597:             *
598:             * @return the value, or defaultValue if the attribute does not exist.
599:             */
600:            public String getAttribute(String name, String defaultValue) {
601:                XMLAttribute attr = this .findAttribute(name);
602:                if (attr == null) {
603:                    return defaultValue;
604:                } else {
605:                    return attr.getValue();
606:                }
607:            }
608:
609:            /**
610:             * Returns the value of an attribute.
611:             *
612:             * @param name the non-null name of the attribute.
613:             * @param namespace the namespace URI, which may be null.
614:             * @param defaultValue the default value of the attribute.
615:             *
616:             * @return the value, or defaultValue if the attribute does not exist.
617:             */
618:            public String getAttribute(String name, String namespace,
619:                    String defaultValue) {
620:                XMLAttribute attr = this .findAttribute(name, namespace);
621:                if (attr == null) {
622:                    return defaultValue;
623:                } else {
624:                    return attr.getValue();
625:                }
626:            }
627:
628:            /**
629:             * Returns the value of an attribute.
630:             *
631:             * @param name the non-null full name of the attribute.
632:             * @param defaultValue the default value of the attribute.
633:             *
634:             * @return the value, or defaultValue if the attribute does not exist.
635:             */
636:            public int getAttribute(String name, int defaultValue) {
637:                String value = this .getAttribute(name, Integer
638:                        .toString(defaultValue));
639:                return Integer.parseInt(value);
640:            }
641:
642:            /**
643:             * Returns the value of an attribute.
644:             *
645:             * @param name the non-null name of the attribute.
646:             * @param namespace the namespace URI, which may be null.
647:             * @param defaultValue the default value of the attribute.
648:             *
649:             * @return the value, or defaultValue if the attribute does not exist.
650:             */
651:            public int getAttribute(String name, String namespace,
652:                    int defaultValue) {
653:                String value = this .getAttribute(name, namespace, Integer
654:                        .toString(defaultValue));
655:                return Integer.parseInt(value);
656:            }
657:
658:            /**
659:             * Returns the type of an attribute.
660:             *
661:             * @param name the non-null full name of the attribute.
662:             *
663:             * @return the type, or null if the attribute does not exist.
664:             */
665:            public String getAttributeType(String name) {
666:                XMLAttribute attr = this .findAttribute(name);
667:                if (attr == null) {
668:                    return null;
669:                } else {
670:                    return attr.getType();
671:                }
672:            }
673:
674:            /**
675:             * Returns the namespace of an attribute.
676:             *
677:             * @param name the non-null full name of the attribute.
678:             *
679:             * @return the namespace, or null if there is none associated.
680:             */
681:            public String getAttributeNamespace(String name) {
682:                XMLAttribute attr = this .findAttribute(name);
683:                if (attr == null) {
684:                    return null;
685:                } else {
686:                    return attr.getNamespace();
687:                }
688:            }
689:
690:            /**
691:             * Returns the type of an attribute.
692:             *
693:             * @param name the non-null name of the attribute.
694:             * @param namespace the namespace URI, which may be null.
695:             *
696:             * @return the type, or null if the attribute does not exist.
697:             */
698:            public String getAttributeType(String name, String namespace) {
699:                XMLAttribute attr = this .findAttribute(name, namespace);
700:                if (attr == null) {
701:                    return null;
702:                } else {
703:                    return attr.getType();
704:                }
705:            }
706:
707:            /**
708:             * Sets an attribute.
709:             *
710:             * @param name the non-null full name of the attribute.
711:             * @param value the non-null value of the attribute.
712:             */
713:            public void setAttribute(String name, String value) {
714:                XMLAttribute attr = this .findAttribute(name);
715:                if (attr == null) {
716:                    attr = new XMLAttribute(name, name, null, value, "CDATA");
717:                    this .attributes.addElement(attr);
718:                } else {
719:                    attr.setValue(value);
720:                }
721:            }
722:
723:            /**
724:             * Sets an attribute.
725:             *
726:             * @param fullName the non-null full name of the attribute.
727:             * @param namespace the namespace URI of the attribute, which may be null.
728:             * @param value the non-null value of the attribute.
729:             */
730:            public void setAttribute(String fullName, String namespace,
731:                    String value) {
732:                int index = fullName.indexOf(':');
733:                String name = fullName.substring(index + 1);
734:                XMLAttribute attr = this .findAttribute(name, namespace);
735:                if (attr == null) {
736:                    attr = new XMLAttribute(fullName, name, namespace, value,
737:                            "CDATA");
738:                    this .attributes.addElement(attr);
739:                } else {
740:                    attr.setValue(value);
741:                }
742:            }
743:
744:            /**
745:             * Removes an attribute.
746:             *
747:             * @param name the non-null name of the attribute.
748:             */
749:            public void removeAttribute(String name) {
750:                for (int i = 0; i < this .attributes.size(); i++) {
751:                    XMLAttribute attr = (XMLAttribute) this .attributes
752:                            .elementAt(i);
753:                    if (attr.getFullName().equals(name)) {
754:                        this .attributes.removeElementAt(i);
755:                        return;
756:                    }
757:                }
758:            }
759:
760:            /**
761:             * Removes an attribute.
762:             *
763:             * @param name the non-null name of the attribute.
764:             * @param namespace the namespace URI of the attribute, which may be null.
765:             */
766:            public void removeAttribute(String name, String namespace) {
767:                for (int i = 0; i < this .attributes.size(); i++) {
768:                    XMLAttribute attr = (XMLAttribute) this .attributes
769:                            .elementAt(i);
770:                    boolean found = attr.getName().equals(name);
771:                    if (namespace == null) {
772:                        found &= (attr.getNamespace() == null);
773:                    } else {
774:                        found &= attr.getNamespace().equals(namespace);
775:                    }
776:
777:                    if (found) {
778:                        this .attributes.removeElementAt(i);
779:                        return;
780:                    }
781:                }
782:            }
783:
784:            /**
785:             * Returns an enumeration of all attribute names.
786:             *
787:             * @return the non-null enumeration.
788:             */
789:            public Enumeration enumerateAttributeNames() {
790:        Vector result = new Vector();
791:        Enumeration enum = this .attributes.elements();
792:        while (enum.hasMoreElements()) {
793:            XMLAttribute attr = (XMLAttribute) enum.nextElement();
794:            result.addElement(attr.getFullName());
795:        }
796:        return result.elements();
797:    }
798:
799:            /**
800:             * Returns whether an attribute exists.
801:             *
802:             * @return true if the attribute exists.
803:             */
804:            public boolean hasAttribute(String name) {
805:                return this .findAttribute(name) != null;
806:            }
807:
808:            /**
809:             * Returns whether an attribute exists.
810:             *
811:             * @return true if the attribute exists.
812:             */
813:            public boolean hasAttribute(String name, String namespace) {
814:                return this .findAttribute(name, namespace) != null;
815:            }
816:
817:            /**
818:             * Returns all attributes as a Properties object.
819:             *
820:             * @return the non-null set.
821:             */
822:            public Properties getAttributes() {
823:        Properties result = new Properties();
824:        Enumeration enum = this .attributes.elements();
825:        while (enum.hasMoreElements()) {
826:            XMLAttribute attr = (XMLAttribute) enum.nextElement();
827:            result.put(attr.getFullName(), attr.getValue());
828:        }
829:        return result;
830:    }
831:
832:            /**
833:             * Returns all attributes in a specific namespace as a Properties object.
834:             *
835:             * @param namespace the namespace URI of the attributes, which may be null.
836:             *
837:             * @return the non-null set.
838:             */
839:            public Properties getAttributesInNamespace(String namespace) {
840:        Properties result = new Properties();
841:        Enumeration enum = this .attributes.elements();
842:        while (enum.hasMoreElements()) {
843:            XMLAttribute attr = (XMLAttribute) enum.nextElement();
844:            if (namespace == null) {
845:                if (attr.getNamespace() == null) {
846:                    result.put(attr.getName(), attr.getValue());
847:                }
848:            } else {
849:                if (namespace.equals(attr.getNamespace())) {
850:                    result.put(attr.getName(), attr.getValue());
851:                }
852:            }
853:        }
854:        return result;
855:    }
856:
857:            /**
858:             * Returns the system ID of the data where the element started.
859:             *
860:             * @return the system ID, or null if unknown.
861:             *
862:             * @see #getLineNr
863:             */
864:            public String getSystemID() {
865:                return this .systemID;
866:            }
867:
868:            /**
869:             * Returns the line number in the data where the element started.
870:             *
871:             * @return the line number, or NO_LINE if unknown.
872:             *
873:             * @see #NO_LINE
874:             * @see #getSystemID
875:             */
876:            public int getLineNr() {
877:                return this .lineNr;
878:            }
879:
880:            /**
881:             * Return the #PCDATA content of the element. If the element has a
882:             * combination of #PCDATA content and child elements, the #PCDATA
883:             * sections can be retrieved as unnamed child objects. In this case,
884:             * this method returns null.
885:             *
886:             * @return the content.
887:             */
888:            public String getContent() {
889:                return this .content;
890:            }
891:
892:            /**
893:             * Sets the #PCDATA content. It is an error to call this method with a
894:             * non-null value if there are child objects.
895:             *
896:             * @param content the (possibly null) content.
897:             */
898:            public void setContent(String content) {
899:                this .content = content;
900:            }
901:
902:            /**
903:             * Returns true if the element equals another element.
904:             *
905:             * @param rawElement the element to compare to
906:             */
907:            public boolean equals(Object rawElement) {
908:                try {
909:                    return this .equalsXMLElement((IXMLElement) rawElement);
910:                } catch (ClassCastException e) {
911:                    return false;
912:                }
913:            }
914:
915:            /**
916:             * Returns true if the element equals another element.
917:             *
918:             * @param rawElement the element to compare to
919:             */
920:            public boolean equalsXMLElement(IXMLElement elt) {
921:        if (! this.name.equals(elt.getName())) {
922:            return false;
923:        }
924:        if (this.attributes.size() != elt.getAttributeCount()) {
925:            return false;
926:        }
927:        Enumeration enum = this.attributes.elements();
928:        while (enum.hasMoreElements()) {
929:            XMLAttribute attr = (XMLAttribute) enum.nextElement();
930:            if (! elt.hasAttribute(attr.getName(), attr.getNamespace())) {
931:                return false;
932:            }
933:            String value = elt.getAttribute(attr.getName(),
934:                                            attr.getNamespace(),
935:                                            null);
936:            if (! attr.getValue().equals(value)) {
937:                return false;
938:            }
939:            String type = elt.getAttributeType(attr.getName(),
940:                                               attr.getNamespace());
941:            if (! attr.getType().equals(type)) {
942:                return false;
943:            }
944:        }
945:        if (this.children.size() != elt.getChildrenCount()) {
946:            return false;
947:        }
948:        for (int i = 0; i < this.children.size(); i++) {
949:            IXMLElement child1 = this.getChildAtIndex(i);
950:            IXMLElement child2 = elt.getChildAtIndex(i);
951:
952:            if (! child1.equalsXMLElement(child2)) {
953:                return false;
954:            }
955:        }
956:        return true;
957:    }
958:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.