OSDN Git Service

f8db09cc1d81ffdaa43f2f3691956d73c6a8c7d3
[mikutoga/Pmd2XML.git] / src / main / java / jp / sfjp / mikutoga / pmd / model / xml / XmlHandler.java
1 /*
2  * xml to pmd SAX Handler
3  *
4  * License : The MIT License
5  * Copyright(c) 2013 MikuToga Partners
6  */
7
8 package jp.sfjp.mikutoga.pmd.model.xml;
9
10 import java.util.EnumMap;
11 import java.util.Map;
12 import jp.sfjp.mikutoga.pmd.model.PmdModel;
13 import org.xml.sax.Attributes;
14 import org.xml.sax.ContentHandler;
15 import org.xml.sax.Locator;
16 import org.xml.sax.SAXException;
17
18 /**
19  * XMLモデルファイルパース用SAXハンドラ。
20  * <p>下位リスナに各種通知が振り分けられる。
21  */
22 class XmlHandler implements ContentHandler{
23
24     private final Map<PmdTag, SaxListener> listenerMap;
25     private SaxListener currentListener = null;
26
27     private PmdModel pmdModel = null;
28
29     private String nspfx = "";
30     private String nsuri = null;
31
32
33     /**
34      * コンストラクタ。
35      */
36     XmlHandler(){
37         super();
38
39         RefHelper helper = new RefHelper();
40         SaxListener modelListener    = new SaxModelListener();
41         SaxListener materialListener = new SaxMaterialListener(helper);
42         SaxListener boneListener     = new SaxBoneListener(helper);
43         SaxListener morphListener    = new SaxMorphListener(helper);
44         SaxListener dynamicsListener = new SaxDynamicsListener(helper);
45         SaxListener shapeListener    = new SaxShapeListener(helper);
46
47         this.listenerMap = new EnumMap<PmdTag, SaxListener>(PmdTag.class);
48         this.listenerMap.put(PmdTag.PMD_MODEL,          modelListener);
49         this.listenerMap.put(PmdTag.MATERIAL_LIST,      materialListener);
50         this.listenerMap.put(PmdTag.BONE_LIST,          boneListener);
51         this.listenerMap.put(PmdTag.MORPH_LIST,         morphListener);
52         this.listenerMap.put(PmdTag.RIGID_LIST,         dynamicsListener);
53         this.listenerMap.put(PmdTag.SURFACE_GROUP_LIST, shapeListener);
54
55         return;
56     }
57
58
59     /**
60      * ビルド対象のモデルを返す。
61      * @return ビルド対象のモデル。ビルド前ならnull
62      */
63     PmdModel getPmdModel(){
64         return this.pmdModel;
65     }
66
67     /**
68      * {@inheritDoc}
69      * @throws SAXException {@inheritDoc}
70      */
71     @Override
72     public void startDocument() throws SAXException{
73         this.pmdModel = new PmdModel();
74
75         for(SaxListener listener : this.listenerMap.values()){
76             listener.setPmdModel(this.pmdModel);
77         }
78
79         return;
80     }
81
82     /**
83      * {@inheritDoc}
84      * @throws SAXException {@inheritDoc}
85      */
86     @Override
87     public void endDocument() throws SAXException{
88         assert this.pmdModel != null;
89         this.currentListener = null;
90         return;
91     }
92
93     /**
94      * {@inheritDoc}
95      * @param prefix {@inheritDoc}
96      * @param uri {@inheritDoc}
97      * @throws SAXException {@inheritDoc}
98      */
99     @Override
100     public void startPrefixMapping(String prefix, String uri)
101             throws SAXException {
102         if(   Schema101009.NS_PMDXML.equals(uri)
103            || Schema130128.NS_PMDXML.equals(uri) ){
104             this.nspfx = prefix;
105             this.nsuri = uri;
106         }
107         return;
108     }
109
110     /**
111      * {@inheritDoc}
112      * @param prefix {@inheritDoc}
113      * @throws SAXException {@inheritDoc}
114      */
115     @Override
116     public void endPrefixMapping(String prefix) throws SAXException {
117         if(prefix.equals(this.nspfx)){
118             this.nspfx = "";
119             this.nsuri = null;
120         }
121         return;
122     }
123
124     /**
125      * {@inheritDoc}
126      * @param uri {@inheritDoc}
127      * @param localName {@inheritDoc}
128      * @param qName {@inheritDoc}
129      * @param attr {@inheritDoc}
130      * @throws SAXException {@inheritDoc}
131      */
132     @Override
133     public void startElement(String uri,
134                                String localName,
135                                String qName,
136                                Attributes attr)
137             throws SAXException {
138         if( ! this.nsuri.equals(uri) ) return;
139
140         PmdTag tag = PmdTag.parse(localName);
141         if(tag == null) return;
142
143         switchListener(tag);
144
145         if(this.currentListener == null) return;
146         this.currentListener.openDispatch(tag, attr);
147
148         return;
149     }
150
151     /**
152      * タグ出現に従い通知リスナを切り替える。
153      * @param tag タグ種別
154      */
155     private void switchListener(PmdTag tag){
156         SaxListener newListener = this.listenerMap.get(tag);
157         if(newListener == null) return;
158
159         this.currentListener = newListener;
160
161         return;
162     }
163
164     /**
165      * {@inheritDoc}
166      * @param uri {@inheritDoc}
167      * @param localName {@inheritDoc}
168      * @param qName {@inheritDoc}
169      * @throws SAXException {@inheritDoc}
170      */
171     @Override
172     public void endElement(String uri, String localName, String qName)
173             throws SAXException {
174         if( ! this.nsuri.equals(uri) ) return;
175
176         PmdTag tag = PmdTag.parse(localName);
177         if(tag == null) return;
178
179         if(this.currentListener != null){
180             this.currentListener.closeDispatch(tag);
181         }
182
183         return;
184     }
185
186     /**
187      * {@inheritDoc}
188      * @param locator {@inheritDoc}
189      */
190     @Override
191     public void setDocumentLocator(Locator locator){
192         // NOTHING
193         return;
194     }
195
196     /**
197      * {@inheritDoc}
198      * @param target {@inheritDoc}
199      * @param data {@inheritDoc}
200      * @throws SAXException {@inheritDoc}
201      */
202     @Override
203     public void processingInstruction(String target, String data)
204             throws SAXException {
205         // NOTHING
206         return;
207     }
208
209     /**
210      * {@inheritDoc}
211      * @param ch {@inheritDoc}
212      * @param start {@inheritDoc}
213      * @param length {@inheritDoc}
214      * @throws SAXException {@inheritDoc}
215      */
216     @Override
217     public void characters(char[] ch, int start, int length)
218             throws SAXException {
219         if(this.currentListener == null) return;
220         this.currentListener.addCharData(ch, start, length);
221         return;
222     }
223
224     /**
225      * {@inheritDoc}
226      * @param ch {@inheritDoc}
227      * @param start {@inheritDoc}
228      * @param length {@inheritDoc}
229      * @throws SAXException {@inheritDoc}
230      */
231     @Override
232     public void ignorableWhitespace(char[] ch, int start, int length)
233             throws SAXException {
234         // NOTHING
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         // NOTHING
246         return;
247     }
248
249 }