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

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

reindented the file - it had horrible huge indentation

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