KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sablecc > sablecc > launcher > Arguments


1 /* This file is part of SableCC ( http://sablecc.org ).
2  *
3  * Copyright 2007 Etienne M. Gagnon <egagnon@j-meg.com>
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.sablecc.sablecc.launcher;
19
20 import java.io.PushbackReader JavaDoc;
21 import java.io.StringReader JavaDoc;
22 import java.util.Collections JavaDoc;
23 import java.util.LinkedList JavaDoc;
24 import java.util.List JavaDoc;
25
26 import org.sablecc.sablecc.exception.InvalidArgumentException;
27 import org.sablecc.sablecc.launcher.syntax3.lexer.Lexer;
28 import org.sablecc.sablecc.launcher.syntax3.node.Start;
29 import org.sablecc.sablecc.launcher.syntax3.parser.Parser;
30
31 public class Arguments {
32
33     private final List JavaDoc<OptionArgument> optionArguments;
34
35     private final List JavaDoc<TextArgument> textArguments;
36
37     public Arguments(
38             String JavaDoc[] args)
39             throws InvalidArgumentException {
40
41         List JavaDoc<OptionArgument> optionArguments = new LinkedList JavaDoc<OptionArgument>();
42
43         List JavaDoc<TextArgument> textArguments = new LinkedList JavaDoc<TextArgument>();
44
45         int currentArgIndex = 0;
46
47         // process options, until a text argument is found
48
while (currentArgIndex < args.length && textArguments.size() == 0) {
49
50             try {
51                 Start ast = new Parser(new Lexer(new PushbackReader JavaDoc(
52                         new StringReader JavaDoc(args[currentArgIndex]), 1024)))
53                         .parse();
54
55                 Option incompleteOption = ArgumentExtractor.extractArguments(
56                         ast, optionArguments, textArguments);
57
58                 if (incompleteOption != null) {
59
60                     if (currentArgIndex + 1 >= args.length) {
61
62                         if (args[currentArgIndex].startsWith("--")) {
63                             throw new InvalidArgumentException("option --"
64                                     + incompleteOption.getLongName()
65                                     + " is missing a "
66                                     + incompleteOption.getOperandName()
67                                     + " operand");
68                         }
69                         else {
70                             throw new InvalidArgumentException("option -"
71                                     + incompleteOption.getShortName()
72                                     + " is missing a "
73                                     + incompleteOption.getOperandName()
74                                     + " operand");
75                         }
76                     }
77
78                     optionArguments.add(new OptionArgument(incompleteOption,
79                             args[++currentArgIndex]));
80                 }
81             }
82             catch (InvalidArgumentException e) {
83                 throw new InvalidArgumentException("invalid argument \""
84                         + args[currentArgIndex] + "\": " + e.getMessage(), e);
85             }
86             catch (Exception JavaDoc e) {
87                 throw new InvalidArgumentException("invalid argument \""
88                         + args[currentArgIndex] + "\"", e);
89             }
90
91             currentArgIndex++;
92         }
93
94         // process remaining text arguments
95
while (currentArgIndex < args.length) {
96
97             textArguments.add(new TextArgument(args[currentArgIndex]));
98
99             currentArgIndex++;
100         }
101
102         this.optionArguments = Collections.unmodifiableList(optionArguments);
103         this.textArguments = Collections.unmodifiableList(textArguments);
104     }
105
106     public List JavaDoc<OptionArgument> getOptionArguments() {
107
108         return this.optionArguments;
109     }
110
111     public List JavaDoc<TextArgument> getTextArguments() {
112
113         return this.textArguments;
114     }
115
116 }
117
Popular Tags