From f57fc68a2f507a00703282471989b8790d77143a Mon Sep 17 00:00:00 2001 From: Olyutorskii Date: Wed, 18 May 2011 00:34:17 +0900 Subject: [PATCH] =?utf8?q?1.102.5-SNAPSHOT=E9=96=8B=E7=99=BA=E9=96=8B?= =?utf8?q?=E5=A7=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .hgignore | 9 + CHANGELOG.txt | 28 +- LICENSE.txt | 68 +- README.txt | 160 ++-- build.xml | 2 +- pom.xml | 122 ++- .../jp/sourceforge/mikutoga/pmd2xml/Pmd2Xml.java | 844 ++++++++++----------- .../sourceforge/mikutoga/pmd2xml/XmlHandler.java | 126 +-- .../sourceforge/mikutoga/pmd2xml/package-info.java | 30 +- 9 files changed, 728 insertions(+), 661 deletions(-) diff --git a/.hgignore b/.hgignore index 14f3cd2..7454a09 100644 --- a/.hgignore +++ b/.hgignore @@ -1,3 +1,12 @@ +syntax: regexp + +\.orig$ +\.orig\..*$ +\.chg\..*$ +\.rej$ +\.conflict\~$ +^maven-build\.properties$ +^maven-build\.xml$ ^nb-configuration\.xml$ ^nbactions\.xml$ ^target$ diff --git a/CHANGELOG.txt b/CHANGELOG.txt index 9a8764c..bf60f49 100644 --- a/CHANGELOG.txt +++ b/CHANGELOG.txt @@ -1,14 +1,14 @@ -[UTF-8 Japanese] - - -Pmd2XML 変更履歴 - - -1.102.4 (2010-10-09) - ・TogaGem1.105.2のバグ修正(接続ボーンを持たない剛体の件)に対処。 - -1.101.2 (2010-10-08) - ・初回リリース。 - - ---- EOF --- +[UTF-8 Japanese] + + +Pmd2XML 変更履歴 + + +1.102.4 (2010-10-09) + ・TogaGem1.105.2のバグ修正(接続ボーンを持たない剛体の件)に対処。 + +1.101.2 (2010-10-08) + ・初回リリース。 + + +--- EOF --- diff --git a/LICENSE.txt b/LICENSE.txt index 5280b80..8888105 100644 --- a/LICENSE.txt +++ b/LICENSE.txt @@ -1,34 +1,34 @@ -[UTF-8 English & Japanese] - -The MIT License - - -Copyright(c) 2010 olyutorskii - - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in -all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -THE SOFTWARE. - - -Pmd2XMLオリジナル制作者自身からのコメント: - - ※ 少なくともこのソフトウェアの実行、複製、配布、改造は自由です。 - ※ 少なくともこのソフトウェアは無保証です。 - - ---- EOF --- +[UTF-8 English & Japanese] + +The MIT License + + +Copyright(c) 2010 olyutorskii + + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. + + +Pmd2XMLオリジナル制作者自身からのコメント: + + ※ 少なくともこのソフトウェアの実行、複製、配布、改造は自由です。 + ※ 少なくともこのソフトウェアは無保証です。 + + +--- EOF --- diff --git a/README.txt b/README.txt index 8d9f439..3eff83c 100644 --- a/README.txt +++ b/README.txt @@ -1,80 +1,80 @@ -[UTF-8 Japanese] - - P m d 2 X M L - Readme - - Copyright(c) 2010 olyutorskii - - -=== Pmd2XMLとは === - -Pmd2XMLは、3D動画制作ツール、MikuMikuDance(MMD)で用いられる -モデルデータファイル(*.pmd)の内容を、XML形式のデータファイルと交換するための -アプリケーションです。 -Pmd2XMLは、MikuTogaプロジェクトの派生物として誕生しました。 - -MikuTogaプロジェクトは、MMDによる3Dアニメーション制作を支援するプログラムの -整備のために発足した、オープンソースプロジェクトです。 - -Pmd2XMLは、同じMikuTogaプロジェクト内のTogaGemライブラリを用いて -開発が進められています。 - -※ MMD開発者の樋口M氏は、MikuTogaの開発活動に一切関与していません。 -  Pmd2XMLに関する問い合わせをを樋口M氏へ投げかけないように!約束だよ! - - -=== 実行環境 === - - - Pmd2XMLはJava言語(JLS3)で記述されたプログラムです。 - - Pmd2XMLはJRE1.6に準拠したJava実行環境で利用できるように作られています。 - 原則として、JRE1.6に準拠した実行系であれば、プラットフォームを選びません。 - - -=== アーカイブ管理体制 === - - このアーカイブは、UTF-8による開発環境を前提として構成されています。 - このアーカイブの原本となる開発資産は、 - http://hg.sourceforge.jp/view/mikutoga/Pmd2XML - を上位に持つMercurialリポジトリで管理されています。 - アーカイブの代わりにMercurialを通じての開発資産へのアクセスが可能です。 - # hg clone http://hg.sourceforge.jp/view/mikutoga/Pmd2XML - - -=== 開発プロジェクト運営元 === - - http://sourceforge.jp/projects/mikutoga/ まで。 - - -=== ディレクトリ内訳構成 === - -基本的にはMaven2のmaven-archetype-quickstart構成に準じます。 - -./README.txt - あなたが今見てるこれ。 - -./CHANGELOG.txt - 変更履歴。 - -./LICENSE.txt - ライセンスに関して。 - -./pom.xml - Maven2用プロジェクト構成定義ファイル。 - -./src/main/java/ - Javaのソースコード。 - -./src/test/java/ - JUnit 4.* 用のユニットテストコード。 - -./src/main/config/ - 各種ビルド・構成管理に必要なファイル群。 - -./src/main/config/checks.xml - Checkstyle用configファイル。 - -./src/main/config/pmdrules.xml - PMD用ルール定義ファイル。 - - ---- EOF --- +[UTF-8 Japanese] + + P m d 2 X M L + Readme + + Copyright(c) 2010 olyutorskii + + +=== Pmd2XMLとは === + +Pmd2XMLは、3D動画制作ツール、MikuMikuDance(MMD)で用いられる +モデルデータファイル(*.pmd)の内容を、XML形式のデータファイルと交換するための +アプリケーションです。 +Pmd2XMLは、MikuTogaプロジェクトの派生物として誕生しました。 + +MikuTogaプロジェクトは、MMDによる3Dアニメーション制作を支援するプログラムの +整備のために発足した、オープンソースプロジェクトです。 + +Pmd2XMLは、同じMikuTogaプロジェクト内のTogaGemライブラリを用いて +開発が進められています。 + +※ MMD開発者の樋口M氏は、MikuTogaの開発活動に一切関与していません。 +  Pmd2XMLに関する問い合わせをを樋口M氏へ投げかけないように!約束だよ! + + +=== 実行環境 === + + - Pmd2XMLはJava言語(JLS3)で記述されたプログラムです。 + - Pmd2XMLはJRE1.6に準拠したJava実行環境で利用できるように作られています。 + 原則として、JRE1.6に準拠した実行系であれば、プラットフォームを選びません。 + + +=== アーカイブ管理体制 === + + このアーカイブは、UTF-8による開発環境を前提として構成されています。 + このアーカイブの原本となる開発資産は、 + http://hg.sourceforge.jp/view/mikutoga/Pmd2XML + を上位に持つMercurialリポジトリで管理されています。 + アーカイブの代わりにMercurialを通じての開発資産へのアクセスが可能です。 + # hg clone http://hg.sourceforge.jp/view/mikutoga/Pmd2XML + + +=== 開発プロジェクト運営元 === + + http://sourceforge.jp/projects/mikutoga/ まで。 + + +=== ディレクトリ内訳構成 === + +基本的にはMaven2のmaven-archetype-quickstart構成に準じます。 + +./README.txt + あなたが今見てるこれ。 + +./CHANGELOG.txt + 変更履歴。 + +./LICENSE.txt + ライセンスに関して。 + +./pom.xml + Maven2用プロジェクト構成定義ファイル。 + +./src/main/java/ + Javaのソースコード。 + +./src/test/java/ + JUnit 4.* 用のユニットテストコード。 + +./src/main/config/ + 各種ビルド・構成管理に必要なファイル群。 + +./src/main/config/checks.xml + Checkstyle用configファイル。 + +./src/main/config/pmdrules.xml + PMD用ルール定義ファイル。 + + +--- EOF --- diff --git a/build.xml b/build.xml index 15e4563..321ecbd 100644 --- a/build.xml +++ b/build.xml @@ -36,7 +36,7 @@ /> 4.0.0 + jp.sourceforge.mikutoga pmd2xml - 1.102.4 + 1.102.5-SNAPSHOT jar Pmd2XML @@ -31,7 +32,7 @@ MikuToga Partners - http://sourceforge.jp/projects/mikutoga/ + http://sourceforge.jp/projects/mikutoga/devel/ @@ -59,7 +60,7 @@ - 2.1 + 2.2 @@ -91,10 +92,18 @@ UTF-8 ${project.basedir}/src/main/config + org.apache.maven.plugins.shade.resource.ManifestResourceTransformer jp.sourceforge.mikutoga.pmd2xml.Pmd2Xml + + + ${maven.compiler.source} + + + ${project.myrepoconf}/checks.xml + @@ -104,7 +113,7 @@ junit junit - [4,) + [4.8.2,) test @@ -127,21 +136,8 @@ org.apache.maven.plugins - maven-compiler-plugin - - 1.6 - 1.6 - true - true - - - - - - - - org.apache.maven.plugins maven-clean-plugin + 2.4.1 @@ -158,7 +154,23 @@ org.apache.maven.plugins + maven-compiler-plugin + 2.3.2 + + 1.6 + 1.6 + true + true + + + + + + + + org.apache.maven.plugins maven-jar-plugin + 2.3.1 @@ -172,6 +184,7 @@ org.apache.maven.plugins maven-assembly-plugin + 2.2.1 src/main/assembly/descriptor.xml @@ -182,6 +195,7 @@ org.apache.maven.plugins maven-shade-plugin + 1.4 package @@ -205,10 +219,11 @@ org.apache.maven.plugins maven-enforcer-plugin + 1.0 - [2.1,3) + [2.2,3) [1.6,) @@ -217,6 +232,29 @@ + + org.apache.maven.plugins + maven-source-plugin + 2.1.2 + + true + + + ${project.organization.name} + + + + + + attach-sources + verify + + jar-no-fork + + + + + @@ -232,7 +270,12 @@ src/main/resources + **/*.css + **/*.html + **/*.png **/*.properties + **/*.txt + **/*.xml **/*.xsd **/*.dtd @@ -246,33 +289,38 @@ + org.apache.maven.plugins - maven-javadoc-plugin - 2.7 + maven-site-plugin + 2.2 - protected + ja + true + true + ${project.build.sourceEncoding} + ${project.reporting.outputEncoding} - maven-site-plugin - 2.1.1 + org.apache.maven.plugins + maven-javadoc-plugin + 2.8 - ja - ${project.build.sourceEncoding} - ${project.reporting.outputEncoding} + protected org.apache.maven.plugins maven-checkstyle-plugin - 2.5 + 2.6 - ${project.myrepoconf}/checks.xml + ${checkstyle.config.location} + UTF-8 @@ -281,18 +329,25 @@ maven-pmd-plugin 2.5 - UTF-8 - 1.6 + ${maven.compiler.target} ${project.myrepoconf}/pmdrules.xml + + + + pmd + cpd + + + org.codehaus.mojo findbugs-maven-plugin - 2.3.1 + 2.3.2 Max Low @@ -321,8 +376,11 @@ + + + diff --git a/src/main/java/jp/sourceforge/mikutoga/pmd2xml/Pmd2Xml.java b/src/main/java/jp/sourceforge/mikutoga/pmd2xml/Pmd2Xml.java index 75b110f..400a8e9 100644 --- a/src/main/java/jp/sourceforge/mikutoga/pmd2xml/Pmd2Xml.java +++ b/src/main/java/jp/sourceforge/mikutoga/pmd2xml/Pmd2Xml.java @@ -1,422 +1,422 @@ -/* - * pmd 2 xml converter main entry - * - * License : The MIT License - * Copyright(c) 2010 MikuToga Partners - */ - -package jp.sourceforge.mikutoga.pmd2xml; - -import java.io.BufferedInputStream; -import java.io.BufferedOutputStream; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.nio.channels.FileChannel; -import java.util.Properties; -import javax.xml.parsers.DocumentBuilder; -import javax.xml.parsers.ParserConfigurationException; -import jp.sourceforge.mikutoga.parser.MmdFormatException; -import jp.sourceforge.mikutoga.parser.MmdSource; -import jp.sourceforge.mikutoga.pmd.PmdModel; -import jp.sourceforge.mikutoga.pmd.pmdexporter.IllegalPmdException; -import jp.sourceforge.mikutoga.pmd.pmdexporter.PmdExporter; -import jp.sourceforge.mikutoga.pmd.pmdloader.PmdLoader; -import jp.sourceforge.mikutoga.pmd.xml.PmdXmlExporter; -import jp.sourceforge.mikutoga.pmd.xml.PmdXmlResources; -import jp.sourceforge.mikutoga.pmd.xml.Xml2PmdLoader; -import jp.sourceforge.mikutoga.xml.TogaXmlException; -import org.xml.sax.InputSource; -import org.xml.sax.SAXException; - -/** - * PMDモデルファイルとXMLとの間で変換を行うアプリケーション。 - */ -public final class Pmd2Xml { - - private static final Class THISCLASS; - private static final String APPNAME; - private static final String APPVER; - private static final String APPLICENSE; - - static{ - THISCLASS = Pmd2Xml.class; - InputStream ver = - THISCLASS.getResourceAsStream("resources/version.properties"); - Properties verProps = new Properties(); - try{ - verProps.load(ver); - }catch(IOException e){ - throw new ExceptionInInitializerError(e); - } - - APPNAME = verProps.getProperty("app.name"); - APPVER = verProps.getProperty("app.version"); - APPLICENSE = verProps.getProperty("app.license"); - - Object dummy = new Pmd2Xml(); - } - - /** - * 隠しコンストラクタ。 - */ - private Pmd2Xml(){ - super(); - assert this.getClass().equals(THISCLASS); - return; - } - - /** - * Mainエントリ。 - * @param args コマンドパラメータ - */ - public static void main(String[] args){ - checkJRE(); - - String inputFile = null; - String outputFile = null; - boolean pmd2xml = false; - boolean xml2pmd = false; - boolean force = false; - int argsLen = args.length; - for(int argIdx = 0; argIdx < argsLen; argIdx++){ - String arg = args[argIdx]; - - if(arg.equals("-h")){ - putHelp(); - }else if(arg.equals("-pmd2xml")){ - pmd2xml = true; - xml2pmd = false; - }else if(arg.equals("-xml2pmd")){ - pmd2xml = false; - xml2pmd = true; - }else if(arg.equals("-i")){ - if(++argIdx >= argsLen){ - System.err.println("ERROR:"); - System.err.println("You need -i argument."); - System.err.println("(-h for help)"); - System.exit(5); - } - inputFile = args[argIdx]; - }else if(arg.equals("-o")){ - if(++argIdx >= argsLen){ - System.err.println("ERROR:"); - System.err.println("You need -o argument."); - System.err.println("(-h for help)"); - System.exit(5); - } - outputFile = args[argIdx]; - }else if(arg.equals("-f")){ - force = true; - }else{ - System.err.println("ERROR:"); - System.err.println("Unknown option:"+arg); - System.err.println("(-h for help)"); - System.exit(5); - } - } - - if( ( ! pmd2xml) && ( ! xml2pmd) ){ - System.err.println("ERROR:"); - System.err.println("You must specify -pmd2xml or -xml2pmd."); - System.err.println("(-h for help)"); - System.exit(5); - } - - if(inputFile == null){ - System.err.println("ERROR:"); - System.err.println("You must specify input file with -i."); - System.err.println("(-h for help)"); - System.exit(5); - } - - if(outputFile == null){ - System.err.println("ERROR:"); - System.err.println("You must specify output file with -o."); - System.err.println("(-h for help)"); - System.exit(5); - } - - File iFile = new File(inputFile); - if( (! iFile.exists()) || (! iFile.isFile()) ){ - System.err.println("ERROR:"); - System.err.println("Can't find input file:" - + iFile.getAbsolutePath()); - System.err.println("(-h for help)"); - System.exit(1); - } - - if( ! force ){ - File oFile = new File(outputFile); - if(oFile.exists()){ - System.err.println("ERROR:"); - System.err.println(oFile.getAbsolutePath() - + " already exists."); - System.err.println("If you want to overwrite, use -f."); - System.err.println("(-h for help)"); - System.exit(1); - } - }else{ - File oFile = new File(outputFile); - if(oFile.exists()){ - if( ! oFile.isFile()){ - System.err.println("ERROR:"); - System.err.println(oFile.getAbsolutePath() - + " is not file."); - System.err.println("(-h for help)"); - System.exit(1); - } - } - } - - try{ - if(pmd2xml) pmd2xml(inputFile, outputFile); - else xml2pmd(inputFile, outputFile); - }catch(IOException e){ - ioError(e); - }catch(ParserConfigurationException e){ - internalError(e); - }catch(IllegalPmdException e){ - internalError(e); - }catch(MmdFormatException e){ - pmdError(e); - }catch(TogaXmlException e){ - xmlError(e); - }catch(SAXException e){ - xmlError(e); - } - - System.exit(0); - - return; - } - - /** - * 入出力エラー処理。 - * 例外を出力してVM終了する。 - * @param ex 例外 - */ - private static void ioError(Throwable ex){ - System.err.println(ex); - System.exit(1); - } - - /** - * XML構文エラー処理。 - * 例外を出力してVM終了する。 - * @param ex 例外 - */ - private static void xmlError(Throwable ex){ - System.err.println(ex); - System.exit(2); - } - - /** - * PMDファイルフォーマットエラー処理。 - * 例外を出力してVM終了する。 - * @param ex 例外 - */ - private static void pmdError(Throwable ex){ - System.err.println(ex); - ex.printStackTrace(System.err); - System.exit(3); - } - - /** - * 内部エラー処理。 - * 例外を出力してVM終了する。 - * @param ex 例外 - */ - private static void internalError(Throwable ex){ - System.err.println(ex); - ex.printStackTrace(System.err); - System.exit(4); - } - - /** - * JREのバージョン判定を行う。 - * 不適切ならVMごと終了。 - */ - private static void checkJRE(){ - Package jrePackage = java.lang.Object.class.getPackage(); - if( ! jrePackage.isCompatibleWith("1.6")){ - System.err.println("You need JRE 1.6 or later."); - System.exit(4); - } - return; - } - - /** - * ヘルプメッセージを出力してVMを終了させる。 - */ - private static void putHelp(){ - System.err.println(APPNAME + ' ' + APPVER ); - System.err.println(" License : " + APPLICENSE); - System.err.println(" http://mikutoga.sourceforge.jp/"); - System.err.println(); - System.err.println("-h : put help massage"); - System.err.println("-pmd2xml : convert *.pmd to *.xml"); - System.err.println("-xml2pmd : convert *.xml to *.pmd"); - System.err.println("-i file : specify input file"); - System.err.println("-o file : specify output file"); - System.err.println("-f : force overwriting"); - System.exit(0); - return; - } - - /** - * PMD->XML変換を行う。 - * @param inputFile 入力ファイル名 - * @param outputFile 出力ファイル名 - * @throws IOException 入出力エラー - * @throws MmdFormatException 不正なPMDファイル - * @throws IllegalPmdException 不正なモデルデータ - */ - private static void pmd2xml(String inputFile, String outputFile) - throws IOException, MmdFormatException, IllegalPmdException{ - File iFile = new File(inputFile); - InputStream is = new FileInputStream(iFile); - is = new BufferedInputStream(is); - PmdModel model = pmdRead(is); - is.close(); - - File oFile = new File(outputFile); - trunc(oFile); - OutputStream ostream; - ostream = new FileOutputStream(oFile, false); - ostream = new BufferedOutputStream(ostream); - xmlOut(model, ostream); - ostream.close(); - - return; - } - - /** - * XML->PMD変換を行う。 - * @param inputFile 入力ファイル名 - * @param outputFile 出力ファイル名 - * @throws IOException 入出力エラー - * @throws ParserConfigurationException XML構成のエラー - * @throws SAXException 不正なXMLファイル - * @throws TogaXmlException 不正なXMLファイル - * @throws IllegalPmdException 不正なPMDモデルデータ - */ - private static void xml2pmd(String inputFile, String outputFile) - throws IOException, - ParserConfigurationException, - SAXException, - TogaXmlException, - IllegalPmdException { - File iFile = new File(inputFile); - InputStream is = new FileInputStream(iFile); - is = new BufferedInputStream(is); - InputSource source = new InputSource(is); - PmdModel model = xmlRead(source); - is.close(); - - File oFile = new File(outputFile); - trunc(oFile); - OutputStream ostream; - ostream = new FileOutputStream(oFile, false); - ostream = new BufferedOutputStream(ostream); - pmdOut(model, ostream); - ostream.close(); - - return; - } - - /** - * ファイルサイズを0に切り詰める。 - * @param file ファイル - * @throws IOException 入出力エラー - */ - private static void trunc(File file) throws IOException{ - if( ! file.exists() ) return; - if( ! file.isFile() ) return; - - FileOutputStream foStream = new FileOutputStream(file); - FileChannel channnel = foStream.getChannel(); - channnel.truncate(0); - - channnel.close(); - foStream.close(); - - return; - } - - /** - * PMDファイルからモデルデータを読み込む。 - * @param is 入力ストリーム - * @return モデルデータ - * @throws IOException 入力エラー - * @throws MmdFormatException 不正なPMDファイルフォーマット - */ - private static PmdModel pmdRead(InputStream is) - throws IOException, MmdFormatException{ - MmdSource source = new MmdSource(is); - PmdLoader loader = new PmdLoader(source); - - PmdModel model = loader.load(); - - return model; - } - - /** - * XMLファイルからモデルデータを読み込む。 - * @param source 入力ソース - * @return モデルデータ - * @throws IOException 入力エラー - * @throws ParserConfigurationException XML構成エラー - * @throws SAXException XML構文エラー - * @throws TogaXmlException 不正なXMLデータ - */ - private static PmdModel xmlRead(InputSource source) - throws IOException, - ParserConfigurationException, - SAXException, - TogaXmlException { - DocumentBuilder builder = - PmdXmlResources.newBuilder(XmlHandler.HANDLER); - Xml2PmdLoader loader = new Xml2PmdLoader(builder); - - PmdModel model = loader.parse(source); - - return model; - } - - /** - * モデルデータをPMDファイルに出力する。 - * @param model モデルデータ - * @param ostream 出力ストリーム - * @throws IOException 出力エラー - * @throws IllegalPmdException 不正なモデルデータ - */ - private static void pmdOut(PmdModel model, OutputStream ostream) - throws IOException, IllegalPmdException{ - PmdExporter exporter = new PmdExporter(ostream); - exporter.dumpPmdModel(model); - ostream.close(); - return; - } - - /** - * モデルデータをXMLファイルに出力する。 - * @param model モデルデータ - * @param ostream 出力ストリーム - * @throws IOException 出力エラー - * @throws IllegalPmdException 不正なモデルデータ - */ - private static void xmlOut(PmdModel model, OutputStream ostream) - throws IOException, IllegalPmdException{ - PmdXmlExporter exporter = new PmdXmlExporter(ostream); - exporter.setNewLine("\r\n"); - exporter.setGenerator(APPNAME + ' ' + APPVER); - exporter.putPmdModel(model); - exporter.close(); - return; - } - -} +/* + * pmd 2 xml converter main entry + * + * License : The MIT License + * Copyright(c) 2010 MikuToga Partners + */ + +package jp.sourceforge.mikutoga.pmd2xml; + +import java.io.BufferedInputStream; +import java.io.BufferedOutputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.nio.channels.FileChannel; +import java.util.Properties; +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.ParserConfigurationException; +import jp.sourceforge.mikutoga.parser.MmdFormatException; +import jp.sourceforge.mikutoga.parser.MmdSource; +import jp.sourceforge.mikutoga.pmd.PmdModel; +import jp.sourceforge.mikutoga.pmd.pmdexporter.IllegalPmdException; +import jp.sourceforge.mikutoga.pmd.pmdexporter.PmdExporter; +import jp.sourceforge.mikutoga.pmd.pmdloader.PmdLoader; +import jp.sourceforge.mikutoga.pmd.xml.PmdXmlExporter; +import jp.sourceforge.mikutoga.pmd.xml.PmdXmlResources; +import jp.sourceforge.mikutoga.pmd.xml.Xml2PmdLoader; +import jp.sourceforge.mikutoga.xml.TogaXmlException; +import org.xml.sax.InputSource; +import org.xml.sax.SAXException; + +/** + * PMDモデルファイルとXMLとの間で変換を行うアプリケーション。 + */ +public final class Pmd2Xml { + + private static final Class THISCLASS; + private static final String APPNAME; + private static final String APPVER; + private static final String APPLICENSE; + + static{ + THISCLASS = Pmd2Xml.class; + InputStream ver = + THISCLASS.getResourceAsStream("resources/version.properties"); + Properties verProps = new Properties(); + try{ + verProps.load(ver); + }catch(IOException e){ + throw new ExceptionInInitializerError(e); + } + + APPNAME = verProps.getProperty("app.name"); + APPVER = verProps.getProperty("app.version"); + APPLICENSE = verProps.getProperty("app.license"); + + Object dummy = new Pmd2Xml(); + } + + /** + * 隠しコンストラクタ。 + */ + private Pmd2Xml(){ + super(); + assert this.getClass().equals(THISCLASS); + return; + } + + /** + * Mainエントリ。 + * @param args コマンドパラメータ + */ + public static void main(String[] args){ + checkJRE(); + + String inputFile = null; + String outputFile = null; + boolean pmd2xml = false; + boolean xml2pmd = false; + boolean force = false; + int argsLen = args.length; + for(int argIdx = 0; argIdx < argsLen; argIdx++){ + String arg = args[argIdx]; + + if(arg.equals("-h")){ + putHelp(); + }else if(arg.equals("-pmd2xml")){ + pmd2xml = true; + xml2pmd = false; + }else if(arg.equals("-xml2pmd")){ + pmd2xml = false; + xml2pmd = true; + }else if(arg.equals("-i")){ + if(++argIdx >= argsLen){ + System.err.println("ERROR:"); + System.err.println("You need -i argument."); + System.err.println("(-h for help)"); + System.exit(5); + } + inputFile = args[argIdx]; + }else if(arg.equals("-o")){ + if(++argIdx >= argsLen){ + System.err.println("ERROR:"); + System.err.println("You need -o argument."); + System.err.println("(-h for help)"); + System.exit(5); + } + outputFile = args[argIdx]; + }else if(arg.equals("-f")){ + force = true; + }else{ + System.err.println("ERROR:"); + System.err.println("Unknown option:"+arg); + System.err.println("(-h for help)"); + System.exit(5); + } + } + + if( ( ! pmd2xml) && ( ! xml2pmd) ){ + System.err.println("ERROR:"); + System.err.println("You must specify -pmd2xml or -xml2pmd."); + System.err.println("(-h for help)"); + System.exit(5); + } + + if(inputFile == null){ + System.err.println("ERROR:"); + System.err.println("You must specify input file with -i."); + System.err.println("(-h for help)"); + System.exit(5); + } + + if(outputFile == null){ + System.err.println("ERROR:"); + System.err.println("You must specify output file with -o."); + System.err.println("(-h for help)"); + System.exit(5); + } + + File iFile = new File(inputFile); + if( (! iFile.exists()) || (! iFile.isFile()) ){ + System.err.println("ERROR:"); + System.err.println("Can't find input file:" + + iFile.getAbsolutePath()); + System.err.println("(-h for help)"); + System.exit(1); + } + + if( ! force ){ + File oFile = new File(outputFile); + if(oFile.exists()){ + System.err.println("ERROR:"); + System.err.println(oFile.getAbsolutePath() + + " already exists."); + System.err.println("If you want to overwrite, use -f."); + System.err.println("(-h for help)"); + System.exit(1); + } + }else{ + File oFile = new File(outputFile); + if(oFile.exists()){ + if( ! oFile.isFile()){ + System.err.println("ERROR:"); + System.err.println(oFile.getAbsolutePath() + + " is not file."); + System.err.println("(-h for help)"); + System.exit(1); + } + } + } + + try{ + if(pmd2xml) pmd2xml(inputFile, outputFile); + else xml2pmd(inputFile, outputFile); + }catch(IOException e){ + ioError(e); + }catch(ParserConfigurationException e){ + internalError(e); + }catch(IllegalPmdException e){ + internalError(e); + }catch(MmdFormatException e){ + pmdError(e); + }catch(TogaXmlException e){ + xmlError(e); + }catch(SAXException e){ + xmlError(e); + } + + System.exit(0); + + return; + } + + /** + * 入出力エラー処理。 + * 例外を出力してVM終了する。 + * @param ex 例外 + */ + private static void ioError(Throwable ex){ + System.err.println(ex); + System.exit(1); + } + + /** + * XML構文エラー処理。 + * 例外を出力してVM終了する。 + * @param ex 例外 + */ + private static void xmlError(Throwable ex){ + System.err.println(ex); + System.exit(2); + } + + /** + * PMDファイルフォーマットエラー処理。 + * 例外を出力してVM終了する。 + * @param ex 例外 + */ + private static void pmdError(Throwable ex){ + System.err.println(ex); + ex.printStackTrace(System.err); + System.exit(3); + } + + /** + * 内部エラー処理。 + * 例外を出力してVM終了する。 + * @param ex 例外 + */ + private static void internalError(Throwable ex){ + System.err.println(ex); + ex.printStackTrace(System.err); + System.exit(4); + } + + /** + * JREのバージョン判定を行う。 + * 不適切ならVMごと終了。 + */ + private static void checkJRE(){ + Package jrePackage = java.lang.Object.class.getPackage(); + if( ! jrePackage.isCompatibleWith("1.6")){ + System.err.println("You need JRE 1.6 or later."); + System.exit(4); + } + return; + } + + /** + * ヘルプメッセージを出力してVMを終了させる。 + */ + private static void putHelp(){ + System.err.println(APPNAME + ' ' + APPVER ); + System.err.println(" License : " + APPLICENSE); + System.err.println(" http://mikutoga.sourceforge.jp/"); + System.err.println(); + System.err.println("-h : put help massage"); + System.err.println("-pmd2xml : convert *.pmd to *.xml"); + System.err.println("-xml2pmd : convert *.xml to *.pmd"); + System.err.println("-i file : specify input file"); + System.err.println("-o file : specify output file"); + System.err.println("-f : force overwriting"); + System.exit(0); + return; + } + + /** + * PMD->XML変換を行う。 + * @param inputFile 入力ファイル名 + * @param outputFile 出力ファイル名 + * @throws IOException 入出力エラー + * @throws MmdFormatException 不正なPMDファイル + * @throws IllegalPmdException 不正なモデルデータ + */ + private static void pmd2xml(String inputFile, String outputFile) + throws IOException, MmdFormatException, IllegalPmdException{ + File iFile = new File(inputFile); + InputStream is = new FileInputStream(iFile); + is = new BufferedInputStream(is); + PmdModel model = pmdRead(is); + is.close(); + + File oFile = new File(outputFile); + trunc(oFile); + OutputStream ostream; + ostream = new FileOutputStream(oFile, false); + ostream = new BufferedOutputStream(ostream); + xmlOut(model, ostream); + ostream.close(); + + return; + } + + /** + * XML->PMD変換を行う。 + * @param inputFile 入力ファイル名 + * @param outputFile 出力ファイル名 + * @throws IOException 入出力エラー + * @throws ParserConfigurationException XML構成のエラー + * @throws SAXException 不正なXMLファイル + * @throws TogaXmlException 不正なXMLファイル + * @throws IllegalPmdException 不正なPMDモデルデータ + */ + private static void xml2pmd(String inputFile, String outputFile) + throws IOException, + ParserConfigurationException, + SAXException, + TogaXmlException, + IllegalPmdException { + File iFile = new File(inputFile); + InputStream is = new FileInputStream(iFile); + is = new BufferedInputStream(is); + InputSource source = new InputSource(is); + PmdModel model = xmlRead(source); + is.close(); + + File oFile = new File(outputFile); + trunc(oFile); + OutputStream ostream; + ostream = new FileOutputStream(oFile, false); + ostream = new BufferedOutputStream(ostream); + pmdOut(model, ostream); + ostream.close(); + + return; + } + + /** + * ファイルサイズを0に切り詰める。 + * @param file ファイル + * @throws IOException 入出力エラー + */ + private static void trunc(File file) throws IOException{ + if( ! file.exists() ) return; + if( ! file.isFile() ) return; + + FileOutputStream foStream = new FileOutputStream(file); + FileChannel channnel = foStream.getChannel(); + channnel.truncate(0); + + channnel.close(); + foStream.close(); + + return; + } + + /** + * PMDファイルからモデルデータを読み込む。 + * @param is 入力ストリーム + * @return モデルデータ + * @throws IOException 入力エラー + * @throws MmdFormatException 不正なPMDファイルフォーマット + */ + private static PmdModel pmdRead(InputStream is) + throws IOException, MmdFormatException{ + MmdSource source = new MmdSource(is); + PmdLoader loader = new PmdLoader(source); + + PmdModel model = loader.load(); + + return model; + } + + /** + * XMLファイルからモデルデータを読み込む。 + * @param source 入力ソース + * @return モデルデータ + * @throws IOException 入力エラー + * @throws ParserConfigurationException XML構成エラー + * @throws SAXException XML構文エラー + * @throws TogaXmlException 不正なXMLデータ + */ + private static PmdModel xmlRead(InputSource source) + throws IOException, + ParserConfigurationException, + SAXException, + TogaXmlException { + DocumentBuilder builder = + PmdXmlResources.newBuilder(XmlHandler.HANDLER); + Xml2PmdLoader loader = new Xml2PmdLoader(builder); + + PmdModel model = loader.parse(source); + + return model; + } + + /** + * モデルデータをPMDファイルに出力する。 + * @param model モデルデータ + * @param ostream 出力ストリーム + * @throws IOException 出力エラー + * @throws IllegalPmdException 不正なモデルデータ + */ + private static void pmdOut(PmdModel model, OutputStream ostream) + throws IOException, IllegalPmdException{ + PmdExporter exporter = new PmdExporter(ostream); + exporter.dumpPmdModel(model); + ostream.close(); + return; + } + + /** + * モデルデータをXMLファイルに出力する。 + * @param model モデルデータ + * @param ostream 出力ストリーム + * @throws IOException 出力エラー + * @throws IllegalPmdException 不正なモデルデータ + */ + private static void xmlOut(PmdModel model, OutputStream ostream) + throws IOException, IllegalPmdException{ + PmdXmlExporter exporter = new PmdXmlExporter(ostream); + exporter.setNewLine("\r\n"); + exporter.setGenerator(APPNAME + ' ' + APPVER); + exporter.putPmdModel(model); + exporter.close(); + return; + } + +} diff --git a/src/main/java/jp/sourceforge/mikutoga/pmd2xml/XmlHandler.java b/src/main/java/jp/sourceforge/mikutoga/pmd2xml/XmlHandler.java index 62efd7a..64198e0 100644 --- a/src/main/java/jp/sourceforge/mikutoga/pmd2xml/XmlHandler.java +++ b/src/main/java/jp/sourceforge/mikutoga/pmd2xml/XmlHandler.java @@ -1,63 +1,63 @@ -/* - * XML custom error-handler - * - * License : The MIT License - * Copyright(c) 2010 MikuToga Partners - */ - -package jp.sourceforge.mikutoga.pmd2xml; - -import org.xml.sax.ErrorHandler; -import org.xml.sax.SAXException; -import org.xml.sax.SAXParseException; - -/** - * 自製エラーハンドラ。 - * 例外を渡されれば即投げる。 - */ -final class XmlHandler implements ErrorHandler{ - - /** - * 唯一のシングルトン。 - */ - static final ErrorHandler HANDLER = new XmlHandler(); - - /** - * 隠しコンストラクタ。 - */ - private XmlHandler(){ - super(); - return; - } - - /** - * {@inheritDoc} - * @param exception {@inheritDoc} - * @throws SAXException {@inheritDoc} - */ - @Override - public void error(SAXParseException exception) throws SAXException{ - throw exception; - } - - /** - * {@inheritDoc} - * @param exception {@inheritDoc} - * @throws SAXException {@inheritDoc} - */ - @Override - public void fatalError(SAXParseException exception) throws SAXException{ - throw exception; - } - - /** - * {@inheritDoc} - * @param exception {@inheritDoc} - * @throws SAXException {@inheritDoc} - */ - @Override - public void warning(SAXParseException exception) throws SAXException{ - throw exception; - } - -} +/* + * XML custom error-handler + * + * License : The MIT License + * Copyright(c) 2010 MikuToga Partners + */ + +package jp.sourceforge.mikutoga.pmd2xml; + +import org.xml.sax.ErrorHandler; +import org.xml.sax.SAXException; +import org.xml.sax.SAXParseException; + +/** + * 自製エラーハンドラ。 + * 例外を渡されれば即投げる。 + */ +final class XmlHandler implements ErrorHandler{ + + /** + * 唯一のシングルトン。 + */ + static final ErrorHandler HANDLER = new XmlHandler(); + + /** + * 隠しコンストラクタ。 + */ + private XmlHandler(){ + super(); + return; + } + + /** + * {@inheritDoc} + * @param exception {@inheritDoc} + * @throws SAXException {@inheritDoc} + */ + @Override + public void error(SAXParseException exception) throws SAXException{ + throw exception; + } + + /** + * {@inheritDoc} + * @param exception {@inheritDoc} + * @throws SAXException {@inheritDoc} + */ + @Override + public void fatalError(SAXParseException exception) throws SAXException{ + throw exception; + } + + /** + * {@inheritDoc} + * @param exception {@inheritDoc} + * @throws SAXException {@inheritDoc} + */ + @Override + public void warning(SAXParseException exception) throws SAXException{ + throw exception; + } + +} diff --git a/src/main/java/jp/sourceforge/mikutoga/pmd2xml/package-info.java b/src/main/java/jp/sourceforge/mikutoga/pmd2xml/package-info.java index f4eddd3..72a2c1a 100644 --- a/src/main/java/jp/sourceforge/mikutoga/pmd2xml/package-info.java +++ b/src/main/java/jp/sourceforge/mikutoga/pmd2xml/package-info.java @@ -1,15 +1,15 @@ -/* - * package information for Javadoc - * - * License : The MIT License - * Copyright(c) 2010 MikuToga Partners - */ - -/** - * PMDモデルデータとXML間でのデータ交換を行うアプリケーション。 - * Mainエントリは、クラスPmd2Xml。 - */ - -package jp.sourceforge.mikutoga.pmd2xml; - -/* EOF */ +/* + * package information for Javadoc + * + * License : The MIT License + * Copyright(c) 2010 MikuToga Partners + */ + +/** + * PMDモデルデータとXML間でのデータ交換を行うアプリケーション。 + * Mainエントリは、クラスPmd2Xml。 + */ + +package jp.sourceforge.mikutoga.pmd2xml; + +/* EOF */ -- 2.11.0