Changeset 7827 for trunk/gli/src
- Timestamp:
- 2004-07-30T04:54:25+12:00 (20 years ago)
- 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 5 5 import java.util.*; 6 6 import org.greenstone.gatherer.util.Utility; 7 import org.greenstone.gatherer.util.XMLTools; 7 8 import org.w3c.dom.*; 8 9 … … 112 113 if (file_relative_path.equals(files_in_doc_xml_file.get(i))) { 113 114 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()); 115 116 break; 116 117 } -
trunk/gli/src/org/greenstone/gatherer/metadata/MetadataSet.java
r7822 r7827 4 4 import java.io.*; 5 5 import java.util.*; 6 import org.greenstone.gatherer.util.XMLTools; 6 7 import org.w3c.dom.*; 7 8 -
trunk/gli/src/org/greenstone/gatherer/metadata/MetadataValue.java
r7822 r7827 13 13 private MetadataElement metadata_element = null; 14 14 private MetadataValueTreeNode metadata_value_tree_node = null; 15 private boolean is_accumulating_metadata = false; 15 16 16 17 … … 73 74 74 75 76 public boolean isAccumulatingMetadata() 77 { 78 return is_accumulating_metadata; 79 } 80 81 75 82 public boolean isInheritedMetadata() 76 83 { 77 84 return (folder_metadata_inherited_from != null); 78 85 } 86 87 88 public void setIsAccumulatingMetadata(boolean is_accumulating_metadata) 89 { 90 this.is_accumulating_metadata = is_accumulating_metadata; 91 } 79 92 } 80 93 -
trunk/gli/src/org/greenstone/gatherer/metadata/MetadataValueTableModel.java
r7822 r7827 17 17 extends AbstractTableModel 18 18 { 19 /** The FileNodes this model is built for */ 20 private FileNode[] file_nodes = null; 19 21 /** The list of MetadataValueTableEntries in the table */ 20 22 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;27 23 28 24 static final private String[] COLUMN_NAMES = {"", Dictionary.get("Metadata.Element"), Dictionary.get("Metadata.Value")}; 29 25 30 26 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 } 34 110 35 111 this.file_nodes = file_nodes; 36 112 if (file_nodes != null && file_nodes.length > 0) { 37 113 // 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 } 178 117 } 179 118 180 119 181 120 private class MetadataValueTableModelBuilder 182 extends Thread 183 { 184 public boolean complete = false; 185 public MetadataValueTableEntry select_closest_metadata_value_table_entry = null; 186 121 { 187 122 public void run() 188 123 { 189 if (!Gatherer.c_man.ready()) {190 Gatherer.println("Not ready!");191 return;192 }193 194 124 // Build a list of MetadataValueTableEntries that represent the metadata asssigned to the selected files 195 metadata_value_table_entries.clear();196 125 boolean hid_extracted_metadata = false; 197 126 boolean has_inherited_metadata = false; … … 220 149 } 221 150 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 } 228 160 } 229 161 } … … 244 176 insertMetadataValueIntoTable(metadata_value_table_entry); 245 177 } 246 }247 248 // Processing complete249 complete = true;250 251 // If while processing we've been asked to select a certain metadata, do it now252 if (select_closest_metadata_value_table_entry != null) {253 selectClosestMetadataValueTableEntryWhenBuildingComplete(select_closest_metadata_value_table_entry);254 178 } 255 179 … … 320 244 } 321 245 } 322 323 324 /** Little utility class for selecting a row in the table, on the AWT thread. */325 private class TableRowSelectionTask326 extends Thread327 {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 }342 246 } -
trunk/gli/src/org/greenstone/gatherer/metadata/MetadataXMLFile.java
r7822 r7827 4 4 import java.io.*; 5 5 import java.util.*; 6 import org.greenstone.gatherer.util.XMLTools; 6 7 import org.w3c.dom.*; 7 8 … … 65 66 // Convert the file path into a regular expression that will match it 66 67 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; 67 78 68 79 // Read all the FileSet elements in the file … … 70 81 for (int i = 0; i < fileset_elements_nodelist.getLength(); i++) { 71 82 Element current_fileset_element = (Element) fileset_elements_nodelist.item(i); 72 boolean current_fileset_matches = false;73 83 74 84 // Check the FileName elements of the FileSet to see if we have a match … … 80 90 // Only exact matches can be extended with new metadata 81 91 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; 83 105 break; 84 106 } 85 107 } 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); 96 118 97 119 // 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 103 128 XMLTools.writeXMLFile(this, document); 104 129 } … … 181 206 } 182 207 183 // Add the new metadata value to the list184 208 MetadataValue metadata_value = new MetadataValue(metadata_element, metadata_value_tree_node); 185 209 if (is_folder_level_metadata && !file_relative_path.equals("")) { 186 210 metadata_value.inheritsMetadataFromFolder(metadata_xml_file_directory); 187 211 } 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 188 219 metadata_values.add(metadata_value); 189 220 } … … 212 243 // Convert the file path into a regular expression that will match it 213 244 String file_path_regexp = file_relative_path.replaceAll("\\.", "\\\\."); 245 if (file_relative_path.equals("")) { 246 file_path_regexp = DIRECTORY_FILENAME; 247 } 214 248 215 249 // Read all the FileSet elements in the file … … 261 295 } 262 296 263 // Write the metadata.xml file297 // Rewrite the metadata.xml file 264 298 XMLTools.writeXMLFile(this, document); 265 299 } -
trunk/gli/src/org/greenstone/gatherer/metadata/MetadataXMLFileManager.java
r7822 r7827 5 5 import java.util.*; 6 6 import org.greenstone.gatherer.file.FileNode; 7 import org.greenstone.gatherer.util.Utility; 8 import org.greenstone.gatherer.util.XMLTools; 7 9 8 10 … … 20 22 21 23 // Find which metadata.xml file needs editing 24 boolean applicable_metadata_xml_file_found = false; 22 25 File current_file_directory = (current_file.isDirectory() ? current_file : current_file.getParentFile()); 23 26 for (int j = 0; j < metadata_xml_files.size(); j++) { … … 26 29 // This metadata.xml file is only potentially applicable if it is above or at the same level as the file 27 30 if (current_file_directory.getAbsolutePath().startsWith(metadata_xml_file.getParentFile().getAbsolutePath())) { 31 applicable_metadata_xml_file_found = true; 28 32 metadata_xml_file.addMetadata(current_file, metadata_value); 29 33 } 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); 30 47 } 31 48 } … … 42 59 { 43 60 // Build up a list of metadata values assigned to this file 44 ArrayList metadata_values = new ArrayList();61 ArrayList metadata_values_all = new ArrayList(); 45 62 46 63 // Look at each loaded metadata.xml file to see if any assign metadata to this file … … 51 68 // This metadata.xml file is only potentially applicable if it is above or at the same level as the file 52 69 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 } 54 85 } 55 86 } 56 87 57 return metadata_values ;88 return metadata_values_all; 58 89 } 59 90 … … 75 106 76 107 77 static private voidloadMetadataXMLFile(File metadata_xml_file_file)108 static private MetadataXMLFile loadMetadataXMLFile(File metadata_xml_file_file) 78 109 { 79 110 MetadataXMLFile metadata_xml_file = new MetadataXMLFile(metadata_xml_file_file.getAbsolutePath()); 80 111 metadata_xml_files.add(metadata_xml_file); 112 return metadata_xml_file; 81 113 } 82 114 … … 95 127 // This metadata.xml file is only potentially applicable if it is above or at the same level as the file 96 128 if (current_file_directory.getAbsolutePath().startsWith(metadata_xml_file.getParentFile().getAbsolutePath())) { 97 System.err.println("Applicable metadata.xml file: " + metadata_xml_file.getAbsolutePath());98 129 metadata_xml_file.removeMetadata(current_file, metadata_value); 99 130 }
Note:
See TracChangeset
for help on using the changeset viewer.