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;
18 private static final String EOL_LF = "\n";
19 private static final String EOL_CRLF = "\r\n";
20 private static final String EOL_DEFAULT = EOL_LF;
22 private static final String FORMAT_PMD = "pmd";
23 private static final String FORMAT_XML = "xml";
24 private static final String FORMAT_XML101009 = "xml101009";
25 private static final String FORMAT_XML130128 = "xml130128";
27 private static final String SFX_PMD = ".pmd";
28 private static final String SFX_XML = ".xml";
30 private static final String NL_LF = "lf";
31 private static final String NL_CRLF = "crlf";
33 private static final String GENERATOR =
34 Pmd2Xml.APPNAME + ' ' + Pmd2Xml.APPVER;
36 private static final String ERRMSG_UNKNOWN =
37 "Unknown option : {0}";
38 private static final String ERRMSG_MOREARG =
39 "You need option arg with : {0}";
40 private static final String ERRMSG_INTYPE =
41 "You must specify input format with -iform.";
42 private static final String ERRMSG_OUTTYPE =
43 "You must specify output format with -oform.";
44 private static final String ERRMSG_NOINFILE =
45 "You must specify input file with -i.";
46 private static final String ERRMSG_NOOUTFILE =
47 "You must specify output file with -o.";
48 private static final String ERRMSG_INVFORM =
49 "Unknown format : \"{0}\" must be \"pmd\" or \"xml\" "
50 + "or \"xml101009\" or \"xml130128\"";
51 private static final String ERRMSG_INVNL =
52 "Unknown newline : \"{0}\" must be \"lf\" or \"crlf\"";
53 private static final String ERRMSG_INVBOOL =
54 "Unknown switch : \"{0}\" must be \"on\" or \"off\"";
57 private boolean needHelp = false;
58 private ModelFileType inTypes = ModelFileType.NONE;
59 private ModelFileType outTypes = ModelFileType.NONE;
60 private String inFilename = null;
61 private String outFilename = null;
62 private boolean overwrite = false;
63 private String newline = EOL_DEFAULT;
64 private String generator = GENERATOR;
80 * @throws CmdLineException 不正なフォーマット種別
82 private static ModelFileType decodeFormatType(String arg)
83 throws CmdLineException{
86 if (FORMAT_PMD.equals(arg)){
87 result = ModelFileType.PMD;
88 }else if(FORMAT_XML.equals(arg)){
89 result = ModelFileType.XML_AUTO;
90 }else if(FORMAT_XML101009.equals(arg)){
91 result = ModelFileType.XML_101009;
92 }else if(FORMAT_XML130128.equals(arg)){
93 result = ModelFileType.XML_130128;
95 String errMsg = MessageFormat.format(ERRMSG_INVFORM, arg);
96 throw new CmdLineException(errMsg);
106 * @throws CmdLineException 不正なフォーマット種別
108 private static String decodeNewline(String arg)
109 throws CmdLineException{
112 if (NL_LF.equals(arg)){
114 }else if(NL_CRLF.equals(arg)){
117 String errMsg = MessageFormat.format(ERRMSG_INVNL, arg);
118 throw new CmdLineException(errMsg);
128 * @throws CmdLineException 不正なフォーマット種別
130 private static boolean decodeBoolean(String arg)
131 throws CmdLineException{
134 if( "on" .equals(arg)
135 || "true".equals(arg)
136 || "yes" .equals(arg) ){
138 }else if( "off" .equals(arg)
139 || "false".equals(arg)
140 || "no" .equals(arg) ){
143 String errMsg = MessageFormat.format(ERRMSG_INVBOOL, arg);
144 throw new CmdLineException(errMsg);
151 * ファイル名からファイル種別を類推する。
152 * <p>拡張子が「pmd」ならPMDファイル、「xml」ならXMLファイル。
153 * @param fileName ファイル名
156 private static ModelFileType getFileType(String fileName){
157 ModelFileType result = ModelFileType.NONE;
158 if(fileName == null) return result;
160 String lower = fileName.toLowerCase();
161 if (lower.endsWith(SFX_PMD)) result = ModelFileType.PMD;
162 else if(lower.endsWith(SFX_XML)) result = ModelFileType.XML_AUTO;
169 * @param args コマンドライン
171 * @throws CmdLineException 不正なコマンドライン
173 static OptInfo parseOption(String... args) throws CmdLineException{
174 OptInfo result = new OptInfo();
176 List<CmdLine> cmdLines = CmdLine.parse(args);
177 for(CmdLine cmd : cmdLines){
178 OptSwitch opt = cmd.getOptSwitch();
179 if(opt == OptSwitch.OPT_HELP){
180 result.needHelp = true;
185 checkCmdLineList(cmdLines);
187 for(CmdLine cmd : cmdLines){
188 List<String> optArgs = cmd.getOptArgs();
189 String exArg1 = null;
190 if(optArgs.size() >= 2){
191 exArg1 = optArgs.get(1);
194 OptSwitch opt = cmd.getOptSwitch();
199 result.overwrite = true;
202 result.inFilename = exArg1;
205 result.outFilename = exArg1;
208 result.newline = decodeNewline(exArg1);
211 boolean genout = decodeBoolean(exArg1);
212 if(genout) result.generator = GENERATOR;
213 else result.generator = null;
216 ModelFileType itype = decodeFormatType(exArg1);
217 result.inTypes = itype;
220 ModelFileType otype = decodeFormatType(exArg1);
221 result.outTypes = otype;
225 throw new AssertionError();
237 * 単純なコマンドラインエラーを検出する。
238 * <p>検出項目は未知のオプションおよび不正な引数の個数
239 * @param cmdLines コマンドライン
240 * @throws CmdLineException 異常系
242 private static void checkCmdLineList(List<CmdLine> cmdLines)
243 throws CmdLineException{
244 for(CmdLine cmd : cmdLines){
245 List<String> optArgs = cmd.getOptArgs();
246 assert optArgs.size() > 0;
248 String optTxt = optArgs.get(0);
249 assert optTxt != null;
251 OptSwitch opt = cmd.getOptSwitch();
254 MessageFormat.format(ERRMSG_UNKNOWN, optTxt);
255 throw new CmdLineException(errMsg);
258 int exArgNum = opt.getExArgNum();
259 if(optArgs.size() != 1 + exArgNum){
261 MessageFormat.format(ERRMSG_MOREARG, optTxt);
262 throw new CmdLineException(errMsg);
270 * ファイルフォーマット情報の推測を行う。
271 * @param result オプション情報
273 private static void fixFormat(OptInfo result){
274 if(result.inTypes == ModelFileType.NONE){
275 result.inTypes = getFileType(result.inFilename);
278 if(result.outTypes == ModelFileType.NONE){
279 result.outTypes = getFileType(result.outFilename);
287 * @param result オプション情報
288 * @throws CmdLineException 不正なオプション設定
290 private static void checkResult(OptInfo result)
291 throws CmdLineException{
292 if(result.getInFilename() == null){
293 throw new CmdLineException(ERRMSG_NOINFILE);
296 if(result.getOutFilename() == null){
297 throw new CmdLineException(ERRMSG_NOOUTFILE);
300 if(result.getInFileType() == ModelFileType.NONE){
301 throw new CmdLineException(ERRMSG_INTYPE);
304 if(result.getOutFileType() == ModelFileType.NONE){
305 throw new CmdLineException(ERRMSG_OUTTYPE);
317 return this.needHelp;
324 ModelFileType getInFileType(){
332 ModelFileType getOutFileType(){
333 return this.outTypes;
340 String getInFilename(){
341 return this.inFilename;
348 String getOutFilename(){
349 return this.outFilename;
354 * @return 上書きモードならtrue
356 boolean overwriteMode(){
357 return this.overwrite;
370 * @return ジェネレータ名。表示したくない時はnull
372 String getGenerator(){
373 return this.generator;