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

Last change on this file since 4838 was 4838, checked in by jmt12, 21 years ago

2030159: Had confused element name and identifier. Also added ability for ElementWrappers to be stored as Argument values, and thus have identifier appear within GLI while the name is written to the collect.cfg

  • Property svn:keywords set to Author Date Id Revision
File size: 15.9 KB
Line 
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 * <BR><BR>
9 *
10 * Author: John Thompson, Greenstone Digital Library, University of Waikato
11 *
12 * <BR><BR>
13 *
14 * Copyright (C) 1999 New Zealand Digital Library Project
15 *
16 * <BR><BR>
17 *
18 * This program is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation; either version 2 of the License, or
21 * (at your option) any later version.
22 *
23 * <BR><BR>
24 *
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
29 *
30 * <BR><BR>
31 *
32 * You should have received a copy of the GNU General Public License
33 * along with this program; if not, write to the Free Software
34 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
35 *########################################################################
36 */
37
38
39
40
41
42
43/* GPL_HEADER */
44package org.greenstone.gatherer.cdm;
45/**************************************************************************************
46 * Title: Gatherer
47 * Description: The Gatherer: a tool for gathering and enriching a digital collection.
48 * Company: The University of Waikato
49 * Written: 01/05/02
50 * Revised: 16/08/02 Optimized and Commented.
51 **************************************************************************************/
52import java.io.Serializable;
53import java.util.ArrayList;
54import java.util.Iterator;
55import java.util.HashMap;
56import org.greenstone.gatherer.msm.ElementWrapper;
57import org.greenstone.gatherer.msm.MSMUtils;
58import org.greenstone.gatherer.util.Utility;
59/** 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.
60 * @author John Thompson, Greenstone Digital Library, University of Waikato
61 * @version 2.3
62 */
63
64// ####################################################################################
65// Optimization Saving
66// ####################################################################################
67// Vector -> ArrayList + Memory, + Processor (pos. - Processor)
68// Hashtable -> HashMap + Processor
69// int -> byte (x7) + Memory (168b)
70// External method calls + Memory, Processor
71// ####################################################################################
72
73public class Argument
74 implements Comparable, Serializable {
75 /** If this argument has multiple values, then they are stored here. This was originally a Hashtable, but since the synchronization overhead is unwarrented it was changed to a HashMap. */
76 private ArrayList values = null;
77 /** <i>true</i> if this argument is required for the applicable script to work properly, <i>false</i> otherwise. */
78 private boolean required = false;
79 /** The type of this argument. Initially an int, but bytes are cheaper. */
80 private byte type = FLAG;
81
82 private ElementWrapper element = null;
83 /** 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. */
84 private HashMap list = null;
85 /** A default value for parameter-type arguments. May be a Perl pattern. */
86 private String default_value = null;
87 /** The text description of this argument parsed from the pluginfo output. */
88 private String desc = null;
89 /** The argument flag as it appears in the command. Also used as the unique identifier of an argument. */
90 private String name = null;
91 /** The plugin that owns this argument, for the purposes of visualising inheritance. */
92 private String owner = null;
93 /** If this argument has been assigned, what is its value. Must be non-null for the argument to be printed. */
94 private String value = null;
95 /** An element of the argument type enumeration. */
96 static final public byte ENUM = 0;
97 /** An element of the argument type enumeration. */
98 static final public byte FLAG = 1;
99 /** An element of the argument type enumeration. */
100 static final public byte HIERARCHY = 2;
101 /** An element of the argument type enumeration. */
102 static final public byte INTEGER = 3;
103 /** An element of the argument type enumeration. */
104 static final public byte LANGUAGE = 4;
105 /** An element of the argument type enumeration. */
106 static final public byte METADATA = 5;
107 /** An element of the argument type enumeration. */
108 static final public byte METADATUM = 6;
109 /** An element of the argument type enumeration. */
110 static final public byte STRING = 7;
111 /** Default Constructor. */
112 public Argument() {
113 }
114 /** Normal Constructor, based on data parsed from an information script.
115 * @param name The argument flag as it appears in the command, as a <strong>String</strong>. Also used as the unique identifier of an argument.
116 * @param desc The text description of this argument parsed from the output as a <strong>String</strong>.
117 * @param type The type of this argument as a <i>byte</i>.
118 * @param default_value The default value for a parameter type argument, which may include a Perl type regular expression, as a <strong>String</strong>.
119 */
120 public Argument(String name, String desc, byte type, String default_value) {
121 this.default_value = default_value;
122 this.desc = desc;
123 this.name = name;
124 this.type = type;
125 if(type == ENUM) {
126 this.list = new HashMap();
127 }
128 if(type == METADATUM) {
129 values = new ArrayList();
130 }
131 }
132 /** Method to add an element to the option list.
133 * @param name The name value of the option as a <strong>String</strong>.
134 * @param desc The description of this options as a <strong>String</strong>.
135 */
136 public void addOption(String name, String desc) {
137 if(type == ENUM && name != null) {
138 if(desc == null) {
139 desc = "";
140 }
141 if(!list.containsKey(name)) {
142 list.put(name, desc);
143 }
144 }
145 }
146 /** Method to compare two arguments for ordering.
147 * @param object The argument we are comparing to, as an <strong>Object</strong>.
148 * @return An <i>int</i> specifying the argument order, using values as set out in String.
149 * @see java.lang.String#compareTo
150 * @see org.greenstone.gatherer.cdm.Argument
151 */
152 public int compareTo(Object object) {
153 Argument argument = (Argument)object;
154 return getName().compareTo(argument.getName());
155 }
156 /** Method to deep copy this argument.
157 * @return A newly created <strong>Argument</strong> which has the same details as this one.
158 */
159 public Argument copy() {
160 Argument copy = new Argument(name, desc, type, default_value);
161 copy.setRequired(required);
162 if(values != null) {
163 copy.setValues(values);
164 }
165 if(list != null) {
166 HashMap list_deep_copy = new HashMap();
167 Iterator it = list.keySet().iterator();
168 while(it.hasNext()) {
169 String key = (String) it.next();
170 String desc = (String) list.get(key);
171 list_deep_copy.put(new String(key), new String(desc));
172 }
173 copy.setOptions(list_deep_copy);
174 }
175 return copy;
176 }
177 /** Method to determine if two arguments are equal.
178 * @param object The argument to test against, as an <strong>Object</strong>.
179 * @return <i>true</i> if the arguments names match, <i>false</i> otherwise.
180 */
181 public boolean equals(Object object) {
182 return (compareTo(object) == 0);
183 }
184 /** Method to retrieve the value of default_value.
185 * @return A <strong>String</strong> containing the default value.
186 */
187 public String getDefaultValue() {
188 return default_value;
189 }
190 /** Method to retrieve this arguments description.
191 * @return A <strong>String</strong> containing the description.
192 */
193 public String getDesc() {
194 return desc;
195 }
196 /** Method to retrieve the description of a certain list option value.
197 * @param key The <strong>String</strong> whose description we are searching for.
198 * @return The description of the desired key as a <strong>String</strong> which may be empty if no such key exists.
199 */
200 public String getDesc(String key) {
201 if(list.containsKey(key)) {
202 return (String)list.get(key);
203 }
204 return "";
205 }
206 /** Method to retrieve the option list for this argument.
207 * @return A <strong>HashMap</strong> containing &lt;option value&gt; -&gt; &lt;description&gt; entries.
208 */
209 public HashMap getList() {
210 return list;
211 }
212 /** Method to retrieve the value of name.
213 * @return A <strong>String</strong> containing the argument name.
214 */
215 public String getName() {
216 return name;
217 }
218 /** Retrieve the name of the owner of this argument.
219 * @return The owners name as a <strong>String</strong>.
220 */
221 public String getOwner() {
222 return owner;
223 }
224 /** Method to determine the type of this argument.
225 * @return An <i>byte</i> specifying the type.
226 */
227 public byte getType() {
228 return type;
229 }
230 /** Method to retrieve the value of value.
231 * @return The value of value as a <strong>String</strong>.
232 */
233 public String getValue() {
234 if(element != null) {
235 return element.toString();
236 }
237 return value;
238 }
239 /** Retrieve the vector of values.
240 * @return An <strong>ArrayList</strong> of values.
241 */
242 public ArrayList getValues() {
243 return values;
244 }
245 /** Method to determine if this argument has been assigned.
246 * @return <i>true</i> if it has, <i>false</i> otherwise.
247 */
248 public boolean isAssigned() {
249 return (required || value != null || (values != null && values.size() > 0));
250 }
251 /** Method to determine of this argument is required for the associated script to work.
252 * @return <i>true</i> if this argument is required, <i>false</i> otherwise.
253 */
254 public boolean isRequired() {
255 return required;
256 }
257 /** Method to allow for the activation of arguments that might never have their setValue() method called.
258 * @param new_state The required state as a <i>boolean</i>.
259 */
260 public void setAssigned(boolean new_state) {
261 if(new_state && (value == null || values == null)) {
262 value = "";
263 }
264 else {
265 value = null;
266 }
267 }
268 /** Sets the value of default_value.
269 * @param new_default_value The new value for default_value as a <strong>String</strong>.
270 */
271 public void setDefault(String new_default_value) {
272 default_value = new_default_value;
273 }
274 /** Set the value of desc.
275 * @param new_desc The new value of desc as a <strong>String</strong>.
276 */
277 public void setDesc(String new_desc) {
278 desc = new_desc;
279 }
280
281 public void setElementValue(ElementWrapper element) {
282 this.element = element;
283 }
284
285 /** Set the value of name.
286 * @param new_name The new value of name as a <strong>String</strong>.
287 */
288 public void setName(String new_name) {
289 name = new_name;
290 }
291 /** Sets the value of the options list.
292 * @param new_list The new options list as a <strong>HashMap</strong>.
293 */
294 public void setOptions(HashMap new_list) {
295 list = new_list;
296 }
297 /** Set the owner of this argument.
298 * @param owner The name of the owner of this argument as a <strong>String</strong>.
299 */
300 public void setOwner(String owner) {
301 this.owner = owner;
302 }
303 /** Set the value of required.
304 * @param new_required The new value of required as a <i>boolean</i>.
305 */
306 public void setRequired(boolean new_required) {
307 required = new_required;
308 }
309 /** Set the value of type.
310 * @param new_type The new value of type as an <i>byte</i>.
311 */
312 public void setType(byte new_type) {
313 type = new_type;
314 if(type == ENUM) {
315 list = new HashMap();
316 }
317 if(type == METADATUM && values == null) {
318 values = new ArrayList();
319 }
320 }
321 /** Set the value of type, by matching a type to the given string.
322 * @param new_type A <strong>String</strong> which contains the name of a certain argument type.
323 */
324 public void setType(String new_type) {
325 if(new_type.equalsIgnoreCase("enum")) {
326 this.type = ENUM;
327 list = new HashMap();
328 }
329 else if(new_type.equalsIgnoreCase("flag")) {
330 this.type = FLAG;
331 }
332 else if(new_type.equalsIgnoreCase("hierarchy")) {
333 this.type = HIERARCHY;
334 }
335 else if(new_type.equalsIgnoreCase("int")) {
336 this.type = INTEGER;
337 }
338 else if(new_type.equalsIgnoreCase("language")) {
339 this.type = LANGUAGE;
340 }
341 else if(new_type.equalsIgnoreCase("metadata")) {
342 this.type = METADATA;
343 }
344 else if(new_type.equalsIgnoreCase("metadatum")) {
345 this.type = METADATUM;
346 if(values == null) {
347 values = new ArrayList();
348 }
349 }
350 else if(new_type.equalsIgnoreCase("string")) {
351 this.type = STRING;
352 }
353 }
354
355 /** Method to set the value of value.
356 * @param new_value The new value of value as a <strong>String</strong>.
357 */
358 public void setValue(String new_value) {
359 this.value = new_value;
360 }
361 /** 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.
362 * @param new_values An <strong>ArrayList</strong> of values.
363 */
364 public void setValues(ArrayList new_values) {
365 values = new_values;
366 }
367 /** Method for translating the data of this class into a string.
368 * @return A <strong>String</strong> containing a fragment of the total arguments string.
369 */
370 public String toString() {
371 switch(type) {
372 case FLAG:
373 return "-" + name;
374 case METADATA:
375 StringBuffer metadata_text = new StringBuffer("-");
376 metadata_text.append(name);
377 metadata_text.append(" ");
378 if(element != null) {
379 String element_name = element.toString();
380 if(element_name.indexOf(" ") != -1) {
381 metadata_text.append("\"");
382 metadata_text.append(element_name);
383 metadata_text.append("\"");
384 }
385 else {
386 metadata_text.append(element_name);
387 }
388 element_name = null;
389 }
390 else {
391 metadata_text.append(value);
392 }
393 return metadata_text.toString();
394 case METADATUM:
395 StringBuffer metadatum_text = new StringBuffer("-");
396 metadatum_text.append(name);
397 metadatum_text.append(" ");
398 for(int i = 0; i < values.size(); i++) {
399 metadatum_text.append(values.get(i).toString());
400 if(i < values.size() - 1) {
401 metadatum_text.append(",");
402 }
403 }
404 return metadatum_text.toString();
405 case STRING:
406 // we need to put quotes if they are not there
407 if (value != null && value.indexOf(' ') != -1 && value.indexOf('"')==-1) {
408 // if there is a space and we haven't got any quotes
409 return "-" + name + " \"" + value + "\"";
410 } // otherwise it will stuff up but I cant be bothered doing this properly
411 default:
412 return "-" + name + " " + value;
413 }
414 }
415
416 public String toStringConfig() {
417 switch(type) {
418 case METADATA:
419 StringBuffer metadata_text = new StringBuffer("-");
420 metadata_text.append(name);
421 if(element != null) {
422 metadata_text.append(" ");
423 String element_name = element.getName();
424 if (element_name.startsWith(Utility.EXTRACTED_METADATA_NAMESPACE+MSMUtils.NS_SEP)) {
425
426 element_name = element_name.substring(element_name.indexOf(MSMUtils.NS_SEP)+1);
427 }
428 metadata_text.append(element_name);
429 element_name = null;
430 }
431 else if(value != null) {
432 metadata_text.append(" ");
433 metadata_text.append(value);
434 }
435 return metadata_text.toString();
436 case METADATUM:
437 StringBuffer metadatum_text = new StringBuffer("-");
438 metadatum_text.append(name);
439 metadatum_text.append(" ");
440 for(int i = 0; i < values.size(); i++) {
441 String temp_value = values.get(i).toString();
442 if (temp_value.startsWith(Utility.EXTRACTED_METADATA_NAMESPACE+MSMUtils.NS_SEP)) {
443
444 temp_value = temp_value.substring(temp_value.indexOf(MSMUtils.NS_SEP)+1);
445 }
446 metadatum_text.append(temp_value);
447 if(i < values.size() - 1) {
448 metadatum_text.append(",");
449 }
450 }
451 return metadatum_text.toString();
452 default:
453 return toString();
454 }
455 }
456
457}
458
459
Note: See TracBrowser for help on using the repository browser.