Menu

[r4431]: / trunk / drjava / src / edu / rice / cs / util / XMLConfig.java  Maximize  Restore  History

Download this file

817 lines (749 with data), 24.6 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
/*BEGIN_COPYRIGHT_BLOCK
*
* Copyright (c) 2001-2007, JavaPLT group at Rice University (javaplt@rice.edu)
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the names of DrJava, the JavaPLT group, Rice University, nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software is Open Source Initiative approved Open Source Software.
* Open Source Initative Approved is a trademark of the Open Source Initiative.
*
* This file is part of DrJava. Download the current version of this project
* from http://www.drjava.org/ or http://sourceforge.net/projects/drjava/
*
* END_COPYRIGHT_BLOCK*/
package edu.rice.cs.util;
import junit.framework.Assert;
import junit.framework.TestCase;
import org.w3c.dom.*;
import org.xml.sax.InputSource;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.*;
import java.util.*;
/**
* XML configuration management.
* <p/>
* This class uses DOM paths of a specific form to refer to nodes in the XML document.
* Consider this XML structure:
* <foo a="foo.a">
* <bar>abc</bar>
* <fum fee="xyz">def</fum>
* </foo>
* The path "foo/bar" refers to the value "abc".
* The path "foo/fum" refers to the value "def".
* If this form is used, there may be only #text or #comment nodes in the node. All #text nodes will be
* concatenated and then stripped of whitespace at the beginning and the end.
* The path "foo/fum.fee" refers to the value "xyz".
* The path "foo.a" refers to the value "foo.a".
*
* When using getMultiple, any node or attribute name can be substituted with "*" to get all elements:
* The path "foo/*" returns both the value "abc" and "def".
* @author Mathias Ricken
*/
public class XMLConfig {
/**
* Newline string.
*/
public static final String NL = System.getProperty("line.separator");
/**
* XML document.
*/
private Document _document;
/**
* XMLConfig to delegate to, or null.
*/
private XMLConfig _parent = null;
/**
* Node where this XMLConfig starts if delegation is used, or null.
*/
private Node _startNode = null;
/**
* Path where this XMLConfig starts, or null.
*/
private String _startPath = null;
/**
* Creates an empty configuration.
*/
public XMLConfig() {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder builder = factory.newDocumentBuilder();
_document = builder.newDocument(); // Create from whole cloth
// NOTE: not 1.4 compatible -- _document.setXmlStandalone(true);
}
catch(ParserConfigurationException e) {
e.printStackTrace();
}
}
/**
* Creates a configuration from an input stream.
* @param is input stream
*/
public XMLConfig(InputStream is) {
init(new InputSource(is));
}
/**
* Creates a configuration from a reader.
* @param r reader
*/
public XMLConfig(Reader r) {
init(new InputSource(r));
}
/**
* Creates a configuration that is a part of another configuration, starting at the specified node.
* @param parent the configuration that contains this part
* @param node the node in the parent configuration where this part starts
*/
public XMLConfig(XMLConfig parent, Node node) {
if ((parent==null) || (node==null)) { throw new XMLConfigException("Error in ctor: parent or node is null"); }
_parent = parent;
_startNode = node;
_startPath = getNodePath(node);
_document = null;
}
/**
* Initialize this XML configuration.
* @param is the XML input source
*/
private void init(InputSource is) {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = null;
try {
builder = factory.newDocumentBuilder();
_document = builder.parse(is);
// NOTE: not 1.4 compatible -- _document.setXmlStandalone(true);
}
catch(Exception e) {
throw new XMLConfigException("Error in ctor", e);
}
_document.normalize();
}
/**
* Creates a configuration from a file.
* @param f file
*/
public XMLConfig(File f) {
try {
init(new InputSource(new FileInputStream(f)));
}
catch(FileNotFoundException e) {
throw new XMLConfigException("Error in ctor", e);
}
}
/**
* Creates a configuration from a file name.
* @param filename file name
*/
public XMLConfig(String filename) {
try {
init(new InputSource(new FileInputStream(filename)));
}
catch(FileNotFoundException e) {
throw new XMLConfigException("Error in ctor", e);
}
}
public boolean isDelegated() { return (_parent!=null); }
/**
* Saves configuration to an output stream
* @param os output stream
*/
public void save(OutputStream os) {
if (isDelegated()) { _parent.save(os); return; }
// Prepare the DOM document for writing
Source source = new DOMSource(_document);
/*
// Prepare the output file
Result result = new StreamResult(os);
*/
// Write the DOM document to the file
try {
TransformerFactory tf = TransformerFactory.newInstance();
tf.setAttribute("indent-number", new Integer(2));
Transformer t = tf.newTransformer();
t.setOutputProperty(OutputKeys.INDENT, "yes");
t.transform(source, new StreamResult(new OutputStreamWriter(os, "utf-8")));
/*
Transformer xformer = TransformerFactory.newInstance().newTransformer();
xformer.setOutputProperty(OutputKeys.INDENT, "yes");
xformer.transform(source, result);
*/
}
catch(TransformerException e) {
throw new XMLConfigException("Error in save", e);
}
catch(UnsupportedEncodingException e) {
throw new XMLConfigException("Error in save", e);
}
}
/**
* Saves configuration to a file.
* @param f file
*/
public void save(File f) {
if (isDelegated()) { _parent.save(f); return; }
try {
save(new FileOutputStream(f));
}
catch(FileNotFoundException e) {
throw new XMLConfigException("Error in save", e);
}
}
/**
* Saves configuration to a file specified by a file name.
* @param filename file name
*/
public void save(String filename) {
save(new File(filename));
}
// ----- String ------
/**
* Returns the value as specified by the DOM path.
* @param path DOM path
* @return value.
*/
public String get(String path) {
List<String> r = getMultiple(path);
if (r.size()!=1) throw new XMLConfigException("Number of results != 1");
return r.get(0);
}
/**
* Returns the value as specified by the DOM path.
* @param path DOM path
* @param root node where the search should start
* @return value.
*/
public String get(String path, Node root) {
List<String> r = getMultiple(path, root);
if (r.size()!=1) throw new XMLConfigException("Number of results != 1");
return r.get(0);
}
/**
* Returns the value as specified by the DOM path, or the default value if the value could not be found.
* @param path DOM path
* @param defaultVal default value in case value is not in DOM
* @return value.
*/
public String get(String path, String defaultVal) {
try {
return get(path);
}
catch(XMLConfigException e) {
return defaultVal;
}
}
/**
* Returns the value as specified by the DOM path, or the default value if the value could not be found.
* @param path DOM path
* @param root node where the search should start
* @param defaultVal default value in case value is not in DOM
* @return value.
*/
public String get(String path, Node root, String defaultVal) {
try {
return get(path, root);
}
catch(XMLConfigException e) {
return defaultVal;
}
}
// ----- Integer ------
/**
* Returns the value as specified by the DOM path.
* @param path DOM path
* @return value.
* @throws IllegalArgumentException
*/
public int getInt(String path) {
List<String> r = getMultiple(path);
if (r.size()!=1) throw new XMLConfigException("Number of results != 1");
try {
return new Integer(r.get(0));
}
catch(NumberFormatException nfe) { throw new IllegalArgumentException("Not an integer value.", nfe); }
}
/**
* Returns the value as specified by the DOM path.
* @param path DOM path
* @param root node where the search should start
* @return value.
* @throws IllegalArgumentException
*/
public int getInt(String path, Node root) {
List<String> r = getMultiple(path, root);
if (r.size()!=1) throw new XMLConfigException("Number of results != 1");
try {
return new Integer(r.get(0));
}
catch(NumberFormatException nfe) { throw new IllegalArgumentException("Not an integer value.", nfe); }
}
/**
* Returns the value as specified by the DOM path, or the default value if the value could not be found.
* @param path DOM path
* @param defaultVal default value in case value is not in DOM
* @return value.
* @throws IllegalArgumentException
*/
public int getInt(String path, int defaultVal) {
try {
return getInt(path);
}
catch(XMLConfigException e) {
return defaultVal;
}
}
/**
* Returns the value as specified by the DOM path, or the default value if the value could not be found.
* @param path DOM path
* @param root node where the search should start
* @param defaultVal default value in case value is not in DOM
* @return value.
* @throws IllegalArgumentException
*/
public int getInt(String path, Node root, int defaultVal) {
try {
return getInt(path, root);
}
catch(XMLConfigException e) {
return defaultVal;
}
}
// ----- Boolean ------
/**
* Returns the value as specified by the DOM path.
* @param path DOM path
* @return value.
* @throws IllegalArgumentException
*/
public boolean getBool(String path) {
List<String> r = getMultiple(path);
if (r.size()!=1) throw new XMLConfigException("Number of results != 1");
String s = r.get(0).toLowerCase().trim();
if ((s.equals("true")) ||
(s.equals("yes")) ||
(s.equals("on"))) return true;
if ((s.equals("false")) ||
(s.equals("no")) ||
(s.equals("off"))) return false;
throw new IllegalArgumentException("Not a Boolean vlaue.");
}
/**
* Returns the value as specified by the DOM path.
* @param path DOM path
* @param root node where the search should start
* @return value.
* @throws IllegalArgumentException
*/
public boolean getBool(String path, Node root) {
List<String> r = getMultiple(path, root);
if (r.size()!=1) throw new XMLConfigException("Number of results != 1");
String s = r.get(0).toLowerCase().trim();
if ((s.equals("true")) ||
(s.equals("yes")) ||
(s.equals("on"))) return true;
if ((s.equals("false")) ||
(s.equals("no")) ||
(s.equals("off"))) return false;
throw new IllegalArgumentException("Not a Boolean vlaue.");
}
/**
* Returns the value as specified by the DOM path, or the default value if the value could not be found.
* @param path DOM path
* @param defaultVal default value in case value is not in DOM
* @return value.
* @throws IllegalArgumentException
*/
public boolean getBool(String path, boolean defaultVal) {
try {
return getBool(path);
}
catch(XMLConfigException e) {
return defaultVal;
}
}
/**
* Returns the value as specified by the DOM path, or the default value if the value could not be found.
* @param path DOM path
* @param root node where the search should start
* @param defaultVal default value in case value is not in DOM
* @return value.
* @throws IllegalArgumentException
*/
public boolean getBool(String path, Node root, boolean defaultVal) {
try {
return getBool(path, root);
}
catch(XMLConfigException e) {
return defaultVal;
}
}
// ----- Other -----
/**
* Returns the value as specified by the DOM path.
* @param path DOM path
* @return list of values.
*/
public List<String> getMultiple(String path) {
if (isDelegated()) { return getMultiple(path, _startNode); }
return getMultiple(path, _document);
}
/**
* Returns the value as specified by the DOM path.
* @param path DOM path
* @param root node where the search should start
* @return list of values.
*/
public List<String> getMultiple(String path, Node root) {
List<Node> accum = getNodes(path, root);
List<String> strings = new LinkedList<String>();
for(Node n: accum) {
if (n instanceof Attr) {
strings.add(n.getNodeValue());
}
else {
Node child;
String acc = "";
child = n.getFirstChild();
while(child!=null) {
if (child.getNodeName().equals("#text")) {
acc += " " + child.getNodeValue();
}
else if (child.getNodeName().equals("#comment")) {
// ignore
}
else {
throw new XMLConfigException("Node "+n.getNodeName()+" contained node "+child.getNodeName()+", but should only contain #text and #comment.");
}
child = child.getNextSibling();
}
strings.add(acc.trim());
}
}
return strings;
}
/**
* Returns the nodes as specified by the DOM path.
* @param path DOM path
* @return list of nodes.
*/
public List<Node> getNodes(String path) {
if (isDelegated()) { return getNodes(path, _startNode); }
return getNodes(path, _document);
}
/**
* Returns the nodes as specified by the DOM path.
* @param path DOM path
* @param root node where the search should start
* @return list of nodes.
*/
public List<Node> getNodes(String path, Node root) {
List<Node> accum = new LinkedList<Node>();
getMultipleHelper(path, root, accum, false);
return accum;
}
/**
* Returns the value as specified by the DOM path.
* @param path DOM path
* @param n node where the search begins
* @param accum accumulator
* @param dotRead whether a dot has been read
*/
private void getMultipleHelper(String path, Node n, List<Node> accum, boolean dotRead) {
int dotPos = path.indexOf('.');
boolean initialDot = (dotPos==0);
if ((path.length()>0) && (dotPos == -1) && (!path.endsWith("/"))) {
path = path + "/";
}
int slashPos = path.indexOf('/');
if (((slashPos > -1) || (dotPos > -1)) && !dotRead || initialDot) {
String nodeName;
if ((slashPos > -1) && ((dotPos == -1) || (slashPos < dotPos))) {
nodeName = path.substring(0, slashPos);
path = path.substring(slashPos+1);
}
else {
if (slashPos > -1) {
throw new XMLConfigException("An attribute cannot have subparts (foo.bar.fum and foo.bar/fum not allowed)");
}
if (!initialDot) {
nodeName = path.substring(0, dotPos);
path = path.substring(dotPos+1);
dotRead = true;
}
else {
path = nodeName = path.substring(1);
getMultipleAddAttributesHelper(path, n, accum);
return;
}
}
Node child = n.getFirstChild();
if (nodeName.equals("*")) {
while(child!=null) {
if (!child.getNodeName().equals("#text") && !child.getNodeName().equals("#comment")) {
if (dotRead) {
getMultipleAddAttributesHelper(path, child, accum);
}
else {
getMultipleHelper(path, child, accum, false);
}
}
child = child.getNextSibling();
}
return;
}
else {
while(child!=null) {
if (child.getNodeName().equals(nodeName)) {
// found
if (dotRead) {
getMultipleAddAttributesHelper(path, child, accum);
}
else {
getMultipleHelper(path, child, accum, false);
}
}
child = child.getNextSibling();
}
return;
}
}
else {
accum.add(n);
}
}
private void getMultipleAddAttributesHelper(String path, Node n, List<Node> accum) {
if ((path.indexOf('.') > -1) || (path.indexOf('/') > -1)) {
throw new XMLConfigException("An attribute cannot have subparts (foo.bar.fum and foo.bar/fum not allowed)");
}
NamedNodeMap attrMap = n.getAttributes();
if (path.equals("*")) {
for(int i=0; i<attrMap.getLength(); ++i) {
Node attr = attrMap.item(i);
accum.add(attr);
}
}
else {
Node attr = attrMap.getNamedItem(path);
if (attr!=null) {
accum.add(attr);
}
}
}
/**
* Set the value of the node or attribute specified by the DOM path.
* @param path DOM path
* @param value node or attribute value
* @return the node that was created, or the parent node of the attribute if it was an attribute
*/
public Node set(String path, String value) {
if (isDelegated()) { return set(path, value, _startNode, true); }
return set(path, value, _document, true);
}
/**
* Set the value of the node or attribute specified by the DOM path.
* @param path DOM path
* @param value node or attribute value
* @param overwrite whether to overwrite (true) or add (false)
* @return the node that was created, or the parent node of the attribute if it was an attribute
*/
public Node set(String path, String value, boolean overwrite) {
if (isDelegated()) { return set(path, value, _startNode, overwrite); }
return set(path, value, _document, overwrite);
}
/**
* Set the value of the node or attribute specified by the DOM path.
* @param path DOM path
* @param value node or attribute value
* @param n node where the search should start
* @param overwrite whether to overwrite (true) or add (false) -- only applies for last node!
* @return the node that was created, or the parent node of the attribute if it was an attribute
*/
public Node set(String path, String value, Node n, boolean overwrite) {
if (isDelegated()) { return _parent.set(path, value, n, overwrite); }
int dotPos = path.lastIndexOf('.');
Node node;
if (dotPos==0) {
node = n;
}
else {
node = createNode(path, n, overwrite);
}
if (dotPos>=0) {
Element e = (Element)node;
e.setAttribute(path.substring(dotPos+1),value);
}
else {
node.appendChild(_document.createTextNode(value));
}
return node;
}
/**
* Create the node specified by the DOM path.
* @param path DOM path
* @return the node that was created, or the parent node of the attribute if it was an attribute
*/
public Node createNode(String path) {
if (isDelegated()) { return createNode(path, _startNode, true); }
return createNode(path, _document, true);
}
/**
* Create the node specified by the DOM path.
* @param path DOM path
* @param n node where the search should start, or null for the root
* @return the node that was created, or the parent node of the attribute if it was an attribute
*/
public Node createNode(String path, Node n) {
return createNode(path, n, true);
}
/**
* Create the node specified by the DOM path.
* @param path DOM path
* @param n node where the search should start, or null for the root
* @param overwrite whether to overwrite (true) or add (false) -- only applies for last node!
* @return the node that was created, or the parent node of the attribute if it was an attribute
*/
public Node createNode(String path, Node n, boolean overwrite) {
if (isDelegated()) { return _parent.createNode(path, n, overwrite); }
if (n==null) { n = _document; }
while(path.indexOf('/') > -1) {
Node child = null;
String nodeName = path.substring(0, path.indexOf('/'));
path = path.substring(path.indexOf('/')+1);
child = n.getFirstChild();
while(child!=null) {
if (child.getNodeName().equals(nodeName)) {
// found
n = child;
break;
}
child = child.getNextSibling();
}
if (child==null) {
// not found
child = _document.createElement(nodeName);
n.appendChild(child);
n = child;
}
}
String nodeName;
if (path.indexOf('.') > -1) {
nodeName = path.substring(0, path.indexOf('.'));
}
else {
if (path.length()==0) {
throw new XMLConfigException("Cannot set node with empty name");
}
nodeName = path;
}
Node child = null;
if (nodeName.length()>0) {
if (overwrite) {
child = n.getFirstChild();
while(child!=null) {
if (child.getNodeName().equals(nodeName)) {
// found
n = child;
break;
}
child = child.getNextSibling();
}
if (child==null) {
child = _document.createElement(nodeName);
n.appendChild(child);
n = child;
}
}
else {
child = _document.createElement(nodeName);
n.appendChild(child);
n = child;
}
}
if (path.indexOf('.') > -1) {
if (!(n instanceof Element)) {
throw new XMLConfigException("Node "+n.getNodeName()+" should be an element so it can contain attributes");
}
return n;
}
else {
if (overwrite) {
child = n.getFirstChild();
// remove all children
while(child!=null) {
Node temp = child.getNextSibling();
n.removeChild(child);
child = temp;
}
return n;
}
else {
return child;
}
}
}
/**
* Returns a string representation of the object.
* @return a string representation of the object.
*/
public String toString() {
ByteArrayOutputStream os = new ByteArrayOutputStream();
save(os);
return os.toString();
}
/**
* Return the path of a node as it is used in XMLConfig.
* @param n node
* @return path
*/
public static String getNodePath(Node n) {
if (n==null) { return ""; }
String path = "";
while(n.getParentNode()!=null) {
path = n.getNodeName()+"/"+path;
n = n.getParentNode();
}
return path.substring(0,path.length()-1);
}
/**
* Exception in XMLConfig methods.
*/
public static class XMLConfigException extends RuntimeException {
public XMLConfigException() {
super();
}
public XMLConfigException(String message) {
super(message);
}
public XMLConfigException(String message, Throwable cause) {
super(message, cause);
}
public XMLConfigException(Throwable cause) {
super(cause);
}
}
}
MongoDB Logo MongoDB