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

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

Removed all occurrences of classes explicitly importing other classes in the same package.

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