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

Last change on this file since 12824 was 12824, checked in by kjdon, 18 years ago

changed option list form HashMap to ArrayList so they stay in order, added ArgumentOption class, which replaces ListOption from ArgumentControl

  • Property svn:keywords set to Author Date Id Revision
File size: 21.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.metadata.MetadataElement;
42import org.greenstone.gatherer.metadata.MetadataTools;
43import org.greenstone.gatherer.util.StaticStrings;
44import org.greenstone.gatherer.util.Utility;
45import org.greenstone.gatherer.util.XMLTools;
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 /** An element of the argument type enumeration specifying a metadata set combobox control. */
73 static final public byte METADATA_SET_NAMESPACE = 9;
74
75 ///////////kk added the number was 9, I changed it to 10//////////////
76 /** An element of the argument type enumeration specifying a text field. */
77 static final public byte URL = 10;
78 /////////////////////////////////////////////////////////////////
79
80 /** true if this argument should actually be hidden within the GLI. This is important for arguments such as import dir or other location critical arguments. */
81 private boolean hidden_gli = false;
82 /** <i>true</i> if this argument is required for the applicable script to work properly, <i>false</i> otherwise. */
83 private boolean required = false;
84 /** The type of this argument. Used to be an int, but bytes are cheaper. */
85 private byte type = STRING;
86 /** The maximum value an integer based control can have. */
87 private int maximum = Integer.MAX_VALUE;
88 /** The minimum value an integer based control can have. */
89 private int minimum = Integer.MIN_VALUE;
90 /** Every argument has a detail mode level at which it becomes available to the user to edit.
91 * @see org.greenstone.gatherer.Configuration
92 */
93 private int mode_level = Configuration.LIBRARIAN_MODE;
94 /** The DOM element this argument is built around, if any. */
95 private Element element;
96 /** 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. */
97 private ArrayList option_list = null;
98 /** A default value for parameter-type arguments. May be a Perl pattern. */
99 private String default_value = null;
100 /** The text description of this argument parsed from the pluginfo output. */
101 private String description = null;
102 /** The argument flag as it appears in the command. Also used as the unique identifier of an argument. */
103 private String name = null;
104 /** The plugin that owns this argument, for the purposes of visualising inheritance. */
105 private String owner = null;
106
107 private String display_name = null;
108
109 /** Default Constructor. */
110 public Argument() {
111 }
112
113 /** Another constructor but this one is a little more interesting as it takes a DOM element.
114 * @param element the Element this argument is based around
115 */
116 public Argument(Element element) {
117 this.element = element;
118 }
119
120 /** Method to add an element to the option_list.
121 * @param name the name value of the option as a String
122 * @param desc the description of this options as a String
123 */
124 public void addOption(String name, String desc) {
125 if(type == ENUM && name != null) {
126 if(desc == null) {
127 desc = "";
128 }
129 if(option_list == null) {
130 option_list = new ArrayList();
131 }
132 option_list.add(new ArgumentOption(name, desc));
133 }
134 }
135
136 /** Method to compare two arguments for ordering.
137 * @param object the argument we are comparing to, as an Object
138 * @return an int specifying the argument order, using values as set out in String
139 * @see org.greenstone.gatherer.cdm.Argument
140 */
141 public int compareTo(Object object) {
142 if(object instanceof Argument) {
143 return getName().compareTo(((Argument)object).getName());
144 }
145 else {
146 return toString().compareTo(object.toString());
147 }
148 }
149
150 /** Create a copy of this argument.
151 * @return a newly created Argument with the same details as this one
152 */
153 public Argument copy() {
154 Argument copy = new Argument();
155 copy.setDefaultValue(default_value);
156 copy.setDescription(description);
157 copy.setOptions(option_list);
158 copy.setOwner(owner);
159 copy.setName(name);
160 copy.setRequired(required);
161 copy.setType(type);
162 copy.setMinimum(minimum);
163 copy.setMaximum(maximum);
164 copy.setModeLevel(mode_level);
165 copy.setHiddenGLI(hidden_gli);
166 return copy;
167 }
168
169 /** Method to determine if two arguments are equal.
170 * @param object the argument to test against, as an Object
171 * @return true if the arguments names match, false otherwise
172 */
173 public boolean equals(Object object) {
174 return (compareTo(object) == 0);
175 }
176
177 /** Method to retrieve the value of default_value.
178 * @return a String containing the default value
179 */
180 public String getDefaultValue() {
181 return default_value;
182 }
183
184 /** Method to retrieve this arguments description.
185 * @return a String containing the description
186 */
187 public String getDescription() {
188 return description;
189 }
190
191 public Element getElement() {
192 return element;
193 }
194 /** Retrieve the upper bound of a range based argument.
195 * @return the maximum as an int
196 */
197 public int getMaximum() {
198 return maximum;
199 }
200
201 /** Retrieve the lower bound of a range based argument.
202 * @return the minimum as an int
203 */
204 public int getMinimum() {
205 return minimum;
206 }
207
208 /** Retrieves the mode level at which this argument should become available. Any higher levels should also see this argument.
209 * @return the mode level as an int
210 */
211 public int getModeLevel() {
212 return mode_level;
213 }
214
215 /** Method to retrieve the value of name.
216 * @return a String containing the argument name
217 * @see org.greenstone.gatherer.util.StaticStrings#NAME_ATTRIBUTE
218 */
219 public String getName() {
220 if(name == null && element != null) {
221 name = element.getAttribute(StaticStrings.NAME_ATTRIBUTE);
222 }
223 return name;
224 }
225
226 public String getDisplayName() {
227 return display_name;
228 }
229
230
231 /** Method to retrieve the option list for this argument.
232 * @return a HashMap containing &lt;option value&gt; -&gt; &lt;description&gt; entries
233 */
234 public ArrayList getOptions() {
235 return option_list;
236 }
237
238 /** Retrieve the name of the owner of this argument.
239 * @return the owners name as a String
240 */
241 public String getOwner() {
242 return owner;
243 }
244
245 /** Method to determine the type of this argument.
246 * @return a byte specifying the type
247 */
248 public byte getType() {
249 return type;
250 }
251
252 /** Method to retrieve the value of value.
253 * @return the value of value as a String
254 * @see org.greenstone.gatherer.Gatherer#c_man
255 * @see org.greenstone.gatherer.collection.CollectionManager#getCollection
256 */
257 public String getValue()
258 {
259 // Only assigned arguments have values.
260 if (element == null) {
261 return null;
262 }
263
264 return XMLTools.getValue(element);
265 }
266
267
268 /** Method to determine if this argument has been assigned.
269 * @return true if it has, false otherwise
270 * @see org.greenstone.gatherer.util.StaticStrings#ASSIGNED_ATTRIBUTE
271 * @see org.greenstone.gatherer.util.StaticStrings#TRUE_STR
272 */
273 public boolean isAssigned() {
274 return (element != null && element.getAttribute(StaticStrings.ASSIGNED_ATTRIBUTE).equals(StaticStrings.TRUE_STR));
275 }
276
277 /** 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.
278 * @return true if this argument is a custom, false otherwise
279 * @see org.greenstone.gatherer.util.StaticStrings#CUSTOM_ATTRIBUTE
280 * @see org.greenstone.gatherer.util.StaticStrings#TRUE_STR
281 */
282 public boolean isCustomArgument() {
283 return (element != null && element.getAttribute(StaticStrings.CUSTOM_ATTRIBUTE).equals(StaticStrings.TRUE_STR));
284 }
285
286 /** Determine if this argument is hidden in GLI
287 * @return true if the argument is hidden, false otherwise
288 */
289 public boolean isHiddenGLI() {
290 return hidden_gli;
291 }
292
293 /** Method to determine of this argument is required for the associated script to work.
294 * @return true if this argument is required, false otherwise
295 */
296 public boolean isRequired() {
297 return required;
298 }
299
300 /** Method to allow for the activation of arguments that might never have their setValue() method called.
301 * @param assigned the desired state as a boolean
302 * @see org.greenstone.gatherer.util.StaticStrings#ASSIGNED_ATTRIBUTE
303 * @see org.greenstone.gatherer.util.StaticStrings#FALSE_STR
304 * @see org.greenstone.gatherer.util.StaticStrings#TRUE_STR
305 */
306 public void setAssigned(boolean assigned) {
307 if(element != null) {
308 element.setAttribute(StaticStrings.ASSIGNED_ATTRIBUTE, (assigned ? StaticStrings.TRUE_STR : StaticStrings.FALSE_STR));
309 }
310 }
311
312 /** Sets the value of default_value.
313 * @param default_value The new value for default_value as a <strong>String</strong>.
314 */
315 public void setDefaultValue(String default_value) {
316 this.default_value = default_value;
317 }
318
319 /** Set the value of desc.
320 * @param description the new value of desc as a String
321 */
322 public void setDescription(String description) {
323 this.description = description;
324 }
325
326 /** Set the element this argument should be based upon.
327 * @param element the Element
328 */
329 public void setElement(Element element) {
330 this.element = element;
331 }
332
333 /** Mark this argument as being hidden in GLI. */
334 public void setHiddenGLI(boolean hidden) {
335 this.hidden_gli = hidden;
336 }
337
338 /** Set the upper bound for a range type argument.
339 * @param maximum the maximum as an int
340 */
341 public void setMaximum(int maximum) {
342 this.maximum = maximum;
343 }
344
345 /** Set the lower bound for a range type argument.
346 * @param minimum the minimum as an int
347 */
348 public void setMinimum(int minimum) {
349 this.minimum = minimum;
350 }
351
352 /** Set the detail mode level where this argument will become available.
353 * @param mode_level the mode level as an int
354 */
355 public void setModeLevel(int mode_level) {
356 this.mode_level = mode_level;
357 }
358
359 /** Set the value of name.
360 * @param name the new value of name as a String
361 */
362 public void setName(String name) {
363 this.name = name;
364 }
365
366 /** Sets the value of the options list.
367 * @param list the new options list as a HashMap
368 */
369 public void setOptions(ArrayList list) {
370 this.option_list = list;
371 }
372
373 /** Set the owner of this argument.
374 * @param owner the name of the owner of this argument as a String
375 */
376 public void setOwner(String owner) {
377 this.owner = owner;
378 }
379
380 /** Set the value of required.
381 * @param required the new value of required as a boolean
382 */
383 public void setRequired(boolean required) {
384 this.required = required;
385 }
386
387 /** Set the value of type.
388 * @param type the new value of type as an byte
389 */
390 public void setType(byte type) {
391 this.type = type;
392 }
393
394 /** Set the value of type, by matching a type to the given string.
395 * @param new_type a String which contains the name of a certain argument type
396 * @see org.greenstone.gatherer.util.StaticStrings#ENUM_STR
397 * @see org.greenstone.gatherer.util.StaticStrings#FLAG_STR
398 * @see org.greenstone.gatherer.util.StaticStrings#HIERARCHY_STR
399 * @see org.greenstone.gatherer.util.StaticStrings#INT_STR
400 * @see org.greenstone.gatherer.util.StaticStrings#LANGUAGE_STR
401 * @see org.greenstone.gatherer.util.StaticStrings#METADATA_TYPE_STR
402 * @see org.greenstone.gatherer.util.StaticStrings#METADATUM_TYPE_STR
403 * @see org.greenstone.gatherer.util.StaticStrings#REGEXP_STR
404 */
405 public void setType(String new_type) {
406 if(new_type.equalsIgnoreCase(StaticStrings.ENUM_STR)) {
407 this.type = ENUM;
408 option_list = new ArrayList();
409 }
410 else if(new_type.equalsIgnoreCase(StaticStrings.FLAG_STR)) {
411 this.type = FLAG;
412 }
413 else if(new_type.equalsIgnoreCase(StaticStrings.HIERARCHY_STR)) {
414 this.type = HIERARCHY;
415 }
416 else if(new_type.equalsIgnoreCase(StaticStrings.INT_STR)) {
417 this.type = INTEGER;
418 }
419 else if(new_type.equalsIgnoreCase(StaticStrings.LANGUAGE_STR)) {
420 this.type = LANGUAGE;
421 }
422 else if(new_type.equalsIgnoreCase(StaticStrings.METADATA_TYPE_STR)) {
423 this.type = METADATA;
424 }
425 else if(new_type.equalsIgnoreCase(StaticStrings.METADATUM_TYPE_STR)) {
426 this.type = METADATUM;
427 }
428 else if(new_type.equalsIgnoreCase(StaticStrings.REGEXP_STR)) {
429 this.type = REGEXP;
430 }
431 else {
432 this.type = STRING;
433 }
434 }
435
436 /** Method to set the value of this argument.
437 * @param value the new value for the argument
438 * @see org.greenstone.gatherer.Gatherer#println
439 */
440 public void setValue(String value) {
441 if(element != null) {
442 XMLTools.setValue(element, value);
443 }
444 else {
445 DebugStream.println("Argument.setValue(" + value + ") called on a base Argument.");
446 }
447 }
448
449 /** 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.
450 * @param values an ArrayList of values
451 * @see org.greenstone.gatherer.Gatherer#println
452 */
453 public void setValues(ArrayList values) {
454 if(element != null) {
455 StringBuffer value = new StringBuffer();
456 int value_length = values.size();
457 for(int i = 0; i < value_length; i++) {
458 value.append(values.get(i));
459 value.append(StaticStrings.COMMA_CHARACTER);
460 }
461 value.deleteCharAt(value.length() - 1); // Remove last ','
462 XMLTools.setValue(element, value.toString());
463 }
464 else {
465 DebugStream.println("Argument.setValues([" + values.size() + " items]) called on a base Argument.");
466 }
467 }
468
469 /** Method for translating the data of this class into a string.
470 * @return a String containing a fragment of the total arguments string
471 * @see org.greenstone.gatherer.Gatherer#c_man
472 * @see org.greenstone.gatherer.collection.CollectionManager#getCollection
473 * @see org.greenstone.gatherer.util.StaticStrings#COMMA_CHARACTER
474 * @see org.greenstone.gatherer.util.StaticStrings#NAME_ATTRIBUTE
475 * @see org.greenstone.gatherer.util.StaticStrings#SPACE_CHARACTER
476 * @see org.greenstone.gatherer.util.StaticStrings#SPEECH_CHARACTER
477 */
478 public String toString()
479 {
480 StringBuffer text = new StringBuffer("-");
481
482 if (element == null) {
483 return text.toString();
484 }
485
486 if (name == null) {
487 name = element.getAttribute(StaticStrings.NAME_ATTRIBUTE);
488 }
489 text.append(name);
490
491 String value = XMLTools.getValue(element);
492 if (value.length() == 0) {
493 return text.toString();
494 }
495
496 text.append(StaticStrings.SPACE_CHARACTER);
497
498// // Handle metadata elements specially
499// if (type == METADATA || type == METADATUM) {
500// // Tokenize the string
501// StringTokenizer tokenizer = new StringTokenizer(value, ",");
502// while (tokenizer.hasMoreTokens()) {
503// String token = tokenizer.nextToken();
504
505// MetadataElement metadata_element = MetadataTools.getMetadataElementWithDisplayName(token);
506// if (metadata_element != null) {
507// text.append(metadata_element.getFullName());
508// }
509// else {
510// text.append(token);
511// }
512
513// if (tokenizer.hasMoreTokens()) {
514// text.append(StaticStrings.COMMA_CHARACTER);
515// }
516// }
517// return text.toString();
518// }
519
520 // If the value contains a space, add speech marks
521 // (Except for metadata elements, which won't have spaces when written out to collect.cfg)
522 if (value.indexOf(StaticStrings.SPACE_CHARACTER) != -1 && !(type == METADATUM || type == METADATA)) {
523 value = StaticStrings.SPEECH_CHARACTER + value + StaticStrings.SPEECH_CHARACTER;
524 }
525
526 text.append(value);
527 return text.toString();
528 }
529
530 /** parse the <Option> XML from eg import.pl -xml or pluginfo.pl -xml */
531 public void parseXML(Element option) {
532
533 for(Node node = option.getFirstChild(); node != null; node = node.getNextSibling()) {
534 String node_name = node.getNodeName();
535 if(node_name.equals("Name")) {
536 setName(XMLTools.getValue(node));
537 }
538 else if(node_name.equals("Desc")) {
539 setDescription(XMLTools.getValue(node));
540 }
541 else if(node_name.equals("Type")) {
542 setType(XMLTools.getValue(node));
543 }
544 else if(node_name.equals("Default")) {
545 setDefaultValue(XMLTools.getValue(node));
546 }
547 else if(node_name.equals("Required")) {
548 String v = XMLTools.getValue(node);
549 if(v != null && v.equals("yes")) {
550 setRequired(true);
551 }
552 }
553 else if(node_name.equals("List")) {
554 // Two final loops are required to parse lists.
555 for(Node value = node.getFirstChild(); value != null; value = value.getNextSibling()) {
556 if(value.getNodeName().equals("Value")) {
557 String key = null;
558 String desc = "";
559 for(Node subvalue = value.getFirstChild(); subvalue != null; subvalue = subvalue.getNextSibling()) {
560 node_name = subvalue.getNodeName();
561 if(node_name.equals("Name")) {
562 key = XMLTools.getValue(subvalue);
563 }
564 else if(node_name.equals("Desc")) {
565 desc = XMLTools.getValue(subvalue);
566 }
567 }
568 if(key != null) {
569 addOption(key, desc);
570 }
571 }
572 }
573 }
574 else if(node_name.equals("Range")) {
575 String range_raw = XMLTools.getValue(node);
576 int index = -1;
577 if((index = range_raw.indexOf(StaticStrings.COMMA_CHARACTER)) != -1) {
578 if(index > 0) {
579 try {
580 String first_number = range_raw.substring(0, index);
581 setMinimum(Integer.parseInt(first_number));
582 first_number = null;
583 }
584 catch(Exception exception) {
585 }
586 }
587
588 if(index + 1 < range_raw.length()) {
589 try {
590 String second_number = range_raw.substring(index + 1);
591 setMaximum(Integer.parseInt(second_number));
592 second_number = null;
593 }
594 catch(Exception exception) {
595 }
596 }
597 }
598 // Else it wasn't a valid range anyway, so ignore it
599 }
600 else if(node_name.equals("HiddenGLI")) {
601 setHiddenGLI(true);
602 }
603 else if(node_name.equals("ModeGLI")) {
604 String mode_level_str = XMLTools.getValue(node);
605 try {
606 int mode_level = Integer.parseInt(mode_level_str);
607 setModeLevel(mode_level);
608 }
609 catch(Exception exception) {
610 DebugStream.println("Exception in Argument.parseXML() - Unexpected but non-fatal");
611 DebugStream.printStackTrace(exception);
612 }
613 }
614
615 } // for each option
616
617 } // parseXML
618
619 public class ArgumentOption
620 implements Comparable {
621 public String name;
622 public String description;
623 private String text; // cached version
624
625 public ArgumentOption(String name, String desc) {
626 this.name = name;
627 this.description = desc;
628 }
629
630 public int compareTo(Object obj) {
631 return toString().compareTo(obj.toString());
632 }
633
634 public boolean equals(Object obj) {
635 return (obj != null && compareTo(obj) == 0);
636 }
637
638 public String toString() {
639 return name + " "+ StaticStrings.MINUS_CHARACTER + " "+ description;
640 }
641
642 public String getToolTip() {
643 return Utility.formatHTMLWidth(name+": "+description, 80);
644 }
645 }
646}
Note: See TracBrowser for help on using the repository browser.