source: trunk/gli/src/org/greenstone/gatherer/gems/MetadataSetManager.java@ 8270

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

Source files for the Greenstone Editor for Metadata Sets (GEMS). This is currently just the old MetadataEditorManager, modified to run stand-alone. It will be substantially improved by Attila Aros.

  • Property svn:keywords set to Author Date Id Revision
File size: 11.9 KB
Line 
1/**
2 *#########################################################################
3 *
4 * A component of the Gatherer application, part of the Greenstone digital
5 * library suite from the New Zealand Digital Library Project at the
6 * University of Waikato, New Zealand.
7 *
8 * <BR><BR>
9 *
10 * Author: John Thompson, Greenstone Digital Library, University of Waikato
11 *
12 * <BR><BR>
13 *
14 * Copyright (C) 1999 New Zealand Digital Library Project
15 *
16 * <BR><BR>
17 *
18 * This program is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation; either version 2 of the License, or
21 * (at your option) any later version.
22 *
23 * <BR><BR>
24 *
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
29 *
30 * <BR><BR>
31 *
32 * You should have received a copy of the GNU General Public License
33 * along with this program; if not, write to the Free Software
34 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
35 *########################################################################
36 */
37package org.greenstone.gatherer.gems;
38
39import java.io.*;
40import java.util.*;
41import javax.swing.*;
42import javax.swing.filechooser.*;
43import javax.swing.tree.TreePath;
44import org.greenstone.gatherer.Configuration;
45import org.greenstone.gatherer.DebugStream;
46import org.greenstone.gatherer.util.Utility;
47import org.apache.xerces.parsers.*;
48import org.apache.xml.serialize.*;
49import org.w3c.dom.*;
50import org.xml.sax.*;
51
52/** This class is responsible for managing all the metadata sets in the collection and for providing methods for manipulating the aforementioned sets contents.
53 * @author John Thompson, Greenstone Digital Library, University of Waikato
54 * @version 2.3b
55 */
56public class MetadataSetManager {
57
58 /** The character used to separate name space from metadata element. */
59 static public char NS_SEP= '.';
60 /** The character used to separate subfields from metadata element. */
61 static public String SF_SEP= "#";
62
63 /** The name of the hidden, or system, metadata set. */
64 static final public String HIDDEN = "hidden";
65
66 /** A mapping from metadata namespace to metadata set. */
67 static private Hashtable mds_hashtable = new Hashtable();
68
69 /** The profiler is responsible for remembering what actions a user has requested when importing metadata, so as to prevent the user needlessly re-entering this information for each import. */
70 // public MSMProfiler profiler = null;
71
72
73 /** Constructor. */
74 public MetadataSetManager() {
75 }
76
77
78 public MetadataSet addSet(String namespace, String name) {
79 MetadataSet mds = new MetadataSet(Utility.METADATA_SET_TEMPLATE);
80 mds.setAttribute("creator", "The Greenstone Librarian Interface");
81 // Calculate lastchanged to right now on this machine by this user
82 String user_name = System.getProperty("user.name");
83 String machine_name = Utility.getMachineName();
84 mds.setAttribute("lastchanged", Utility.getDateString() + " - " + user_name + " on " + machine_name);
85 // And the remaining attributes.
86 //mds.setAttribute("name", name);
87 mds.setAttribute("namespace", namespace);
88 mds_hashtable.put(namespace, mds);
89 // Add the name element.
90 mds.setName(name);
91 // fireSetChanged(mds);
92 return mds;
93 }
94
95 /** Add a value tree to a given metadata element represented as a GValueModel
96 * @param model The <strong>GValueTree</strong> model
97 */
98 public void addValueTree(GValueModel model) {
99 ElementWrapper element = model.getElement();
100 String namespace = element.getNamespace();
101 MetadataSet mds = (MetadataSet) mds_hashtable.get(namespace);
102 if(mds != null) {
103 mds.addValueTree(element, model);
104 }
105 }
106
107
108 /** Destructor.
109 */
110 public void destroy() {
111 mds_hashtable.clear();
112 }
113
114
115 /** Used to get all the (non-hidden) elements in this manager.
116 * @return A Vector of ElementWrappers.
117 */
118 public Vector getElements() {
119 return getElements(false);
120 }
121
122 public Vector getElements(boolean all) {
123 return getElements(all, false);
124 }
125
126 /** Used to get all the elements in this manager.
127 * @param all <i>true</i> if all elements, including hidden, should be returned.
128 * @return A Vector of ElementWrappers.
129 */
130 public Vector getElements(boolean all, boolean force_extracted) {
131 Vector all_elements = new Vector();
132 for(Enumeration keys = mds_hashtable.keys(); keys.hasMoreElements(); ) {
133 MetadataSet mds = (MetadataSet)mds_hashtable.get(keys.nextElement());
134 if((!mds.getNamespace().equals(Utility.EXTRACTED_METADATA_NAMESPACE) && !mds.getNamespace().equals(HIDDEN))
135 || (mds.getNamespace().equals(Utility.EXTRACTED_METADATA_NAMESPACE) && (Configuration.get("general.view_extracted_metadata", Configuration.COLLECTION_SPECIFIC) || force_extracted))
136 || (mds.getNamespace().equals(Utility.EXTRACTED_METADATA_NAMESPACE) && mds.getNamespace().equals(HIDDEN) && all)) {
137 NodeList set_elements = mds.getElements();
138 ///ystem.err.println("The set " + mds + " has " + set_elements.getLength() + " elements.");
139 for(int i = 0; i < set_elements.getLength(); i++) {
140 Element raw_element = (Element)set_elements.item(i);
141 ElementWrapper element = new ElementWrapper(raw_element);
142 // For now we do not add subfield elements and their parents, just the subfields.
143 NodeList child_elements = raw_element.getElementsByTagName("Element");
144 if(child_elements.getLength() == 0) {
145 all_elements.add(element);
146 }
147 }
148 }
149 }
150 Collections.sort(all_elements);
151 return all_elements;
152 }
153
154 /** Retrieve a metadata element by its index.
155 * @param index The specified index as an int.
156 * @return An ElementWrapper containing the specied element, or <i>null</i> is no such element exists.
157 */
158 public ElementWrapper getElement(int index) {
159 Vector elements = getElements(false);
160 ElementWrapper result = null;
161 if(0 <= index && index < elements.size()) {
162 result = (ElementWrapper) elements.get(index);
163 }
164 return result;
165 }
166 /** Retrieve a metadata element by looking at the current metadata element. Note that this 'index' element may now be disconnected from the DOM model, so we have to reload the target element by the string method.
167 * @param element The possibly out-of-data MetadataElement.
168 * @return An ElementWrapper containing the specied element, or <i>null</i> is no such element exists.
169 */
170 public ElementWrapper getElement(ElementWrapper element) {
171 return getElement(element.toString());
172 }
173 /** Retrieve a metadata element by its fully qualified name.
174 * @param name The elements name as a String.
175 * @return An ElementWrapper containing the specied element, or <i>null</i> is no such element exists.
176 */
177 public ElementWrapper getElement(String name) {
178 return getElement(name, false);
179 }
180
181 public ElementWrapper getElement(String name, boolean perfect) {
182 ///ystem.err.println("Retrieve element " + name);
183 if(name == null) {
184 ///ystem.err.println("No name!");
185 return null;
186 }
187 ElementWrapper result = null;
188 MetadataSet set = null;
189 String element = null;
190 // First we seperate off what set it is in, where we have '<set><namespace_separator><element>'.
191 if(name.indexOf(NS_SEP) != -1) {
192 String namespace = name.substring(0, name.indexOf(NS_SEP));
193 // Retrieve the correct set if possible.
194 set = (MetadataSet)mds_hashtable.get(namespace);
195 namespace = null;
196 // Now retrieve the element name.
197 element = name.substring(name.indexOf(NS_SEP) + 1);
198 }
199 // If we are looking for a perfect match, we can assume that no namespace means extracted metadata
200 else if(!perfect) {
201 // No namespace so assume that its extracted metadata.
202 set = (MetadataSet)mds_hashtable.get(Utility.EXTRACTED_METADATA_NAMESPACE);
203 element = name;
204 }
205 if(set != null) {
206 // Now we have a set we are ready to locate the requested element. Break the remaining element name down by the subfield separator, attempting to retrieve the element indicated by each step.
207 if(element.indexOf(SF_SEP) != -1) {
208 StringTokenizer tokenizer = new StringTokenizer(element, SF_SEP);
209 // Has to be at least two tokens
210 if(tokenizer.countTokens() >= 2) {
211 Element current_element = set.getElement(tokenizer.nextToken());
212 while(current_element != null && tokenizer.hasMoreTokens()) {
213 current_element = set.getElement(current_element, tokenizer.nextToken());
214 }
215 if(current_element != null) {
216 result = new ElementWrapper(current_element);
217 current_element = null;
218 }
219 }
220 tokenizer = null;
221 }
222 // No subfields - much easier.
223 if(result == null) {
224 ///ystem.err.print("Trying to match element " + element +"?");
225 Element temp = set.getElement(element);
226 if(temp != null) {
227 result = new ElementWrapper(temp);
228 }
229 temp = null;
230 }
231 set = null;
232 }
233 element = null;
234 return result;
235 }
236 /** Retrieve a certain named element from a certain named set.
237 * @param set The metadata set whose element you want.
238 * @param name The name of the element.
239 * @return An ElementWrapper around the requested element, or null if no such set or element.
240 */
241 public ElementWrapper getElement(String set, String name) {
242 if(mds_hashtable.containsKey(set)) {
243 MetadataSet temp = (MetadataSet)mds_hashtable.get(set);
244 return new ElementWrapper(temp.getElement(name));
245 }
246 return null;
247 }
248
249 /** Retrieve the named metadata set.
250 * @param name The sets name as a String.
251 * @return The MetadataSet as named, or null if no such set.
252 */
253 public MetadataSet getSet(String name) {
254 if(mds_hashtable.containsKey(name)) {
255 return (MetadataSet) mds_hashtable.get(name);
256 }
257// else if(name.equals(HIDDEN)) {
258// return createHidden();
259// }
260 return null;
261 }
262
263 /** Method to retrieve all of the metadata sets loaded in this collection.
264 * @return A Vector of metadata sets.
265 */
266 public Vector getSets() {
267 return getSets(true);
268 }
269
270 public Vector getSets(boolean include_greenstone_extracted) {
271 Vector result = new Vector();
272 for(Enumeration keys = mds_hashtable.keys(); keys.hasMoreElements(); ) {
273 MetadataSet set = (MetadataSet)mds_hashtable.get(keys.nextElement());
274 if(!set.getNamespace().equals(HIDDEN) && (include_greenstone_extracted || !set.getNamespace().equals(Utility.EXTRACTED_METADATA_NAMESPACE))) {
275 result.add(set);
276 }
277 }
278 return result;
279 }
280
281
282 /** Get the value tree that matches the given element.
283 * @param element The ElementWrapper representing the element.
284 * @return The GValueModel representing the value tree or null.
285 */
286 public GValueModel getValueTree(ElementWrapper element) {
287 GValueModel value_tree = null;
288 if(element != null) {
289 String namespace = element.getNamespace();
290 if(namespace.length() > 0) {
291 MetadataSet mds = (MetadataSet) mds_hashtable.get(namespace);
292 if(mds != null) {
293 value_tree = mds.getValueTree(element);
294 }
295 }
296 }
297 return value_tree;
298 }
299
300
301 public MetadataSet loadMetadataSet(File metadata_set_file)
302 {
303 System.err.println("Loading metadata set file " + metadata_set_file + "...");
304 MetadataSet metadata_set = new MetadataSet(metadata_set_file);
305 mds_hashtable.put(metadata_set.getNamespace(), metadata_set);
306 return metadata_set;
307 }
308
309
310 public void removeElement(ElementWrapper element) {
311 // Retrieve the metadata set this element belongs to.
312 String namespace = element.getNamespace();
313 MetadataSet set = (MetadataSet) mds_hashtable.get(namespace);
314 if(set != null) {
315 // Bugger. Get the old name -before- we remove the element from the set.
316 String old_name = element.toString();
317 // Remove the element.
318 set.removeElement(element.getElement());
319 }
320 else {
321 ///ystem.err.println("no such set " + namespace);
322 }
323 // No such set. No such element.
324 }
325
326
327 public void removeSet(MetadataSet set) {
328 mds_hashtable.remove(set.getNamespace());
329 // fireSetChanged(set);
330 }
331}
Note: See TracBrowser for help on using the repository browser.