|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| |
|
17 |
| |
|
18 |
| |
|
19 |
| |
|
20 |
| package org.apache.xindice.util; |
|
21 |
| |
|
22 |
| import org.apache.commons.logging.Log; |
|
23 |
| import org.apache.commons.logging.LogFactory; |
|
24 |
| |
|
25 |
| import org.w3c.dom.Attr; |
|
26 |
| import org.w3c.dom.Document; |
|
27 |
| import org.w3c.dom.Element; |
|
28 |
| import org.w3c.dom.NamedNodeMap; |
|
29 |
| import org.w3c.dom.Node; |
|
30 |
| import org.w3c.dom.NodeList; |
|
31 |
| import org.w3c.dom.Text; |
|
32 |
| |
|
33 |
| import java.util.ArrayList; |
|
34 |
| import java.util.List; |
|
35 |
| |
|
36 |
| |
|
37 |
| |
|
38 |
| |
|
39 |
| |
|
40 |
| |
|
41 |
| |
|
42 |
| |
|
43 |
| |
|
44 |
| public final class Configuration { |
|
45 |
| |
|
46 |
| private static final Log log = LogFactory.getLog(Configuration.class); |
|
47 |
| |
|
48 |
| private static final Configuration[] EMPTY = new Configuration[0]; |
|
49 |
| |
|
50 |
| private Element config; |
|
51 |
| private boolean readOnly; |
|
52 |
| private boolean isDirty; |
|
53 |
| private Configuration root; |
|
54 |
| |
|
55 |
| |
|
56 |
1503
| public Configuration(Element config, boolean readOnly) {
|
|
57 |
1503
| this.config = config;
|
|
58 |
1503
| this.readOnly = readOnly;
|
|
59 |
| } |
|
60 |
| |
|
61 |
802
| public Configuration(Document config, boolean readOnly) {
|
|
62 |
802
| this(config.getDocumentElement(), readOnly);
|
|
63 |
| } |
|
64 |
| |
|
65 |
| |
|
66 |
| |
|
67 |
| |
|
68 |
| |
|
69 |
288
| public Configuration(Element config) {
|
|
70 |
288
| this(config, true);
|
|
71 |
| } |
|
72 |
| |
|
73 |
| |
|
74 |
| |
|
75 |
| |
|
76 |
| |
|
77 |
288
| public Configuration(Document config) {
|
|
78 |
288
| this(config.getDocumentElement());
|
|
79 |
| } |
|
80 |
| |
|
81 |
6027
| private Configuration(Configuration root, Element config, boolean readOnly) {
|
|
82 |
6027
| this.root = root;
|
|
83 |
6027
| this.config = config;
|
|
84 |
6027
| this.readOnly = readOnly;
|
|
85 |
| } |
|
86 |
| |
|
87 |
13794
| private void setDirty() {
|
|
88 |
13794
| if (root != null) {
|
|
89 |
11111
| root.setDirty();
|
|
90 |
| } else { |
|
91 |
2683
| this.isDirty = true;
|
|
92 |
| } |
|
93 |
| } |
|
94 |
| |
|
95 |
1791
| public void resetDirty() {
|
|
96 |
| |
|
97 |
1791
| this.isDirty = false;
|
|
98 |
| } |
|
99 |
| |
|
100 |
5980
| public boolean isDirty() {
|
|
101 |
5980
| if (root != null) {
|
|
102 |
1
| return root.isDirty();
|
|
103 |
| } else { |
|
104 |
5979
| return isDirty;
|
|
105 |
| } |
|
106 |
| } |
|
107 |
| |
|
108 |
| |
|
109 |
| |
|
110 |
| |
|
111 |
| |
|
112 |
| |
|
113 |
| |
|
114 |
| |
|
115 |
| |
|
116 |
| |
|
117 |
1918
| public Element getElement() throws ReadOnlyException {
|
|
118 |
1918
| if (readOnly) {
|
|
119 |
0
| throw new ReadOnlyException();
|
|
120 |
| } |
|
121 |
1918
| return config;
|
|
122 |
| } |
|
123 |
| |
|
124 |
| |
|
125 |
| |
|
126 |
| |
|
127 |
| |
|
128 |
| |
|
129 |
945
| public String getName() {
|
|
130 |
945
| return config.getNodeName();
|
|
131 |
| } |
|
132 |
| |
|
133 |
| |
|
134 |
| |
|
135 |
| |
|
136 |
| |
|
137 |
| |
|
138 |
| |
|
139 |
17
| public boolean hasAttributes() {
|
|
140 |
17
| return config.getAttributes().getLength() > 0;
|
|
141 |
| } |
|
142 |
| |
|
143 |
| |
|
144 |
| |
|
145 |
| |
|
146 |
| |
|
147 |
| |
|
148 |
| |
|
149 |
| |
|
150 |
11113
| public String getAttribute(String name, String defValue) {
|
|
151 |
11113
| String value = config.getAttribute(name);
|
|
152 |
11113
| if (value == null || value.length() == 0) {
|
|
153 |
3457
| value = defValue;
|
|
154 |
| } |
|
155 |
11113
| return value;
|
|
156 |
| } |
|
157 |
| |
|
158 |
| |
|
159 |
| |
|
160 |
| |
|
161 |
| |
|
162 |
| |
|
163 |
| |
|
164 |
5996
| public String getAttribute(String name) {
|
|
165 |
5996
| return getAttribute(name, "");
|
|
166 |
| } |
|
167 |
| |
|
168 |
| |
|
169 |
| |
|
170 |
| |
|
171 |
| |
|
172 |
| |
|
173 |
| |
|
174 |
| |
|
175 |
5038
| public boolean getBooleanAttribute(String name, boolean defValue) {
|
|
176 |
5038
| try {
|
|
177 |
5038
| String attr = getAttribute(name, defValue ? "1" : "0").toLowerCase();
|
|
178 |
5038
| return "[true][yes][1][y][on]".indexOf("[" + attr + "]") != -1;
|
|
179 |
| } catch (Exception e) { |
|
180 |
0
| return defValue;
|
|
181 |
| } |
|
182 |
| } |
|
183 |
| |
|
184 |
| |
|
185 |
| |
|
186 |
| |
|
187 |
| |
|
188 |
| |
|
189 |
| |
|
190 |
1
| public boolean getBooleanAttribute(String name) {
|
|
191 |
1
| return getBooleanAttribute(name, false);
|
|
192 |
| } |
|
193 |
| |
|
194 |
| |
|
195 |
| |
|
196 |
| |
|
197 |
| |
|
198 |
| |
|
199 |
| |
|
200 |
| |
|
201 |
2269
| public int getIntAttribute(String name, int defValue) {
|
|
202 |
2269
| try {
|
|
203 |
2269
| return Integer.parseInt(config.getAttribute(name));
|
|
204 |
| } catch (Exception e) { |
|
205 |
1840
| return defValue;
|
|
206 |
| } |
|
207 |
| } |
|
208 |
| |
|
209 |
| |
|
210 |
| |
|
211 |
| |
|
212 |
| |
|
213 |
| |
|
214 |
| |
|
215 |
1
| public int getIntAttribute(String name) {
|
|
216 |
1
| return getIntAttribute(name, 0);
|
|
217 |
| } |
|
218 |
| |
|
219 |
| |
|
220 |
| |
|
221 |
| |
|
222 |
| |
|
223 |
| |
|
224 |
| |
|
225 |
| |
|
226 |
3141
| public short getShortAttribute(String name, short defValue) {
|
|
227 |
3141
| try {
|
|
228 |
3141
| return Short.parseShort(config.getAttribute(name));
|
|
229 |
| } catch (Exception e) { |
|
230 |
3141
| return defValue;
|
|
231 |
| } |
|
232 |
| } |
|
233 |
| |
|
234 |
| |
|
235 |
| |
|
236 |
| |
|
237 |
| |
|
238 |
| |
|
239 |
| |
|
240 |
0
| public short getShortAttribute(String name) {
|
|
241 |
0
| return getShortAttribute(name, (short) 0);
|
|
242 |
| } |
|
243 |
| |
|
244 |
| |
|
245 |
| |
|
246 |
| |
|
247 |
| |
|
248 |
| |
|
249 |
| |
|
250 |
| |
|
251 |
1134
| public long getLongAttribute(String name, long defValue) {
|
|
252 |
1134
| try {
|
|
253 |
1134
| return Long.parseLong(config.getAttribute(name));
|
|
254 |
| } catch (Exception e) { |
|
255 |
973
| return defValue;
|
|
256 |
| } |
|
257 |
| } |
|
258 |
| |
|
259 |
| |
|
260 |
| |
|
261 |
| |
|
262 |
| |
|
263 |
| |
|
264 |
| |
|
265 |
0
| public long getLongAttribute(String name) {
|
|
266 |
0
| return getLongAttribute(name, (long) 0);
|
|
267 |
| } |
|
268 |
| |
|
269 |
| |
|
270 |
| |
|
271 |
| |
|
272 |
| |
|
273 |
| |
|
274 |
| |
|
275 |
| |
|
276 |
0
| public float getFloatAttribute(String name, float defValue) {
|
|
277 |
0
| try {
|
|
278 |
0
| return Float.parseFloat(config.getAttribute(name));
|
|
279 |
| } catch (Exception e) { |
|
280 |
0
| return defValue;
|
|
281 |
| } |
|
282 |
| } |
|
283 |
| |
|
284 |
| |
|
285 |
| |
|
286 |
| |
|
287 |
| |
|
288 |
| |
|
289 |
| |
|
290 |
0
| public float getFloatAttribute(String name) {
|
|
291 |
0
| return getFloatAttribute(name, (float) 0.0);
|
|
292 |
| } |
|
293 |
| |
|
294 |
| |
|
295 |
| |
|
296 |
| |
|
297 |
| |
|
298 |
| |
|
299 |
| |
|
300 |
| |
|
301 |
0
| public double getDoubleAttribute(String name, double defValue) {
|
|
302 |
0
| try {
|
|
303 |
0
| return Double.parseDouble(config.getAttribute(name));
|
|
304 |
| } catch (Exception e) { |
|
305 |
0
| return defValue;
|
|
306 |
| } |
|
307 |
| } |
|
308 |
| |
|
309 |
| |
|
310 |
| |
|
311 |
| |
|
312 |
| |
|
313 |
| |
|
314 |
| |
|
315 |
0
| public double getDoubleAttribute(String name) {
|
|
316 |
0
| return getDoubleAttribute(name, 0.0D);
|
|
317 |
| } |
|
318 |
| |
|
319 |
| |
|
320 |
| |
|
321 |
| |
|
322 |
| |
|
323 |
| |
|
324 |
| |
|
325 |
| |
|
326 |
0
| public byte getByteAttribute(String name, byte defValue) {
|
|
327 |
0
| try {
|
|
328 |
0
| return Byte.parseByte(config.getAttribute(name));
|
|
329 |
| } catch (Exception e) { |
|
330 |
0
| return defValue;
|
|
331 |
| } |
|
332 |
| } |
|
333 |
| |
|
334 |
| |
|
335 |
| |
|
336 |
| |
|
337 |
| |
|
338 |
| |
|
339 |
| |
|
340 |
0
| public byte getByteAttribute(String name) {
|
|
341 |
0
| return getByteAttribute(name, (byte) 0);
|
|
342 |
| } |
|
343 |
| |
|
344 |
| |
|
345 |
| |
|
346 |
| |
|
347 |
| |
|
348 |
| |
|
349 |
| |
|
350 |
| |
|
351 |
0
| public char getCharAttribute(String name, char defValue) {
|
|
352 |
0
| try {
|
|
353 |
0
| return config.getAttribute(name).charAt(0);
|
|
354 |
| } catch (Exception e) { |
|
355 |
0
| return defValue;
|
|
356 |
| } |
|
357 |
| } |
|
358 |
| |
|
359 |
| |
|
360 |
| |
|
361 |
| |
|
362 |
| |
|
363 |
| |
|
364 |
| |
|
365 |
0
| public char getCharAttribute(String name) {
|
|
366 |
0
| return getCharAttribute(name, '\0');
|
|
367 |
| } |
|
368 |
| |
|
369 |
| |
|
370 |
| |
|
371 |
| |
|
372 |
| |
|
373 |
| |
|
374 |
| |
|
375 |
10
| public String[] listAttributes() {
|
|
376 |
10
| NamedNodeMap map = config.getAttributes();
|
|
377 |
10
| String[] result = new String[map.getLength()];
|
|
378 |
10
| int size = map.getLength();
|
|
379 |
10
| for (int i = 0; i < size; i++) {
|
|
380 |
20
| result[i] = ((Attr) map.item(i)).getName();
|
|
381 |
| } |
|
382 |
10
| return result;
|
|
383 |
| } |
|
384 |
| |
|
385 |
| |
|
386 |
| |
|
387 |
| |
|
388 |
| |
|
389 |
| |
|
390 |
| |
|
391 |
17
| public boolean hasValue() {
|
|
392 |
17
| NodeList list = config.getChildNodes();
|
|
393 |
17
| return (list.getLength() > 0 && list.item(0).getNodeType() == Node.TEXT_NODE);
|
|
394 |
| } |
|
395 |
| |
|
396 |
| |
|
397 |
| |
|
398 |
| |
|
399 |
| |
|
400 |
| |
|
401 |
| |
|
402 |
0
| public String getValue(String defValue) {
|
|
403 |
0
| NodeList list = config.getChildNodes();
|
|
404 |
0
| if (list.getLength() == 1 && list.item(0).getNodeType() == Node.TEXT_NODE) {
|
|
405 |
0
| return list.item(0).getNodeValue();
|
|
406 |
| } else { |
|
407 |
0
| return defValue;
|
|
408 |
| } |
|
409 |
| } |
|
410 |
| |
|
411 |
| |
|
412 |
| |
|
413 |
| |
|
414 |
| |
|
415 |
| |
|
416 |
0
| public String getValue() {
|
|
417 |
0
| return getValue("");
|
|
418 |
| } |
|
419 |
| |
|
420 |
| |
|
421 |
| |
|
422 |
| |
|
423 |
| |
|
424 |
| |
|
425 |
| |
|
426 |
0
| public boolean getBooleanValue(boolean defValue) {
|
|
427 |
0
| try {
|
|
428 |
0
| return "[true][yes][1][y][on]".indexOf("[" + getValue().toLowerCase() + "]") != -1;
|
|
429 |
| } catch (Exception e) { |
|
430 |
0
| return defValue;
|
|
431 |
| } |
|
432 |
| } |
|
433 |
| |
|
434 |
| |
|
435 |
| |
|
436 |
| |
|
437 |
| |
|
438 |
| |
|
439 |
0
| public boolean getBooleanValue() {
|
|
440 |
0
| return getBooleanValue(false);
|
|
441 |
| } |
|
442 |
| |
|
443 |
| |
|
444 |
| |
|
445 |
| |
|
446 |
| |
|
447 |
| |
|
448 |
| |
|
449 |
0
| public int getIntValue(int defValue) {
|
|
450 |
0
| try {
|
|
451 |
0
| return Integer.parseInt(getValue());
|
|
452 |
| } catch (Exception e) { |
|
453 |
0
| return defValue;
|
|
454 |
| } |
|
455 |
| } |
|
456 |
| |
|
457 |
| |
|
458 |
| |
|
459 |
| |
|
460 |
| |
|
461 |
| |
|
462 |
0
| public int getIntValue() {
|
|
463 |
0
| return getIntValue(0);
|
|
464 |
| } |
|
465 |
| |
|
466 |
| |
|
467 |
| |
|
468 |
| |
|
469 |
| |
|
470 |
| |
|
471 |
| |
|
472 |
0
| public short getShortValue(short defValue) {
|
|
473 |
0
| try {
|
|
474 |
0
| return Short.parseShort(getValue());
|
|
475 |
| } catch (Exception e) { |
|
476 |
0
| return defValue;
|
|
477 |
| } |
|
478 |
| } |
|
479 |
| |
|
480 |
| |
|
481 |
| |
|
482 |
| |
|
483 |
| |
|
484 |
| |
|
485 |
0
| public short getShortValue() {
|
|
486 |
0
| return getShortValue((short) 0);
|
|
487 |
| } |
|
488 |
| |
|
489 |
| |
|
490 |
| |
|
491 |
| |
|
492 |
| |
|
493 |
|