KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.List JavaDoc;
21
22 import org.sablecc.sablecc.exception.InternalException;
23 import org.sablecc.sablecc.exception.InvalidArgumentException;
24 import org.sablecc.sablecc.exception.InvalidArgumentRuntimeException;
25 import org.sablecc.sablecc.launcher.syntax3.analysis.DepthFirstAdapter;
26 import org.sablecc.sablecc.launcher.syntax3.node.ALongOption;
27 import org.sablecc.sablecc.launcher.syntax3.node.AOperand;
28 import org.sablecc.sablecc.launcher.syntax3.node.APlainArgument;
29 import org.sablecc.sablecc.launcher.syntax3.node.AShortOption;
30 import org.sablecc.sablecc.launcher.syntax3.node.Start;
31
32 class ArgumentExtractor
33         extends DepthFirstAdapter {
34
35     private final List JavaDoc<OptionArgument> optionArguments;
36
37     private final List JavaDoc<TextArgument> textArguments;
38
39     private Option incompleteOption;
40
41     private ArgumentExtractor(
42             final List JavaDoc<OptionArgument> optionArguments,
43             final List JavaDoc<TextArgument> textArguments) {
44
45         if (optionArguments == null) {
46             throw new InternalException("optionArguments may not be null");
47         }
48
49         if (textArguments == null) {
50             throw new InternalException("textArguments may not be null");
51         }
52
53         this.optionArguments = optionArguments;
54         this.textArguments = textArguments;
55     }
56
57     @Override JavaDoc
58     public void caseAPlainArgument(
59             APlainArgument node) {
60
61         this.textArguments.add(new TextArgument(node.getText().getText()));
62     }
63
64     @Override JavaDoc
65     public void caseALongOption(
66             ALongOption node)
67             throws InvalidArgumentRuntimeException {
68
69         String JavaDoc longName = node.getLongName().getText();
70
71         // make sure option is valid
72
Option option = Option.getLongOption(longName);
73
74         if (option == null) {
75             throw new InvalidArgumentRuntimeException("invalid option: --"
76                     + longName);
77         }
78
79         // expects an operand?
80
if (option.hasOperand()) {
81             // yes
82

83             AOperand operand = (AOperand) node.getOperand();
84
85             // is it there?
86
if (operand != null) {
87                 // yes
88

89                 if (operand.getOperandText() != null) {
90                     this.optionArguments.add(new OptionArgument(option, operand
91                             .getOperandText().getText()));
92                 }
93                 else {
94                     this.optionArguments.add(new OptionArgument(option, ""));
95                 }
96             }
97             else {
98                 // no, we have an incomplete option
99
this.incompleteOption = option;
100             }
101         }
102         else {
103             // no
104

105             if (node.getOperand() != null) {
106                 throw new InvalidArgumentRuntimeException("option --"
107                         + longName + " does not expect an operand");
108             }
109
110             this.optionArguments.add(new OptionArgument(option, null));
111         }
112     }
113
114     @Override JavaDoc
115     public void caseAShortOption(
116             AShortOption node) {
117
118         if (this.incompleteOption != null) {
119             throw new InvalidArgumentRuntimeException("option -"
120                     + this.incompleteOption.getShortName() + " is missing a "
121                     + this.incompleteOption.getOperandName() + " operand");
122         }
123
124         String JavaDoc shortName = node.getShortName().getText();
125
126         // make sure option is valid
127
Option option = Option.getShortOption(shortName);
128
129         if (option == null) {
130             throw new InvalidArgumentRuntimeException("invalid option: -"
131                     + shortName);
132         }
133
134         // expects an operand?
135
if (option.hasOperand()) {
136             // yes
137

138             AOperand operand = (AOperand) node.getOperand();
139
140             // is it there?
141
if (operand != null) {
142                 // yes
143

144                 if (operand.getOperandText() != null) {
145                     this.optionArguments.add(new OptionArgument(option, operand
146                             .getOperandText().getText()));
147                 }
148                 else {
149                     this.optionArguments.add(new OptionArgument(option, ""));
150                 }
151             }
152             else {
153                 // no, we have an incomplete option
154
this.incompleteOption = option;
155             }
156         }
157         else {
158             // no
159

160             if (node.getOperand() != null) {
161                 throw new InvalidArgumentRuntimeException("option -"
162                         + shortName + " does not expect an operand");
163             }
164
165             this.optionArguments.add(new OptionArgument(option, null));
166         }
167
168     }
169
170     public static Option extractArguments(
171             Start ast,
172             List JavaDoc<OptionArgument> optionArguments,
173             List JavaDoc<TextArgument> textArguments)
174             throws InvalidArgumentException {
175
176         ArgumentExtractor extractor = new ArgumentExtractor(optionArguments,
177                 textArguments);
178
179         try {
180             ast.apply(extractor);
181         }
182         catch (InvalidArgumentRuntimeException e) {
183             throw new InvalidArgumentException(e.getMessage(), e);
184         }
185
186         return extractor.incompleteOption;
187     }
188 }
189
Popular Tags