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

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

More improvements to the new metadata code, including language-specific metadata element display and a 5x speed up in the skimming of the doc.xml files.

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