- Timestamp:
- 2005-05-12T12:24:38+12:00 (19 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/gli/src/org/greenstone/gatherer/metadata/MetadataXMLFile.java
r9525 r9856 69 69 70 70 // Form a regular expression that specifies the scope of the metadata 71 String file_path_regexp = file_relative_path; 72 if (file_path_regexp.equals("")) { 71 String file_path_regexp; 72 if (file_relative_path.equals("")) { 73 // Special case for matching all files in the directory 73 74 file_path_regexp = DIRECTORY_FILENAME; 74 75 } 75 76 else { 76 77 // Convert the file path into a regular expression that will match it 77 file_path_regexp = file_path_regexp.replaceAll("\\.", "\\\\."); 78 file_path_regexp = file_path_regexp.replaceAll("\\(", "\\\\("); 79 file_path_regexp = file_path_regexp.replaceAll("\\)", "\\\\)"); 80 file_path_regexp = file_path_regexp.replaceAll("\\[", "\\\\["); 81 file_path_regexp = file_path_regexp.replaceAll("\\]", "\\\\]"); 82 file_path_regexp = file_path_regexp.replaceAll("\\{", "\\\\{"); 83 file_path_regexp = file_path_regexp.replaceAll("\\}", "\\\\}"); 84 file_path_regexp = file_path_regexp.replaceAll("\\+", "\\\\+"); 85 } 78 file_path_regexp = MetadataTools.getRegularExpressionThatMatchesFilePath(file_relative_path); 79 } 80 81 // Remove any characters that are invalid in XML 82 String metadata_value_string = XMLTools.removeInvalidCharacters(metadata_value.getFullValue()); 86 83 87 84 // Square brackets need to be escaped because they are a special character in Greenstone 88 String metadata_value_string = metadata_value.getFullValue();89 85 metadata_value_string = metadata_value_string.replaceAll("\\[", "["); 90 86 metadata_value_string = metadata_value_string.replaceAll("\\]", "]"); … … 313 309 314 310 // Form a regular expression that specifies the scope of the metadata 315 String file_path_regexp = file_relative_path; 316 if (file_path_regexp.equals("")) { 311 String file_path_regexp; 312 if (file_relative_path.equals("")) { 313 // Special case for matching all files in the directory 317 314 file_path_regexp = DIRECTORY_FILENAME; 318 315 } 319 316 else { 320 317 // Convert the file path into a regular expression that will match it 321 file_path_regexp = file_path_regexp.replaceAll("\\.", "\\\\."); 322 file_path_regexp = file_path_regexp.replaceAll("\\(", "\\\\("); 323 file_path_regexp = file_path_regexp.replaceAll("\\)", "\\\\)"); 324 file_path_regexp = file_path_regexp.replaceAll("\\[", "\\\\["); 325 file_path_regexp = file_path_regexp.replaceAll("\\]", "\\\\]"); 326 file_path_regexp = file_path_regexp.replaceAll("\\{", "\\\\{"); 327 file_path_regexp = file_path_regexp.replaceAll("\\}", "\\\\}"); 328 file_path_regexp = file_path_regexp.replaceAll("\\+", "\\\\+"); 329 } 318 file_path_regexp = MetadataTools.getRegularExpressionThatMatchesFilePath(file_relative_path); 319 } 320 321 // Remove any characters that are invalid in XML 322 String metadata_value_string = XMLTools.removeInvalidCharacters(metadata_value.getFullValue()); 330 323 331 324 // Square brackets need to be escaped because they are a special character in Greenstone 332 String metadata_value_string = metadata_value.getFullValue();333 325 metadata_value_string = metadata_value_string.replaceAll("\\[", "["); 334 326 metadata_value_string = metadata_value_string.replaceAll("\\]", "]"); 335 327 336 328 // Read all the FileSet elements in the file 337 329 NodeList fileset_elements_nodelist = document.getElementsByTagName(FILESET_ELEMENT); … … 378 370 // Remove this Metadata element 379 371 current_metadata_element.getParentNode().removeChild(current_metadata_element); 372 } 373 } 374 375 // Rewrite the metadata.xml file 376 XMLTools.writeXMLFile(this, document); 377 } 378 379 380 public void replaceMetadata(File file, MetadataValue old_metadata_value, MetadataValue new_metadata_value) 381 { 382 // Parse the metadata.xml file 383 Document document = XMLTools.parseXMLFile(this); 384 if (document == null) { 385 System.err.println("Error: Could not parse metadata.xml file " + getAbsolutePath()); 386 return; 387 } 388 389 // Determine the file's path relative to the location of the metadata.xml file 390 File metadata_xml_file_directory = getParentFile(); 391 String file_relative_path = file.getAbsolutePath().substring(metadata_xml_file_directory.getAbsolutePath().length()); 392 if (file_relative_path.startsWith(File.separator)) { 393 file_relative_path = file_relative_path.substring(File.separator.length()); 394 } 395 396 // Form a regular expression that specifies the scope of the metadata 397 String file_path_regexp; 398 if (file_relative_path.equals("")) { 399 // Special case for matching all files in the directory 400 file_path_regexp = DIRECTORY_FILENAME; 401 } 402 else { 403 // Convert the file path into a regular expression that will match it 404 file_path_regexp = MetadataTools.getRegularExpressionThatMatchesFilePath(file_relative_path); 405 } 406 407 // Remove any characters that are invalid in XML 408 String old_metadata_value_string = XMLTools.removeInvalidCharacters(old_metadata_value.getFullValue()); 409 String new_metadata_value_string = XMLTools.removeInvalidCharacters(new_metadata_value.getFullValue()); 410 411 // Square brackets need to be escaped because they are a special character in Greenstone 412 old_metadata_value_string = old_metadata_value_string.replaceAll("\\[", "["); 413 old_metadata_value_string = old_metadata_value_string.replaceAll("\\]", "]"); 414 new_metadata_value_string = new_metadata_value_string.replaceAll("\\[", "["); 415 new_metadata_value_string = new_metadata_value_string.replaceAll("\\]", "]"); 416 417 // Read all the FileSet elements in the file 418 NodeList fileset_elements_nodelist = document.getElementsByTagName(FILESET_ELEMENT); 419 for (int i = 0; i < fileset_elements_nodelist.getLength(); i++) { 420 Element current_fileset_element = (Element) fileset_elements_nodelist.item(i); 421 boolean current_fileset_matches = false; 422 423 // Check the FileName elements of the FileSet to see if we have a match 424 NodeList filename_elements_nodelist = current_fileset_element.getElementsByTagName(FILENAME_ELEMENT); 425 for (int j = 0; j < filename_elements_nodelist.getLength(); j++) { 426 Element current_filename_element = (Element) filename_elements_nodelist.item(j); 427 String current_filename_element_value = XMLTools.getElementTextValue(current_filename_element); 428 429 // Only exact matches can be edited 430 if (current_filename_element_value.equals(file_path_regexp)) { 431 current_fileset_matches = true; 432 break; 433 } 434 } 435 436 // The FileSet doesn't apply, so move onto the next one 437 if (current_fileset_matches == false) { 438 continue; 439 } 440 441 // Each metadata value is only allowed to be assigned once 442 boolean new_metadata_value_already_exists = false; 443 Element metadata_element_to_edit = null; 444 445 // Find the Metadata element to replace in the fileset 446 String metadata_element_name_full = old_metadata_value.getMetadataElement().getFullName(); 447 NodeList metadata_elements_nodelist = current_fileset_element.getElementsByTagName(METADATA_ELEMENT); 448 for (int k = 0; k < metadata_elements_nodelist.getLength(); k++) { 449 Element current_metadata_element = (Element) metadata_elements_nodelist.item(k); 450 451 // Check the metadata element name matches 452 String current_metadata_element_name_full = current_metadata_element.getAttribute("name"); 453 if (!current_metadata_element_name_full.equals(metadata_element_name_full)) { 454 continue; 455 } 456 457 // Check the new metadata value doesn't already exist 458 String current_metadata_value_string = XMLTools.getElementTextValue(current_metadata_element); 459 if (current_metadata_value_string.equals(new_metadata_value_string)) { 460 new_metadata_value_already_exists = true; 461 } 462 463 // Check the metadata element value matches 464 if (current_metadata_value_string.equals(old_metadata_value_string)) { 465 metadata_element_to_edit = current_metadata_element; 466 } 467 } 468 469 // If the new metadata value already existed, remove the original value 470 if (new_metadata_value_already_exists) { 471 metadata_element_to_edit.getParentNode().removeChild(metadata_element_to_edit); 472 } 473 // Otherwise replace the old value with the new value 474 else { 475 XMLTools.setElementTextValue(metadata_element_to_edit, new_metadata_value_string); 380 476 } 381 477 }
Note:
See TracChangeset
for help on using the changeset viewer.