source: trunk/gli/src/org/greenstone/gatherer/cdm/CollectionMetaManager.java@ 4580

Last change on this file since 4580 was 4540, checked in by kjdon, 21 years ago

have changed teh way indexes are written to and read from the config file: greenstone extracted metadata has the ex namespace internally in the gatherer, but not in the index specifications in the config file.

  • Property svn:keywords set to Author Date Id Revision
File size: 14.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 */
37
38
39
40
41
42
43/* GPL_HEADER */
44package org.greenstone.gatherer.cdm;
45/**************************************************************************************
46 * Title: Gatherer
47 * Description: The Gatherer: a tool for gathering and enriching a digital collection.
48 * Copyright: Copyright (c) 2001
49 * Company: The University of Waikato
50 * Written: /05/02
51 * Revised: 22/08/02 Revamped, Optimized and Commented.
52 **************************************************************************************/
53import java.awt.*;
54import java.awt.event.*;
55import java.util.*;
56import javax.swing.*;
57import javax.swing.event.*;
58import org.greenstone.gatherer.Gatherer;
59import org.greenstone.gatherer.cdm.CollectionDesignManager;
60import org.greenstone.gatherer.cdm.CollectionMeta;
61import org.greenstone.gatherer.cdm.Index;
62import org.greenstone.gatherer.cdm.Language;
63import org.greenstone.gatherer.gui.EditorDialog;
64import org.greenstone.gatherer.util.Utility;
65/** This class is responsible for maintaining a list of assigned collection level metadata, and for allows manipulations on the aforementioned data.
66 * @author John Thompson, Greenstone Digital Library, University of Waikato
67 * @version 2.3
68 */
69// ####################################################################################
70// Optimization Saving
71// ####################################################################################
72// Removed Vector and Hashtable + Memory, + Processor
73// ####################################################################################
74public class CollectionMetaManager
75 extends DefaultListModel {
76 /** A reference to the cdm manager so we can access indexes and languages. */
77 private CollectionDesignManager manager = null;
78 /** A reference to ourself so that inner classes can use us as a model. */
79 private DefaultListModel model = null;
80 /** A reference to the Gatherer. */
81 private Gatherer gatherer = null;
82 /** The language the most recent metadata returned was in. */
83 private Language current_language = null;
84 /** We can't safely parse metadata commands until after all the other commands have been parsed, so we store commands here for now. */
85 private ArrayList unresolved_commands = null;
86 /** Constructor.
87 * @param gatherer A reference to the <strong>Gatherer</strong>.
88 * @param manager A reference to the <strong>CollectionDesignManager</strong> for access to other configuration managers.
89 */
90 public CollectionMetaManager(Gatherer gatherer, CollectionDesignManager manager) {
91 super();
92 this.gatherer = gatherer;
93 this.manager = manager;
94 this.model = this;
95 this.unresolved_commands = new ArrayList();
96 }
97 /** Method to add a new piece of metadata.
98 * @param metadata The new <strong>CollectionMeta</strong>.
99 */
100 public void addMetadata(CollectionMeta metadata) {
101 CollectionMeta existing = getMetadata(metadata.getName().toString(), metadata.getLanguage(), false);
102 if(existing != null) {
103 removeElement(existing);
104 }
105 addElement(metadata);
106 gatherer.c_man.configurationChanged();
107 }
108 /** Retrieve the collectionextra metadata in the default language, if present.
109 * @return A <strong>CollectionMeta</strong> containing the collectionextra in the default language if present, or else the first collectionextra of any language, otherwise <i>null</i>.
110 * @see org.greenstone.gatherer.cdm.Language
111 */
112 public CollectionMeta getCollectionExtra() {
113 CollectionMeta result = getMetadata("collectionextra", manager.languages.getDefaultLanguage(), true);
114 if(result == null) {
115 result = new CollectionMeta(manager, "collectionextra", manager.languages.getDefaultLanguage(), "");
116 addMetadata(result);
117 }
118 return result;
119 }
120 /** Retrieve the collectionname metadata in the default language, if present.
121 * @return A <strong>CollectionMeta</strong> containing the collectionname in the default language if present, or else the first collectionname of any language, otherwise <i>null</i>.
122 * @see org.greenstone.gatherer.cdm.Language
123 */
124 public CollectionMeta getCollectionName() {
125 return getMetadata("collectionname", manager.languages.getDefaultLanguage(), true);
126 }
127 /** Retrieve the iconcollection metadata in the default language, if present.
128 * @return A <strong>CollectionMeta</strong> containing the iconcollection in the default language if present, or else the first iconcollection of any language, otherwise <i>null</i>.
129 * @see org.greenstone.gatherer.cdm.Language
130 */
131 public CollectionMeta getIconCollection() {
132 CollectionMeta result = getMetadata("iconcollection", manager.languages.getDefaultLanguage(), true);
133 if(result == null) {
134 result = new CollectionMeta(manager, "iconcollection", manager.languages.getDefaultLanguage(), "");
135 addMetadata(result);
136 }
137 return result;
138 }
139 /** Method to retrieve the list of metadata.
140 * @return An <strong>ArrayList</strong> containing the metadata.
141 */
142 public ArrayList getMetadata() {
143 ArrayList metadata = new ArrayList();
144 for(int i = 0; i < size(); i++) {
145 metadata.add(get(i));
146 }
147 Collections.sort(metadata);
148 return metadata;
149 }
150
151 /** Retrieve all of the metadata for the given feature, regardless of language. */
152 public ArrayList getMetadata(Object name) {
153 ArrayList result = new ArrayList();
154 for(int i = 0; i < size(); i++) {
155 CollectionMeta metadata = (CollectionMeta) get(i);
156 if(metadata.getName().equals(name)) {
157 result.add(metadata);
158 }
159 }
160 return result;
161 }
162 /** Method to retrieve a certain piece of metadata based on its name and language.
163 * @param name the name of the metadata as an Object (as it may actually be a refernce to an Index or SubIndex)
164 * @param language The <strong>Language</strong> of the metadata.
165 * @param partial <i>true</i> to return the first partial match (ie matches name but not language).
166 * @return The <strong>CollectionMeta</strong> requested, or <i>null</i> if no such metadata.
167 */
168 public CollectionMeta getMetadata(Object name, Language language, boolean partial) {
169 CollectionMeta partial_match = null;
170 for(int i = 0; i < size(); i++) {
171 CollectionMeta metadata = (CollectionMeta) get(i);
172 Object metadata_name = metadata.getName();
173 // We test the case of an object match (ie Index to Index)...
174 if(metadata_name.equals(name)) {
175 if (metadata.getLanguage().equals(language)) {
176 return metadata;
177 }
178 partial_match = metadata;
179 }
180 // But if that fails we also try a string match such that "section:dls.Title" == Index
181 if(metadata_name.toString().equals(name.toString())) {
182 if (metadata.getLanguage().equals(language)) {
183 return metadata;
184 }
185 partial_match = metadata;
186 }
187 }
188 if(partial) {
189 return partial_match;
190 }
191 return null;
192 }
193 /** Method that attempts to parse a collection metadata command from the given text. If a command is parsed successfully it is immediately added to the the collections metadata.
194 * @param command The command text we wish to parse, as a <strong>String</strong>.
195 * @return A <i>boolean</i> which is <i>true</i> if a collection metadata command was successfully parsed, <i>false</i> otherwise.
196 * @see org.greenstone.gatherer.cdm.CollectionDesignManager
197 * @see org.greenstone.gatherer.cdm.CommandTokenizer
198 * @see org.greenstone.gatherer.cdm.Language
199 * @see org.greenstone.gatherer.cdm.LanguageManager
200 * @see org.greenstone.gatherer.util.Utility
201 */
202 public boolean parse(String command, boolean finished) {
203 String temp = command.toLowerCase();
204 if(temp.startsWith("collectionmeta")) {
205 if(finished) {
206 CommandTokenizer ct = new CommandTokenizer(command);
207 if(ct.countTokens() >= 3) {
208 ct.nextToken(); // Throw away collectionmeta
209 Object key = ct.nextToken();
210 Language language = null;
211 String language_code = null;
212 String value = ct.nextToken();
213 // Arg. Remember a language token will be '[l=<code>]'
214 if(value.startsWith("[") && value.endsWith("]")) {
215 language_code = value.substring(3, value.length() - 1);
216 ///ystem.err.println("Language code = " + language_code);
217 value = ct.nextToken();
218 }
219 // Check if the key is an index, and if so retrieve it.
220 if(((String)key).startsWith(".")) {
221 ///ystem.err.println("Detected '.' prefix.");
222 String key_str = (String)key;
223 key = null;
224 key_str = key_str.substring(1);
225 Index ind = Index.parseIndexConfig(key_str, manager);
226 if (ind != null) {
227
228 key = manager.indexes.getIndex(ind.toString(false));
229 }
230
231 // If that didn't work, then it might be a subindex so have a bash at retrieving that instead.
232 if(key == null) {
233 key = manager.subcollections.getSubIndex(key_str);
234 //if(key != null) {
235 ///ystem.err.println("Its a subindex.");
236 //}
237 //else {
238 ///ystem.err.println("Error Will Robinson.");
239 //}
240 }
241 //else {
242 ///ystem.err.println("Its an index.");
243 //}
244 }
245 // An if we have a language code, retrieve its object too.
246 if(language_code != null) {
247 language = manager.languages.getLanguage(language_code, false);
248 }
249 // Otherwise set language to the default language.
250 else {
251 language = manager.languages.getDefaultLanguage();
252 }
253 if(key != null) {
254 // Trim any "
255 if(value.equals("\"\"")) {
256 value = "";
257 }
258 else {
259 if(value.startsWith("\"")) {
260 value = value.substring(1);
261 }
262 if(value.endsWith("\"")) {
263 value = value.substring(0, value.length() - 1);
264 }
265 }
266 CollectionMeta meta = new CollectionMeta(manager, key, language, Utility.decodeGreenstone(value));
267 addMetadata(meta);
268 }
269 return true;
270 }
271 }
272 else {
273 unresolved_commands.add(command);
274 return true;
275 }
276 }
277 return false;
278 }
279 /** Ensure that the values being showed are the most up to date. */
280 public void refresh() {
281 fireContentsChanged(this, 0, size());
282 }
283
284 /** Method to remove a piece of metadata.
285 * @param metadata The <strong>CollectionMeta</strong> you wish to remove.
286 */
287 public void removeMetadata(CollectionMeta metadata) {
288 removeElement(metadata);
289 gatherer.c_man.configurationChanged();
290 }
291 /** Method which attempts to reparse obvious metadata commands which used unresovable references.
292 */
293 public void reparseUnresolved() {
294 for(int i = 0; i < unresolved_commands.size(); i++) {
295 parse((String)unresolved_commands.get(i), true);
296 }
297 // Regardless of if they work, clear the commands.
298 unresolved_commands.clear();
299 }
300 /** Sets the value of a certain metadata.
301 * @param name The name of the metadata as a <strong>String</strong>.
302 * @param language The <strong>Language</strong> to use.
303 * @param value The value of the metadata also as a <strong>String</strong>.
304 */
305 public void setMetadata(String name, Language language, String value) {
306 addMetadata(new CollectionMeta(manager, name, language, value));
307 }
308 /** Method to produce the list of metadata in a string such as you would find in the collection configuration file.
309 * @return A <strong>String</strong> containing the list of collection metadata.
310 */
311 public String toString() {
312 // We sort the metadata first.
313 ArrayList metadatum = new ArrayList();
314 for(int i = 0; i < size(); i++) {
315 metadatum.add(get(i));
316 }
317 Collections.sort(metadatum, new CollectionMetaComparator());
318 // Now we print them
319 StringBuffer text = new StringBuffer("");
320 for(int i = 0; i < metadatum.size(); i++) {
321 CollectionMeta data = (CollectionMeta) metadatum.get(i);
322 if(data.valid()) {
323 text.append(data.toString());
324 }
325 }
326 metadatum = null;
327 text.append("\n");
328 return text.toString();
329 }
330 /** Overloaded to call get with both a key and an empty argument array.
331 * @param key A <strong>String</strong> which is mapped to a initial String within the ResourceBundle.
332 * @return A <strong>String</strong> which has been referenced by the key String and that either contains no argument fields, or has had the argument fields automatiically populated with formatting Strings of with argument String provided in the get call.
333 */
334 private String get(String key) {
335 return get(key, null);
336 }
337 /** Used to retrieve a property value from the Locale specific ResourceBundle, based upon the key and arguments supplied. If the key cannot be found or if some other part of the call fails a default (English) error message is returned. <BR>
338 * Here the get recieves a second argument which is an array of Strings used to populate argument fields, denoted {<I>n</I>}, within the value String returned. Note that argument numbers greater than or equal to 32 are automatically mapped to the formatting String named Farg<I>n</I>.
339 * @param key A <strong>String</strong> which is mapped to a initial String within the ResourceBundle.
340 * @param args A <strong>String[]</strong> used to populate argument fields within the complete String.
341 * @return A <strong>String</strong> which has been referenced by the key String and that either contains no argument fields, or has had the argument fields automatiically populated with formatting Strings of with argument String provided in the get call.
342 * @see org.greenstone.gatherer.Gatherer
343 * @see org.greenstone.gatherer.Dictionary
344 */
345 private String get(String key, String args[]) {
346 if(key.indexOf('.') == -1) {
347 key = "CDM.CollectionMetaManager." + key;
348 }
349 return gatherer.dictionary.get(key, args);
350 }
351
352 private class CollectionMetaComparator
353 implements Comparator {
354 public int compare(Object o1, Object o2) {
355 String s1 = o1.toString();
356 String s2 = o2.toString();
357 if(s1.startsWith(".")) {
358 return 1;
359 }
360 else if(s2.startsWith(".")) {
361 return -1;
362 }
363 return o1.toString().compareTo(o2.toString());
364 }
365 public boolean equals(Object o2) {
366 return (compare(this, o2) == 0);
367 }
368 }
369}
Note: See TracBrowser for help on using the repository browser.