Ignore:
Timestamp:
2004-03-19T13:22:45+13:00 (20 years ago)
Author:
mdewsnip
Message:

Rewrote the LegacyCollectionImporter to fix a number of bugs. It also now updates the collect.cfg file as it should.

File:
1 edited

Legend:

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

    r6879 r7095  
    2626 */
    2727package org.greenstone.gatherer.msm;
    28 /**************************************************************************************
    29  * Written:      26/11/03
    30  * Revised:     
    31  **************************************************************************************/
     28
     29
    3230import java.io.*;
    3331import java.util.*;
    34 import javax.swing.*;
    3532import org.w3c.dom.*;
    36 import org.greenstone.gatherer.Dictionary;
    3733import org.greenstone.gatherer.Gatherer;
    3834import org.greenstone.gatherer.cdm.Argument;
    3935import org.greenstone.gatherer.cdm.Classifier;
    40 import org.greenstone.gatherer.cdm.ClassifierManager;
    4136import org.greenstone.gatherer.cdm.CollectionDesignManager;
    42 import org.greenstone.gatherer.cdm.CommandTokenizer;
    43 import org.greenstone.gatherer.collection.CollectionManager;
    44 import org.greenstone.gatherer.file.FileManager;
    45 import org.greenstone.gatherer.file.FileQueue;
    4637import org.greenstone.gatherer.msm.ElementWrapper;
    4738import org.greenstone.gatherer.msm.MetadataSetManager;
    48 import org.greenstone.gatherer.msm.MSMProfiler;
    49 import org.greenstone.gatherer.msm.MSMPrompt;
    50 import org.greenstone.gatherer.util.Codec;
    5139import org.greenstone.gatherer.util.StaticStrings;
    5240import org.greenstone.gatherer.util.Utility;
    5341import org.greenstone.gatherer.valuetree.GValueModel;
    54 import org.greenstone.gatherer.valuetree.GValueNode;
    55 /** Importing metadata from legacy collections requires three distinct steps:
    56  * 1. Import hfiles and build value tree for the appropriate element. This is quite tricky as the only place to determine the relation between hfiles and the elements is the collect.cfg file. Fortunately we can reuse a lot of code from SearchForMetadata.
    57  * 2. Recurses through the import folder of a legacy collection, and prompts the user how to transform old skool metadata names into the new and improved namespace standard. Because of the optimization of only opening each metadata.xml file once, this method is so different from GreenstoneMetadataParser that I decided not to try to hang to two together, but instead to create this new class. GLI's becoming bloatware.
    58  * 3. At the same time watch for values which come from subject hierarchies, and replace with the new, confirmably unique, value.
    59  * 4. Update collection configuration as we should now be able to fix the classify commands anyway
    60  * Ok so that is four. Four distinct steps. Definately four. Couldn't possibly be more than six.
     42
     43
     44/**
     45 * Rewrote almost all this class to fix a variety of bugs.
     46 * Importing legacy collections involves three main steps:
     47 *
     48 * 1. Read the existing metadata.xml files and namespace them, usually by prompting the user
     49 *    to specify the mapping between old metadata elements and new elements.
     50 *
     51 * 2. Build complete value trees for the new metadata elements. This ensures that all the
     52 *    metadata will show up and that the hfiles are written out complete. Building the
     53 *    value trees involves parsing the old hfiles and processing hierarchical metadata
     54 *    into GLI format (where '|' is the level separator).
     55 *
     56 * 3. Fix up the classify commands in the collect.cfg file to specify the new element names.
     57 *
     58 * @author Michael Dewsnip
    6159 */
    6260public class LegacyCollectionImporter
    63     extends HashMap {
    64 
     61{
     62    private File collection_folder;
     63    private String collection_folder_path;
    6564    private CollectionDesignManager cdm;
    66     private File collection_folder;
    67 
    68     /** Constructor. The legacy collection importer object itself is a HashMap of element wrappers to HFile objects. Actually it starts off as a mapping of raw_element_name_str's to Files, but we hope to remedy that as we progress through importing metadata.
    69      * @param collection_folder the base File folder for the collection, mostly used for profile matching
    70      * @param cdm the CollectionDesignManager so we ca determine what elements map to what hierarchy file
    71      */
    72     public LegacyCollectionImporter(File collection_folder, CollectionDesignManager cdm) {
    73     super();
     65    private MetadataSetManager msm;
     66    private HashMap source_metadata_to_hfile_mapping;
     67    private boolean cancelled;
     68
     69
     70    public LegacyCollectionImporter(File collection_folder, CollectionDesignManager cdm)
     71    {
     72    this.collection_folder = collection_folder;
     73    this.collection_folder_path = collection_folder.getAbsolutePath();
    7474    this.cdm = cdm;
    75     this.collection_folder = collection_folder;
    76     // Prefill the HashMap with raw_element_name_str mappings to Files
    77     // Retrieve all of the hierarchy classifiers
     75    this.msm = Gatherer.c_man.getCollection().msm;
     76
     77    // Create a mapping from metadata element to hierarchy classifier
     78    source_metadata_to_hfile_mapping = new HashMap();
    7879    ArrayList hierarchy_classifiers_list = cdm.classifier_manager.getHierarchyClassifiers();
    79     ///ystem.err.println("When Legacy importing found: " + hierarchy_classifiers_list.size() + " hierarchies");
    80     for(int i = 0; i < hierarchy_classifiers_list.size(); i++) {
     80    for (int i = 0; i < hierarchy_classifiers_list.size(); i++) {
    8181        Classifier classifier = (Classifier) hierarchy_classifiers_list.get(i);
     82        // System.err.println("Hierarchy classifier: " + classifier);
     83
    8284        // Get the element name
    8385        Argument metadata_name_argument = classifier.getArgument(StaticStrings.METADATA_ARGUMENT);
    84         String metadata_name_str = metadata_name_argument.getValue();
    85         // Because of the way of things the elements may already have had ex. put infront of them
    86         if(metadata_name_str.startsWith(StaticStrings.EXTRACTED_NAMESPACE)) {
    87         metadata_name_str = metadata_name_str.substring(StaticStrings.EXTRACTED_NAMESPACE.length());
    88         }
    89         metadata_name_argument = null;
    90         // Generate the file
    91         Argument hfile_filename_argument = classifier.getArgument(StaticStrings.HFILE_ARGUMENT);
    92         String hfile_filename_str = hfile_filename_argument.getValue();
    93         hfile_filename_argument = null;
    94         File hfile_file = new File(collection_folder, StaticStrings.ETC_FOLDER + File.separator + hfile_filename_str);
    95         put(metadata_name_str, hfile_file);
    96         ///ystem.err.println("Queued hierarchy information for later parsing: " + metadata_name_str + "->" + hfile_file.getAbsolutePath());
    97         classifier = null;
    98     }
    99     }
    100 
    101     public void updateCDM() {
    102        
    103     }
    104 
    105     public void importMetadata() {
     86        String metadata_name = metadata_name_argument.getValue();
     87        // System.err.println("Metadata name: " + metadata_name);
     88
     89        // Remove the extracted namespace if it has been added
     90        if (metadata_name.startsWith(StaticStrings.EXTRACTED_NAMESPACE)) {
     91        metadata_name = metadata_name.substring(StaticStrings.EXTRACTED_NAMESPACE.length());
     92        }
     93
     94        // Parse the hfile for this Hierarchy classifier
     95        Argument hfile_name_argument = classifier.getArgument(StaticStrings.HFILE_ARGUMENT);
     96        String hfile_name = hfile_name_argument.getValue();
     97
     98        File hfile_file = new File(collection_folder, StaticStrings.ETC_FOLDER + File.separator + hfile_name);
     99
     100        // System.err.println("Checking hfile " + hfile_file + " for " + metadata_name);
     101        HFile hfile = new HFile(hfile_file);
     102        source_metadata_to_hfile_mapping.put(metadata_name, hfile);
     103    }
     104    }
     105
     106
     107    public void importMetadata()
     108    {
     109    // Nothing to do if we don't have any metadata sets (apart from extracted) loaded
     110    if (msm.getSets().size() <= 1) {
     111        System.err.println("No metadata sets!");
     112        return;
     113    }
     114
     115    cancelled = false;
    106116    importMetadata(new File(collection_folder, StaticStrings.IMPORT_FOLDER));
    107117    }
    108118
    109     /** Processes the files in the import tree, editing metadata.xml files so they contain namespaced element names.
    110      * @param file the current file we are inspecting
    111      */
    112     private void importMetadata(File file) {
    113     // If its a directory we recurse into it searching for metadata.xml files
    114     if(file.isDirectory()) {
     119
     120    private void importMetadata(File file)
     121    {
     122    if (file.isDirectory()) {
     123        // Apply recursively to the contents of the directory
    115124        File[] files = file.listFiles();
    116         for(int i = 0; files != null && i < files.length; i++) {
    117         importMetadata(files[i]);
    118         }
    119         files = null;
    120     }
    121     else {
    122         // We only care about metadata.xml files
    123         if(file.getName().equals(StaticStrings.METADATA_XML)) {
    124         boolean changed = false; // only set if we change something
    125         // Parse in the metadata.xml document
    126         try {
    127             Document document = Utility.parse(file.getAbsolutePath(), false);
    128             // Now, if we haven't currently got any metadata sets available to the collection, then this is as far as we need go. Rename the metadata.xml file and then move on the to next one. Remember we always have one set, the extracted metadata set.
    129             if(Gatherer.c_man.getCollection().msm.getSets().size() <= 1) {
    130             // First of all we create a backup of the current metadata.xml
    131             // I'll start by trying to rename it - but we all know how successful thats been in the past
    132             boolean done = false;
    133             try {
    134                 File old_file = new File(file.getAbsolutePath());
    135                 File new_file = new File(file.getParentFile(), StaticStrings.METADATA_BAK);
    136                 // Remove any existing backup
    137                 if(new_file.exists()) {
    138                 new_file.delete();
    139                 }
    140                 old_file.renameTo(new_file);
    141                 // Then test if the file has been renamed
    142                 if(new_file.exists()) {
    143                 ///ystem.err.println("Rename Gooooood");
    144                 done = true;
    145                 }
     125        if (files != null) {
     126        for (int i = 0; i < files.length && !cancelled; i++) {
     127            importMetadata(files[i]);
     128        }
     129        }
     130
     131        return;
     132    }
     133
     134    // We only care about metadata.xml files
     135    if (!file.getName().equals(StaticStrings.METADATA_XML)) {
     136        return;
     137    }
     138
     139    // First, copy the original file to make a backup
     140    File backup_file = new File(file.getParentFile().getParentFile(), "metadata.xml.orig");
     141    try {
     142        Gatherer.f_man.getQueue().copyFile(file, backup_file, null);
     143        if (!backup_file.exists()) {
     144        throw new Error();
     145        }
     146    }
     147    catch (Exception ex) {
     148        System.err.println("Exception occurred while importing legacy collection.");
     149        System.err.println("Error: Could not backup metadata.xml file to " + backup_file);
     150        ex.printStackTrace();
     151        System.err.println("Aborting legacy collection import.");
     152        cancelled = true;
     153        return;
     154    }
     155
     156    // Parse the metadata.xml file
     157    // System.err.println("Importing metadata from " + file);
     158    Document document = Utility.parse(file.getAbsolutePath(), false);
     159
     160    // Get a list of all the <Metadata> elements in the file, and put them in an array
     161    NodeList metadata_elements_list = document.getDocumentElement().getElementsByTagName(StaticStrings.METADATA_ELEMENT);
     162    Node[] metadata_elements = new Node[metadata_elements_list.getLength()];
     163    for (int i = 0; i < metadata_elements_list.getLength(); i++) {
     164        metadata_elements[i] = metadata_elements_list.item(i);
     165    }
     166
     167    // Now, for each metadata element...
     168    for (int i = 0; i < metadata_elements.length; i++) {
     169        Element source_element = (Element) metadata_elements[i];
     170        String source_element_name = source_element.getAttribute(StaticStrings.NAME_ATTRIBUTE);
     171        // System.err.println("Source element name: " + source_element_name);
     172
     173        // Check if there is a profile already set up for this element
     174        if (msm.profiler.containsAction(collection_folder_path, source_element_name)) {
     175        String target_element_name = msm.profiler.getAction(collection_folder_path, source_element_name);
     176
     177        // Update the metadata element and move onto the next one
     178        if (target_element_name != null) {
     179            updateMetadataElement(source_element, target_element_name);
     180        }
     181        else {
     182            // Element has been ignored, so remove it
     183            source_element.getParentNode().removeChild(source_element);
     184        }
     185
     186        continue;
     187        }
     188
     189        // No profile, so check if the element is already in the metadata set
     190        ElementWrapper target_element = msm.getElement(source_element_name, true);
     191        if (target_element != null) {
     192        String target_element_name = target_element.getName();
     193
     194        // Update the metadata element and move onto the next one
     195        updateMetadataElement(source_element, target_element_name);
     196        continue;
     197        }
     198
     199        // We must ask the user how to process this metadata element
     200        target_element = msm.prompt.selectElement(source_element_name);
     201        if (msm.prompt.wasDialogCancelled()) {
     202        cancelled = true;
     203        return;
     204        }
     205
     206        if (target_element == null) {
     207        // The user has chosen to ignore this element, so remove it
     208        source_element.getParentNode().removeChild(source_element);
     209
     210        // Add the user's choice to the profile for this collection
     211        msm.profiler.addAction(collection_folder_path, source_element_name, null);
     212        }
     213        else {
     214        // Replace the old metadata element name with the new one
     215        String target_element_name = target_element.getName();
     216
     217        // Update the metadata element
     218        updateMetadataElement(source_element, target_element_name);
     219
     220        // Add the user's choice to the profile for this collection
     221        msm.profiler.addAction(collection_folder_path, source_element_name, target_element_name);
     222        }
     223    }
     224
     225    // Write the modified metadata.xml file back out
     226    Utility.export(document, file);
     227    }
     228
     229
     230    private void updateMetadataElement(Element metadata_element, String new_element_name)
     231    {
     232    String source_element_name = metadata_element.getAttribute(StaticStrings.NAME_ATTRIBUTE);
     233    HFile hfile = (HFile) source_metadata_to_hfile_mapping.get(source_element_name);
     234
     235    // Get the value of this metadata element
     236    String element_value = MSMUtils.getValue(metadata_element);
     237    if (hfile != null) {
     238        // Map to the full value
     239        String full_element_value = hfile.getFullValue(element_value);
     240        if (full_element_value != null) {
     241        element_value = full_element_value;
     242        }
     243    }
     244
     245    // Update the name and value of the metadata element
     246    metadata_element.setAttribute(StaticStrings.NAME_ATTRIBUTE, new_element_name);
     247    MSMUtils.setValue(metadata_element, element_value);
     248
     249    // Add the value of this metadata element to the value tree
     250    GValueModel value_model = msm.getValueTree(msm.getElement(new_element_name, true));
     251    value_model.addValue(element_value);
     252    }
     253
     254
     255    public void updateClassifiers()
     256    {
     257    // Update the metadata elements in each of the classifiers
     258    for (int i = 0; i < cdm.classifier_manager.getSize(); i++) {
     259        Classifier classifier = cdm.classifier_manager.getClassifier(i);
     260        // System.err.println("Classifier: " + classifier);
     261
     262        // Update the "-metadata" value
     263        mapClassifierArgumentToNewValue(classifier, StaticStrings.METADATA_ARGUMENT);
     264
     265        // Update the "-sort" value
     266        mapClassifierArgumentToNewValue(classifier, "-sort");
     267
     268        // With Hierarchy classifiers, update the hfile arguments
     269        if (classifier.getName().equalsIgnoreCase(StaticStrings.HIERARCHY_CLASSIFIER)) {
     270        // Update the "-hfile" value
     271        Argument hfile_argument = classifier.getArgument(StaticStrings.HFILE_ARGUMENT);
     272        String hfile_value = hfile_argument.getValue();
     273
     274        // Find the source metadata element
     275        Iterator keys = source_metadata_to_hfile_mapping.keySet().iterator();
     276        while (keys.hasNext()) {
     277            String source_metadata = (String) keys.next();
     278            String hfile_name = ((HFile) source_metadata_to_hfile_mapping.get(source_metadata)).hfile_name;
     279            if (hfile_name.equals(hfile_value)) {
     280            // Update the metadata value to the new (namespaced) one
     281            if (msm.profiler.containsAction(collection_folder_path, source_metadata)) {
     282                String target_value = msm.profiler.getAction(collection_folder_path, source_metadata);
     283                hfile_argument.setValue(target_value + ".txt");
    146284            }
    147             catch(Exception exception) {
    148                 ///ystem.err.println("Rename Baaaaaad");
    149             }
    150            
    151             // Failing that I'll copy it.
    152             if(!done) {
    153                 File new_file = new File(file.getParentFile(), StaticStrings.METADATA_BAK);
    154                 Gatherer.f_man.getQueue().copyFile(file, new_file, null);
    155                 if(new_file.exists()) {
    156                 ///ystem.err.println("Copy Goooooood");
    157                 }
    158                 else {
    159                 ///ystem.err.println("Copy Baaaaaaad");
    160                 }
    161             }
    162             }
    163             // Otherwise me have some metadata sets, and so can carry on importing
    164             else {
    165             // For each metadata DOM Element
    166             NodeList metadata_DOM_elements = document.getDocumentElement().getElementsByTagName(StaticStrings.METADATA_ELEMENT);
    167             for(int i = 0; i < metadata_DOM_elements.getLength(); i++) {
    168                 Element metadata_DOM_element = (Element) metadata_DOM_elements.item(i);
    169                 // Extract the metadata element name, then check profiles to see if we already know how to handle this element. Once done try to retrieve the element from msm. If that fails, prompt the user for how to import this element.
    170                 String raw_element_name_str = metadata_DOM_element.getAttribute(StaticStrings.NAME_ATTRIBUTE);
    171                 // Check profiles
    172                 if(Gatherer.c_man.getCollection().msm.profiler.containsAction(collection_folder.getAbsolutePath(), raw_element_name_str)) {
    173                 String element_name_str = Gatherer.c_man.getCollection().msm.profiler.getAction(collection_folder.getAbsolutePath(), raw_element_name_str);
    174                 // Update the DOM
    175                 if(element_name_str != null) {
    176                     metadata_DOM_element.setAttribute(StaticStrings.NAME_ATTRIBUTE, element_name_str);
    177                     // Now to ensure GLI correctly shows this element as assigned we'll increment its occurances
    178                     ElementWrapper element_ew = Gatherer.c_man.getCollection().msm.getElement(element_name_str, true);
    179                     element_ew.inc();
    180                     // We now check if this element has a hierarchy, and if so we update the value
    181                     checkForHierarchy(element_ew, metadata_DOM_element);
    182                     element_ew = null;
    183                 }
    184                 // If the raw name is null now, we have been instructed to ignore this metadata, so we can delete it from the metadata.xml (MetadataXMLFile would do this anyway when it tried to save)
    185                 else {
    186                     // Retrieve the parent node
    187                     Node parent_DOM_node = metadata_DOM_element.getParentNode();
    188                     parent_DOM_node.removeChild(metadata_DOM_element);
    189                     parent_DOM_node = null;
    190                 }
    191                 changed = true;
    192                 element_name_str = null;
    193                 }
    194                 // No profile, lets hope it either matches straight away, or the user can decide what to do with it
    195                 else {
    196                 // Try to retrieve an element with this name from the msm. Only a perfect match is acceptable
    197                 ElementWrapper element_ew = Gatherer.c_man.getCollection().msm.getElement(raw_element_name_str, true);
    198                 // If an element was found, we increment the element count but thats it. Welcome to the only case where the DOM isn't changed
    199                 if(element_ew != null) {
    200                     element_ew.inc();
    201                     // We now check if this element has a hierarchy, and if so we update the value
    202                     checkForHierarchy(element_ew, metadata_DOM_element);
    203                 }
    204                 // If no match was found prompt the user as to how to proceed
    205                 else {
    206                     boolean confirmed = false;
    207                     boolean dialog_cancelled = false;
    208                     while(!confirmed) {
    209                     element_ew = Gatherer.c_man.getCollection().msm.prompt.selectElement(raw_element_name_str);
    210                     dialog_cancelled = Gatherer.c_man.getCollection().msm.prompt.wasDialogCancelled();
    211                     // If the user chooses something then add to profile, and process the DOM as appropriate
    212                     if(!dialog_cancelled) {
    213                         // Choosen an element to append or merge
    214                         if(element_ew != null) {
    215                         String element_name_str = element_ew.getName();
    216                         Gatherer.c_man.getCollection().msm.profiler.addAction(collection_folder.getAbsolutePath(), raw_element_name_str, element_name_str);
    217                         metadata_DOM_element.setAttribute(StaticStrings.NAME_ATTRIBUTE, element_name_str);
    218                         element_name_str = null;
    219                         element_ew.inc();
    220                        
    221                         // We can now build the hfile properly is necessary
    222                         File hfile_file = (File) get(raw_element_name_str);
    223                         if(hfile_file != null) {
    224                             ///ystem.err.println("Processing the mapping for: " + raw_element_name_str);
    225                             // Remove the current mapping
    226                             remove(raw_element_name_str);
    227                             // Create the HFile
    228                             try {
    229                             HFile hfile = new HFile(element_ew, hfile_file);
    230                             // Store the mapping
    231                             ///ystem.err.println("Adding a hfile mapping for: " + element_ew);
    232                             put(element_ew, hfile);
    233                             // Now update the value
    234                             String raw_value_str = MSMUtils.getValue(metadata_DOM_element);
    235                             String new_value_str = hfile.getFullValue(raw_value_str);
    236                             MSMUtils.setValue(metadata_DOM_element, new_value_str);
    237                             raw_value_str = null;
    238                             new_value_str = null;
    239                             hfile = null;
    240                             }
    241                             catch (Exception exception) {
    242                             Gatherer.println("***** Exception in org.greenstone.gatherer.msm.LegacyCollectionImporter *****");
    243                             Gatherer.printStackTrace(exception);
    244                             }
    245                         }
    246                         element_ew = null;
    247                         }
    248                         // Choosen to ignore the element
    249                         else {
    250                         Gatherer.c_man.getCollection().msm.profiler.addAction(collection_folder.getAbsolutePath(), raw_element_name_str, null);
    251                         // Retrieve the parent node
    252                         Node parent_DOM_node = metadata_DOM_element.getParentNode();
    253                         parent_DOM_node.removeChild(metadata_DOM_element);
    254                         parent_DOM_node = null;
    255                         }
    256                         confirmed = true;
    257                         changed = true;
    258                     }
    259                     // Cancel only skips the current metadata.xml file - actually I'll ignore the file on a cancel, as otherwise it will bugger up MetadataXMLFileManager when it starts up
    260                     else {
    261                         // Confirm the cancel, informing the user that the metadata.xml will be completely ignored if they continue
    262                         confirmed = (JOptionPane.showConfirmDialog(Gatherer.g_man, Dictionary.get("MSM.Legacy.Delete_Metadata_XML", file.getAbsolutePath()), Dictionary.get("General.Warning"), JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION);
    263                     }
    264                     }
    265                 }
    266                 }
    267                 raw_element_name_str = null;
    268                 metadata_DOM_element = null;
    269             }
    270             // If the DOM has saved, write it back out again
    271             if(changed) {
    272                 // First of all we create a backup of the current metadata.xml
    273                 // I'll start by trying to rename it - but we all know how successful thats been in the past
    274                 boolean done = false;
    275                 try {
    276                 File old_file = new File(file.getAbsolutePath());
    277                 File new_file = new File(file.getParentFile(), StaticStrings.METADATA_BAK);
    278                 // Remove any existing backup
    279                 if(new_file.exists()) {
    280                     new_file.delete();
    281                 }
    282                 old_file.renameTo(new_file);
    283                 // Then test if the file has been renamed
    284                 if(new_file.exists()) {
    285                 ///ystem.err.println("Rename Gooooood");
    286                     done = true;
    287                 }
    288                 }
    289                 catch(Exception exception) {
    290                 ///ystem.err.println("Rename Baaaaaad");
    291                 }
    292                
    293                 // Failing that I'll copy it.
    294                 if(!done) {
    295                 File new_file = new File(file.getParentFile(), StaticStrings.METADATA_BAK);
    296                 Gatherer.f_man.getQueue().copyFile(file, new_file, null);
    297                 if(new_file.exists()) {
    298                 ///ystem.err.println("Copy Goooooood");
    299                 }
    300                 else {
    301                 ///ystem.err.println("Copy Baaaaaaad");
    302                 }
    303                 }
    304                 Utility.export(document, file);
    305             }
    306             document = null;
     285
     286            break;
    307287            }
    308288        }
    309         catch(Exception exception) {
    310             // Dump a stack trace to debug
    311             Gatherer.println("***** Exception in org.greenstone.gatherer.msm.LegacyCollectionImporter *****");
    312             Gatherer.printStackTrace(exception);
    313             // Then display a message complaining about this file
    314             JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("MSM.Legacy.Corrupt_Metadata_XML", file.getAbsolutePath()), Dictionary.get("General.Error"), JOptionPane.ERROR_MESSAGE);
    315         }
    316         }
    317         // Else you may go to the Devil's dam. Yours gifts are so good, heres none will hold you. Their love is not so great, Hortensio, but we may blow our nails together, and fast it fairly out.
    318     }
    319     }
    320 
    321     private void checkForHierarchy(ElementWrapper element, Element metadata_DOM_element) {
    322     // Iterator through the keys in the HashMap looking for a match to our element. Why, oh why, doesn't HashMap use element.equals(element) like its meant to?
    323     HFile hfile = null;
    324     for(Iterator keys = keySet().iterator(); hfile == null && keys.hasNext(); ) {
    325         Object key_object = keys.next();
    326         if(key_object instanceof ElementWrapper) {
    327         ElementWrapper key = (ElementWrapper) key_object;
    328         ///ystem.err.print("Does " + key + " equal " + element + "? ");
    329         if(key.equals(element)) {
    330             ///ystem.err.println("Yes!");
    331             hfile = (HFile) get(key);
    332         }
    333         key = null;
    334         }
    335         key_object = null;
    336     }
    337     if(hfile != null) {
    338         String raw_value_str = MSMUtils.getValue(metadata_DOM_element);
    339         String new_value_str = hfile.getFullValue(raw_value_str);
    340         MSMUtils.setValue(metadata_DOM_element, new_value_str);
    341         raw_value_str = null;
    342         new_value_str = null;
    343         hfile = null;
    344     }
    345     }
     289        }
     290
     291        // System.err.println("Classifier (after): " + classifier);
     292    }
     293    }
     294
     295
     296    private void mapClassifierArgumentToNewValue(Classifier classifier, String argument_name)
     297    {
     298    Argument argument = classifier.getArgument(argument_name);
     299    String value = argument.getValue();
     300    // System.err.println("Value: " + value);
     301
     302    // Remove the extracted namespace if it has been added
     303    if (value.startsWith(StaticStrings.EXTRACTED_NAMESPACE)) {
     304        value = value.substring(StaticStrings.EXTRACTED_NAMESPACE.length());
     305    }
     306
     307    // Update the metadata value to the new (namespaced) one
     308    if (msm.profiler.containsAction(collection_folder_path, value)) {
     309        String target_value = msm.profiler.getAction(collection_folder_path, value);
     310        argument.setValue(target_value);
     311    }
     312    }
     313
    346314
    347315    /** Another basic HFile wrapper. This one expects you to provide an element when you create it, then as it is built it generates the value tree as well. Later it allows you to provide an alias and retrieve the full path string (delimited by pipes) */
    348316    private class HFile
    349     extends HashMap {
    350 
    351     public HFile(ElementWrapper element, File file)
    352         throws Exception {
    353         super();
    354         HashMap index_to_entry = new HashMap();
    355         // Read in the hfile, line by line, creating entry mappings
    356         FileReader in_filereader = new FileReader(file);
    357         BufferedReader in = new BufferedReader(in_filereader);
    358         String line = null;
    359         while((line = in.readLine()) != null) {
    360         CommandTokenizer tokenizer = new CommandTokenizer(line);
    361         String alias = tokenizer.nextToken();
    362         String index = tokenizer.nextToken();
    363         String value = Utility.decodeGreenstone(tokenizer.nextToken());
    364         ///ystem.err.println("Read " + index + ", " + alias + ", " + value);
    365         if(alias.startsWith("\"") && alias.endsWith("\"") && !alias.equals("\"\"")) {
    366             alias = alias.substring(1, alias.length() - 1);
    367         }
    368         if(value.startsWith("\"") && value.endsWith("\"") && !value.equals("\"\"")) {
    369             value = value.substring(1, value.length() - 1);
    370         }
    371         ///ystem.err.println("Storing:\nindex:" + index + "\nalias:" + alias + "\nvalue:" + value);
    372         index_to_entry.put(index, new Entry(alias, value));
    373         value = null;
    374         alias = null;
    375         index = null;
    376         tokenizer = null;
    377         }
    378         in.close();
    379         in_filereader.close();
    380         in = null;
    381         in_filereader = null;
    382         // Now iterate through the HashMap creating ValueNodes and mappings as we go
    383         for(Iterator keys = index_to_entry.keySet().iterator(); keys.hasNext(); ) {
    384         String index = (String) keys.next();
    385         Entry entry = (Entry) index_to_entry.get(index);
    386         String alias = entry.alias;
    387         String value = entry.value;
    388         // Chop the last reference off index, as we already have it
    389         if(index.indexOf(StaticStrings.STOP_CHARACTER) > -1) {
    390             index = index.substring(0, index.lastIndexOf(StaticStrings.STOP_CHARACTER));
    391             // Then while theres still futher indexes left, retrieve them
    392             while(index.length() > 0) {
    393             // Retrieve that value (if any).
    394             entry = (Entry) index_to_entry.get(index);
    395             if(entry != null) {
    396                 // Precatenate with the current value separating with a pipe
    397                 value = entry.value + StaticStrings.PIPE_CHAR + value;
    398             }
    399             // Then trim the index down
    400             if(index.indexOf(StaticStrings.STOP_CHARACTER) > -1) {
    401                 index = index.substring(0, index.lastIndexOf(StaticStrings.STOP_CHARACTER));
    402             }
    403             else {
    404                 index="";
    405             }
    406             }
    407         }
    408         // Create the value node
    409         GValueModel value_model = Gatherer.c_man.getCollection().msm.getValueTree(element);
    410         GValueNode value_node = value_model.addValue(value);
    411         // And place the mapping
    412         put(alias, value_node);
    413         }
    414         index_to_entry.clear();
    415         index_to_entry = null;
    416 
    417         // Now we rename the old hfile
    418         boolean done = false;
     317    {
     318    public String hfile_name;
     319    private HashMap index_to_entry_mapping;
     320    private HashMap alias_to_value_mapping;
     321
     322    public HFile(File file)
     323    {
     324        hfile_name = file.getName();
     325        index_to_entry_mapping = new HashMap();
     326        alias_to_value_mapping = new HashMap();
     327
    419328        try {
    420         File old_file = new File(file.getAbsolutePath());
    421         File new_file = new File(file.getParentFile(), StaticStrings.METADATA_BAK);
    422         // Remove any existing backup
    423         if(new_file.exists()) {
    424             new_file.delete();
    425         }
    426         old_file.renameTo(new_file);
    427         // Then test if the file has been renamed
    428         if(new_file.exists()) {
    429             ///ystem.err.println("Rename Gooooood");
    430             done = true;
    431         }
    432         }
    433         catch(Exception exception) {
    434         ///ystem.err.println("Rename Baaaaaad");
    435         }
    436        
    437         // Failing that I'll copy it.
    438         if(!done) {
    439         File new_file = new File(file.getParentFile(), StaticStrings.METADATA_BAK);
    440         Gatherer.f_man.getQueue().copyFile(file, new_file, null);
    441         if(new_file.exists()) {
    442             ///ystem.err.println("Copy Goooooood");
    443         }
    444         else {
    445             ///ystem.err.println("Copy Baaaaaaad");
    446         }
    447         }
    448         // Right. Get rid of original
    449         file.delete();
    450     }
    451    
    452     public String getFullValue(String alias) {
    453         ///ystem.err.println("Searching for the value for alias: " + alias);
    454         for(Iterator keys = keySet().iterator(); keys.hasNext(); ) {
    455         String key = (String) keys.next();
    456         if(alias.equals(key)) {
    457             GValueNode value_node = (GValueNode) get(key);
    458             if(value_node != null) {
    459             ///ystem.err.println("Found value: " + value_node.getFullPath(false));
    460             return Codec.transform(value_node.getFullPath(false), Codec.TEXT_TO_DOM);
     329        // Read in the hfile, line by line, creating entry mappings
     330        FileReader file_reader = new FileReader(file);
     331        BufferedReader buffered_reader = new BufferedReader(file_reader);
     332        String line = null;
     333        while ((line = buffered_reader.readLine()) != null) {
     334            // Read alias
     335            String alias = "";
     336            int alias_end;
     337            if (line.indexOf("\"") == 0) {
     338            alias_end = line.indexOf("\"", 1);
     339            alias = line.substring(1, alias_end);
    461340            }
    462341            else {
    463             ///ystem.err.println("Error!");
    464             return alias; // Can't do any better
     342            alias_end = line.indexOf(" ", 1);
     343            alias = line.substring(0, alias_end);
    465344            }
     345            // System.err.println("Alias: " + alias);
     346
     347            int value_start = line.indexOf("\"", alias_end + 1);
     348            int value_end = line.indexOf("\"", value_start + 1);
     349            String value = line.substring(value_start + 1, value_end);
     350            // System.err.println("Value: " + value);
     351
     352            // if (!alias.equals(value)) {
     353            // System.err.println("Alias (" + alias + ") and value (" + value + ") differ!");
     354            // }
     355
     356            String index = line.substring(alias_end + 1, value_start).trim();
     357            // System.err.println("Index: " + index);
     358
     359            index_to_entry_mapping.put(index, new Entry(alias, value));
    466360        }
    467         }
    468         ///ystem.err.println("Error!");
    469         return alias;
    470     }
    471 
    472     private class Entry {
    473         public String alias;
    474         public String value;
    475         public Entry(String alias, String value) {
    476         this.alias = alias;
    477         this.value = value;
    478         }
    479     }
     361
     362        buffered_reader.close();
     363        file_reader.close();
     364        }
     365        catch (Exception ex) {
     366        System.err.println("Exception reading hfile " + file);
     367        ex.printStackTrace();
     368        }
     369
     370        Iterator index_keys = index_to_entry_mapping.keySet().iterator();
     371        while (index_keys.hasNext()) {
     372        String index = (String) index_keys.next();
     373        String alias = ((Entry) index_to_entry_mapping.get(index)).alias;
     374        String value = ((Entry) index_to_entry_mapping.get(index)).value;
     375
     376        // Chop the last reference off index, as we already have it
     377        if (index.indexOf(StaticStrings.STOP_CHARACTER) > -1) {
     378            index = index.substring(0, index.lastIndexOf(StaticStrings.STOP_CHARACTER));
     379            // Then while there are still futher indexes left, retrieve them
     380            while (index.length() > 0) {
     381            // Retrieve that value (if any).
     382            Entry entry = (Entry) index_to_entry_mapping.get(index);
     383            if (entry != null) {
     384                // Precatenate with the current value separating with a pipe
     385                value = entry.value + StaticStrings.PIPE_CHAR + value;
     386            }
     387            // Then trim the index down
     388            if (index.indexOf(StaticStrings.STOP_CHARACTER) > -1) {
     389                index = index.substring(0, index.lastIndexOf(StaticStrings.STOP_CHARACTER));
     390            }
     391            else {
     392                index = "";
     393            }
     394            }
     395        }
     396
     397        alias_to_value_mapping.put(alias, value);
     398        }
     399
     400        index_to_entry_mapping.clear();
     401    }
     402
     403
     404    public String getFullValue(String alias)
     405    {
     406        return (String) alias_to_value_mapping.get(alias);
     407    }
     408
     409
     410    private class Entry
     411    {
     412        public String alias;
     413        public String value;
     414
     415        public Entry(String alias, String value) {
     416        this.alias = alias;
     417        this.value = value;
     418        }
     419    }
    480420    }
    481421}
Note: See TracChangeset for help on using the changeset viewer.