OSDN Git Service

MMD Ver7.40 対応 新スキーマ開発開始
[mikutoga/Vmd2XML.git] / src / main / java / jp / sfjp / mikutoga / vmd / model / binio / VmdExporter.java
1 /*
2  * vmd exporter
3  *
4  * License : The MIT License
5  * Copyright(c) 2011 MikuToga Partners
6  */
7
8 package jp.sfjp.mikutoga.vmd.model.binio;
9
10 import java.io.IOException;
11 import java.io.OutputStream;
12 import jp.sfjp.mikutoga.bin.export.IllegalTextExportException;
13 import jp.sfjp.mikutoga.vmd.IllegalVmdDataException;
14 import jp.sfjp.mikutoga.vmd.model.VmdMotion;
15
16 /**
17  * VMDファイルのエクスポーター。
18  */
19 public class VmdExporter {
20
21     private BasicExporter    basicExporter = null;
22     private CameraExporter   cameraExporter = null;
23     private LightingExporter lightingExporter = null;
24     private BoolExporter     boolExporter = null;
25
26
27     /**
28      * コンストラクタ。
29      */
30     public VmdExporter(){
31         super();
32         return;
33     }
34
35     /**
36      * モーションデータをVMDファイル形式で出力する。
37      * <p>異常時には出力データのフラッシュが試みられる。
38      * @param motion モーションデータ
39      * @param ostream 出力先ストリーム
40      * @throws IOException 出力エラー
41      * @throws IllegalVmdDataException モーションデータに不備が発見された
42      */
43     public void dumpVmdMotion(VmdMotion motion, OutputStream ostream)
44             throws IOException, IllegalVmdDataException{
45         this.basicExporter    = new BasicExporter(ostream);
46         this.cameraExporter   = new CameraExporter(ostream);
47         this.lightingExporter = new LightingExporter(ostream);
48         this.boolExporter     = new BoolExporter(ostream);
49
50         try{
51             dumpVmdMotionImpl(motion);
52         }finally{
53             ostream.flush();
54         }
55
56         return;
57     }
58
59     /**
60      * モーションデータをVMDファイル形式で出力する。
61      * @param motion モーションデータ
62      * @throws IOException 出力エラー
63      * @throws IllegalVmdDataException モーションデータに不備が発見された
64      */
65     private void dumpVmdMotionImpl(VmdMotion motion)
66             throws IOException, IllegalVmdDataException{
67         this.basicExporter.dumpHeader();
68
69         try{
70             this.basicExporter.dumpModelName(motion);
71             this.basicExporter.dumpBoneMotion(motion);
72             this.basicExporter.dumpMorphMotion(motion);
73         }catch(IllegalTextExportException e){
74             throw new IllegalVmdDataException(e);
75         }
76
77         this.cameraExporter.dumpCameraMotion(motion);
78         this.lightingExporter.dumpLuminousMotion(motion);
79         this.lightingExporter.dumpShadowMotion(motion);
80
81         if(motion.getNumberedFlagList().isEmpty()) return;
82         try{
83             this.boolExporter.dumpNumberedFlagMotion(motion);
84         }catch(IllegalTextExportException e){
85             throw new IllegalVmdDataException(e);
86         }
87
88         return;
89     }
90
91 }