source: trunk/gli/src/org/greenstone/gatherer/cdm/CollectionDesignManager.java@ 4675

Last change on this file since 4675 was 4675, checked in by jmt12, 21 years ago

Sunday's work

  • Property svn:keywords set to Author Date Id Revision
File size: 18.0 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 * Company: The University of Waikato
49 * Written: 02/05/02
50 * Revised: 16/08/02 Optimized and added Destructor.
51 **************************************************************************************/
52import java.awt.BorderLayout;
53import java.awt.Rectangle;
54import java.io.BufferedReader;
55import java.io.File;
56import java.io.FileReader;
57import java.io.FileOutputStream;
58import java.lang.Exception;
59import java.lang.String;
60import java.lang.StringBuffer;
61import java.util.ArrayList;
62import java.util.StringTokenizer;
63import javax.swing.JPanel;
64import org.greenstone.gatherer.Gatherer;
65import org.greenstone.gatherer.cdm.ClassifierManager;
66import org.greenstone.gatherer.cdm.CollectionMetaManager;
67import org.greenstone.gatherer.cdm.FormatManager;
68import org.greenstone.gatherer.cdm.GUI;
69import org.greenstone.gatherer.cdm.Index;
70import org.greenstone.gatherer.cdm.IndexManager;
71import org.greenstone.gatherer.cdm.MetadataSetManager;
72import org.greenstone.gatherer.cdm.PlugInManager;
73import org.greenstone.gatherer.cdm.SubcollectionManager;
74import org.greenstone.gatherer.util.EmailAddress;
75/** This manager provides access to submanagers, which in turn provide tools for the designing of Greenstone collections via the information stored in etc/collect.cfg. This class acts as a hub for the managers that handle specific parts of the configuration such as classifiers, format strings and language settings.
76 * @author John Thompson, Greenstone Digital Library, University of Waikato
77 * @version 2.3
78 */
79// ####################################################################################
80// Optimization Saving
81// ####################################################################################
82// Vector -> ArrayList + Processor
83// String -> StringBuffer + Memory, + Processor
84// ####################################################################################
85public class CollectionDesignManager {
86 /** Whether this collection is to be made public or not. */
87 public boolean public_col = false;
88 /** Whether this collection is a beta version or not. */
89 public boolean beta = true;
90 /** A list of classifiers to use at build time. */
91 public ClassifierManager classifiers = null;
92 /** A manager of collection level metadata. */
93 public CollectionMetaManager collectionmetadatum = null;
94 /** E-mail address of the collection's creator. */
95 public EmailAddress creator = null;
96 /** E-mail address of the collection's maintainer. */
97 public EmailAddress maintainer = null;
98 /** The collection configuration file. */
99 public File in_file = null;
100 /** A list of formating strings to use at build time. */
101 public FormatManager formats = null;
102 /** The manager in charge of displaying this manager and the controls for other managers. */
103 public GUI gui = null;
104 /** List of indexes to be built, and the default index. */
105 public IndexManager indexes = null;
106 /** Contains instructions dealing with the collection language. */
107 public LanguageManager languages = null;
108 /** A simple manager for the visual review of metadata sets. */
109 public MetadataSetManager metadatasets = null;
110 /** A list of plugins to use at build time. */
111 public PlugInManager plugins = null;
112 /** Contains: A list of subcollections, (defined on metadatadata), a list of which subcollection indexes to build and the default subcollection index. */
113 public SubcollectionManager subcollections = null;
114 /** A supercollection command allows a single search to be conducted across several collections. It is a very basic command and so avoids all the crazy model stuff that exists in most of the design managers. */
115 public SuperCollectionManager superman; // Just cause I could ;p
116 /** The text translation manager. */
117 public TranslationManager transman;
118 /** A list of whatever commands could not be parsed at all. */
119 private ArrayList rest = null;
120 /** A reference to the Gatherer. */
121 private Gatherer gatherer = null;
122 /** Constructor.
123 * @param gatherer The <strong>Gatherer</strong> that created this class.
124 * @see org.greenstone.gatherer.cdm.ClassifierManager
125 * @see org.greenstone.gatherer.cdm.CollectionMetaManager
126 * @see org.greenstone.gatherer.cdm.FormatManager
127 * @see org.greenstone.gatherer.cdm.IndexManager
128 * @see org.greenstone.gatherer.cdm.LanguageManager
129 * @see org.greenstone.gatherer.cdm.MetadataSetManager
130 * @see org.greenstone.gatherer.cdm.PlugInManager
131 * @see org.greenstone.gatherer.cdm.SubcollectionManager
132 */
133 public CollectionDesignManager() {
134 this.gatherer = Gatherer.self;
135 this.classifiers = new ClassifierManager(gatherer, this);
136 this.collectionmetadatum = new CollectionMetaManager(gatherer, this);
137 this.formats = new FormatManager(gatherer, this);
138 this.indexes = new IndexManager(gatherer, this);
139 this.languages = new LanguageManager(gatherer, this);
140 this.metadatasets = new MetadataSetManager(gatherer);
141 this.plugins = new PlugInManager(gatherer, this);
142 this.rest = new ArrayList();
143 this.subcollections = new SubcollectionManager(gatherer, this);
144 this.superman = new SuperCollectionManager();
145 this.transman = new TranslationManager(this);
146 }
147 /** In order to prevent Components that have registered themselves wasting memory, this method invalidates each of the sub-managers controls, causing them to unregister listeners.
148 * @see org.greenstone.gatherer.cdm.ClassifierManager
149 * @see org.greenstone.gatherer.cdm.CollectionMetaManager
150 * @see org.greenstone.gatherer.cdm.FormatManager
151 * @see org.greenstone.gatherer.cdm.IndexManager
152 * @see org.greenstone.gatherer.cdm.LanguageManager
153 * @see org.greenstone.gatherer.cdm.MetadataSetManager
154 * @see org.greenstone.gatherer.cdm.PlugInManager
155 * @see org.greenstone.gatherer.cdm.SubcollectionManager
156 */
157 public void destroy() {
158 // Remove visual the component from its parent.
159 if(gui.getParent() != null) {
160 gui.getParent().remove(gui);
161 }
162 // Remove references from persistant listeners.
163 if(classifiers != null && formats != null && gui != null && indexes != null && languages != null && metadatasets != null && plugins != null && subcollections != null) {
164 classifiers.invalidateControls();
165 formats.invalidateControls();
166 gui.invalidateControls();
167 indexes.invalidateControls();
168 languages.invalidateControls();
169 metadatasets.invalidateControls();
170 plugins.invalidateControls();
171 subcollections.invalidateControls();
172 superman.invalidateControls();
173 }
174 // Null globals.
175 classifiers = null;
176 collectionmetadatum = null;
177 creator = null;
178 maintainer = null;
179 in_file = null;
180 formats = null;
181 gui = null;
182 indexes = null;
183 languages = null;
184 metadatasets = null;
185 plugins = null;
186 subcollections = null;
187 superman = null;
188 rest = null;
189 gatherer = null;
190 }
191
192 /** Display the GUI interface for the CollectionDesignManager in the centre of the indicated panel.
193 * @param target The <strong>JPanel</strong> you wish to display the GUI on.
194 * @see org.greenstone.gatherer.cdm.GUI
195 */
196 public void display(JPanel target) {
197 this.gui = new GUI(gatherer, this);
198 target.add(gui, BorderLayout.CENTER);
199 }
200 /** When the tab on the JTabbedPane that contains the GUI is selected, this method is called to ensure that the controls are all up to date, in terms of references to metadata etc.
201 * @see org.greenstone.gatherer.cdm.GUI
202 */
203 public void gainFocus() {
204 gui.updateUI();
205 }
206 /** Retrieve the current set of indexes as defined by the user configuration.
207 * @return An <strong>ArrayList</strong> of indexes.
208 * @see org.greenstone.gatherer.cdm.Index
209 * @see org.greenstone.gatherer.cdm.IndexManager
210 */
211 public ArrayList getIndexes() {
212 ArrayList result = new ArrayList();
213 int size = indexes.size();
214 for(int i = 0; i < size; i++) {
215 result.add(indexes.getIndex(i));
216 }
217 return result;
218 }
219 /** Retrieve the name of the collection configuration file which is being used as the source of the information in this object.
220 * @return The files absolute path as a <strong>String</strong>.
221 */
222 public String getFilename() {
223 return in_file.getAbsolutePath();
224 }
225 /** Used to parse the given file as if it was a collection configuration file, passing any parsed commands to the relevant submanager. Currently ignores any comments, and marks any other lines as being unrecognizable if they can't be parsed.
226 * @param filename The name of the file you wish to attempt to pass as a <strong>String</strong>.
227 * @see org.greenstone.gatherer.cdm.ClassifierManager
228 * @see org.greenstone.gatherer.cdm.CollectionMetaManager
229 * @see org.greenstone.gatherer.cdm.FormatManager
230 * @see org.greenstone.gatherer.cdm.IndexManager
231 * @see org.greenstone.gatherer.cdm.LanguageManager
232 * @see org.greenstone.gatherer.cdm.MetadataSetManager
233 * @see org.greenstone.gatherer.cdm.PlugInManager
234 * @see org.greenstone.gatherer.cdm.SubcollectionManager
235 * @see org.greenstone.gatherer.util.EmailAddress
236 */
237 private BufferedReader in;
238 /** If one of the submanagers detects that it hasn't been provided enough arguments (such as format which always requires at least three) it can call this method to request another line of text from the collect.cfg. If the collect.cfg was not well formed this will cause some problems, but that can't be helped.
239 * @param prefix the String we have so far
240 * @return a String with a further line form the collect.cfg appended
241 */
242 public String parseMore(String prefix) {
243 ///ystem.err.println("Command incomplete: " + prefix);
244 try {
245 String suffix = in.readLine();
246 ///ystem.err.println("Adding: " + suffix);
247 if(suffix != null) {
248 prefix = prefix + " " + suffix;
249 }
250 suffix = null;
251 }
252 catch(Exception error) {
253 error.printStackTrace();
254 }
255 return prefix;
256 }
257
258 public void parse(String filename) {
259 try {
260 in_file = new File(filename);
261 FileReader in_reader = new FileReader(in_file);
262 in = new BufferedReader(in_reader);
263 String command = null;
264 while((command = in.readLine()) != null) {
265 if(command.length() > 0) {
266 // Now we've finished parsing a command line, see what manager wants a piece of it.
267 // We have to test the end of command for the special character '\'. If found, remove it and append the next line, then repeat.
268 while(command.trim().endsWith("\\")) {
269 String next_line = (in.readLine()).trim();
270 if(next_line != null && next_line.length() > 0) {
271 command = command.substring(0, command.lastIndexOf("\\"));
272 command = command + next_line;
273 }
274 }
275 // Now we've finished parsing a command line, see what manager wants a piece of it.
276 boolean found = false;
277 String command_lc = command.toLowerCase();
278 if(command_lc.startsWith("creator")) {
279 creator = new EmailAddress(gatherer, command);
280 found = true;
281 }
282 if(command_lc.startsWith("maintainer")) {
283 maintainer = new EmailAddress(gatherer, command);
284 found = true;
285 }
286 if(command_lc.startsWith("public")) {
287 if(command_lc.endsWith("true")) {
288 public_col = true;
289 }
290 else {
291 public_col = false;
292 }
293 found = true;
294 }
295 if(command_lc.startsWith("beta")) {
296 if(command_lc.endsWith("false")) {
297 beta = false;
298 }
299 else {
300 beta = true;
301 }
302 found = true;
303 }
304 if(!found) {
305 found = indexes.parse(command);
306 }
307 if(!found) {
308 found = subcollections.parse(command, false);
309 }
310 if(!found) {
311 found = languages.parse(command);
312 }
313 if(!found) {
314 found = plugins.parse(command);
315 }
316 if(!found) {
317 found = classifiers.parse(command);
318 }
319 if(!found) {
320 found = formats.parse(command, false);
321 }
322 if(!found) {
323 found = collectionmetadatum.parse(command, false);
324 }
325 if(!found) {
326 found = superman.parse(command);
327 }
328 // Metadataset commands
329 if(command_lc.startsWith("metadataset")) {
330 // Nothing yet. Eventually used to import metadata.
331 found = true;
332 }
333 // Comments we ignore.
334 if(command_lc.startsWith("# these instructions are not recognized by the gatherer.")) {
335 // Ignore
336 found = true;
337 }
338 // We have been unable to parse this command, add it to rest.
339 if(!found) {
340 rest.add(command);
341 }
342 }
343 }
344 in.close();
345 // Now attempt to finalize any commands that were not immediately parsed as they were waiting for unresolved references.
346 subcollections.reparseUnresolved();
347 classifiers.reparseUnresolved();
348 formats.reparseUnresolved();
349 collectionmetadatum.reparseUnresolved();
350 }
351 catch(Exception error) {
352 error.printStackTrace();
353 }
354 }
355 /** Cause the current definitions within the Collection Design Manager to be written back out to whatever collect.cfg it is based upon, taking care to change reference pointers into something more sensible within the text file.
356 * @see org.greenstone.gatherer.cdm.ClassifierManager
357 * @see org.greenstone.gatherer.cdm.CollectionMetaManager
358 * @see org.greenstone.gatherer.cdm.FormatManager
359 * @see org.greenstone.gatherer.cdm.IndexManager
360 * @see org.greenstone.gatherer.cdm.LanguageManager
361 * @see org.greenstone.gatherer.cdm.MetadataSetManager
362 * @see org.greenstone.gatherer.cdm.PlugInManager
363 * @see org.greenstone.gatherer.cdm.SubcollectionManager
364 * @see org.greenstone.gatherer.util.EmailAddress
365 */
366 public void save() {
367 try {
368 // If the file already exists (it should) rename it.
369 if(in_file.exists()) {
370 String filename = in_file.getAbsolutePath();
371 File backup = new File(filename + "~");
372 backup.deleteOnExit();
373 if(!in_file.renameTo(backup)) {
374 Gatherer.println("Error in CollectionDesignManager.parse(): FileRenamedException");
375 }
376 in_file = new File(filename); // Just in case we moved it.
377 }
378 FileOutputStream out = new FileOutputStream(in_file);
379
380 StringBuffer text = new StringBuffer("");
381 if(creator != null) {
382 text.append(creator.toString());
383 text.append("\n");
384 }
385 if(maintainer != null) {
386 text.append(maintainer.toString());
387 text.append("\n");
388 }
389 if(public_col) {
390 text.append("public true\n");
391 }
392 else {
393 text.append("public false\n");
394 }
395 if(beta) {
396 text.append("beta true\n");
397 }
398 else {
399 text.append("beta false\n");
400 }
401 if(text.length() > 0) {
402 text.append("\n");
403 }
404 if(plugins.size() > 0) {
405 text.append(plugins.toString());
406 }
407 if(indexes.size() > 0) {
408 text.append(indexes.toString());
409 }
410 if(languages.size() > 0) {
411 text.append(languages.toString());
412 }
413 if(subcollections.size() > 0) {
414 text.append(subcollections.toString());
415 }
416 String super_collections_command = superman.toString();
417 if(super_collections_command != null) {
418 text.append(super_collections_command);
419 }
420 if(classifiers.size() > 0) {
421 text.append(classifiers.toString());
422 }
423 if(formats.size() > 0) {
424 text.append(formats.toString());
425 }
426 if(collectionmetadatum.size() > 0) {
427 text.append(collectionmetadatum.toString());
428 }
429 text.append(metadatasets.toString());
430 if(rest.size() > 0) {
431 // Write out rest at the bottom.
432 text.append("# These instructions are not recognized by the Gatherer.\n");
433 for(int i = 0; i < rest.size(); i++) {
434 text.append(rest.get(i));
435 text.append("\n");
436 }
437 }
438 out.write(text.toString().getBytes());
439 out.close();
440 out = null;
441 }
442 catch(Exception error) {
443 error.printStackTrace();
444 }
445 }
446 /** Method used during a global search and replace to highlight the appropriate record within the Collection Design Managers version of the Metadata Set Manager (view only).
447 * @param element The name of the desired element as a <strong>String</strong>.
448 * @see org.greenstone.gatherer.cdm.GUI
449 * @see org.greenstone.gatherer.cdm.MetadataSetManager
450 */
451 public Rectangle setSelectedElement(String element) {
452 // First ensure that the metadata set controls are visible.
453 gui.setSelectedView("CDM.GUI.MetadataSets");
454 // Then tell them to select the given element.
455 return metadatasets.setSelectedElement(element);
456 }
457}
Note: See TracBrowser for help on using the repository browser.