Ignore:
Timestamp:
2004-01-28T14:37:50+13:00 (20 years ago)
Author:
jmt12
Message:

File associations are now stored in the user specific config. Also, to make everyones life a little easier, the config has a version number which if no match is found, causes the current config to be backed-up, and a new default config created.

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

Legend:

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

    r6622 r6630  
    6868    /** The name of the general Gatherer configuration file. */
    6969    static final private String CONFIG_XML = "config.xml";
     70
     71    static final private String CURRENT_CONFIGURATION_VERSION = "2.4";
    7072    /** 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). */
    7173    static final private String GATHERER_CONFIG = "GathererConfig";
     
    189191
    190192    // Try to reload the configuration. The first place we look is the user specific location
    191     File user_config_xml = new File(Utility.getGLIUserFolder(), CONFIG_XML);       
    192     if(user_config_xml != null && user_config_xml.exists()) {
    193         general_config = Utility.parse(user_config_xml.getAbsolutePath(), false);
     193    File config_xml = new File(Utility.getGLIUserFolder(), CONFIG_XML);     
     194    if(config_xml != null && config_xml.exists()) {
     195        general_config = Utility.parse(config_xml.getAbsolutePath(), false);
    194196    }
    195197
    196198    // We then try within the gli directory itself
    197199    if(general_config == null) {
    198         File config_xml = new File(CONFIG_XML);
     200        config_xml = new File(CONFIG_XML);
    199201        if(config_xml.exists()) {
    200202        general_config = Utility.parse(CONFIG_XML, false);
    201203        }
     204    }
     205
     206    // Check the version of the config we have loaded, and if it isn't recent enough, backup the old version, and then create a new config.
     207    if(general_config != null) {
     208        Element configuration_element = general_config.getDocumentElement();
     209        String configuration_version = configuration_element.getAttribute(StaticStrings.VERSION_ATTRIBUTE);
     210        if(!configuration_version.equals(CURRENT_CONFIGURATION_VERSION)) {
     211        JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("Configuration.Obsolete_Config"), Dictionary.get("Configuration.Obsolete_Config_Title"), JOptionPane.ERROR_MESSAGE);
     212        general_config = null;
     213        configuration_element = null;
     214        File backup = new File(config_xml.getAbsolutePath() + ".bak");
     215        config_xml.renameTo(backup);
     216        backup = null;
     217        }
     218        else {
     219        configuration_element = null;
     220        }
     221        configuration_version = null;
    202222    }
    203223
     
    422442    String email = getString(GENERAL_EMAIL_SETTING, true);
    423443    return (email.length() > 0 ? email : null);
     444    }
     445
     446    public Element getFileAssociations() {
     447    NodeList file_association_elements = general_config.getDocumentElement().getElementsByTagName(StaticStrings.ASSOCIATIONS_ELEMENT);
     448    return (Element) file_association_elements.item(0);
    424449    }
    425450
  • trunk/gli/src/org/greenstone/gatherer/file/FileAssociationManager.java

    r6622 r6630  
    4141    extends AbstractTableModel {
    4242    static final public String FILENAME_ARG = "%1";
    43     static final private String DATA_FILENAME = "associations.xml";
    44     static final private String ENTRY_ELEMENT = "Entry";
    4543    static final private String ESCAPE = "\\\\"; // '\'
    4644    static final private String ESCAPED_ESCAPE = "\\\\\\\\"; // '\\'
    47     static final private String EXTENSION_ATTRIBUTE = "extension";
    48     private Document document;
     45    private Element associations_element;
    4946    private File data_file;   
    5047
    5148    public FileAssociationManager() {
    52     // If a associations.xml is available in the GLI install directory load it.
    53     data_file = new File(Utility.BASE_DIR + DATA_FILENAME);
    54     if(data_file.exists()) {
    55         document = Utility.parse(data_file, true);
    56     }
    57     // Load the default associations xml data file. This can be done using the classloader.
    58     else {
    59         document = Utility.parse(Utility.XML_DIRECTORY + DATA_FILENAME, true);
    60     }
     49    // Retrieve the associations_element from the config
     50    associations_element = Gatherer.config.getFileAssociations();
    6151    // Initialize the associations. This involves looking through all current associations searching for those with a command of "".
    62     if(document != null) {
    63         NodeList entries = (document.getDocumentElement()).getElementsByTagName(ENTRY_ELEMENT);
     52    if(associations_element != null) {
     53        NodeList entries = associations_element.getElementsByTagName(StaticStrings.ENTRY_ELEMENT);
    6454        for(int i = 0; i < entries.getLength(); i++) {
    6555        Element entry = (Element) entries.item(i);
    66         String extension = entry.getAttribute(EXTENSION_ATTRIBUTE);
     56        String extension = entry.getAttribute(StaticStrings.EXTENSION_ATTRIBUTE);
    6757        String command = MSMUtils.getValue(entry);
    6858        // If we encounter a command of ""...
     
    139129    // pretend we are trying to open an html file
    140130    // Mmmoooo. - jmt12
    141     /*
    142     String extension = "html";
    143     Element entry = getCommand(extension);
    144     if (entry != null) {
    145         command = MSMUtils.getValue(entry);
    146     }
    147    
    148     if(command == null || command.length() == 0) {
    149         FileAssociationDialog dialog = new FileAssociationDialog(this);
    150         command = dialog.display(extension);
    151         dialog = null;
    152     }
    153     if(command != null) {
    154         // If no previous entry existed create one.
    155         if(entry == null) {
    156         entry = document.createElement(ENTRY_ELEMENT);
    157         entry.setAttribute(EXTENSION_ATTRIBUTE, extension);
    158         document.getDocumentElement().appendChild(entry);
    159         }
    160         // Replace the text in this node. Remember to replace the dummy filename with %1
    161         MSMUtils.setValue(entry, command.replaceAll("temp.html", FILENAME_ARG));
    162     }
    163     */
     131
    164132    // if we haven't got a command by now, we'll never get one
    165133    if (command == null) {
     
    234202            // If no previous entry existed create one.
    235203            if(entry == null) {
    236             entry = document.createElement(ENTRY_ELEMENT);
    237             entry.setAttribute(EXTENSION_ATTRIBUTE, extension);
    238             document.getDocumentElement().appendChild(entry);
     204            entry = associations_element.getOwnerDocument().createElement(StaticStrings.ENTRY_ELEMENT);
     205            entry.setAttribute(StaticStrings.EXTENSION_ATTRIBUTE, extension);
     206            associations_element.appendChild(entry);
    239207            }
    240208            // Replace the text in this node. Remember to replace the dummy filename with %1 - I dont think the filename will ever be in the comand now
     
    258226
    259227    public Element getCommand(String target_extension) {
    260     NodeList entries = document.getDocumentElement().getElementsByTagName(ENTRY_ELEMENT);
     228    NodeList entries = associations_element.getElementsByTagName(StaticStrings.ENTRY_ELEMENT);
    261229    for(int i = 0; i < entries.getLength(); i++) {
    262230        Element entry = (Element) entries.item(i);
    263         String extension = entry.getAttribute(EXTENSION_ATTRIBUTE);
     231        String extension = entry.getAttribute(StaticStrings.EXTENSION_ATTRIBUTE);
    264232        if(extension.equalsIgnoreCase(target_extension)) {
    265233        entries = null;
     
    283251
    284252    public String getExtension(int index) {
    285     NodeList entries = document.getDocumentElement().getElementsByTagName(ENTRY_ELEMENT);
     253    NodeList entries = associations_element.getElementsByTagName(StaticStrings.ENTRY_ELEMENT);
    286254    if(0 <= index && index < entries.getLength()) {
    287255        Element entry = (Element) entries.item(index);
    288         return entry.getAttribute(EXTENSION_ATTRIBUTE);
     256        return entry.getAttribute(StaticStrings.EXTENSION_ATTRIBUTE);
    289257    }
    290258    return "";
     
    292260
    293261    public int getRowCount() {
    294     NodeList entries = document.getDocumentElement().getElementsByTagName(ENTRY_ELEMENT);
    295     return entries.getLength();
     262    return size();
    296263    }
    297264
     
    307274
    308275    public void save() {
    309     Utility.export(document, data_file);
    310276    }
    311277
     
    316282    // If no previous entry existed create one.
    317283    if(entry == null && command != null) {
    318         entry = document.createElement(ENTRY_ELEMENT);
    319         entry.setAttribute(EXTENSION_ATTRIBUTE, extension);
    320         document.getDocumentElement().appendChild(entry);
     284        entry = associations_element.getOwnerDocument().createElement(StaticStrings.ENTRY_ELEMENT);
     285        entry.setAttribute(StaticStrings.EXTENSION_ATTRIBUTE, extension);
     286        associations_element.appendChild(entry);
    321287    }
    322288
     
    327293    else {
    328294        // Remove the entry
    329         document.getDocumentElement().removeChild(entry);
     295        associations_element.removeChild(entry);
    330296    }
    331297    entry = null;
     
    334300
    335301    public int size() {
    336     NodeList entries = document.getDocumentElement().getElementsByTagName(ENTRY_ELEMENT);
     302    NodeList entries = associations_element.getElementsByTagName(StaticStrings.ENTRY_ELEMENT);
    337303    return entries.getLength();
    338304    }
Note: See TracChangeset for help on using the changeset viewer.