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

Last change on this file since 4558 was 4558, checked in by kjdon, 21 years ago

have a new toStringConfig method that returns the config file format - metadata should not have the ex. namespace, also changed one part to use StringBuffer instead of string

  • Property svn:keywords set to Author Date Id Revision
File size: 14.7 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.MSMUtils;
57import org.greenstone.gatherer.util.Utility;
58/** 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.
59 * @author John Thompson, Greenstone Digital Library, University of Waikato
60 * @version 2.3
61 */
62
63// ####################################################################################
64// Optimization Saving
65// ####################################################################################
66// Vector -> ArrayList + Memory, + Processor (pos. - Processor)
67// Hashtable -> HashMap + Processor
68// int -> byte (x7) + Memory (168b)
69// External method calls + Memory, Processor
70// ####################################################################################
71
72public class Argument
73 implements Comparable, Serializable {
74 /** 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. */
75 private ArrayList values = null;
76 /** <i>true</i> if this argument is required for the applicable script to work properly, <i>false</i> otherwise. */
77 private boolean required = false;
78 /** The type of this argument. Initially an int, but bytes are cheaper. */
79 private byte type = FLAG;
80 /** 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. */
81 private HashMap list = null;
82 /** A default value for parameter-type arguments. May be a Perl pattern. */
83 private String default_value = null;
84 /** The text description of this argument parsed from the pluginfo output. */
85 private String desc = null;
86 /** The argument flag as it appears in the command. Also used as the unique identifier of an argument. */
87 private String name = null;
88 /** The plugin that owns this argument, for the purposes of visualising inheritance. */
89 private String owner = null;
90 /** If this argument has been assigned, what is its value. Must be non-null for the argument to be printed. */
91 private String value = null;
92 /** An element of the argument type enumeration. */
93 static final public byte ENUM = 0;
94 /** An element of the argument type enumeration. */
95 static final public byte FLAG = 1;
96 /** An element of the argument type enumeration. */
97 static final public byte HIERARCHY = 2;
98 /** An element of the argument type enumeration. */
99 static final public byte INTEGER = 3;
100 /** An element of the argument type enumeration. */
101 static final public byte LANGUAGE = 4;
102 /** An element of the argument type enumeration. */
103 static final public byte METADATA = 5;
104 /** An element of the argument type enumeration. */
105 static final public byte METADATUM = 6;
106 /** An element of the argument type enumeration. */
107 static final public byte STRING = 7;
108 /** Default Constructor. */
109 public Argument() {
110 }
111 /** Normal Constructor, based on data parsed from an information script.
112 * @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.
113 * @param desc The text description of this argument parsed from the output as a <strong>String</strong>.
114 * @param type The type of this argument as a <i>byte</i>.
115 * @param default_value The default value for a parameter type argument, which may include a Perl type regular expression, as a <strong>String</strong>.
116 */
117 public Argument(String name, String desc, byte type, String default_value) {
118 this.default_value = default_value;
119 this.desc = desc;
120 this.name = name;
121 this.type = type;
122 if(type == ENUM) {
123 this.list = new HashMap();
124 }
125 if(type == METADATUM) {
126 values = new ArrayList();
127 }
128 }
129 /** Method to add an element to the option list.
130 * @param name The name value of the option as a <strong>String</strong>.
131 * @param desc The description of this options as a <strong>String</strong>.
132 */
133 public void addOption(String name, String desc) {
134 if(type == ENUM && name != null) {
135 if(desc == null) {
136 desc = "";
137 }
138 if(!list.containsKey(name)) {
139 list.put(name, desc);
140 }
141 }
142 }
143 /** Method to compare two arguments for ordering.
144 * @param object The argument we are comparing to, as an <strong>Object</strong>.
145 * @return An <i>int</i> specifying the argument order, using values as set out in String.
146 * @see java.lang.String#compareTo
147 * @see org.greenstone.gatherer.cdm.Argument
148 */
149 public int compareTo(Object object) {
150 Argument argument = (Argument)object;
151 return getName().compareTo(argument.getName());
152 }
153 /** Method to deep copy this argument.
154 * @return A newly created <strong>Argument</strong> which has the same details as this one.
155 */
156 public Argument copy() {
157 Argument copy = new Argument(name, desc, type, default_value);
158 copy.setRequired(required);
159 if(values != null) {
160 copy.setValues(values);
161 }
162 if(list != null) {
163 HashMap list_deep_copy = new HashMap();
164 Iterator it = list.keySet().iterator();
165 while(it.hasNext()) {
166 String key = (String) it.next();
167 String desc = (String) list.get(key);
168 list_deep_copy.put(new String(key), new String(desc));
169 }
170 copy.setOptions(list_deep_copy);
171 }
172 return copy;
173 }
174 /** Method to determine if two arguments are equal.
175 * @param object The argument to test against, as an <strong>Object</strong>.
176 * @return <i>true</i> if the arguments names match, <i>false</i> otherwise.
177 */
178 public boolean equals(Object object) {
179 return (compareTo(object) == 0);
180 }
181 /** Method to retrieve the value of default_value.
182 * @return A <strong>String</strong> containing the default value.
183 */
184 public String getDefaultValue() {
185 return default_value;
186 }
187 /** Method to retrieve this arguments description.
188 * @return A <strong>String</strong> containing the description.
189 */
190 public String getDesc() {
191 return desc;
192 }
193 /** Method to retrieve the description of a certain list option value.
194 * @param key The <strong>String</strong> whose description we are searching for.
195 * @return The description of the desired key as a <strong>String</strong> which may be empty if no such key exists.
196 */
197 public String getDesc(String key) {
198 if(list.containsKey(key)) {
199 return (String)list.get(key);
200 }
201 return "";
202 }
203 /** Method to retrieve the option list for this argument.
204 * @return A <strong>HashMap</strong> containing &lt;option value&gt; -&gt; &lt;description&gt; entries.
205 */
206 public HashMap getList() {
207 return list;
208 }
209 /** Method to retrieve the value of name.
210 * @return A <strong>String</strong> containing the argument name.
211 */
212 public String getName() {
213 return name;
214 }
215 /** Retrieve the name of the owner of this argument.
216 * @return The owners name as a <strong>String</strong>.
217 */
218 public String getOwner() {
219 return owner;
220 }
221 /** Method to determine the type of this argument.
222 * @return An <i>byte</i> specifying the type.
223 */
224 public byte getType() {
225 return type;
226 }
227 /** Method to retrieve the value of value.
228 * @return The value of value as a <strong>String</strong>.
229 */
230 public String getValue() {
231 return value;
232 }
233 /** Retrieve the vector of values.
234 * @return An <strong>ArrayList</strong> of values.
235 */
236 public ArrayList getValues() {
237 return values;
238 }
239 /** Method to determine if this argument has been assigned.
240 * @return <i>true</i> if it has, <i>false</i> otherwise.
241 */
242 public boolean isAssigned() {
243 return (required || value != null || (values != null && values.size() > 0));
244 }
245 /** Method to determine of this argument is required for the associated script to work.
246 * @return <i>true</i> if this argument is required, <i>false</i> otherwise.
247 */
248 public boolean isRequired() {
249 return required;
250 }
251 /** Method to allow for the activation of arguments that might never have their setValue() method called.
252 * @param new_state The required state as a <i>boolean</i>.
253 */
254 public void setAssigned(boolean new_state) {
255 if(new_state && (value == null || values == null)) {
256 value = "";
257 }
258 else {
259 value = null;
260 }
261 }
262 /** Sets the value of default_value.
263 * @param new_default_value The new value for default_value as a <strong>String</strong>.
264 */
265 public void setDefault(String new_default_value) {
266 default_value = new_default_value;
267 }
268 /** Set the value of desc.
269 * @param new_desc The new value of desc as a <strong>String</strong>.
270 */
271 public void setDesc(String new_desc) {
272 desc = new_desc;
273 }
274 /** Set the value of name.
275 * @param new_name The new value of name as a <strong>String</strong>.
276 */
277 public void setName(String new_name) {
278 name = new_name;
279 }
280 /** Sets the value of the options list.
281 * @param new_list The new options list as a <strong>HashMap</strong>.
282 */
283 public void setOptions(HashMap new_list) {
284 list = new_list;
285 }
286 /** Set the owner of this argument.
287 * @param owner The name of the owner of this argument as a <strong>String</strong>.
288 */
289 public void setOwner(String owner) {
290 this.owner = owner;
291 }
292 /** Set the value of required.
293 * @param new_required The new value of required as a <i>boolean</i>.
294 */
295 public void setRequired(boolean new_required) {
296 required = new_required;
297 }
298 /** Set the value of type.
299 * @param new_type The new value of type as an <i>byte</i>.
300 */
301 public void setType(byte new_type) {
302 type = new_type;
303 if(type == ENUM) {
304 list = new HashMap();
305 }
306 if(type == METADATUM && values == null) {
307 values = new ArrayList();
308 }
309 }
310 /** Set the value of type, by matching a type to the given string.
311 * @param new_type A <strong>String</strong> which contains the name of a certain argument type.
312 */
313 public void setType(String new_type) {
314 if(new_type.equalsIgnoreCase("enum")) {
315 this.type = ENUM;
316 list = new HashMap();
317 }
318 else if(new_type.equalsIgnoreCase("flag")) {
319 this.type = FLAG;
320 }
321 else if(new_type.equalsIgnoreCase("hierarchy")) {
322 this.type = HIERARCHY;
323 }
324 else if(new_type.equalsIgnoreCase("int")) {
325 this.type = INTEGER;
326 }
327 else if(new_type.equalsIgnoreCase("language")) {
328 this.type = LANGUAGE;
329 }
330 else if(new_type.equalsIgnoreCase("metadata")) {
331 this.type = METADATA;
332 }
333 else if(new_type.equalsIgnoreCase("metadatum")) {
334 this.type = METADATUM;
335 if(values == null) {
336 values = new ArrayList();
337 }
338 }
339 else if(new_type.equalsIgnoreCase("string")) {
340 this.type = STRING;
341 }
342 }
343 /** Method to set the value of value.
344 * @param new_value The new value of value as a <strong>String</strong>.
345 */
346 public void setValue(String new_value) {
347 this.value = new_value;
348 }
349 /** 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.
350 * @param new_values An <strong>ArrayList</strong> of values.
351 */
352 public void setValues(ArrayList new_values) {
353 values = new_values;
354 }
355 /** Method for translating the data of this class into a string.
356 * @return A <strong>String</strong> containing a fragment of the total arguments string.
357 */
358 public String toString() {
359 switch(type) {
360 case FLAG:
361 return "-" + name;
362 case METADATUM:
363 StringBuffer text = new StringBuffer("-");
364 text.append(name);
365 text.append(" ");
366 for(int i = 0; i < values.size(); i++) {
367 text.append(values.get(i).toString());
368 if(i < values.size() - 1) {
369 text.append(",");
370 }
371 }
372 return text.toString();
373 case STRING:
374 // we need to put quotes if they are not there
375 if (value.indexOf(' ') != -1 && value.indexOf('"')==-1) {
376 // if there is a space and we haven't got any quotes
377 return "-" + name + " \"" + value + "\"";
378 } // otherwise it will stuff up but I cant be bothered doing this properly
379 default:
380 return "-" + name + " " + value;
381 }
382 }
383
384 public String toStringConfig() {
385 switch(type) {
386 case METADATA:
387 if (value != null) {
388 String temp_value = value;
389
390 if (temp_value.startsWith(Utility.EXTRACTED_METADATA_NAMESPACE+MSMUtils.NS_SEP)) {
391
392 temp_value = temp_value.substring(temp_value.indexOf(MSMUtils.NS_SEP)+1);
393 }
394 return "-" + name + " " + temp_value;
395 }
396 return "-" + name;
397 case METADATUM:
398 StringBuffer text = new StringBuffer("-");
399 text.append(name);
400 text.append(" ");
401 for(int i = 0; i < values.size(); i++) {
402 String temp_value = values.get(i).toString();
403 if (temp_value.startsWith(Utility.EXTRACTED_METADATA_NAMESPACE+MSMUtils.NS_SEP)) {
404
405 temp_value = temp_value.substring(temp_value.indexOf(MSMUtils.NS_SEP)+1);
406 }
407 text.append(temp_value);
408 if(i < values.size() - 1) {
409 text.append(",");
410 }
411 }
412 return text.toString();
413 default:
414 return toString();
415 }
416 }
417
418}
419
420
Note: See TracBrowser for help on using the repository browser.