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

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

re-tabbed the code for java

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