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

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

added in min, max and modelevel to the copy constructor

  • Property svn:keywords set to Author Date Id Revision
File size: 20.5 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.Collection#msm
248 * @see org.greenstone.gatherer.collection.CollectionManager#getCollection
249 * @see org.greenstone.gatherer.msm.ElementWrapper
250 * @see org.greenstone.gatherer.msm.MetadataSetManager#getElement(String)
251 * @see org.greenstone.gatherer.msm.MSMUtils#getValue(Node)
252 */
253 public String getValue() {
254 String value = null;
255 // Only assigned arguments have values.
256 if(element != null) {
257 value = MSMUtils.getValue(element);
258 // We may have to retrieve the language dependant string for a MSM Element
259 if(type == METADATUM) {
260 ElementWrapper element_wrapper = Gatherer.c_man.getCollection().msm.getElement(value);
261 if(element_wrapper != null) {
262 value = element_wrapper.getName();
263 }
264 }
265 }
266 return value;
267 }
268
269 /** Retrieve the vector of values.
270 * @return an ArrayList of values
271 * @see org.greenstone.gatherer.Gatherer#c_man
272 * @see org.greenstone.gatherer.collection.Collection#msm
273 * @see org.greenstone.gatherer.collection.CollectionManager#getCollection
274 * @see org.greenstone.gatherer.msm.ElementWrapper
275 * @see org.greenstone.gatherer.msm.MetadataSetManager#getElement(String)
276 * @see org.greenstone.gatherer.msm.MSMUtils#getValue(Node)
277 */
278 public ArrayList getValues() {
279 ArrayList values = new ArrayList();
280 // Only assigned arguments have values.
281 if(element != null) {
282 String value = MSMUtils.getValue(element);
283 StringTokenizer tokenizer = new StringTokenizer(value, ",");
284 while(tokenizer.hasMoreTokens()) {
285 String token = tokenizer.nextToken();
286 if(type == METADATA) {
287 ElementWrapper element_wrapper = Gatherer.c_man.getCollection().msm.getElement(token);
288 if(element_wrapper != null) {
289 token = element_wrapper.toString();
290 }
291 }
292 values.add(token);
293 }
294 }
295 return values;
296 }
297
298 /** Method to determine if this argument has been assigned.
299 * @return true if it has, false otherwise
300 * @see org.greenstone.gatherer.util.StaticStrings#ASSIGNED_ATTRIBUTE
301 * @see org.greenstone.gatherer.util.StaticStrings#TRUE_STR
302 */
303 public boolean isAssigned() {
304 return (element != null && element.getAttribute(StaticStrings.ASSIGNED_ATTRIBUTE).equals(StaticStrings.TRUE_STR));
305 }
306
307 /** 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.
308 * @return true if this argument is a custom, false otherwise
309 * @see org.greenstone.gatherer.util.StaticStrings#CUSTOM_ATTRIBUTE
310 * @see org.greenstone.gatherer.util.StaticStrings#TRUE_STR
311 */
312 public boolean isCustomArgument() {
313 return (element != null && element.getAttribute(StaticStrings.CUSTOM_ATTRIBUTE).equals(StaticStrings.TRUE_STR));
314 }
315
316 /** Determine if this argument is hidden in GLI
317 * @return true if the argument is hidden, false otherwise
318 */
319 public boolean isHiddenGLI() {
320 return hidden_gli;
321 }
322
323 /** Method to determine of this argument is required for the associated script to work.
324 * @return true if this argument is required, false otherwise
325 */
326 public boolean isRequired() {
327 return required;
328 }
329
330 /** Method to allow for the activation of arguments that might never have their setValue() method called.
331 * @param assigned the desired state as a boolean
332 * @see org.greenstone.gatherer.util.StaticStrings#ASSIGNED_ATTRIBUTE
333 * @see org.greenstone.gatherer.util.StaticStrings#FALSE_STR
334 * @see org.greenstone.gatherer.util.StaticStrings#TRUE_STR
335 */
336 public void setAssigned(boolean assigned) {
337 if(element != null) {
338 element.setAttribute(StaticStrings.ASSIGNED_ATTRIBUTE, (assigned ? StaticStrings.TRUE_STR : StaticStrings.FALSE_STR));
339 }
340 }
341
342 /** Set the custom argument flag.
343 * @param custom true to make this argument custom, false otherwise
344 * @see org.greenstone.gatherer.util.StaticStrings#CUSTOM_ATTRIBUTE
345 * @see org.greenstone.gatherer.util.StaticStrings#FALSE_STR
346 * @see org.greenstone.gatherer.util.StaticStrings#TRUE_STR
347 */
348 public void setCustomArgument(boolean custom) {
349 if(element != null) {
350 element.setAttribute(StaticStrings.CUSTOM_ATTRIBUTE, (custom ? StaticStrings.TRUE_STR : StaticStrings.FALSE_STR));
351 }
352 }
353
354 /** Sets the value of default_value.
355 * @param default_value The new value for default_value as a <strong>String</strong>.
356 */
357 public void setDefaultValue(String default_value) {
358 this.default_value = default_value;
359 }
360
361 /** Set the value of desc.
362 * @param description the new value of desc as a String
363 */
364 public void setDescription(String description) {
365 this.description = description;
366 }
367
368 /** Set the element this argument should be based upon.
369 * @param element the Element
370 */
371 public void setElement(Element element) {
372 this.element = element;
373 }
374
375 /** Mark this argument as being hidden in GLI. */
376 public void setHiddenGLI() {
377 hidden_gli = true;
378 }
379
380 /** Set the upper bound for a range type argument.
381 * @param maximum the maximum as an int
382 */
383 public void setMaximum(int maximum) {
384 this.maximum = maximum;
385 }
386
387 /** Set the lower bound for a range type argument.
388 * @param minimum the minimum as an int
389 */
390 public void setMinimum(int minimum) {
391 this.minimum = minimum;
392 }
393
394 /** Set the detail mode level where this argument will become available.
395 * @param mode_level the mode level as an int
396 */
397 public void setModeLevel(int mode_level) {
398 this.mode_level = mode_level;
399 }
400
401 /** Set the value of name.
402 * @param name the new value of name as a String
403 */
404 public void setName(String name) {
405 this.name = name;
406 }
407
408 /** Sets the value of the options list.
409 * @param list the new options list as a HashMap
410 */
411 public void setOptions(HashMap list) {
412 this.list = list;
413 }
414
415 /** Set the owner of this argument.
416 * @param owner the name of the owner of this argument as a String
417 */
418 public void setOwner(String owner) {
419 this.owner = owner;
420 }
421
422 /** Set the value of required.
423 * @param required the new value of required as a boolean
424 */
425 public void setRequired(boolean required) {
426 this.required = required;
427 }
428
429 /** Set the value of type.
430 * @param type the new value of type as an byte
431 */
432 public void setType(byte type) {
433 this.type = type;
434 }
435
436 /** Set the value of type, by matching a type to the given string.
437 * @param new_type a String which contains the name of a certain argument type
438 * @see org.greenstone.gatherer.util.StaticStrings#ENUM_STR
439 * @see org.greenstone.gatherer.util.StaticStrings#FLAG_STR
440 * @see org.greenstone.gatherer.util.StaticStrings#HIERARCHY_STR
441 * @see org.greenstone.gatherer.util.StaticStrings#INT_STR
442 * @see org.greenstone.gatherer.util.StaticStrings#LANGUAGE_STR
443 * @see org.greenstone.gatherer.util.StaticStrings#METADATA_TYPE_STR
444 * @see org.greenstone.gatherer.util.StaticStrings#METADATUM_TYPE_STR
445 * @see org.greenstone.gatherer.util.StaticStrings#REGEXP_STR
446 */
447 public void setType(String new_type) {
448 if(new_type.equalsIgnoreCase(StaticStrings.ENUM_STR)) {
449 this.type = ENUM;
450 list = new HashMap();
451 }
452 else if(new_type.equalsIgnoreCase(StaticStrings.FLAG_STR)) {
453 this.type = FLAG;
454 }
455 else if(new_type.equalsIgnoreCase(StaticStrings.HIERARCHY_STR)) {
456 this.type = HIERARCHY;
457 }
458 else if(new_type.equalsIgnoreCase(StaticStrings.INT_STR)) {
459 this.type = INTEGER;
460 }
461 else if(new_type.equalsIgnoreCase(StaticStrings.LANGUAGE_STR)) {
462 this.type = LANGUAGE;
463 }
464 else if(new_type.equalsIgnoreCase(StaticStrings.METADATA_TYPE_STR)) {
465 this.type = METADATA;
466 }
467 else if(new_type.equalsIgnoreCase(StaticStrings.METADATUM_TYPE_STR)) {
468 this.type = METADATUM;
469 }
470 else if(new_type.equalsIgnoreCase(StaticStrings.REGEXP_STR)) {
471 this.type = REGEXP;
472 }
473 else {
474 this.type = STRING;
475 }
476 }
477
478 /** Method to set the value of this argument.
479 * @param value the new value for the argument
480 * @see org.greenstone.gatherer.Gatherer#println
481 * @see org.greenstone.gatherer.msm.MSMUtils#setValue
482 */
483 public void setValue(String value) {
484 if(element != null) {
485 MSMUtils.setValue(element, value);
486 }
487 else {
488 Gatherer.println("Argument.setValue(" + value + ") called on a base Argument.");
489 }
490 }
491
492 /** 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.
493 * @param values an ArrayList of values
494 * @see org.greenstone.gatherer.Gatherer#println
495 * @see org.greenstone.gatherer.msm.MSMUtils#setValue
496 */
497 public void setValues(ArrayList values) {
498 if(element != null) {
499 StringBuffer value = new StringBuffer();
500 int value_length = values.size();
501 for(int i = 0; i < value_length; i++) {
502 value.append(values.get(i));
503 value.append(StaticStrings.COMMA_CHARACTER);
504 }
505 value.deleteCharAt(value.length() - 1); // Remove last ','
506 MSMUtils.setValue(element, value.toString());
507 }
508 else {
509 Gatherer.println("Argument.setValues([" + values.size() + " items]) called on a base Argument.");
510 }
511 }
512
513 /** Method for translating the data of this class into a string.
514 * @return a String containing a fragment of the total arguments string
515 * @see org.greenstone.gatherer.Gatherer#c_man
516 * @see org.greenstone.gatherer.collection.Collection#msm
517 * @see org.greenstone.gatherer.collection.CollectionManager#getCollection
518 * @see org.greenstone.gatherer.msm.MetadataSetManager#getElement(String)
519 * @see org.greenstone.gatherer.msm.MSMUtils#getValue
520 * @see org.greenstone.gatherer.util.StaticStrings#COMMA_CHARACTER
521 * @see org.greenstone.gatherer.util.StaticStrings#NAME_ATTRIBUTE
522 * @see org.greenstone.gatherer.util.StaticStrings#SPACE_CHARACTER
523 * @see org.greenstone.gatherer.util.StaticStrings#SPEECH_CHARACTER
524 */
525 public String toString() {
526 StringBuffer text = new StringBuffer("-");
527 if(element != null) {
528 if(name == null) {
529 name = element.getAttribute(StaticStrings.NAME_ATTRIBUTE);
530 }
531 text.append(name);
532 String value = MSMUtils.getValue(element);
533 if(value.length() > 0) {
534 text.append(StaticStrings.SPACE_CHARACTER);
535 // If the value contains a space, add speech marks
536 if(value.indexOf(StaticStrings.SPACE_CHARACTER) != -1) {
537 value = StaticStrings.SPEECH_CHARACTER + value + StaticStrings.SPEECH_CHARACTER;
538 }
539 // Tokenize the string
540 StringTokenizer tokenizer = new StringTokenizer(value, ",");
541 while(tokenizer.hasMoreTokens()) {
542 String token = tokenizer.nextToken();
543 if(type == METADATA || type == METADATUM) {
544 ElementWrapper element_wrapper = null;
545 // if you click on exit before saving and format statements have changed, this could be null
546 if (Gatherer.c_man.getCollection() != null) {
547 element_wrapper = Gatherer.c_man.getCollection().msm.getElement(token);
548 }
549 if(element_wrapper != null) {
550 text.append(element_wrapper.toString());
551 element_wrapper = null;
552 }
553 else {
554 text.append(token);
555 }
556 }
557 else {
558 text.append(token);
559 }
560 token = null;
561 text.append(StaticStrings.COMMA_CHARACTER);
562 }
563 tokenizer = null;
564 text.deleteCharAt(text.length() - 1);
565 }
566 value = null;
567 }
568 return text.toString();
569 }
570}
Note: See TracBrowser for help on using the repository browser.