OSDN Git Service

MMD Ver7.40 対応 新スキーマ開発開始
[mikutoga/Vmd2XML.git] / src / main / java / jp / sfjp / mikutoga / vmd / model / xml / SaxVmdListener.java
1 /*
2  * VMD-SAX element listsner
3  *
4  * License : The MIT License
5  * Copyright(c) 2013 MikuToga Partners
6  */
7
8 package jp.sfjp.mikutoga.vmd.model.xml;
9
10 import jp.sfjp.mikutoga.vmd.model.BezierParam;
11 import jp.sfjp.mikutoga.vmd.model.PosCurve;
12 import jp.sfjp.mikutoga.vmd.model.VmdMotion;
13 import org.xml.sax.Attributes;
14
15 /**
16  * XML要素出現の通知受信部の共通実装。
17  */
18 class SaxVmdListener {
19
20     private VmdMotion vmdMotion;
21
22     private PosCurve currentPosCurve;
23     private int axisIdx;
24
25     private BezierParam currentBezParam;
26
27
28     /**
29      * コンストラクタ。
30      */
31     protected SaxVmdListener(){
32         super();
33
34         this.vmdMotion = null;
35
36         this.currentPosCurve = null;
37         this.axisIdx = -1;
38
39         this.currentBezParam = null;
40
41         return;
42     }
43
44
45     /**
46      * XML要素開始の通知。
47      * @param tag 要素種別
48      * @param attr 属性群
49      */
50     void openTag(VmdTag tag, Attributes attr){
51         switch(tag){
52         case BEZIER:
53             openBezier(attr);
54             break;
55         case DEF_LINEAR:
56             openDefLinear();
57             break;
58         case DEF_EASE_IN_OUT:
59             openDefEaseInOut();
60             break;
61         default:
62             break;
63         }
64
65         return;
66     }
67
68     /**
69      * XML要素終了の通知。
70      * @param tag 要素種別
71      */
72     void closeTag(VmdTag tag){
73         return;
74     }
75
76     /**
77      * ビルド対象オブジェクトの登録。
78      * @param motion ビルド対象オブジェクト
79      * @throws NullPointerException 引数がnull
80      */
81     void setVmdMotion(VmdMotion motion) throws NullPointerException{
82         if(motion == null) throw new NullPointerException();
83         this.vmdMotion = motion;
84         return;
85     }
86
87     /**
88      * ビルド対象オブジェクトの取得。
89      * @return ビルド対象オブジェクト。未登録の場合はnull。
90      */
91     protected VmdMotion getVmdMotion(){
92         return this.vmdMotion;
93     }
94
95     /**
96      * ビルド対象の位置補間曲線情報を受け取る。
97      * @param curve 位置補間曲線情報
98      */
99     protected void setCurrentPosCurve(PosCurve curve){
100         this.currentPosCurve = curve;
101         this.axisIdx = 0;
102
103         this.currentBezParam = null;
104
105         return;
106     }
107
108     /**
109      * ビルド対象の単一補間曲線情報を受け取る。
110      * @param bez 補間曲線情報
111      */
112     protected void setCurrentBezierParam(BezierParam bez){
113         this.currentBezParam = bez;
114
115         this.currentPosCurve = null;
116         this.axisIdx = -1;
117
118         return;
119     }
120
121     /**
122      * ビルド対象の補間曲線情報を返す。
123      * @return 補間曲線情報
124      */
125     private BezierParam getTargetBezierParam(){
126         if(this.currentBezParam != null){
127             return this.currentBezParam;
128         }
129
130         if(this.currentPosCurve == null){
131             assert false;
132             throw new AssertionError();
133         }
134
135         BezierParam result;
136
137         switch(this.axisIdx){
138         case 0:
139             result = this.currentPosCurve.getIntpltXpos();
140             break;
141         case 1:
142             result = this.currentPosCurve.getIntpltYpos();
143             break;
144         case 2:
145             result = this.currentPosCurve.getIntpltZpos();
146             break;
147         default:
148             assert false;
149             throw new AssertionError();
150         }
151
152         this.axisIdx++;
153
154         return result;
155     }
156
157     /**
158      * ベジェ補間曲線情報を構築。
159      * @param p1x P1-x
160      * @param p1y P1-y
161      * @param p2x P2-x
162      * @param p2y P2-y
163      */
164     protected void putBezier(byte p1x, byte p1y, byte p2x, byte p2y){
165         BezierParam bez = getTargetBezierParam();
166
167         bez.setP1(p1x, p1y);
168         bez.setP2(p2x, p2y);
169
170         return;
171     }
172
173     /**
174      * bezier要素開始の通知。
175      * @param attr 属性群
176      */
177     protected void openBezier(Attributes attr){
178         byte p1x = SaxAttr.getByteAttr(attr, XmlAttr.ATTR_P1X);
179         byte p1y = SaxAttr.getByteAttr(attr, XmlAttr.ATTR_P1Y);
180         byte p2x = SaxAttr.getByteAttr(attr, XmlAttr.ATTR_P2X);
181         byte p2y = SaxAttr.getByteAttr(attr, XmlAttr.ATTR_P2Y);
182
183         putBezier(p1x, p1y, p2x, p2y);
184
185         return;
186     }
187
188     /**
189      * defLinear要素開始の通知。
190      */
191     protected void openDefLinear(){
192         byte p1x = BezierParam.DEF_P1X;
193         byte p1y = BezierParam.DEF_P1Y;
194         byte p2x = BezierParam.DEF_P2X;
195         byte p2y = BezierParam.DEF_P2Y;
196
197         putBezier(p1x, p1y, p2x, p2y);
198
199         return;
200     }
201
202     /**
203      * defEaseInOut要素開始の通知。
204      */
205     protected void openDefEaseInOut(){
206         byte p1x = BezierParam.EIO_P1X;
207         byte p1y = BezierParam.EIO_P1Y;
208         byte p2x = BezierParam.EIO_P2X;
209         byte p2y = BezierParam.EIO_P2Y;
210
211         putBezier(p1x, p1y, p2x, p2y);
212
213         return;
214     }
215
216 }