KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > thoughtworks > xstream > io > xml > xppdom > Xpp3Dom


1 package com.thoughtworks.xstream.io.xml.xppdom;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.HashMap JavaDoc;
5 import java.util.List JavaDoc;
6 import java.util.Map JavaDoc;
7
8 public class Xpp3Dom {
9     protected String JavaDoc name;
10
11     protected String JavaDoc value;
12
13     protected Map JavaDoc attributes;
14
15     protected List JavaDoc childList;
16
17     protected Map JavaDoc childMap;
18
19     protected Xpp3Dom parent;
20
21     public Xpp3Dom(String JavaDoc name) {
22         this.name = name;
23         childList = new ArrayList JavaDoc();
24         childMap = new HashMap JavaDoc();
25     }
26
27     // ----------------------------------------------------------------------
28
// Name handling
29
// ----------------------------------------------------------------------
30

31     public String JavaDoc getName() {
32         return name;
33     }
34
35     // ----------------------------------------------------------------------
36
// Value handling
37
// ----------------------------------------------------------------------
38

39     public String JavaDoc getValue() {
40         return value;
41     }
42
43     public void setValue(String JavaDoc value) {
44         this.value = value;
45     }
46
47     // ----------------------------------------------------------------------
48
// Attribute handling
49
// ----------------------------------------------------------------------
50

51     public String JavaDoc[] getAttributeNames() {
52         if ( null == attributes ) {
53             return new String JavaDoc[0];
54         }
55         else {
56             return (String JavaDoc[]) attributes.keySet().toArray( new String JavaDoc[0] );
57         }
58     }
59
60     public String JavaDoc getAttribute(String JavaDoc name) {
61         return (null != attributes) ? (String JavaDoc) attributes.get(name) : null;
62     }
63
64     public void setAttribute(String JavaDoc name, String JavaDoc value) {
65         if (null == attributes) {
66             attributes = new HashMap JavaDoc();
67         }
68
69         attributes.put(name, value);
70     }
71
72     // ----------------------------------------------------------------------
73
// Child handling
74
// ----------------------------------------------------------------------
75

76     public Xpp3Dom getChild(int i) {
77         return (Xpp3Dom) childList.get(i);
78     }
79
80     public Xpp3Dom getChild(String JavaDoc name) {
81         return (Xpp3Dom) childMap.get(name);
82     }
83
84     public void addChild(Xpp3Dom xpp3Dom) {
85         xpp3Dom.setParent(this);
86         childList.add(xpp3Dom);
87         childMap.put(xpp3Dom.getName(), xpp3Dom);
88     }
89
90     public Xpp3Dom[] getChildren() {
91         if ( null == childList ) {
92             return new Xpp3Dom[0];
93         }
94         else {
95             return (Xpp3Dom[]) childList.toArray( new Xpp3Dom[0] );
96         }
97     }
98
99     public Xpp3Dom[] getChildren( String JavaDoc name ) {
100         if ( null == childList ) {
101             return new Xpp3Dom[0];
102         }
103         else {
104             ArrayList JavaDoc children = new ArrayList JavaDoc();
105             int size = this.childList.size();
106
107             for ( int i = 0; i < size; i++ ) {
108                 Xpp3Dom configuration = (Xpp3Dom) this.childList.get( i );
109                 if ( name.equals( configuration.getName() ) ) {
110                     children.add( configuration );
111                 }
112             }
113
114             return (Xpp3Dom[]) children.toArray( new Xpp3Dom[0] );
115         }
116     }
117
118     public int getChildCount() {
119         if (null == childList) {
120             return 0;
121         }
122
123         return childList.size();
124     }
125
126     // ----------------------------------------------------------------------
127
// Parent handling
128
// ----------------------------------------------------------------------
129

130     public Xpp3Dom getParent() {
131         return parent;
132     }
133
134     public void setParent(Xpp3Dom parent) {
135         this.parent = parent;
136     }
137 }
138
Popular Tags