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 |
|
---|
27 | package org.greenstone.gatherer.metadata;
|
---|
28 |
|
---|
29 |
|
---|
30 | import java.io.*;
|
---|
31 | import java.util.*;
|
---|
32 | import org.greenstone.gatherer.gui.MetadataImportMappingPrompt;
|
---|
33 |
|
---|
34 |
|
---|
35 | /** This class is a static class that manages the metadata sets */
|
---|
36 | public class MetadataSetManager
|
---|
37 | {
|
---|
38 | static public final String EXTRACTED_METADATA_NAMESPACE = "ex";
|
---|
39 |
|
---|
40 | static private ArrayList metadata_sets = new ArrayList();
|
---|
41 |
|
---|
42 |
|
---|
43 | static public void clearMetadataSets()
|
---|
44 | {
|
---|
45 | metadata_sets.clear();
|
---|
46 | }
|
---|
47 |
|
---|
48 |
|
---|
49 | static public int compareMetadataElements(MetadataElement metadata_element_a, MetadataElement metadata_element_b)
|
---|
50 | {
|
---|
51 | // First compare the namespaces of the two metadata elements
|
---|
52 | if (!metadata_element_a.getNamespace().equals(metadata_element_b.getNamespace())) {
|
---|
53 | return metadata_element_a.getNamespace().compareTo(metadata_element_b.getNamespace());
|
---|
54 | }
|
---|
55 |
|
---|
56 | // Otherwise compare the two elements within the one set
|
---|
57 | MetadataSet metadata_set = getMetadataSet(metadata_element_a.getNamespace());
|
---|
58 | return metadata_set.compareMetadataElements(metadata_element_a, metadata_element_b);
|
---|
59 | }
|
---|
60 |
|
---|
61 |
|
---|
62 | static public ArrayList getEveryMetadataSetElement()
|
---|
63 | {
|
---|
64 | ArrayList every_metadata_set_element = new ArrayList();
|
---|
65 |
|
---|
66 | for (int i = 0; i < metadata_sets.size(); i++) {
|
---|
67 | MetadataSet metadata_set = (MetadataSet) metadata_sets.get(i);
|
---|
68 | every_metadata_set_element.addAll(metadata_set.getMetadataSetElements());
|
---|
69 | }
|
---|
70 |
|
---|
71 | return every_metadata_set_element;
|
---|
72 | }
|
---|
73 |
|
---|
74 |
|
---|
75 | static public MetadataSet getMetadataSet(String metadata_set_namespace)
|
---|
76 | {
|
---|
77 | // Find the desired metadata set in the list
|
---|
78 | for (int i = 0; i < metadata_sets.size(); i++) {
|
---|
79 | MetadataSet metadata_set = (MetadataSet) metadata_sets.get(i);
|
---|
80 | if (metadata_set.getNamespace().equals(metadata_set_namespace)) {
|
---|
81 | return metadata_set;
|
---|
82 | }
|
---|
83 | }
|
---|
84 |
|
---|
85 | // Not found
|
---|
86 | return null;
|
---|
87 | }
|
---|
88 |
|
---|
89 |
|
---|
90 | static public ArrayList getMetadataSets()
|
---|
91 | {
|
---|
92 | // We don't want anyone to modify the list, so return a copy of it
|
---|
93 | return (ArrayList) metadata_sets.clone();
|
---|
94 | }
|
---|
95 |
|
---|
96 |
|
---|
97 | static public ArrayList listMetadataSets(File directory)
|
---|
98 | {
|
---|
99 | // Make sure the directory (metadata) exists
|
---|
100 | if (directory.exists() == false) {
|
---|
101 | return null;
|
---|
102 | }
|
---|
103 |
|
---|
104 | // Load just those .mds files in this directory, and return them
|
---|
105 | ArrayList directory_metadata_sets = new ArrayList();
|
---|
106 | File[] directory_files = directory.listFiles();
|
---|
107 | for (int i = 0; i < directory_files.length; i++) {
|
---|
108 | File child_file = directory_files[i];
|
---|
109 | if (!child_file.isDirectory() && child_file.getName().endsWith(".mds")) {
|
---|
110 | MetadataSet metadata_set = new MetadataSet(child_file);
|
---|
111 | directory_metadata_sets.add(metadata_set);
|
---|
112 | }
|
---|
113 | }
|
---|
114 |
|
---|
115 | return directory_metadata_sets;
|
---|
116 | }
|
---|
117 |
|
---|
118 |
|
---|
119 | static public void loadMetadataSets(File directory)
|
---|
120 | {
|
---|
121 | // Make sure the directory (metadata) exists
|
---|
122 | if (directory.exists() == false) {
|
---|
123 | return;
|
---|
124 | }
|
---|
125 |
|
---|
126 | // Look recursively at each subfile of the directory for .mds files
|
---|
127 | File[] directory_files = directory.listFiles();
|
---|
128 | for (int i = 0; i < directory_files.length; i++) {
|
---|
129 | File child_file = directory_files[i];
|
---|
130 | if (child_file.isDirectory()) {
|
---|
131 | loadMetadataSets(child_file);
|
---|
132 | }
|
---|
133 | else if (child_file.getName().endsWith(".mds")) {
|
---|
134 | loadMetadataSet(child_file);
|
---|
135 | }
|
---|
136 | }
|
---|
137 | }
|
---|
138 |
|
---|
139 |
|
---|
140 | static public void loadMetadataSet(File metadata_set_file)
|
---|
141 | {
|
---|
142 | MetadataSet metadata_set = new MetadataSet(metadata_set_file);
|
---|
143 |
|
---|
144 | // Ignore legacy crap
|
---|
145 | if (metadata_set.getNamespace().equals("hidden")) {
|
---|
146 | return;
|
---|
147 | }
|
---|
148 |
|
---|
149 | metadata_sets.add(metadata_set);
|
---|
150 | }
|
---|
151 |
|
---|
152 |
|
---|
153 | static public String mapUnloadedMetadataElement(String metadata_element_name_full)
|
---|
154 | {
|
---|
155 | // Check if we have an import mapping for this metadata element
|
---|
156 | String target_metadata_element_name_full = ProfileXMLFileManager.getMetadataElementFor(metadata_element_name_full);
|
---|
157 | if (target_metadata_element_name_full != null) {
|
---|
158 | // Yes, so return it
|
---|
159 | return target_metadata_element_name_full;
|
---|
160 | }
|
---|
161 |
|
---|
162 | // If there are no metadata sets (except extracted) loaded then there is nothing to map the element into
|
---|
163 | if (metadata_sets.size() <= 1) {
|
---|
164 | return null;
|
---|
165 | }
|
---|
166 |
|
---|
167 | // Ask the user how they want to deal with this element
|
---|
168 | MetadataImportMappingPrompt metadata_import_mapping_prompt = new MetadataImportMappingPrompt(metadata_element_name_full);
|
---|
169 | int result = metadata_import_mapping_prompt.getResult();
|
---|
170 |
|
---|
171 | // Add the element into an existing metadata set
|
---|
172 | if (result == MetadataImportMappingPrompt.ADD_BUTTON_PRESSED) {
|
---|
173 | MetadataSet target_metadata_set = metadata_import_mapping_prompt.getSelectedMetadataSet();
|
---|
174 | String metadata_element_name = MetadataTools.getMetadataElementName(metadata_element_name_full);
|
---|
175 | target_metadata_set.addMetadataElementForThisSession(metadata_element_name);
|
---|
176 | target_metadata_element_name_full = target_metadata_set.getNamespace() + "." + metadata_element_name;
|
---|
177 | }
|
---|
178 |
|
---|
179 | // Replace the element with an element in an existing metadata set
|
---|
180 | if (result == MetadataImportMappingPrompt.REPLACE_BUTTON_PRESSED) {
|
---|
181 | MetadataElement target_metadata_element = metadata_import_mapping_prompt.getSelectedMetadataElement();
|
---|
182 | target_metadata_element_name_full = target_metadata_element.getFullName();
|
---|
183 | }
|
---|
184 |
|
---|
185 | // Ignore the element
|
---|
186 | if (result == MetadataImportMappingPrompt.IGNORE_BUTTON_PRESSED) {
|
---|
187 | target_metadata_element_name_full = "";
|
---|
188 | }
|
---|
189 |
|
---|
190 | // Store this import mapping for future elements with the same name
|
---|
191 | ProfileXMLFileManager.mapElement(metadata_element_name_full, target_metadata_element_name_full);
|
---|
192 |
|
---|
193 | return target_metadata_element_name_full;
|
---|
194 | }
|
---|
195 |
|
---|
196 |
|
---|
197 | static public void unloadMetadataSet(MetadataSet metadata_set)
|
---|
198 | {
|
---|
199 | // Find the metadata set in the list of loaded sets, and remove it
|
---|
200 | for (int i = 0; i < metadata_sets.size(); i++) {
|
---|
201 | if (metadata_set == (MetadataSet) metadata_sets.get(i)) {
|
---|
202 | metadata_sets.remove(i);
|
---|
203 | break;
|
---|
204 | }
|
---|
205 | }
|
---|
206 | }
|
---|
207 |
|
---|
208 |
|
---|
209 | static public void writeHierarchyFiles(File directory)
|
---|
210 | {
|
---|
211 | // Make sure the directory (etc) exists
|
---|
212 | if (directory.exists() == false) {
|
---|
213 | return;
|
---|
214 | }
|
---|
215 |
|
---|
216 | // Write a hierarchy file for each loaded metadata element, except extracted metadata
|
---|
217 | ArrayList every_metadata_set_element = getEveryMetadataSetElement();
|
---|
218 | for (int i = 0; i < every_metadata_set_element.size(); i++) {
|
---|
219 | MetadataElement metadata_element = (MetadataElement) every_metadata_set_element.get(i);
|
---|
220 | if (metadata_element.isExtractedMetadata() == false) {
|
---|
221 | File hierarchy_file = new File(directory, metadata_element.getFullName() + ".txt");
|
---|
222 | metadata_element.writeHierarchyFile(hierarchy_file);
|
---|
223 | }
|
---|
224 | }
|
---|
225 | }
|
---|
226 | }
|
---|