- Timestamp:
- 2010-10-15T20:33:43+13:00 (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
main/trunk/gli/src/org/greenstone/gatherer/util/Utility.java
r22911 r23143 461 461 462 462 463 463 /** Write out a property line--a (property, value) pair--to the gsdl(3)site.cfg file. 464 464 * If the file already contains the line as-is, it is not re-written. 465 465 * If the file doesn't contain the line, it is appended. 466 466 * If the file contained a different value for the property, the line is corrected 467 467 * and the file is written out. 468 * If the propertyValue parameter is null, the property line is removed from the file. 468 469 * Not using the Properties class, as we want to keep the file's contents in the 469 470 * same order and preserve all the comments in as they're meant to help the user. 470 */ 471 public static void updatePropertyConfigFile(String filename, String propertyName, String propertyValue) { 471 * Return the old value for the property, if it existed, else "". 472 */ 473 public static String updatePropertyConfigFile( 474 String filename, String propertyName, String propertyValue) 475 { 472 476 File propFile = new File(filename); 477 String oldValue = ""; 473 478 if(!propFile.exists()) { 474 479 System.err.println("*** Unable to update property " + propertyName + " in file " 475 480 + filename + "to\n" + propertyValue + ". File does not exist.\n"); 476 return ;481 return oldValue; 477 482 } 478 483 BufferedReader fin = null; 479 484 BufferedWriter fout = null; 480 485 StringBuffer contents = new StringBuffer(); 481 String insertLine = propertyName+"\t"+propertyValue+"\n"; // new line after every propertyLine 486 String insertLine = null; 487 if(propertyValue != null) { 488 insertLine = propertyName+"\t"+propertyValue+"\n"; // new line after every propertyLine 489 } 482 490 boolean found = false; 483 491 try { … … 485 493 String line = ""; 486 494 while((line = fin.readLine()) != null) { 487 if(line.startsWith(propertyName)) { // won't match comment 488 found = true; 489 if(line.equals(insertLine)) { // file is already correct, nothing to do 495 line = line.trim(); // remove any preceding (surrounding) whitespace 496 if(line.startsWith(propertyName)) { // won't match comment 497 found = true; 498 // store the previous value for the property 499 oldValue = line; 500 oldValue = oldValue.substring(propertyName.length()); 501 oldValue = oldValue.trim(); 502 503 if(propertyValue != null) { // line should be removed if propertyValue == null 504 if(line.equals(insertLine)) { // file is already correct, nothing to do 505 fin.close(); 506 fin = null; 507 break; 508 } else { 509 contents.append(insertLine); 510 } 511 } 512 } else { // any other line 513 contents.append(line); 514 contents.append("\n"); // ensures the required new line at end of file 515 } 516 } 517 518 if(fin != null) { // need to write something out to the file 490 519 fin.close(); 491 520 fin = null; 492 break; 493 } else { 494 contents.append(insertLine); 495 } 496 497 } else { // any other line 498 contents.append(line); 499 contents.append("\n"); // ensures the required new line at end of file 500 } 501 } 502 503 if(fin != null) { // need to write something out to the file 504 fin.close(); 505 fin = null; 506 507 // if collecthome wasn't already specified in the file, append it 508 if(!found) { 509 fout = new BufferedWriter(new FileWriter(filename, true)); // append mode 510 fout.write(insertLine, 0, insertLine.length()); 511 } else { 512 513 fout = new BufferedWriter(new FileWriter(filename)); // hopefully this will overwrite 514 fout.write(contents.toString(), 0, contents.length()); 515 } 516 517 fout.close(); 518 fout = null; 521 522 // if collecthome/property wasn't already specified in the file, append it 523 // but only if we have a value to write out to the file 524 if(!found && propertyValue != null) { 525 fout = new BufferedWriter(new FileWriter(filename, true)); // append mode 526 fout.write(insertLine, 0, insertLine.length()); 527 } else { 528 fout = new BufferedWriter(new FileWriter(filename)); // hopefully this will overwrite 529 fout.write(contents.toString(), 0, contents.length()); 530 } 531 532 fout.close(); 533 fout = null; 534 519 535 } // else the file is fine 520 536 } catch(IOException e) { … … 538 554 539 555 } 540 541 } 542 556 return oldValue; 557 } 543 558 }
Note:
See TracChangeset
for help on using the changeset viewer.