Changeset 7827


Ignore:
Timestamp:
2004-07-30T04:54:25+12:00 (20 years ago)
Author:
mdewsnip
Message:

More of the new metadata code.

Location:
trunk/gli/src/org/greenstone/gatherer/metadata
Files:
1 added
6 edited

Legend:

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

    r7822 r7827  
    55import java.util.*;
    66import org.greenstone.gatherer.util.Utility;
     7import org.greenstone.gatherer.util.XMLTools;
    78import org.w3c.dom.*;
    89
     
    112113     if (file_relative_path.equals(files_in_doc_xml_file.get(i))) {
    113114        contains_extracted_metadata_for_file = true;
    114         System.err.println("Found extracted metadata in file " + getAbsolutePath());
     115        // System.err.println("Found extracted metadata in file " + getAbsolutePath());
    115116        break;
    116117     }
  • trunk/gli/src/org/greenstone/gatherer/metadata/MetadataSet.java

    r7822 r7827  
    44import java.io.*;
    55import java.util.*;
     6import org.greenstone.gatherer.util.XMLTools;
    67import org.w3c.dom.*;
    78
  • trunk/gli/src/org/greenstone/gatherer/metadata/MetadataValue.java

    r7822 r7827  
    1313   private MetadataElement metadata_element = null;
    1414   private MetadataValueTreeNode metadata_value_tree_node = null;
     15   private boolean is_accumulating_metadata = false;
    1516
    1617
     
    7374
    7475
     76   public boolean isAccumulatingMetadata()
     77   {
     78      return is_accumulating_metadata;
     79   }
     80
     81
    7582   public boolean isInheritedMetadata()
    7683   {
    7784      return (folder_metadata_inherited_from != null);
    7885   }
     86
     87
     88   public void setIsAccumulatingMetadata(boolean is_accumulating_metadata)
     89   {
     90      this.is_accumulating_metadata = is_accumulating_metadata;
     91   }
    7992}
    8093
  • trunk/gli/src/org/greenstone/gatherer/metadata/MetadataValueTableModel.java

    r7822 r7827  
    1717   extends AbstractTableModel
    1818{
     19   /** The FileNodes this model is built for */
     20   private FileNode[] file_nodes = null;
    1921   /** The list of MetadataValueTableEntries in the table */
    2022   private ArrayList metadata_value_table_entries = new ArrayList();
    21    /** The FileNodes this model is built for */
    22    private FileNode[] file_nodes = null;
    23    /** The table this model is being displayed in */
    24    private JTable metadata_value_table = null;
    25 
    26    private MetadataValueTableModelBuilder builder = null;
    2723
    2824   static final private String[] COLUMN_NAMES = {"", Dictionary.get("Metadata.Element"),  Dictionary.get("Metadata.Value")};
    2925
    3026
    31    public MetadataValueTableModel(JTable metadata_value_table, FileNode[] file_nodes)
    32    {
    33       this.metadata_value_table = metadata_value_table;
     27   /** Returns the number of columns in this table. */
     28   public int getColumnCount()
     29   {
     30      return COLUMN_NAMES.length;
     31   }
     32
     33
     34   /** Retrieves the name of the specified column. */
     35   public String getColumnName(int col)
     36   {
     37      return COLUMN_NAMES[col];
     38   }
     39
     40
     41   /* Called to retrieve the MetadataValueTableEntry at a certain row. Usually caused by the user selecting a row in the table. It is synchronized so that the model doesn't up and change while we're trying to retrieve the indicated element. */
     42   public synchronized MetadataValueTableEntry getMetadataValueTableEntry(int row)
     43   {
     44      if (row >= 0 && row < metadata_value_table_entries.size()) {
     45     return (MetadataValueTableEntry) metadata_value_table_entries.get(row);
     46      }
     47
     48      return null;
     49   }
     50
     51
     52   /** Returns the number of rows in this table. */
     53   public int getRowCount()
     54   {
     55      return metadata_value_table_entries.size();
     56   }
     57
     58
     59   /** Returns the cell value at a given row and column as an Object. */
     60   public Object getValueAt(int row, int col)
     61   {
     62      // Check values are reasonable
     63      if (row < 0 || row >= metadata_value_table_entries.size() || col < 0 || col >= COLUMN_NAMES.length) {
     64     return null;
     65      }
     66
     67      MetadataValueTableEntry metadata_value_table_entry = (MetadataValueTableEntry) metadata_value_table_entries.get(row);
     68      if (col == 0 && metadata_value_table_entry.isInheritedMetadata()) {
     69     return metadata_value_table_entry.getFolderMetadataInheritedFrom();
     70      }
     71
     72      if (col == 1) {
     73     return metadata_value_table_entry.getMetadataElement();
     74      }
     75
     76      if (col == 2) {
     77     return metadata_value_table_entry.getFullValue();
     78      }
     79
     80      return null;
     81   }
     82
     83
     84   /** Determine if the given metadata is common to all selected file nodes given the context of the current view. */
     85   public boolean isCommon(MetadataValueTableEntry metadata_value_table_entry)
     86   {
     87      return (file_nodes != null && metadata_value_table_entry.getOccurrences() == file_nodes.length);
     88   }
     89
     90
     91   /** Determine if the given metadata is common to all selected file nodes given the context of the current view. */
     92   public boolean isCommon(int row)
     93   {
     94      if (row >= 0 && row < metadata_value_table_entries.size()) {
     95     return isCommon((MetadataValueTableEntry) metadata_value_table_entries.get(row));
     96      }
     97
     98      return false;
     99   }
     100
     101
     102   public void rebuild(FileNode[] file_nodes)
     103   {
     104      metadata_value_table_entries.clear();
     105
     106      if (!Gatherer.c_man.ready()) {
     107     Gatherer.println("Not ready!");
     108     return;
     109      }
    34110
    35111      this.file_nodes = file_nodes;
    36112      if (file_nodes != null && file_nodes.length > 0) {
    37113     // Create model builder
    38      builder = new MetadataValueTableModelBuilder();
    39      builder.start();
    40       }
    41    }
    42 
    43 
    44    /** Returns the number of columns in this table. */
    45    public int getColumnCount()
    46    {
    47       return COLUMN_NAMES.length;
    48    }
    49 
    50 
    51    /** Retrieves the name of the specified column. */
    52    public String getColumnName(int col)
    53    {
    54       return COLUMN_NAMES[col];
    55    }
    56 
    57 
    58    /* Called to retrieve the MetadataValueTableEntry at a certain row. Usually caused by the user selecting a row in the table. It is synchronized so that the model doesn't up and change while we're trying to retrieve the indicated element. */
    59    public synchronized MetadataValueTableEntry getMetadataValueTableEntry(int row)
    60    {
    61       if (row >= 0 && row < metadata_value_table_entries.size()) {
    62      return (MetadataValueTableEntry) metadata_value_table_entries.get(row);
    63       }
    64 
    65       return null;
    66    }
    67 
    68 
    69    /** Returns the number of rows in this table. */
    70    public int getRowCount()
    71    {
    72       return metadata_value_table_entries.size();
    73    }
    74 
    75 
    76    /** Returns the cell value at a given row and column as an Object. */
    77    public Object getValueAt(int row, int col)
    78    {
    79       // Check values are reasonable
    80       if (row < 0 || row >= metadata_value_table_entries.size() || col < 0 || col >= COLUMN_NAMES.length) {
    81      return null;
    82       }
    83 
    84       MetadataValueTableEntry metadata_value_table_entry = (MetadataValueTableEntry) metadata_value_table_entries.get(row);
    85       if (col == 0 && metadata_value_table_entry.isInheritedMetadata()) {
    86      return metadata_value_table_entry.getFolderMetadataInheritedFrom();
    87       }
    88 
    89       if (col == 1) {
    90      return metadata_value_table_entry.getMetadataElement();
    91       }
    92 
    93       if (col == 2) {
    94      return metadata_value_table_entry.getFullValue();
    95       }
    96 
    97       return null;
    98    }
    99 
    100    
    101    public boolean isBuildingComplete()
    102    {
    103       return (builder == null || builder.complete);
    104    }
    105 
    106 
    107    /** Determine if the given metadata is common to all selected file nodes given the context of the current view. */
    108    public boolean isCommon(MetadataValueTableEntry metadata_value_table_entry)
    109    {
    110       return (file_nodes != null && metadata_value_table_entry.getOccurrences() == file_nodes.length);
    111    }
    112 
    113 
    114    /** Determine if the given metadata is common to all selected file nodes given the context of the current view. */
    115    public boolean isCommon(int row)
    116    {
    117       if (row >= 0 && row < metadata_value_table_entries.size()) {
    118      return isCommon((MetadataValueTableEntry) metadata_value_table_entries.get(row));
    119       }
    120 
    121       return false;
    122    }
    123 
    124 
    125    public void selectClosestMetadataValueTableEntryWhenBuildingComplete(MetadataValueTableEntry metadata_value_table_entry)
    126    {
    127       // If there is no builder or no data then there is no point...
    128       if (builder == null || metadata_value_table_entry == null) {
    129      return;
    130       }
    131 
    132       // If building is not complete, ask the builder to select the metadata when finished
    133       if (!builder.complete) {
    134      builder.select_closest_metadata_value_table_entry = metadata_value_table_entry;
    135      return;
    136       }
    137 
    138       // Otherwise, select the appropriate row in the table...
    139       // ...but only if there actually is some metadata in the table
    140       if (metadata_value_table_entries == null || metadata_value_table_entries.size() == 0) {
    141      return;
    142       }
    143 
    144       // Find the position in the list where the piece of metadata should be
    145       int i = 0;
    146       for (i = 0; i < metadata_value_table_entries.size(); i++) {
    147      if (metadata_value_table_entry.compareTo(metadata_value_table_entries.get(i)) <= 0) {
    148         break;
    149      }
    150       }
    151 
    152       // Ensure the position is within the current metadata
    153       if (i >= metadata_value_table_entries.size()) {
    154      i = metadata_value_table_entries.size() - 1;
    155       }
    156 
    157       // Select the same row as before, except if it would select a different element - in which case select the first element
    158       MetadataElement metadata_element = metadata_value_table_entry.getMetadataElement();
    159       MetadataValueTableEntry newly_selected_metadata_value_table_entry = (MetadataValueTableEntry) metadata_value_table_entries.get(i);
    160       if (!newly_selected_metadata_value_table_entry.getMetadataElement().equals(metadata_element)) {
    161      // Try one row previous
    162      i--;
    163      if (i < 0) {
    164         Gatherer.println("Could not find suitable row to select!");
    165         i = 0;
    166      }
    167      else {
    168         newly_selected_metadata_value_table_entry = (MetadataValueTableEntry) metadata_value_table_entries.get(i);
    169         if (!newly_selected_metadata_value_table_entry.getMetadataElement().equals(metadata_element)) {
    170            Gatherer.println("Could not find suitable row to select!");
    171            i = 0;
    172         }
    173      }
    174       }
    175 
    176       // We have to put the selection task onto the AWT thread
    177       SwingUtilities.invokeLater(new TableRowSelectionTask(i));
     114     MetadataValueTableModelBuilder builder = new MetadataValueTableModelBuilder();
     115     builder.run();
     116      }
    178117   }
    179118
    180119
    181120   private class MetadataValueTableModelBuilder
    182       extends Thread
    183    {
    184       public boolean complete = false;
    185       public MetadataValueTableEntry select_closest_metadata_value_table_entry = null;
    186 
     121   {
    187122      public void run()
    188123      {
    189      if (!Gatherer.c_man.ready()) {
    190         Gatherer.println("Not ready!");
    191         return;
    192      }
    193 
    194124     // Build a list of MetadataValueTableEntries that represent the metadata asssigned to the selected files
    195      metadata_value_table_entries.clear();
    196125     boolean hid_extracted_metadata = false;
    197126     boolean has_inherited_metadata = false;
     
    220149        }
    221150
    222         ArrayList extracted_metadata = DocXMLFileManager.getMetadataExtractedFromFile(current_file);
    223         for (int k = 0; k < extracted_metadata.size(); k++) {
    224            MetadataValue metadata_value = (MetadataValue) extracted_metadata.get(k);
    225 
    226            // Insert this metadata value into the table, unless it already exists (in which case increment its count)
    227            insertMetadataValueIntoTable(metadata_value);
     151        // Get the extracted metadata for this file, if desired
     152        if (Gatherer.config.get("general.view_extracted_metadata", Configuration.COLLECTION_SPECIFIC) == true) {
     153           ArrayList extracted_metadata = DocXMLFileManager.getMetadataExtractedFromFile(current_file);
     154           for (int k = 0; k < extracted_metadata.size(); k++) {
     155          MetadataValue metadata_value = (MetadataValue) extracted_metadata.get(k);
     156
     157          // Insert this metadata value into the table, unless it already exists (in which case increment its count)
     158          insertMetadataValueIntoTable(metadata_value);
     159           }
    228160        }
    229161     }
     
    244176           insertMetadataValueIntoTable(metadata_value_table_entry);
    245177        }
    246      }
    247 
    248      // Processing complete
    249      complete = true;
    250 
    251      // If while processing we've been asked to select a certain metadata, do it now
    252      if (select_closest_metadata_value_table_entry != null) {
    253         selectClosestMetadataValueTableEntryWhenBuildingComplete(select_closest_metadata_value_table_entry);
    254178     }
    255179
     
    320244      }
    321245   }
    322 
    323 
    324    /** Little utility class for selecting a row in the table, on the AWT thread. */
    325    private class TableRowSelectionTask
    326       extends Thread
    327    {
    328       private int row_to_select;
    329 
    330       public TableRowSelectionTask(int row_to_select)
    331       {
    332      this.row_to_select = row_to_select;
    333       }
    334 
    335       public void run()
    336       {
    337      Rectangle cell_rect = metadata_value_table.getCellRect(row_to_select, 0, true);
    338      metadata_value_table.scrollRectToVisible(cell_rect);
    339      metadata_value_table.setRowSelectionInterval(row_to_select, row_to_select);
    340       }
    341    }
    342246}
  • trunk/gli/src/org/greenstone/gatherer/metadata/MetadataXMLFile.java

    r7822 r7827  
    44import java.io.*;
    55import java.util.*;
     6import org.greenstone.gatherer.util.XMLTools;
    67import org.w3c.dom.*;
    78
     
    6566      // Convert the file path into a regular expression that will match it
    6667      String file_path_regexp = file_relative_path.replaceAll("\\.", "\\\\.");
     68      if (file_relative_path.equals("")) {
     69     file_path_regexp = DIRECTORY_FILENAME;
     70      }
     71
     72      // Create a new Metadata element to record this metadata
     73      Element new_metadata_value_element = document.createElement(METADATA_ELEMENT);
     74      new_metadata_value_element.setAttribute("name", metadata_value.getMetadataElement().getFullName());
     75      new_metadata_value_element.setAttribute("mode", (metadata_value.isAccumulatingMetadata() ? "accumulate" : "override"));
     76      new_metadata_value_element.appendChild(document.createTextNode(metadata_value.getFullValue()));
     77      boolean have_added_metadata = false;
    6778
    6879      // Read all the FileSet elements in the file
     
    7081      for (int i = 0; i < fileset_elements_nodelist.getLength(); i++) {
    7182     Element current_fileset_element = (Element) fileset_elements_nodelist.item(i);
    72      boolean current_fileset_matches = false;
    7383
    7484     // Check the FileName elements of the FileSet to see if we have a match
     
    8090        // Only exact matches can be extended with new metadata
    8191        if (current_filename_element_value.equals(file_path_regexp)) {
    82            current_fileset_matches = true;
     92           // Append the new Metadata element to the Description element of this FileSet
     93           Element description_element = (Element) current_fileset_element.getElementsByTagName(DESCRIPTION_ELEMENT).item(0);
     94
     95           // Accumulating metadata: add at the end
     96           if (metadata_value.isAccumulatingMetadata()) {
     97          description_element.appendChild(new_metadata_value_element);
     98           }
     99           // Override metadata: add at the start (so it overrides inherited metadata without affecting other assigned metadata)
     100           else {
     101          description_element.insertBefore(new_metadata_value_element, description_element.getFirstChild());
     102           }
     103
     104           have_added_metadata = true;
    83105           break;
    84106        }
    85107     }
    86 
    87      // The FileSet doesn't apply, so move onto the next one
    88      if (current_fileset_matches == false) {
    89         continue;
    90      }
    91 
    92      // Create a new Metadata element to record this metadata
    93      Element new_metadata_value_element = document.createElement(METADATA_ELEMENT);
    94      new_metadata_value_element.setAttribute("name", metadata_value.getMetadataElement().getFullName());
    95      new_metadata_value_element.appendChild(document.createTextNode(metadata_value.getFullValue()));
     108      }
     109
     110      // Check if the metadata was added to an existing FileSet
     111      if (!have_added_metadata) {
     112     // It wasn't, so create a new FileSet element for it
     113     Element new_fileset_element = document.createElement(FILESET_ELEMENT);
     114
     115     Element new_filename_element = document.createElement(FILENAME_ELEMENT);
     116     new_filename_element.appendChild(document.createTextNode(file_path_regexp));
     117     new_fileset_element.appendChild(new_filename_element);
    96118
    97119     // Append the new Metadata element to the Description element of this FileSet
    98      Element description_element = (Element) current_fileset_element.getElementsByTagName(DESCRIPTION_ELEMENT).item(0);
    99      description_element.appendChild(new_metadata_value_element);
    100       }
    101 
    102       // Write the metadata.xml file
     120     Element new_description_element = document.createElement(DESCRIPTION_ELEMENT);
     121     new_description_element.appendChild(new_metadata_value_element);
     122     new_fileset_element.appendChild(new_description_element);
     123
     124     document.getDocumentElement().appendChild(new_fileset_element);
     125      }
     126
     127      // Rewrite the metadata.xml file
    103128      XMLTools.writeXMLFile(this, document);
    104129   }
     
    181206        }
    182207
    183         // Add the new metadata value to the list
    184208        MetadataValue metadata_value = new MetadataValue(metadata_element, metadata_value_tree_node);
    185209        if (is_folder_level_metadata && !file_relative_path.equals("")) {
    186210           metadata_value.inheritsMetadataFromFolder(metadata_xml_file_directory);
    187211        }
     212
     213        // Is this accumulating metadata?
     214        if (current_metadata_element.getAttribute("mode").equals("accumulate")) {
     215           metadata_value.setIsAccumulatingMetadata(true);
     216        }
     217
     218        // Add the new metadata value to the list
    188219        metadata_values.add(metadata_value);
    189220     }
     
    212243      // Convert the file path into a regular expression that will match it
    213244      String file_path_regexp = file_relative_path.replaceAll("\\.", "\\\\.");
     245      if (file_relative_path.equals("")) {
     246     file_path_regexp = DIRECTORY_FILENAME;
     247      }
    214248
    215249      // Read all the FileSet elements in the file
     
    261295      }
    262296
    263       // Write the metadata.xml file
     297      // Rewrite the metadata.xml file
    264298      XMLTools.writeXMLFile(this, document);
    265299   }
  • trunk/gli/src/org/greenstone/gatherer/metadata/MetadataXMLFileManager.java

    r7822 r7827  
    55import java.util.*;
    66import org.greenstone.gatherer.file.FileNode;
     7import org.greenstone.gatherer.util.Utility;
     8import org.greenstone.gatherer.util.XMLTools;
    79
    810
     
    2022
    2123     // Find which metadata.xml file needs editing
     24     boolean applicable_metadata_xml_file_found = false;
    2225     File current_file_directory = (current_file.isDirectory() ? current_file : current_file.getParentFile());
    2326     for (int j = 0; j < metadata_xml_files.size(); j++) {
     
    2629        // This metadata.xml file is only potentially applicable if it is above or at the same level as the file
    2730        if (current_file_directory.getAbsolutePath().startsWith(metadata_xml_file.getParentFile().getAbsolutePath())) {
     31           applicable_metadata_xml_file_found = true;
    2832           metadata_xml_file.addMetadata(current_file, metadata_value);
    2933        }
     34     }
     35
     36     // If no applicable metadata.xml file exists, we have to create a new one
     37     if (!applicable_metadata_xml_file_found) {
     38        // Create a new (empty) metadata.xml file in the file's directory...
     39        File new_metadata_xml_file_file = new File(current_file_directory, "metadata.xml");
     40        XMLTools.writeXMLFile(new_metadata_xml_file_file, Utility.parse("xml/metadata.xml", true));
     41
     42        // ...load it...
     43        MetadataXMLFile new_metadata_xml_file = loadMetadataXMLFile(new_metadata_xml_file_file);
     44
     45        // ...and add the metadata
     46        new_metadata_xml_file.addMetadata(current_file, metadata_value);
    3047     }
    3148      }
     
    4259   {
    4360      // Build up a list of metadata values assigned to this file
    44       ArrayList metadata_values = new ArrayList();
     61      ArrayList metadata_values_all = new ArrayList();
    4562
    4663      // Look at each loaded metadata.xml file to see if any assign metadata to this file
     
    5168     // This metadata.xml file is only potentially applicable if it is above or at the same level as the file
    5269     if (file_directory.getAbsolutePath().startsWith(metadata_xml_file.getParentFile().getAbsolutePath())) {
    53         metadata_values.addAll(metadata_xml_file.getMetadataAssignedToFile(file));
     70        ArrayList metadata_values = metadata_xml_file.getMetadataAssignedToFile(file);
     71        for (int j = 0; j < metadata_values.size(); j++) {
     72           MetadataValue metadata_value = (MetadataValue) metadata_values.get(j);
     73
     74           // Overriding metadata: remove any values with this metadata element
     75           if (metadata_value.isAccumulatingMetadata() == false) {
     76          for (int k = metadata_values_all.size() - 1; k >= 0; k--) {
     77             if (((MetadataValue) metadata_values_all.get(k)).getMetadataElement().equals(metadata_value.getMetadataElement())) {
     78            metadata_values_all.remove(k);
     79             }
     80          }
     81           }
     82
     83           metadata_values_all.add(metadata_value);
     84        }
    5485     }
    5586      }
    5687
    57       return metadata_values;
     88      return metadata_values_all;
    5889   }
    5990
     
    75106
    76107
    77    static private void loadMetadataXMLFile(File metadata_xml_file_file)
     108   static private MetadataXMLFile loadMetadataXMLFile(File metadata_xml_file_file)
    78109   {
    79110      MetadataXMLFile metadata_xml_file = new MetadataXMLFile(metadata_xml_file_file.getAbsolutePath());
    80111      metadata_xml_files.add(metadata_xml_file);
     112      return metadata_xml_file;
    81113   }
    82114
     
    95127        // This metadata.xml file is only potentially applicable if it is above or at the same level as the file
    96128        if (current_file_directory.getAbsolutePath().startsWith(metadata_xml_file.getParentFile().getAbsolutePath())) {
    97            System.err.println("Applicable metadata.xml file: " + metadata_xml_file.getAbsolutePath());
    98129           metadata_xml_file.removeMetadata(current_file, metadata_value);
    99130        }
Note: See TracChangeset for help on using the changeset viewer.