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