2 * pmd 2 xml converter main entry
4 * License : The MIT License
5 * Copyright(c) 2010 MikuToga Partners
8 package jp.sourceforge.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.util.Properties;
22 import jp.sourceforge.mikutoga.parser.MmdFormatException;
23 import jp.sourceforge.mikutoga.pmd.IllegalPmdDataException;
24 import jp.sourceforge.mikutoga.xml.TogaXmlException;
25 import org.xml.sax.SAXException;
28 * PMDモデルファイルとXMLとの間で変換を行うアプリケーション。
30 public final class Pmd2Xml {
33 public static final int EXIT_OK = 0;
34 /** ファイル入出力に起因するエラー。 */
35 public static final int EXIT_FILE = 1;
36 /** XMLフォーマットに起因するエラー。 */
37 public static final int EXIT_XML = 2;
38 /** PMDフォーマットに起因するエラー。 */
39 public static final int EXIT_PMD = 3;
41 public static final int EXIT_JREVER = 4;
42 /** オプション指定に起因するエラー。 */
43 public static final int EXIT_OPT = 5;
45 public static final int EXIT_INTERN = 6;
48 public static final String APPNAME;
50 public static final String APPVER;
52 public static final String APPLICENSE;
54 private static final Class<?> THISCLASS;
56 private static final PrintStream ERROUT = System.err;
59 THISCLASS = Pmd2Xml.class;
61 THISCLASS.getResourceAsStream("resources/version.properties");
62 Properties verProps = new Properties();
69 }catch(IOException e){
70 throw new ExceptionInInitializerError(e);
73 APPNAME = verProps.getProperty("app.name");
74 APPVER = verProps.getProperty("app.version");
75 APPLICENSE = verProps.getProperty("app.license");
77 new Pmd2Xml().hashCode();
86 assert this.getClass().equals(THISCLASS);
95 private static void exit(int code){
103 * @param dumpStack スタックトレースを出力するならtrue
105 private static void errPrintln(Throwable ex, boolean dumpStack){
106 String text = ex.toString();
107 ERROUT.println(text);
110 ex.printStackTrace(ERROUT);
120 private static void errPrintln(Throwable ex){
121 errPrintln(ex, false);
127 * @param text 個別メッセージ
129 private static void errMsg(String text){
130 ERROUT.println("ERROR:");
131 ERROUT.println(text);
132 ERROUT.println("(-h for help)");
141 private static void ioError(Throwable ex){
151 private static void xmlError(Throwable ex){
157 * PMDファイルフォーマットエラー処理。
161 private static void pmdError(Throwable ex){
162 errPrintln(ex, true);
171 private static void internalError(Throwable ex){
172 errPrintln(ex, true);
180 private static void checkJRE(){
181 Package jrePackage = java.lang.Object.class.getPackage();
182 if( ! jrePackage.isCompatibleWith("1.6")){
183 ERROUT.println("You need JRE 1.6 or later.");
190 * ヘルプメッセージを出力してVMを終了させる。
192 private static void putHelp(){
193 StringBuilder appInfo = new StringBuilder();
196 appInfo.append(APPNAME).append(' ').append(APPVER)
198 appInfo.append(indent)
199 .append("License").append(" : ").append(APPLICENSE)
201 appInfo.append(indent)
202 .append("http://mikutoga.sourceforge.jp/")
205 ERROUT.println(appInfo.toString());
206 ERROUT.println(OptInfo.getConsoleHelp());
214 * @throws IOException 入出力エラー
216 private static void trunc(File file) throws IOException{
217 if( ! file.exists() ) return;
218 if( ! file.isFile() ) return;
220 FileOutputStream foStream = new FileOutputStream(file);
221 FileChannel channnel = foStream.getChannel();
222 channnel.truncate(0);
232 * @param fileName 入力ファイル名
235 private static InputStream openInfile(String fileName){
236 File inFile = new File(fileName);
238 if( (! inFile.exists()) || (! inFile.isFile()) ){
239 String absPath = inFile.getAbsolutePath();
240 errMsg("Can't find input file:" + absPath);
244 InputStream is = null;
246 is = new FileInputStream(inFile);
247 }catch(FileNotFoundException e){
252 is = new BufferedInputStream(is);
259 * @param fileName 出力ファイル名
260 * @param overWrite 頭から上書きして良ければtrue
263 private static OutputStream openOutfile(String fileName,
265 File outFile = new File(fileName);
267 if(outFile.exists()){
268 String absPath = outFile.getAbsolutePath();
269 if( ! outFile.isFile() ){
270 String msg = absPath + " is not file.";
273 }else if( ! overWrite ){
275 absPath + " already exists.\n"
276 + "If you want to overwrite, use -f.";
284 }catch(IOException e){
288 OutputStream os = null;
290 os = new FileOutputStream(outFile);
291 }catch(FileNotFoundException e){
296 os = new BufferedOutputStream(os);
303 * @param args コマンドパラメータ
305 public static void main(String[] args){
308 Pmd2XmlConv converter = new Pmd2XmlConv();
310 OptInfo optInfo = OptInfo.parseOption(args);
311 if(optInfo.needHelp()){
314 }else if(optInfo.hasError()){
315 String optErrMsg = optInfo.getErrorMessage();
320 String inputFile = optInfo.getInFilename();
321 String outputFile = optInfo.getOutFilename();
322 boolean overwrite = optInfo.overwriteMode();
324 InputStream is = openInfile(inputFile);
325 OutputStream os = openOutfile(outputFile, overwrite);
327 converter.setInType(optInfo.getInFileType());
328 converter.setOutType(optInfo.getOutFileType());
330 converter.setNewline(optInfo.getNewline());
331 converter.setGenerator(optInfo.getGenerator());
334 converter.convert(is, os);
335 }catch(IOException e){
337 }catch(IllegalPmdDataException e){
339 }catch(MmdFormatException e){
341 }catch(TogaXmlException e){
343 }catch(SAXException e){
351 }catch(IOException e){
354 }catch(IOException e){