source: trunk/gsdl3/src/java/org/greenstone/core/Configuration.java@ 10929

Last change on this file since 10929 was 10929, checked in by kjdon, 18 years ago

merged Chi's admin stuff from ant install branch into main repository

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