source: trunk/gli/src/org/greenstone/gatherer/metadata/MetadataXMLFileManager.java@ 8236

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

Replaced all Gatherer.print* with DebugStream.print*.

  • Property svn:keywords set to Author Date Id Revision
File size: 10.1 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.file.FileNode;
34import org.greenstone.gatherer.util.Utility;
35import org.greenstone.gatherer.util.XMLTools;
36
37
38/** This class is a static class that manages the metadata.xml files */
39public class MetadataXMLFileManager
40{
41 static private ArrayList metadata_xml_files = new ArrayList();
42
43
44 static public void addMetadata(FileNode file_node, MetadataValue metadata_value)
45 {
46 FileNode[] file_nodes = new FileNode[1];
47 file_nodes[0] = file_node;
48 addMetadata(file_nodes, metadata_value);
49 }
50
51
52 static public void addMetadata(FileNode[] file_nodes, MetadataValue metadata_value)
53 {
54 // Add the metadata to each file node in turn
55 for (int i = 0; i < file_nodes.length; i++) {
56 File current_file = file_nodes[i].getFile();
57 DebugStream.println("Adding metadata to " + current_file.getAbsolutePath());
58
59 // Find which metadata.xml file needs editing
60 boolean applicable_metadata_xml_file_found = false;
61 File current_file_directory = (current_file.isDirectory() ? current_file : current_file.getParentFile());
62 for (int j = 0; j < metadata_xml_files.size(); j++) {
63 MetadataXMLFile metadata_xml_file = (MetadataXMLFile) metadata_xml_files.get(j);
64
65 // This metadata.xml file is only applicable if it is at the same level as the file
66 if (current_file_directory.getAbsolutePath().equals(metadata_xml_file.getParentFile().getAbsolutePath())) {
67 applicable_metadata_xml_file_found = true;
68 metadata_xml_file.addMetadata(current_file, metadata_value);
69 }
70 }
71
72 // If no applicable metadata.xml file exists, we have to create a new one
73 if (!applicable_metadata_xml_file_found) {
74 // Create a new (empty) metadata.xml file in the file's directory...
75 File new_metadata_xml_file_file = new File(current_file_directory, "metadata.xml");
76 XMLTools.writeXMLFile(new_metadata_xml_file_file, Utility.parse("xml/metadata.xml", true));
77
78 // ...load it...
79 MetadataXMLFile new_metadata_xml_file = loadMetadataXMLFile(new_metadata_xml_file_file);
80
81 // ...and add the metadata
82 new_metadata_xml_file.addMetadata(current_file, metadata_value);
83 }
84 }
85 }
86
87
88 static public void clearMetadataXMLFiles()
89 {
90 metadata_xml_files.clear();
91 }
92
93
94 /** Returns the metadata assigned to a file outside the collection, excluding folder-level/inherited metadata. */
95 static public ArrayList getMetadataAssignedDirectlyToExternalFile(File file)
96 {
97 DebugStream.println("Getting metadata assigned directly to external file " + file + "...");
98
99 // Build up a list of applicable metadata.xml files
100 ArrayList applicable_metadata_xml_files = new ArrayList();
101
102 File directory = (file.isDirectory() ? file : file.getParentFile());
103 while (directory != null) {
104 File[] directory_files = directory.listFiles();
105 for (int i = 0; i < directory_files.length; i++) {
106 File child_file = directory_files[i];
107 if (!child_file.isDirectory() && child_file.getName().equals("metadata.xml")) {
108 // It is very important that shallower files come before deeper ones
109 applicable_metadata_xml_files.add(0, new MetadataXMLFile(child_file.getAbsolutePath()));
110 }
111 }
112
113 directory = directory.getParentFile();
114 }
115
116 // Get the metadata assigned to the specified file from the applicable metadata.xml files
117 ArrayList assigned_metadata = getMetadataAssignedToFile(file, applicable_metadata_xml_files);
118
119 // Remove any folder-level metadata
120 for (int i = assigned_metadata.size() - 1; i >= 0; i--) {
121 if (((MetadataValue) assigned_metadata.get(i)).isInheritedMetadata()) {
122 assigned_metadata.remove(i);
123 }
124 }
125
126 return assigned_metadata;
127 }
128
129
130 /** Returns the metadata assigned to a file inside the collection, excluding folder-level/inherited metadata. */
131 static public ArrayList getMetadataAssignedDirectlyToFile(File file)
132 {
133 // Get all the metadata assigned to the specified file...
134 ArrayList assigned_metadata = getMetadataAssignedToFile(file);
135
136 // ...then remove any folder-level metadata
137 for (int i = assigned_metadata.size() - 1; i >= 0; i--) {
138 if (((MetadataValue) assigned_metadata.get(i)).isInheritedMetadata()) {
139 assigned_metadata.remove(i);
140 }
141 }
142
143 return assigned_metadata;
144 }
145
146
147 /** Returns all the metadata assigned to a file inside the collection. */
148 static public ArrayList getMetadataAssignedToFile(File file)
149 {
150 // Build up a list of applicable metadata.xml files
151 ArrayList applicable_metadata_xml_files = new ArrayList();
152
153 // Look at each loaded metadata.xml file to see if it is potentially applicable
154 File file_directory = (file.isDirectory() ? file : file.getParentFile());
155 for (int i = 0; i < metadata_xml_files.size(); i++) {
156 MetadataXMLFile metadata_xml_file = (MetadataXMLFile) metadata_xml_files.get(i);
157
158 // This metadata.xml file is only potentially applicable if it is above or at the same level as the file
159 if (file_directory.getAbsolutePath().startsWith(metadata_xml_file.getParentFile().getAbsolutePath())) {
160 applicable_metadata_xml_files.add(metadata_xml_file);
161 }
162 }
163
164 // Return the metadata assigned to the specified file from the applicable metadata.xml files
165 return getMetadataAssignedToFile(file, applicable_metadata_xml_files);
166 }
167
168
169 static private ArrayList getMetadataAssignedToFile(File file, ArrayList applicable_metadata_xml_files)
170 {
171 // Build up a list of metadata values assigned to this file
172 ArrayList metadata_values_all = new ArrayList();
173
174 // Look at each applicable metadata.xml file to see if it assigns metadata to this file
175 for (int i = 0; i < applicable_metadata_xml_files.size(); i++) {
176 MetadataXMLFile metadata_xml_file = (MetadataXMLFile) applicable_metadata_xml_files.get(i);
177 DebugStream.println("Applicable metadata.xml file: " + metadata_xml_file);
178
179 ArrayList metadata_values = metadata_xml_file.getMetadataAssignedToFile(file);
180 for (int j = 0; j < metadata_values.size(); j++) {
181 MetadataValue metadata_value = (MetadataValue) metadata_values.get(j);
182
183 // Overriding metadata: remove any values with this metadata element
184 if (metadata_value.isAccumulatingMetadata() == false) {
185 for (int k = metadata_values_all.size() - 1; k >= 0; k--) {
186 if (((MetadataValue) metadata_values_all.get(k)).getMetadataElement().equals(metadata_value.getMetadataElement())) {
187 metadata_values_all.remove(k);
188 }
189 }
190 }
191
192 metadata_values_all.add(metadata_value);
193 }
194 }
195
196 return metadata_values_all;
197 }
198
199
200 static public void loadMetadataXMLFiles(File directory)
201 {
202 // Make sure the directory (import) exists
203 if (directory.exists() == false) {
204 return;
205 }
206
207 // Look recursively at each subfile of the directory for metadata.xml files
208 File[] directory_files = directory.listFiles();
209 for (int i = 0; i < directory_files.length; i++) {
210 File child_file = directory_files[i];
211 if (child_file.isDirectory()) {
212 loadMetadataXMLFiles(child_file);
213 }
214 else if (child_file.getName().equals("metadata.xml")) {
215 loadMetadataXMLFile(child_file);
216 }
217 }
218 }
219
220
221 static private MetadataXMLFile loadMetadataXMLFile(File metadata_xml_file_file)
222 {
223 MetadataXMLFile metadata_xml_file = new MetadataXMLFile(metadata_xml_file_file.getAbsolutePath());
224 metadata_xml_file.skimFile();
225 metadata_xml_files.add(metadata_xml_file);
226 return metadata_xml_file;
227 }
228
229
230 static public void removeMetadata(FileNode file_node, MetadataValue metadata_value)
231 {
232 FileNode[] file_nodes = new FileNode[1];
233 file_nodes[0] = file_node;
234 removeMetadata(file_nodes, metadata_value);
235 }
236
237
238 static public void removeMetadata(FileNode[] file_nodes, MetadataValue metadata_value)
239 {
240 // Remove the metadata from each file node in turn
241 for (int i = 0; i < file_nodes.length; i++) {
242 File current_file = file_nodes[i].getFile();
243 DebugStream.println("Removing metadata from " + current_file.getAbsolutePath());
244
245 // Find which metadata.xml file needs editing
246 File current_file_directory = (current_file.isDirectory() ? current_file : current_file.getParentFile());
247 for (int j = 0; j < metadata_xml_files.size(); j++) {
248 MetadataXMLFile metadata_xml_file = (MetadataXMLFile) metadata_xml_files.get(j);
249
250 // This metadata.xml file is only potentially applicable if it is above or at the same level as the file
251 if (current_file_directory.getAbsolutePath().startsWith(metadata_xml_file.getParentFile().getAbsolutePath())) {
252 metadata_xml_file.removeMetadata(current_file, metadata_value);
253 }
254 }
255 }
256 }
257
258
259 static public void unloadMetadataXMLFile(File metadata_xml_file_file)
260 {
261 DebugStream.println("Unloading metadata.xml file " + metadata_xml_file_file);
262
263 // Find the metadata.xml file in the list of loaded files, and remove it
264 for (int i = 0; i < metadata_xml_files.size(); i++) {
265 MetadataXMLFile metadata_xml_file = (MetadataXMLFile) metadata_xml_files.get(i);
266 if (metadata_xml_file_file.getAbsolutePath().equals(metadata_xml_file.getAbsolutePath())) {
267 metadata_xml_files.remove(i);
268 break;
269 }
270 }
271 }
272}
Note: See TracBrowser for help on using the repository browser.