source: gli/trunk/src/org/greenstone/gatherer/Configuration.java@ 14974

Last change on this file since 14974 was 14974, checked in by davidb, 16 years ago

Changes to GLI to support export into Fedora. New utility called flisvn diff gems/MetadataSetManager.java

  • Property svn:keywords set to Author Date Id Revision
File size: 38.8 KB
Line 
1/**
2 *#########################################################################
3 * Configuration
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;
38
39import java.awt.*;
40import java.io.*;
41import java.lang.ref.*;
42import java.net.*;
43import java.util.*;
44import javax.swing.*;
45import javax.swing.plaf.*;
46import org.greenstone.gatherer.util.StaticStrings;
47import org.greenstone.gatherer.util.XMLTools;
48import org.w3c.dom.*;
49
50/** This class stores the various configurable settings inside the Gatherer, both during a session, and between sessions in the form of XML. However not all data members are retained during xml serialization. To further improve efficiency, the property-name -> DOM Element pairs are stored in a SoftReferenced Hashtable.
51 * @author John Thompson, Greenstone Digital Library, University of Waikato
52 * @version 2.3
53 */
54public class Configuration
55 extends Hashtable {
56
57 static final public boolean COLLECTION_SPECIFIC = true;
58 static final public boolean GENERAL_SETTING = true;
59
60 static final public int ASSISTANT_MODE = 1;
61 static final public int LIBRARIAN_MODE = 2;
62 static final public int SYSTEMS_MODE = 3;
63 static final public int EXPERT_MODE = 4;
64
65 /** The string identifying an argument's name attribute. */
66 static final private String ARGUMENT_NAME = "name";
67 /** The name of the general Gatherer configuration file. */
68 static public String CONFIG_XML = "config.xml";
69 /** The name of the general Gatherer configuration file for running with GS3. */
70 static public String GS3_CONFIG_XML = "config3.xml";
71 static public String FEDORA_CONFIG_PREFIX = "fedora-";
72
73 /** The name of the root element of the subtree containing gatherer configuration options. This is required as the document itself may contain several other subtrees of settings (such as in the case of a '.col' file). */
74 static final private String GATHERER_CONFIG = "GathererConfig";
75 /** The string identifying an argument element. */
76 static final private String GATHERER_CONFIG_ARGUMENT = "Argument";
77
78 static final private String GENERAL_EMAIL_SETTING = "general.email";
79 /** the lang attribute of the other element */
80 static final private String LANGUAGE = "lang";
81 /** The name of a Name Element. */
82 static final private String NAME = "Name";
83 /** The name of the other arguments element. */
84 static final private String OTHER = "Other";
85 /** The name of the general Gatherer configuration template. */
86 static public String TEMPLATE_CONFIG_XML = "xml/config.xml";
87 /** The first of three patterns used during tokenization, this pattern handles a comma separated list. */
88 static final private String TOKENIZER_PATTERN1 = " ,\n\t";
89 /** The second of three patterns used during tokenization, this pattern handles an underscore separated list. */
90 static final private String TOKENIZER_PATTERN2 = "_\n\t";
91 /** The last of three patterns used during tokenization, this pattern handles an comma separated list containing spaces. */
92 static final private String TOKENIZER_PATTERN3 = ",\n\t";
93
94 static public Configuration self = null;
95 static public String gli_user_directory_path = null;
96
97 static public boolean just_updated_config_xml_file = false;
98 /** The path to the Greenstone Suite installation directory. */
99 static public String gsdl_path = "";
100 /** If we are using GLI in Greenstone 3, the path to gsdl3 home (== web dir) */
101 static public String gsdl3_path = "";
102 /** If we are using GLI in Greenstone 3, the path to gsdl3 src directory (== gsdl3 dir)*/
103 static public String gsdl3_src_path = "";
104
105 static public FedoraInfo fedora_info = null;
106
107 /** The path to the PERL executable, up to and including Perl.exe. */
108 static public String perl_path = "";
109 /** The password for the proxy server indicated above. */
110 static public String proxy_pass = null;
111 /** The username for the proxy server indicated above. */
112 static public String proxy_user = null;
113 /** The screen size of the desktop the Gatherer will be displayed on. */
114 static public Dimension screen_size = Toolkit.getDefaultToolkit().getScreenSize();
115 /** The site name if we are using GS3 */
116 static public String site_name = "";
117 /** The servlet path if we are using GS3 */
118 static public String servlet_path = "";
119 /** Collection level configuration (which in some cases overrides general configuration. */
120 static private Document collection_config;
121 /** The general configuration settings. */
122 static private Document general_config;
123
124 /** The element, from glis config file, that contains the various directory mappings. */
125 static private Element directory_mappings_element;
126
127 static private int cache_hit = 0;
128 static private int cache_miss = 0;
129
130 static public URL gliserver_url = null;
131 static public URL library_url = null;
132
133
134 /** Constructor.
135 * @param gsdl_path The path to the Greenstone directory as a <strong>String</strong>.
136 * @param gsdl3_path The path to the Greenstone 3 home (web) directory as a <strong>String</strong>.
137 * @param gsdl3_src_path The path to the Greenstone 3 src directory as a <strong>String</strong>.
138 * @param site_name The name of the Greenstone 3 site currently in use.
139 */
140 public Configuration(String gli_user_directory_path, String gsdl_path, String gsdl3_path, String gsdl3_src_path, String site_name,
141 FedoraInfo fedora_info)
142 {
143 super();
144 self = this;
145
146 this.gli_user_directory_path = gli_user_directory_path;
147 this.gsdl_path = gsdl_path;
148 this.gsdl3_path = gsdl3_path;
149 this.gsdl3_src_path = gsdl3_src_path;
150 this.site_name = site_name;
151
152 this.fedora_info = fedora_info;
153
154 // Try to load the configuration file from the user specific location
155 String config_xml_file_name = CONFIG_XML;
156 if (gsdl3_path != null || (Gatherer.isGsdlRemote && Gatherer.GS3)) {
157 config_xml_file_name = GS3_CONFIG_XML;
158 }
159
160 if (fedora_info.isActive()) {
161 config_xml_file_name = FEDORA_CONFIG_PREFIX + config_xml_file_name;
162 }
163
164 // If the existing user config.xml file isn't recent enough, backup the old version and update it
165 File config_xml_file = new File(gli_user_directory_path + config_xml_file_name);
166
167 if (config_xml_file != null && config_xml_file.exists()) {
168 just_updated_config_xml_file = updateUserConfigXMLFileIfNecessary(config_xml_file);
169 general_config = XMLTools.parseXMLFile(config_xml_file);
170 }
171
172 // Create a new config from the default configuration file from our xml library
173 if (general_config == null) {
174 general_config = XMLTools.parseXMLFile(TEMPLATE_CONFIG_XML, true);
175 just_updated_config_xml_file = true;
176 }
177
178 // Determine the Directory Mappings element
179 directory_mappings_element = (Element) XMLTools.getNodeFromNamed(general_config.getDocumentElement(), StaticStrings.DIRECTORY_MAPPINGS_ELEMENT);
180
181 // Re-establish the color settings.
182 updateUI();
183
184 // Read the Greenstone library URL from the config file
185 String library_url_string = getString("general.library_url", true);
186 if (!library_url_string.equals("")) {
187 try {
188 library_url = new URL(library_url_string);
189 }
190 catch (MalformedURLException exception) {
191 DebugStream.printStackTrace(exception);
192 }
193 }
194
195 // Read the Greenstone gliserver URL from the config file, if using a remote Greenstone
196 if (Gatherer.isGsdlRemote) {
197 String gliserver_url_string = getString("general.gliserver_url", true);
198 if (!gliserver_url_string.equals("")) {
199 try {
200 gliserver_url = new URL(gliserver_url_string);
201 }
202 catch (MalformedURLException exception) {
203 DebugStream.printStackTrace(exception);
204 }
205 }
206 }
207
208 if (gsdl3_path != null || (Gatherer.GS3 && Gatherer.isGsdlRemote)) {
209 if (this.site_name == null || this.site_name.equals("")) {
210 this.site_name = getString("general.site_name", true);
211 this.servlet_path = getString("general.servlet_path", true);
212 if (this.site_name.equals("")) {
213 this.site_name = "localsite"; // can we assume these??
214 this.servlet_path = "/library";
215 setString("general.site_name", true, this.site_name);
216 setString("general.servlet_path", true, this.servlet_path);
217 }
218
219 } else {
220 // we set the current one in the config
221 setString("general.site_name", true, this.site_name);
222 if (this.servlet_path != null && !this.servlet_path.equals("")) {
223 setString("general.servlet_path", true, this.servlet_path);
224 }
225 }
226 }
227 }
228
229
230 static private boolean updateUserConfigXMLFileIfNecessary(File config_xml_file)
231 {
232 // Check if the configuration file actually needs updating by looking at the version numbers
233 Document new_document = XMLTools.parseXMLFile(TEMPLATE_CONFIG_XML, true);
234 Document old_document = XMLTools.parseXMLFile(config_xml_file);
235 if (new_document == null || old_document == null) {
236 // Something has gone badly wrong, so we can't update the file
237 return false;
238 }
239
240 String new_version = new_document.getDocumentElement().getAttribute(StaticStrings.VERSION_ATTRIBUTE);
241 String old_version = old_document.getDocumentElement().getAttribute(StaticStrings.VERSION_ATTRIBUTE);
242 if (new_version.compareTo(old_version) <= 0) {
243 // Don't need to update file
244 return false;
245 }
246
247 System.err.println("Updating user config.xml from version " + old_version + " to version " + new_version + "...");
248
249 // Build up the new user config.xml file from the template config.xml with the user's preferences added
250 // Make a backup of the old config.xml file first
251 config_xml_file.renameTo(new File(config_xml_file.getAbsolutePath() + ".old"));
252
253 // Check all of the Argument elements in the new config.xml file
254 NodeList new_argument_elements_nodelist = new_document.getElementsByTagName("Argument");
255 NodeList old_argument_elements_nodelist = old_document.getElementsByTagName("Argument");
256 for (int i = 0; i < new_argument_elements_nodelist.getLength(); i++) {
257 Element new_argument_element = (Element) new_argument_elements_nodelist.item(i);
258 String new_argument_element_name = new_argument_element.getAttribute("name");
259 String new_argument_element_value = XMLTools.getElementTextValue(new_argument_element);
260
261 // Did this Argument have a non-default value set?
262 for (int j = old_argument_elements_nodelist.getLength() - 1; j >= 0; j--) {
263 Element old_argument_element = (Element) old_argument_elements_nodelist.item(j);
264 String old_argument_element_name = old_argument_element.getAttribute("name");
265
266 // Argument found
267 if (old_argument_element_name.equals(new_argument_element_name)) {
268 if (!new_argument_element_name.equals("general.open_collection")) {
269 String old_argument_element_value = XMLTools.getElementTextValue(old_argument_element);
270 if (!old_argument_element_value.equals(new_argument_element_value)) {
271 XMLTools.setElementTextValue(new_argument_element, old_argument_element_value);
272 }
273 }
274
275 // We don't care about this option any more
276 old_argument_element.getParentNode().removeChild(old_argument_element);
277 break;
278 }
279 }
280 }
281
282 // Are there any old Argument elements left over? Keep these around for old time's sake
283 old_argument_elements_nodelist = old_document.getElementsByTagName("Argument");
284 if (old_argument_elements_nodelist.getLength() > 0) {
285 NodeList new_gathererconfig_elements_nodelist = new_document.getElementsByTagName("GathererConfig");
286 if (new_gathererconfig_elements_nodelist.getLength() > 0) {
287 Element new_gathererconfig_element = (Element) new_gathererconfig_elements_nodelist.item(0);
288 new_gathererconfig_element.appendChild(new_document.createComment(" Legacy options "));
289
290 // Add the legacy Arguments to the GathererConfig element
291 for (int i = 0; i < old_argument_elements_nodelist.getLength(); i++) {
292 Element old_argument_element = (Element) old_argument_elements_nodelist.item(i);
293 new_gathererconfig_element.appendChild(new_document.importNode(old_argument_element, true));
294 }
295 }
296 }
297
298 // Keep all the Mapping elements
299 NodeList new_directorymappings_elements_nodelist = new_document.getElementsByTagName("DirectoryMappings");
300 if (new_directorymappings_elements_nodelist.getLength() > 0) {
301 Element new_directorymappings_element = (Element) new_directorymappings_elements_nodelist.item(0);
302
303 NodeList old_mapping_elements_nodelist = old_document.getElementsByTagName("Mapping");
304 for (int i = 0; i < old_mapping_elements_nodelist.getLength(); i++) {
305 Element old_mapping_element = (Element) old_mapping_elements_nodelist.item(i);
306 new_directorymappings_element.appendChild(new_document.importNode(old_mapping_element, true));
307 }
308 }
309
310 // Check all of the Associations elements in the new config.xml file
311 NodeList new_entry_elements_nodelist = new_document.getElementsByTagName("Entry");
312 NodeList old_entry_elements_nodelist = old_document.getElementsByTagName("Entry");
313 for (int i = 0; i < new_entry_elements_nodelist.getLength(); i++) {
314 Element new_entry_element = (Element) new_entry_elements_nodelist.item(i);
315 String new_entry_element_name = new_entry_element.getAttribute("extension");
316 String new_entry_element_value = XMLTools.getElementTextValue(new_entry_element);
317
318 // Did this Entry have a non-default value set?
319 for (int j = old_entry_elements_nodelist.getLength() - 1; j >= 0; j--) {
320 Element old_entry_element = (Element) old_entry_elements_nodelist.item(j);
321 String old_entry_element_name = old_entry_element.getAttribute("extension");
322
323 // Entry found
324 if (old_entry_element_name.equals(new_entry_element_name)) {
325 String old_entry_element_value = XMLTools.getElementTextValue(old_entry_element);
326 if (!old_entry_element_value.equals(new_entry_element_value)) {
327 XMLTools.setElementTextValue(new_entry_element, old_entry_element_value);
328 }
329
330 // We don't care about this option any more
331 old_entry_element.getParentNode().removeChild(old_entry_element);
332 break;
333 }
334 }
335 }
336
337 // Are there any old Entry elements left over? We want to keep these
338 old_entry_elements_nodelist = old_document.getElementsByTagName("Entry");
339 if (old_entry_elements_nodelist.getLength() > 0) {
340 NodeList new_associations_elements_nodelist = new_document.getElementsByTagName("Associations");
341 if (new_associations_elements_nodelist.getLength() > 0) {
342 Element new_associations_element = (Element) new_associations_elements_nodelist.item(0);
343
344 // Add the legacy Entries to the Associations element
345 for (int i = 0; i < old_entry_elements_nodelist.getLength(); i++) {
346 Element old_entry_element = (Element) old_entry_elements_nodelist.item(i);
347 new_associations_element.appendChild(new_document.importNode(old_entry_element, true));
348 }
349 }
350 }
351
352 // Write out the updated config.xml file
353 XMLTools.writeXMLFile(config_xml_file, new_document);
354 return true;
355 }
356
357
358 /** Add a special directory mapping. */
359 static public boolean addDirectoryMapping(String name, File file) {
360 boolean result = false;
361 try {
362 // Ensure the name isn't already in use.
363 boolean found = false;
364 NodeList mappings = directory_mappings_element.getElementsByTagName(StaticStrings.MAPPING_ELEMENT);
365 for(int i = 0; !found && i < mappings.getLength(); i++) {
366 Element mapping_element = (Element) mappings.item(i);
367 if(mapping_element.getAttribute(StaticStrings.NAME_ATTRIBUTE).equalsIgnoreCase(name)) {
368 found = true;
369 }
370 mapping_element = null;
371 }
372 // Otherwise add the mapping.
373 if(!found) {
374 Element mapping_element = general_config.createElement(StaticStrings.MAPPING_ELEMENT);
375 mapping_element.setAttribute(StaticStrings.NAME_ATTRIBUTE, name);
376 mapping_element.setAttribute(StaticStrings.FILE_ATTRIBUTE, file.toString());
377 directory_mappings_element.appendChild(mapping_element);
378 result = true;
379 mapping_element = null;
380 }
381 mappings = null;
382 }
383 catch (Exception exception) {
384 DebugStream.printStackTrace(exception);
385 }
386 return result;
387 } /** addDirectoryMapping(String name, String file) **/
388
389 /** The default get action retrieves the named property from the desired configuration, and returns a true or false. */
390 static public boolean get(String property, boolean general) {
391 String raw = getString(property, general);
392 return (raw != null && raw.equalsIgnoreCase("true"));
393 }
394
395 /** Retrieve all of the configuration preferences which match a certain string. They are returned as a hash map of property names to String objects. */
396 static public HashMap getAll(String property_pattern, boolean general) {
397 HashMap properties = new HashMap();
398 try {
399 // Locate the appropriate element
400 Element document_element = null;
401 if(general) {
402 document_element = general_config.getDocumentElement();
403 }
404 else if(collection_config != null) {
405 document_element = collection_config.getDocumentElement();
406 }
407 if(document_element != null) {
408 // Retrieve the Gatherer element
409 Element gatherer_element = (Element) XMLTools.getNodeFromNamed(document_element, GATHERER_CONFIG);
410 NodeList arguments = gatherer_element.getElementsByTagName(GATHERER_CONFIG_ARGUMENT);
411 for(int i = 0; i < arguments.getLength(); i++) {
412 Element argument_element = (Element) arguments.item(i);
413 if(argument_element.getAttribute(ARGUMENT_NAME).matches(property_pattern)) {
414 String result = XMLTools.getValue(argument_element);
415 // Store a mapping in the cache. Sometimes we will overwrite an existing value (say for collection and general level workflow options) but the processing overhead of detecting these clashes far exceeds any savings.
416 self.put(argument_element.getAttribute(ARGUMENT_NAME) + general, new SoftReference(argument_element));
417 // Add mapping to the properties we're going to return
418 properties.put(argument_element.getAttribute(ARGUMENT_NAME), result);
419 }
420 }
421 }
422 }
423 catch (Exception exception) {
424 DebugStream.printStackTrace(exception);
425 }
426 return properties;
427 }
428
429 /** Retrieve the value of the named property as a Rectangle. */
430 static public Rectangle getBounds(String property, boolean general) {
431 Rectangle result = null;
432 try {
433 String raw = getString(property, general);
434 // Rectangle is (x, y, width, height)
435 StringTokenizer tokenizer = new StringTokenizer(raw, TOKENIZER_PATTERN1);
436 int x = Integer.parseInt(tokenizer.nextToken());
437 int y = Integer.parseInt(tokenizer.nextToken());
438 int width = Integer.parseInt(tokenizer.nextToken());
439 int height = Integer.parseInt(tokenizer.nextToken());
440 result = new Rectangle(x, y, width, height);
441 }
442 catch (Exception exception) {
443 DebugStream.printStackTrace(exception);
444 }
445 return result;
446 }
447
448 /** Retrieve the value of the named property as a Color. */
449 static public Color getColor(String property, boolean general) {
450 Color result = Color.white; // Default
451 try {
452 String raw = getString(property, general);
453 // Color is a RGB triplet list, comma separated (also remove whitespace)
454 StringTokenizer tokenizer = new StringTokenizer(raw, TOKENIZER_PATTERN1);
455 int red = Integer.parseInt(tokenizer.nextToken());
456 int green = Integer.parseInt(tokenizer.nextToken());
457 int blue = Integer.parseInt(tokenizer.nextToken());
458 result = new Color(red, green, blue);
459 }
460 catch (Exception exception) {
461 DebugStream.printStackTrace(exception);
462 }
463 return result;
464 }
465
466
467 /** Retrieve the special directory mappings associated with this collection.
468 * @return A <strong>HashMap</strong> containing mappings from names to directories.
469 */
470 static public HashMap getDirectoryMappings() {
471 HashMap special_directories = new HashMap();
472 try {
473 // Ensure the name isn't already in use.
474 boolean found = false;
475 NodeList mappings = directory_mappings_element.getElementsByTagName(StaticStrings.MAPPING_ELEMENT);
476 for(int i = 0; !found && i < mappings.getLength(); i++) {
477 Element mapping_element = (Element) mappings.item(i);
478 String name = mapping_element.getAttribute(StaticStrings.NAME_ATTRIBUTE);
479 File file = new File(mapping_element.getAttribute(StaticStrings.FILE_ATTRIBUTE));
480 special_directories.put(name, file);
481 file = null;
482 name = null;
483 mapping_element = null;
484 }
485 mappings = null;
486 }
487 catch (Exception exception) {
488 DebugStream.printStackTrace(exception);
489 }
490 return special_directories;
491 } /** getDirectoryMappings() */
492
493 /** Retrieve the current users email. These are always stored in the general settings.
494 * @return the email address, if it is set, as a String
495 */
496 static public String getEmail() {
497 String email = getString(GENERAL_EMAIL_SETTING, true);
498 return (email.length() > 0 ? email : null);
499 }
500
501 static public Element getFileAssociations() {
502 NodeList file_association_elements = general_config.getDocumentElement().getElementsByTagName(StaticStrings.ASSOCIATIONS_ELEMENT);
503 return (Element) file_association_elements.item(0);
504 }
505
506 /** Retrieve the value of the named property as a FontUIResource. */
507 static public FontUIResource getFont(String property, boolean general) {
508 FontUIResource result = new FontUIResource("Times New Roman", Font.PLAIN, 10);
509 try {
510 String raw = getString(property, general);
511 // Font is a face, style, size triplet.
512 StringTokenizer tokenizer = new StringTokenizer(raw, TOKENIZER_PATTERN3);
513 String face = tokenizer.nextToken().trim();
514 ///ystem.err.println("Face: " + face);
515 int style = Font.PLAIN;
516 String temp = tokenizer.nextToken().toUpperCase().trim();
517 ///ystem.err.println("Style: " + temp);
518 if(temp.equals("BOLD")) {
519 style = Font.BOLD;
520 }
521 else if(temp.equals("ITALIC")) {
522 style = Font.ITALIC;
523 }
524 int size = Integer.parseInt(tokenizer.nextToken().trim());
525 ///ystem.err.println("Size: " + size);
526 result = new FontUIResource(face, style, size);
527 }
528 catch (Exception exception) {
529 DebugStream.printStackTrace(exception);
530 }
531 return result;
532 }
533
534 /** Retrieve the value of the named property as an integer. */
535 static public int getInt(String property, boolean general) {
536 int result = 0;
537 try {
538 String raw = getString(property, general);
539 result = Integer.parseInt(raw);
540 }
541 catch (Exception exception) {
542 DebugStream.printStackTrace(exception);
543 }
544 return result;
545 }
546
547 /** Retrieves the current interface language two letter code. */
548 static public String getLanguage() {
549 Locale locale = getLocale("general.locale", GENERAL_SETTING);
550 String code = "en"; // Default
551 if(locale != null) {
552 code = locale.getLanguage();
553 }
554 return code;
555 }
556
557 /** Retrieve the value of the named property as a Locale. */
558 static public Locale getLocale(String property, boolean general) {
559 Locale result = Locale.getDefault();
560 try {
561 String raw = getString(property, general);
562 // Locale is a underscore separated code.
563 StringTokenizer tokenizer = new StringTokenizer(raw, TOKENIZER_PATTERN2);
564 String language = tokenizer.nextToken();
565 if(tokenizer.hasMoreTokens()) {
566 String country = tokenizer.nextToken();
567 result = new Locale(language, country);
568 }
569 else {
570 result = new Locale(language);
571 }
572 }
573 catch (Exception exception) {
574 DebugStream.printStackTrace(exception);
575 }
576 return result;
577 }
578
579 /** Because modes will soon be an integral part of GLI, they get their own easy to remember methods such as this one to get the mode.
580 * @return an int representing the mode
581 */
582 static public int getMode() {
583 return getInt("general.mode", GENERAL_SETTING);
584 }
585
586 /** Return the current mode as a string for use in title bar etc
587 * @return the mode as a String
588 */
589 static public String getModeAsString() {
590 String result;
591 switch(getInt("general.mode", GENERAL_SETTING)) {
592 case ASSISTANT_MODE:
593 result = Dictionary.get("Preferences.Mode.Assistant");
594 break;
595 case SYSTEMS_MODE:
596 result = Dictionary.get("Preferences.Mode.Systems");
597 break;
598 case EXPERT_MODE:
599 result = Dictionary.get("Preferences.Mode.Expert");
600 break;
601 default:
602 result = Dictionary.get("Preferences.Mode.Librarian");
603 }
604 return result;
605 }
606
607 static public String getPreviewCommand() {
608 return getString("general.preview_program", GENERAL_SETTING);
609 }
610
611 static public String getApplicationTitle() {
612 String gli_title = getString("GLI.Title", GENERAL_SETTING);
613 String title = (gli_title=="") ? Gatherer.PROGRAM_NAME : gli_title;
614
615 return title;
616 }
617
618
619 static public String getServletPath() {
620 return servlet_path;
621 }
622
623 /** Retrieve the value of the named property, and noting whether we consult the general or collection specific configuration. */
624 static public String getString(String property, boolean general) {
625 // Its up to this method to find the appropriate node and retrieve the data itself.
626 String result = "";
627 try {
628 // First of all we look in the cache to see if we have a match.
629 SoftReference reference = (SoftReference) self.get(property + general);
630 if(reference != null) {
631 Element argument_element = (Element) reference.get();
632 if(argument_element != null) {
633 cache_hit++;
634 result = XMLTools.getValue(argument_element);
635 }
636 }
637 // We may have missed in the cache, or the reference may have been consumed.
638 if(result.length() == 0) {
639 cache_miss++;
640 // Locate the appropriate element
641 Element document_element = null;
642 if(general) {
643 document_element = general_config.getDocumentElement();
644 }
645 else if(collection_config != null) {
646 document_element = collection_config.getDocumentElement();
647 }
648 if(document_element != null) {
649 // Retrieve the Gatherer element
650 Element gatherer_element = (Element) XMLTools.getNodeFromNamed(document_element, GATHERER_CONFIG);
651 NodeList arguments = gatherer_element.getElementsByTagName(GATHERER_CONFIG_ARGUMENT);
652 for(int i = 0; result.length() == 0 && i < arguments.getLength(); i++) {
653 Element argument_element = (Element) arguments.item(i);
654 if(argument_element.getAttribute(ARGUMENT_NAME).equalsIgnoreCase(property)) {
655 result = XMLTools.getValue(argument_element);
656 // Store a mapping in the cache. Sometimes we will overwrite an existing value (say for collection and general level workflow options) but the processing overhead of detecting these clashes far exceeds any savings.
657 self.put(property + general, new SoftReference(argument_element));
658 }
659 }
660 }
661 }
662 }
663 catch (Exception exception) {
664 DebugStream.printStackTrace(exception);
665 }
666 // If we still have no result, and the search was made in the collection configuration, retrieve the general one instead.
667 if(result.length() == 0 && !general) {
668 result = getString(property, true);
669 }
670 return result;
671 }
672
673 static public String getGS3ScriptPath() {
674 return gsdl3_src_path + "bin" + File.separator + "script" + File.separator;
675 }
676
677 /** Remove a previously defined special directory mapping.
678 * @param name The name of the mapping to remove as a <strong>String</strong>.
679 * @return The <strong>File</strong> of the mapping removed.
680 */
681 static public File removeDirectoryMapping(String name) {
682 File file = null;
683 try {
684 // Ensure the name isn't already in use.
685 boolean found = false;
686 NodeList mappings = directory_mappings_element.getElementsByTagName(StaticStrings.MAPPING_ELEMENT);
687 for(int i = 0; !found && i < mappings.getLength(); i++) {
688 Element mapping_element = (Element) mappings.item(i);
689 if(mapping_element.getAttribute(StaticStrings.NAME_ATTRIBUTE).equalsIgnoreCase(name)) {
690 file = new File(XMLTools.getValue(mapping_element));
691 directory_mappings_element.removeChild(mapping_element);
692 found = true;
693 }
694 mapping_element = null;
695 }
696 mappings = null;
697 }
698 catch (Exception exception) {
699 DebugStream.printStackTrace(exception);
700 }
701 return file;
702 } /** removeDirectoryMapping(String name) */
703
704 /** Export the general configuration to file. */
705 static public void save() {
706 ///ystem.err.println("Hits " + cache_hit + " vs Misses " + cache_miss);
707 // We first try exporting to a user specific place
708 File user_config_xml = null;
709 String config_xml_name = CONFIG_XML;
710
711 if (gsdl3_path!=null) {
712 config_xml_name = GS3_CONFIG_XML;
713 }
714
715 if (Gatherer.isGsdlRemote) {
716 if (Gatherer.GS3){
717 config_xml_name = GS3_CONFIG_XML;
718 }else{
719 config_xml_name = CONFIG_XML;
720 }
721 }
722
723 if (fedora_info.isActive()) {
724 config_xml_name = FEDORA_CONFIG_PREFIX + config_xml_name;
725 }
726
727 try {
728 user_config_xml = new File(gli_user_directory_path + config_xml_name);
729 ///ystem.err.println("Trying to save to: " + user_config_xml.getAbsolutePath());
730 ///ystem.err.println("Writing.");
731 user_config_xml.getParentFile().mkdirs();
732 XMLTools.writeXMLFile(new File(user_config_xml.getAbsolutePath()), general_config);
733 }
734 catch (Exception exception) {
735 DebugStream.printStackTrace(exception);
736 user_config_xml = null;
737 }
738 }
739
740 /** Set the named property, from the specified configuration, using the given boolean value. */
741 static public void set(String property, boolean general, boolean value) {
742 if(property.startsWith("workflow")) {
743 DebugStream.println("Set property: " + property + ", general=" + general + ", value=" + value);
744 }
745 setString(property, general, (value ? "true" : "false"));
746 }
747
748 /** Set the collection configuration. */
749 static public void setCollectionConfiguration(Document collection_config_arg) {
750 // clear the cached values
751 self.clear();
752 collection_config = collection_config_arg;
753 updateUI();
754 ///atherer.println("Collection configuration set.");
755 }
756
757 /** Set the named property, from the specified configuration, using the given Rectangle value. */
758 static public void setBounds(String property, boolean general, Rectangle value) {
759 StringBuffer text = new StringBuffer("");
760 text.append(value.x);
761 text.append(", ");
762 text.append(value.y);
763 text.append(", ");
764 text.append(value.width);
765 text.append(", ");
766 text.append(value.height);
767 setString(property, general, text.toString());
768 }
769
770 /** Set the named property, from the specified configuration, using the given Color value. */
771 static public void setColor(String property, boolean general, Color value) {
772 StringBuffer text = new StringBuffer("");
773 text.append(value.getRed());
774 text.append(", ");
775 text.append(value.getGreen());
776 text.append(", ");
777 text.append(value.getBlue());
778 setString(property, general, text.toString());
779 }
780
781 /** Establish the current users email.
782 * @param email the email as a String
783 */
784 static public void setEmail(String email) {
785 setString(GENERAL_EMAIL_SETTING, true, email);
786 }
787
788 /** Set the named property, from the specified configuration, using the given Font value. */
789 static public void setFont(String property, boolean general, Font value) {
790 StringBuffer text = new StringBuffer("");
791 text.append(value.getName());
792 text.append(", ");
793 switch(value.getStyle()) {
794 case Font.BOLD:
795 text.append("BOLD");
796 break;
797 case Font.ITALIC:
798 text.append("ITALIC");
799 break;
800 default:
801 text.append("PLAIN");
802 }
803 text.append(", ");
804 text.append(value.getSize());
805 setString(property, general, text.toString());
806 }
807
808 /** Set the named property, from the specified configuration, using the given integer value. */
809 static public void setInt(String property, boolean general, int value) {
810 setString(property, general, String.valueOf(value));
811 }
812
813 /** Set the named property, from the specified configuration, using the given Locale value. */
814 static public void setLocale(String property, boolean general, Locale value) {
815 StringBuffer text = new StringBuffer("");
816 text.append(value.getLanguage());
817 String country = value.getCountry();
818 if(country != null && country.length() > 0) {
819 text.append("_");
820 text.append(country);
821 }
822 country = null;
823 setString(property, general, text.toString());
824 }
825
826 /** Because modes will soon be an integral part of GLI, they get their own easy to remember methods such as this one to set the mode.
827 * @param value the new value for mode
828 */
829 static public void setMode(int value) {
830 setInt("general.mode", GENERAL_SETTING, value);
831 }
832
833 static public void setPreviewCommand(String value) {
834 setString("general.preview_program", GENERAL_SETTING, value);
835 }
836
837 static public void setSiteAndServlet(String site, String servlet) {
838 site_name = site;
839 servlet_path = servlet;
840 setString("general.site_name", GENERAL_SETTING, site);
841 setString("general.servlet_path", GENERAL_SETTING, servlet);
842
843 }
844 /** Sets the value of the named property argument using the given string. */
845 static public void setString(String property, boolean general, String value) {
846 DebugStream.println("Set configuration property: " + property + " = " + value + (general ? "" : " [Collection]"));
847 try {
848 Document document = general_config;
849 if(!general && collection_config != null) {
850 document = collection_config;
851 }
852 if(document != null) {
853 Element argument_element = null;
854 // Try to retrieve from cache
855 SoftReference reference = (SoftReference) self.get(property + general);
856 if(reference != null) {
857 argument_element = (Element) reference.get();
858 }
859 if(argument_element == null) {
860 Element document_element = document.getDocumentElement();
861 Element gatherer_element = (Element) XMLTools.getNodeFromNamed(document_element, GATHERER_CONFIG);
862 NodeList arguments = document_element.getElementsByTagName(GATHERER_CONFIG_ARGUMENT);
863 boolean found = false;
864 for(int i = 0; argument_element == null && i < arguments.getLength(); i++) {
865 Element possible_element = (Element) arguments.item(i);
866 if(possible_element.getAttribute(ARGUMENT_NAME).equalsIgnoreCase(property)) {
867 argument_element = possible_element;
868 }
869 }
870 // If argument element is still null, create it in the target document.
871 if(argument_element == null) {
872 argument_element = document.createElement(GATHERER_CONFIG_ARGUMENT);
873 argument_element.setAttribute(ARGUMENT_NAME, property);
874 gatherer_element.appendChild(argument_element);
875 }
876 // Update cache
877 self.put(property + general, new SoftReference(argument_element));
878
879 }
880 if(value == null) {
881 value = "";
882 }
883 // Now remove any current text node children.
884 NodeList children = argument_element.getChildNodes();
885 for(int i = 0; i < children.getLength(); i++) {
886 argument_element.removeChild(children.item(i));
887 }
888 // Add a new text node child with the new value
889 argument_element.appendChild(document.createTextNode(value));
890 }
891 }
892 catch (Exception exception) {
893 DebugStream.printStackTrace(exception);
894 }
895 }
896
897 static private void updateUI() {
898 // Buttons
899 UIManager.put("Button.select", new ColorUIResource(getColor("coloring.button_selected_background", false)));
900 UIManager.put("Button.background", new ColorUIResource(getColor("coloring.button_background", false)));
901 UIManager.put("Button.foreground", new ColorUIResource(getColor("coloring.button_foreground", false)));
902
903 UIManager.put("ToggleButton.background", new ColorUIResource(getColor("coloring.button_background", false)));
904 UIManager.put("ToggleButton.foreground", new ColorUIResource(getColor("coloring.button_foreground", false)));
905 UIManager.put("ToggleButton.select", new ColorUIResource(getColor("coloring.button_selected_background", false)));
906
907 // All the things with a lovely Collection green background
908 UIManager.put("OptionPane.background", new ColorUIResource(getColor("coloring.collection_heading_background", false)));
909 UIManager.put("Panel.background", new ColorUIResource(getColor("coloring.collection_heading_background", false)));
910 UIManager.put("Label.background", new ColorUIResource(getColor("coloring.collection_heading_background", false)));
911 UIManager.put("TabbedPane.background", new ColorUIResource(getColor("coloring.collection_heading_background", false)));
912 UIManager.put("SplitPane.background", new ColorUIResource(getColor("coloring.collection_heading_background", false)));
913 UIManager.put("CheckBox.background", new ColorUIResource(getColor("coloring.collection_heading_background", false)));
914
915
916 // Editable coloring
917 UIManager.put("ComboBox.background", new ColorUIResource(getColor("coloring.collection_tree_background", false))); // Indicate clickable
918 UIManager.put("Tree.background", new ColorUIResource(getColor("coloring.collection_tree_background", false)));
919 UIManager.put("Tree.textBackground", new ColorUIResource(getColor("coloring.collection_tree_background", false)));
920 UIManager.put("ProgressBar.background", new ColorUIResource(getColor("coloring.collection_tree_background", false)));
921 UIManager.put("TextArea.background", new ColorUIResource(getColor("coloring.collection_tree_background", false)));
922 UIManager.put("TextField.background", new ColorUIResource(getColor("coloring.editable_background", false)));
923 UIManager.put("Table.background", new ColorUIResource(getColor("coloring.collection_tree_background", false)));
924 UIManager.put("List.background", new ColorUIResource(getColor("coloring.collection_tree_background", false)));
925 UIManager.put("RadioButton.background", new ColorUIResource(getColor("coloring.collection_tree_background", false)));
926
927 // Selection color
928 UIManager.put("TabbedPane.selected", new ColorUIResource(getColor("coloring.collection_selection_background", false)));
929 UIManager.put("Tree.selectionBackground", new ColorUIResource(getColor("coloring.collection_selection_background", false)));
930 UIManager.put("ComboBox.selectionBackground", new ColorUIResource(getColor("coloring.collection_selection_background", false)));
931 UIManager.put("ProgressBar.selectionBackground", new ColorUIResource(getColor("coloring.collection_selection_background", false)));
932 UIManager.put("TextArea.selectionBackground", new ColorUIResource(getColor("coloring.collection_selection_background", false)));
933 UIManager.put("TextField.selectionBackground", new ColorUIResource(getColor("coloring.collection_selection_background", false)));
934 UIManager.put("List.selectionBackground", new ColorUIResource(getColor("coloring.collection_selection_background", false)));
935
936 // Scroll bar stuff
937 UIManager.put("ScrollBar.background", new ColorUIResource(getColor("coloring.scrollbar_background", false)));
938 UIManager.put("ScrollBar.thumb", new ColorUIResource(getColor("coloring.scrollbar_foreground", false)));
939 }
940}
Note: See TracBrowser for help on using the repository browser.