KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > gumby > Settings


1 package org.sapia.gumby;
2
3 import java.io.InputStream JavaDoc;
4 import java.net.URL JavaDoc;
5 import java.util.ArrayList JavaDoc;
6 import java.util.HashMap JavaDoc;
7 import java.util.List JavaDoc;
8 import java.util.Map JavaDoc;
9
10 /**
11  * Utility class that holds configuration settings and provides various services
12  * (such as resource and class loading).
13  *
14  * @author Yanick Duchesne
15  *
16  * <dl>
17  * <dt><b>Copyright: </b>
18  * <dd>Copyright &#169; 2002-2005 <a HREF="http://www.sapia-oss.org">Sapia Open
19  * Source Software </a>. All Rights Reserved.</dd>
20  * </dt>
21  * <dt><b>License: </b>
22  * <dd>Read the license.txt file of the jar or visit the <a
23  * HREF="http://www.sapia-oss.org/license.html">license page </a> at the Sapia
24  * OSS web site</dd>
25  * </dt>
26  * </dl>
27  */

28 public class Settings {
29
30   public static final String JavaDoc SCOPE = "settings";
31
32   private Settings _parent;
33   private List JavaDoc _imports = new ArrayList JavaDoc();
34   private Map JavaDoc _vars = new HashMap JavaDoc();
35   private Class JavaDoc _resourceBase;
36
37   public Settings() {
38     _imports.add("javax.swing");
39   }
40
41   public Settings(Settings parent) {
42     _parent = parent;
43   }
44
45   /**
46    * @return returns the variables that are bound to this instance, in a
47    * <code>Map</code>.
48    */

49   public Map JavaDoc getBindings() {
50     return _vars;
51   }
52
53   /**
54    * @param pckg
55    * adds the given import package.
56    */

57   public void addImport(String JavaDoc pckg) {
58     _imports.add(pckg);
59   }
60
61   /**
62    * Sets the <code>Class</code> instance that should be used to retrieve
63    * resources from the classpath.
64    *
65    * @param base
66    * a <code>Class</code> instance.
67    */

68   public void setResourceBase(Class JavaDoc base) {
69     _resourceBase = base;
70   }
71
72   /**
73    * This method uses the <code>Class</code> that has been defined as the
74    * "resource base" to retrieve the resource corresponding to the given name.
75    * If the resource base has not beed set, this method uses the
76    * <code>ClassLoader</code> of the calling thread.
77    *
78    * @see #setResourceBase(Class)
79    *
80    * @param resName
81    * the name of the desired resource.
82    * @return an <code>InputStream</code>, or <code>null</code> if no
83    * resource could be found for the given name.
84    */

85   public InputStream JavaDoc resolveResource(String JavaDoc resName) {
86     InputStream JavaDoc resource = null;
87     if(_resourceBase != null)
88       resource = _resourceBase.getResourceAsStream(resName);
89     else if(_parent != null)
90       resource = _parent.resolveResource(resName);
91     if(resource == null)
92       return Thread.currentThread().getContextClassLoader()
93           .getResourceAsStream(resName);
94
95     return resource;
96   }
97
98   /**
99    * @see #resolveResource(String)
100    * @return a <code>URL</code>, or <code>null</code> if no URL could be
101    * found for the given resource.
102    */

103   public URL JavaDoc resolveURL(String JavaDoc resName) {
104     URL JavaDoc resource = null;
105     if(_resourceBase != null)
106       resource = _resourceBase.getResource(resName);
107     else if(_parent != null)
108       resource = _parent.resolveURL(resName);
109     if(resource == null)
110       return Thread.currentThread().getContextClassLoader()
111           .getResource(resName);
112
113     return resource;
114   }
115
116   /**
117    * @param shortName
118    * the "short" name of a class (excluding the package).
119    * @return a <code>Class</code>, or <code>null</code> if no class could
120    * be found for the given name.
121    */

122   public Class JavaDoc resolveClass(String JavaDoc shortName) {
123     for(int i = 0; i < _imports.size(); i++) {
124       try {
125         return Class.forName(((String JavaDoc) _imports.get(i) + "." + shortName));
126       } catch(ClassNotFoundException JavaDoc e) {
127         //noop;
128
}
129     }
130     try {
131       return Class.forName(shortName);
132     } catch(ClassNotFoundException JavaDoc e) {
133       //noop
134
}
135     if(_parent != null)
136       return _parent.resolveClass(shortName);
137     else
138       return null;
139
140   }
141 }
142
Popular Tags