source: other-projects/FileTransfer-WebSocketPair/testGXTWithGreenstone/src/org/greenstone/gatherer/cdm/Argument.java@ 33053

Last change on this file since 33053 was 33053, checked in by ak19, 5 years ago

I still had some stuff of Nathan Kelly's (FileTransfer-WebSocketPair) sitting on my USB. Had already commited the Themes folder at the time, 2 years back. Not sure if he wanted this additional folder commited. But I didn't want to delete it and decided it will be better off on SVN. When we use his project, if we find we didn't need this test folder, we can remove it from svn then.

File size: 21.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.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 metadata combobox control. */
65 static final public byte METADATA = 5;
66 /** An element of the argument type enumeration specifying a text field. */
67 static final public byte STRING = 6;
68 /** An element of the argument type enumeration specifying a regular expression text field. */
69 static final public byte REGEXP = 7;
70 /** An element of the argument type enumeration specifying a metadata set combobox control. */
71 static final public byte METADATA_SET_NAMESPACE = 8;
72 /** An element of the argument type enumeration specifying a text field. */
73 static final public byte URL = 9;
74 /** An editable combo box */
75 static final public byte ENUM_STRING = 10;
76
77 /////////////////////////////////////////////////////////////////
78
79 /** 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. */
80 private boolean hidden_gli = false;
81 /** <i>true</i> if this argument is required for the applicable script to work properly, <i>false</i> otherwise. */
82 private boolean required = false;
83 /** The type of this argument. Used to be an int, but bytes are cheaper. */
84 private byte type = STRING;
85 /** The maximum value an integer based control can have. */
86 private int maximum = Integer.MAX_VALUE;
87 /** The minimum value an integer based control can have. */
88 private int minimum = Integer.MIN_VALUE;
89 /** Every argument has a detail mode level at which it becomes available to the user to edit.
90 * @see org.greenstone.gatherer.Configuration
91 */
92 private int mode_level = Configuration.LIBRARIAN_MODE;
93 /** The DOM element this argument is built around, if any. */
94 private Element element;
95 /** 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. */
96 private ArrayList option_list = null;
97 /** A default value for parameter-type arguments. May be a Perl pattern. */
98 private String default_value = null;
99 /** The text description of this argument parsed from the pluginfo output. */
100 private String description = null;
101 /** The argument flag as it appears in the command. Also used as the unique identifier of an argument. */
102 private String name = null;
103 /** The value of the arg, stored for metadata type options */
104 private String stored_value = 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 if (stored_value != null) {
267 return stored_value;
268 }
269 String value = XMLTools.getValue(element);
270 if (type == METADATA) {
271 // We display using metadata display name, but store in the XML using
272 // full name (canonical name)
273 stored_value = MetadataTools.convertMetadataElementListNames(value, MetadataTools.TO_DISPLAY_NAMES);
274 return stored_value;
275 }
276 return value;
277 }
278
279
280 /** Method to determine if this argument has been assigned.
281 * @return true if it has, false otherwise
282 * @see org.greenstone.gatherer.util.StaticStrings#ASSIGNED_ATTRIBUTE
283 * @see org.greenstone.gatherer.util.StaticStrings#TRUE_STR
284 */
285 public boolean isAssigned() {
286 return (element != null && element.getAttribute(StaticStrings.ASSIGNED_ATTRIBUTE).equals(StaticStrings.TRUE_STR));
287 }
288
289 /** 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.
290 * @return true if this argument is a custom, false otherwise
291 * @see org.greenstone.gatherer.util.StaticStrings#CUSTOM_ATTRIBUTE
292 * @see org.greenstone.gatherer.util.StaticStrings#TRUE_STR
293 */
294 public boolean isCustomArgument() {
295 return (element != null && element.getAttribute(StaticStrings.CUSTOM_ATTRIBUTE).equals(StaticStrings.TRUE_STR));
296 }
297
298 /** Determine if this argument is hidden in GLI
299 * @return true if the argument is hidden, false otherwise
300 */
301 public boolean isHiddenGLI() {
302 return hidden_gli;
303 }
304
305 /** Method to determine of this argument is required for the associated script to work.
306 * @return true if this argument is required, false otherwise
307 */
308 public boolean isRequired() {
309 return required;
310 }
311
312 /** Method to allow for the activation of arguments that might never have their setValue() method called.
313 * @param assigned the desired state as a boolean
314 * @see org.greenstone.gatherer.util.StaticStrings#ASSIGNED_ATTRIBUTE
315 * @see org.greenstone.gatherer.util.StaticStrings#FALSE_STR
316 * @see org.greenstone.gatherer.util.StaticStrings#TRUE_STR
317 */
318 public void setAssigned(boolean assigned) {
319 if(element != null) {
320 element.setAttribute(StaticStrings.ASSIGNED_ATTRIBUTE, (assigned ? StaticStrings.TRUE_STR : StaticStrings.FALSE_STR));
321 }
322 }
323
324 /** Sets the value of default_value.
325 * @param default_value The new value for default_value as a <strong>String</strong>.
326 */
327 public void setDefaultValue(String default_value) {
328 this.default_value = default_value;
329 }
330
331 /** Set the value of desc.
332 * @param description the new value of desc as a String
333 */
334 public void setDescription(String description) {
335 this.description = description;
336 }
337
338 /** Set the element this argument should be based upon.
339 * @param element the Element
340 */
341 public void setElement(Element element) {
342 this.element = element;
343 }
344
345 /** Mark this argument as being hidden in GLI. */
346 public void setHiddenGLI(boolean hidden) {
347 this.hidden_gli = hidden;
348 }
349
350 /** Set the upper bound for a range type argument.
351 * @param maximum the maximum as an int
352 */
353 public void setMaximum(int maximum) {
354 this.maximum = maximum;
355 }
356
357 /** Set the lower bound for a range type argument.
358 * @param minimum the minimum as an int
359 */
360 public void setMinimum(int minimum) {
361 this.minimum = minimum;
362 }
363
364 /** Set the detail mode level where this argument will become available.
365 * @param mode_level the mode level as an int
366 */
367 public void setModeLevel(int mode_level) {
368 this.mode_level = mode_level;
369 }
370
371 /** Set the value of name.
372 * @param name the new value of name as a String
373 */
374 public void setName(String name) {
375 this.name = name;
376 }
377
378 public void setDisplayName(String name) {
379 this.display_name = name;
380 }
381
382 /** Sets the value of the options list.
383 * @param list the new options list as a HashMap
384 */
385 public void setOptions(ArrayList list) {
386 this.option_list = list;
387 }
388
389 /** Set the owner of this argument.
390 * @param owner the name of the owner of this argument as a String
391 */
392 public void setOwner(String owner) {
393 this.owner = owner;
394 }
395
396 /** Set the value of required.
397 * @param required the new value of required as a boolean
398 */
399 public void setRequired(boolean required) {
400 this.required = required;
401 }
402
403 /** Set the value of type.
404 * @param type the new value of type as an byte
405 */
406 public void setType(byte type) {
407 this.type = type;
408 }
409
410 /** Set the value of type, by matching a type to the given string.
411 * @param new_type a String which contains the name of a certain argument type
412 * @see org.greenstone.gatherer.util.StaticStrings#ENUM_STR
413 * @see org.greenstone.gatherer.util.StaticStrings#FLAG_STR
414 * @see org.greenstone.gatherer.util.StaticStrings#HIERARCHY_STR
415 * @see org.greenstone.gatherer.util.StaticStrings#INT_STR
416 * @see org.greenstone.gatherer.util.StaticStrings#LANGUAGE_STR
417 * @see org.greenstone.gatherer.util.StaticStrings#METADATA_TYPE_STR
418 * @see org.greenstone.gatherer.util.StaticStrings#REGEXP_STR
419 */
420 public void setType(String new_type) {
421 if(new_type.equalsIgnoreCase(StaticStrings.ENUM_STR)) {
422 this.type = ENUM;
423 option_list = new ArrayList();
424 }
425 else if(new_type.equalsIgnoreCase(StaticStrings.ENUM_STRING_STR)) {
426 this.type = ENUM_STRING;
427 option_list = new ArrayList();
428 }
429 else if(new_type.equalsIgnoreCase(StaticStrings.FLAG_STR)) {
430 this.type = FLAG;
431 }
432 else if(new_type.equalsIgnoreCase(StaticStrings.HIERARCHY_STR)) {
433 this.type = HIERARCHY;
434 }
435 else if(new_type.equalsIgnoreCase(StaticStrings.INT_STR)) {
436 this.type = INTEGER;
437 }
438 else if(new_type.equalsIgnoreCase(StaticStrings.LANGUAGE_STR)) {
439 this.type = LANGUAGE;
440 }
441 else if(new_type.equalsIgnoreCase(StaticStrings.METADATA_TYPE_STR)) {
442 this.type = METADATA;
443 }
444 else if(new_type.equalsIgnoreCase(StaticStrings.REGEXP_STR)) {
445 this.type = REGEXP;
446 }
447 else {
448 this.type = STRING;
449 }
450 }
451
452 /** Method to set the value of this argument.
453 * @param value the new value for the argument
454 * @see org.greenstone.gatherer.Gatherer#println
455 */
456 public void setValue(String value) {
457 if(element != null) {
458 if (type == METADATA) {
459 value = MetadataTools.convertMetadataElementListNames(value, MetadataTools.FROM_DISPLAY_NAMES);
460 stored_value = value;
461 }
462 XMLTools.setValue(element, value);
463 }
464 else {
465 DebugStream.println("Argument.setValue(" + value + ") called on a base Argument.");
466 }
467 }
468
469 /** 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.
470 * @param values an ArrayList of values
471 * @see org.greenstone.gatherer.Gatherer#println
472 */
473 public void setValues(ArrayList values) {
474 if(element != null) {
475 StringBuffer value = new StringBuffer();
476 int value_length = values.size();
477 for(int i = 0; i < value_length; i++) {
478 value.append(values.get(i));
479 value.append(StaticStrings.COMMA_CHARACTER);
480 }
481 value.deleteCharAt(value.length() - 1); // Remove last ','
482 XMLTools.setValue(element, value.toString());
483 }
484 else {
485 DebugStream.println("Argument.setValues([" + values.size() + " items]) called on a base Argument.");
486 }
487 }
488
489 /** Method for translating the data of this class into a string.
490 * @return a String containing a fragment of the total arguments string
491 * @see org.greenstone.gatherer.Gatherer#c_man
492 * @see org.greenstone.gatherer.collection.CollectionManager#getCollection
493 * @see org.greenstone.gatherer.util.StaticStrings#COMMA_CHARACTER
494 * @see org.greenstone.gatherer.util.StaticStrings#NAME_ATTRIBUTE
495 * @see org.greenstone.gatherer.util.StaticStrings#SPACE_CHARACTER
496 * @see org.greenstone.gatherer.util.StaticStrings#SPEECH_CHARACTER
497 */
498 public String toString()
499 {
500 StringBuffer text = new StringBuffer("-");
501
502 if (element == null) {
503 return text.toString();
504 }
505
506 if (name == null) {
507 name = element.getAttribute(StaticStrings.NAME_ATTRIBUTE);
508 }
509 text.append(name);
510
511 // getValue returns display name for metadata args
512 String value = getValue();
513 if (value.length() == 0) {
514 return text.toString();
515 }
516
517 text.append(StaticStrings.SPACE_CHARACTER);
518
519 // If the value contains a space, add speech marks
520 // (Except for metadata elements, which won't have spaces when written out to collect.cfg)
521 if (value.indexOf(StaticStrings.SPACE_CHARACTER) != -1 && type != METADATA) {
522 value = StaticStrings.SPEECH_CHARACTER + value + StaticStrings.SPEECH_CHARACTER;
523 }
524
525 text.append(value);
526 return text.toString();
527 }
528
529 /** parse the <Option> XML from eg import.pl -xml or pluginfo.pl -xml */
530 public void parseXML(Element option) {
531
532 for(Node node = option.getFirstChild(); node != null; node = node.getNextSibling()) {
533 String node_name = node.getNodeName();
534 if(node_name.equals("Name")) {
535 setName(XMLTools.getValue(node));
536 }
537 else if(node_name.equals("DisplayName")) {
538 setDisplayName(XMLTools.getValue(node));
539 }
540 else if(node_name.equals("Desc")) {
541 setDescription(XMLTools.getValue(node));
542 }
543 else if(node_name.equals("Type")) {
544 setType(XMLTools.getValue(node));
545 }
546 else if(node_name.equals("Default")) {
547 setDefaultValue(XMLTools.getValue(node));
548 }
549 else if(node_name.equals("Required")) {
550 String v = XMLTools.getValue(node);
551 if(v != null && v.equals("yes")) {
552 setRequired(true);
553 }
554 }
555 else if(node_name.equals("List")) {
556 // Two final loops are required to parse lists.
557 for(Node value = node.getFirstChild(); value != null; value = value.getNextSibling()) {
558 if(value.getNodeName().equals("Value")) {
559 String key = null;
560 String desc = "";
561 for(Node subvalue = value.getFirstChild(); subvalue != null; subvalue = subvalue.getNextSibling()) {
562 node_name = subvalue.getNodeName();
563 if(node_name.equals("Name")) {
564 key = XMLTools.getValue(subvalue);
565 }
566 else if(node_name.equals("Desc")) {
567 desc = XMLTools.getValue(subvalue);
568 }
569 }
570 if(key != null) {
571 addOption(key, desc);
572 }
573 }
574 }
575 }
576 else if(node_name.equals("Range")) {
577 String range_raw = XMLTools.getValue(node);
578 int index = -1;
579 if((index = range_raw.indexOf(StaticStrings.COMMA_CHARACTER)) != -1) {
580 if(index > 0) {
581 try {
582 String first_number = range_raw.substring(0, index);
583 setMinimum(Integer.parseInt(first_number));
584 first_number = null;
585 }
586 catch(Exception exception) {
587 }
588 }
589
590 if(index + 1 < range_raw.length()) {
591 try {
592 String second_number = range_raw.substring(index + 1);
593 setMaximum(Integer.parseInt(second_number));
594 second_number = null;
595 }
596 catch(Exception exception) {
597 }
598 }
599 }
600 // Else it wasn't a valid range anyway, so ignore it
601 }
602 else if(node_name.equals("HiddenGLI")) {
603 setHiddenGLI(true);
604 }
605 else if(node_name.equals("ModeGLI")) {
606 String mode_level_str = XMLTools.getValue(node);
607 try {
608 int mode_level = Integer.parseInt(mode_level_str);
609 setModeLevel(mode_level);
610 }
611 catch(Exception exception) {
612 DebugStream.println("Exception in Argument.parseXML() - Unexpected but non-fatal");
613 DebugStream.printStackTrace(exception);
614 }
615 }
616
617 } // for each option
618
619 } // parseXML
620
621 public class ArgumentOption
622 implements Comparable {
623 public String name;
624 public String description;
625 private String text; // cached version
626
627 public ArgumentOption(String name, String desc) {
628 this.name = name;
629 this.description = desc;
630 }
631
632 public int compareTo(Object obj) {
633 return toString().compareTo(obj.toString());
634 }
635
636 public boolean equals(Object obj) {
637 return (obj != null && compareTo(obj) == 0);
638 }
639
640 public String toString() {
641 return name + " "+ StaticStrings.MINUS_CHARACTER + " "+ description;
642 }
643
644 public String getToolTip() {
645 return Utility.formatHTMLWidth(name+": "+description, 80);
646 }
647 }
648}
Note: See TracBrowser for help on using the repository browser.