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 ERRMSG_UNKNOWN =
35 "Unknown option : {0}";
36 private static final String ERRMSG_MOREARG =
37 "You need option arg with : {0}";
38 private static final String ERRMSG_INTYPE =
39 "You must specify input format with -iform.";
40 private static final String ERRMSG_OUTTYPE =
41 "You must specify output format with -oform.";
42 private static final String ERRMSG_NOINFILE =
43 "You must specify input file with -i.";
44 private static final String ERRMSG_NOOUTFILE =
45 "You must specify output file with -o.";
46 private static final String ERRMSG_INVFORM =
47 "Unknown format : \"{0}\" must be \"pmd\" or \"xml\" "
48 + "or \"xml101009\" or \"xml130128\"";
49 private static final String ERRMSG_INVNL =
50 "Unknown newline : \"{0}\" must be \"lf\" or \"crlf\"";
51 private static final String ERRMSG_INVBOOL =
52 "Unknown switch : \"{0}\" must be \"on\" or \"off\"";
55 private boolean needHelp = false;
56 private ModelFileType inTypes = ModelFileType.NONE;
57 private ModelFileType outTypes = ModelFileType.NONE;
58 private String inFilename = null;
59 private String outFilename = null;
60 private boolean overwrite = false;
61 private String newline = EOL_DEFAULT;
62 private String generator = Pmd2Xml.GENERATOR;
78 * @throws CmdLineException 不正なフォーマット種別
80 private static ModelFileType decodeFormatType(String arg)
81 throws CmdLineException{
84 if (FORMAT_PMD.equals(arg)){
85 result = ModelFileType.PMD;
86 }else if(FORMAT_XML.equals(arg)){
87 result = ModelFileType.XML_AUTO;
88 }else if(FORMAT_XML101009.equals(arg)){
89 result = ModelFileType.XML_101009;
90 }else if(FORMAT_XML130128.equals(arg)){
91 result = ModelFileType.XML_130128;
93 String errMsg = MessageFormat.format(ERRMSG_INVFORM, arg);
94 throw new CmdLineException(errMsg);
104 * @throws CmdLineException 不正なフォーマット種別
106 private static String decodeNewline(String arg)
107 throws CmdLineException{
110 if (NL_LF.equals(arg)){
112 }else if(NL_CRLF.equals(arg)){
115 String errMsg = MessageFormat.format(ERRMSG_INVNL, arg);
116 throw new CmdLineException(errMsg);
126 * @throws CmdLineException 不正なフォーマット種別
128 private static boolean decodeBoolean(String arg)
129 throws CmdLineException{
132 if( "on" .equals(arg)
133 || "true".equals(arg)
134 || "yes" .equals(arg) ){
136 }else if( "off" .equals(arg)
137 || "false".equals(arg)
138 || "no" .equals(arg) ){
141 String errMsg = MessageFormat.format(ERRMSG_INVBOOL, arg);
142 throw new CmdLineException(errMsg);
149 * ファイル名からファイル種別を類推する。
150 * <p>拡張子が「pmd」ならPMDファイル、「xml」ならXMLファイル。
151 * @param fileName ファイル名
154 private static ModelFileType getFileType(String fileName){
155 ModelFileType result = ModelFileType.NONE;
156 if(fileName == null) return result;
158 String lower = fileName.toLowerCase(Locale.ROOT);
159 if (lower.endsWith(SFX_PMD)) result = ModelFileType.PMD;
160 else if(lower.endsWith(SFX_XML)) result = ModelFileType.XML_AUTO;
167 * @param args コマンドライン
169 * @throws CmdLineException 不正なコマンドライン
171 static OptInfo parseOption(String... args) throws CmdLineException{
172 OptInfo result = new OptInfo();
174 List<CmdLine> cmdLines = CmdLine.parse(args);
175 for(CmdLine cmd : cmdLines){
176 OptSwitch opt = cmd.getOptSwitch();
177 if(opt == OptSwitch.OPT_HELP){
178 result.needHelp = true;
183 checkCmdLineList(cmdLines);
185 for(CmdLine cmd : cmdLines){
186 List<String> optArgs = cmd.getOptArgs();
187 String exArg1 = null;
188 if(optArgs.size() >= 2){
189 exArg1 = optArgs.get(1);
192 OptSwitch opt = cmd.getOptSwitch();
197 result.overwrite = true;
200 result.inFilename = exArg1;
203 result.outFilename = exArg1;
206 result.newline = decodeNewline(exArg1);
209 boolean genout = decodeBoolean(exArg1);
210 if(genout) result.generator = Pmd2Xml.GENERATOR;
211 else result.generator = null;
214 ModelFileType itype = decodeFormatType(exArg1);
215 result.inTypes = itype;
218 ModelFileType otype = decodeFormatType(exArg1);
219 result.outTypes = otype;
223 throw new AssertionError();
235 * 単純なコマンドラインエラーを検出する。
236 * <p>検出項目は未知のオプションおよび不正な引数の個数
237 * @param cmdLines コマンドライン
238 * @throws CmdLineException 異常系
240 private static void checkCmdLineList(List<CmdLine> cmdLines)
241 throws CmdLineException{
242 for(CmdLine cmd : cmdLines){
243 List<String> optArgs = cmd.getOptArgs();
244 assert ! optArgs.isEmpty();
246 String optTxt = optArgs.get(0);
247 assert optTxt != null;
249 OptSwitch opt = cmd.getOptSwitch();
252 MessageFormat.format(ERRMSG_UNKNOWN, optTxt);
253 throw new CmdLineException(errMsg);
256 int exArgNum = opt.getExArgNum();
257 if(optArgs.size() != 1 + exArgNum){
259 MessageFormat.format(ERRMSG_MOREARG, optTxt);
260 throw new CmdLineException(errMsg);
268 * ファイルフォーマット情報の推測を行う。
269 * @param result オプション情報
271 private static void fixFormat(OptInfo result){
272 if(result.inTypes == ModelFileType.NONE){
273 result.inTypes = getFileType(result.inFilename);
276 if(result.outTypes == ModelFileType.NONE){
277 result.outTypes = getFileType(result.outFilename);
285 * @param result オプション情報
286 * @throws CmdLineException 不正なオプション設定
288 private static void checkResult(OptInfo result)
289 throws CmdLineException{
290 if(result.getInFilename() == null){
291 throw new CmdLineException(ERRMSG_NOINFILE);
294 if(result.getOutFilename() == null){
295 throw new CmdLineException(ERRMSG_NOOUTFILE);
298 if(result.getInFileType() == ModelFileType.NONE){
299 throw new CmdLineException(ERRMSG_INTYPE);
302 if(result.getOutFileType() == ModelFileType.NONE){
303 throw new CmdLineException(ERRMSG_OUTTYPE);
315 return this.needHelp;
322 ModelFileType getInFileType(){
330 ModelFileType getOutFileType(){
331 return this.outTypes;
338 String getInFilename(){
339 return this.inFilename;
346 String getOutFilename(){
347 return this.outFilename;
352 * @return 上書きモードならtrue
354 boolean overwriteMode(){
355 return this.overwrite;
368 * @return ジェネレータ名。表示したくない時はnull
370 String getGenerator(){
371 return this.generator;