source: trunk/gli/src/org/greenstone/gatherer/cdm/Argument.java@ 6944

Last change on this file since 6944 was 6800, checked in by kjdon, 20 years ago

added in a check for null that was throwing an exception when exit GLI with out saving changed format statements

  • Property svn:keywords set to Author Date Id Revision
File size: 20.4 KB
Line 
1/**
2 *#########################################################################
3 *
4 * A component of the Greenstone Librarian Interface (GLI) application,
5 * part of the Greenstone digital library software suite from the New
6 * Zealand Digital Library Project at the University of Waikato,
7 * New Zealand.
8 *
9 * Author: John Thompson
10 * Greenstone Project, New Zealand Digital Library
11 * University of Waikato
12 * http://www.nzdl.org
13 *
14 * Copyright (C) 2004 New Zealand Digital Library, University of Waikato
15 *
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
20 *
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25 *
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 *########################################################################
30 */
31
32package org.greenstone.gatherer.cdm;
33
34import java.io.*;
35import java.util.*;
36import org.greenstone.gatherer.Configuration;
37import org.greenstone.gatherer.Gatherer;
38import org.greenstone.gatherer.cdm.CollectionConfiguration;
39import org.greenstone.gatherer.collection.Collection;
40import org.greenstone.gatherer.collection.CollectionManager;
41import org.greenstone.gatherer.msm.ElementWrapper;
42import org.greenstone.gatherer.msm.MetadataSetManager;
43import org.greenstone.gatherer.msm.MSMUtils;
44import org.greenstone.gatherer.util.StaticStrings;
45import org.greenstone.gatherer.util.Utility;
46import org.w3c.dom.*;
47
48/** This class contains all the details about a single argument that can be passed to this plugin, including option lists if the parameters are restricted.
49 * @author John Thompson, Greenstone Project, New Zealand Digital Library, University of Waikato
50 * @version 2.41 final
51 */
52public class Argument
53 implements Comparable, Serializable {
54 /** An element of the argument type enumeration specifying a combobox control. */
55 static final public byte ENUM = 0;
56 /** An element of the argument type enumeration specifying a checkbox control. */
57 static final public byte FLAG = 1;
58 /** An element of the argument type enumeration specifying a tree control. */
59 static final public byte HIERARCHY = 2;
60 /** An element of the argument type enumeration specifying a spinner control. */
61 static final public byte INTEGER = 3;
62 /** An element of the argument type enumeration specifying a language combobox control. */
63 static final public byte LANGUAGE = 4;
64 /** An element of the argument type enumeration specifying a list control. */
65 static final public byte METADATA = 5;
66 /** An element of the argument type enumeration specifying a metadata combobox control. */
67 static final public byte METADATUM = 6;
68 /** An element of the argument type enumeration specifying a text field. */
69 static final public byte STRING = 7;
70 /** An element of the argument type enumeration specifying a regular expression text field. */
71 static final public byte REGEXP = 8;
72 /** true if this argument should actually be hidden within the GLI. This is iportant for arguments such as import dir or other location critical arguments. */
73 private boolean hidden_gli = false;
74 /** <i>true</i> if this argument is required for the applicable script to work properly, <i>false</i> otherwise. */
75 private boolean required = false;
76 /** The type of this argument. Used to be an int, but bytes are cheaper. */
77 private byte type = STRING;
78 /** The maximum value an integer based control can have. */
79 private int maximum = Integer.MAX_VALUE;
80 /** The minimum value an integer based control can have. */
81 private int minimum = Integer.MIN_VALUE;
82 /** Every argument has a detail mode level at which it becomes available to the user to edit.
83 * @see org.greenstone.gatherer.Configuration
84 */
85 private int mode_level = Configuration.LIBRARIAN_MODE;
86 /** The DOM element this argument is built around, if any. */
87 private Element element;
88 /** If the argument is of type ENUM then this map holds all the various options. Each entry is an &lt;option value&gt; -&gt; &lt;description&gt; mapping. */
89 private HashMap list = null;
90 /** A default value for parameter-type arguments. May be a Perl pattern. */
91 private String default_value = null;
92 /** The text description of this argument parsed from the pluginfo output. */
93 private String description = null;
94 /** The argument flag as it appears in the command. Also used as the unique identifier of an argument. */
95 private String name = null;
96 /** The plugin that owns this argument, for the purposes of visualising inheritance. */
97 private String owner = null;
98
99 /** Default Constructor. */
100 public Argument() {
101 }
102
103 /** Another constructor but this one is a little more interesting as it takes a DOM element.
104 * @param element the Element this argument is based around
105 */
106 public Argument(Element element) {
107 this.element = element;
108 }
109
110 /** Method to add an element to the option list.
111 * @param name the name value of the option as a String
112 * @param desc the description of this options as a String
113 */
114 public void addOption(String name, String desc) {
115 if(type == ENUM && name != null) {
116 if(desc == null) {
117 desc = "";
118 }
119 if(list == null) {
120 list = new HashMap();
121 }
122 list.put(name, desc);
123 }
124 }
125
126 /** Method to compare two arguments for ordering.
127 * @param object the argument we are comparing to, as an Object
128 * @return an int specifying the argument order, using values as set out in String
129 * @see org.greenstone.gatherer.cdm.Argument
130 */
131 public int compareTo(Object object) {
132 if(object instanceof Argument) {
133 return getName().compareTo(((Argument)object).getName());
134 }
135 else {
136 return toString().compareTo(object.toString());
137 }
138 }
139
140 /** Create a copy of this argument.
141 * @return a newly created Argument with the same details as this one
142 */
143 public Argument copy() {
144 Argument copy = new Argument();
145 copy.setDefaultValue(default_value);
146 copy.setDescription(description);
147 copy.setOptions(list);
148 copy.setOwner(owner);
149 copy.setName(name);
150 copy.setRequired(required);
151 copy.setType(type);
152 return copy;
153 }
154
155 /** Method to determine if two arguments are equal.
156 * @param object the argument to test against, as an Object
157 * @return true if the arguments names match, false otherwise
158 */
159 public boolean equals(Object object) {
160 return (compareTo(object) == 0);
161 }
162
163 /** Method to retrieve the value of default_value.
164 * @return a String containing the default value
165 */
166 public String getDefaultValue() {
167 return default_value;
168 }
169
170 /** Method to retrieve this arguments description.
171 * @return a String containing the description
172 */
173 public String getDescription() {
174 return description;
175 }
176
177 /** Method to retrieve the description of a certain list option value.
178 * @param key the String whose description we are searching for
179 * @return the description of the desired key as a String which may be empty if no such key exists
180 */
181 public String getDescription(String key) {
182 if(list.containsKey(key)) {
183 return (String)list.get(key);
184 }
185 return "";
186 }
187
188 /** Retrieve the upper bound of a range based argument.
189 * @return the maximum as an int
190 */
191 public int getMaximum() {
192 return maximum;
193 }
194
195 /** Retrieve the lower bound of a range based argument.
196 * @return the minimum as an int
197 */
198 public int getMinimum() {
199 return minimum;
200 }
201
202 /** Retrieves the mode level at which this argument should become available. Any higher levels should also see this argument.
203 * @return the mode level as an int
204 */
205 public int getModeLevel() {
206 return mode_level;
207 }
208
209 /** Method to retrieve the value of name.
210 * @return a String containing the argument name
211 * @see org.greenstone.gatherer.util.StaticStrings#NAME_ATTRIBUTE
212 */
213 public String getName() {
214 if(name == null && element != null) {
215 name = element.getAttribute(StaticStrings.NAME_ATTRIBUTE);
216 }
217 return name;
218 }
219
220 /** Method to retrieve the option list for this argument.
221 * @return a HashMap containing &lt;option value&gt; -&gt; &lt;description&gt; entries
222 */
223 public HashMap getOptions() {
224 return list;
225 }
226
227 /** Retrieve the name of the owner of this argument.
228 * @return the owners name as a String
229 */
230 public String getOwner() {
231 return owner;
232 }
233
234 /** Method to determine the type of this argument.
235 * @return a byte specifying the type
236 */
237 public byte getType() {
238 return type;
239 }
240
241 /** Method to retrieve the value of value.
242 * @return the value of value as a String
243 * @see org.greenstone.gatherer.Gatherer#c_man
244 * @see org.greenstone.gatherer.collection.Collection#msm
245 * @see org.greenstone.gatherer.collection.CollectionManager#getCollection
246 * @see org.greenstone.gatherer.msm.ElementWrapper
247 * @see org.greenstone.gatherer.msm.MetadataSetManager#getElement(String)
248 * @see org.greenstone.gatherer.msm.MSMUtils#getValue(Node)
249 */
250 public String getValue() {
251 String value = null;
252 // Only assigned arguments have values.
253 if(element != null) {
254 value = MSMUtils.getValue(element);
255 // We may have to retrieve the language dependant string for a MSM Element
256 if(type == METADATUM) {
257 ElementWrapper element_wrapper = Gatherer.c_man.getCollection().msm.getElement(value);
258 if(element_wrapper != null) {
259 value = element_wrapper.getName();
260 }
261 }
262 }
263 return value;
264 }
265
266 /** Retrieve the vector of values.
267 * @return an ArrayList of values
268 * @see org.greenstone.gatherer.Gatherer#c_man
269 * @see org.greenstone.gatherer.collection.Collection#msm
270 * @see org.greenstone.gatherer.collection.CollectionManager#getCollection
271 * @see org.greenstone.gatherer.msm.ElementWrapper
272 * @see org.greenstone.gatherer.msm.MetadataSetManager#getElement(String)
273 * @see org.greenstone.gatherer.msm.MSMUtils#getValue(Node)
274 */
275 public ArrayList getValues() {
276 ArrayList values = new ArrayList();
277 // Only assigned arguments have values.
278 if(element != null) {
279 String value = MSMUtils.getValue(element);
280 StringTokenizer tokenizer = new StringTokenizer(value, ",");
281 while(tokenizer.hasMoreTokens()) {
282 String token = tokenizer.nextToken();
283 if(type == METADATA) {
284 ElementWrapper element_wrapper = Gatherer.c_man.getCollection().msm.getElement(token);
285 if(element_wrapper != null) {
286 token = element_wrapper.toString();
287 }
288 }
289 values.add(token);
290 }
291 }
292 return values;
293 }
294
295 /** Method to determine if this argument has been assigned.
296 * @return true if it has, false otherwise
297 * @see org.greenstone.gatherer.util.StaticStrings#ASSIGNED_ATTRIBUTE
298 * @see org.greenstone.gatherer.util.StaticStrings#TRUE_STR
299 */
300 public boolean isAssigned() {
301 return (element != null && element.getAttribute(StaticStrings.ASSIGNED_ATTRIBUTE).equals(StaticStrings.TRUE_STR));
302 }
303
304 /** Determine if this is a custom argument ie one that has been parsed from the config file but doesn't have a matching entry in the argument library.
305 * @return true if this argument is a custom, false otherwise
306 * @see org.greenstone.gatherer.util.StaticStrings#CUSTOM_ATTRIBUTE
307 * @see org.greenstone.gatherer.util.StaticStrings#TRUE_STR
308 */
309 public boolean isCustomArgument() {
310 return (element != null && element.getAttribute(StaticStrings.CUSTOM_ATTRIBUTE).equals(StaticStrings.TRUE_STR));
311 }
312
313 /** Determine if this argument is hidden in GLI
314 * @return true if the argument is hidden, false otherwise
315 */
316 public boolean isHiddenGLI() {
317 return hidden_gli;
318 }
319
320 /** Method to determine of this argument is required for the associated script to work.
321 * @return true if this argument is required, false otherwise
322 */
323 public boolean isRequired() {
324 return required;
325 }
326
327 /** Method to allow for the activation of arguments that might never have their setValue() method called.
328 * @param assigned the desired state as a boolean
329 * @see org.greenstone.gatherer.util.StaticStrings#ASSIGNED_ATTRIBUTE
330 * @see org.greenstone.gatherer.util.StaticStrings#FALSE_STR
331 * @see org.greenstone.gatherer.util.StaticStrings#TRUE_STR
332 */
333 public void setAssigned(boolean assigned) {
334 if(element != null) {
335 element.setAttribute(StaticStrings.ASSIGNED_ATTRIBUTE, (assigned ? StaticStrings.TRUE_STR : StaticStrings.FALSE_STR));
336 }
337 }
338
339 /** Set the custom argument flag.
340 * @param custom true to make this argument custom, false otherwise
341 * @see org.greenstone.gatherer.util.StaticStrings#CUSTOM_ATTRIBUTE
342 * @see org.greenstone.gatherer.util.StaticStrings#FALSE_STR
343 * @see org.greenstone.gatherer.util.StaticStrings#TRUE_STR
344 */
345 public void setCustomArgument(boolean custom) {
346 if(element != null) {
347 element.setAttribute(StaticStrings.CUSTOM_ATTRIBUTE, (custom ? StaticStrings.TRUE_STR : StaticStrings.FALSE_STR));
348 }
349 }
350
351 /** Sets the value of default_value.
352 * @param default_value The new value for default_value as a <strong>String</strong>.
353 */
354 public void setDefaultValue(String default_value) {
355 this.default_value = default_value;
356 }
357
358 /** Set the value of desc.
359 * @param description the new value of desc as a String
360 */
361 public void setDescription(String description) {
362 this.description = description;
363 }
364
365 /** Set the element this argument should be based upon.
366 * @param element the Element
367 */
368 public void setElement(Element element) {
369 this.element = element;
370 }
371
372 /** Mark this argument as being hidden in GLI. */
373 public void setHiddenGLI() {
374 hidden_gli = true;
375 }
376
377 /** Set the upper bound for a range type argument.
378 * @param maximum the maximum as an int
379 */
380 public void setMaximum(int maximum) {
381 this.maximum = maximum;
382 }
383
384 /** Set the lower bound for a range type argument.
385 * @param minimum the minimum as an int
386 */
387 public void setMinimum(int minimum) {
388 this.minimum = minimum;
389 }
390
391 /** Set the detail mode level where this argument will become available.
392 * @param mode_level the mode level as an int
393 */
394 public void setModeLevel(int mode_level) {
395 this.mode_level = mode_level;
396 }
397
398 /** Set the value of name.
399 * @param name the new value of name as a String
400 */
401 public void setName(String name) {
402 this.name = name;
403 }
404
405 /** Sets the value of the options list.
406 * @param list the new options list as a HashMap
407 */
408 public void setOptions(HashMap list) {
409 this.list = list;
410 }
411
412 /** Set the owner of this argument.
413 * @param owner the name of the owner of this argument as a String
414 */
415 public void setOwner(String owner) {
416 this.owner = owner;
417 }
418
419 /** Set the value of required.
420 * @param required the new value of required as a boolean
421 */
422 public void setRequired(boolean required) {
423 this.required = required;
424 }
425
426 /** Set the value of type.
427 * @param type the new value of type as an byte
428 */
429 public void setType(byte type) {
430 this.type = type;
431 }
432
433 /** Set the value of type, by matching a type to the given string.
434 * @param new_type a String which contains the name of a certain argument type
435 * @see org.greenstone.gatherer.util.StaticStrings#ENUM_STR
436 * @see org.greenstone.gatherer.util.StaticStrings#FLAG_STR
437 * @see org.greenstone.gatherer.util.StaticStrings#HIERARCHY_STR
438 * @see org.greenstone.gatherer.util.StaticStrings#INT_STR
439 * @see org.greenstone.gatherer.util.StaticStrings#LANGUAGE_STR
440 * @see org.greenstone.gatherer.util.StaticStrings#METADATA_TYPE_STR
441 * @see org.greenstone.gatherer.util.StaticStrings#METADATUM_TYPE_STR
442 * @see org.greenstone.gatherer.util.StaticStrings#REGEXP_STR
443 */
444 public void setType(String new_type) {
445 if(new_type.equalsIgnoreCase(StaticStrings.ENUM_STR)) {
446 this.type = ENUM;
447 list = new HashMap();
448 }
449 else if(new_type.equalsIgnoreCase(StaticStrings.FLAG_STR)) {
450 this.type = FLAG;
451 }
452 else if(new_type.equalsIgnoreCase(StaticStrings.HIERARCHY_STR)) {
453 this.type = HIERARCHY;
454 }
455 else if(new_type.equalsIgnoreCase(StaticStrings.INT_STR)) {
456 this.type = INTEGER;
457 }
458 else if(new_type.equalsIgnoreCase(StaticStrings.LANGUAGE_STR)) {
459 this.type = LANGUAGE;
460 }
461 else if(new_type.equalsIgnoreCase(StaticStrings.METADATA_TYPE_STR)) {
462 this.type = METADATA;
463 }
464 else if(new_type.equalsIgnoreCase(StaticStrings.METADATUM_TYPE_STR)) {
465 this.type = METADATUM;
466 }
467 else if(new_type.equalsIgnoreCase(StaticStrings.REGEXP_STR)) {
468 this.type = REGEXP;
469 }
470 else {
471 this.type = STRING;
472 }
473 }
474
475 /** Method to set the value of this argument.
476 * @param value the new value for the argument
477 * @see org.greenstone.gatherer.Gatherer#println
478 * @see org.greenstone.gatherer.msm.MSMUtils#setValue
479 */
480 public void setValue(String value) {
481 if(element != null) {
482 MSMUtils.setValue(element, value);
483 }
484 else {
485 Gatherer.println("Argument.setValue(" + value + ") called on a base Argument.");
486 }
487 }
488
489 /** Set the values vector to the given values. Currently I just assign the new values, whereas I may later want to implement a deep clone.
490 * @param values an ArrayList of values
491 * @see org.greenstone.gatherer.Gatherer#println
492 * @see org.greenstone.gatherer.msm.MSMUtils#setValue
493 */
494 public void setValues(ArrayList values) {
495 if(element != null) {
496 StringBuffer value = new StringBuffer();
497 int value_length = values.size();
498 for(int i = 0; i < value_length; i++) {
499 value.append(values.get(i));
500 value.append(StaticStrings.COMMA_CHARACTER);
501 }
502 value.deleteCharAt(value.length() - 1); // Remove last ','
503 MSMUtils.setValue(element, value.toString());
504 }
505 else {
506 Gatherer.println("Argument.setValues([" + values.size() + " items]) called on a base Argument.");
507 }
508 }
509
510 /** Method for translating the data of this class into a string.
511 * @return a String containing a fragment of the total arguments string
512 * @see org.greenstone.gatherer.Gatherer#c_man
513 * @see org.greenstone.gatherer.collection.Collection#msm
514 * @see org.greenstone.gatherer.collection.CollectionManager#getCollection
515 * @see org.greenstone.gatherer.msm.MetadataSetManager#getElement(String)
516 * @see org.greenstone.gatherer.msm.MSMUtils#getValue
517 * @see org.greenstone.gatherer.util.StaticStrings#COMMA_CHARACTER
518 * @see org.greenstone.gatherer.util.StaticStrings#NAME_ATTRIBUTE
519 * @see org.greenstone.gatherer.util.StaticStrings#SPACE_CHARACTER
520 * @see org.greenstone.gatherer.util.StaticStrings#SPEECH_CHARACTER
521 */
522 public String toString() {
523 StringBuffer text = new StringBuffer("-");
524 if(element != null) {
525 if(name == null) {
526 name = element.getAttribute(StaticStrings.NAME_ATTRIBUTE);
527 }
528 text.append(name);
529 String value = MSMUtils.getValue(element);
530 if(value.length() > 0) {
531 text.append(StaticStrings.SPACE_CHARACTER);
532 // If the value contains a space, add speech marks
533 if(value.indexOf(StaticStrings.SPACE_CHARACTER) != -1) {
534 value = StaticStrings.SPEECH_CHARACTER + value + StaticStrings.SPEECH_CHARACTER;
535 }
536 // Tokenize the string
537 StringTokenizer tokenizer = new StringTokenizer(value, ",");
538 while(tokenizer.hasMoreTokens()) {
539 String token = tokenizer.nextToken();
540 if(type == METADATA || type == METADATUM) {
541 ElementWrapper element_wrapper = null;
542 // if you click on exit before saving and format statements have changed, this could be null
543 if (Gatherer.c_man.getCollection() != null) {
544 element_wrapper = Gatherer.c_man.getCollection().msm.getElement(token);
545 }
546 if(element_wrapper != null) {
547 text.append(element_wrapper.toString());
548 element_wrapper = null;
549 }
550 else {
551 text.append(token);
552 }
553 }
554 else {
555 text.append(token);
556 }
557 token = null;
558 text.append(StaticStrings.COMMA_CHARACTER);
559 }
560 tokenizer = null;
561 text.deleteCharAt(text.length() - 1);
562 }
563 value = null;
564 }
565 return text.toString();
566 }
567}
Note: See TracBrowser for help on using the repository browser.