2 * pmd 2 xml converter main entry
4 * License : The MIT License
5 * Copyright(c) 2010 MikuToga Partners
8 package jp.sfjp.mikutoga.pmd2xml;
10 import java.io.BufferedInputStream;
11 import java.io.BufferedOutputStream;
13 import java.io.FileInputStream;
14 import java.io.FileNotFoundException;
15 import java.io.FileOutputStream;
16 import java.io.IOException;
17 import java.io.InputStream;
18 import java.io.OutputStream;
19 import java.io.PrintStream;
20 import java.nio.channels.FileChannel;
21 import java.text.MessageFormat;
22 import java.util.Properties;
23 import jp.sfjp.mikutoga.bin.parser.MmdFormatException;
24 import jp.sfjp.mikutoga.pmd.IllegalPmdDataException;
25 import jp.sourceforge.mikutoga.xml.TogaXmlException;
26 import org.xml.sax.SAXException;
29 * PMDモデルファイルとXMLとの間で変換を行うアプリケーション。
31 public final class Pmd2Xml {
34 public static final int EXIT_OK = 0;
36 public static final int EXIT_INTERR = 1;
38 public static final int EXIT_ENVERR = 2;
39 /** オプション指定に起因するエラー。 */
40 public static final int EXIT_OPTERR = 3;
41 /** ファイル入出力に起因するエラー。 */
42 public static final int EXIT_IOERR = 4;
43 /** XMLフォーマットに起因するエラー。 */
44 public static final int EXIT_XMLERR = 5;
45 /** PMDフォーマットに起因するエラー。 */
46 public static final int EXIT_PMDERR = 6;
49 public static final String APPNAME;
51 public static final String APPVER;
53 public static final String APPLICENSE;
55 public static final String APPURL;
57 private static final Class<?> THISCLASS;
58 private static final String RES_VER = "resources/version.properties";
60 private static final PrintStream ERROUT = System.err;
61 private static final String MSG_ERR = "ERROR:\n{0}\n(-h for help)";
62 private static final String MSG_HELP =
64 + "\u0020\u0020License\u0020:\u0020{2}\n"
65 + "\u0020\u0020{3}\n";
66 private static final String MSG_NOINFILE = "Can't find input file:{0}";
67 private static final String MSG_ABNFILE = "{0} is not file.";
68 private static final String MSG_OWOUTFILE =
69 "{0} already exists.\n"
70 + "If you want to overwrite, use -f.";
72 private static final String MSG_OLDJRE = "You need JRE {0} or later.";
73 private static final String REQUIRED_JRE = "1.6";
76 THISCLASS = Pmd2Xml.class;
77 InputStream ver = THISCLASS.getResourceAsStream(RES_VER);
78 Properties verProps = new Properties();
85 }catch(IOException e){
86 throw new ExceptionInInitializerError(e);
89 APPNAME = verProps.getProperty("app.name");
90 APPVER = verProps.getProperty("app.version");
91 APPLICENSE = verProps.getProperty("app.license");
92 APPURL = verProps.getProperty("app.url");
94 new Pmd2Xml().hashCode();
103 assert this.getClass().equals(THISCLASS);
111 * @see java.lang.System#exit(int)
113 private static void exit(int code){
116 throw new AssertionError();
121 * @param text 個別メッセージ
123 private static void errMsg(String text){
124 String msg = MessageFormat.format(MSG_ERR, text);
132 * @param dumpStack スタックトレースを出力するならtrue
134 private static void thPrintln(Throwable ex, boolean dumpStack){
136 ex.printStackTrace(ERROUT);
138 String text = ex.toString();
139 ERROUT.println(text);
149 private static void thPrintln(Throwable ex){
150 thPrintln(ex, false);
159 private static void ioError(IOException ex){
169 private static void xmlError(Throwable ex){
175 * PMDファイルフォーマットエラー処理。
179 private static void pmdError(MmdFormatException ex){
189 private static void internalError(Throwable ex){
198 private static void checkJRE(){
199 Package jrePackage = java.lang.Object.class.getPackage();
200 if( ! jrePackage.isCompatibleWith(REQUIRED_JRE)){
201 String msg = MessageFormat.format(MSG_OLDJRE, REQUIRED_JRE);
211 private static void putHelp(){
213 MessageFormat.format(MSG_HELP,
214 APPNAME, APPVER, APPLICENSE, APPURL);
216 ERROUT.println(OptSwitch.getConsoleHelp());
222 * <p>ファイルが存在しなければなにもしない。
223 * <p>通常ファイルでなければなにもしない。
225 * @throws IOException 入出力エラー
227 private static void trunc(File file) throws IOException{
228 if( ! file.exists() ) return;
229 if( ! file.isFile() ) return;
231 if(file.length() <= 0L) return;
233 FileOutputStream foStream = new FileOutputStream(file);
235 FileChannel channnel = foStream.getChannel();
237 channnel.truncate(0L);
250 * <p>入力ファイルが通常ファイルとして存在しなければエラー終了。
251 * @param fileName 入力ファイル名
254 private static InputStream openInfile(String fileName){
255 File inFile = new File(fileName);
257 if( (! inFile.exists()) || (! inFile.isFile()) ){
258 String absPath = inFile.getAbsolutePath();
259 String msg = MessageFormat.format(MSG_NOINFILE, absPath);
266 is = new FileInputStream(inFile);
267 }catch(FileNotFoundException e){
270 throw new AssertionError(e);
273 is = new BufferedInputStream(is);
280 * <p>出力ファイルが通常ファイルでない場合はエラー終了。
281 * <p>既存の出力ファイルに上書き指示が伴っていなければエラー終了。
282 * @param fileName 出力ファイル名
283 * @param overWrite 頭から上書きして良ければtrue
286 private static OutputStream openOutfile(String fileName,
288 File outFile = new File(fileName);
290 if(outFile.exists()){
291 String absPath = outFile.getAbsolutePath();
292 if( ! outFile.isFile() ){
293 String msg = MessageFormat.format(MSG_ABNFILE, absPath);
296 }else if( ! overWrite ){
297 String msg = MessageFormat.format(MSG_OWOUTFILE, absPath);
305 }catch(IOException e){
311 os = new FileOutputStream(outFile);
312 }catch(FileNotFoundException e){
315 throw new AssertionError(e);
318 os = new BufferedOutputStream(os);
325 * @param args コマンドパラメータ
327 public static void main(String[] args){
332 optInfo = OptInfo.parseOption(args);
333 }catch(CmdLineException e){
334 String optErrMsg = e.getLocalizedMessage();
340 if(optInfo.needHelp()){
345 String inputFile = optInfo.getInFilename();
346 String outputFile = optInfo.getOutFilename();
347 boolean overwrite = optInfo.overwriteMode();
349 InputStream is = openInfile(inputFile);
350 OutputStream os = openOutfile(outputFile, overwrite);
352 Pmd2XmlConv converter = new Pmd2XmlConv();
354 converter.setInType (optInfo.getInFileType());
355 converter.setOutType(optInfo.getOutFileType());
357 converter.setNewline(optInfo.getNewline());
358 converter.setGenerator(optInfo.getGenerator());
361 converter.convert(is, os);
362 }catch(IOException e){
364 }catch(IllegalPmdDataException e){
366 }catch(MmdFormatException e){
368 }catch(TogaXmlException e){
370 }catch(SAXException e){
378 }catch(IOException e){
381 }catch(IOException e){