2 * pmd 2 xml converter main entry
\r
4 * License : The MIT License
\r
5 * Copyright(c) 2010 MikuToga Partners
\r
8 package jp.sourceforge.mikutoga.pmd2xml;
\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
36 * PMDモデルファイルとXMLとの間で変換を行うアプリケーション。
\r
38 public final class Pmd2Xml {
\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
46 THISCLASS = Pmd2Xml.class;
\r
48 THISCLASS.getResourceAsStream("resources/version.properties");
\r
49 Properties verProps = new Properties();
\r
52 }catch(IOException e){
\r
53 throw new ExceptionInInitializerError(e);
\r
56 APPNAME = verProps.getProperty("app.name");
\r
57 APPVER = verProps.getProperty("app.version");
\r
58 APPLICENSE = verProps.getProperty("app.license");
\r
60 Object dummy = new Pmd2Xml();
\r
68 assert this.getClass().equals(THISCLASS);
\r
74 * @param args コマンドパラメータ
\r
76 public static void main(String[] args){
\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
88 if(arg.equals("-h")){
\r
90 }else if(arg.equals("-pmd2xml")){
\r
93 }else if(arg.equals("-xml2pmd")){
\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
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
111 outputFile = args[argIdx];
\r
112 }else if(arg.equals("-f")){
\r
115 System.err.println("ERROR:");
\r
116 System.err.println("Unknown option:"+arg);
\r
117 System.err.println("(-h for help)");
\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
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
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
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
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
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
176 if(pmd2xml) pmd2xml(inputFile, outputFile);
\r
177 else xml2pmd(inputFile, outputFile);
\r
178 }catch(IOException e){
\r
180 }catch(ParserConfigurationException e){
\r
182 }catch(IllegalPmdException e){
\r
184 }catch(MmdFormatException e){
\r
186 }catch(TogaXmlException e){
\r
188 }catch(SAXException e){
\r
202 private static void ioError(Throwable ex){
\r
203 System.err.println(ex);
\r
212 private static void xmlError(Throwable ex){
\r
213 System.err.println(ex);
\r
218 * PMDファイルフォーマットエラー処理。
\r
222 private static void pmdError(Throwable ex){
\r
223 System.err.println(ex);
\r
224 ex.printStackTrace(System.err);
\r
233 private static void internalError(Throwable ex){
\r
234 System.err.println(ex);
\r
235 ex.printStackTrace(System.err);
\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
253 * ヘルプメッセージを出力してVMを終了させる。
\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
272 * @param inputFile 入力ファイル名
\r
273 * @param outputFile 出力ファイル名
\r
274 * @throws IOException 入出力エラー
\r
275 * @throws MmdFormatException 不正なPMDファイル
\r
276 * @throws IllegalPmdException 不正なモデルデータ
\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
286 File oFile = new File(outputFile);
\r
288 OutputStream ostream;
\r
289 ostream = new FileOutputStream(oFile, false);
\r
290 ostream = new BufferedOutputStream(ostream);
\r
291 xmlOut(model, ostream);
\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
307 private static void xml2pmd(String inputFile, String outputFile)
\r
308 throws IOException,
\r
309 ParserConfigurationException,
\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
320 File oFile = new File(outputFile);
\r
322 OutputStream ostream;
\r
323 ostream = new FileOutputStream(oFile, false);
\r
324 ostream = new BufferedOutputStream(ostream);
\r
325 pmdOut(model, ostream);
\r
334 * @throws IOException 入出力エラー
\r
336 private static void trunc(File file) throws IOException{
\r
337 if( ! file.exists() ) return;
\r
338 if( ! file.isFile() ) return;
\r
340 FileOutputStream foStream = new FileOutputStream(file);
\r
341 FileChannel channnel = foStream.getChannel();
\r
342 channnel.truncate(0);
\r
351 * PMDファイルからモデルデータを読み込む。
\r
352 * @param is 入力ストリーム
\r
354 * @throws IOException 入力エラー
\r
355 * @throws MmdFormatException 不正なPMDファイルフォーマット
\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
362 PmdModel model = loader.load();
\r
368 * XMLファイルからモデルデータを読み込む。
\r
369 * @param source 入力ソース
\r
371 * @throws IOException 入力エラー
\r
372 * @throws ParserConfigurationException XML構成エラー
\r
373 * @throws SAXException XML構文エラー
\r
374 * @throws TogaXmlException 不正なXMLデータ
\r
376 private static PmdModel xmlRead(InputSource source)
\r
377 throws IOException,
\r
378 ParserConfigurationException,
\r
381 DocumentBuilder builder =
\r
382 PmdXmlResources.newBuilder(XmlHandler.HANDLER);
\r
383 Xml2PmdLoader loader = new Xml2PmdLoader(builder);
\r
385 PmdModel model = loader.parse(source);
\r
391 * モデルデータをPMDファイルに出力する。
\r
392 * @param model モデルデータ
\r
393 * @param ostream 出力ストリーム
\r
394 * @throws IOException 出力エラー
\r
395 * @throws IllegalPmdException 不正なモデルデータ
\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
406 * モデルデータをXMLファイルに出力する。
\r
407 * @param model モデルデータ
\r
408 * @param ostream 出力ストリーム
\r
409 * @throws IOException 出力エラー
\r
410 * @throws IllegalPmdException 不正なモデルデータ
\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