OSDN Git Service

エラー時のプロセス終了コードを整理。
[mikutoga/Pmd2XML.git] / src / main / java / jp / sourceforge / mikutoga / pmd2xml / Pmd2Xml.java
1 /*\r
2  * pmd 2 xml converter main entry\r
3  *\r
4  * License : The MIT License\r
5  * Copyright(c) 2010 MikuToga Partners\r
6  */\r
7 \r
8 package jp.sourceforge.mikutoga.pmd2xml;\r
9 \r
10 import java.io.BufferedInputStream;\r
11 import java.io.BufferedOutputStream;\r
12 import java.io.File;\r
13 import java.io.FileInputStream;\r
14 import java.io.FileOutputStream;\r
15 import java.io.IOException;\r
16 import java.io.InputStream;\r
17 import java.io.OutputStream;\r
18 import java.nio.channels.FileChannel;\r
19 import java.util.Properties;\r
20 import javax.xml.parsers.DocumentBuilder;\r
21 import javax.xml.parsers.ParserConfigurationException;\r
22 import jp.sourceforge.mikutoga.parser.MmdFormatException;\r
23 import jp.sourceforge.mikutoga.parser.MmdSource;\r
24 import jp.sourceforge.mikutoga.pmd.PmdModel;\r
25 import jp.sourceforge.mikutoga.pmd.pmdexporter.IllegalPmdException;\r
26 import jp.sourceforge.mikutoga.pmd.pmdexporter.PmdExporter;\r
27 import jp.sourceforge.mikutoga.pmd.pmdloader.PmdLoader;\r
28 import jp.sourceforge.mikutoga.pmd.xml.PmdXmlExporter;\r
29 import jp.sourceforge.mikutoga.pmd.xml.PmdXmlResources;\r
30 import jp.sourceforge.mikutoga.pmd.xml.Xml2PmdLoader;\r
31 import jp.sourceforge.mikutoga.xml.TogaXmlException;\r
32 import org.xml.sax.InputSource;\r
33 import org.xml.sax.SAXException;\r
34 \r
35 /**\r
36  * PMDモデルファイルとXMLとの間で変換を行うアプリケーション。\r
37  */\r
38 public final class Pmd2Xml {\r
39 \r
40     private static final Class THISCLASS;\r
41     private static final String APPNAME;\r
42     private static final String APPVER;\r
43     private static final String APPLICENSE;\r
44 \r
45     static{\r
46         THISCLASS = Pmd2Xml.class;\r
47         InputStream ver =\r
48                 THISCLASS.getResourceAsStream("resources/version.properties");\r
49         Properties verProps = new Properties();\r
50         try{\r
51             verProps.load(ver);\r
52         }catch(IOException e){\r
53             throw new ExceptionInInitializerError(e);\r
54         }\r
55 \r
56         APPNAME    = verProps.getProperty("app.name");\r
57         APPVER     = verProps.getProperty("app.version");\r
58         APPLICENSE = verProps.getProperty("app.license");\r
59 \r
60         Object dummy = new Pmd2Xml();\r
61     }\r
62 \r
63     /**\r
64      * 隠しコンストラクタ。\r
65      */\r
66     private Pmd2Xml(){\r
67         super();\r
68         assert this.getClass().equals(THISCLASS);\r
69         return;\r
70     }\r
71 \r
72     /**\r
73      * Mainエントリ。\r
74      * @param args コマンドパラメータ\r
75      */\r
76     public static void main(String[] args){\r
77         checkJRE();\r
78 \r
79         String inputFile = null;\r
80         String outputFile = null;\r
81         boolean pmd2xml = false;\r
82         boolean xml2pmd = false;\r
83         boolean force = false;\r
84         int argsLen = args.length;\r
85         for(int argIdx = 0; argIdx < argsLen; argIdx++){\r
86             String arg = args[argIdx];\r
87 \r
88             if(arg.equals("-h")){\r
89                 putHelp();\r
90             }else if(arg.equals("-pmd2xml")){\r
91                 pmd2xml = true;\r
92                 xml2pmd = false;\r
93             }else if(arg.equals("-xml2pmd")){\r
94                 pmd2xml = false;\r
95                 xml2pmd = true;\r
96             }else if(arg.equals("-i")){\r
97                 if(++argIdx >= argsLen){\r
98                     System.err.println("ERROR:");\r
99                     System.err.println("You need -i argument.");\r
100                     System.err.println("(-h for help)");\r
101                     System.exit(5);\r
102                 }\r
103                 inputFile = args[argIdx];\r
104             }else if(arg.equals("-o")){\r
105                 if(++argIdx >= argsLen){\r
106                     System.err.println("ERROR:");\r
107                     System.err.println("You need -o argument.");\r
108                     System.err.println("(-h for help)");\r
109                     System.exit(5);\r
110                 }\r
111                 outputFile = args[argIdx];\r
112             }else if(arg.equals("-f")){\r
113                 force = true;\r
114             }else{\r
115                 System.err.println("ERROR:");\r
116                 System.err.println("Unknown option:"+arg);\r
117                 System.err.println("(-h for help)");\r
118                 System.exit(5);\r
119             }\r
120         }\r
121 \r
122         if( ( ! pmd2xml) && ( ! xml2pmd) ){\r
123             System.err.println("ERROR:");\r
124             System.err.println("You must specify -pmd2xml or -xml2pmd.");\r
125             System.err.println("(-h for help)");\r
126             System.exit(5);\r
127         }\r
128 \r
129         if(inputFile == null){\r
130             System.err.println("ERROR:");\r
131             System.err.println("You must specify input file with -i.");\r
132             System.err.println("(-h for help)");\r
133             System.exit(5);\r
134         }\r
135 \r
136         if(outputFile == null){\r
137             System.err.println("ERROR:");\r
138             System.err.println("You must specify output file with -o.");\r
139             System.err.println("(-h for help)");\r
140             System.exit(5);\r
141         }\r
142 \r
143         File iFile = new File(inputFile);\r
144         if( (! iFile.exists()) || (! iFile.isFile()) ){\r
145             System.err.println("ERROR:");\r
146             System.err.println("Can't find input file:"\r
147                     + iFile.getAbsolutePath());\r
148             System.err.println("(-h for help)");\r
149             System.exit(1);\r
150         }\r
151 \r
152         if( ! force ){\r
153             File oFile = new File(outputFile);\r
154             if(oFile.exists()){\r
155                 System.err.println("ERROR:");\r
156                 System.err.println(oFile.getAbsolutePath()\r
157                         + " already exists.");\r
158                 System.err.println("If you want to overwrite, use -f.");\r
159                 System.err.println("(-h for help)");\r
160                 System.exit(1);\r
161             }\r
162         }else{\r
163             File oFile = new File(outputFile);\r
164             if(oFile.exists()){\r
165                 if( ! oFile.isFile()){\r
166                     System.err.println("ERROR:");\r
167                     System.err.println(oFile.getAbsolutePath()\r
168                             + " is not file.");\r
169                     System.err.println("(-h for help)");\r
170                     System.exit(1);\r
171                 }\r
172             }\r
173         }\r
174 \r
175         try{\r
176             if(pmd2xml) pmd2xml(inputFile, outputFile);\r
177             else        xml2pmd(inputFile, outputFile);\r
178         }catch(IOException e){\r
179             ioError(e);\r
180         }catch(ParserConfigurationException e){\r
181             internalError(e);\r
182         }catch(IllegalPmdException e){\r
183             internalError(e);\r
184         }catch(MmdFormatException e){\r
185             pmdError(e);\r
186         }catch(TogaXmlException e){\r
187             xmlError(e);\r
188         }catch(SAXException e){\r
189             xmlError(e);\r
190         }\r
191 \r
192         System.exit(0);\r
193 \r
194         return;\r
195     }\r
196 \r
197     /**\r
198      * 入出力エラー処理。\r
199      * 例外を出力してVM終了する。\r
200      * @param ex 例外\r
201      */\r
202     private static void ioError(Throwable ex){\r
203         System.err.println(ex);\r
204         System.exit(1);\r
205     }\r
206 \r
207     /**\r
208      * XML構文エラー処理。\r
209      * 例外を出力してVM終了する。\r
210      * @param ex 例外\r
211      */\r
212     private static void xmlError(Throwable ex){\r
213         System.err.println(ex);\r
214         System.exit(2);\r
215     }\r
216 \r
217     /**\r
218      * PMDファイルフォーマットエラー処理。\r
219      * 例外を出力してVM終了する。\r
220      * @param ex 例外\r
221      */\r
222     private static void pmdError(Throwable ex){\r
223         System.err.println(ex);\r
224         ex.printStackTrace(System.err);\r
225         System.exit(3);\r
226     }\r
227 \r
228     /**\r
229      * 内部エラー処理。\r
230      * 例外を出力してVM終了する。\r
231      * @param ex 例外\r
232      */\r
233     private static void internalError(Throwable ex){\r
234         System.err.println(ex);\r
235         ex.printStackTrace(System.err);\r
236         System.exit(4);\r
237     }\r
238 \r
239     /**\r
240      * JREのバージョン判定を行う。\r
241      * 不適切ならVMごと終了。\r
242      */\r
243     private static void checkJRE(){\r
244         Package jrePackage = java.lang.Object.class.getPackage();\r
245         if( ! jrePackage.isCompatibleWith("1.6")){\r
246             System.err.println("You need JRE 1.6 or later.");\r
247             System.exit(4);\r
248         }\r
249         return;\r
250     }\r
251 \r
252     /**\r
253      * ヘルプメッセージを出力してVMを終了させる。\r
254      */\r
255     private static void putHelp(){\r
256         System.err.println(APPNAME + ' ' + APPVER );\r
257         System.err.println("  License : " + APPLICENSE);\r
258         System.err.println("  http://mikutoga.sourceforge.jp/");\r
259         System.err.println();\r
260         System.err.println("-h       : put help massage");\r
261         System.err.println("-pmd2xml : convert *.pmd to *.xml");\r
262         System.err.println("-xml2pmd : convert *.xml to *.pmd");\r
263         System.err.println("-i file  : specify input file");\r
264         System.err.println("-o file  : specify output file");\r
265         System.err.println("-f       : force overwriting");\r
266         System.exit(0);\r
267         return;\r
268     }\r
269 \r
270     /**\r
271      * PMD->XML変換を行う。\r
272      * @param inputFile 入力ファイル名\r
273      * @param outputFile 出力ファイル名\r
274      * @throws IOException 入出力エラー\r
275      * @throws MmdFormatException 不正なPMDファイル\r
276      * @throws IllegalPmdException 不正なモデルデータ\r
277      */\r
278     private static void pmd2xml(String inputFile, String outputFile)\r
279             throws IOException, MmdFormatException, IllegalPmdException{\r
280         File iFile = new File(inputFile);\r
281         InputStream is = new FileInputStream(iFile);\r
282         is = new BufferedInputStream(is);\r
283         PmdModel model = pmdRead(is);\r
284         is.close();\r
285 \r
286         File oFile = new File(outputFile);\r
287         trunc(oFile);\r
288         OutputStream ostream;\r
289         ostream = new FileOutputStream(oFile, false);\r
290         ostream = new BufferedOutputStream(ostream);\r
291         xmlOut(model, ostream);\r
292         ostream.close();\r
293 \r
294         return;\r
295     }\r
296 \r
297     /**\r
298      * XML->PMD変換を行う。\r
299      * @param inputFile 入力ファイル名\r
300      * @param outputFile 出力ファイル名\r
301      * @throws IOException 入出力エラー\r
302      * @throws ParserConfigurationException XML構成のエラー\r
303      * @throws SAXException 不正なXMLファイル\r
304      * @throws TogaXmlException 不正なXMLファイル\r
305      * @throws IllegalPmdException 不正なPMDモデルデータ\r
306      */\r
307     private static void xml2pmd(String inputFile, String outputFile)\r
308             throws IOException,\r
309                    ParserConfigurationException,\r
310                    SAXException,\r
311                    TogaXmlException,\r
312                    IllegalPmdException {\r
313         File iFile = new File(inputFile);\r
314         InputStream is = new FileInputStream(iFile);\r
315         is = new BufferedInputStream(is);\r
316         InputSource source = new InputSource(is);\r
317         PmdModel model = xmlRead(source);\r
318         is.close();\r
319 \r
320         File oFile = new File(outputFile);\r
321         trunc(oFile);\r
322         OutputStream ostream;\r
323         ostream = new FileOutputStream(oFile, false);\r
324         ostream = new BufferedOutputStream(ostream);\r
325         pmdOut(model, ostream);\r
326         ostream.close();\r
327 \r
328         return;\r
329     }\r
330 \r
331     /**\r
332      * ファイルサイズを0に切り詰める。\r
333      * @param file ファイル\r
334      * @throws IOException 入出力エラー\r
335      */\r
336     private static void trunc(File file) throws IOException{\r
337         if( ! file.exists() ) return;\r
338         if( ! file.isFile() ) return;\r
339 \r
340         FileOutputStream foStream = new FileOutputStream(file);\r
341         FileChannel channnel = foStream.getChannel();\r
342         channnel.truncate(0);\r
343 \r
344         channnel.close();\r
345         foStream.close();\r
346 \r
347         return;\r
348     }\r
349 \r
350     /**\r
351      * PMDファイルからモデルデータを読み込む。\r
352      * @param is 入力ストリーム\r
353      * @return モデルデータ\r
354      * @throws IOException 入力エラー\r
355      * @throws MmdFormatException 不正なPMDファイルフォーマット\r
356      */\r
357     private static PmdModel pmdRead(InputStream is)\r
358             throws IOException, MmdFormatException{\r
359         MmdSource source = new MmdSource(is);\r
360         PmdLoader loader = new PmdLoader(source);\r
361 \r
362         PmdModel model = loader.load();\r
363 \r
364         return model;\r
365     }\r
366 \r
367     /**\r
368      * XMLファイルからモデルデータを読み込む。\r
369      * @param source 入力ソース\r
370      * @return モデルデータ\r
371      * @throws IOException 入力エラー\r
372      * @throws ParserConfigurationException XML構成エラー\r
373      * @throws SAXException XML構文エラー\r
374      * @throws TogaXmlException 不正なXMLデータ\r
375      */\r
376     private static PmdModel xmlRead(InputSource source)\r
377             throws IOException,\r
378                    ParserConfigurationException,\r
379                    SAXException,\r
380                    TogaXmlException {\r
381         DocumentBuilder builder =\r
382                 PmdXmlResources.newBuilder(XmlHandler.HANDLER);\r
383         Xml2PmdLoader loader = new Xml2PmdLoader(builder);\r
384 \r
385         PmdModel model = loader.parse(source);\r
386 \r
387         return model;\r
388     }\r
389 \r
390     /**\r
391      * モデルデータをPMDファイルに出力する。\r
392      * @param model モデルデータ\r
393      * @param ostream 出力ストリーム\r
394      * @throws IOException 出力エラー\r
395      * @throws IllegalPmdException 不正なモデルデータ\r
396      */\r
397     private static void pmdOut(PmdModel model, OutputStream ostream)\r
398             throws IOException, IllegalPmdException{\r
399         PmdExporter exporter = new PmdExporter(ostream);\r
400         exporter.dumpPmdModel(model);\r
401         ostream.close();\r
402         return;\r
403     }\r
404 \r
405     /**\r
406      * モデルデータをXMLファイルに出力する。\r
407      * @param model モデルデータ\r
408      * @param ostream 出力ストリーム\r
409      * @throws IOException 出力エラー\r
410      * @throws IllegalPmdException 不正なモデルデータ\r
411      */\r
412     private static void xmlOut(PmdModel model, OutputStream ostream)\r
413             throws IOException, IllegalPmdException{\r
414         PmdXmlExporter exporter = new PmdXmlExporter(ostream);\r
415         exporter.setNewLine("\r\n");\r
416         exporter.setGenerator(APPNAME + ' ' + APPVER);\r
417         exporter.putPmdModel(model);\r
418         exporter.close();\r
419         return;\r
420     }\r
421 \r
422 }\r