source: trunk/gli/src/org/greenstone/gatherer/metadata/MetadataXMLFile.java@ 13278

Last change on this file since 13278 was 13278, checked in by mdewsnip, 17 years ago

Considered trying to disable the value trees for faster loading, but skimFile() is vital for too many important reasons. Extended the comments to explain this a bit more.

  • Property svn:keywords set to Author Date Id Revision
File size: 26.3 KB
Line 
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
27package org.greenstone.gatherer.metadata;
28
29
30import java.io.*;
31import java.util.*;
32import org.greenstone.gatherer.DebugStream;
33import org.greenstone.gatherer.util.XMLTools;
34import org.w3c.dom.*;
35
36
37/** This class represents one metadata.xml file */
38public class MetadataXMLFile
39 extends File
40{
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";
46
47
48 public MetadataXMLFile(String metadata_xml_file_path)
49 {
50 super(metadata_xml_file_path);
51 }
52
53
54 public void addMetadata(File file, ArrayList metadata_values)
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 }
62
63 // Determine the file's path relative to the location of the metadata.xml file
64 String metadata_xml_file_directory_path = getParentFile().getAbsolutePath();
65 String file_relative_path = file.getAbsolutePath().substring(metadata_xml_file_directory_path.length());
66 if (file_relative_path.startsWith(File.separator)) {
67 file_relative_path = file_relative_path.substring(File.separator.length());
68 }
69
70 // Form a regular expression that specifies the scope of the metadata
71 String file_path_regexp;
72 if (file_relative_path.equals("")) {
73 // Special case for matching all files in the directory
74 file_path_regexp = DIRECTORY_FILENAME;
75 }
76 else {
77 // Convert the file path into a regular expression that will match it
78 file_path_regexp = MetadataTools.getRegularExpressionThatMatchesFilePath(file_relative_path);
79 }
80
81 // Find the appropriate FileSet element for this file
82 Element appropriate_fileset_element = null;
83
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);
88
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);
94
95 // Only exact matches can be extended with new metadata
96 if (current_filename_element_value.equals(file_path_regexp)) {
97 appropriate_fileset_element = current_fileset_element;
98 break;
99 }
100 }
101 }
102
103 // If no appropriate FileSet element exists create a new one for this file
104 if (appropriate_fileset_element == null) {
105 DebugStream.println("Creating new FileSet element for file since none exists...");
106 appropriate_fileset_element = document.createElement(FILESET_ELEMENT);
107
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);
111
112 Element new_description_element = document.createElement(DESCRIPTION_ELEMENT);
113 appropriate_fileset_element.appendChild(new_description_element);
114
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("\\[", "&#091;");
129 metadata_value_string = metadata_value_string.replaceAll("\\]", "&#093;");
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)) {
143 // Metadata already assigned
144 metadata_already_assigned = true;
145 break;
146 }
147 }
148 }
149
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));
157
158 // Accumulating metadata: add at the end
159 if (metadata_value.isAccumulatingMetadata()) {
160 description_element.appendChild(new_metadata_element);
161 }
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 }
166 }
167 }
168
169 // Rewrite the metadata.xml file
170 XMLTools.writeXMLFile(this, document);
171 }
172
173
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());
180 return new ArrayList();
181 }
182
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 }
189
190 // Build up a list of metadata assigned to this file
191 ArrayList metadata_values = new ArrayList();
192
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;
198 boolean is_one_file_only_metadata = true;
199 File folder_metadata_inherited_from = null;
200
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);
206
207 // Does this fileset specify metadata for one file only?
208 is_one_file_only_metadata = true;
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)
211 is_one_file_only_metadata = false;
212 }
213
214 // This fileset specifies metadata for the file
215 if (file_relative_path.matches(current_filename_element_value)) {
216 current_fileset_matches = true;
217 if (!file_relative_path.equals("") && current_filename_element_value.equals(DIRECTORY_FILENAME)) {
218 folder_metadata_inherited_from = metadata_xml_file_directory;
219 }
220 break;
221 }
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 }
229 }
230
231 // The FileSet doesn't apply, so move onto the next one
232 if (current_fileset_matches == false) {
233 continue;
234 }
235
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);
240 String metadata_element_name_full = current_metadata_element.getAttribute("name");
241 String metadata_set_namespace = MetadataTools.getMetadataSetNamespace(metadata_element_name_full);
242
243 // Ignore legacy crap
244 if (metadata_set_namespace.equals("hidden")) {
245 continue;
246 }
247
248 MetadataSet metadata_set = MetadataSetManager.getMetadataSet(metadata_set_namespace);
249 if (metadata_set == null) {
250 // The metadata set isn't loaded, so give the option of mapping the element into a loaded set
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);
260 }
261
262 MetadataElement metadata_element = MetadataTools.getMetadataElementWithName(metadata_element_name_full);
263
264 // If the element doesn't exist in the metadata set, we're not interested
265 if (metadata_element == null) {
266 continue;
267 }
268
269 // Square brackets need to be escaped because they are a special character in Greenstone
270 String metadata_value_string = XMLTools.getElementTextValue(current_metadata_element);
271 metadata_value_string = metadata_value_string.replaceAll("&#091;", "[");
272 metadata_value_string = metadata_value_string.replaceAll("&#093;", "]");
273
274 MetadataValueTreeNode metadata_value_tree_node = metadata_element.getMetadataValueTreeNode(metadata_value_string);
275
276 // If there is no metadata value tree node for this value, create it
277 if (metadata_value_tree_node == null) {
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);
281 }
282
283 MetadataValue metadata_value = new MetadataValue(metadata_element, metadata_value_tree_node);
284 metadata_value.inheritsMetadataFromFolder(folder_metadata_inherited_from);
285 metadata_value.setIsOneFileOnlyMetadata(is_one_file_only_metadata);
286
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);
294 }
295 }
296
297 return metadata_values;
298 }
299
300
301 public void removeMetadata(File file, ArrayList metadata_values)
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 }
309
310 // Determine the file's path relative to the location of the metadata.xml file
311 String metadata_xml_file_directory_path = getParentFile().getAbsolutePath();
312 String file_relative_path = file.getAbsolutePath().substring(metadata_xml_file_directory_path.length());
313 if (file_relative_path.startsWith(File.separator)) {
314 file_relative_path = file_relative_path.substring(File.separator.length());
315 }
316
317 // Form a regular expression that specifies the scope of the metadata
318 String file_path_regexp;
319 if (file_relative_path.equals("")) {
320 // Special case for matching all files in the directory
321 file_path_regexp = DIRECTORY_FILENAME;
322 }
323 else {
324 // Convert the file path into a regular expression that will match it
325 file_path_regexp = MetadataTools.getRegularExpressionThatMatchesFilePath(file_relative_path);
326 }
327
328 // Find the appropriate FileSet element for this file
329 Element appropriate_fileset_element = null;
330
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);
335
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);
341
342 // Only exact matches can be extended with new metadata
343 if (current_filename_element_value.equals(file_path_regexp)) {
344 appropriate_fileset_element = current_fileset_element;
345 break;
346 }
347 }
348 }
349
350 // If no appropriate FileSet element exists the metadata isn't assigned in this metadata.xml file
351 if (appropriate_fileset_element == null) {
352 DebugStream.println("Note: No appropriate FileSet element found when removing metadata from " + this);
353 return;
354 }
355
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("\\[", "&#091;");
365 metadata_value_string = metadata_value_string.replaceAll("\\]", "&#093;");
366
367 // Find the Metadata element to delete from the fileset
368 String metadata_element_name_full = metadata_value.getMetadataElement().getFullName();
369 NodeList metadata_elements_nodelist = appropriate_fileset_element.getElementsByTagName(METADATA_ELEMENT);
370 for (int k = 0; k < metadata_elements_nodelist.getLength(); k++) {
371 Element current_metadata_element = (Element) metadata_elements_nodelist.item(k);
372
373 // Check the metadata element name matches
374 String current_metadata_element_name_full = current_metadata_element.getAttribute("name");
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);
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
387 break;
388 }
389 }
390 }
391 }
392
393 // Rewrite the metadata.xml file
394 XMLTools.writeXMLFile(this, document);
395 }
396
397
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
408 String metadata_xml_file_directory_path = getParentFile().getAbsolutePath();
409 String file_relative_path = file.getAbsolutePath().substring(metadata_xml_file_directory_path.length());
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("\\[", "&#091;");
431 old_metadata_value_string = old_metadata_value_string.replaceAll("\\]", "&#093;");
432 new_metadata_value_string = new_metadata_value_string.replaceAll("\\[", "&#091;");
433 new_metadata_value_string = new_metadata_value_string.replaceAll("\\]", "&#093;");
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
492 // Ensure metadata_element_to_edit isn't null (may occur when multiple files are selected)
493 else if (metadata_element_to_edit != null) {
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
503 /**
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)
508 */
509 public void skimFile()
510 {
511 boolean file_changed = false;
512
513 // Parse the metadata.xml file
514 DebugStream.println("Skimming metadata.xml file " + this + "...");
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
522 HashMap target_metadata_element_name_attrs_cache = new HashMap();
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");
527 String metadata_set_namespace = MetadataTools.getMetadataSetNamespace(metadata_element_name_full);
528
529 // Ignore legacy crap
530 if (metadata_set_namespace.equals("hidden")) {
531 continue;
532 }
533
534 MetadataSet metadata_set = MetadataSetManager.getMetadataSet(metadata_set_namespace);
535 if (metadata_set == null) {
536 // The metadata set isn't loaded, so give the option of mapping the element into a loaded set
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
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));
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);
566 MetadataElement metadata_element = metadata_set.getMetadataElementWithName(metadata_element_name);
567
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
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("&#091;", "[");
576 metadata_value_string = metadata_value_string.replaceAll("&#093;", "]");
577
578 metadata_element.addMetadataValue(metadata_value_string);
579 }
580
581 // Rewrite the metadata.xml file if it has changed
582 if (file_changed) {
583 XMLTools.writeXMLFile(this, document);
584 }
585 }
586}
Note: See TracBrowser for help on using the repository browser.