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

Last change on this file since 17826 was 17826, checked in by kjdon, 15 years ago

changed method name from setDispalyName to setDisplayName

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