Changeset 9869


Ignore:
Timestamp:
2005-05-13T15:57:47+12:00 (19 years ago)
Author:
mdewsnip
Message:

Dealing with the user config.xml files is now much smarter. Instead of throwing away all the user preferences every time the version of GLI is updated, they are now intelligently merged into the updated (core) config.xml file.

Location:
trunk/gli/src/org/greenstone/gatherer
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/gli/src/org/greenstone/gatherer/Configuration.java

    r9437 r9869  
    7171    static public String GS3_CONFIG_XML = "config3.xml";
    7272
    73     static final private String CURRENT_CONFIGURATION_VERSION = "2.60";
    7473    /** The name of the root element of the subtree containing gatherer configuration options. This is required as the document itself may contain several other subtrees of settings (such as in the case of a '.col' file). */
    7574    static final private String GATHERER_CONFIG = "GathererConfig";
     
    9796    static public Configuration self = null;
    9897
    99     static public boolean just_created_new_config_file = false;
     98    static public boolean just_updated_config_xml_file = false;
    10099    /** The path to the Greenstone Suite installation directory. */
    101100    static public String gsdl_path = "";
     
    150149        config_xml_file_name = GS3_CONFIG_XML;
    151150    }
     151
     152    // If the existing user config.xml file isn't recent enough, backup the old version and update it
    152153    File config_xml_file = new File(Utility.getGLIUserFolder(), config_xml_file_name);
    153154    if (config_xml_file != null && config_xml_file.exists()) {
    154         general_config = Utility.parse(config_xml_file.getAbsolutePath(), false);
    155     }
    156 
    157     // If the version of the config we've loaded isn't recent enough, backup the old version and create a new one
    158     just_created_new_config_file = false;
    159     if (general_config != null) {
    160         Element configuration_element = general_config.getDocumentElement();
    161         String configuration_version = configuration_element.getAttribute(StaticStrings.VERSION_ATTRIBUTE);
    162         if (!configuration_version.equals(CURRENT_CONFIGURATION_VERSION)) {
    163         // !!! JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("Configuration.Obsolete_Config"), Dictionary.get("Configuration.Obsolete_Config_Title"), JOptionPane.ERROR_MESSAGE);
    164         File backup = new File(config_xml_file.getAbsolutePath() + ".bak");
    165         config_xml_file.renameTo(backup);
    166         general_config = null;
    167         }
     155        just_updated_config_xml_file = updateUserConfigXMLFileIfNecessary(config_xml_file);
     156        general_config = XMLTools.parseXMLFile(config_xml_file);
    168157    }
    169158
     
    171160    if (general_config == null) {
    172161        general_config = Utility.parse(TEMPLATE_CONFIG_XML, true);
    173         just_created_new_config_file = true;
     162        just_updated_config_xml_file = true;
    174163    }
    175164
     
    210199        }
    211200    }
     201    }
     202
     203
     204    static private boolean updateUserConfigXMLFileIfNecessary(File config_xml_file)
     205    {
     206    // Check if the configuration file actually needs updating by looking at the version numbers
     207    Document new_document = Utility.parse(TEMPLATE_CONFIG_XML, true);
     208    Document old_document = XMLTools.parseXMLFile(config_xml_file);
     209
     210    String new_version = new_document.getDocumentElement().getAttribute(StaticStrings.VERSION_ATTRIBUTE);
     211    String old_version = old_document.getDocumentElement().getAttribute(StaticStrings.VERSION_ATTRIBUTE);
     212    if (new_version.compareTo(old_version) <= 0) {
     213        // Don't need to update file
     214        return false;
     215    }
     216
     217    System.err.println("Updating user config.xml from version " + old_version + " to version " + new_version + "...");
     218
     219    // Build up the new user config.xml file from the template config.xml with the user's preferences added
     220    // Make a backup of the old config.xml file first
     221    config_xml_file.renameTo(new File(config_xml_file.getAbsolutePath() + ".old"));
     222
     223    // Check all of the Argument elements in the new config.xml file
     224    NodeList new_argument_elements_nodelist = new_document.getElementsByTagName("Argument");
     225    NodeList old_argument_elements_nodelist = old_document.getElementsByTagName("Argument");
     226    for (int i = 0; i < new_argument_elements_nodelist.getLength(); i++) {
     227        Element new_argument_element = (Element) new_argument_elements_nodelist.item(i);
     228        String new_argument_element_name = new_argument_element.getAttribute("name");
     229        String new_argument_element_value = XMLTools.getElementTextValue(new_argument_element);
     230
     231        // Did this Argument have a non-default value set?
     232        for (int j = old_argument_elements_nodelist.getLength() - 1; j >= 0; j--) {
     233        Element old_argument_element = (Element) old_argument_elements_nodelist.item(j);
     234        String old_argument_element_name = old_argument_element.getAttribute("name");
     235
     236        // Argument found
     237        if (old_argument_element_name.equals(new_argument_element_name)) {
     238            String old_argument_element_value = XMLTools.getElementTextValue(old_argument_element);
     239            if (!old_argument_element_value.equals(new_argument_element_value)) {
     240            XMLTools.setElementTextValue(new_argument_element, old_argument_element_value);
     241            }
     242
     243            // We don't care about this option any more
     244            old_argument_element.getParentNode().removeChild(old_argument_element);
     245            break;
     246        }
     247        }
     248    }
     249
     250    // Are there any old Argument elements left over? Keep these around for old time's sake
     251    old_argument_elements_nodelist = old_document.getElementsByTagName("Argument");
     252    if (old_argument_elements_nodelist.getLength() > 0) {
     253        NodeList new_gathererconfig_elements_nodelist = new_document.getElementsByTagName("GathererConfig");
     254        if (new_gathererconfig_elements_nodelist.getLength() > 0) {
     255        Element new_gathererconfig_element = (Element) new_gathererconfig_elements_nodelist.item(0);
     256        new_gathererconfig_element.appendChild(new_document.createComment(" Legacy options "));
     257
     258        // Add the legacy Arguments to the GathererConfig element
     259        for (int i = 0; i < old_argument_elements_nodelist.getLength(); i++) {
     260            Element old_argument_element = (Element) old_argument_elements_nodelist.item(i);
     261            new_gathererconfig_element.appendChild(new_document.importNode(old_argument_element, true));
     262        }
     263        }
     264    }
     265
     266    // Keep all the Mapping elements
     267    NodeList new_directorymappings_elements_nodelist = new_document.getElementsByTagName("DirectoryMappings");
     268    if (new_directorymappings_elements_nodelist.getLength() > 0) {
     269        Element new_directorymappings_element = (Element) new_directorymappings_elements_nodelist.item(0);
     270
     271        NodeList old_mapping_elements_nodelist = old_document.getElementsByTagName("Mapping");
     272        for (int i = 0; i < old_mapping_elements_nodelist.getLength(); i++) {
     273        Element old_mapping_element = (Element) old_mapping_elements_nodelist.item(i);
     274        new_directorymappings_element.appendChild(new_document.importNode(old_mapping_element, true));
     275        }
     276    }
     277
     278    // Check all of the Associations elements in the new config.xml file
     279    NodeList new_entry_elements_nodelist = new_document.getElementsByTagName("Entry");
     280    NodeList old_entry_elements_nodelist = old_document.getElementsByTagName("Entry");
     281    for (int i = 0; i < new_entry_elements_nodelist.getLength(); i++) {
     282        Element new_entry_element = (Element) new_entry_elements_nodelist.item(i);
     283        String new_entry_element_name = new_entry_element.getAttribute("extension");
     284        String new_entry_element_value = XMLTools.getElementTextValue(new_entry_element);
     285
     286        // Did this Entry have a non-default value set?
     287        for (int j = old_entry_elements_nodelist.getLength() - 1; j >= 0; j--) {
     288        Element old_entry_element = (Element) old_entry_elements_nodelist.item(j);
     289        String old_entry_element_name = old_entry_element.getAttribute("extension");
     290
     291        // Entry found
     292        if (old_entry_element_name.equals(new_entry_element_name)) {
     293            String old_entry_element_value = XMLTools.getElementTextValue(old_entry_element);
     294            if (!old_entry_element_value.equals(new_entry_element_value)) {
     295            XMLTools.setElementTextValue(new_entry_element, old_entry_element_value);
     296            }
     297
     298            // We don't care about this option any more
     299            old_entry_element.getParentNode().removeChild(old_entry_element);
     300            break;
     301        }
     302        }
     303    }
     304
     305    // Are there any old Entry elements left over? We want to keep these
     306    old_entry_elements_nodelist = old_document.getElementsByTagName("Entry");
     307    if (old_entry_elements_nodelist.getLength() > 0) {
     308        NodeList new_associations_elements_nodelist = new_document.getElementsByTagName("Associations");
     309        if (new_associations_elements_nodelist.getLength() > 0) {
     310        Element new_associations_element = (Element) new_associations_elements_nodelist.item(0);
     311
     312        // Add the legacy Entries to the Associations element
     313        for (int i = 0; i < old_entry_elements_nodelist.getLength(); i++) {
     314            Element old_entry_element = (Element) old_entry_elements_nodelist.item(i);
     315            new_associations_element.appendChild(new_document.importNode(old_entry_element, true));
     316        }
     317        }
     318    }
     319
     320    // Write out the updated config.xml file
     321    XMLTools.writeXMLFile(config_xml_file, new_document);
     322    return true;
    212323    }
    213324
  • trunk/gli/src/org/greenstone/gatherer/Gatherer.java

    r9865 r9869  
    146146        // Load GLI config file
    147147        new Configuration(gsdl_path, gsdl3_path, site_name);
    148         if (Configuration.just_created_new_config_file) {
     148        if (Configuration.just_updated_config_xml_file) {
    149149        // Delete the plugins.dat and classifiers.dat files
    150150        PluginManager.clearPluginCache();
Note: See TracChangeset for help on using the changeset viewer.