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

Last change on this file since 16330 was 16330, checked in by ak19, 16 years ago

Changes to work with the remote GS server which now knows its server version and informs the GLI client application. These changes (which mostly involved moving around code and deleting lines no longer needed) are based on the fact that config3.xml has been merged into config.xml and configRemote3.xml has been merged into configRemote.xml.

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