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

Last change on this file since 7161 was 7022, checked in by kjdon, 20 years ago

now reads in build options in the right language, and doesn't use the ones saved in config.xml unless the language is the same

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