KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > gulden > framework > amoda > environment > commandline > CommandLineArgsParser


1 /*
2  * Project: AMODA - Abstract Modeled Application
3  * Class: de.gulden.framework.amoda.environment.commandline.CommandLineArgsParser
4  * Version: snapshot-beautyj-1.1
5  *
6  * Date: 2004-09-29
7  *
8  * This is a snapshot version of the AMODA 0.2 development branch,
9  * it is not released as a seperate version.
10  * For AMODA, see http://amoda.berlios.de/.
11  *
12  * This is licensed under the GNU Lesser General Public License (LGPL)
13  * and comes with NO WARRANTY.
14  *
15  * Author: Jens Gulden
16  * Email: amoda@jensgulden.de
17  */

18
19 package de.gulden.framework.amoda.environment.commandline;
20
21 import de.gulden.framework.amoda.generic.core.AbstractArgsParser;
22 import de.gulden.framework.amoda.generic.data.GenericValue;
23 import de.gulden.framework.amoda.generic.option.*;
24 import de.gulden.framework.amoda.model.data.*;
25 import de.gulden.framework.amoda.model.data.Value;
26 import java.lang.*;
27 import java.util.*;
28
29 /**
30  * Class CommandLineArgsParser.
31  *
32  * @author Jens Gulden
33  * @version snapshot-beautyj-1.1
34  */

35 public class CommandLineArgsParser extends AbstractArgsParser {
36
37     // ------------------------------------------------------------------------
38
// --- fields ---
39
// ------------------------------------------------------------------------
40

41     protected String JavaDoc[] args;
42
43     protected int index;
44
45
46     // ------------------------------------------------------------------------
47
// --- constructor ---
48
// ------------------------------------------------------------------------
49

50     public CommandLineArgsParser(String JavaDoc[] args) {
51         this.args=args;
52         index=0;
53     }
54
55
56     // ------------------------------------------------------------------------
57
// --- methods ---
58
// ------------------------------------------------------------------------
59

60     public void parseOptions(GenericOptions options) {
61         // format: -option1[=][value] -option2[=][value] ...
62
if (args.length > 0) {
63             int index = 0;
64             String JavaDoc a=args[0];
65             while (a != null) {
66                 if ((a.startsWith("-"))&&(a.length()>=2)) {
67                     boolean useShortcut = (a.charAt(1)!='-'); // no shortcut if second '-'
68
a=a.substring(useShortcut ? 1 : 2); // remove leading '-' or '--'
69
index++;
70                     int eqPos=a.indexOf('=');
71                     String JavaDoc possibleValue;
72                     if (eqPos==-1) { // not separated by '=', so value _maybe_ follows in next arg (boolean values may have no following value but are implicitly set to true if occurring)
73
if (index<args.length) {
74                             possibleValue=args[index];
75                         } else {
76                             possibleValue=null;
77                         }
78                     } else {
79                         possibleValue=a.substring(eqPos+1); // (is already known value, not only 'possible')
80
a=a.substring(0,eqPos);
81                     }
82                     if (getSingleArgParser().parseIndividualOption(a,useShortcut,possibleValue,options) && (eqPos==-1)) { //... increase index only value followed and if value wasn't given after '='
83
index++; // parseOptionInternal returns true if possibleValue has been consumed
84
}
85                     this.index = index; // candidate for input values, if no more options follow
86
} else { // not an option: ignore
87
index++;
88                 }
89                 if (index < args.length) {
90                     a=args[index];
91                 } else {
92                     a=null;
93                 }
94             }
95         }
96     }
97
98     public Collection parseBatchCommands(Collection availableCommands) {
99         // start with first arg, either full-name or shortcut feature name
100
// (when shortcuts used, multiple ones per arg are allowed)
101
// stop parsing when first unknown
102
Collection c = new ArrayList();
103         Map byId = new HashMap();
104         Map byShortcut = new HashMap();
105
106         for (Iterator it = availableCommands.iterator(); it.hasNext(); ) {
107             de.gulden.framework.amoda.generic.core.GenericFeature f = (de.gulden.framework.amoda.generic.core.GenericFeature)it.next();
108             byId.put(f.getId(), f);
109             String JavaDoc sc = f.getShortcut();
110             if (sc != null) {
111                 byShortcut.put(sc, f);
112             }
113         }
114
115         // parse
116
while (this.index < this.args.length) {
117             String JavaDoc a = this.args[this.index];
118             de.gulden.framework.amoda.generic.core.GenericFeature f = (de.gulden.framework.amoda.generic.core.GenericFeature)byId.get(a);
119             if (f != null) { // given by full id
120
c.add(f);
121             } else { // shortcuts?
122
// each entry may consist of multiple letters - accept only if all are valid commands
123
int charindex=0;
124                 Collection tempC=new ArrayList();
125                 while (charindex<a.length()) {
126                     String JavaDoc id=a.substring(charindex,charindex+1);
127                     f = (de.gulden.framework.amoda.generic.core.GenericFeature)byShortcut.get(id);
128                     if (f==null) { // not a feature
129
return c; // break here, return result so far, index stays on current
130
} else { // single feature found
131
tempC.add(f);
132                     }
133                     charindex++;
134                 }
135                 // now it is sure that all commands are valid, so copy them
136
c.addAll(tempC);
137             }
138             this.index++;
139         }
140         if (c.isEmpty()) {
141             Object JavaDoc defaultCommand = byId.get("default");
142             if (defaultCommand!=null) {
143                 c.add(defaultCommand);
144             }
145         }
146         return c;
147     }
148
149     public Value[] parseInputValues() {
150         // parseOptions and parseBatchCommands must have been called before
151
de.gulden.framework.amoda.model.data.Value[] values=new de.gulden.framework.amoda.model.data.Value[args.length-this.index];
152         for (int i=0;i<values.length;i++) {
153             values[i] = new de.gulden.framework.amoda.generic.data.GenericValue(args[this.index+i]);
154         }
155         return values;
156     }
157
158     public boolean parseIndividualOption(String JavaDoc name, boolean useShortcut, String JavaDoc suggestedValue, GenericOptions options) {
159         // useShortcut is ignored, both names (id or shortcut) are possible
160
boolean result;
161         de.gulden.framework.amoda.model.option.OptionEntry o;
162         try {
163             o = options.getOptionEntry(name); // IllegalOptionError if unknown option
164
} catch (de.gulden.framework.amoda.model.option.IllegalOptionError ioe) {
165             // try shortcut
166
Object JavaDoc oo = options.getByShortcut(name,true);
167             if ((oo==null)||(! (oo instanceof de.gulden.framework.amoda.model.option.OptionEntry))) {
168                 if (options.getApplication().getOptions().getBoolean("error-on-unknown-option")) {
169                     options.getApplication().error("unknown option '"+ioe.getMessage()+"'");
170                 }
171                 return false;
172             } else {
173                 o = (de.gulden.framework.amoda.model.option.OptionEntry)oo;
174             }
175         }
176         Class JavaDoc type=o.getType();
177         String JavaDoc v;
178         if (!Boolean JavaDoc.class.isAssignableFrom(type)) { // normal case: non-boolean (value always follows)
179
if (suggestedValue==null) {
180                 throw new de.gulden.framework.amoda.model.option.IllegalOptionError("value for option "+name+" must be specified");
181             } else {
182                 v=suggestedValue;
183                 result=true; // use up suggestedValue
184
}
185         } else {
186             // boolean, maybe value follows (if no value follows, assume TRUE)
187
if (suggestedValue!=null) {
188                 if ((GenericValue.isBooleanLiteral(true,suggestedValue)||GenericValue.isBooleanLiteral(false,suggestedValue))) {
189                     v=suggestedValue;
190                     result=true;
191                 } else {
192                     v="true";
193                     result=false;
194                 }
195             } else {
196                 v="true";
197                 result=false;
198             }
199         }
200         ((GenericValue)o.getValue(GenericOptions.STATE_CURRENT)).parseString(v);
201         return result;
202     }
203
204 } // end CommandLineArgsParser
205
Popular Tags