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

Last change on this file since 7995 was 7995, checked in by mdewsnip, 20 years ago

Removed a whole lot of references to the msm package, which is on its way out.

  • Property svn:keywords set to Author Date Id Revision
File size: 19.6 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 copy.setMinimum(minimum);
153 copy.setMaximum(maximum);
154 copy.setModeLevel(mode_level);
155 return copy;
156 }
157
158 /** Method to determine if two arguments are equal.
159 * @param object the argument to test against, as an Object
160 * @return true if the arguments names match, false otherwise
161 */
162 public boolean equals(Object object) {
163 return (compareTo(object) == 0);
164 }
165
166 /** Method to retrieve the value of default_value.
167 * @return a String containing the default value
168 */
169 public String getDefaultValue() {
170 return default_value;
171 }
172
173 /** Method to retrieve this arguments description.
174 * @return a String containing the description
175 */
176 public String getDescription() {
177 return description;
178 }
179
180 /** Method to retrieve the description of a certain list option value.
181 * @param key the String whose description we are searching for
182 * @return the description of the desired key as a String which may be empty if no such key exists
183 */
184 public String getDescription(String key) {
185 if(list.containsKey(key)) {
186 return (String)list.get(key);
187 }
188 return "";
189 }
190
191 /** Retrieve the upper bound of a range based argument.
192 * @return the maximum as an int
193 */
194 public int getMaximum() {
195 return maximum;
196 }
197
198 /** Retrieve the lower bound of a range based argument.
199 * @return the minimum as an int
200 */
201 public int getMinimum() {
202 return minimum;
203 }
204
205 /** Retrieves the mode level at which this argument should become available. Any higher levels should also see this argument.
206 * @return the mode level as an int
207 */
208 public int getModeLevel() {
209 return mode_level;
210 }
211
212 /** Method to retrieve the value of name.
213 * @return a String containing the argument name
214 * @see org.greenstone.gatherer.util.StaticStrings#NAME_ATTRIBUTE
215 */
216 public String getName() {
217 if(name == null && element != null) {
218 name = element.getAttribute(StaticStrings.NAME_ATTRIBUTE);
219 }
220 return name;
221 }
222
223 /** Method to retrieve the option list for this argument.
224 * @return a HashMap containing &lt;option value&gt; -&gt; &lt;description&gt; entries
225 */
226 public HashMap getOptions() {
227 return list;
228 }
229
230 /** Retrieve the name of the owner of this argument.
231 * @return the owners name as a String
232 */
233 public String getOwner() {
234 return owner;
235 }
236
237 /** Method to determine the type of this argument.
238 * @return a byte specifying the type
239 */
240 public byte getType() {
241 return type;
242 }
243
244 /** Method to retrieve the value of value.
245 * @return the value of value as a String
246 * @see org.greenstone.gatherer.Gatherer#c_man
247 * @see org.greenstone.gatherer.collection.CollectionManager#getCollection
248 */
249 public String getValue() {
250 String value = null;
251 // Only assigned arguments have values.
252 if(element != null) {
253 value = MSMUtils.getValue(element);
254 // We may have to retrieve the language dependant string for a MSM Element
255 if(type == METADATUM) {
256 ElementWrapper element_wrapper = Gatherer.c_man.getCollection().msm.getElement(value);
257 if(element_wrapper != null) {
258 value = element_wrapper.getName();
259 }
260 }
261 }
262 return value;
263 }
264
265 /** Retrieve the vector of values.
266 * @return an ArrayList of values
267 * @see org.greenstone.gatherer.Gatherer#c_man
268 * @see org.greenstone.gatherer.collection.CollectionManager#getCollection
269 */
270 public ArrayList getValues() {
271 ArrayList values = new ArrayList();
272 // Only assigned arguments have values.
273 if(element != null) {
274 String value = MSMUtils.getValue(element);
275 StringTokenizer tokenizer = new StringTokenizer(value, ",");
276 while(tokenizer.hasMoreTokens()) {
277 String token = tokenizer.nextToken();
278 if(type == METADATA) {
279 ElementWrapper element_wrapper = Gatherer.c_man.getCollection().msm.getElement(token);
280 if(element_wrapper != null) {
281 token = element_wrapper.toString();
282 }
283 }
284 values.add(token);
285 }
286 }
287 return values;
288 }
289
290 /** Method to determine if this argument has been assigned.
291 * @return true if it has, false otherwise
292 * @see org.greenstone.gatherer.util.StaticStrings#ASSIGNED_ATTRIBUTE
293 * @see org.greenstone.gatherer.util.StaticStrings#TRUE_STR
294 */
295 public boolean isAssigned() {
296 return (element != null && element.getAttribute(StaticStrings.ASSIGNED_ATTRIBUTE).equals(StaticStrings.TRUE_STR));
297 }
298
299 /** 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.
300 * @return true if this argument is a custom, false otherwise
301 * @see org.greenstone.gatherer.util.StaticStrings#CUSTOM_ATTRIBUTE
302 * @see org.greenstone.gatherer.util.StaticStrings#TRUE_STR
303 */
304 public boolean isCustomArgument() {
305 return (element != null && element.getAttribute(StaticStrings.CUSTOM_ATTRIBUTE).equals(StaticStrings.TRUE_STR));
306 }
307
308 /** Determine if this argument is hidden in GLI
309 * @return true if the argument is hidden, false otherwise
310 */
311 public boolean isHiddenGLI() {
312 return hidden_gli;
313 }
314
315 /** Method to determine of this argument is required for the associated script to work.
316 * @return true if this argument is required, false otherwise
317 */
318 public boolean isRequired() {
319 return required;
320 }
321
322 /** Method to allow for the activation of arguments that might never have their setValue() method called.
323 * @param assigned the desired state as a boolean
324 * @see org.greenstone.gatherer.util.StaticStrings#ASSIGNED_ATTRIBUTE
325 * @see org.greenstone.gatherer.util.StaticStrings#FALSE_STR
326 * @see org.greenstone.gatherer.util.StaticStrings#TRUE_STR
327 */
328 public void setAssigned(boolean assigned) {
329 if(element != null) {
330 element.setAttribute(StaticStrings.ASSIGNED_ATTRIBUTE, (assigned ? StaticStrings.TRUE_STR : StaticStrings.FALSE_STR));
331 }
332 }
333
334 /** Set the custom argument flag.
335 * @param custom true to make this argument custom, false otherwise
336 * @see org.greenstone.gatherer.util.StaticStrings#CUSTOM_ATTRIBUTE
337 * @see org.greenstone.gatherer.util.StaticStrings#FALSE_STR
338 * @see org.greenstone.gatherer.util.StaticStrings#TRUE_STR
339 */
340 public void setCustomArgument(boolean custom) {
341 if(element != null) {
342 element.setAttribute(StaticStrings.CUSTOM_ATTRIBUTE, (custom ? StaticStrings.TRUE_STR : StaticStrings.FALSE_STR));
343 }
344 }
345
346 /** Sets the value of default_value.
347 * @param default_value The new value for default_value as a <strong>String</strong>.
348 */
349 public void setDefaultValue(String default_value) {
350 this.default_value = default_value;
351 }
352
353 /** Set the value of desc.
354 * @param description the new value of desc as a String
355 */
356 public void setDescription(String description) {
357 this.description = description;
358 }
359
360 /** Set the element this argument should be based upon.
361 * @param element the Element
362 */
363 public void setElement(Element element) {
364 this.element = element;
365 }
366
367 /** Mark this argument as being hidden in GLI. */
368 public void setHiddenGLI() {
369 hidden_gli = true;
370 }
371
372 /** Set the upper bound for a range type argument.
373 * @param maximum the maximum as an int
374 */
375 public void setMaximum(int maximum) {
376 this.maximum = maximum;
377 }
378
379 /** Set the lower bound for a range type argument.
380 * @param minimum the minimum as an int
381 */
382 public void setMinimum(int minimum) {
383 this.minimum = minimum;
384 }
385
386 /** Set the detail mode level where this argument will become available.
387 * @param mode_level the mode level as an int
388 */
389 public void setModeLevel(int mode_level) {
390 this.mode_level = mode_level;
391 }
392
393 /** Set the value of name.
394 * @param name the new value of name as a String
395 */
396 public void setName(String name) {
397 this.name = name;
398 }
399
400 /** Sets the value of the options list.
401 * @param list the new options list as a HashMap
402 */
403 public void setOptions(HashMap list) {
404 this.list = list;
405 }
406
407 /** Set the owner of this argument.
408 * @param owner the name of the owner of this argument as a String
409 */
410 public void setOwner(String owner) {
411 this.owner = owner;
412 }
413
414 /** Set the value of required.
415 * @param required the new value of required as a boolean
416 */
417 public void setRequired(boolean required) {
418 this.required = required;
419 }
420
421 /** Set the value of type.
422 * @param type the new value of type as an byte
423 */
424 public void setType(byte type) {
425 this.type = type;
426 }
427
428 /** Set the value of type, by matching a type to the given string.
429 * @param new_type a String which contains the name of a certain argument type
430 * @see org.greenstone.gatherer.util.StaticStrings#ENUM_STR
431 * @see org.greenstone.gatherer.util.StaticStrings#FLAG_STR
432 * @see org.greenstone.gatherer.util.StaticStrings#HIERARCHY_STR
433 * @see org.greenstone.gatherer.util.StaticStrings#INT_STR
434 * @see org.greenstone.gatherer.util.StaticStrings#LANGUAGE_STR
435 * @see org.greenstone.gatherer.util.StaticStrings#METADATA_TYPE_STR
436 * @see org.greenstone.gatherer.util.StaticStrings#METADATUM_TYPE_STR
437 * @see org.greenstone.gatherer.util.StaticStrings#REGEXP_STR
438 */
439 public void setType(String new_type) {
440 if(new_type.equalsIgnoreCase(StaticStrings.ENUM_STR)) {
441 this.type = ENUM;
442 list = new HashMap();
443 }
444 else if(new_type.equalsIgnoreCase(StaticStrings.FLAG_STR)) {
445 this.type = FLAG;
446 }
447 else if(new_type.equalsIgnoreCase(StaticStrings.HIERARCHY_STR)) {
448 this.type = HIERARCHY;
449 }
450 else if(new_type.equalsIgnoreCase(StaticStrings.INT_STR)) {
451 this.type = INTEGER;
452 }
453 else if(new_type.equalsIgnoreCase(StaticStrings.LANGUAGE_STR)) {
454 this.type = LANGUAGE;
455 }
456 else if(new_type.equalsIgnoreCase(StaticStrings.METADATA_TYPE_STR)) {
457 this.type = METADATA;
458 }
459 else if(new_type.equalsIgnoreCase(StaticStrings.METADATUM_TYPE_STR)) {
460 this.type = METADATUM;
461 }
462 else if(new_type.equalsIgnoreCase(StaticStrings.REGEXP_STR)) {
463 this.type = REGEXP;
464 }
465 else {
466 this.type = STRING;
467 }
468 }
469
470 /** Method to set the value of this argument.
471 * @param value the new value for the argument
472 * @see org.greenstone.gatherer.Gatherer#println
473 */
474 public void setValue(String value) {
475 if(element != null) {
476 MSMUtils.setValue(element, value);
477 }
478 else {
479 Gatherer.println("Argument.setValue(" + value + ") called on a base Argument.");
480 }
481 }
482
483 /** 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.
484 * @param values an ArrayList of values
485 * @see org.greenstone.gatherer.Gatherer#println
486 */
487 public void setValues(ArrayList values) {
488 if(element != null) {
489 StringBuffer value = new StringBuffer();
490 int value_length = values.size();
491 for(int i = 0; i < value_length; i++) {
492 value.append(values.get(i));
493 value.append(StaticStrings.COMMA_CHARACTER);
494 }
495 value.deleteCharAt(value.length() - 1); // Remove last ','
496 MSMUtils.setValue(element, value.toString());
497 }
498 else {
499 Gatherer.println("Argument.setValues([" + values.size() + " items]) called on a base Argument.");
500 }
501 }
502
503 /** Method for translating the data of this class into a string.
504 * @return a String containing a fragment of the total arguments string
505 * @see org.greenstone.gatherer.Gatherer#c_man
506 * @see org.greenstone.gatherer.collection.CollectionManager#getCollection
507 * @see org.greenstone.gatherer.util.StaticStrings#COMMA_CHARACTER
508 * @see org.greenstone.gatherer.util.StaticStrings#NAME_ATTRIBUTE
509 * @see org.greenstone.gatherer.util.StaticStrings#SPACE_CHARACTER
510 * @see org.greenstone.gatherer.util.StaticStrings#SPEECH_CHARACTER
511 */
512 public String toString() {
513 StringBuffer text = new StringBuffer("-");
514 if(element != null) {
515 if(name == null) {
516 name = element.getAttribute(StaticStrings.NAME_ATTRIBUTE);
517 }
518 text.append(name);
519 String value = MSMUtils.getValue(element);
520 if(value.length() > 0) {
521 text.append(StaticStrings.SPACE_CHARACTER);
522 // If the value contains a space, add speech marks
523 if(value.indexOf(StaticStrings.SPACE_CHARACTER) != -1) {
524 value = StaticStrings.SPEECH_CHARACTER + value + StaticStrings.SPEECH_CHARACTER;
525 }
526 // Tokenize the string
527 StringTokenizer tokenizer = new StringTokenizer(value, ",");
528 while(tokenizer.hasMoreTokens()) {
529 String token = tokenizer.nextToken();
530 if(type == METADATA || type == METADATUM) {
531 ElementWrapper element_wrapper = null;
532 // if you click on exit before saving and format statements have changed, this could be null
533 if (Gatherer.c_man.getCollection() != null) {
534 element_wrapper = Gatherer.c_man.getCollection().msm.getElement(token);
535 }
536 if(element_wrapper != null) {
537 text.append(element_wrapper.toString());
538 element_wrapper = null;
539 }
540 else {
541 text.append(token);
542 }
543 }
544 else {
545 text.append(token);
546 }
547 token = null;
548 text.append(StaticStrings.COMMA_CHARACTER);
549 }
550 tokenizer = null;
551 text.deleteCharAt(text.length() - 1);
552 }
553 value = null;
554 }
555 return text.toString();
556 }
557}
Note: See TracBrowser for help on using the repository browser.