2 * command line argument info
4 * License : The MIT License
5 * Copyright(c) 2012 MikuToga Partners
8 package jp.sfjp.mikutoga.pmd2xml;
10 import java.text.MessageFormat;
11 import java.util.List;
12 import java.util.Locale;
19 private static final String EOL_LF = "\n";
20 private static final String EOL_CRLF = "\r\n";
21 private static final String EOL_DEFAULT = EOL_LF;
23 private static final String FORMAT_PMD = "pmd";
24 private static final String FORMAT_XML = "xml";
25 private static final String FORMAT_XML101009 = "xml101009";
26 private static final String FORMAT_XML130128 = "xml130128";
28 private static final String SFX_PMD = ".pmd";
29 private static final String SFX_XML = ".xml";
31 private static final String NL_LF = "lf";
32 private static final String NL_CRLF = "crlf";
34 private static final String GENERATOR =
35 Pmd2Xml.APPNAME + ' ' + Pmd2Xml.APPVER;
37 private static final String ERRMSG_UNKNOWN =
38 "Unknown option : {0}";
39 private static final String ERRMSG_MOREARG =
40 "You need option arg with : {0}";
41 private static final String ERRMSG_INTYPE =
42 "You must specify input format with -iform.";
43 private static final String ERRMSG_OUTTYPE =
44 "You must specify output format with -oform.";
45 private static final String ERRMSG_NOINFILE =
46 "You must specify input file with -i.";
47 private static final String ERRMSG_NOOUTFILE =
48 "You must specify output file with -o.";
49 private static final String ERRMSG_INVFORM =
50 "Unknown format : \"{0}\" must be \"pmd\" or \"xml\" "
51 + "or \"xml101009\" or \"xml130128\"";
52 private static final String ERRMSG_INVNL =
53 "Unknown newline : \"{0}\" must be \"lf\" or \"crlf\"";
54 private static final String ERRMSG_INVBOOL =
55 "Unknown switch : \"{0}\" must be \"on\" or \"off\"";
58 private boolean needHelp = false;
59 private ModelFileType inTypes = ModelFileType.NONE;
60 private ModelFileType outTypes = ModelFileType.NONE;
61 private String inFilename = null;
62 private String outFilename = null;
63 private boolean overwrite = false;
64 private String newline = EOL_DEFAULT;
65 private String generator = GENERATOR;
81 * @throws CmdLineException 不正なフォーマット種別
83 private static ModelFileType decodeFormatType(String arg)
84 throws CmdLineException{
87 if (FORMAT_PMD.equals(arg)){
88 result = ModelFileType.PMD;
89 }else if(FORMAT_XML.equals(arg)){
90 result = ModelFileType.XML_AUTO;
91 }else if(FORMAT_XML101009.equals(arg)){
92 result = ModelFileType.XML_101009;
93 }else if(FORMAT_XML130128.equals(arg)){
94 result = ModelFileType.XML_130128;
96 String errMsg = MessageFormat.format(ERRMSG_INVFORM, arg);
97 throw new CmdLineException(errMsg);
107 * @throws CmdLineException 不正なフォーマット種別
109 private static String decodeNewline(String arg)
110 throws CmdLineException{
113 if (NL_LF.equals(arg)){
115 }else if(NL_CRLF.equals(arg)){
118 String errMsg = MessageFormat.format(ERRMSG_INVNL, arg);
119 throw new CmdLineException(errMsg);
129 * @throws CmdLineException 不正なフォーマット種別
131 private static boolean decodeBoolean(String arg)
132 throws CmdLineException{
135 if( "on" .equals(arg)
136 || "true".equals(arg)
137 || "yes" .equals(arg) ){
139 }else if( "off" .equals(arg)
140 || "false".equals(arg)
141 || "no" .equals(arg) ){
144 String errMsg = MessageFormat.format(ERRMSG_INVBOOL, arg);
145 throw new CmdLineException(errMsg);
152 * ファイル名からファイル種別を類推する。
153 * <p>拡張子が「pmd」ならPMDファイル、「xml」ならXMLファイル。
154 * @param fileName ファイル名
157 private static ModelFileType getFileType(String fileName){
158 ModelFileType result = ModelFileType.NONE;
159 if(fileName == null) return result;
161 String lower = fileName.toLowerCase(Locale.ROOT);
162 if (lower.endsWith(SFX_PMD)) result = ModelFileType.PMD;
163 else if(lower.endsWith(SFX_XML)) result = ModelFileType.XML_AUTO;
170 * @param args コマンドライン
172 * @throws CmdLineException 不正なコマンドライン
174 static OptInfo parseOption(String... args) throws CmdLineException{
175 OptInfo result = new OptInfo();
177 List<CmdLine> cmdLines = CmdLine.parse(args);
178 for(CmdLine cmd : cmdLines){
179 OptSwitch opt = cmd.getOptSwitch();
180 if(opt == OptSwitch.OPT_HELP){
181 result.needHelp = true;
186 checkCmdLineList(cmdLines);
188 for(CmdLine cmd : cmdLines){
189 List<String> optArgs = cmd.getOptArgs();
190 String exArg1 = null;
191 if(optArgs.size() >= 2){
192 exArg1 = optArgs.get(1);
195 OptSwitch opt = cmd.getOptSwitch();
200 result.overwrite = true;
203 result.inFilename = exArg1;
206 result.outFilename = exArg1;
209 result.newline = decodeNewline(exArg1);
212 boolean genout = decodeBoolean(exArg1);
213 if(genout) result.generator = GENERATOR;
214 else result.generator = null;
217 ModelFileType itype = decodeFormatType(exArg1);
218 result.inTypes = itype;
221 ModelFileType otype = decodeFormatType(exArg1);
222 result.outTypes = otype;
226 throw new AssertionError();
238 * 単純なコマンドラインエラーを検出する。
239 * <p>検出項目は未知のオプションおよび不正な引数の個数
240 * @param cmdLines コマンドライン
241 * @throws CmdLineException 異常系
243 private static void checkCmdLineList(List<CmdLine> cmdLines)
244 throws CmdLineException{
245 for(CmdLine cmd : cmdLines){
246 List<String> optArgs = cmd.getOptArgs();
247 assert ! optArgs.isEmpty();
249 String optTxt = optArgs.get(0);
250 assert optTxt != null;
252 OptSwitch opt = cmd.getOptSwitch();
255 MessageFormat.format(ERRMSG_UNKNOWN, optTxt);
256 throw new CmdLineException(errMsg);
259 int exArgNum = opt.getExArgNum();
260 if(optArgs.size() != 1 + exArgNum){
262 MessageFormat.format(ERRMSG_MOREARG, optTxt);
263 throw new CmdLineException(errMsg);
271 * ファイルフォーマット情報の推測を行う。
272 * @param result オプション情報
274 private static void fixFormat(OptInfo result){
275 if(result.inTypes == ModelFileType.NONE){
276 result.inTypes = getFileType(result.inFilename);
279 if(result.outTypes == ModelFileType.NONE){
280 result.outTypes = getFileType(result.outFilename);
288 * @param result オプション情報
289 * @throws CmdLineException 不正なオプション設定
291 private static void checkResult(OptInfo result)
292 throws CmdLineException{
293 if(result.getInFilename() == null){
294 throw new CmdLineException(ERRMSG_NOINFILE);
297 if(result.getOutFilename() == null){
298 throw new CmdLineException(ERRMSG_NOOUTFILE);
301 if(result.getInFileType() == ModelFileType.NONE){
302 throw new CmdLineException(ERRMSG_INTYPE);
305 if(result.getOutFileType() == ModelFileType.NONE){
306 throw new CmdLineException(ERRMSG_OUTTYPE);
318 return this.needHelp;
325 ModelFileType getInFileType(){
333 ModelFileType getOutFileType(){
334 return this.outTypes;
341 String getInFilename(){
342 return this.inFilename;
349 String getOutFilename(){
350 return this.outFilename;
355 * @return 上書きモードならtrue
357 boolean overwriteMode(){
358 return this.overwrite;
371 * @return ジェネレータ名。表示したくない時はnull
373 String getGenerator(){
374 return this.generator;