2 * option argument information
4 * License : The MIT License
5 * Copyright(c) 2009 olyutorskii
8 package jp.sfjp.jindolf.config;
10 import java.text.MessageFormat;
11 import java.util.Collections;
12 import java.util.EnumMap;
13 import java.util.Iterator;
14 import java.util.LinkedList;
15 import java.util.List;
17 import java.util.regex.Matcher;
18 import java.util.regex.Pattern;
23 * <p>public static void main()の引数から展開される。
25 public class OptionInfo{
27 private static final String REGEX_DIMNO =
29 private static final String REGEX_SIGN =
31 private static final String REGEX_LOCNO =
32 REGEX_SIGN + REGEX_DIMNO;
33 private static final String REGEX_GEOMETRY =
34 REGEX_DIMNO + "x" + REGEX_DIMNO
35 + "(?:" + REGEX_LOCNO + REGEX_LOCNO + ")?";
36 private static final Pattern PATTERN_GEOMETRY =
37 Pattern.compile(REGEX_GEOMETRY);
39 private static final String ERRFORM_UKNOWN =
40 "未定義の起動オプション[{0}]が指定されました。";
41 private static final String ERRFORM_NOARG =
42 "起動オプション[{0}]に引数がありません。";
43 private static final String ERRFORM_GEOM =
44 "起動オプション[{0}]のジオメトリ指定[{1}]が不正です。"
45 + "WIDTHxHEIGHT[(+|-)XPOS(+|-)YPOS]の形式で指定してください";
46 private static final String ERRFORM_BOOL =
47 "起動オプション[{0}]の真偽指定[{1}]が不正です。"
48 + "on, off, yes, no, true, falseのいずれかを指定してください。";
49 private static final String ERRFORM_NONBOOL =
50 "起動オプション[{0}]は真偽を指定するオプションではありません。";
53 private Integer frameWidth = null;
54 private Integer frameHeight = null;
55 private Integer frameXpos = null;
56 private Integer frameYpos = null;
58 private final List<String> invokeArgs = new LinkedList<>();
59 private final List<CmdOption> optionList = new LinkedList<>();
60 private final Map<CmdOption, Boolean> boolOptionMap =
61 new EnumMap<>(CmdOption.class);
62 private final Map<CmdOption, String> stringOptionMap =
63 new EnumMap<>(CmdOption.class);
69 protected OptionInfo(){
76 * 文字列が可変引数のいずれかと英字大小無視で等しいか判定する。
79 * @param names 文字列の可変引数
80 * @return 等しい物があればtrue
82 private static boolean equalsIgnoreCase(String text, String ... names){
83 for(String name : names){
84 if(text.equalsIgnoreCase(name)) return true;
92 * @param info オプション情報格納先
93 * @param option オプション種別
94 * @param optTxt オプション名文字列
95 * @param onoff オプション引数
96 * @throws IllegalArgumentException 引数の構文エラー
98 private static void parseBooleanSwitch(OptionInfo info,
102 throws IllegalArgumentException{
105 if(equalsIgnoreCase(onoff, "on", "yes", "true")){
107 }else if(equalsIgnoreCase(onoff, "off", "no", "false")){
108 flag = Boolean.FALSE;
111 MessageFormat.format(ERRFORM_BOOL, optTxt, onoff);
112 throw new IllegalArgumentException(errmsg);
115 info.boolOptionMap.put(option, flag);
123 * <p>例) WIDTHxHEIGHT+XPOS+YPOS
125 * @param info オプション情報格納先
126 * @param optTxt オプション名文字列
127 * @param geometry オプション引数
128 * @throws IllegalArgumentException 引数の構文エラー
130 private static void parseGeometry(OptionInfo info,
133 throws IllegalArgumentException{
134 Matcher matcher = PATTERN_GEOMETRY.matcher(geometry);
135 if( ! matcher.matches() ){
136 String errmsg = MessageFormat.format(ERRFORM_GEOM,
138 throw new IllegalArgumentException(errmsg);
142 String width = matcher.group(gpos++);
143 String height = matcher.group(gpos++);
144 String xMinus = matcher.group(gpos++);
145 String xPos = matcher.group(gpos++);
146 String yMinus = matcher.group(gpos++);
147 String yPos = matcher.group(gpos++);
149 info.frameWidth = Integer.parseInt(width);
150 info.frameHeight = Integer.parseInt(height);
153 info.frameXpos = Integer.parseInt(xPos);
155 info.frameXpos = -info.frameXpos;
160 info.frameYpos = Integer.parseInt(yPos);
162 info.frameYpos = -info.frameYpos;
172 * @param info オプション情報
173 * @param optTxt オプション文字列
174 * @param option オプション種別
175 * @param iterator コマンドライン引数並び
176 * @throws IllegalArgumentException オプションの引数がない
178 private static void parseOptionArg(OptionInfo info,
181 Iterator<String> iterator )
182 throws IllegalArgumentException {
184 if(iterator.hasNext()){
185 nextArg = iterator.next();
187 String errMsg = MessageFormat.format(ERRFORM_NOARG, optTxt);
188 throw new IllegalArgumentException(errMsg);
191 if(option == CmdOption.OPT_GEOMETRY){
192 parseGeometry(info, optTxt, nextArg);
193 }else if(option.isBooleanOption()){
194 parseBooleanSwitch(info, option, optTxt, nextArg);
195 }else if( option == CmdOption.OPT_INITFONT
196 || option == CmdOption.OPT_CONFDIR ){
197 info.stringOptionMap.put(option, nextArg);
208 * @param args main()に渡されるオプション文字列
209 * @return 解析済みのオプション情報。
210 * @throws IllegalArgumentException 構文エラー
212 public static OptionInfo parseOptions(String ... args)
213 throws IllegalArgumentException{
214 OptionInfo result = new OptionInfo();
216 for(String arg : args){
217 if(arg == null) continue;
218 result.invokeArgs.add(arg);
220 Iterator<String> iterator = result.invokeArgs.iterator();
222 while(iterator.hasNext()){
223 String arg = iterator.next();
225 CmdOption option = CmdOption.parseCmdOption(arg);
227 String errmsg = MessageFormat.format(ERRFORM_UKNOWN, arg);
228 throw new IllegalArgumentException(errmsg);
230 result.optionList.add(option);
232 if( ! option.isIndepOption() ){
233 parseOptionArg(result, arg, option, iterator);
246 public List<String> getInvokeArgList(){
247 return Collections.unmodifiableList(this.invokeArgs);
251 * オプションが指定されていたか否か判定する。
253 * @param option オプション
254 * @return 指定されていたらtrue
256 public boolean hasOption(CmdOption option){
257 if(this.optionList.contains(option)) return true;
264 * <p>複数回指定された場合は最後の値。
266 * @param option オプション
267 * @return 真偽値。オプション指定がなかった場合はnull
268 * @throws IllegalArgumentException 真偽値を取るオプションではない。
270 public Boolean getBooleanArg(CmdOption option)
271 throws IllegalArgumentException{
272 if( ! option.isBooleanOption() ){
274 MessageFormat.format(ERRFORM_NONBOOL, option.toString());
275 throw new IllegalArgumentException(errMsg);
277 Boolean result = this.boolOptionMap.get(option);
284 * <p>複数回指定された場合は最後の値。
286 * @param option オプション
287 * @return 文字列。オプション指定がなかった場合はnull
289 public String getStringArg(CmdOption option){
290 String result = this.stringOptionMap.get(option);
295 * 排他的オプションのいずれかが指定されたか判定する。
297 * <p>後から指定された方が有効となる。
299 * @param options 排他的オプション群
300 * @return いずれかのオプション。どれも指定されなければnull
302 public CmdOption getExclusiveOption(CmdOption... options){
303 CmdOption result = null;
304 for(CmdOption option : this.optionList){
305 for(CmdOption excOption : options){
306 if(option == excOption){
318 * @return 初期のフレーム幅。オプション指定されてなければnull
320 public Integer initialFrameWidth(){
321 return this.frameWidth;
327 * @return 初期のフレーム高。オプション指定されてなければnull
329 public Integer initialFrameHeight(){
330 return this.frameHeight;
336 * @return 初期のフレーム位置のX座標。オプション指定されてなければnull
338 public Integer initialFrameXpos(){
339 return this.frameXpos;
345 * @return 初期のフレーム位置のY座標。オプション指定されてなければnull
347 public Integer initialFrameYpos(){
348 return this.frameYpos;