OSDN Git Service

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