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

Last change on this file since 12468 was 12466, checked in by shaoqun, 18 years ago

added code for download pane

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