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

Last change on this file since 8622 was 8433, checked in by mdewsnip, 20 years ago

Now escapes '(', ')', ',', '{', '}' in file paths when converting to regular expressions so files with these characters in the names can have metadata assigned and removed.

  • Property svn:keywords set to Author Date Id Revision
File size: 18.0 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, MetadataValue metadata_value)
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 File metadata_xml_file_directory = getParentFile();
65 String file_relative_path = file.getAbsolutePath().substring(metadata_xml_file_directory.getAbsolutePath().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 = file_relative_path;
72 if (file_path_regexp.equals("")) {
73 file_path_regexp = DIRECTORY_FILENAME;
74 }
75 else {
76 // 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 }
85
86 // Square brackets need to be escaped because they are a special character in Greenstone
87 String metadata_value_string = metadata_value.getFullValue();
88 metadata_value_string = metadata_value_string.replaceAll("\\[", "[");
89 metadata_value_string = metadata_value_string.replaceAll("\\]", "]");
90
91 // Create a new Metadata element to record this metadata
92 Element new_metadata_value_element = document.createElement(METADATA_ELEMENT);
93 new_metadata_value_element.setAttribute("name", metadata_value.getMetadataElement().getFullName());
94 new_metadata_value_element.setAttribute("mode", (metadata_value.isAccumulatingMetadata() ? "accumulate" : "override"));
95 new_metadata_value_element.appendChild(document.createTextNode(metadata_value_string));
96 boolean have_added_metadata = false;
97
98 // Read all the FileSet elements in the file
99 NodeList fileset_elements_nodelist = document.getElementsByTagName(FILESET_ELEMENT);
100 for (int i = 0; i < fileset_elements_nodelist.getLength(); i++) {
101 Element current_fileset_element = (Element) fileset_elements_nodelist.item(i);
102
103 // Check the FileName elements of the FileSet to see if we have a match
104 NodeList filename_elements_nodelist = current_fileset_element.getElementsByTagName(FILENAME_ELEMENT);
105 for (int j = 0; j < filename_elements_nodelist.getLength(); j++) {
106 Element current_filename_element = (Element) filename_elements_nodelist.item(j);
107 String current_filename_element_value = XMLTools.getElementTextValue(current_filename_element);
108
109 // Only exact matches can be extended with new metadata
110 if (current_filename_element_value.equals(file_path_regexp)) {
111 // Append the new Metadata element to the Description element of this FileSet
112 Element description_element = (Element) current_fileset_element.getElementsByTagName(DESCRIPTION_ELEMENT).item(0);
113
114 // Accumulating metadata: add at the end
115 if (metadata_value.isAccumulatingMetadata()) {
116 description_element.appendChild(new_metadata_value_element);
117 }
118 // Override metadata: add at the start (so it overrides inherited metadata without affecting other assigned metadata)
119 else {
120 description_element.insertBefore(new_metadata_value_element, description_element.getFirstChild());
121 }
122
123 have_added_metadata = true;
124 break;
125 }
126 }
127 }
128
129 // Check if the metadata was added to an existing FileSet
130 if (!have_added_metadata) {
131 // It wasn't, so create a new FileSet element for it
132 Element new_fileset_element = document.createElement(FILESET_ELEMENT);
133
134 Element new_filename_element = document.createElement(FILENAME_ELEMENT);
135 new_filename_element.appendChild(document.createTextNode(file_path_regexp));
136 new_fileset_element.appendChild(new_filename_element);
137
138 // Append the new Metadata element to the Description element of this FileSet
139 Element new_description_element = document.createElement(DESCRIPTION_ELEMENT);
140 new_description_element.appendChild(new_metadata_value_element);
141 new_fileset_element.appendChild(new_description_element);
142
143 document.getDocumentElement().appendChild(new_fileset_element);
144 }
145
146 // Rewrite the metadata.xml file
147 XMLTools.writeXMLFile(this, document);
148 }
149
150
151 public ArrayList getMetadataAssignedToFile(File file)
152 {
153 // Parse the metadata.xml file
154 Document document = XMLTools.parseXMLFile(this);
155 if (document == null) {
156 System.err.println("Error: Could not parse metadata.xml file " + getAbsolutePath());
157 return null;
158 }
159
160 // Determine the file's path relative to the location of the metadata.xml file
161 File metadata_xml_file_directory = getParentFile();
162 String file_relative_path = file.getAbsolutePath().substring(metadata_xml_file_directory.getAbsolutePath().length());
163 if (file_relative_path.startsWith(File.separator)) {
164 file_relative_path = file_relative_path.substring(File.separator.length());
165 }
166
167 // Build up a list of metadata assigned to this file
168 ArrayList metadata_values = new ArrayList();
169
170 // Read all the FileSet elements in the file
171 NodeList fileset_elements_nodelist = document.getElementsByTagName(FILESET_ELEMENT);
172 for (int i = 0; i < fileset_elements_nodelist.getLength(); i++) {
173 Element current_fileset_element = (Element) fileset_elements_nodelist.item(i);
174 boolean current_fileset_matches = false;
175 File folder_metadata_inherited_from = null;
176
177 // Check the FileName elements of the FileSet to see if we have a match
178 NodeList filename_elements_nodelist = current_fileset_element.getElementsByTagName(FILENAME_ELEMENT);
179 for (int j = 0; j < filename_elements_nodelist.getLength(); j++) {
180 Element current_filename_element = (Element) filename_elements_nodelist.item(j);
181 String current_filename_element_value = XMLTools.getElementTextValue(current_filename_element);
182
183 // This fileset specifies metadata for the file
184 if (file_relative_path.matches(current_filename_element_value)) {
185 current_fileset_matches = true;
186 if (!file_relative_path.equals("") && current_filename_element_value.equals(DIRECTORY_FILENAME)) {
187 folder_metadata_inherited_from = metadata_xml_file_directory;
188 }
189 break;
190 }
191
192 // This fileset specifies metadata for the folder the file is in
193 if (file_relative_path.startsWith(current_filename_element_value + File.separator)) {
194 current_fileset_matches = true;
195 folder_metadata_inherited_from = new File(metadata_xml_file_directory, current_filename_element_value);
196 break;
197 }
198 }
199
200 // The FileSet doesn't apply, so move onto the next one
201 if (current_fileset_matches == false) {
202 continue;
203 }
204
205 // Read all the Metadata elements in the fileset
206 NodeList metadata_elements_nodelist = current_fileset_element.getElementsByTagName(METADATA_ELEMENT);
207 for (int k = 0; k < metadata_elements_nodelist.getLength(); k++) {
208 Element current_metadata_element = (Element) metadata_elements_nodelist.item(k);
209 String metadata_element_name_full = current_metadata_element.getAttribute("name");
210 String metadata_set_namespace = MetadataTools.getMetadataSetNamespace(metadata_element_name_full);
211
212 // Ignore legacy crap
213 if (metadata_set_namespace.equals("hidden")) {
214 continue;
215 }
216
217 MetadataSet metadata_set = MetadataSetManager.getMetadataSet(metadata_set_namespace);
218 if (metadata_set == null) {
219 // The metadata set isn't loaded, so give the option of mapping the element into a loaded set
220 String target_metadata_element_name_full = MetadataSetManager.mapUnloadedMetadataElement(metadata_element_name_full);
221 if (target_metadata_element_name_full == null || target_metadata_element_name_full.equals("")) {
222 // Skip this element if we still don't have a loaded element for it
223 continue;
224 }
225
226 metadata_element_name_full = target_metadata_element_name_full;
227 metadata_set_namespace = MetadataTools.getMetadataSetNamespace(metadata_element_name_full);
228 metadata_set = MetadataSetManager.getMetadataSet(metadata_set_namespace);
229 }
230
231 MetadataElement metadata_element = MetadataTools.getMetadataElementWithName(metadata_element_name_full);
232
233 // If the element doesn't exist in the metadata set, we're not interested
234 if (metadata_element == null) {
235 continue;
236 }
237
238 // Square brackets need to be escaped because they are a special character in Greenstone
239 String metadata_element_value = XMLTools.getElementTextValue(current_metadata_element);
240 metadata_element_value = metadata_element_value.replaceAll("&#091;", "[");
241 metadata_element_value = metadata_element_value.replaceAll("&#093;", "]");
242
243 MetadataValueTreeNode metadata_value_tree_node = metadata_element.getMetadataValueTreeNode(metadata_element_value);
244
245 // If there is no metadata value tree node for this value, create it
246 if (metadata_value_tree_node == null) {
247 DebugStream.println("Note: No value tree node for metadata value \"" + metadata_element_value + "\"");
248 metadata_element.addMetadataValue(metadata_element_value);
249 metadata_value_tree_node = metadata_element.getMetadataValueTreeNode(metadata_element_value);
250 }
251
252 MetadataValue metadata_value = new MetadataValue(metadata_element, metadata_value_tree_node);
253 metadata_value.inheritsMetadataFromFolder(folder_metadata_inherited_from);
254
255 // Is this accumulating metadata?
256 if (current_metadata_element.getAttribute("mode").equals("accumulate")) {
257 metadata_value.setIsAccumulatingMetadata(true);
258 }
259
260 // Add the new metadata value to the list
261 metadata_values.add(metadata_value);
262 }
263 }
264
265 return metadata_values;
266 }
267
268
269 public void removeMetadata(File file, MetadataValue metadata_value)
270 {
271 // Parse the metadata.xml file
272 Document document = XMLTools.parseXMLFile(this);
273 if (document == null) {
274 System.err.println("Error: Could not parse metadata.xml file " + getAbsolutePath());
275 return;
276 }
277
278 // Determine the file's path relative to the location of the metadata.xml file
279 File metadata_xml_file_directory = getParentFile();
280 String file_relative_path = file.getAbsolutePath().substring(metadata_xml_file_directory.getAbsolutePath().length());
281 if (file_relative_path.startsWith(File.separator)) {
282 file_relative_path = file_relative_path.substring(File.separator.length());
283 }
284
285 // Form a regular expression that specifies the scope of the metadata
286 String file_path_regexp = file_relative_path;
287 if (file_path_regexp.equals("")) {
288 file_path_regexp = DIRECTORY_FILENAME;
289 }
290 else {
291 // Convert the file path into a regular expression that will match it
292 file_path_regexp = file_path_regexp.replaceAll("\\.", "\\\\.");
293 file_path_regexp = file_path_regexp.replaceAll("\\(", "\\\\(");
294 file_path_regexp = file_path_regexp.replaceAll("\\)", "\\\\)");
295 file_path_regexp = file_path_regexp.replaceAll("\\[", "\\\\[");
296 file_path_regexp = file_path_regexp.replaceAll("\\]", "\\\\]");
297 file_path_regexp = file_path_regexp.replaceAll("\\{", "\\\\{");
298 file_path_regexp = file_path_regexp.replaceAll("\\}", "\\\\}");
299 }
300
301 // Read all the FileSet elements in the file
302 NodeList fileset_elements_nodelist = document.getElementsByTagName(FILESET_ELEMENT);
303 for (int i = 0; i < fileset_elements_nodelist.getLength(); i++) {
304 Element current_fileset_element = (Element) fileset_elements_nodelist.item(i);
305 boolean current_fileset_matches = false;
306
307 // Check the FileName elements of the FileSet to see if we have a match
308 NodeList filename_elements_nodelist = current_fileset_element.getElementsByTagName(FILENAME_ELEMENT);
309 for (int j = 0; j < filename_elements_nodelist.getLength(); j++) {
310 Element current_filename_element = (Element) filename_elements_nodelist.item(j);
311 String current_filename_element_value = XMLTools.getElementTextValue(current_filename_element);
312
313 // Only exact matches can be edited
314 if (current_filename_element_value.equals(file_path_regexp)) {
315 current_fileset_matches = true;
316 break;
317 }
318 }
319
320 // The FileSet doesn't apply, so move onto the next one
321 if (current_fileset_matches == false) {
322 continue;
323 }
324
325 // Find the Metadata element to delete from the fileset
326 String metadata_element_name_full = metadata_value.getMetadataElement().getFullName();
327 String metadata_element_value = metadata_value.getFullValue();
328 NodeList metadata_elements_nodelist = current_fileset_element.getElementsByTagName(METADATA_ELEMENT);
329 for (int k = 0; k < metadata_elements_nodelist.getLength(); k++) {
330 Element current_metadata_element = (Element) metadata_elements_nodelist.item(k);
331
332 // Check the metadata element name matches
333 String current_metadata_element_name_full = current_metadata_element.getAttribute("name");
334 if (!current_metadata_element_name_full.equals(metadata_element_name_full)) {
335 continue;
336 }
337
338 // Check the metadata element value matches
339 String current_metadata_element_value = XMLTools.getElementTextValue(current_metadata_element);
340 if (!current_metadata_element_value.equals(metadata_element_value)) {
341 continue;
342 }
343
344 // Remove this Metadata element
345 current_metadata_element.getParentNode().removeChild(current_metadata_element);
346 }
347 }
348
349 // Rewrite the metadata.xml file
350 XMLTools.writeXMLFile(this, document);
351 }
352
353
354 /**
355 * Every metadata.xml file must be skimmed when a collection is opened, for two reasons:
356 * - To build complete and accurate metadata value trees (needed for Enrich pane and hierarchy files)
357 * - To handle any non-namespaced metadata in the file
358 */
359 public void skimFile()
360 {
361 boolean file_changed = false;
362
363 // Parse the metadata.xml file
364 DebugStream.println("Skimming metadata.xml file " + this + "...");
365 Document document = XMLTools.parseXMLFile(this);
366 if (document == null) {
367 System.err.println("Error: Could not parse metadata.xml file " + getAbsolutePath());
368 return;
369 }
370
371 // Read all the Metadata elements in the file
372 NodeList metadata_elements_nodelist = document.getElementsByTagName(METADATA_ELEMENT);
373 for (int i = 0; i < metadata_elements_nodelist.getLength(); i++) {
374 Element current_metadata_element = (Element) metadata_elements_nodelist.item(i);
375 String metadata_element_name_full = current_metadata_element.getAttribute("name");
376 String metadata_set_namespace = MetadataTools.getMetadataSetNamespace(metadata_element_name_full);
377
378 // Ignore legacy crap
379 if (metadata_set_namespace.equals("hidden")) {
380 continue;
381 }
382
383 MetadataSet metadata_set = MetadataSetManager.getMetadataSet(metadata_set_namespace);
384 if (metadata_set == null) {
385 // The metadata set isn't loaded, so give the option of mapping the element into a loaded set
386 String target_metadata_element_name_full = MetadataSetManager.mapUnloadedMetadataElement(metadata_element_name_full);
387 if (target_metadata_element_name_full == null || target_metadata_element_name_full.equals("")) {
388 // Skip this element if we still don't have a loaded element for it
389 continue;
390 }
391
392 // Update the metadata.xml file to have the new element name
393 current_metadata_element.setAttribute("name", target_metadata_element_name_full);
394 file_changed = true;
395
396 metadata_element_name_full = target_metadata_element_name_full;
397 metadata_set_namespace = MetadataTools.getMetadataSetNamespace(metadata_element_name_full);
398 metadata_set = MetadataSetManager.getMetadataSet(metadata_set_namespace);
399 }
400
401 String metadata_element_name = MetadataTools.getMetadataElementName(metadata_element_name_full);
402 MetadataElement metadata_element = metadata_set.getMetadataElementWithName(metadata_element_name);
403
404 // If the element doesn't exist in the metadata set, add it
405 if (metadata_element == null) {
406 metadata_element = metadata_set.addMetadataElementForThisSession(metadata_element_name);
407 }
408
409 String metadata_element_value = XMLTools.getElementTextValue(current_metadata_element);
410 metadata_element.addMetadataValue(metadata_element_value);
411 }
412
413 // Rewrite the metadata.xml file if it has changed
414 if (file_changed) {
415 XMLTools.writeXMLFile(this, document);
416 }
417 }
418}
Note: See TracBrowser for help on using the repository browser.