1 package org.tigris.scarab.util.build; 2 3 48 49 import java.io.BufferedReader ; 50 import java.io.File ; 51 import java.io.FileReader ; 52 import java.io.FileWriter ; 53 import java.io.IOException ; 54 import java.io.PrintWriter ; 55 import java.io.Reader ; 56 import java.io.Writer ; 57 import java.util.Hashtable ; 58 import java.util.List ; 59 import java.util.Map ; 60 import java.util.StringTokenizer ; 61 import java.util.Vector ; 62 63 70 71 public class PropertyFileGenerator 72 { 73 78 private File templateFile; 79 80 87 private File customFile; 88 89 94 95 private Map userProperties; 96 97 103 public boolean setTemplate(String theTemplatePath) 104 { 105 boolean status = true; 106 templateFile = new File (theTemplatePath); 107 if(!templateFile.exists()) 108 { 109 status = false; 110 } 111 return status; 112 } 113 114 119 public String getTemplate() 120 { 121 return (templateFile==null)? null:templateFile.getAbsolutePath(); 122 } 123 124 130 public boolean setCustom(String theCustomPath) 131 { 132 boolean status = true; 133 customFile = new File (theCustomPath); 134 if(!customFile.getParentFile().exists()) 135 { 136 customFile.getParentFile().mkdir(); 137 } 138 139 if(customFile.exists() && 140 !customFile.canWrite()) 141 { 142 status = false; 143 } 144 145 return status; 146 } 147 148 153 public String getCustom() 154 { 155 String result = null; 156 if ( customFile != null ) 157 { 158 result = customFile.getAbsolutePath(); 159 } 160 return result; 161 } 162 163 171 public boolean setProperties(String theUserPathes) 172 { 173 List filePathes = createPathList(theUserPathes); 174 175 if(filePathes != null) 176 { 177 userProperties = new Hashtable (); 178 for(int index=0; index<filePathes.size(); index++) 179 { 180 File file = new File ((String )filePathes.get(index)); 181 if(file.exists()) 182 { 183 if(!file.canRead()) 184 { 185 throw new RuntimeException ("No Read permission for file ["+filePathes.get(index)+"]"); 186 } 187 try 188 { 189 addUnresolvedProperties(file,userProperties); 190 } 191 catch (IOException e) 192 { 193 throw new RuntimeException ("Could not read file ["+filePathes.get(index)+"]"); 194 } 195 } 196 } 197 } 198 return true; 199 } 200 201 205 private void addUnresolvedProperties(File file, Map properties) throws IOException 206 { 207 Reader reader = new FileReader (file); 208 BufferedReader br = new BufferedReader (reader); 209 210 String line; 211 212 while((line=br.readLine()) != null) 213 { 214 String trimmedLine = line.trim(); 215 if ( trimmedLine.equals("") 216 ||trimmedLine.startsWith("#") ) 217 { 218 continue; } 220 else 221 { 222 String name = null; 223 String value = null; 224 int index = line.indexOf("="); 225 if(index >=0) 226 { 227 name = line.substring(0,index).trim(); 228 value = line.substring(index+1).trim(); 229 } 230 else 231 { 232 name = line.trim(); 233 value = ""; 234 } 235 236 if(properties.get(name) == null) 237 { 238 properties.put(name,value); 239 } 240 } 241 } 242 br.close(); 243 } 244 245 254 private List createPathList(String theUserPathes) 255 { 256 List result = null; 257 StringTokenizer stok = new StringTokenizer (theUserPathes,":"); 258 while(stok.hasMoreTokens()) 259 { 260 String path = stok.nextToken(); 261 if(path.length()==1 && stok.hasMoreTokens()) 262 { 263 path+=":"+stok.nextToken(); 265 } 266 267 if(result == null) 268 { 269 result = new Vector (); 270 } 271 272 result.add(path); 273 } 274 return result; 275 } 276 277 278 296 public void execute(PropertyGetter props) throws IOException { 297 298 299 Reader reader = new FileReader (templateFile); 300 Writer writer = new FileWriter (customFile); 301 BufferedReader br = new BufferedReader (reader); 302 PrintWriter pw = new PrintWriter (writer); 303 304 String line; 305 306 while((line=br.readLine()) != null) 307 { 308 String trimmedLine = line.trim(); 309 if ( trimmedLine.equals("") 310 ||trimmedLine.startsWith("#") ) 311 { 312 pw.println(line); 313 } 314 else 315 { 316 String resultLine = createResultLine(line, props); 317 pw.println(resultLine); 318 } 319 } 320 pw.close(); 321 br.close(); 322 323 } 324 325 345 private String createResultLine(String line, PropertyGetter props) 346 { 347 String propertyName = null; 348 String templateValue = null; 349 String resultLine; 350 int index = line.indexOf("="); 351 if(index >=0) 352 { 353 propertyName = line.substring(0,index).trim(); 354 templateValue = line.substring(index+1).trim(); 355 int beginOfValue = line.indexOf(templateValue,index+1); 356 resultLine = (beginOfValue == -1) ? 357 line : line.substring(0,beginOfValue); 358 } 359 else 360 { 361 propertyName = line.trim(); 362 templateValue = ""; 363 int endOfLine = line.indexOf(propertyName)+propertyName.length(); 364 resultLine = ((endOfLine == -1) ? line : line.substring(0,endOfLine)) + " = "; 365 } 366 367 String newValue = (String ) userProperties.get(propertyName); 368 369 if(newValue == null) 370 { 371 newValue = (String ) props.getProperty(propertyName,templateValue); 372 if(newValue == null) 373 { 374 newValue = ""; 375 } 376 } 377 else if(newValue.equalsIgnoreCase("**generated**")) 378 { 379 String dbtype = (String )props.getProperty("scarab.database.type","hypersonic"); 380 if(dbtype.equals("")) 381 { 382 dbtype="hypersonic"; 383 } 384 newValue = "${"+propertyName+"."+dbtype+"}"; 385 } 386 387 if ( newValue.equals(templateValue)) 388 { 389 resultLine = line; 390 } 391 else 392 { 393 resultLine += newValue; 394 } 395 return resultLine; 396 } 397 } 398 | Popular Tags |