1 16 package org.apache.jetspeed.util; 17 18 import java.io.File ; 19 import java.io.BufferedReader ; 20 import java.io.FileNotFoundException ; 21 import java.io.FileOutputStream ; 22 import java.io.FileReader ; 23 import java.io.IOException ; 24 25 import java.util.ArrayList ; 26 import java.util.HashMap ; 27 import java.util.StringTokenizer ; 28 29 37 public class OverwriteProperties { 38 39 protected File baseProperties; 40 41 protected File properties; 42 43 protected File includeRoot; 44 45 46 public boolean verbose = false; 47 48 49 protected ArrayList baseArray = new ArrayList (1024); 50 51 protected ArrayList removeArray = new ArrayList (128); 52 53 protected HashMap baseMap = new HashMap (); 54 55 protected String lineSeparator = System.getProperty("line.separator", "\r\n"); 56 57 58 63 public void setBaseProperties(File baseProperties) 64 { 65 this.baseProperties = baseProperties; 66 } 67 68 69 74 public void setProperties(File properties) 75 { 76 this.properties = properties; 77 } 78 79 80 85 public void setIncludeRoot(File includeRoot) 86 { 87 this.includeRoot = includeRoot; 88 } 89 90 91 96 public void setVerbose(boolean verbose) 97 { 98 this.verbose = verbose; 99 } 100 101 102 107 public File getBaseProperties() 108 { 109 return baseProperties; 110 } 111 112 113 118 public File getProperties() 119 { 120 return properties; 121 } 122 123 124 129 public File getIncludeRoot() 130 { 131 return includeRoot; 132 } 133 134 135 140 public boolean getVerbose() 141 { 142 return verbose; 143 } 144 145 146 152 public static void main(String [] args) 153 throws Exception 154 { 155 OverwriteProperties overwriteProperties = new OverwriteProperties(); 156 157 try 158 { 159 if (args.length < 3) 160 { 161 System.out.println("Usage: java OverwriteProperties c:/temp/File1.props c:/temp/File2.props c:/include-root/"); 162 System.out.println("Usage: File1 will be modified, new parameters from File 2 will be added,"); 163 System.out.println("Usage: and same parameters will be updated. The include-root is where include files are found."); 164 throw new Exception ("Incorrect number of arguments supplied"); 165 } 166 overwriteProperties.setBaseProperties(new File (args[0])); 167 overwriteProperties.setProperties(new File (args[1])); 168 overwriteProperties.setIncludeRoot(new File (args[2])); 169 170 overwriteProperties.execute(); 171 172 } 173 catch (FileNotFoundException ex) 174 { 175 System.err.println(ex.getMessage()); 176 } 177 catch (IOException ex) 178 { 179 System.err.println(ex.getMessage()); 180 } 181 catch (SecurityException ex) 182 { 183 System.err.println(ex.getMessage()); 184 } 185 } 186 187 188 189 public void execute() throws FileNotFoundException , IOException , SecurityException 190 { 191 192 if (verbose) 193 { 194 System.out.println("Merging into file " + getBaseProperties() + " file " + getProperties()); 195 } 196 197 if (!getBaseProperties().exists()) 198 { 199 throw new FileNotFoundException ("Could not find file:" + getBaseProperties()); 200 } 201 202 if (!getProperties().exists()) 203 { 204 throw new FileNotFoundException ("Could not find file:" + getProperties()); 205 } 206 207 if (!getIncludeRoot().exists() || !getIncludeRoot().isDirectory()) 208 { 209 throw new FileNotFoundException ("Could not find directory:" + getIncludeRoot()); 210 } 211 212 BufferedReader reader = new BufferedReader (new FileReader (baseProperties)); 213 int index = 0; 214 String key = null; 215 String line = null; 216 while ((line = reader.readLine()) != null) 217 { 218 StringTokenizer tokenizer = new StringTokenizer (line, "="); 219 baseArray.add(index, line); 220 if (verbose) 221 { 222 System.out.println("While reading baseArray[" + index + "] = " + line); 223 } 224 if (tokenizer.countTokens() >= 1 225 && !line.startsWith("#") 226 && !line.startsWith("include") 227 && !line.startsWith("module.packages")) 228 { 229 key = tokenizer.nextToken().trim(); 230 if (key != null && key.length() > 0) 231 { 232 baseMap.put(key, new Integer (index)); 233 if (verbose) 234 { 235 System.out.println("baseMap[" + key + "," + index + "]"); 236 } 237 } 238 } 239 index++; 240 } 241 reader.close(); 242 if (verbose) 243 { 244 System.out.println("\nOverwrite with Delta\n"); 245 } 246 247 readProperties(properties, index); 248 249 boolean flags[] = removeProperties(); 250 251 baseArray.trimToSize(); 252 writeToFile(flags); 253 254 255 } 256 257 258 265 public void writeToFile(boolean[] flags) 266 throws FileNotFoundException , IOException 267 { 268 FileOutputStream writer = new FileOutputStream (baseProperties); 269 writer.flush(); 270 for (int i = 0; i < baseArray.size(); i++) 271 { 272 if (true == flags[i]) 273 { 274 if (verbose) 275 { 276 System.out.println("Skipping property[" + i + "] = " + baseArray.get(i)); 277 } 278 continue; 279 } 280 if (verbose) 281 { 282 System.out.println("Writing property[" + i + "] = " + baseArray.get(i)); 283 } 284 writer.write(((String ) baseArray.get(i)).getBytes()); 285 writer.write(lineSeparator.getBytes()); 286 writer.flush(); 287 } 288 writer.close(); 289 290 } 291 292 293 298 public boolean[] removeProperties() 299 { 300 301 boolean flags[] = new boolean[baseArray.size()]; 302 303 for (int i = 0; i < baseArray.size(); i++) 304 { 305 flags[i] = false; 306 } 307 for (int ix = 0; ix < removeArray.size(); ix++) 308 { 309 String prefix = (String ) removeArray.get(ix); 310 for (int iy = 0; iy < baseArray.size(); iy++) 311 { 312 String line = (String ) baseArray.get(iy); 313 if (line.startsWith(prefix)) 314 { 315 flags[iy] = true; 316 if (verbose) 317 { 318 System.out.println("flagging removal of property: " + line); 319 } 320 } 321 } 322 } 323 return flags; 324 } 325 326 327 335 public void readProperties(File propFile, int index) 336 throws FileNotFoundException , IOException 337 { 338 BufferedReader reader = new BufferedReader (new FileReader (propFile)); 339 String key = null; 340 String line = null; 341 342 while ((line = reader.readLine()) != null) 343 { 344 StringTokenizer tokenizer = new StringTokenizer (line, "="); 345 346 int count = tokenizer.countTokens(); 347 if (count == 2 && line.startsWith("include")) 348 { 349 key = tokenizer.nextToken().trim(); 350 File includeFile = new File (includeRoot + tokenizer.nextToken().trim()); 351 if (verbose) 352 { 353 System.out.println("include File = " + includeFile); 354 } 355 readProperties(includeFile, index); 356 continue; 357 } 358 if (count >= 1 && line.startsWith("module.packages")) 359 { 360 baseArray.add(index, line); 361 if (verbose) 362 { 363 System.out.println("Adding module.package to baseArray[" + index + "] = " + line); 364 } 365 index++; 366 367 key = line.trim(); 368 if (baseMap.containsKey(key)) 369 { 370 int ix = ((Integer ) baseMap.get(key)).intValue(); 371 baseArray.set(ix, line); 372 if (verbose) 373 { 374 System.out.println("Resetting baseArray[" + ix + "] = " + line); 375 } 376 } 377 378 continue; 379 } 380 if (count >= 1 && line.startsWith("-")) 381 { 382 384 String prefix = line.trim().substring(1); 385 removeArray.add(prefix); 386 if (verbose) 387 { 388 System.out.println("Flagging for removal = " + line); 389 } 390 continue; 391 } 392 if (count >= 1 && !line.startsWith("#")) 393 { 394 key = tokenizer.nextToken().trim(); 395 if (key != null && key.length() > 0) 396 { 397 if (baseMap.containsKey(key)) 398 { 399 int ix = ((Integer ) baseMap.get(key)).intValue(); 400 baseArray.set(ix, line); 401 if (verbose) 402 { 403 System.out.println("Resetting baseArray[" + ix + "] = " + line); 404 } 405 } 406 else 407 { 408 baseArray.add(index, line); 409 if (verbose) 410 { 411 System.out.println("Adding new entry to baseArray[" + index + "] = " + line); 412 } 413 baseMap.put(key, new Integer (index)); 414 if (verbose) 415 { 416 System.out.println("baseMap[" + key + "," + index + "]"); 417 } 418 index++; 419 } 420 } 421 } 422 423 } 424 reader.close(); 425 426 } 427 428 } 429 430 431 432 433 | Popular Tags |