Ignore:
Timestamp:
2003-07-15T13:55:22+12:00 (21 years ago)
Author:
jmt12
Message:

Major CDM rewrite so it uses DOM.

File:
1 edited

Legend:

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

    r4675 r4932  
    66 * University of Waikato, New Zealand.
    77 *
    8  * <BR><BR>
    9  *
    108 * Author: John Thompson, Greenstone Digital Library, University of Waikato
    119 *
    12  * <BR><BR>
    13  *
    1410 * Copyright (C) 1999 New Zealand Digital Library Project
    15  *
    16  * <BR><BR>
    1711 *
    1812 * This program is free software; you can redistribute it and/or modify
     
    2115 * (at your option) any later version.
    2216 *
    23  * <BR><BR>
    24  *
    2517 * This program is distributed in the hope that it will be useful,
    2618 * but WITHOUT ANY WARRANTY; without even the implied warranty of
    2719 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    2820 * GNU General Public License for more details.
    29  *
    30  * <BR><BR>
    3121 *
    3222 * You should have received a copy of the GNU General Public License
     
    3525 *########################################################################
    3626 */
    37 
    38  
    39 
    40 
    41 
    42 
    43 /* GPL_HEADER */
    4427package org.greenstone.gatherer.cdm;
    45 /**************************************************************************************
    46  * Title:        Gatherer
    47  * Description:  The Gatherer: a tool for gathering and enriching a digital collection.
    48  * Company:      The University of Waikato
    49  * Written:      02/05/02
    50  * Revised:      16/08/02 Optimized and added Destructor.
    51  **************************************************************************************/
    52 import java.awt.BorderLayout;
    53 import java.awt.Rectangle;
    54 import java.io.BufferedReader;
    55 import java.io.File;
    56 import java.io.FileReader;
    57 import java.io.FileOutputStream;
    58 import java.lang.Exception;
    59 import java.lang.String;
    60 import java.lang.StringBuffer;
    61 import java.util.ArrayList;
    62 import java.util.StringTokenizer;
    63 import javax.swing.JPanel;
     28import java.awt.*;
     29import java.awt.event.*;
     30import java.io.*;
     31import javax.swing.*;
     32import javax.swing.event.*;
    6433import org.greenstone.gatherer.Gatherer;
    6534import org.greenstone.gatherer.cdm.ClassifierManager;
     35import org.greenstone.gatherer.cdm.CollectionConfiguration;
    6636import org.greenstone.gatherer.cdm.CollectionMetaManager;
    6737import org.greenstone.gatherer.cdm.FormatManager;
    68 import org.greenstone.gatherer.cdm.GUI;
    69 import org.greenstone.gatherer.cdm.Index;
     38import org.greenstone.gatherer.cdm.GeneralManager;
    7039import org.greenstone.gatherer.cdm.IndexManager;
    71 import org.greenstone.gatherer.cdm.MetadataSetManager;
     40import org.greenstone.gatherer.cdm.MetadataSetView;
    7241import org.greenstone.gatherer.cdm.PlugInManager;
    7342import org.greenstone.gatherer.cdm.SubcollectionManager;
    74 import org.greenstone.gatherer.util.EmailAddress;
     43import org.greenstone.gatherer.cdm.TranslationView;
    7544/** This manager provides access to submanagers, which in turn provide tools for the designing of Greenstone collections via the information stored in etc/collect.cfg. This class acts as a hub for the managers that handle specific parts of the configuration such as classifiers, format strings and language settings.
    7645 * @author John Thompson, Greenstone Digital Library, University of Waikato
    77  * @version 2.3
     46 * @version 2.3d
    7847 */
    79 // ####################################################################################
    80 // Optimization                          Saving
    81 // ####################################################################################
    82 // Vector -> ArrayList                   + Processor
    83 // String -> StringBuffer                + Memory, + Processor
    84 // ####################################################################################
    8548public class CollectionDesignManager {
    86     /** Whether this collection is to be made public or not. */
    87     public boolean public_col = false;
    88     /** Whether this collection is a beta version or not. */
    89     public boolean beta = true;
     49    /** This listener listens for any event on any of the components in any of the sub-views, and marks the collection as needing saving if any change occurs. */
     50    static public CDMChangeListener change_listener;
    9051    /** A list of classifiers to use at build time. */
    91     public ClassifierManager classifiers = null;
     52    static public ClassifierManager classifier_manager;
     53    /** The CollectionConfiguration object on which this CDM will be based. */
     54    static public CollectionConfiguration collect_config;
    9255    /** A manager of collection level metadata. */
    93     public CollectionMetaManager collectionmetadatum = null;
    94     /** E-mail address of the collection's creator. */
    95     public EmailAddress creator = null;
    96     /** E-mail address of the collection's maintainer. */
    97     public EmailAddress maintainer = null;
    98     /** The collection configuration file. */
    99     public File in_file = null;
     56    static public CollectionMetaManager collectionmeta_manager;
    10057    /** A list of formating strings to use at build time. */
    101     public FormatManager formats = null;
     58    static public FormatManager format_manager;
    10259    /** The manager in charge of displaying this manager and the controls for other managers. */
    103     public GUI gui = null;
     60    static public GeneralManager general_manager;
    10461    /** List of indexes to be built, and the default index. */
    105     public IndexManager indexes = null;
     62    static public IndexManager index_manager;
    10663    /** Contains instructions dealing with the collection language. */
    107     public LanguageManager languages = null;
     64    static public LanguageManager language_manager;
    10865    /** A simple manager for the visual review of metadata sets. */
    109     public MetadataSetManager metadatasets = null;
     66    static public MetadataSetView metadataset_view;
    11067    /** A list of plugins to use at build time. */
    111     public PlugInManager plugins = null;
     68    static public PlugInManager plugin_manager;
    11269    /** Contains: A list of subcollections, (defined on metadatadata), a list of which subcollection indexes to build and the default subcollection index. */
    113     public SubcollectionManager subcollections = null;
     70    static public SubcollectionManager subcollection_manager;
     71
     72    static public SubcollectionIndexManager subcollectionindex_manager;
    11473    /** A supercollection command allows a single search to be conducted across several collections. It is a very basic command and so avoids all the crazy model stuff that exists in most of the design managers. */
    115     public SuperCollectionManager superman; // Just cause I could ;p
     74    static public SuperCollectionManager supercollection_manager; // Just cause I could ;p
    11675    /** The text translation manager. */
    117     public TranslationManager transman;
    118     /** A list of whatever commands could not be parsed at all. */
    119     private ArrayList rest = null;
    120     /** A reference to the Gatherer. */
    121     private Gatherer gatherer = null;
    122     /** Constructor.
    123      * @param gatherer The <strong>Gatherer</strong> that created this class.
    124      * @see org.greenstone.gatherer.cdm.ClassifierManager
    125      * @see org.greenstone.gatherer.cdm.CollectionMetaManager
    126      * @see org.greenstone.gatherer.cdm.FormatManager
    127      * @see org.greenstone.gatherer.cdm.IndexManager
    128      * @see org.greenstone.gatherer.cdm.LanguageManager
    129      * @see org.greenstone.gatherer.cdm.MetadataSetManager
    130      * @see org.greenstone.gatherer.cdm.PlugInManager
    131      * @see org.greenstone.gatherer.cdm.SubcollectionManager
    132      */
    133     public CollectionDesignManager() {
    134     this.gatherer = Gatherer.self;
    135     this.classifiers = new ClassifierManager(gatherer, this);
    136     this.collectionmetadatum = new CollectionMetaManager(gatherer, this);
    137     this.formats = new FormatManager(gatherer, this);
    138     this.indexes = new IndexManager(gatherer, this);
    139     this.languages = new LanguageManager(gatherer, this);
    140     this.metadatasets = new MetadataSetManager(gatherer);
    141     this.plugins = new PlugInManager(gatherer, this);
    142     this.rest = new ArrayList();
    143     this.subcollections = new SubcollectionManager(gatherer, this);
    144     this.superman = new SuperCollectionManager();
    145     this.transman = new TranslationManager(this);
    146     }
    147     /** In order to prevent Components that have registered themselves wasting memory, this method invalidates each of the sub-managers controls, causing them to unregister listeners.
    148      * @see org.greenstone.gatherer.cdm.ClassifierManager
    149      * @see org.greenstone.gatherer.cdm.CollectionMetaManager
    150      * @see org.greenstone.gatherer.cdm.FormatManager
    151      * @see org.greenstone.gatherer.cdm.IndexManager
    152      * @see org.greenstone.gatherer.cdm.LanguageManager
    153      * @see org.greenstone.gatherer.cdm.MetadataSetManager
    154      * @see org.greenstone.gatherer.cdm.PlugInManager
    155      * @see org.greenstone.gatherer.cdm.SubcollectionManager
     76    static public TranslationView translation_view;
     77    /** Constructor. Loads a certain collection configuration file, which is parsed into a DOM. This model is then registered with the command information managers, each of whom knows how to, and provides controls to, alter certain commands.
     78     * @param collect_config_file the File representing a collection configuration file either in its text (G2) or xml (G3) form
     79     */
     80    public CollectionDesignManager(File collect_config_file) {
     81    Gatherer.println("Initializaing CollectionDesignModule.");
     82    change_listener = new CDMChangeListener();
     83    // Parse the collection configuration
     84    collect_config = new CollectionConfiguration(collect_config_file);
     85    if(Gatherer.debug != null) {
     86        collect_config.display();
     87    }
     88    // Create the command information managers, registering the config file with each as necessary
     89    language_manager = new LanguageManager(collect_config.getLanguages());
     90    collectionmeta_manager = new CollectionMetaManager();
     91    classifier_manager = new ClassifierManager();
     92    general_manager = new GeneralManager();
     93    index_manager = new IndexManager(collect_config.getIndexes());
     94    metadataset_view = new MetadataSetView();
     95    plugin_manager = new PlugInManager();
     96    plugin_manager.placeSeparator();
     97    subcollection_manager = new SubcollectionManager();
     98    subcollectionindex_manager = new SubcollectionIndexManager(collect_config.getSubIndexes());
     99    supercollection_manager = new SuperCollectionManager(collect_config.getSuperCollection());
     100    translation_view = new TranslationView();
     101    format_manager = new FormatManager(); // Parse formats at the very end, given that they depend upon several other managers to appear properly.
     102    Gatherer.println("CollectionDesignModule loaded.");
     103    }
     104    /** This method deconstructs each of the managers, causing them to dispose of their controls.
    156105     */
    157106    public void destroy() {
    158107    // Remove visual the component from its parent.
    159     if(gui.getParent() != null) {
    160         gui.getParent().remove(gui);
     108    if(general_manager.getParent() != null) {
     109        general_manager.getParent().remove(general_manager);
    161110    }
    162111    // Remove references from persistant listeners.
    163     if(classifiers != null && formats != null && gui != null && indexes != null && languages != null && metadatasets != null && plugins != null && subcollections != null) {
    164         classifiers.invalidateControls();
    165         formats.invalidateControls();
    166         gui.invalidateControls();
    167         indexes.invalidateControls();
    168         languages.invalidateControls();
    169         metadatasets.invalidateControls();
    170         plugins.invalidateControls();
    171         subcollections.invalidateControls();
    172         superman.invalidateControls();
    173     }
    174     // Null globals.
    175     classifiers = null;
    176     collectionmetadatum = null;
    177     creator = null;
    178     maintainer = null;
    179     in_file = null;
    180     formats = null;
    181     gui = null;
    182     indexes = null;
    183     languages = null;
    184     metadatasets = null;
    185     plugins = null;
    186     subcollections = null;
    187     superman = null;
    188     rest = null;
    189     gatherer = null;
     112    classifier_manager.destroy();
     113    classifier_manager = null;
     114    format_manager.destroy();
     115    format_manager = null;
     116    general_manager.destroy();
     117    general_manager = null;
     118    index_manager.destroy();
     119    index_manager = null;
     120    language_manager.destroy();
     121    language_manager = null;
     122    metadataset_view.destroy();
     123    metadataset_view = null;
     124    plugin_manager.destroy();
     125    plugin_manager = null;
     126    subcollection_manager.destroy();
     127    subcollection_manager = null;
     128    supercollection_manager.destroy();
     129    supercollection_manager = null;
     130    translation_view.destroy();
     131    translation_view = null;
    190132    }
    191133
    192134    /** Display the GUI interface for the CollectionDesignManager in the centre of the indicated panel.
    193      * @param target The <strong>JPanel</strong> you wish to display the GUI on.
    194      * @see org.greenstone.gatherer.cdm.GUI
     135     * @param target the JPanel you wish to display the gui on
    195136     */
    196137    public void display(JPanel target) {
    197     this.gui = new GUI(gatherer, this);
    198     target.add(gui, BorderLayout.CENTER);
     138    target.add(general_manager, BorderLayout.CENTER);
    199139    }
    200140    /** When the tab on the JTabbedPane that contains the GUI is selected, this method is called to ensure that the controls are all up to date, in terms of references to metadata etc.
    201      * @see org.greenstone.gatherer.cdm.GUI
    202141     */
    203142    public void gainFocus() {
    204     gui.updateUI();
    205     }
    206     /** Retrieve the current set of indexes as defined by the user configuration.
    207      * @return An <strong>ArrayList</strong> of indexes.
    208      * @see org.greenstone.gatherer.cdm.Index
    209      * @see org.greenstone.gatherer.cdm.IndexManager
    210      */
    211     public ArrayList getIndexes() {
    212     ArrayList result = new ArrayList();
    213     int size = indexes.size();
    214     for(int i = 0; i < size; i++) {
    215         result.add(indexes.getIndex(i));
    216     }
    217     return result;
     143    general_manager.gainFocus();
    218144    }
    219145    /** Retrieve the name of the collection configuration file which is being used as the source of the information in this object.
     
    221147     */
    222148    public String getFilename() {
    223     return in_file.getAbsolutePath();
    224     }
    225     /** Used to parse the given file as if it was a collection configuration file, passing any parsed commands to the relevant submanager. Currently ignores any comments, and marks any other lines as being unrecognizable if they can't be parsed.
    226      * @param filename The name of the file you wish to attempt to pass as a <strong>String</strong>.
     149    return collect_config.getFile().getAbsolutePath();
     150    }
     151
     152    /** Cause the current collection configuration to be written out to disk.
    227153     * @see org.greenstone.gatherer.cdm.ClassifierManager
    228154     * @see org.greenstone.gatherer.cdm.CollectionMetaManager
     
    235161     * @see org.greenstone.gatherer.util.EmailAddress
    236162     */
    237     private BufferedReader in;
    238     /** If one of the submanagers detects that it hasn't been provided enough arguments (such as format which always requires at least three) it can call this method to request another line of text from the collect.cfg. If the collect.cfg was not well formed this will cause some problems, but that can't be helped.
    239      * @param prefix the String we have so far
    240      * @return a String with a further line form the collect.cfg appended
    241      */
    242     public String parseMore(String prefix) {
    243     ///ystem.err.println("Command incomplete: " + prefix);
    244     try {
    245         String suffix = in.readLine();
    246         ///ystem.err.println("Adding: " + suffix);
    247         if(suffix != null) {
    248         prefix = prefix + " " + suffix;
    249         }
    250         suffix = null;
    251     }
    252     catch(Exception error) {
    253         error.printStackTrace();
    254     }
    255     return prefix;
    256     }
    257 
    258     public void parse(String filename) {
    259     try {
    260         in_file = new File(filename);
    261         FileReader in_reader = new FileReader(in_file);
    262         in = new BufferedReader(in_reader);
    263         String command = null;
    264         while((command = in.readLine()) != null) {
    265         if(command.length() > 0) {
    266             // Now we've finished parsing a command line, see what manager wants a piece of it.
    267             // We have to test the end of command for the special character '\'. If found, remove it and append the next line, then repeat.
    268             while(command.trim().endsWith("\\")) {
    269             String next_line = (in.readLine()).trim();
    270             if(next_line != null && next_line.length() > 0) {
    271                 command = command.substring(0, command.lastIndexOf("\\"));
    272                 command = command + next_line;
    273             }
    274             }
    275             // Now we've finished parsing a command line, see what manager wants a piece of it.
    276             boolean found = false;
    277             String command_lc = command.toLowerCase();
    278             if(command_lc.startsWith("creator")) {
    279             creator = new EmailAddress(gatherer, command);
    280             found = true;
    281             }
    282             if(command_lc.startsWith("maintainer")) {
    283             maintainer = new EmailAddress(gatherer, command);
    284             found = true;
    285             }
    286             if(command_lc.startsWith("public")) {
    287             if(command_lc.endsWith("true")) {
    288                 public_col = true;
    289             }
    290             else {
    291                 public_col = false;
    292             }
    293             found = true;
    294             }
    295             if(command_lc.startsWith("beta")) {
    296             if(command_lc.endsWith("false")) {
    297                 beta = false;
    298             }
    299             else {
    300                 beta = true;
    301             }
    302             found = true;
    303             }
    304             if(!found) {
    305             found = indexes.parse(command);
    306             }
    307             if(!found) {
    308             found = subcollections.parse(command, false);
    309             }
    310             if(!found) {
    311             found = languages.parse(command);
    312             }
    313             if(!found) {
    314             found = plugins.parse(command);
    315             }
    316             if(!found) {
    317             found = classifiers.parse(command);
    318             }
    319             if(!found) {
    320             found = formats.parse(command, false);
    321             }
    322             if(!found) {
    323             found = collectionmetadatum.parse(command, false);
    324             }
    325             if(!found) {
    326             found = superman.parse(command);
    327             }
    328             // Metadataset commands
    329             if(command_lc.startsWith("metadataset")) {
    330             // Nothing yet. Eventually used to import metadata.
    331             found = true;
    332             }
    333             // Comments we ignore.
    334             if(command_lc.startsWith("# these instructions are not recognized by the gatherer.")) {
    335             // Ignore
    336             found = true;
    337             }
    338             // We have been unable to parse this command, add it to rest.
    339             if(!found) {
    340             rest.add(command);
    341             }
    342         }
    343         }
    344         in.close();
    345         // Now attempt to finalize any commands that were not immediately parsed as they were waiting for unresolved references.
    346         subcollections.reparseUnresolved();
    347         classifiers.reparseUnresolved();
    348         formats.reparseUnresolved();
    349         collectionmetadatum.reparseUnresolved();
    350     }
    351     catch(Exception error) {
    352         error.printStackTrace();
    353     }
    354     }
    355     /** Cause the current definitions within the Collection Design Manager to be written back out to whatever collect.cfg it is based upon, taking care to change reference pointers into something more sensible within the text file.
    356      * @see org.greenstone.gatherer.cdm.ClassifierManager
    357      * @see org.greenstone.gatherer.cdm.CollectionMetaManager
    358      * @see org.greenstone.gatherer.cdm.FormatManager
    359      * @see org.greenstone.gatherer.cdm.IndexManager
    360      * @see org.greenstone.gatherer.cdm.LanguageManager
    361      * @see org.greenstone.gatherer.cdm.MetadataSetManager
    362      * @see org.greenstone.gatherer.cdm.PlugInManager
    363      * @see org.greenstone.gatherer.cdm.SubcollectionManager
    364      * @see org.greenstone.gatherer.util.EmailAddress
    365      */
    366163    public void save() {
    367     try {
    368         // If the file already exists (it should) rename it.
    369         if(in_file.exists()) {
    370         String filename = in_file.getAbsolutePath();
    371         File backup = new File(filename + "~");
    372         backup.deleteOnExit();
    373         if(!in_file.renameTo(backup)) {
    374             Gatherer.println("Error in CollectionDesignManager.parse(): FileRenamedException");
    375         }
    376         in_file = new File(filename); // Just in case we moved it.
    377         }
    378         FileOutputStream out = new FileOutputStream(in_file);
    379 
    380         StringBuffer text = new StringBuffer("");
    381         if(creator != null) {
    382         text.append(creator.toString());
    383         text.append("\n");
    384         }
    385         if(maintainer != null) {
    386         text.append(maintainer.toString());
    387         text.append("\n");
    388         }
    389         if(public_col) {
    390         text.append("public true\n");
    391         }
    392         else {
    393         text.append("public false\n");
    394         }
    395         if(beta) {
    396         text.append("beta true\n");
    397         }
    398         else {
    399         text.append("beta false\n");
    400         }
    401         if(text.length() > 0) {
    402         text.append("\n");
    403         }
    404         if(plugins.size() > 0) {
    405         text.append(plugins.toString());
    406         }
    407         if(indexes.size() > 0) {
    408         text.append(indexes.toString());
    409         }
    410         if(languages.size() > 0) {
    411         text.append(languages.toString());
    412         }
    413         if(subcollections.size() > 0) {
    414         text.append(subcollections.toString());
    415         }
    416         String super_collections_command = superman.toString();
    417         if(super_collections_command != null) {
    418         text.append(super_collections_command);
    419         }
    420         if(classifiers.size() > 0) {
    421         text.append(classifiers.toString());
    422         }
    423         if(formats.size() > 0) {
    424         text.append(formats.toString());
    425         }
    426         if(collectionmetadatum.size() > 0) {
    427         text.append(collectionmetadatum.toString());
    428         }
    429         text.append(metadatasets.toString());
    430         if(rest.size() > 0) {
    431         // Write out rest at the bottom.
    432         text.append("# These instructions are not recognized by the Gatherer.\n");
    433         for(int i = 0; i < rest.size(); i++) {
    434             text.append(rest.get(i));
    435             text.append("\n");
    436         }
    437         }
    438         out.write(text.toString().getBytes());
    439         out.close();
    440         out = null;
    441     }
    442     catch(Exception error) {
    443         error.printStackTrace();
    444     }
     164    general_manager.loseFocus();
     165    collect_config.save();
    445166    }
    446167    /** Method used during a global search and replace to highlight the appropriate record within the Collection Design Managers version of the Metadata Set Manager (view only).
     
    451172    public Rectangle setSelectedElement(String element) {
    452173    // First ensure that the metadata set controls are visible.
    453     gui.setSelectedView("CDM.GUI.MetadataSets");
     174    general_manager.setSelectedView("CDM.GUI.MetadataSets");
    454175    // Then tell them to select the given element.
    455     return metadatasets.setSelectedElement(element);
     176    return metadataset_view.setSelectedElement(element);
     177    }
     178
     179    private class CDMChangeListener
     180    implements ActionListener, DocumentListener {
     181   
     182    public void actionPerformed(ActionEvent event) {
     183        Gatherer.c_man.getCollection().setSaved(false);
     184    }
     185
     186    /** Gives notification that an attribute or set of attributes changed. */
     187    public void changedUpdate(DocumentEvent e) {
     188        Gatherer.c_man.getCollection().setSaved(false);
     189    }
     190         
     191    /** Gives notification that there was an insert into the document. */
     192    public void insertUpdate(DocumentEvent e) {
     193        Gatherer.c_man.getCollection().setSaved(false);
     194    }
     195         
     196    /** Gives notification that a portion of the document has been removed. */
     197    public void removeUpdate(DocumentEvent e) {
     198        Gatherer.c_man.getCollection().setSaved(false);
     199    }
    456200    }
    457201}
Note: See TracChangeset for help on using the changeset viewer.