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

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

added a getElement method, and removed the Custom methods - can no longer have custom args

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