Source code: net/jxta/ext/config/Resource.java
1 /*
2 * Copyright (c) 2001 Sun Microsystems, Inc. All rights
3 * reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and thproe following disclaimer in
14 * the documentation and/or other materials provided with the
15 * distribution.
16 *
17 * 3. The end-user documentation included with the redistribution,
18 * if any, must include the following acknowledgment:
19 * "This product includes software developed by the
20 * Sun Microsystems, Inc. for Project JXTA."
21 * Alternately, this acknowledgment may appear in the software itself,
22 * if and wherever such third-party acknowledgments normally appear.
23 *
24 * 4. The names "Sun", "Sun Microsystems, Inc.", "JXTA" and "Project JXTA"
25 * must not be used to endorse or promote products derived from this
26 * software without prior written permission. For written
27 * permission, please contact Project JXTA at http://www.jxta.org.
28 *
29 * 5. Products derived from this software may not be called "JXTA",
30 * nor may "JXTA" appear in their name, without prior written
31 * permission of Sun.
32 *
33 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
34 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
35 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
36 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
37 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
38 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
39 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
40 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
41 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
42 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
43 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
44 * SUCH DAMAGE.
45 * ====================================================================
46 *
47 * This software consists of voluntary contributions made by many
48 * individuals on behalf of Project JXTA. For more
49 * information on Project JXTA, please see
50 * <http://www.jxta.org/>.
51 *
52 * This license is based on the BSD license adopted by the Apache Foundation.
53 *
54 * $Id: Resource.java,v 1.15 2004/07/31 07:44:34 gonzo Exp $
55 */
56 package net.jxta.ext.config;
57
58 import java.awt.Color;
59 import java.io.ByteArrayInputStream;
60 import java.io.ByteArrayOutputStream;
61 import java.io.File;
62 import java.io.InputStream;
63 import java.io.IOException;
64 import java.net.MalformedURLException;
65 import java.net.URI;
66 import java.net.URL;
67 import java.util.ArrayList;
68 import java.util.Iterator;
69 import java.util.List;
70 import org.jaxen.JaxenException;
71 import org.jaxen.jdom.JDOMXPath;
72 import org.jaxen.XPath;
73 import org.jdom.Attribute;
74 import org.jdom.Document;
75 import org.jdom.Element;
76 import org.jdom.Content;
77 import org.jdom.input.SAXBuilder;
78 import org.jdom.JDOMException;
79 import org.jdom.output.XMLOutputter;
80 import org.jdom.output.Format;
81 import org.jdom.Text;
82
83 /**
84 * @author james todd [gonzo at jxta dot org]
85 * @version $Id: Resource.java,v 1.15 2004/07/31 07:44:34 gonzo Exp $
86 */
87
88 public class Resource {
89
90 private final static String ROOT = "ROOT";
91 private final static String PATH_DELIMITER = "/";
92 private final static String FILE_SEPERATOR =
93 System.getProperty("file.separator", "/");
94 private final static String EXCEPTION_PREFIX =
95 "unable to find resource: ";
96 private final static Object lock = new Object();
97 private final static boolean VERBOSE = false;
98
99 private List resources = null;
100 private Element root = null;
101
102
103 /**
104 * Constructor for the Resource object
105 */
106 public Resource() {
107 this.resources = new ArrayList();
108 this.root = new Element(ROOT);
109 }
110
111
112 /**
113 * Description of the Method
114 *
115 * @param s Description of the Parameter
116 * @return Description of the Return Value
117 * @exception ConversionException Description of the Exception
118 */
119 public static URL toURL(String s)
120 throws ConversionException {
121 return Conversion.toURL(s);
122 }
123
124
125 /**
126 * Description of the Method
127 *
128 * @param f Description of the Parameter
129 * @return Description of the Return Value
130 * @exception ConversionException Description of the Exception
131 */
132 public static URL toURL(File f)
133 throws ConversionException {
134 return Conversion.toURL(f);
135 }
136
137
138 /**
139 * Description of the Method
140 *
141 * @param resource Description of the Parameter
142 * @exception ResourceNotFoundException Description of the Exception
143 */
144 public void load(URL resource)
145 throws ResourceNotFoundException {
146 process(resource);
147 }
148
149
150 /**
151 * Description of the Method
152 *
153 * @param resource Description of the Parameter
154 * @exception ResourceNotFoundException Description of the Exception
155 */
156 public void load(String resource)
157 throws ResourceNotFoundException {
158 Exception e = null;
159
160 try {
161 load(Resource.toURL(resource));
162 } catch (ConversionException ce) {
163 e = ce;
164 }
165 catch (ResourceNotFoundException rnfe) {
166 e = rnfe;
167 }
168
169 if (e != null) {
170 e = null;
171
172 try {
173 process(resource);
174 } catch (ResourceNotFoundException rnfe) {
175 if (!resource.startsWith(FILE_SEPERATOR)) {
176 process(FILE_SEPERATOR + resource);
177 }
178 }
179 }
180 }
181
182
183 /**
184 * Description of the Method
185 *
186 * @param resource Description of the Parameter
187 * @exception ResourceNotFoundException Description of the Exception
188 * @exception MalformedURLException Description of the Exception
189 */
190 public void load(File resource)
191 throws ResourceNotFoundException, MalformedURLException {
192 load(resource.toURL());
193 }
194
195
196 /**
197 * Description of the Method
198 *
199 * @param resource Description of the Parameter
200 * @param clazz Description of the Parameter
201 * @exception ResourceNotFoundException Description of the Exception
202 */
203 public void load(String resource, Class clazz)
204 throws ResourceNotFoundException {
205 // try {
206 // load(resource, clazz);
207 // } catch (ResourceNotFoundException rnfe) {
208 process(resource, clazz);
209 // }
210 }
211
212
213 /**
214 * Description of the Method
215 *
216 * @param is Description of the Parameter
217 * @exception ResourceNotFoundException Description of the Exception
218 */
219 public void load(InputStream is)
220 throws ResourceNotFoundException {
221 process(is);
222 }
223
224 /**
225 * Gets the resourceAsStream attribute of the Resource object
226 *
227 * @param resource Description of the Parameter
228 * @return The resourceAsStream value
229 */
230 public InputStream getResourceAsStream(String resource) {
231 return getResourceAsStream(resource, null);
232 }
233
234
235 /**
236 * Gets the resourceAsStream attribute of the Resource object
237 *
238 * @param resource Description of the Parameter
239 * @param clazz Description of the Parameter
240 * @return The resourceAsStream value
241 */
242 public InputStream getResourceAsStream(String resource, Class clazz) {
243 if (clazz == null) {
244 clazz = Resource.class;
245 }
246
247 return clazz.getResourceAsStream(resource);
248 }
249
250
251 /**
252 * Description of the Method
253 *
254 * @param key Description of the Parameter
255 * @return Description of the Return Value
256 */
257 public String get(String key) {
258 return get(key, null);
259 }
260
261
262 /**
263 * Description of the Method
264 *
265 * @param key Description of the Parameter
266 * @param d Description of the Parameter
267 * @return Description of the Return Value
268 */
269 public String get(String key, String d) {
270 return getValue(key, d);
271 }
272
273
274 /**
275 * Gets the int attribute of the Resource object
276 *
277 * @param key Description of the Parameter
278 * @return The int value
279 * @exception ConversionException Description of the Exception
280 */
281 public int getInt(String key)
282 throws ConversionException {
283 return getInt(key, null);
284 }
285
286
287 /**
288 * Gets the int attribute of the Resource object
289 *
290 * @param key Description of the Parameter
291 * @param d Description of the Parameter
292 * @return The int value
293 */
294 public int getInt(String key, int d) {
295 int i = 0;
296
297 try {
298 i = getInt(key, new Integer(d));
299 } catch (ConversionException ce) {}
300
301 return i;
302 }
303
304
305 /**
306 * Gets the int attribute of the Resource object
307 *
308 * @param key Description of the Parameter
309 * @param d Description of the Parameter
310 * @return The int value
311 * @exception ConversionException Description of the Exception
312 */
313 public int getInt(String key, Integer d)
314 throws ConversionException {
315 String s = get(key, (d != null) ? d.toString() : null);
316
317 return Conversion.toInt(s);
318 }
319
320 /**
321 * Gets the long attribute of the Resource object
322 *
323 * @param key Description of the Parameter
324 * @return The long value
325 * @exception ConversionException Description of the Exception
326 */
327 public long getLong(String key)
328 throws ConversionException {
329 return getLong(key, null);
330 }
331
332
333 /**
334 * Gets the long attribute of the Resource object
335 *
336 * @param key Description of the Parameter
337 * @param d Description of the Parameter
338 * @return The int value
339 */
340 public long getLong(String key, long d) {
341 long i = 0;
342
343 try {
344 i = getLong(key, new Long(d));
345 } catch (ConversionException ce) {}
346
347 return i;
348 }
349
350
351 /**
352 * Gets the long attribute of the Resource object
353 *
354 * @param key Description of the Parameter
355 * @param d Description of the Parameter
356 * @return The long value
357 * @exception ConversionException Description of the Exception
358 */
359 public long getLong(String key, Long d)
360 throws ConversionException {
361 String s = get(key, (d != null) ? d.toString() : null);
362
363 return Conversion.toLong(s);
364 }
365
366 /**
367 * Gets the float attribute of the Resource object
368 *
369 * @param key Description of the Parameter
370 * @return The float value
371 * @exception ConversionException Description of the Exception
372 */
373 public float getFloat(String key)
374 throws ConversionException {
375 return getFloat(key, null);
376 }
377
378
379 /**
380 * Gets the float attribute of the Resource object
381 *
382 * @param key Description of the Parameter
383 * @param d Description of the Parameter
384 * @return The float value
385 * @exception ConversionException Description of the Exception
386 */
387 public float getFloat(String key, Float d)
388 throws ConversionException {
389 String s = get(key, (d != null) ? d.toString() : null);
390
391 return Conversion.toFloat(s);
392 }
393
394
395 /**
396 * Gets the boolean attribute of the Resource object
397 *
398 * @param key Description of the Parameter
399 * @return The boolean value
400 */
401 public boolean getBoolean(String key) {
402 return getBoolean(key, null);
403 }
404
405
406 /**
407 * Gets the boolean attribute of the Resource object
408 *
409 * @param key Description of the Parameter
410 * @param d Description of the Parameter
411 * @return The boolean value
412 */
413 public boolean getBoolean(String key, boolean d) {
414 return getBoolean(key, Boolean.valueOf(d));
415 }
416
417
418 /**
419 * Gets the boolean attribute of the Resource object
420 *
421 * @param key Description of the Parameter
422 * @param d Description of the Parameter
423 * @return The boolean value
424 */
425 public boolean getBoolean(String key, Boolean d) {
426 String s = get(key, (d != null) ? d.toString() : null);
427
428 return Boolean.valueOf(s).booleanValue();
429 }
430
431
432 /**
433 * Gets the char attribute of the Resource object
434 *
435 * @param key Description of the Parameter
436 * @return The char value
437 * @exception ConversionException Description of the Exception
438 */
439 public char getChar(String key)
440 throws ConversionException {
441 return getChar(key, new Character((char)0));
442 }
443
444
445 /**
446 * Gets the char attribute of the Resource object
447 *
448 * @param key Description of the Parameter
449 * @param d Description of the Parameter
450 * @return The char value
451 * @exception ConversionException Description of the Exception
452 */
453 public char getChar(String key, Character d)
454 throws ConversionException {
455 String s = get(key, (d != null) ? d.toString() : null);
456
457 return Conversion.toChar(s);
458 }
459
460
461 /**
462 * Gets the URL attribute of the Resource object
463 *
464 * @param key Description of the Parameter
465 * @return The URL value
466 * @exception ConversionException Description of the Exception
467 */
468 public URL getURL(String key)
469 throws ConversionException {
470 return getURL(key, null);
471 }
472
473
474 /**
475 * Gets the URL attribute of the Resource object
476 *
477 * @param key Description of the Parameter
478 * @param d Description of the Parameter
479 * @return The URL value
480 * @exception ConversionException Description of the Exception
481 */
482 public URL getURL(String key, URL d)
483 throws ConversionException {
484 String s = get(key, (d != null) ? d.toString() : null);
485
486 return Conversion.toURL(s);
487 }
488
489
490 /**
491 * Gets the URLs attribute of the Resource object
492 *
493 * @param key Description of the Parameter
494 * @return The URLs value
495 * @exception ConversionException Description of the Exception
496 */
497 public List getURLs(String key)
498 throws ConversionException {
499 return getURLs(key, null);
500 }
501
502
503 /**
504 * Gets the URLs attribute of the Resource object
505 *
506 * @param key Description of the Parameter
507 * @param d Description of the Parameter
508 * @return The URLs value
509 * @exception ConversionException Description of the Exception
510 */
511 public List getURLs(String key, URL d)
512 throws ConversionException {
513 List values = getAll(key, (d != null) ? d.toString() : null);
514
515 return Conversion.toURLs(values);
516 }
517
518
519 /**
520 * Gets the URI attribute of the Resource object
521 *
522 * @param key Description of the Parameter
523 * @return The URI value
524 * @exception ConversionException Description of the Exception
525 */
526 public URI getURI(String key)
527 throws ConversionException {
528 return getURI(key, null);
529 }
530
531
532 /**
533 * Gets the URI attribute of the Resource object
534 *
535 * @param key Description of the Parameter
536 * @param d Description of the Parameter
537 * @return The URI value
538 * @exception ConversionException Description of the Exception
539 */
540 public URI getURI(String key, URI d)
541 throws ConversionException {
542 String s = get(key, (d != null) ? d.toString() : null);
543
544 return Conversion.toURI(s);
545 }
546
547
548 /**
549 * Gets the URIs attribute of the Resource object
550 *
551 * @param key Description of the Parameter
552 * @return The URIs value
553 * @exception ConversionException Description of the Exception
554 */
555 public List getURIs(String key)
556 throws ConversionException {
557 return getURIs(key, null);
558 }
559
560
561 /**
562 * Gets the URIs attribute of the Resource object
563 *
564 * @param key Description of the Parameter
565 * @param d Description of the Parameter
566 * @return The URIs value
567 * @exception ConversionException Description of the Exception
568 */
569 public List getURIs(String key, URI d)
570 throws ConversionException {
571 List values = getAll(key, (d != null) ? d.toString() : null);
572
573 return Conversion.toURIs(values);
574 }
575
576 /**
577 * Gets the color attribute of the Resource object
578 *
579 * @param key Description of the Parameter
580 * @return The color value
581 * @exception ConversionException Description of the Exception
582 */
583 public Color getColor(String key)
584 throws ConversionException {
585 return getColor(key, null);
586 }
587
588
589 /**
590 * Gets the color attribute of the Resource object
591 *
592 * @param key Description of the Parameter
593 * @param d Description of the Parameter
594 * @return The color value
595 * @exception ConversionException Description of the Exception
596 */
597 public Color getColor(String key, Color d)
598 throws ConversionException {
599 String s = get(key, (d != null) ? d.toString() : null);
600
601 return Conversion.toColor(s);
602 }
603
604 public Resource getResource(String key) {
605 List r = getResources(key);
606
607 return r != null ? (Resource)r.get(0) : null;
608 }
609
610 public List getResources(String key) {
611 List r = new ArrayList();
612
613 for (Iterator v = getValues(key).iterator(); v.hasNext(); ) {
614 Object o = v.next();
615
616 if (o instanceof Element) {
617 ByteArrayOutputStream buf = new ByteArrayOutputStream();
618
619 try {
620 new XMLOutputter().output(new Document((Element)o), buf);
621 } catch (IOException ioe) {
622 ioe.printStackTrace();
623 }
624
625 Resource cr = new Resource();
626
627 try {
628 cr.load(new ByteArrayInputStream(buf.toByteArray()));
629 } catch (ResourceNotFoundException rnfe) {
630 rnfe.printStackTrace();
631 }
632
633 r.add(cr);
634 }
635 }
636
637 return r;
638 }
639
640 /**
641 * Gets the class attribute of the Resource object
642 *
643 * @param key Description of the Parameter
644 * @return The class value
645 * @exception ConversionException Description of the Exception
646 */
647 public Class getClass(String key)
648 throws ConversionException {
649 return getClass(key, null);
650 }
651
652
653 /**
654 * Gets the class attribute of the Resource object
655 *
656 * @param key Description of the Parameter
657 * @param d Description of the Parameter
658 * @return The class value
659 * @exception ConversionException Description of the Exception
660 */
661 public Class getClass(String key, Class d)
662 throws ConversionException {
663 String s = get(key, (d != null) ? d.getName() : null);
664
665 return Conversion.toClass(s);
666 }
667
668
669 /**
670 * Gets the classes attribute of the Resource object
671 *
672 * @param key Description of the Parameter
673 * @return The classes value
674 * @exception ConversionException Description of the Exception
675 */
676 public List getClasses(String key)
677 throws ConversionException {
678 return getClasses(key, null);
679 }
680
681
682 /**
683 * Gets the classes attribute of the Resource object
684 *
685 * @param key Description of the Parameter
686 * @param d Description of the Parameter
687 * @return The classes value
688 * @exception ConversionException Description of the Exception
689 */
690 public List getClasses(String key, URL d)
691 throws ConversionException {
692 List values = getAll(key, (d != null) ? d.toString() : null);
693
694 return Conversion.toClasses(values);
695 }
696
697
698 /**
699 * Description of the Method
700 *
701 * @param key Description of the Parameter
702 */
703 public void set(String key) {
704 set(key, null);
705 }
706
707
708 /**
709 * Description of the Method
710 *
711 * @param key Description of the Parameter
712 * @param value Description of the Parameter
713 */
714 public void set(String key, String value) {
715 synchronized (this.root) {
716 Object o = getValue(key, false);
717
718 if (o != null) {
719 if (o instanceof Attribute) {
720 Attribute a = (Attribute)o;
721
722 if (value != null) {
723 a.setValue(value);
724 } else {
725 a.getParent().removeAttribute(a);
726 }
727 } else if (o instanceof Element) {
728 Element e = (Element)o;
729
730 if (value != null) {
731 List l = new ArrayList();
732
733 l.add(new Text(value));
734
735 e.setContent(l);
736 } else {
737 e.getParent().removeContent(e);
738 }
739 }
740 } else {
741 // xxx: add it ... oh my
742 }
743 }
744 }
745
746
747 /**
748 * Gets the all attribute of the Resource object
749 *
750 * @param key Description of the Parameter
751 * @return The all value
752 */
753 public List getAll(String key) {
754 return getAll(key, null);
755 }
756
757
758 /**
759 * Gets the all attribute of the Resource object
760 *
761 * @param key Description of the Parameter
762 * @param d Description of the Parameter
763 * @return The all value
764 */
765 public List getAll(String key, String d) {
766 return getValues(key, d);
767 }
768
769 public boolean contains(String key) {
770 return get(key) != null;
771 }
772
773 /**
774 * Description of the Method
775 *
776 * @return Description of the Return Value
777 */
778 public String toString() {
779 String s = null;
780
781 if (this.root != null) {
782 ByteArrayOutputStream buf = new ByteArrayOutputStream();
783
784 try {
785 new XMLOutputter(Format.getPrettyFormat()).output(new Document(this.root), buf);
786 } catch (IOException ioe) {
787 ioe.printStackTrace();
788 }
789
790 s = buf.toString();
791 }
792
793 return s != null ? s : "null";
794 }
795
796 /**
797 * Description of the Method
798 *
799 * @param resource Description of the Parameter
800 * @exception ResourceNotFoundException Description of the Exception
801 */
802 private void process(Object resource)
803 throws ResourceNotFoundException {
804 process(resource, null);
805 }
806
807
808 /**
809 * Description of the Method
810 *
811 * @param resource Description of the Parameter
812 * @param clazz Description of the Parameter
813 * @exception ResourceNotFoundException Description of the Exception
814 */
815 private void process(Object resource, Class clazz)
816 throws ResourceNotFoundException {
817 if (!this.resources.contains(resource)) {
818 Element el = null;
819 Exception ex = null;
820
821 if (resource instanceof URL) {
822 try {
823 el = getRootElement((URL)resource);
824 } catch (JDOMException jde) {
825 ex = jde;
826 }
827 catch (IOException ioe) {
828 ex = ioe;
829 }
830 } else if (resource instanceof String) {
831 if (clazz == null) {
832 clazz = Resource.class;
833 }
834
835 try {
836 el = getElement(clazz.getResourceAsStream((String)resource));
837 } catch (JDOMException jde) {
838 jde.printStackTrace();
839 ex = jde;
840 }
841 catch (IOException ioe) {
842 ex = ioe;
843 }
844 } else if (resource instanceof InputStream) {
845 try {
846 el = getElement((InputStream)resource);
847 } catch (JDOMException jde) {
848 ex = jde;
849 }
850 catch (IOException ioe) {
851 ex = ioe;
852 }
853
854 resource = new String("InputStream." +
855 System.currentTimeMillis());
856 }
857
858 if (el != null &&
859 ex == null) {
860 load(resource, el);
861 } else {
862 if (VERBOSE) {
863 ex.printStackTrace();
864 }
865
866 throw new ResourceNotFoundException(EXCEPTION_PREFIX +
867 ": " +
868 resource.toString(), ex);
869 }
870 }
871 }
872
873
874 /**
875 * Gets the element attribute of the Resource object
876 *
877 * @param is Description of the Parameter
878 * @return The element value
879 * @exception JDOMException Description of the Exception
880 * @exception IOException Description of the Exception
881 */
882 private Element getElement(InputStream is)
883 throws JDOMException, IOException {
884 Element element = null;
885
886 try {
887 element = getRootElement(is);
888 } catch (JDOMException jde) {
889 throw jde;
890 }
891 catch (IOException ioe) {
892 throw ioe;
893 } finally {
894 if (is != null) {
895 try {
896 is.close();
897 } catch (IOException ioe) {}
898 }
899 }
900
901 return element;
902 }
903
904
905 /**
906 * Gets the rootElement attribute of the Resource object
907 *
908 * @param resource Description of the Parameter
909 * @return The rootElement value
910 * @exception JDOMException Description of the Exception
911 * @exception IOException Description of the Exception
912 */
913 private Element getRootElement(URL resource)
914 throws JDOMException, IOException {
915 return new SAXBuilder().build((URL)resource).getRootElement();
916 }
917
918
919 /**
920 * Gets the rootElement attribute of the Resource object
921 *
922 * @param is Description of the Parameter
923 * @return The rootElement value
924 * @exception JDOMException Description of the Exception
925 * @exception IOException Description of the Exception
926 */
927 private Element getRootElement(InputStream is)
928 throws JDOMException, IOException {
929 return new SAXBuilder().build(is).getRootElement();
930 }
931
932 /**
933 * Description of the Method
934 *
935 * @param resource Description of the Parameter
936 * @param element Description of the Parameter
937 */
938 private void load(Object resource, Element element) {
939 if (resources != null &&
940 element != null) {
941 synchronized (lock) {
942 this.resources.add(resource);
943 this.root.addContent((Content)element.clone());
944 }
945 }
946 }
947
948
949 /**
950 * Gets the value attribute of the Resource object
951 *
952 * @param key Description of the Parameter
953 * @param d Description of the Parameter
954 * @return The value value
955 */
956 private String getValue(String key, String d) {
957 String v = null;
958 Object o = getValue(key);
959
960 if (o != null) {
961 if (o instanceof Attribute) {
962 v = ((Attribute)o).getValue();
963 } else if (o instanceof Element) {
964 v = ((Element)o).getTextTrim();
965 }
966 }
967
968 return Util.expand(v != null ? v : d);
969 }
970
971
972 /**
973 * Gets the value attribute of the Resource object
974 *
975 * @param key Description of the Parameter
976 * @return The value value
977 */
978 private Object getValue(String key) {
979 return getValue(key, true);
980 }
981
982
983 /**
984 * Gets the value attribute of the Resource object
985 *
986 * @param key Description of the Parameter
987 * @param isClone Description of the Parameter
988 * @return The value value
989 */
990 private Object getValue(String key, boolean isClone) {
991 Object o = null;
992 XPath xp = getXP(key);
993
994 if (xp != null) {
995 try {
996 o = xp.selectSingleNode(getRootDocument(isClone));
997 } catch (JaxenException je) {
998 if (VERBOSE) {
999 je.printStackTrace();
1000 }
1001 }
1002 }
1003
1004 return o;
1005 }
1006
1007
1008 /**
1009 * Gets the values attribute of the Resource object
1010 *
1011 * @param key Description of the Parameter
1012 * @param d Description of the Parameter
1013 * @return The values value
1014 */
1015 private List getValues(String key, String d) {
1016 List v = new ArrayList();
1017 Object o = null;
1018
1019 for (Iterator n = getValues(key).iterator(); n.hasNext(); ) {
1020 o = n.next();
1021
1022 if (o != null) {
1023 if (o instanceof Attribute) {
1024 v.add(((Attribute)o).getValue());
1025 } else if (o instanceof Element) {
1026 v.add(((Element)o).getTextTrim());
1027 }
1028 }
1029 }
1030
1031 if (v.size() == 0 &&
1032 d != null) {
1033 v.add(d);
1034 }
1035
1036 return v;
1037 }
1038
1039
1040 /**
1041 * Gets the values attribute of the Resource object
1042 *
1043 * @param key Description of the Parameter
1044 * @return The values value
1045 */
1046 private List getValues(String key) {
1047 List v = new ArrayList();
1048 XPath xp = getXP(key);
1049
1050 if (xp != null) {
1051
1052 try {
1053 for (Iterator n = xp.selectNodes(getRootDocument()).iterator();
1054 n.hasNext(); ) {
1055 v.add(n.next());
1056 }
1057 } catch (JaxenException je) {
1058 if (VERBOSE) {
1059 je.printStackTrace();
1060 }
1061 }
1062 }
1063
1064 return v;
1065 }
1066
1067
1068 /**
1069 * Gets the xP attribute of the Resource object
1070 *
1071 * @param key Description of the Parameter
1072 * @return The xP value
1073 */
1074 private XPath getXP(String key) {
1075 XPath xp = null;
1076
1077 try {
1078 xp = new JDOMXPath(PATH_DELIMITER + ROOT + PATH_DELIMITER + key);
1079 } catch (JaxenException je) {
1080 if (VERBOSE) {
1081 je.printStackTrace();
1082 }
1083 }
1084
1085 return xp;
1086 }
1087
1088
1089 /**
1090 * Gets the rootDocument attribute of the Resource object
1091 *
1092 * @return The rootDocument value
1093 */
1094 private Document getRootDocument() {
1095 return getRootDocument(true);
1096 }
1097
1098
1099 /**
1100 * Gets the rootDocument attribute of the Resource object
1101 *
1102 * @param isClone Description of the Parameter
1103 * @return The rootDocument value
1104 */
1105 private Document getRootDocument(boolean isClone) {
1106 Document d = null;
1107
1108 if (isClone) {
1109 d = new Document((Element)this.root.clone());
1110 } else if ((d = this.root.getDocument()) == null) {
1111 d = new Document(this.root);
1112 }
1113
1114 return d;
1115 }
1116}
1117