[8164] | 1 | /**
|
---|
| 2 | *############################################################################
|
---|
| 3 | * A component of the Greenstone Librarian Interface, part of the Greenstone
|
---|
| 4 | * digital library suite from the New Zealand Digital Library Project at the
|
---|
| 5 | * University of Waikato, New Zealand.
|
---|
| 6 | *
|
---|
| 7 | * Author: Michael Dewsnip, NZDL Project, University of Waikato, NZ
|
---|
| 8 | *
|
---|
| 9 | * Copyright (C) 2004 New Zealand Digital Library Project
|
---|
| 10 | *
|
---|
| 11 | * This program is free software; you can redistribute it and/or modify
|
---|
| 12 | * it under the terms of the GNU General Public License as published by
|
---|
| 13 | * the Free Software Foundation; either version 2 of the License, or
|
---|
| 14 | * (at your option) any later version.
|
---|
| 15 | *
|
---|
| 16 | * This program is distributed in the hope that it will be useful,
|
---|
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 19 | * GNU General Public License for more details.
|
---|
| 20 | *
|
---|
| 21 | * You should have received a copy of the GNU General Public License
|
---|
| 22 | * along with this program; if not, write to the Free Software
|
---|
| 23 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
| 24 | *############################################################################
|
---|
| 25 | */
|
---|
| 26 |
|
---|
[7822] | 27 | package org.greenstone.gatherer.metadata;
|
---|
| 28 |
|
---|
| 29 |
|
---|
| 30 | import java.io.*;
|
---|
| 31 | import java.util.*;
|
---|
[8240] | 32 | import org.greenstone.gatherer.DebugStream;
|
---|
[7827] | 33 | import org.greenstone.gatherer.util.XMLTools;
|
---|
[7822] | 34 | import org.w3c.dom.*;
|
---|
| 35 |
|
---|
| 36 |
|
---|
| 37 | /** This class represents one metadata.xml file */
|
---|
| 38 | public class MetadataXMLFile
|
---|
[7978] | 39 | extends File
|
---|
[7822] | 40 | {
|
---|
[7978] | 41 | static final private String DESCRIPTION_ELEMENT = "Description";
|
---|
| 42 | static final private String DIRECTORY_FILENAME = ".*";
|
---|
| 43 | static final private String FILENAME_ELEMENT = "FileName";
|
---|
| 44 | static final private String FILESET_ELEMENT = "FileSet";
|
---|
| 45 | static final private String METADATA_ELEMENT = "Metadata";
|
---|
[7822] | 46 |
|
---|
| 47 |
|
---|
[7978] | 48 | public MetadataXMLFile(String metadata_xml_file_path)
|
---|
| 49 | {
|
---|
| 50 | super(metadata_xml_file_path);
|
---|
| 51 | }
|
---|
[7822] | 52 |
|
---|
| 53 |
|
---|
[10479] | 54 | public void addMetadata(File file, ArrayList metadata_values)
|
---|
[7978] | 55 | {
|
---|
| 56 | // Parse the metadata.xml file
|
---|
| 57 | Document document = XMLTools.parseXMLFile(this);
|
---|
| 58 | if (document == null) {
|
---|
| 59 | System.err.println("Error: Could not parse metadata.xml file " + getAbsolutePath());
|
---|
| 60 | return;
|
---|
| 61 | }
|
---|
[7822] | 62 |
|
---|
[7978] | 63 | // Determine the file's path relative to the location of the metadata.xml file
|
---|
[10479] | 64 | String metadata_xml_file_directory_path = getParentFile().getAbsolutePath();
|
---|
| 65 | String file_relative_path = file.getAbsolutePath().substring(metadata_xml_file_directory_path.length());
|
---|
[7978] | 66 | if (file_relative_path.startsWith(File.separator)) {
|
---|
| 67 | file_relative_path = file_relative_path.substring(File.separator.length());
|
---|
| 68 | }
|
---|
[7822] | 69 |
|
---|
[8433] | 70 | // Form a regular expression that specifies the scope of the metadata
|
---|
[9856] | 71 | String file_path_regexp;
|
---|
| 72 | if (file_relative_path.equals("")) {
|
---|
| 73 | // Special case for matching all files in the directory
|
---|
[7978] | 74 | file_path_regexp = DIRECTORY_FILENAME;
|
---|
| 75 | }
|
---|
[8433] | 76 | else {
|
---|
| 77 | // Convert the file path into a regular expression that will match it
|
---|
[9856] | 78 | file_path_regexp = MetadataTools.getRegularExpressionThatMatchesFilePath(file_relative_path);
|
---|
[8433] | 79 | }
|
---|
[7822] | 80 |
|
---|
[10479] | 81 | // Find the appropriate FileSet element for this file
|
---|
| 82 | Element appropriate_fileset_element = null;
|
---|
[9856] | 83 |
|
---|
[7978] | 84 | // Read all the FileSet elements in the file
|
---|
| 85 | NodeList fileset_elements_nodelist = document.getElementsByTagName(FILESET_ELEMENT);
|
---|
| 86 | for (int i = 0; i < fileset_elements_nodelist.getLength(); i++) {
|
---|
| 87 | Element current_fileset_element = (Element) fileset_elements_nodelist.item(i);
|
---|
[7822] | 88 |
|
---|
[7978] | 89 | // Check the FileName elements of the FileSet to see if we have a match
|
---|
| 90 | NodeList filename_elements_nodelist = current_fileset_element.getElementsByTagName(FILENAME_ELEMENT);
|
---|
| 91 | for (int j = 0; j < filename_elements_nodelist.getLength(); j++) {
|
---|
| 92 | Element current_filename_element = (Element) filename_elements_nodelist.item(j);
|
---|
| 93 | String current_filename_element_value = XMLTools.getElementTextValue(current_filename_element);
|
---|
[7822] | 94 |
|
---|
[7978] | 95 | // Only exact matches can be extended with new metadata
|
---|
| 96 | if (current_filename_element_value.equals(file_path_regexp)) {
|
---|
[10479] | 97 | appropriate_fileset_element = current_fileset_element;
|
---|
| 98 | break;
|
---|
| 99 | }
|
---|
| 100 | }
|
---|
| 101 | }
|
---|
[7827] | 102 |
|
---|
[10479] | 103 | // If no appropriate FileSet element exists create a new one for this file
|
---|
| 104 | if (appropriate_fileset_element == null) {
|
---|
[10487] | 105 | DebugStream.println("Creating new FileSet element for file since none exists...");
|
---|
[10479] | 106 | appropriate_fileset_element = document.createElement(FILESET_ELEMENT);
|
---|
[9524] | 107 |
|
---|
[10479] | 108 | Element new_filename_element = document.createElement(FILENAME_ELEMENT);
|
---|
| 109 | new_filename_element.appendChild(document.createTextNode(file_path_regexp));
|
---|
| 110 | appropriate_fileset_element.appendChild(new_filename_element);
|
---|
[9524] | 111 |
|
---|
[10479] | 112 | Element new_description_element = document.createElement(DESCRIPTION_ELEMENT);
|
---|
| 113 | appropriate_fileset_element.appendChild(new_description_element);
|
---|
[9524] | 114 |
|
---|
[10479] | 115 | document.getDocumentElement().appendChild(appropriate_fileset_element);
|
---|
| 116 | }
|
---|
| 117 |
|
---|
| 118 | // Add each of the metadata values to the FileSet's Description element
|
---|
| 119 | Element description_element = (Element) appropriate_fileset_element.getElementsByTagName(DESCRIPTION_ELEMENT).item(0);
|
---|
| 120 | for (int i = 0; i < metadata_values.size(); i++) {
|
---|
| 121 | MetadataValue metadata_value = (MetadataValue) metadata_values.get(i);
|
---|
| 122 | String metadata_element_name_full = metadata_value.getMetadataElement().getFullName();
|
---|
| 123 |
|
---|
| 124 | // Remove any characters that are invalid in XML
|
---|
| 125 | String metadata_value_string = XMLTools.removeInvalidCharacters(metadata_value.getFullValue());
|
---|
| 126 |
|
---|
| 127 | // Square brackets need to be escaped because they are a special character in Greenstone
|
---|
| 128 | metadata_value_string = metadata_value_string.replaceAll("\\[", "[");
|
---|
| 129 | metadata_value_string = metadata_value_string.replaceAll("\\]", "]");
|
---|
| 130 |
|
---|
| 131 | // Check if this piece of metadata has already been assigned to this FileSet element
|
---|
| 132 | boolean metadata_already_assigned = false;
|
---|
| 133 | NodeList metadata_elements_nodelist = description_element.getElementsByTagName(METADATA_ELEMENT);
|
---|
| 134 | for (int k = 0; k < metadata_elements_nodelist.getLength(); k++) {
|
---|
| 135 | Element current_metadata_element = (Element) metadata_elements_nodelist.item(k);
|
---|
| 136 |
|
---|
| 137 | // Check if the metadata element name matches
|
---|
| 138 | String current_metadata_element_name_full = current_metadata_element.getAttribute("name");
|
---|
| 139 | if (current_metadata_element_name_full.equals(metadata_element_name_full)) {
|
---|
| 140 | // Check if the metadata element value matches
|
---|
| 141 | String current_metadata_value_string = XMLTools.getElementTextValue(current_metadata_element);
|
---|
| 142 | if (current_metadata_value_string.equals(metadata_value_string)) {
|
---|
[9524] | 143 | // Metadata already assigned
|
---|
| 144 | metadata_already_assigned = true;
|
---|
| 145 | break;
|
---|
[8711] | 146 | }
|
---|
[10479] | 147 | }
|
---|
| 148 | }
|
---|
[8711] | 149 |
|
---|
[10479] | 150 | // If the piece of metadata hasn't already been assigned, add it now
|
---|
| 151 | if (!metadata_already_assigned) {
|
---|
| 152 | // Create a new Metadata element to record this metadata
|
---|
| 153 | Element new_metadata_element = document.createElement(METADATA_ELEMENT);
|
---|
| 154 | new_metadata_element.setAttribute("name", metadata_value.getMetadataElement().getFullName());
|
---|
| 155 | new_metadata_element.setAttribute("mode", (metadata_value.isAccumulatingMetadata() ? "accumulate" : "override"));
|
---|
| 156 | new_metadata_element.appendChild(document.createTextNode(metadata_value_string));
|
---|
[8711] | 157 |
|
---|
[10479] | 158 | // Accumulating metadata: add at the end
|
---|
| 159 | if (metadata_value.isAccumulatingMetadata()) {
|
---|
| 160 | description_element.appendChild(new_metadata_element);
|
---|
[7978] | 161 | }
|
---|
[10479] | 162 | // Override metadata: add at the start (so it overrides inherited metadata without affecting other assigned metadata)
|
---|
| 163 | else {
|
---|
| 164 | description_element.insertBefore(new_metadata_element, description_element.getFirstChild());
|
---|
| 165 | }
|
---|
[7822] | 166 | }
|
---|
[7978] | 167 | }
|
---|
[7822] | 168 |
|
---|
[7978] | 169 | // Rewrite the metadata.xml file
|
---|
| 170 | XMLTools.writeXMLFile(this, document);
|
---|
| 171 | }
|
---|
[7822] | 172 |
|
---|
| 173 |
|
---|
[7978] | 174 | public ArrayList getMetadataAssignedToFile(File file)
|
---|
| 175 | {
|
---|
| 176 | // Parse the metadata.xml file
|
---|
| 177 | Document document = XMLTools.parseXMLFile(this);
|
---|
| 178 | if (document == null) {
|
---|
| 179 | System.err.println("Error: Could not parse metadata.xml file " + getAbsolutePath());
|
---|
[8667] | 180 | return new ArrayList();
|
---|
[7978] | 181 | }
|
---|
[7822] | 182 |
|
---|
[7978] | 183 | // Determine the file's path relative to the location of the metadata.xml file
|
---|
| 184 | File metadata_xml_file_directory = getParentFile();
|
---|
| 185 | String file_relative_path = file.getAbsolutePath().substring(metadata_xml_file_directory.getAbsolutePath().length());
|
---|
| 186 | if (file_relative_path.startsWith(File.separator)) {
|
---|
| 187 | file_relative_path = file_relative_path.substring(File.separator.length());
|
---|
| 188 | }
|
---|
[7822] | 189 |
|
---|
[7978] | 190 | // Build up a list of metadata assigned to this file
|
---|
| 191 | ArrayList metadata_values = new ArrayList();
|
---|
[7822] | 192 |
|
---|
[7978] | 193 | // Read all the FileSet elements in the file
|
---|
| 194 | NodeList fileset_elements_nodelist = document.getElementsByTagName(FILESET_ELEMENT);
|
---|
| 195 | for (int i = 0; i < fileset_elements_nodelist.getLength(); i++) {
|
---|
| 196 | Element current_fileset_element = (Element) fileset_elements_nodelist.item(i);
|
---|
| 197 | boolean current_fileset_matches = false;
|
---|
[11301] | 198 | boolean is_one_file_only_metadata = true;
|
---|
[8135] | 199 | File folder_metadata_inherited_from = null;
|
---|
[7822] | 200 |
|
---|
[7978] | 201 | // Check the FileName elements of the FileSet to see if we have a match
|
---|
| 202 | NodeList filename_elements_nodelist = current_fileset_element.getElementsByTagName(FILENAME_ELEMENT);
|
---|
| 203 | for (int j = 0; j < filename_elements_nodelist.getLength(); j++) {
|
---|
| 204 | Element current_filename_element = (Element) filename_elements_nodelist.item(j);
|
---|
| 205 | String current_filename_element_value = XMLTools.getElementTextValue(current_filename_element);
|
---|
[7822] | 206 |
|
---|
[11301] | 207 | // Does this fileset specify metadata for one file only?
|
---|
| 208 | is_one_file_only_metadata = true;
|
---|
[11404] | 209 | if (current_filename_element_value.indexOf("*") != -1 && !current_filename_element_value.equals(DIRECTORY_FILENAME)) {
|
---|
| 210 | // No, it specifies metadata for multiple files (but not all the files in the directory)
|
---|
[11301] | 211 | is_one_file_only_metadata = false;
|
---|
| 212 | }
|
---|
| 213 |
|
---|
[8135] | 214 | // This fileset specifies metadata for the file
|
---|
[7978] | 215 | if (file_relative_path.matches(current_filename_element_value)) {
|
---|
| 216 | current_fileset_matches = true;
|
---|
[8135] | 217 | if (!file_relative_path.equals("") && current_filename_element_value.equals(DIRECTORY_FILENAME)) {
|
---|
| 218 | folder_metadata_inherited_from = metadata_xml_file_directory;
|
---|
| 219 | }
|
---|
[7978] | 220 | break;
|
---|
| 221 | }
|
---|
[8135] | 222 |
|
---|
| 223 | // This fileset specifies metadata for the folder the file is in
|
---|
| 224 | if (file_relative_path.startsWith(current_filename_element_value + File.separator)) {
|
---|
| 225 | current_fileset_matches = true;
|
---|
| 226 | folder_metadata_inherited_from = new File(metadata_xml_file_directory, current_filename_element_value);
|
---|
| 227 | break;
|
---|
| 228 | }
|
---|
[7822] | 229 | }
|
---|
| 230 |
|
---|
[7978] | 231 | // The FileSet doesn't apply, so move onto the next one
|
---|
| 232 | if (current_fileset_matches == false) {
|
---|
| 233 | continue;
|
---|
| 234 | }
|
---|
[7822] | 235 |
|
---|
[7978] | 236 | // Read all the Metadata elements in the fileset
|
---|
| 237 | NodeList metadata_elements_nodelist = current_fileset_element.getElementsByTagName(METADATA_ELEMENT);
|
---|
| 238 | for (int k = 0; k < metadata_elements_nodelist.getLength(); k++) {
|
---|
| 239 | Element current_metadata_element = (Element) metadata_elements_nodelist.item(k);
|
---|
[8140] | 240 | String metadata_element_name_full = current_metadata_element.getAttribute("name");
|
---|
[8371] | 241 | String metadata_set_namespace = MetadataTools.getMetadataSetNamespace(metadata_element_name_full);
|
---|
[7822] | 242 |
|
---|
[8371] | 243 | // Ignore legacy crap
|
---|
| 244 | if (metadata_set_namespace.equals("hidden")) {
|
---|
| 245 | continue;
|
---|
| 246 | }
|
---|
| 247 |
|
---|
[7978] | 248 | MetadataSet metadata_set = MetadataSetManager.getMetadataSet(metadata_set_namespace);
|
---|
| 249 | if (metadata_set == null) {
|
---|
[8371] | 250 | // The metadata set isn't loaded, so give the option of mapping the element into a loaded set
|
---|
[8140] | 251 | String target_metadata_element_name_full = MetadataSetManager.mapUnloadedMetadataElement(metadata_element_name_full);
|
---|
| 252 | if (target_metadata_element_name_full == null || target_metadata_element_name_full.equals("")) {
|
---|
| 253 | // Skip this element if we still don't have a loaded element for it
|
---|
| 254 | continue;
|
---|
| 255 | }
|
---|
| 256 |
|
---|
| 257 | metadata_element_name_full = target_metadata_element_name_full;
|
---|
| 258 | metadata_set_namespace = MetadataTools.getMetadataSetNamespace(metadata_element_name_full);
|
---|
| 259 | metadata_set = MetadataSetManager.getMetadataSet(metadata_set_namespace);
|
---|
[7978] | 260 | }
|
---|
[7822] | 261 |
|
---|
[8163] | 262 | MetadataElement metadata_element = MetadataTools.getMetadataElementWithName(metadata_element_name_full);
|
---|
[7822] | 263 |
|
---|
[7978] | 264 | // If the element doesn't exist in the metadata set, we're not interested
|
---|
| 265 | if (metadata_element == null) {
|
---|
| 266 | continue;
|
---|
| 267 | }
|
---|
[7822] | 268 |
|
---|
[8124] | 269 | // Square brackets need to be escaped because they are a special character in Greenstone
|
---|
[8655] | 270 | String metadata_value_string = XMLTools.getElementTextValue(current_metadata_element);
|
---|
| 271 | metadata_value_string = metadata_value_string.replaceAll("[", "[");
|
---|
| 272 | metadata_value_string = metadata_value_string.replaceAll("]", "]");
|
---|
[8124] | 273 |
|
---|
[8655] | 274 | MetadataValueTreeNode metadata_value_tree_node = metadata_element.getMetadataValueTreeNode(metadata_value_string);
|
---|
[7822] | 275 |
|
---|
[8140] | 276 | // If there is no metadata value tree node for this value, create it
|
---|
[7978] | 277 | if (metadata_value_tree_node == null) {
|
---|
[8655] | 278 | DebugStream.println("Note: No value tree node for metadata value \"" + metadata_value_string + "\"");
|
---|
| 279 | metadata_element.addMetadataValue(metadata_value_string);
|
---|
| 280 | metadata_value_tree_node = metadata_element.getMetadataValueTreeNode(metadata_value_string);
|
---|
[7978] | 281 | }
|
---|
[7822] | 282 |
|
---|
[7978] | 283 | MetadataValue metadata_value = new MetadataValue(metadata_element, metadata_value_tree_node);
|
---|
[8135] | 284 | metadata_value.inheritsMetadataFromFolder(folder_metadata_inherited_from);
|
---|
[11301] | 285 | metadata_value.setIsOneFileOnlyMetadata(is_one_file_only_metadata);
|
---|
[7827] | 286 |
|
---|
[7978] | 287 | // Is this accumulating metadata?
|
---|
| 288 | if (current_metadata_element.getAttribute("mode").equals("accumulate")) {
|
---|
| 289 | metadata_value.setIsAccumulatingMetadata(true);
|
---|
| 290 | }
|
---|
| 291 |
|
---|
| 292 | // Add the new metadata value to the list
|
---|
| 293 | metadata_values.add(metadata_value);
|
---|
[7827] | 294 | }
|
---|
[7978] | 295 | }
|
---|
[7827] | 296 |
|
---|
[7978] | 297 | return metadata_values;
|
---|
| 298 | }
|
---|
[7822] | 299 |
|
---|
| 300 |
|
---|
[10479] | 301 | public void removeMetadata(File file, ArrayList metadata_values)
|
---|
[7978] | 302 | {
|
---|
| 303 | // Parse the metadata.xml file
|
---|
| 304 | Document document = XMLTools.parseXMLFile(this);
|
---|
| 305 | if (document == null) {
|
---|
| 306 | System.err.println("Error: Could not parse metadata.xml file " + getAbsolutePath());
|
---|
| 307 | return;
|
---|
| 308 | }
|
---|
[7822] | 309 |
|
---|
[7978] | 310 | // Determine the file's path relative to the location of the metadata.xml file
|
---|
[10479] | 311 | String metadata_xml_file_directory_path = getParentFile().getAbsolutePath();
|
---|
| 312 | String file_relative_path = file.getAbsolutePath().substring(metadata_xml_file_directory_path.length());
|
---|
[7978] | 313 | if (file_relative_path.startsWith(File.separator)) {
|
---|
| 314 | file_relative_path = file_relative_path.substring(File.separator.length());
|
---|
| 315 | }
|
---|
[7822] | 316 |
|
---|
[8433] | 317 | // Form a regular expression that specifies the scope of the metadata
|
---|
[9856] | 318 | String file_path_regexp;
|
---|
| 319 | if (file_relative_path.equals("")) {
|
---|
| 320 | // Special case for matching all files in the directory
|
---|
[7978] | 321 | file_path_regexp = DIRECTORY_FILENAME;
|
---|
| 322 | }
|
---|
[8433] | 323 | else {
|
---|
| 324 | // Convert the file path into a regular expression that will match it
|
---|
[9856] | 325 | file_path_regexp = MetadataTools.getRegularExpressionThatMatchesFilePath(file_relative_path);
|
---|
[8433] | 326 | }
|
---|
[7822] | 327 |
|
---|
[10479] | 328 | // Find the appropriate FileSet element for this file
|
---|
| 329 | Element appropriate_fileset_element = null;
|
---|
[9856] | 330 |
|
---|
[7978] | 331 | // Read all the FileSet elements in the file
|
---|
| 332 | NodeList fileset_elements_nodelist = document.getElementsByTagName(FILESET_ELEMENT);
|
---|
| 333 | for (int i = 0; i < fileset_elements_nodelist.getLength(); i++) {
|
---|
| 334 | Element current_fileset_element = (Element) fileset_elements_nodelist.item(i);
|
---|
[7822] | 335 |
|
---|
[7978] | 336 | // Check the FileName elements of the FileSet to see if we have a match
|
---|
| 337 | NodeList filename_elements_nodelist = current_fileset_element.getElementsByTagName(FILENAME_ELEMENT);
|
---|
| 338 | for (int j = 0; j < filename_elements_nodelist.getLength(); j++) {
|
---|
| 339 | Element current_filename_element = (Element) filename_elements_nodelist.item(j);
|
---|
| 340 | String current_filename_element_value = XMLTools.getElementTextValue(current_filename_element);
|
---|
[7822] | 341 |
|
---|
[10479] | 342 | // Only exact matches can be extended with new metadata
|
---|
[7978] | 343 | if (current_filename_element_value.equals(file_path_regexp)) {
|
---|
[10479] | 344 | appropriate_fileset_element = current_fileset_element;
|
---|
[7978] | 345 | break;
|
---|
| 346 | }
|
---|
| 347 | }
|
---|
[10479] | 348 | }
|
---|
[7822] | 349 |
|
---|
[11129] | 350 | // If no appropriate FileSet element exists the metadata isn't assigned in this metadata.xml file
|
---|
[10479] | 351 | if (appropriate_fileset_element == null) {
|
---|
[11129] | 352 | DebugStream.println("Note: No appropriate FileSet element found when removing metadata from " + this);
|
---|
[10479] | 353 | return;
|
---|
| 354 | }
|
---|
[7822] | 355 |
|
---|
[10479] | 356 | // Remove each of the metadata values from the FileSet's Description element
|
---|
| 357 | for (int i = 0; i < metadata_values.size(); i++) {
|
---|
| 358 | MetadataValue metadata_value = (MetadataValue) metadata_values.get(i);
|
---|
| 359 |
|
---|
| 360 | // Remove any characters that are invalid in XML
|
---|
| 361 | String metadata_value_string = XMLTools.removeInvalidCharacters(metadata_value.getFullValue());
|
---|
| 362 |
|
---|
| 363 | // Square brackets need to be escaped because they are a special character in Greenstone
|
---|
| 364 | metadata_value_string = metadata_value_string.replaceAll("\\[", "[");
|
---|
| 365 | metadata_value_string = metadata_value_string.replaceAll("\\]", "]");
|
---|
| 366 |
|
---|
[7978] | 367 | // Find the Metadata element to delete from the fileset
|
---|
| 368 | String metadata_element_name_full = metadata_value.getMetadataElement().getFullName();
|
---|
[10479] | 369 | NodeList metadata_elements_nodelist = appropriate_fileset_element.getElementsByTagName(METADATA_ELEMENT);
|
---|
[7978] | 370 | for (int k = 0; k < metadata_elements_nodelist.getLength(); k++) {
|
---|
| 371 | Element current_metadata_element = (Element) metadata_elements_nodelist.item(k);
|
---|
[7822] | 372 |
|
---|
[7978] | 373 | // Check the metadata element name matches
|
---|
| 374 | String current_metadata_element_name_full = current_metadata_element.getAttribute("name");
|
---|
[10479] | 375 | if (current_metadata_element_name_full.equals(metadata_element_name_full)) {
|
---|
| 376 | // Check the metadata element value matches
|
---|
| 377 | String current_metadata_value_string = XMLTools.getElementTextValue(current_metadata_element);
|
---|
| 378 | if (current_metadata_value_string.equals(metadata_value_string)) {
|
---|
| 379 | // Remove this Metadata element
|
---|
| 380 | current_metadata_element.getParentNode().removeChild(current_metadata_element);
|
---|
[11439] | 381 |
|
---|
| 382 | // If there are no Metadata elements left now, remove the (empty) FileSet element
|
---|
| 383 | if (metadata_elements_nodelist.getLength() == 0) {
|
---|
| 384 | appropriate_fileset_element.getParentNode().removeChild(appropriate_fileset_element);
|
---|
| 385 | }
|
---|
| 386 |
|
---|
[10479] | 387 | break;
|
---|
| 388 | }
|
---|
[7978] | 389 | }
|
---|
[7822] | 390 | }
|
---|
[7978] | 391 | }
|
---|
[7822] | 392 |
|
---|
[7978] | 393 | // Rewrite the metadata.xml file
|
---|
| 394 | XMLTools.writeXMLFile(this, document);
|
---|
| 395 | }
|
---|
[8140] | 396 |
|
---|
| 397 |
|
---|
[9856] | 398 | public void replaceMetadata(File file, MetadataValue old_metadata_value, MetadataValue new_metadata_value)
|
---|
| 399 | {
|
---|
| 400 | // Parse the metadata.xml file
|
---|
| 401 | Document document = XMLTools.parseXMLFile(this);
|
---|
| 402 | if (document == null) {
|
---|
| 403 | System.err.println("Error: Could not parse metadata.xml file " + getAbsolutePath());
|
---|
| 404 | return;
|
---|
| 405 | }
|
---|
| 406 |
|
---|
| 407 | // Determine the file's path relative to the location of the metadata.xml file
|
---|
[10479] | 408 | String metadata_xml_file_directory_path = getParentFile().getAbsolutePath();
|
---|
| 409 | String file_relative_path = file.getAbsolutePath().substring(metadata_xml_file_directory_path.length());
|
---|
[9856] | 410 | if (file_relative_path.startsWith(File.separator)) {
|
---|
| 411 | file_relative_path = file_relative_path.substring(File.separator.length());
|
---|
| 412 | }
|
---|
| 413 |
|
---|
| 414 | // Form a regular expression that specifies the scope of the metadata
|
---|
| 415 | String file_path_regexp;
|
---|
| 416 | if (file_relative_path.equals("")) {
|
---|
| 417 | // Special case for matching all files in the directory
|
---|
| 418 | file_path_regexp = DIRECTORY_FILENAME;
|
---|
| 419 | }
|
---|
| 420 | else {
|
---|
| 421 | // Convert the file path into a regular expression that will match it
|
---|
| 422 | file_path_regexp = MetadataTools.getRegularExpressionThatMatchesFilePath(file_relative_path);
|
---|
| 423 | }
|
---|
| 424 |
|
---|
| 425 | // Remove any characters that are invalid in XML
|
---|
| 426 | String old_metadata_value_string = XMLTools.removeInvalidCharacters(old_metadata_value.getFullValue());
|
---|
| 427 | String new_metadata_value_string = XMLTools.removeInvalidCharacters(new_metadata_value.getFullValue());
|
---|
| 428 |
|
---|
| 429 | // Square brackets need to be escaped because they are a special character in Greenstone
|
---|
| 430 | old_metadata_value_string = old_metadata_value_string.replaceAll("\\[", "[");
|
---|
| 431 | old_metadata_value_string = old_metadata_value_string.replaceAll("\\]", "]");
|
---|
| 432 | new_metadata_value_string = new_metadata_value_string.replaceAll("\\[", "[");
|
---|
| 433 | new_metadata_value_string = new_metadata_value_string.replaceAll("\\]", "]");
|
---|
| 434 |
|
---|
| 435 | // Read all the FileSet elements in the file
|
---|
| 436 | NodeList fileset_elements_nodelist = document.getElementsByTagName(FILESET_ELEMENT);
|
---|
| 437 | for (int i = 0; i < fileset_elements_nodelist.getLength(); i++) {
|
---|
| 438 | Element current_fileset_element = (Element) fileset_elements_nodelist.item(i);
|
---|
| 439 | boolean current_fileset_matches = false;
|
---|
| 440 |
|
---|
| 441 | // Check the FileName elements of the FileSet to see if we have a match
|
---|
| 442 | NodeList filename_elements_nodelist = current_fileset_element.getElementsByTagName(FILENAME_ELEMENT);
|
---|
| 443 | for (int j = 0; j < filename_elements_nodelist.getLength(); j++) {
|
---|
| 444 | Element current_filename_element = (Element) filename_elements_nodelist.item(j);
|
---|
| 445 | String current_filename_element_value = XMLTools.getElementTextValue(current_filename_element);
|
---|
| 446 |
|
---|
| 447 | // Only exact matches can be edited
|
---|
| 448 | if (current_filename_element_value.equals(file_path_regexp)) {
|
---|
| 449 | current_fileset_matches = true;
|
---|
| 450 | break;
|
---|
| 451 | }
|
---|
| 452 | }
|
---|
| 453 |
|
---|
| 454 | // The FileSet doesn't apply, so move onto the next one
|
---|
| 455 | if (current_fileset_matches == false) {
|
---|
| 456 | continue;
|
---|
| 457 | }
|
---|
| 458 |
|
---|
| 459 | // Each metadata value is only allowed to be assigned once
|
---|
| 460 | boolean new_metadata_value_already_exists = false;
|
---|
| 461 | Element metadata_element_to_edit = null;
|
---|
| 462 |
|
---|
| 463 | // Find the Metadata element to replace in the fileset
|
---|
| 464 | String metadata_element_name_full = old_metadata_value.getMetadataElement().getFullName();
|
---|
| 465 | NodeList metadata_elements_nodelist = current_fileset_element.getElementsByTagName(METADATA_ELEMENT);
|
---|
| 466 | for (int k = 0; k < metadata_elements_nodelist.getLength(); k++) {
|
---|
| 467 | Element current_metadata_element = (Element) metadata_elements_nodelist.item(k);
|
---|
| 468 |
|
---|
| 469 | // Check the metadata element name matches
|
---|
| 470 | String current_metadata_element_name_full = current_metadata_element.getAttribute("name");
|
---|
| 471 | if (!current_metadata_element_name_full.equals(metadata_element_name_full)) {
|
---|
| 472 | continue;
|
---|
| 473 | }
|
---|
| 474 |
|
---|
| 475 | // Check the new metadata value doesn't already exist
|
---|
| 476 | String current_metadata_value_string = XMLTools.getElementTextValue(current_metadata_element);
|
---|
| 477 | if (current_metadata_value_string.equals(new_metadata_value_string)) {
|
---|
| 478 | new_metadata_value_already_exists = true;
|
---|
| 479 | }
|
---|
| 480 |
|
---|
| 481 | // Check the metadata element value matches
|
---|
| 482 | if (current_metadata_value_string.equals(old_metadata_value_string)) {
|
---|
| 483 | metadata_element_to_edit = current_metadata_element;
|
---|
| 484 | }
|
---|
| 485 | }
|
---|
| 486 |
|
---|
| 487 | // If the new metadata value already existed, remove the original value
|
---|
| 488 | if (new_metadata_value_already_exists) {
|
---|
| 489 | metadata_element_to_edit.getParentNode().removeChild(metadata_element_to_edit);
|
---|
| 490 | }
|
---|
| 491 | // Otherwise replace the old value with the new value
|
---|
[12713] | 492 | // Ensure metadata_element_to_edit isn't null (may occur when multiple files are selected)
|
---|
| 493 | else if (metadata_element_to_edit != null) {
|
---|
[9856] | 494 | XMLTools.setElementTextValue(metadata_element_to_edit, new_metadata_value_string);
|
---|
| 495 | }
|
---|
| 496 | }
|
---|
| 497 |
|
---|
| 498 | // Rewrite the metadata.xml file
|
---|
| 499 | XMLTools.writeXMLFile(this, document);
|
---|
| 500 | }
|
---|
| 501 |
|
---|
| 502 |
|
---|
[8142] | 503 | /**
|
---|
[13278] | 504 | * Every metadata.xml file must be skimmed when a collection is opened, for three very important reasons:
|
---|
| 505 | * - To handle any non-namespaced metadata in the metadata.xml files (this is mapped and the files rewritten)
|
---|
| 506 | * - To get a complete list of the metadata elements in the collection (used in Design and Format panes)
|
---|
| 507 | * - To build complete and accurate metadata value trees (used in the Enrich pane)
|
---|
[8142] | 508 | */
|
---|
[8140] | 509 | public void skimFile()
|
---|
| 510 | {
|
---|
| 511 | boolean file_changed = false;
|
---|
| 512 |
|
---|
| 513 | // Parse the metadata.xml file
|
---|
[8236] | 514 | DebugStream.println("Skimming metadata.xml file " + this + "...");
|
---|
[8140] | 515 | Document document = XMLTools.parseXMLFile(this);
|
---|
| 516 | if (document == null) {
|
---|
| 517 | System.err.println("Error: Could not parse metadata.xml file " + getAbsolutePath());
|
---|
| 518 | return;
|
---|
| 519 | }
|
---|
| 520 |
|
---|
| 521 | // Read all the Metadata elements in the file
|
---|
[11240] | 522 | HashMap target_metadata_element_name_attrs_cache = new HashMap();
|
---|
[8140] | 523 | NodeList metadata_elements_nodelist = document.getElementsByTagName(METADATA_ELEMENT);
|
---|
| 524 | for (int i = 0; i < metadata_elements_nodelist.getLength(); i++) {
|
---|
| 525 | Element current_metadata_element = (Element) metadata_elements_nodelist.item(i);
|
---|
| 526 | String metadata_element_name_full = current_metadata_element.getAttribute("name");
|
---|
[8371] | 527 | String metadata_set_namespace = MetadataTools.getMetadataSetNamespace(metadata_element_name_full);
|
---|
[8140] | 528 |
|
---|
[8371] | 529 | // Ignore legacy crap
|
---|
| 530 | if (metadata_set_namespace.equals("hidden")) {
|
---|
| 531 | continue;
|
---|
| 532 | }
|
---|
| 533 |
|
---|
[8140] | 534 | MetadataSet metadata_set = MetadataSetManager.getMetadataSet(metadata_set_namespace);
|
---|
| 535 | if (metadata_set == null) {
|
---|
[8371] | 536 | // The metadata set isn't loaded, so give the option of mapping the element into a loaded set
|
---|
[8140] | 537 | String target_metadata_element_name_full = MetadataSetManager.mapUnloadedMetadataElement(metadata_element_name_full);
|
---|
| 538 | if (target_metadata_element_name_full == null || target_metadata_element_name_full.equals("")) {
|
---|
| 539 | // Skip this element if we still don't have a loaded element for it
|
---|
| 540 | continue;
|
---|
| 541 | }
|
---|
| 542 |
|
---|
[11240] | 543 | // Update the metadata.xml file to have the new (namespaced) element name
|
---|
| 544 | // Instead of using current_metadata_element.setAttribute("name", target_metadata_element_name_full)
|
---|
| 545 | // we create an Attr object for each target metadata element name, and cache them
|
---|
| 546 | // This makes a *huge* difference (namespacing a metadata.xml file with 45000 metadata entries now
|
---|
| 547 | // takes 45 seconds instead of 30 minutes!) -- why is setting the value of a Node so slow?
|
---|
| 548 | Attr target_metadata_element_name_attr = (Attr) target_metadata_element_name_attrs_cache.get(target_metadata_element_name_full);
|
---|
| 549 | if (target_metadata_element_name_attr == null) {
|
---|
| 550 | target_metadata_element_name_attr = document.createAttribute("name");
|
---|
| 551 | target_metadata_element_name_attr.setValue(target_metadata_element_name_full);
|
---|
| 552 | target_metadata_element_name_attrs_cache.put(target_metadata_element_name_full, target_metadata_element_name_attr);
|
---|
| 553 | }
|
---|
| 554 |
|
---|
| 555 | // Remove the old name attribute and add the new (namespaced) one
|
---|
| 556 | current_metadata_element.removeAttribute("name");
|
---|
| 557 | current_metadata_element.setAttributeNode((Attr) target_metadata_element_name_attr.cloneNode(false));
|
---|
[8140] | 558 | file_changed = true;
|
---|
| 559 |
|
---|
| 560 | metadata_element_name_full = target_metadata_element_name_full;
|
---|
| 561 | metadata_set_namespace = MetadataTools.getMetadataSetNamespace(metadata_element_name_full);
|
---|
| 562 | metadata_set = MetadataSetManager.getMetadataSet(metadata_set_namespace);
|
---|
| 563 | }
|
---|
| 564 |
|
---|
| 565 | String metadata_element_name = MetadataTools.getMetadataElementName(metadata_element_name_full);
|
---|
[8160] | 566 | MetadataElement metadata_element = metadata_set.getMetadataElementWithName(metadata_element_name);
|
---|
[8140] | 567 |
|
---|
[8141] | 568 | // If the element doesn't exist in the metadata set, add it
|
---|
| 569 | if (metadata_element == null) {
|
---|
| 570 | metadata_element = metadata_set.addMetadataElementForThisSession(metadata_element_name);
|
---|
| 571 | }
|
---|
| 572 |
|
---|
[8655] | 573 | // Square brackets need to be escaped because they are a special character in Greenstone
|
---|
| 574 | String metadata_value_string = XMLTools.getElementTextValue(current_metadata_element);
|
---|
| 575 | metadata_value_string = metadata_value_string.replaceAll("[", "[");
|
---|
| 576 | metadata_value_string = metadata_value_string.replaceAll("]", "]");
|
---|
| 577 |
|
---|
| 578 | metadata_element.addMetadataValue(metadata_value_string);
|
---|
[8140] | 579 | }
|
---|
| 580 |
|
---|
| 581 | // Rewrite the metadata.xml file if it has changed
|
---|
| 582 | if (file_changed) {
|
---|
| 583 | XMLTools.writeXMLFile(this, document);
|
---|
| 584 | }
|
---|
| 585 | }
|
---|
[7822] | 586 | }
|
---|