1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package net.smartlab.config;
24
25 import java.util.ArrayList;
26 import java.util.Collection;
27 import java.util.Collections;
28 import java.util.HashMap;
29 import java.util.Iterator;
30 import java.util.List;
31 import java.util.Map;
32
33 import org.xml.sax.Attributes;
34
35
36
37
38 public class Node extends Element {
39
40
41
42
43
44
45
46 protected List children;
47
48
49
50
51
52
53
54 protected Map attributes = new HashMap();
55
56
57
58
59
60
61 protected String content;
62
63
64
65
66 protected boolean resolved;
67
68
69
70
71
72
73
74
75
76 protected Node(Node parent, String name, Attributes attributes) {
77 super(parent, name);
78 this.children = new ArrayList();
79 if (attributes != null) {
80 for (int i = 0; i < attributes.getLength(); i++) {
81 this.attributes.put(attributes.getQName(i), attributes.getValue(i));
82 }
83 }
84 }
85
86
87
88
89 public String getId() {
90 return (String)attributes.get(Reference.IDENTIFIER);
91 }
92
93
94
95
96 public Collection getElements() {
97 return Collections.unmodifiableCollection(children);
98 }
99
100
101
102
103 public Collection getElements(String name) {
104 List result = new ArrayList();
105 if (name != null) {
106 Iterator children = this.children.iterator();
107 while (children.hasNext()) {
108 Element child = (Element)children.next();
109 if (name.equals(child.getName())) {
110 result.add(child);
111 }
112 }
113 }
114 return Collections.unmodifiableCollection(result);
115 }
116
117
118
119
120
121 public Element getElement(String name, String attribute, String value) throws ConfigurationException {
122 Iterator children = this.children.iterator();
123 while (children.hasNext()) {
124 Element child = (Element)children.next();
125 if (name != null && name.equals(child.name)) {
126 if (attribute != null) {
127 String found = child.getAttribute(attribute);
128 if (found != null) {
129 if (value != null) {
130 if (value.equals(found)) {
131 return child;
132 }
133 } else {
134 return child;
135 }
136 }
137 } else {
138 return child;
139 }
140 }
141 }
142 return null;
143 }
144
145
146
147
148 public Collection getAttributeNames() {
149 return Collections.unmodifiableCollection(attributes.keySet());
150 }
151
152
153
154
155 public Collection getAttributes() {
156 return Collections.unmodifiableCollection(attributes.entrySet());
157 }
158
159
160
161
162 public String getAttribute(String name) {
163 return (String)attributes.get(name);
164 }
165
166
167
168
169
170 public String getContent() {
171 return content;
172 }
173
174
175
176
177
178
179 protected void setContent(String content) {
180 if (content.replace('\n', ' ').replace('\t', ' ').trim().length() > 0) {
181 this.content = content;
182 }
183 }
184
185
186
187
188 public void resolve() throws ConfigurationException {
189 if (!resolved) {
190 Iterator elements = children.iterator();
191 while (elements.hasNext()) {
192 ((Element)elements.next()).resolve();
193 }
194 resolved = true;
195 }
196 }
197
198
199
200
201
202
203
204
205
206
207
208 protected Node resolve(String name, String id) throws ConfigurationException {
209 Iterator children = this.children.iterator();
210 while (children.hasNext()) {
211 Element child = (Element)children.next();
212 if (child instanceof Node && name.equals(child.name) && id.equals(child.getId())) {
213 return (Node)child;
214 }
215 }
216 return parent.resolve(name, id);
217 }
218 }