2 * vmd to xml converter main entry
4 * License : The MIT License
5 * Copyright(c) 2011 MikuToga Partners
8 package jp.sfjp.mikutoga.vmd2xml;
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.text.MessageFormat;
22 import java.util.Properties;
23 import jp.sfjp.mikutoga.bin.parser.MmdFormatException;
24 import jp.sfjp.mikutoga.vmd.IllegalVmdDataException;
25 import jp.sourceforge.mikutoga.xml.TogaXmlException;
26 import org.xml.sax.SAXException;
30 * VMDモーションファイルとXMLとの間で変換を行うアプリケーション。
32 public final class Vmd2Xml {
35 public static final int EXIT_OK = 0;
37 public static final int EXIT_INTERR = 1;
39 public static final int EXIT_ENVERR = 2;
40 /** オプション指定に起因するエラー。 */
41 public static final int EXIT_OPTERR = 3;
42 /** ファイル入出力に起因するエラー。 */
43 public static final int EXIT_IOERR = 4;
44 /** XMLフォーマットに起因するエラー。 */
45 public static final int EXIT_XMLERR = 5;
46 /** VMDフォーマットに起因するエラー。 */
47 public static final int EXIT_VMDERR = 7;
50 public static final String APPNAME;
52 public static final String APPVER;
54 public static final String APPLICENSE;
56 public static final String APPURL;
58 private static final Class<?> THISCLASS;
59 private static final String RES_VER = "resources/version.properties";
61 private static final PrintStream ERROUT = System.err;
62 private static final String MSG_ERR = "ERROR:\n{0}\n(-h for help)";
63 private static final String MSG_HELP =
65 + "\u0020\u0020License\u0020:\u0020{2}\n"
66 + "\u0020\u0020{3}\n";
67 private static final String MSG_NOINFILE = "Can't find input file:{0}";
68 private static final String MSG_ABNFILE = "{0} is not file.";
69 private static final String MSG_OWOUTFILE =
70 "{0} already exists.\n"
71 + "If you want to overwrite, use -f.";
73 private static final String MSG_OLDJRE = "You need JRE {0} or later.";
74 private static final String REQUIRED_JRE = "1.6";
77 THISCLASS = Vmd2Xml.class;
78 InputStream ver = THISCLASS.getResourceAsStream(RES_VER);
79 Properties verProps = new Properties();
86 }catch(IOException e){
87 throw new ExceptionInInitializerError(e);
90 APPNAME = verProps.getProperty("app.name");
91 APPVER = verProps.getProperty("app.version");
92 APPLICENSE = verProps.getProperty("app.license");
93 APPURL = verProps.getProperty("app.url");
95 new Vmd2Xml().hashCode();
104 assert this.getClass().equals(THISCLASS);
112 * @see java.lang.System#exit(int)
114 private static void exit(int code){
117 throw new AssertionError();
122 * @param text 個別メッセージ
124 private static void errMsg(String text){
125 String msg = MessageFormat.format(MSG_ERR, text);
133 * @param dumpStack スタックトレースを出力するならtrue
135 private static void thPrintln(Throwable ex, boolean dumpStack){
137 ex.printStackTrace(ERROUT);
139 String text = ex.toString();
140 ERROUT.println(text);
150 private static void thPrintln(Throwable ex){
151 thPrintln(ex, false);
160 private static void ioError(IOException ex){
170 private static void xmlError(Throwable ex){
176 * VMDファイルフォーマットエラー処理。
180 private static void vmdError(MmdFormatException ex){
190 private static void internalError(Throwable ex){
199 private static void checkJRE(){
200 Package jrePackage = java.lang.Object.class.getPackage();
201 if( ! jrePackage.isCompatibleWith(REQUIRED_JRE)){
202 String msg = MessageFormat.format(MSG_OLDJRE, REQUIRED_JRE);
212 private static void putHelp(){
214 MessageFormat.format(MSG_HELP,
215 APPNAME, APPVER, APPLICENSE, APPURL);
217 ERROUT.println(OptSwitch.getConsoleHelp());
223 * <p>ファイルが存在しなければなにもしない。
224 * <p>通常ファイルでなければなにもしない。
226 * @throws IOException 入出力エラー
228 private static void trunc(File file) throws IOException{
229 if( ! file.exists() ) return;
230 if( ! file.isFile() ) return;
232 if(file.length() <= 0L) return;
234 FileOutputStream foStream = new FileOutputStream(file);
236 FileChannel channnel = foStream.getChannel();
238 channnel.truncate(0L);
251 * <p>入力ファイルが通常ファイルとして存在しなければエラー終了。
252 * @param fileName 入力ファイル名
255 private static InputStream openInfile(String fileName){
256 File inFile = new File(fileName);
258 if( (! inFile.exists()) || (! inFile.isFile()) ){
259 String absPath = inFile.getAbsolutePath();
260 String msg = MessageFormat.format(MSG_NOINFILE, absPath);
267 is = new FileInputStream(inFile);
268 }catch(FileNotFoundException e){
271 throw new AssertionError(e);
274 is = new BufferedInputStream(is);
281 * <p>出力ファイルが通常ファイルでない場合はエラー終了。
282 * <p>既存の出力ファイルに上書き指示が伴っていなければエラー終了。
283 * @param fileName 出力ファイル名
284 * @param overWrite 頭から上書きして良ければtrue
287 private static OutputStream openOutfile(String fileName,
289 File outFile = new File(fileName);
291 if(outFile.exists()){
292 String absPath = outFile.getAbsolutePath();
293 if( ! outFile.isFile() ){
294 String msg = MessageFormat.format(MSG_ABNFILE, absPath);
297 }else if( ! overWrite ){
298 String msg = MessageFormat.format(MSG_OWOUTFILE, absPath);
306 }catch(IOException e){
312 os = new FileOutputStream(outFile);
313 }catch(FileNotFoundException e){
316 throw new AssertionError(e);
319 os = new BufferedOutputStream(os);
326 * @param args コマンドパラメータ
328 public static void main(String[] args){
333 optInfo = OptInfo.parseOption(args);
334 }catch(CmdLineException e){
335 String optErrMsg = e.getLocalizedMessage();
341 if(optInfo.needHelp()){
346 String inputFile = optInfo.getInFilename();
347 String outputFile = optInfo.getOutFilename();
348 boolean overwrite = optInfo.overwriteMode();
350 InputStream is = openInfile(inputFile);
351 OutputStream os = openOutfile(outputFile, overwrite);
353 Vmd2XmlConv converter = new Vmd2XmlConv();
355 converter.setInType (optInfo.getInFileType());
356 converter.setOutType(optInfo.getOutFileType());
358 converter.setNewline(optInfo.getNewline());
359 converter.setGenerator(optInfo.getGenerator());
360 converter.setQuaterniomMode(optInfo.isQuaterniomMode());
363 converter.convert(is, os);
364 }catch(IOException e){
366 }catch(IllegalVmdDataException e){
368 }catch(MmdFormatException e){
370 }catch(TogaXmlException e){
372 }catch(SAXException e){
380 }catch(IOException e){
383 }catch(IOException e){