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

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

Removed some debug statements and made others go to the debug stream.

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