1 package org.sapia.regis.util; 2 3 import java.io.BufferedReader ; 4 import java.io.File ; 5 import java.io.FileInputStream ; 6 import java.io.FileNotFoundException ; 7 import java.io.IOException ; 8 import java.io.InputStream ; 9 import java.io.InputStreamReader ; 10 import java.net.URL ; 11 import java.util.Enumeration ; 12 import java.util.Iterator ; 13 import java.util.Map ; 14 import java.util.Properties ; 15 16 import org.sapia.util.text.SystemContext; 17 import org.sapia.util.text.TemplateContextIF; 18 import org.sapia.util.text.TemplateElementIF; 19 import org.sapia.util.text.TemplateException; 20 import org.sapia.util.text.TemplateFactory; 21 22 public class Utils { 23 24 public static void copyMapToProps(Properties target, Map src){ 25 Iterator itr = src.entrySet().iterator(); 26 while(itr.hasNext()){ 27 String key = (String )itr.next(); 28 target.setProperty(key, (String )src.get(key)); 29 } 30 } 31 32 public static void copyPropsToMap(Map target, Properties src){ 33 Enumeration en = src.propertyNames(); 34 while(en.hasMoreElements()){ 35 String key = (String )en.nextElement(); 36 target.put(key, src.getProperty(key)); 37 } 38 } 39 40 public static void loadProps(Class clazz, Properties props, String resource) throws FileNotFoundException , IOException { 41 InputStream is = load(clazz, resource); 42 try{ 43 props.load(is); 44 Properties newProps = replaceVars(new SystemContext(), props); 45 Enumeration names = newProps.propertyNames(); 46 while(names.hasMoreElements()){ 47 String name = (String )names.nextElement(); 48 props.setProperty(name, newProps.getProperty(name)); 49 } 50 }finally{ 51 is.close(); 52 } 53 } 54 55 public static InputStream load(Class clazz, String resource) throws FileNotFoundException , IOException { 56 InputStream is = null; 57 try{ 58 is = new FileInputStream (new File (resource)); 59 }catch(IOException e){ 60 } 62 if(is == null){ 63 try{ 64 is = new URL (resource).openStream(); 65 }catch(IOException e){ 66 } 68 } 69 if(is == null){ 70 is = clazz.getClassLoader().getResourceAsStream(resource); 71 } 72 if(is == null){ 73 is = Thread.currentThread().getContextClassLoader().getResourceAsStream(resource); 74 } 75 if(is == null){ 76 throw new FileNotFoundException (resource); 77 } 78 return is; 79 } 80 81 public static String loadAsString(InputStream is) throws IOException , Exception { 82 BufferedReader reader = new BufferedReader (new InputStreamReader (is)); 83 String line; 84 StringBuffer buf = new StringBuffer (); 85 while((line = reader.readLine()) != null){ 86 buf.append(line); 87 buf.append(System.getProperty("line.separator")); 88 } 89 is.close(); 90 return buf.toString(); 91 } 92 93 107 public static Properties replaceVars(TemplateContextIF ctx, 108 Properties props){ 109 return doReplaceVars(ctx, props, true); 110 } 111 112 public static Properties doReplaceVars(TemplateContextIF ctx, 113 Properties props, boolean secondPass){ 114 Enumeration names = props.propertyNames(); 115 Properties result = new Properties (props); 116 TemplateFactory fac = new TemplateFactory(); 117 PropertiesContext child = new PropertiesContext(props, ctx); 118 while(names.hasMoreElements()){ 119 String name = (String )names.nextElement(); 120 String value = props.getProperty(name); 121 if(value != null){ 122 TemplateElementIF elem = fac.parse(value); 123 try{ 124 value = elem.render(child); 125 }catch(TemplateException e){ 126 } 128 result.setProperty(name, value); 129 } 130 } 131 if(secondPass){ 132 return doReplaceVars(ctx, result, false); 133 } 134 return result; 135 } 136 137 public static void deleteRecurse(File f){ 138 if(f.isDirectory()){ 139 File [] files = f.listFiles(); 140 if(files != null){ 141 for(int i = 0; i < files.length; i++){ 142 if(files[i].isDirectory()){ 143 deleteRecurse(files[i]); 144 } 145 else{ 146 files[i].delete(); 147 } 148 } 149 } 150 f.delete(); 151 } 152 else{ 153 f.delete(); 154 } 155 } 156 } 157 | Popular Tags |