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

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

re-tabbed the code for java

  • Property svn:keywords set to Author Date Id Revision
File size: 15.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 * 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 list of whatever commands could not be parsed at all. */
115 private ArrayList rest = null;
116 /** A reference to the Gatherer. */
117 private Gatherer gatherer = null;
118 /** Constructor.
119 * @param gatherer The <strong>Gatherer</strong> that created this class.
120 * @see org.greenstone.gatherer.cdm.ClassifierManager
121 * @see org.greenstone.gatherer.cdm.CollectionMetaManager
122 * @see org.greenstone.gatherer.cdm.FormatManager
123 * @see org.greenstone.gatherer.cdm.IndexManager
124 * @see org.greenstone.gatherer.cdm.LanguageManager
125 * @see org.greenstone.gatherer.cdm.MetadataSetManager
126 * @see org.greenstone.gatherer.cdm.PlugInManager
127 * @see org.greenstone.gatherer.cdm.SubcollectionManager
128 */
129 public CollectionDesignManager() {
130 this.gatherer = Gatherer.self;
131 this.classifiers = new ClassifierManager(gatherer, this);
132 this.collectionmetadatum = new CollectionMetaManager(gatherer, this);
133 this.formats = new FormatManager(gatherer, this);
134 this.indexes = new IndexManager(gatherer, this);
135 this.languages = new LanguageManager(gatherer, this);
136 this.metadatasets = new MetadataSetManager(gatherer);
137 this.plugins = new PlugInManager(gatherer, this);
138 this.rest = new ArrayList();
139 this.subcollections = new SubcollectionManager(gatherer);
140 }
141 /** 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.
142 * @see org.greenstone.gatherer.cdm.ClassifierManager
143 * @see org.greenstone.gatherer.cdm.CollectionMetaManager
144 * @see org.greenstone.gatherer.cdm.FormatManager
145 * @see org.greenstone.gatherer.cdm.IndexManager
146 * @see org.greenstone.gatherer.cdm.LanguageManager
147 * @see org.greenstone.gatherer.cdm.MetadataSetManager
148 * @see org.greenstone.gatherer.cdm.PlugInManager
149 * @see org.greenstone.gatherer.cdm.SubcollectionManager
150 */
151 public void destroy() {
152 // Remove visual the component from its parent.
153 if(gui.getParent() != null) {
154 gui.getParent().remove(gui);
155 }
156 // Remove references from persistant listeners.
157 if(classifiers != null && formats != null && gui != null && indexes != null && languages != null && metadatasets != null && plugins != null && subcollections != null) {
158 classifiers.invalidateControls();
159 formats.invalidateControls();
160 gui.invalidateControls();
161 indexes.invalidateControls();
162 languages.invalidateControls();
163 metadatasets.invalidateControls();
164 plugins.invalidateControls();
165 subcollections.invalidateControls();
166 }
167 // Null globals.
168 classifiers = null;
169 collectionmetadatum = null;
170 creator = null;
171 maintainer = null;
172 in_file = null;
173 formats = null;
174 gui = null;
175 indexes = null;
176 languages = null;
177 metadatasets = null;
178 plugins = null;
179 subcollections = null;
180 rest = null;
181 gatherer = null;
182 }
183
184 /** Display the GUI interface for the CollectionDesignManager in the centre of the indicated panel.
185 * @param target The <strong>JPanel</strong> you wish to display the GUI on.
186 * @see org.greenstone.gatherer.cdm.GUI
187 */
188 public void display(JPanel target) {
189 this.gui = new GUI(gatherer, this);
190 target.add(gui, BorderLayout.CENTER);
191 }
192 /** 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.
193 * @see org.greenstone.gatherer.cdm.GUI
194 */
195 public void gainFocus() {
196 gui.updateUI();
197 }
198 /** Retrieve the current set of indexes as defined by the user configuration.
199 * @return An <strong>ArrayList</strong> of indexes.
200 * @see org.greenstone.gatherer.cdm.Index
201 * @see org.greenstone.gatherer.cdm.IndexManager
202 */
203 public ArrayList getIndexes() {
204 ArrayList result = new ArrayList();
205 int size = indexes.size();
206 for(int i = 0; i < size; i++) {
207 result.add(indexes.getIndex(i));
208 }
209 return result;
210 }
211 /** Retrieve the name of the collection configuration file which is being used as the source of the information in this object.
212 * @return The files absolute path as a <strong>String</strong>.
213 */
214 public String getFilename() {
215 return in_file.getAbsolutePath();
216 }
217 /** 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.
218 * @param filename The name of the file you wish to attempt to pass as a <strong>String</strong>.
219 * @see org.greenstone.gatherer.cdm.ClassifierManager
220 * @see org.greenstone.gatherer.cdm.CollectionMetaManager
221 * @see org.greenstone.gatherer.cdm.FormatManager
222 * @see org.greenstone.gatherer.cdm.IndexManager
223 * @see org.greenstone.gatherer.cdm.LanguageManager
224 * @see org.greenstone.gatherer.cdm.MetadataSetManager
225 * @see org.greenstone.gatherer.cdm.PlugInManager
226 * @see org.greenstone.gatherer.cdm.SubcollectionManager
227 * @see org.greenstone.gatherer.util.EmailAddress
228 */
229 public void parse(String filename) {
230 try {
231 in_file = new File(filename);
232 FileReader in_reader = new FileReader(in_file);
233 BufferedReader in = new BufferedReader(in_reader);
234 String command = null;
235 while((command = in.readLine()) != null) {
236 if(command.length() > 0) {
237 // We have to test the end of command for the special character '\'. If found, remove it and append the next line, then repeat.
238 while(command.trim().endsWith("\\")) {
239 command = command.substring(0, command.lastIndexOf("\\"));
240 String next_line = in.readLine();
241 if(next_line != null) {
242 command = command + next_line;
243 }
244 }
245 // Now we've finished parsing a command line, see what manager wants a piece of it.
246 boolean found = false;
247 String command_lc = command.toLowerCase();
248 if(command_lc.startsWith("creator")) {
249 creator = new EmailAddress(gatherer, command);
250 found = true;
251 }
252 if(command_lc.startsWith("maintainer")) {
253 maintainer = new EmailAddress(gatherer, command);
254 found = true;
255 }
256 if(command_lc.startsWith("public")) {
257 if(command_lc.endsWith("true")) {
258 public_col = true;
259 }
260 else {
261 public_col = false;
262 }
263 found = true;
264 }
265 if(command_lc.startsWith("beta")) {
266 if(command_lc.endsWith("false")) {
267 beta = false;
268 }
269 else {
270 beta = true;
271 }
272 found = true;
273 }
274 if(!found) {
275 found = indexes.parse(command);
276 }
277 if(!found) {
278 found = subcollections.parse(command, false);
279 }
280 if(!found) {
281 found = languages.parse(command);
282 }
283 if(!found) {
284 found = plugins.parse(command);
285 }
286 if(!found) {
287 found = classifiers.parse(command);
288 }
289 if(!found) {
290 found = formats.parse(command, false);
291 }
292 if(!found) {
293 found = collectionmetadatum.parse(command, false);
294 }
295 // Metadataset commands
296 if(command_lc.startsWith("metadataset")) {
297 // Nothing yet. Eventually used to import metadata.
298 found = true;
299 }
300 // Comments we ignore.
301 if(command_lc.startsWith("# these instructions are not recognized by the gatherer.")) {
302 // Ignore
303 found = true;
304 }
305 // We have been unable to parse this command, add it to rest.
306 if(!found) {
307 rest.add(command);
308 }
309 }
310 }
311 in.close();
312 // Now attempt to finalize any commands that were not immediately parsed as they were waiting for unresolved references.
313 subcollections.reparseUnresolved();
314 classifiers.reparseUnresolved();
315 formats.reparseUnresolved();
316 collectionmetadatum.reparseUnresolved();
317 }
318 catch(Exception error) {
319 error.printStackTrace();
320 }
321 }
322 /** 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.
323 * @see org.greenstone.gatherer.cdm.ClassifierManager
324 * @see org.greenstone.gatherer.cdm.CollectionMetaManager
325 * @see org.greenstone.gatherer.cdm.FormatManager
326 * @see org.greenstone.gatherer.cdm.IndexManager
327 * @see org.greenstone.gatherer.cdm.LanguageManager
328 * @see org.greenstone.gatherer.cdm.MetadataSetManager
329 * @see org.greenstone.gatherer.cdm.PlugInManager
330 * @see org.greenstone.gatherer.cdm.SubcollectionManager
331 * @see org.greenstone.gatherer.util.EmailAddress
332 */
333 public void save() {
334 try {
335 // If the file already exists (it should) rename it.
336 if(in_file.exists()) {
337 String filename = in_file.getAbsolutePath();
338 File backup = new File(filename + "~");
339 backup.deleteOnExit();
340 if(!in_file.renameTo(backup)) {
341 gatherer.debug("Error in CollectionDesignManager.parse(): FileRenamedException");
342 }
343 in_file = new File(filename); // Just in case we moved it.
344 }
345 FileOutputStream out = new FileOutputStream(in_file);
346
347 StringBuffer text = new StringBuffer("");
348 if(creator != null) {
349 text.append(creator.toString());
350 text.append("\n");
351 }
352 if(maintainer != null) {
353 text.append(maintainer.toString());
354 text.append("\n");
355 }
356 if(public_col) {
357 text.append("public true\n");
358 }
359 else {
360 text.append("public false\n");
361 }
362 if(beta) {
363 text.append("beta true\n");
364 }
365 else {
366 text.append("beta false\n");
367 }
368 if(text.length() > 0) {
369 text.append("\n");
370 }
371 if(indexes.size() > 0) {
372 text.append(indexes.toString());
373 }
374 if(subcollections.size() > 0) {
375 text.append(subcollections.toString());
376 }
377 if(languages.size() > 0) {
378 text.append(languages.toString());
379 }
380 if(plugins.size() > 0) {
381 text.append(plugins.toString());
382 }
383 if(classifiers.size() > 0) {
384 text.append(classifiers.toString());
385 }
386 if(formats.size() > 0) {
387 text.append(formats.toString());
388 }
389 if(collectionmetadatum.size() > 0) {
390 text.append(collectionmetadatum.toString());
391 }
392 text.append(metadatasets.toString());
393 if(rest.size() > 0) {
394 // Write out rest at the bottom.
395 text.append("# These instructions are not recognized by the Gatherer.\n");
396 for(int i = 0; i < rest.size(); i++) {
397 text.append(rest.get(i));
398 text.append("\n");
399 }
400 }
401 out.write(text.toString().getBytes());
402 out.close();
403 out = null;
404 }
405 catch(Exception error) {
406 error.printStackTrace();
407 }
408 }
409 /** 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).
410 * @param element The name of the desired element as a <strong>String</strong>.
411 * @see org.greenstone.gatherer.cdm.GUI
412 * @see org.greenstone.gatherer.cdm.MetadataSetManager
413 */
414 public Rectangle setSelectedElement(String element) {
415 // First ensure that the metadata set controls are visible.
416 gui.setSelectedView("CDM.GUI.MetadataSets");
417 // Then tell them to select the given element.
418 return metadatasets.setSelectedElement(element);
419 }
420}
Note: See TracBrowser for help on using the repository browser.