Ignore:
Timestamp:
2010-10-15T20:33:43+13:00 (14 years ago)
Author:
ak19
Message:

More changes to making collectdir movable: 1. When not working with a remote GS or server.exe: if the collecthome set in GLI is not the same as the one in gsdlsite.cfg used by the apache web server, then a dialog pops up on exiting GLI allowing the user to set the collecthome value for both GLI (in the user's GLI config.xml) and the server (in gsdlsite.cfg). 2. Collecthome line in gsdlsite.cfg is removed if it is the default GS collect folder. (Just as a recent commit stores an empty string for the open_collection element in the GLI config.xml file if no collection is left open on exiting GLI and the collect folder is the default GS collect directory.) 3. Some bug fixes.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/trunk/gli/src/org/greenstone/gatherer/util/Utility.java

    r22911 r23143  
    461461
    462462   
    463     /** Write out a property line--a (property, value) pair--to the gsdl(3)site.cfg file.
     463  /** Write out a property line--a (property, value) pair--to the gsdl(3)site.cfg file.
    464464     * If the file already contains the line as-is, it is not re-written.   
    465465     * If the file doesn't contain the line, it is appended.
    466466     * If the file contained a different value for the property, the line is corrected
    467467     * and the file is written out.
     468     * If the propertyValue parameter is null, the property line is removed from the file.
    468469     * Not using the Properties class, as we want to keep the file's contents in the
    469470     * 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    {   
    472476    File propFile = new File(filename);
     477    String oldValue = "";
    473478    if(!propFile.exists()) {
    474479        System.err.println("*** Unable to update property " + propertyName + " in file "
    475480                + filename + "to\n" + propertyValue + ". File does not exist.\n");
    476         return;
     481        return oldValue;
    477482    }   
    478483    BufferedReader fin = null;
    479484    BufferedWriter fout = null;
    480485    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    }
    482490    boolean found = false;
    483491    try {
     
    485493        String line = "";
    486494        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
    490519            fin.close();
    491520            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           
    519535        } // else the file is fine
    520536    } catch(IOException e) {
     
    538554       
    539555    }
    540    
    541     }
    542    
     556        return oldValue;
     557    }
    543558}
Note: See TracChangeset for help on using the changeset viewer.