1 | /**
|
---|
2 | *#########################################################################
|
---|
3 | *
|
---|
4 | * A component of the Gatherer application, part of the Greenstone digital
|
---|
5 | * library suite from the New Zealand Digital Library Project at the
|
---|
6 | * University of Waikato, New Zealand.
|
---|
7 | *
|
---|
8 | * Author: John Thompson, Greenstone Digital Library, University of Waikato
|
---|
9 | *
|
---|
10 | * Copyright (C) 1999 New Zealand Digital Library Project
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or modify
|
---|
13 | * it under the terms of the GNU General Public License as published by
|
---|
14 | * the Free Software Foundation; either version 2 of the License, or
|
---|
15 | * (at your option) any later version.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful,
|
---|
18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
20 | * GNU General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, write to the Free Software
|
---|
24 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
25 | *########################################################################
|
---|
26 | */
|
---|
27 | package org.greenstone.gatherer.cdm;
|
---|
28 |
|
---|
29 | import java.io.*;
|
---|
30 | import java.util.*;
|
---|
31 | import org.greenstone.gatherer.Gatherer;
|
---|
32 | import org.greenstone.gatherer.cdm.CollectionConfiguration;
|
---|
33 | import org.greenstone.gatherer.collection.Collection;
|
---|
34 | import org.greenstone.gatherer.collection.CollectionManager;
|
---|
35 | import org.greenstone.gatherer.msm.ElementWrapper;
|
---|
36 | import org.greenstone.gatherer.msm.MetadataSetManager;
|
---|
37 | import org.greenstone.gatherer.msm.MSMUtils;
|
---|
38 | import org.greenstone.gatherer.util.StaticStrings;
|
---|
39 | import org.greenstone.gatherer.util.Utility;
|
---|
40 | import org.w3c.dom.*;
|
---|
41 |
|
---|
42 | /** 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.
|
---|
43 | * @author John Thompson, Greenstone Digital Library, University of Waikato
|
---|
44 | * @version 2.3
|
---|
45 | */
|
---|
46 | public class Argument
|
---|
47 | implements Comparable, Serializable {
|
---|
48 | /** An element of the argument type enumeration. */
|
---|
49 | static final public byte ENUM = 0;
|
---|
50 | /** An element of the argument type enumeration. */
|
---|
51 | static final public byte FLAG = 1;
|
---|
52 | /** An element of the argument type enumeration. */
|
---|
53 | static final public byte HIERARCHY = 2;
|
---|
54 | /** An element of the argument type enumeration. */
|
---|
55 | static final public byte INTEGER = 3;
|
---|
56 | /** An element of the argument type enumeration. */
|
---|
57 | static final public byte LANGUAGE = 4;
|
---|
58 | /** An element of the argument type enumeration. */
|
---|
59 | static final public byte METADATA = 5;
|
---|
60 | /** An element of the argument type enumeration. */
|
---|
61 | static final public byte METADATUM = 6;
|
---|
62 | /** An element of the argument type enumeration. */
|
---|
63 | static final public byte STRING = 7;
|
---|
64 | /** <i>true</i> if this argument is required for the applicable script to work properly, <i>false</i> otherwise. */
|
---|
65 | private boolean required = false;
|
---|
66 | /** The type of this argument. Initially an int, but bytes are cheaper. */
|
---|
67 | private byte type = STRING;
|
---|
68 | private Element element;
|
---|
69 | /** If the argument is of type ENUM then this map holds all the various options. Each entry is an <option value> -> <description> mapping. */
|
---|
70 | private HashMap list = null;
|
---|
71 | /** A default value for parameter-type arguments. May be a Perl pattern. */
|
---|
72 | private String default_value = null;
|
---|
73 | /** The text description of this argument parsed from the pluginfo output. */
|
---|
74 | private String description = null;
|
---|
75 | /** The argument flag as it appears in the command. Also used as the unique identifier of an argument. */
|
---|
76 | private String name = null;
|
---|
77 | /** The plugin that owns this argument, for the purposes of visualising inheritance. */
|
---|
78 | private String owner = null;
|
---|
79 |
|
---|
80 | /** Default Constructor. */
|
---|
81 | public Argument() {
|
---|
82 | }
|
---|
83 |
|
---|
84 | public Argument(Element element) {
|
---|
85 | this.element = element;
|
---|
86 | }
|
---|
87 |
|
---|
88 | /** Method to add an element to the option list.
|
---|
89 | * @param name The name value of the option as a <strong>String</strong>.
|
---|
90 | * @param desc The description of this options as a <strong>String</strong>.
|
---|
91 | */
|
---|
92 | public void addOption(String name, String desc) {
|
---|
93 | if(type == ENUM && name != null) {
|
---|
94 | if(desc == null) {
|
---|
95 | desc = "";
|
---|
96 | }
|
---|
97 | if(list == null) {
|
---|
98 | list = new HashMap();
|
---|
99 | }
|
---|
100 | list.put(name, desc);
|
---|
101 | }
|
---|
102 | }
|
---|
103 |
|
---|
104 | /** Method to compare two arguments for ordering.
|
---|
105 | * @param object The argument we are comparing to, as an <strong>Object</strong>.
|
---|
106 | * @return An <i>int</i> specifying the argument order, using values as set out in String.
|
---|
107 | * @see java.lang.String#compareTo
|
---|
108 | * @see org.greenstone.gatherer.cdm.Argument
|
---|
109 | */
|
---|
110 | public int compareTo(Object object) {
|
---|
111 | if(object instanceof Argument) {
|
---|
112 | return getName().compareTo(((Argument)object).getName());
|
---|
113 | }
|
---|
114 | else {
|
---|
115 | return toString().compareTo(object.toString());
|
---|
116 | }
|
---|
117 | }
|
---|
118 |
|
---|
119 | public Argument copy() {
|
---|
120 | Argument copy = new Argument();
|
---|
121 | copy.setDefaultValue(default_value);
|
---|
122 | copy.setDescription(description);
|
---|
123 | copy.setOptions(list);
|
---|
124 | copy.setOwner(owner);
|
---|
125 | copy.setName(name);
|
---|
126 | copy.setRequired(required);
|
---|
127 | copy.setType(type);
|
---|
128 | return copy;
|
---|
129 | }
|
---|
130 |
|
---|
131 | /** Method to determine if two arguments are equal.
|
---|
132 | * @param object The argument to test against, as an <strong>Object</strong>.
|
---|
133 | * @return <i>true</i> if the arguments names match, <i>false</i> otherwise.
|
---|
134 | */
|
---|
135 | public boolean equals(Object object) {
|
---|
136 | return (compareTo(object) == 0);
|
---|
137 | }
|
---|
138 |
|
---|
139 | /** Method to retrieve the value of default_value.
|
---|
140 | * @return A <strong>String</strong> containing the default value.
|
---|
141 | */
|
---|
142 | public String getDefaultValue() {
|
---|
143 | return default_value;
|
---|
144 | }
|
---|
145 |
|
---|
146 | /** Method to retrieve this arguments description.
|
---|
147 | * @return A <strong>String</strong> containing the description.
|
---|
148 | */
|
---|
149 | public String getDescription() {
|
---|
150 | return description;
|
---|
151 | }
|
---|
152 |
|
---|
153 | /** Method to retrieve the description of a certain list option value.
|
---|
154 | * @param key The <strong>String</strong> whose description we are searching for.
|
---|
155 | * @return The description of the desired key as a <strong>String</strong> which may be empty if no such key exists.
|
---|
156 | */
|
---|
157 | public String getDescription(String key) {
|
---|
158 | if(list.containsKey(key)) {
|
---|
159 | return (String)list.get(key);
|
---|
160 | }
|
---|
161 | return "";
|
---|
162 | }
|
---|
163 |
|
---|
164 | /** Method to retrieve the option list for this argument.
|
---|
165 | * @return A <strong>HashMap</strong> containing <option value> -> <description> entries.
|
---|
166 | */
|
---|
167 | public HashMap getOptions() {
|
---|
168 | return list;
|
---|
169 | }
|
---|
170 |
|
---|
171 | /** Method to retrieve the value of name.
|
---|
172 | * @return A <strong>String</strong> containing the argument name.
|
---|
173 | */
|
---|
174 | public String getName() {
|
---|
175 | if(name == null && element != null) {
|
---|
176 | name = element.getAttribute(StaticStrings.NAME_ATTRIBUTE);
|
---|
177 | }
|
---|
178 | return name;
|
---|
179 | }
|
---|
180 |
|
---|
181 | /** Retrieve the name of the owner of this argument.
|
---|
182 | * @return The owners name as a <strong>String</strong>.
|
---|
183 | */
|
---|
184 | public String getOwner() {
|
---|
185 | return owner;
|
---|
186 | }
|
---|
187 |
|
---|
188 | /** Method to determine the type of this argument.
|
---|
189 | * @return An <i>byte</i> specifying the type.
|
---|
190 | */
|
---|
191 | public byte getType() {
|
---|
192 | return type;
|
---|
193 | }
|
---|
194 |
|
---|
195 | /** Method to retrieve the value of value.
|
---|
196 | * @return The value of value as a <strong>String</strong>.
|
---|
197 | */
|
---|
198 | public String getValue() {
|
---|
199 | String value = null;
|
---|
200 | // Only assigned arguments have values.
|
---|
201 | if(element != null) {
|
---|
202 | value = MSMUtils.getValue(element);
|
---|
203 | // We may have to retrieve the language dependant string for a MSM Element
|
---|
204 | if(type == METADATUM) {
|
---|
205 | ElementWrapper element_wrapper = Gatherer.c_man.getCollection().msm.getElement(value);
|
---|
206 | if(element_wrapper != null) {
|
---|
207 | value = element_wrapper.toString();
|
---|
208 | }
|
---|
209 | }
|
---|
210 | }
|
---|
211 | return value;
|
---|
212 | }
|
---|
213 |
|
---|
214 | /** Retrieve the vector of values.
|
---|
215 | * @return An <strong>ArrayList</strong> of values.
|
---|
216 | */
|
---|
217 | public ArrayList getValues() {
|
---|
218 | ArrayList values = new ArrayList();
|
---|
219 | // Only assigned arguments have values.
|
---|
220 | if(element != null) {
|
---|
221 | String value = MSMUtils.getValue(element);
|
---|
222 | StringTokenizer tokenizer = new StringTokenizer(value, ",");
|
---|
223 | while(tokenizer.hasMoreTokens()) {
|
---|
224 | String token = tokenizer.nextToken();
|
---|
225 | if(type == METADATA) {
|
---|
226 | ElementWrapper element_wrapper = Gatherer.c_man.getCollection().msm.getElement(token);
|
---|
227 | if(element_wrapper != null) {
|
---|
228 | token = element_wrapper.toString();
|
---|
229 | }
|
---|
230 | }
|
---|
231 | values.add(token);
|
---|
232 | }
|
---|
233 | }
|
---|
234 | return values;
|
---|
235 | }
|
---|
236 |
|
---|
237 | /** Method to determine if this argument has been assigned.
|
---|
238 | * @return <i>true</i> if it has, <i>false</i> otherwise.
|
---|
239 | */
|
---|
240 | public boolean isAssigned() {
|
---|
241 | return (element != null && element.getAttribute(StaticStrings.ASSIGNED_ATTRIBUTE).equals(StaticStrings.TRUE_STR));
|
---|
242 | }
|
---|
243 |
|
---|
244 | public boolean isCustomArgument() {
|
---|
245 | return (element != null && element.getAttribute(StaticStrings.CUSTOM_ATTRIBUTE).equals(StaticStrings.TRUE_STR));
|
---|
246 | }
|
---|
247 |
|
---|
248 | /** Method to determine of this argument is required for the associated script to work.
|
---|
249 | * @return <i>true</i> if this argument is required, <i>false</i> otherwise.
|
---|
250 | */
|
---|
251 | public boolean isRequired() {
|
---|
252 | return required;
|
---|
253 | }
|
---|
254 |
|
---|
255 | /** Method to allow for the activation of arguments that might never have their setValue() method called.
|
---|
256 | * @param assigned the desired state as a boolean
|
---|
257 | */
|
---|
258 | public void setAssigned(boolean assigned) {
|
---|
259 | if(element != null) {
|
---|
260 | element.setAttribute(StaticStrings.ASSIGNED_ATTRIBUTE, (assigned ? StaticStrings.TRUE_STR : StaticStrings.FALSE_STR));
|
---|
261 | }
|
---|
262 | }
|
---|
263 |
|
---|
264 | public void setCustomArgument(boolean custom) {
|
---|
265 | if(element != null) {
|
---|
266 | element.setAttribute(StaticStrings.CUSTOM_ATTRIBUTE, (custom ? StaticStrings.TRUE_STR : StaticStrings.FALSE_STR));
|
---|
267 | }
|
---|
268 | }
|
---|
269 |
|
---|
270 | /** Sets the value of default_value.
|
---|
271 | * @param default_value The new value for default_value as a <strong>String</strong>.
|
---|
272 | */
|
---|
273 | public void setDefaultValue(String default_value) {
|
---|
274 | this.default_value = default_value;
|
---|
275 | }
|
---|
276 |
|
---|
277 | /** Set the value of desc.
|
---|
278 | * @param description The new value of desc as a <strong>String</strong>.
|
---|
279 | */
|
---|
280 | public void setDescription(String description) {
|
---|
281 | this.description = description;
|
---|
282 | }
|
---|
283 |
|
---|
284 | public void setElement(Element element) {
|
---|
285 | this.element = element;
|
---|
286 | }
|
---|
287 |
|
---|
288 | /** Set the value of name.
|
---|
289 | * @param name The new value of name as a <strong>String</strong>.
|
---|
290 | */
|
---|
291 | public void setName(String name) {
|
---|
292 | this.name = name;
|
---|
293 | }
|
---|
294 |
|
---|
295 | /** Sets the value of the options list.
|
---|
296 | * @param list The new options list as a <strong>HashMap</strong>.
|
---|
297 | */
|
---|
298 | public void setOptions(HashMap list) {
|
---|
299 | this.list = list;
|
---|
300 | }
|
---|
301 |
|
---|
302 | /** Set the owner of this argument.
|
---|
303 | * @param owner The name of the owner of this argument as a <strong>String</strong>.
|
---|
304 | */
|
---|
305 | public void setOwner(String owner) {
|
---|
306 | this.owner = owner;
|
---|
307 | }
|
---|
308 |
|
---|
309 | /** Set the value of required.
|
---|
310 | * @param required The new value of required as a <i>boolean</i>.
|
---|
311 | */
|
---|
312 | public void setRequired(boolean required) {
|
---|
313 | this.required = required;
|
---|
314 | }
|
---|
315 |
|
---|
316 | /** Set the value of type.
|
---|
317 | * @param type The new value of type as an <i>byte</i>.
|
---|
318 | */
|
---|
319 | public void setType(byte type) {
|
---|
320 | this.type = type;
|
---|
321 | }
|
---|
322 |
|
---|
323 | /** Set the value of type, by matching a type to the given string.
|
---|
324 | * @param new_type A <strong>String</strong> which contains the name of a certain argument type.
|
---|
325 | */
|
---|
326 | public void setType(String new_type) {
|
---|
327 | if(new_type.equalsIgnoreCase(StaticStrings.ENUM_STR)) {
|
---|
328 | this.type = ENUM;
|
---|
329 | list = new HashMap();
|
---|
330 | }
|
---|
331 | else if(new_type.equalsIgnoreCase(StaticStrings.FLAG_STR)) {
|
---|
332 | this.type = FLAG;
|
---|
333 | }
|
---|
334 | else if(new_type.equalsIgnoreCase(StaticStrings.HIERARCHY_STR)) {
|
---|
335 | this.type = HIERARCHY;
|
---|
336 | }
|
---|
337 | else if(new_type.equalsIgnoreCase(StaticStrings.INT_STR)) {
|
---|
338 | this.type = INTEGER;
|
---|
339 | }
|
---|
340 | else if(new_type.equalsIgnoreCase(StaticStrings.LANGUAGE_STR)) {
|
---|
341 | this.type = LANGUAGE;
|
---|
342 | }
|
---|
343 | else if(new_type.equalsIgnoreCase(StaticStrings.METADATA_TYPE_STR)) {
|
---|
344 | this.type = METADATA;
|
---|
345 | }
|
---|
346 | else if(new_type.equalsIgnoreCase(StaticStrings.METADATUM_TYPE_STR)) {
|
---|
347 | this.type = METADATUM;
|
---|
348 | }
|
---|
349 | else {
|
---|
350 | this.type = STRING;
|
---|
351 | }
|
---|
352 |
|
---|
353 | }
|
---|
354 | /** Method to set the value of this argument.
|
---|
355 | * @param value
|
---|
356 | * @see org.greenstone.gatherer.Gatherer
|
---|
357 | * @see org.greenstone.gatherer.msm.MSMUtils
|
---|
358 | */
|
---|
359 | public void setValue(String value) {
|
---|
360 | if(element != null) {
|
---|
361 | MSMUtils.setValue(element, value);
|
---|
362 | }
|
---|
363 | else {
|
---|
364 | Gatherer.println("Argument.setValue(" + value + ") called on a base Argument.");
|
---|
365 | }
|
---|
366 | }
|
---|
367 |
|
---|
368 | /** 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.
|
---|
369 | * @param values an ArrayList of values
|
---|
370 | * @see org.greenstone.gatherer.Gatherer
|
---|
371 | * @see org.greenstone.gatherer.msm.MSMUtils
|
---|
372 | */
|
---|
373 | public void setValues(ArrayList values) {
|
---|
374 | if(element != null) {
|
---|
375 | StringBuffer value = new StringBuffer();
|
---|
376 | int value_length = values.size();
|
---|
377 | for(int i = 0; i < value_length; i++) {
|
---|
378 | value.append(values.get(i));
|
---|
379 | value.append(StaticStrings.COMMA_CHARACTER);
|
---|
380 | }
|
---|
381 | value.deleteCharAt(value.length() - 1); // Remove last ','
|
---|
382 | MSMUtils.setValue(element, value.toString());
|
---|
383 | }
|
---|
384 | else {
|
---|
385 | Gatherer.println("Argument.setValues([" + values.size() + " items]) called on a base Argument.");
|
---|
386 | }
|
---|
387 | }
|
---|
388 |
|
---|
389 | /** Method for translating the data of this class into a string.
|
---|
390 | * @return a String containing a fragment of the total arguments string
|
---|
391 | * @see org.greenstone.gatherer.Gatherer
|
---|
392 | * @see org.greenstone.gatherer.collection.Collection
|
---|
393 | * @see org.greenstone.gatherer.collection.CollectionManager
|
---|
394 | * @see org.greenstone.gatherer.msm.MetadataSetManager
|
---|
395 | * @see org.greenstone.gatherer.msm.MSMUtils
|
---|
396 | * @see org.greenstone.gatherer.util.StaticStrings
|
---|
397 | */
|
---|
398 | public String toString() {
|
---|
399 | StringBuffer text = new StringBuffer("-");
|
---|
400 | if(element != null) {
|
---|
401 | if(name == null) {
|
---|
402 | name = element.getAttribute(StaticStrings.NAME_ATTRIBUTE);
|
---|
403 | }
|
---|
404 | text.append(name);
|
---|
405 | String value = MSMUtils.getValue(element);
|
---|
406 | if(value.length() > 0) {
|
---|
407 | text.append(StaticStrings.SPACE_CHARACTER);
|
---|
408 | // Tokenize the string
|
---|
409 | StringTokenizer tokenizer = new StringTokenizer(value, ",");
|
---|
410 | while(tokenizer.hasMoreTokens()) {
|
---|
411 | String token = tokenizer.nextToken();
|
---|
412 | if(type == METADATA || type == METADATUM) {
|
---|
413 | ElementWrapper element_wrapper = Gatherer.c_man.getCollection().msm.getElement(token);
|
---|
414 | if(element_wrapper != null) {
|
---|
415 | text.append(element_wrapper.toString());
|
---|
416 | element_wrapper = null;
|
---|
417 | }
|
---|
418 | else {
|
---|
419 | text.append(token);
|
---|
420 | }
|
---|
421 | }
|
---|
422 | else {
|
---|
423 | text.append(token);
|
---|
424 | }
|
---|
425 | token = null;
|
---|
426 | text.append(StaticStrings.COMMA_CHARACTER);
|
---|
427 | }
|
---|
428 | tokenizer = null;
|
---|
429 | text.deleteCharAt(text.length() - 1);
|
---|
430 | }
|
---|
431 | value = null;
|
---|
432 | }
|
---|
433 | return text.toString();
|
---|
434 | }
|
---|
435 | }
|
---|