OSDN Git Service

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