KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > regis > util > ResourceFinder


1 package org.sapia.regis.util;
2
3 import java.io.FileNotFoundException JavaDoc;
4 import java.io.IOException JavaDoc;
5 import java.io.InputStream JavaDoc;
6 import java.util.ArrayList JavaDoc;
7 import java.util.Enumeration JavaDoc;
8 import java.util.List JavaDoc;
9 import java.util.Properties JavaDoc;
10
11 import org.sapia.util.text.SystemContext;
12
13 public class ResourceFinder {
14   
15   private List JavaDoc _resources = new ArrayList JavaDoc();
16   private String JavaDoc _list;
17   
18   ResourceFinder(String JavaDoc list){
19     _list = list;
20   }
21   
22   public static InputStream JavaDoc findResource(String JavaDoc resourceList) throws FileNotFoundException JavaDoc, IOException JavaDoc{
23     return ResourceFinderFactory.parse(resourceList).find(false);
24   }
25   
26   public static InputStream JavaDoc getResource(String JavaDoc resourceList) throws IOException JavaDoc{
27     return ResourceFinderFactory.parse(resourceList).find(true);
28   }
29   
30   public static void findProperties(String JavaDoc resourceList, Properties JavaDoc props) throws FileNotFoundException JavaDoc, IOException JavaDoc{
31     ResourceFinderFactory.parse(resourceList).doLoadProperties(false, props);
32   }
33   
34   public static void loadProperties(String JavaDoc resourceList, Properties JavaDoc props) throws FileNotFoundException JavaDoc, IOException JavaDoc{
35     ResourceFinderFactory.parse(resourceList).doLoadProperties(true, props);
36   }
37   
38   void addResource(String JavaDoc resource){
39     _resources.add(resource);
40   }
41   
42   void addResources(List JavaDoc resources){
43     _resources.add(resources);
44   }
45   
46   InputStream JavaDoc find(boolean ignoreNotFound) throws FileNotFoundException JavaDoc, IOException JavaDoc{
47     for(int i = 0; i < _resources.size(); i++){
48       Object JavaDoc res = _resources.get(i);
49       if(res instanceof String JavaDoc){
50         if(ignoreNotFound){
51           try{
52             return Utils.load(ResourceFinder.class, (String JavaDoc)res);
53           }catch(FileNotFoundException JavaDoc e){
54             continue;
55           }
56         }
57         else{
58           return Utils.load(ResourceFinder.class, (String JavaDoc)res);
59         }
60       }
61       else{
62         try{
63           List JavaDoc resources = (List JavaDoc)res;
64           for(int j = 0; j < resources.size(); j++){
65             return Utils.load(ResourceFinder.class, (String JavaDoc)resources.get(j));
66           }
67         }catch(IOException JavaDoc e){
68           //noop
69
}
70       }
71     }
72     if(ignoreNotFound){
73       return null;
74     }
75     throw new FileNotFoundException JavaDoc("No resource could be found in: " + _list);
76   }
77   
78   void doLoadProperties(boolean ignoreNotFound, Properties JavaDoc props) throws FileNotFoundException JavaDoc, IOException JavaDoc{
79     for(int i = 0; i < _resources.size(); i++){
80       Object JavaDoc res = _resources.get(i);
81       if(res instanceof String JavaDoc){
82         if(ignoreNotFound){
83           try{
84             doLoad(Utils.load(ResourceFinder.class, (String JavaDoc)res), props);
85           }catch(FileNotFoundException JavaDoc e){
86             continue;
87           }
88         }
89         else{
90           doLoad(Utils.load(ResourceFinder.class, (String JavaDoc)res), props);
91         }
92       }
93       else{
94         try{
95           List JavaDoc resources = (List JavaDoc)res;
96           for(int j = 0; j < resources.size(); j++){
97             doLoad(Utils.load(ResourceFinder.class, (String JavaDoc)resources.get(j)), props);
98           }
99         }catch(IOException JavaDoc e){
100           //noop
101
}
102       }
103     }
104   }
105   
106   private void doLoad(InputStream JavaDoc is, Properties JavaDoc props) throws IOException JavaDoc{
107     Properties JavaDoc newProps = new Properties JavaDoc();
108     try{
109       newProps.load(is);
110     }finally{
111       is.close();
112     }
113     newProps = Utils.replaceVars(new PropertiesContext(props, new SystemContext()), newProps);
114     Enumeration JavaDoc names = newProps.propertyNames();
115     while(names.hasMoreElements()){
116       String JavaDoc name = (String JavaDoc)names.nextElement();
117       props.setProperty(name, newProps.getProperty(name));
118     }
119   }
120
121 }
122
Popular Tags