1 package org.apache.velocity.texen.util; 2 3 18 19 import java.io.FileInputStream ; 20 import java.io.InputStream ; 21 import java.io.IOException ; 22 import java.util.Properties ; 23 import java.util.StringTokenizer ; 24 import org.apache.velocity.texen.Generator; 25 26 34 public class PropertiesUtil 35 { 36 45 public Properties load(String propertiesFile) 46 { 47 Properties properties = new Properties (); 48 String templatePath = Generator.getInstance().getTemplatePath(); 49 if (templatePath != null) 50 { 51 properties = loadFromTemplatePath(propertiesFile); 52 } 53 else 54 { 55 properties = loadFromClassPath(propertiesFile); 56 } 57 58 return properties; 59 60 } 61 62 75 protected Properties loadFromTemplatePath(String propertiesFile) 76 { 77 Properties properties = new Properties (); 78 String templatePath = Generator.getInstance().getTemplatePath(); 79 80 StringTokenizer st = new StringTokenizer (templatePath, ","); 93 while (st.hasMoreTokens()) 94 { 95 String templateDir = st.nextToken(); 96 try 97 { 98 String fullPath = propertiesFile; 110 111 if (!fullPath.startsWith(templateDir)) 114 { 115 fullPath = templateDir + "/" + propertiesFile; 116 } 117 118 properties.load(new FileInputStream (fullPath)); 119 break; 122 } 123 catch (Exception e) 124 { 125 } 127 } 128 return properties; 129 } 130 131 138 protected Properties loadFromClassPath(String propertiesFile) 139 { 140 Properties properties = new Properties (); 141 ClassLoader classLoader = this.getClass().getClassLoader(); 142 143 try 144 { 145 if (propertiesFile.startsWith("$generator")) 153 { 154 propertiesFile = propertiesFile.substring( 155 "$generator.templatePath/".length()); 156 } 157 158 InputStream inputStream = classLoader.getResourceAsStream(propertiesFile); 159 properties.load(inputStream); 160 } 161 catch (IOException ioe) 162 { 163 } 165 return properties; 166 } 167 } 168 | Popular Tags |