OSDN Git Service

MMD Ver7.40 対応 新スキーマ開発開始
[mikutoga/Vmd2XML.git] / src / main / java / jp / sfjp / mikutoga / vmd / model / xml / XmlHandler.java
1 /*
2  * xml 2 vmd SAX Handler
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.VmdMotion;
11 import org.xml.sax.Attributes;
12 import org.xml.sax.ContentHandler;
13 import org.xml.sax.Locator;
14 import org.xml.sax.SAXException;
15
16 /**
17  * XMLモーションファイルパース用SAXハンドラ。
18  * <p>下位リスナに各種通知が振り分けられる。
19  */
20 class XmlHandler implements ContentHandler{
21
22     private VmdMotion vmdMotion;
23
24     private String nspfx = "";
25     private String nsuri = null;
26
27     private final SaxVmdListener motionListener;
28     private final SaxVmdListener cameraListener;
29     private final SaxVmdListener lightListener;
30
31     private SaxVmdListener currentListener = null;
32
33
34     /**
35      * コンストラクタ。
36      */
37     XmlHandler(){
38         super();
39
40         this.motionListener = new SaxMotionListener();
41         this.cameraListener = new SaxCameraListener();
42         this.lightListener  = new SaxLightingListener();
43
44         return;
45     }
46
47
48     /**
49      * ビルド対象のモーションを返す。
50      * @return ビルド対象のモーション
51      */
52     VmdMotion getVmdMotion(){
53         return this.vmdMotion;
54     }
55
56     /**
57      * {@inheritDoc}
58      * @throws SAXException {@inheritDoc}
59      */
60     @Override
61     public void startDocument() throws SAXException{
62         this.vmdMotion = new VmdMotion();
63
64         this.motionListener.setVmdMotion(this.vmdMotion);
65         this.cameraListener.setVmdMotion(this.vmdMotion);
66         this.lightListener .setVmdMotion(this.vmdMotion);
67
68         return;
69     }
70
71     /**
72      * {@inheritDoc}
73      * @throws SAXException {@inheritDoc}
74      */
75     @Override
76     public void endDocument() throws SAXException{
77         assert this.vmdMotion != null;
78         return;
79     }
80
81     /**
82      * {@inheritDoc}
83      * @param prefix {@inheritDoc}
84      * @param uri {@inheritDoc}
85      * @throws SAXException {@inheritDoc}
86      */
87     @Override
88     public void startPrefixMapping(String prefix, String uri)
89             throws SAXException {
90         if(Schema110820.NS_VMDXML.equals(uri)){
91             this.nspfx = prefix;
92             this.nsuri = uri;
93         }
94         return;
95     }
96
97     /**
98      * {@inheritDoc}
99      * @param prefix {@inheritDoc}
100      * @throws SAXException {@inheritDoc}
101      */
102     @Override
103     public void endPrefixMapping(String prefix) throws SAXException {
104         if(prefix.equals(this.nspfx)){
105             this.nspfx = "";
106             this.nsuri = null;
107         }
108         return;
109     }
110
111     /**
112      * {@inheritDoc}
113      * @param uri {@inheritDoc}
114      * @param localName {@inheritDoc}
115      * @param qName {@inheritDoc}
116      * @param attr {@inheritDoc}
117      * @throws SAXException {@inheritDoc}
118      */
119     @Override
120     public void startElement(String uri,
121                                String localName,
122                                String qName,
123                                Attributes attr)
124             throws SAXException {
125         if( ! this.nsuri.equals(uri) ) return;
126
127         VmdTag tag = VmdTag.parse(localName);
128         if(tag == null) return;
129
130         if(tag == VmdTag.MODEL_NAME){
131             String modelName =
132                     SaxAttr.getStringAttr(attr, XmlAttr.ATTR_NAME);
133             this.vmdMotion.setModelName(modelName);
134             return;
135         }
136
137         switchListener(tag);
138         if(this.currentListener != null){
139             this.currentListener.openTag(tag, attr);
140         }
141
142         return;
143     }
144
145     /**
146      * タグ出現に従い通知リスナを切り替える。
147      * @param tag タグ種別
148      */
149     private void switchListener(VmdTag tag){
150         switch(tag){
151         case BONE_M_SEQUENCE:
152         case MORPH_SEQUENCE:
153             this.currentListener = this.motionListener;
154             break;
155         case CAMERA_SEQUENCE:
156             this.currentListener = this.cameraListener;
157             break;
158         case LUMI_SEQUENCE:
159         case SHADOW_SEQUENCE:
160             this.currentListener = this.lightListener;
161             break;
162         default:
163             break;
164         }
165
166         return;
167     }
168
169     /**
170      * {@inheritDoc}
171      * @param uri {@inheritDoc}
172      * @param localName {@inheritDoc}
173      * @param qName {@inheritDoc}
174      * @throws SAXException {@inheritDoc}
175      */
176     @Override
177     public void endElement(String uri, String localName, String qName)
178             throws SAXException {
179         if( ! this.nsuri.equals(uri) ) return;
180
181         VmdTag tag = VmdTag.parse(localName);
182         if(tag == null) return;
183
184         if(this.currentListener != null){
185             this.currentListener.closeTag(tag);
186         }
187
188         return;
189     }
190
191     /**
192      * {@inheritDoc}
193      * @param locator {@inheritDoc}
194      */
195     @Override
196     public void setDocumentLocator(Locator locator){
197         return;
198     }
199
200     /**
201      * {@inheritDoc}
202      * @param target {@inheritDoc}
203      * @param data {@inheritDoc}
204      * @throws SAXException {@inheritDoc}
205      */
206     @Override
207     public void processingInstruction(String target, String data)
208             throws SAXException {
209         return;
210     }
211
212     /**
213      * {@inheritDoc}
214      * @param ch {@inheritDoc}
215      * @param start {@inheritDoc}
216      * @param length {@inheritDoc}
217      * @throws SAXException {@inheritDoc}
218      */
219     @Override
220     public void characters(char[] ch, int start, int length)
221             throws SAXException {
222         return;
223     }
224
225     /**
226      * {@inheritDoc}
227      * @param ch {@inheritDoc}
228      * @param start {@inheritDoc}
229      * @param length {@inheritDoc}
230      * @throws SAXException {@inheritDoc}
231      */
232     @Override
233     public void ignorableWhitespace(char[] ch, int start, int length)
234             throws SAXException {
235         return;
236     }
237
238     /**
239      * {@inheritDoc}
240      * @param name {@inheritDoc}
241      * @throws SAXException {@inheritDoc}
242      */
243     @Override
244     public void skippedEntity(String name) throws SAXException{
245         return;
246     }
247
248 }