OSDN Git Service

e52c92247ac31a2c6ed233fbe9a07ea74e633f4f
[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(1);\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(1);\r
110                 }\r
111                 outputFile = args[argIdx];\r
112             }else if(arg.equals("-f")){\r
113                 force = true;\r
114             }\r
115         }\r
116 \r
117         if( ( ! pmd2xml) && ( ! xml2pmd) ){\r
118             System.err.println("ERROR:");\r
119             System.err.println("You must specify -pmd2xml or -xml2pmd.");\r
120             System.err.println("(-h for help)");\r
121             System.exit(1);\r
122         }\r
123 \r
124         if(inputFile == null){\r
125             System.err.println("ERROR:");\r
126             System.err.println("You must specify input file with -i.");\r
127             System.err.println("(-h for help)");\r
128             System.exit(1);\r
129         }\r
130 \r
131         if(outputFile == null){\r
132             System.err.println("ERROR:");\r
133             System.err.println("You must specify output file with -o.");\r
134             System.err.println("(-h for help)");\r
135             System.exit(1);\r
136         }\r
137 \r
138         File iFile = new File(inputFile);\r
139         if( (! iFile.exists()) || (! iFile.isFile()) ){\r
140             System.err.println("ERROR:");\r
141             System.err.println("Can't find input file:"\r
142                     + iFile.getAbsolutePath());\r
143             System.err.println("(-h for help)");\r
144             System.exit(1);\r
145         }\r
146 \r
147         if( ! force ){\r
148             File oFile = new File(outputFile);\r
149             if(oFile.exists()){\r
150                 System.err.println("ERROR:");\r
151                 System.err.println(oFile.getAbsolutePath()\r
152                         + " already exists.");\r
153                 System.err.println("If you want to overwrite, use -f.");\r
154                 System.err.println("(-h for help)");\r
155                 System.exit(1);\r
156             }\r
157         }else{\r
158             File oFile = new File(outputFile);\r
159             if(oFile.exists()){\r
160                 if( ! oFile.isFile()){\r
161                     System.err.println("ERROR:");\r
162                     System.err.println(oFile.getAbsolutePath()\r
163                             + " is not file.");\r
164                     System.err.println("(-h for help)");\r
165                     System.exit(1);\r
166                 }\r
167             }\r
168         }\r
169 \r
170         try{\r
171             if(pmd2xml) pmd2xml(inputFile, outputFile);\r
172             else        xml2pmd(inputFile, outputFile);\r
173         }catch(IOException e){\r
174             ioError(e);\r
175         }catch(ParserConfigurationException e){\r
176             internalError(e);\r
177         }catch(IllegalPmdException e){\r
178             internalError(e);\r
179         }catch(MmdFormatException e){\r
180             pmdError(e);\r
181         }catch(TogaXmlException e){\r
182             xmlError(e);\r
183         }catch(SAXException e){\r
184             xmlError(e);\r
185         }\r
186 \r
187         System.exit(0);\r
188 \r
189         return;\r
190     }\r
191 \r
192     /**\r
193      * 入出力エラー処理。\r
194      * 例外を出力してVM終了する。\r
195      * @param ex 例外\r
196      */\r
197     private static void ioError(Throwable ex){\r
198         System.err.println(ex);\r
199         System.exit(1);\r
200     }\r
201 \r
202     /**\r
203      * XML構文エラー処理。\r
204      * 例外を出力してVM終了する。\r
205      * @param ex 例外\r
206      */\r
207     private static void xmlError(Throwable ex){\r
208         System.err.println(ex);\r
209         System.exit(2);\r
210     }\r
211 \r
212     /**\r
213      * PMDファイルフォーマットエラー処理。\r
214      * 例外を出力してVM終了する。\r
215      * @param ex 例外\r
216      */\r
217     private static void pmdError(Throwable ex){\r
218         System.err.println(ex);\r
219         ex.printStackTrace(System.err);\r
220         System.exit(3);\r
221     }\r
222 \r
223     /**\r
224      * 内部エラー処理。\r
225      * 例外を出力してVM終了する。\r
226      * @param ex 例外\r
227      */\r
228     private static void internalError(Throwable ex){\r
229         System.err.println(ex);\r
230         ex.printStackTrace(System.err);\r
231         System.exit(4);\r
232     }\r
233 \r
234     /**\r
235      * JREのバージョン判定を行う。\r
236      * 不適切ならVMごと終了。\r
237      */\r
238     private static void checkJRE(){\r
239         Package jrePackage = java.lang.Object.class.getPackage();\r
240         if( ! jrePackage.isCompatibleWith("1.6")){\r
241             System.err.println("You need JRE 1.6 or later.");\r
242             System.exit(1);\r
243         }\r
244         return;\r
245     }\r
246 \r
247     /**\r
248      * ヘルプメッセージを出力してVMを終了させる。\r
249      */\r
250     private static void putHelp(){\r
251         System.err.println(APPNAME + ' ' + APPVER );\r
252         System.err.println("  License : " + APPLICENSE);\r
253         System.err.println("  http://mikutoga.sourceforge.jp/");\r
254         System.err.println();\r
255         System.err.println("-h       : put help massage");\r
256         System.err.println("-pmd2xml : convert *.pmd to *.xml");\r
257         System.err.println("-xml2pmd : convert *.xml to *.pmd");\r
258         System.err.println("-i file  : specify input file");\r
259         System.err.println("-o file  : specify output file");\r
260         System.err.println("-f       : force overwriting");\r
261         System.exit(0);\r
262         return;\r
263     }\r
264 \r
265     /**\r
266      * PMD->XML変換を行う。\r
267      * @param inputFile 入力ファイル名\r
268      * @param outputFile 出力ファイル名\r
269      * @throws IOException 入出力エラー\r
270      * @throws MmdFormatException 不正なPMDファイル\r
271      * @throws IllegalPmdException 不正なモデルデータ\r
272      */\r
273     private static void pmd2xml(String inputFile, String outputFile)\r
274             throws IOException, MmdFormatException, IllegalPmdException{\r
275         File iFile = new File(inputFile);\r
276         InputStream is = new FileInputStream(iFile);\r
277         is = new BufferedInputStream(is);\r
278         PmdModel model = pmdRead(is);\r
279         is.close();\r
280 \r
281         File oFile = new File(outputFile);\r
282         trunc(oFile);\r
283         OutputStream ostream;\r
284         ostream = new FileOutputStream(oFile, false);\r
285         ostream = new BufferedOutputStream(ostream);\r
286         xmlOut(model, ostream);\r
287         ostream.close();\r
288 \r
289         return;\r
290     }\r
291 \r
292     /**\r
293      * XML->PMD変換を行う。\r
294      * @param inputFile 入力ファイル名\r
295      * @param outputFile 出力ファイル名\r
296      * @throws IOException 入出力エラー\r
297      * @throws ParserConfigurationException XML構成のエラー\r
298      * @throws SAXException 不正なXMLファイル\r
299      * @throws TogaXmlException 不正なXMLファイル\r
300      * @throws IllegalPmdException 不正なPMDモデルデータ\r
301      */\r
302     private static void xml2pmd(String inputFile, String outputFile)\r
303             throws IOException,\r
304                    ParserConfigurationException,\r
305                    SAXException,\r
306                    TogaXmlException,\r
307                    IllegalPmdException {\r
308         File iFile = new File(inputFile);\r
309         InputStream is = new FileInputStream(iFile);\r
310         is = new BufferedInputStream(is);\r
311         InputSource source = new InputSource(is);\r
312         PmdModel model = xmlRead(source);\r
313         is.close();\r
314 \r
315         File oFile = new File(outputFile);\r
316         trunc(oFile);\r
317         OutputStream ostream;\r
318         ostream = new FileOutputStream(oFile, false);\r
319         ostream = new BufferedOutputStream(ostream);\r
320         pmdOut(model, ostream);\r
321         ostream.close();\r
322 \r
323         return;\r
324     }\r
325 \r
326     /**\r
327      * ファイルサイズを0に切り詰める。\r
328      * @param file ファイル\r
329      * @throws IOException 入出力エラー\r
330      */\r
331     private static void trunc(File file) throws IOException{\r
332         if( ! file.exists() ) return;\r
333         if( ! file.isFile() ) return;\r
334 \r
335         FileOutputStream foStream = new FileOutputStream(file);\r
336         FileChannel channnel = foStream.getChannel();\r
337         channnel.truncate(0);\r
338 \r
339         channnel.close();\r
340         foStream.close();\r
341 \r
342         return;\r
343     }\r
344 \r
345     /**\r
346      * PMDファイルからモデルデータを読み込む。\r
347      * @param is 入力ストリーム\r
348      * @return モデルデータ\r
349      * @throws IOException 入力エラー\r
350      * @throws MmdFormatException 不正なPMDファイルフォーマット\r
351      */\r
352     private static PmdModel pmdRead(InputStream is)\r
353             throws IOException, MmdFormatException{\r
354         MmdSource source = new MmdSource(is);\r
355         PmdLoader loader = new PmdLoader(source);\r
356 \r
357         PmdModel model = loader.load();\r
358 \r
359         return model;\r
360     }\r
361 \r
362     /**\r
363      * XMLファイルからモデルデータを読み込む。\r
364      * @param source 入力ソース\r
365      * @return モデルデータ\r
366      * @throws IOException 入力エラー\r
367      * @throws ParserConfigurationException XML構成エラー\r
368      * @throws SAXException XML構文エラー\r
369      * @throws TogaXmlException 不正なXMLデータ\r
370      */\r
371     private static PmdModel xmlRead(InputSource source)\r
372             throws IOException,\r
373                    ParserConfigurationException,\r
374                    SAXException,\r
375                    TogaXmlException {\r
376         DocumentBuilder builder =\r
377                 PmdXmlResources.newBuilder(XmlHandler.HANDLER);\r
378         Xml2PmdLoader loader = new Xml2PmdLoader(builder);\r
379 \r
380         PmdModel model = loader.parse(source);\r
381 \r
382         return model;\r
383     }\r
384 \r
385     /**\r
386      * モデルデータをPMDファイルに出力する。\r
387      * @param model モデルデータ\r
388      * @param ostream 出力ストリーム\r
389      * @throws IOException 出力エラー\r
390      * @throws IllegalPmdException 不正なモデルデータ\r
391      */\r
392     private static void pmdOut(PmdModel model, OutputStream ostream)\r
393             throws IOException, IllegalPmdException{\r
394         PmdExporter exporter = new PmdExporter(ostream);\r
395         exporter.dumpPmdModel(model);\r
396         ostream.close();\r
397         return;\r
398     }\r
399 \r
400     /**\r
401      * モデルデータをXMLファイルに出力する。\r
402      * @param model モデルデータ\r
403      * @param ostream 出力ストリーム\r
404      * @throws IOException 出力エラー\r
405      * @throws IllegalPmdException 不正なモデルデータ\r
406      */\r
407     private static void xmlOut(PmdModel model, OutputStream ostream)\r
408             throws IOException, IllegalPmdException{\r
409         PmdXmlExporter exporter = new PmdXmlExporter(ostream);\r
410         exporter.setNewLine("\r\n");\r
411         exporter.setGenerator(APPNAME + ' ' + APPVER);\r
412         exporter.putPmdModel(model);\r
413         exporter.close();\r
414         return;\r
415     }\r
416 \r
417 }\r