/* * command line argument info * * License : The MIT License * Copyright(c) 2012 MikuToga Partners */ package jp.sfjp.mikutoga.pmd2xml; import java.text.MessageFormat; import java.util.List; import java.util.Locale; /** * コマンドラインオプション情報。 */ final class OptInfo { private static final String EOL_LF = "\n"; private static final String EOL_CRLF = "\r\n"; private static final String EOL_DEFAULT = EOL_LF; private static final String FORMAT_PMD = "pmd"; private static final String FORMAT_XML = "xml"; private static final String FORMAT_XML101009 = "xml101009"; private static final String FORMAT_XML130128 = "xml130128"; private static final String SFX_PMD = ".pmd"; private static final String SFX_XML = ".xml"; private static final String NL_LF = "lf"; private static final String NL_CRLF = "crlf"; private static final String ERRMSG_UNKNOWN = "Unknown option : {0}"; private static final String ERRMSG_MOREARG = "You need option arg with : {0}"; private static final String ERRMSG_INTYPE = "You must specify input format with -iform."; private static final String ERRMSG_OUTTYPE = "You must specify output format with -oform."; private static final String ERRMSG_NOINFILE = "You must specify input file with -i."; private static final String ERRMSG_NOOUTFILE = "You must specify output file with -o."; private static final String ERRMSG_INVFORM = "Unknown format : \"{0}\" must be \"pmd\" or \"xml\" " + "or \"xml101009\" or \"xml130128\""; private static final String ERRMSG_INVNL = "Unknown newline : \"{0}\" must be \"lf\" or \"crlf\""; private static final String ERRMSG_INVBOOL = "Unknown switch : \"{0}\" must be \"on\" or \"off\""; private boolean needHelp = false; private ModelFileType inTypes = ModelFileType.NONE; private ModelFileType outTypes = ModelFileType.NONE; private String inFilename = null; private String outFilename = null; private boolean overwrite = false; private String newline = EOL_DEFAULT; private String generator = Pmd2Xml.GENERATOR; /** * コンストラクタ。 */ private OptInfo(){ super(); return; } /** * フォーマット種別指定子をデコードする。 * * @param arg 文字列 * @return デコード結果。 * @throws CmdLineException 不正なフォーマット種別 */ private static ModelFileType decodeFormatType(String arg) throws CmdLineException{ ModelFileType result; if (FORMAT_PMD.equals(arg)){ result = ModelFileType.PMD; }else if(FORMAT_XML.equals(arg)){ result = ModelFileType.XML_AUTO; }else if(FORMAT_XML101009.equals(arg)){ result = ModelFileType.XML_101009; }else if(FORMAT_XML130128.equals(arg)){ result = ModelFileType.XML_130128; }else{ String errMsg = MessageFormat.format(ERRMSG_INVFORM, arg); throw new CmdLineException(errMsg); } return result; } /** * 改行文字指定子をデコードする。 * * @param arg 文字列 * @return デコード結果。 * @throws CmdLineException 不正なフォーマット種別 */ private static String decodeNewline(String arg) throws CmdLineException{ String result; if (NL_LF.equals(arg)){ result = EOL_LF; }else if(NL_CRLF.equals(arg)){ result = EOL_CRLF; }else{ String errMsg = MessageFormat.format(ERRMSG_INVNL, arg); throw new CmdLineException(errMsg); } return result; } /** * ブール指定子をデコードする。 * * @param arg 文字列 * @return デコード結果。 * @throws CmdLineException 不正なフォーマット種別 */ private static boolean decodeBoolean(String arg) throws CmdLineException{ boolean result; if( "on" .equals(arg) || "true".equals(arg) || "yes" .equals(arg) ){ result = true; }else if( "off" .equals(arg) || "false".equals(arg) || "no" .equals(arg) ){ result = false; }else{ String errMsg = MessageFormat.format(ERRMSG_INVBOOL, arg); throw new CmdLineException(errMsg); } return result; } /** * ファイル名からファイル種別を類推する。 * *
拡張子が「pmd」ならPMDファイル、「xml」ならXMLファイル。
*
* @param fileName ファイル名
* @return ファイル種別
*/
private static ModelFileType getFileType(String fileName){
ModelFileType result = ModelFileType.NONE;
if(fileName == null) return result;
String lower = fileName.toLowerCase(Locale.ROOT);
if (lower.endsWith(SFX_PMD)) result = ModelFileType.PMD;
else if(lower.endsWith(SFX_XML)) result = ModelFileType.XML_AUTO;
return result;
}
/**
* コマンドラインを解析する。
*
* @param args コマンドライン
* @return オプション情報
* @throws CmdLineException 不正なコマンドライン
*/
static OptInfo parseOption(String... args) throws CmdLineException{
OptInfo result = new OptInfo();
List 検出項目は未知のオプションおよび不正な引数の個数
*
* @param cmdLines コマンドライン
* @throws CmdLineException 異常系
*/
private static void checkCmdLineList(List