KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > incava > jagol > ListOption


1 package org.incava.jagol;
2
3 import java.io.*;
4 import java.util.*;
5 import org.incava.lang.StringExt;
6
7
8 /**
9  * Represents a list of objects that comprise this option.
10  */

11 public class ListOption extends Option
12 {
13     private List value;
14     
15     /**
16      * Creates the option.
17      */

18     public ListOption(String JavaDoc longName, String JavaDoc description)
19     {
20         this(longName, description, new ArrayList());
21     }
22
23     /**
24      * Creates the option, with a default list.
25      */

26     public ListOption(String JavaDoc longName, String JavaDoc description, List value)
27     {
28         super(longName, description);
29         this.value = value;
30     }
31
32     /**
33      * Returns the value. This is empty by default.
34      */

35     public List getValue()
36     {
37         return value;
38     }
39
40     /**
41      * Sets the value.
42      */

43     public void setValue(List value)
44     {
45         this.value = value;
46     }
47
48     /**
49      * Sets the value from the string, for a list type. Assumes whitespace or
50      * comma delimiter
51      */

52     public void setValue(String JavaDoc value) throws InvalidTypeException
53     {
54         tr.Ace.log("value: '" + value + "'");
55         parse(value);
56     }
57
58     /**
59      * Sets from a list of command-line arguments. Returns whether this option
60      * could be set from the current head of the list. Assumes whitespace or
61      * comma delimiter.
62      */

63     public boolean set(String JavaDoc arg, List args) throws OptionException
64     {
65         tr.Ace.log("arg: " + arg + "; args: " + args);
66      
67         if (arg.equals("--" + longName)) {
68             tr.Ace.log("matched long name");
69
70             if (args.size() == 0) {
71                 throw new InvalidTypeException(longName + " expects following argument");
72             }
73             else {
74                 String JavaDoc value = (String JavaDoc)args.remove(0);
75                 setValue(value);
76             }
77         }
78         else if (arg.startsWith("--" + longName + "=")) {
79             tr.Ace.log("matched long name + equals");
80
81             // args.remove(0);
82
int pos = ("--" + longName + "=").length();
83             tr.Ace.log("position: " + pos);
84             if (pos >= arg.length()) {
85                 throw new InvalidTypeException(longName + " expects argument");
86             }
87             else {
88                 String JavaDoc value = arg.substring(pos);
89                 setValue(value);
90             }
91         }
92         else if (shortName != 0 && arg.equals("-" + shortName)) {
93             tr.Ace.log("matched short name");
94
95             if (args.size() == 0) {
96                 throw new InvalidTypeException(shortName + " expects following argument");
97             }
98             else {
99                 String JavaDoc value = (String JavaDoc)args.remove(0);
100                 setValue(value);
101             }
102         }
103         else {
104             tr.Ace.log("not a match");
105             return false;
106         }
107         return true;
108     }
109
110     /**
111      * Parses the value into the value list. If subclasses want to convert the
112      * string to their own data type, override the <code>convert</code> method.
113      *
114      * @see ListOption#convert(String)
115      */

116     protected void parse(String JavaDoc str) throws InvalidTypeException
117     {
118         List list = StringExt.listify(str);
119         Iterator it = list.iterator();
120         while (it.hasNext()) {
121             String JavaDoc s = (String JavaDoc)it.next();
122             if (!s.equals("+=")) {
123                 value.add(convert(s));
124             }
125         }
126     }
127
128     /**
129      * Returns the string, possibly converted to a different Object type.
130      * Subclasses can convert the string to their own data type.
131      */

132     protected Object JavaDoc convert(String JavaDoc str) throws InvalidTypeException
133     {
134         return str;
135     }
136
137     public String JavaDoc toString()
138     {
139         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
140         Iterator it = value.iterator();
141         boolean isFirst = true;
142         while (it.hasNext()) {
143             if (isFirst) {
144                 isFirst = false;
145             }
146             else {
147                 buf.append(", ");
148             }
149             buf.append(it.next());
150         }
151         return buf.toString();
152     }
153
154 }
155
Popular Tags