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

Last change on this file since 12110 was 11344, checked in by mdewsnip, 18 years ago

Added an empty metadata set for putting exploded metadata into, and added the "-metadata_set" option into the Explode Metadata Database prompt.

  • Property svn:keywords set to Author Date Id Revision
File size: 21.3 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 /** Retrieve the upper bound of a range based argument.
194 * @return the maximum as an int
195 */
196 public int getMaximum() {
197 return maximum;
198 }
199
200 /** Retrieve the lower bound of a range based argument.
201 * @return the minimum as an int
202 */
203 public int getMinimum() {
204 return minimum;
205 }
206
207 /** Retrieves the mode level at which this argument should become available. Any higher levels should also see this argument.
208 * @return the mode level as an int
209 */
210 public int getModeLevel() {
211 return mode_level;
212 }
213
214 /** Method to retrieve the value of name.
215 * @return a String containing the argument name
216 * @see org.greenstone.gatherer.util.StaticStrings#NAME_ATTRIBUTE
217 */
218 public String getName() {
219 if(name == null && element != null) {
220 name = element.getAttribute(StaticStrings.NAME_ATTRIBUTE);
221 }
222 return name;
223 }
224
225 /** Method to retrieve the option list for this argument.
226 * @return a HashMap containing &lt;option value&gt; -&gt; &lt;description&gt; entries
227 */
228 public HashMap getOptions() {
229 return list;
230 }
231
232 /** Retrieve the name of the owner of this argument.
233 * @return the owners name as a String
234 */
235 public String getOwner() {
236 return owner;
237 }
238
239 /** Method to determine the type of this argument.
240 * @return a byte specifying the type
241 */
242 public byte getType() {
243 return type;
244 }
245
246 /** Method to retrieve the value of value.
247 * @return the value of value as a String
248 * @see org.greenstone.gatherer.Gatherer#c_man
249 * @see org.greenstone.gatherer.collection.CollectionManager#getCollection
250 */
251 public String getValue()
252 {
253 // Only assigned arguments have values.
254 if (element == null) {
255 return null;
256 }
257
258 return XMLTools.getValue(element);
259 }
260
261
262 /** Method to determine if this argument has been assigned.
263 * @return true if it has, false otherwise
264 * @see org.greenstone.gatherer.util.StaticStrings#ASSIGNED_ATTRIBUTE
265 * @see org.greenstone.gatherer.util.StaticStrings#TRUE_STR
266 */
267 public boolean isAssigned() {
268 return (element != null && element.getAttribute(StaticStrings.ASSIGNED_ATTRIBUTE).equals(StaticStrings.TRUE_STR));
269 }
270
271 /** 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.
272 * @return true if this argument is a custom, false otherwise
273 * @see org.greenstone.gatherer.util.StaticStrings#CUSTOM_ATTRIBUTE
274 * @see org.greenstone.gatherer.util.StaticStrings#TRUE_STR
275 */
276 public boolean isCustomArgument() {
277 return (element != null && element.getAttribute(StaticStrings.CUSTOM_ATTRIBUTE).equals(StaticStrings.TRUE_STR));
278 }
279
280 /** Determine if this argument is hidden in GLI
281 * @return true if the argument is hidden, false otherwise
282 */
283 public boolean isHiddenGLI() {
284 return hidden_gli;
285 }
286
287 /** Method to determine of this argument is required for the associated script to work.
288 * @return true if this argument is required, false otherwise
289 */
290 public boolean isRequired() {
291 return required;
292 }
293
294 /** Method to allow for the activation of arguments that might never have their setValue() method called.
295 * @param assigned the desired state as a boolean
296 * @see org.greenstone.gatherer.util.StaticStrings#ASSIGNED_ATTRIBUTE
297 * @see org.greenstone.gatherer.util.StaticStrings#FALSE_STR
298 * @see org.greenstone.gatherer.util.StaticStrings#TRUE_STR
299 */
300 public void setAssigned(boolean assigned) {
301 if(element != null) {
302 element.setAttribute(StaticStrings.ASSIGNED_ATTRIBUTE, (assigned ? StaticStrings.TRUE_STR : StaticStrings.FALSE_STR));
303 }
304 }
305
306 /** Set the custom argument flag.
307 * @param custom true to make this argument custom, false otherwise
308 * @see org.greenstone.gatherer.util.StaticStrings#CUSTOM_ATTRIBUTE
309 * @see org.greenstone.gatherer.util.StaticStrings#FALSE_STR
310 * @see org.greenstone.gatherer.util.StaticStrings#TRUE_STR
311 */
312 public void setCustomArgument(boolean custom) {
313 if(element != null) {
314 element.setAttribute(StaticStrings.CUSTOM_ATTRIBUTE, (custom ? StaticStrings.TRUE_STR : StaticStrings.FALSE_STR));
315 }
316 }
317
318 /** Sets the value of default_value.
319 * @param default_value The new value for default_value as a <strong>String</strong>.
320 */
321 public void setDefaultValue(String default_value) {
322 this.default_value = default_value;
323 }
324
325 /** Set the value of desc.
326 * @param description the new value of desc as a String
327 */
328 public void setDescription(String description) {
329 this.description = description;
330 }
331
332 /** Set the element this argument should be based upon.
333 * @param element the Element
334 */
335 public void setElement(Element element) {
336 this.element = element;
337 }
338
339 /** Mark this argument as being hidden in GLI. */
340 public void setHiddenGLI(boolean hidden) {
341 this.hidden_gli = hidden;
342 }
343
344 /** Set the upper bound for a range type argument.
345 * @param maximum the maximum as an int
346 */
347 public void setMaximum(int maximum) {
348 this.maximum = maximum;
349 }
350
351 /** Set the lower bound for a range type argument.
352 * @param minimum the minimum as an int
353 */
354 public void setMinimum(int minimum) {
355 this.minimum = minimum;
356 }
357
358 /** Set the detail mode level where this argument will become available.
359 * @param mode_level the mode level as an int
360 */
361 public void setModeLevel(int mode_level) {
362 this.mode_level = mode_level;
363 }
364
365 /** Set the value of name.
366 * @param name the new value of name as a String
367 */
368 public void setName(String name) {
369 this.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(HashMap list) {
376 this.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 list = new HashMap();
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("Desc")) {
545 setDescription(XMLTools.getValue(node));
546 }
547 else if(node_name.equals("Type")) {
548 setType(XMLTools.getValue(node));
549 }
550 else if(node_name.equals("Default")) {
551 setDefaultValue(XMLTools.getValue(node));
552 }
553 else if(node_name.equals("Required")) {
554 String v = XMLTools.getValue(node);
555 if(v != null && v.equals("yes")) {
556 setRequired(true);
557 }
558 }
559 else if(node_name.equals("List")) {
560 // Two final loops are required to parse lists.
561 for(Node value = node.getFirstChild(); value != null; value = value.getNextSibling()) {
562 if(value.getNodeName().equals("Value")) {
563 String key = null;
564 String desc = "";
565 for(Node subvalue = value.getFirstChild(); subvalue != null; subvalue = subvalue.getNextSibling()) {
566 node_name = subvalue.getNodeName();
567 if(node_name.equals("Name")) {
568 key = XMLTools.getValue(subvalue);
569 }
570 else if(node_name.equals("Desc")) {
571 desc = XMLTools.getValue(subvalue);
572 }
573 }
574 if(key != null) {
575 addOption(key, desc);
576 }
577 }
578 }
579 }
580 else if(node_name.equals("Range")) {
581 String range_raw = XMLTools.getValue(node);
582 int index = -1;
583 if((index = range_raw.indexOf(StaticStrings.COMMA_CHARACTER)) != -1) {
584 if(index > 0) {
585 try {
586 String first_number = range_raw.substring(0, index);
587 setMinimum(Integer.parseInt(first_number));
588 first_number = null;
589 }
590 catch(Exception exception) {
591 }
592 }
593
594 if(index + 1 < range_raw.length()) {
595 try {
596 String second_number = range_raw.substring(index + 1);
597 setMaximum(Integer.parseInt(second_number));
598 second_number = null;
599 }
600 catch(Exception exception) {
601 }
602 }
603 }
604 // Else it wasn't a valid range anyway, so ignore it
605 }
606 else if(node_name.equals("HiddenGLI")) {
607 setHiddenGLI(true);
608 }
609 else if(node_name.equals("ModeGLI")) {
610 String mode_level_str = XMLTools.getValue(node);
611 try {
612 int mode_level = Integer.parseInt(mode_level_str);
613 setModeLevel(mode_level);
614 }
615 catch(Exception exception) {
616 DebugStream.println("Exception in Argument.parseXML() - Unexpected but non-fatal");
617 DebugStream.printStackTrace(exception);
618 }
619 }
620
621 } // for each option
622
623 } // parseXML
624}
Note: See TracBrowser for help on using the repository browser.