2 * xml to pmd SAX Handler
4 * License : The MIT License
5 * Copyright(c) 2013 MikuToga Partners
8 package jp.sfjp.mikutoga.pmd.model.xml;
10 import java.util.EnumMap;
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;
19 * XMLモデルファイルパース用SAXハンドラ。
21 * <p>下位リスナに各種通知が振り分けられる。
23 class XmlHandler implements ContentHandler{
25 private final Map<PmdTag, SaxListener> listenerMap;
26 private SaxListener currentListener = null;
28 private PmdModel pmdModel = null;
30 private String nspfx = "";
31 private String nsuri = null;
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);
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);
63 * @return ビルド対象のモデル。ビルド前ならnull
65 PmdModel getPmdModel(){
72 * @throws SAXException {@inheritDoc}
75 public void startDocument() throws SAXException{
76 this.pmdModel = new PmdModel();
78 for(SaxListener listener : this.listenerMap.values()){
79 listener.setPmdModel(this.pmdModel);
88 * @throws SAXException {@inheritDoc}
91 public void endDocument() throws SAXException{
92 assert this.pmdModel != null;
93 this.currentListener = null;
100 * @param prefix {@inheritDoc}
101 * @param uri {@inheritDoc}
102 * @throws SAXException {@inheritDoc}
105 public void startPrefixMapping(String prefix, String uri)
106 throws SAXException {
107 if( Schema101009.NS_PMDXML.equals(uri)
108 || Schema130128.NS_PMDXML.equals(uri) ){
118 * @param prefix {@inheritDoc}
119 * @throws SAXException {@inheritDoc}
122 public void endPrefixMapping(String prefix) throws SAXException {
123 if(prefix.equals(this.nspfx)){
133 * @param uri {@inheritDoc}
134 * @param localName {@inheritDoc}
135 * @param qName {@inheritDoc}
136 * @param attr {@inheritDoc}
137 * @throws SAXException {@inheritDoc}
140 public void startElement(String uri,
144 throws SAXException {
145 if( ! this.nsuri.equals(uri) ) return;
147 PmdTag tag = PmdTag.parse(localName);
148 if(tag == null) return;
152 if(this.currentListener == null) return;
153 this.currentListener.openDispatch(tag, attr);
159 * タグ出現に従い通知リスナを切り替える。
163 private void switchListener(PmdTag tag){
164 SaxListener newListener = this.listenerMap.get(tag);
165 if(newListener == null) return;
167 this.currentListener = newListener;
175 * @param uri {@inheritDoc}
176 * @param localName {@inheritDoc}
177 * @param qName {@inheritDoc}
178 * @throws SAXException {@inheritDoc}
181 public void endElement(String uri, String localName, String qName)
182 throws SAXException {
183 if( ! this.nsuri.equals(uri) ) return;
185 PmdTag tag = PmdTag.parse(localName);
186 if(tag == null) return;
188 if(this.currentListener != null){
189 this.currentListener.closeDispatch(tag);
198 * @param locator {@inheritDoc}
201 public void setDocumentLocator(Locator locator){
209 * @param target {@inheritDoc}
210 * @param data {@inheritDoc}
211 * @throws SAXException {@inheritDoc}
214 public void processingInstruction(String target, String data)
215 throws SAXException {
223 * @param ch {@inheritDoc}
224 * @param start {@inheritDoc}
225 * @param length {@inheritDoc}
226 * @throws SAXException {@inheritDoc}
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);
239 * @param ch {@inheritDoc}
240 * @param start {@inheritDoc}
241 * @param length {@inheritDoc}
242 * @throws SAXException {@inheritDoc}
245 public void ignorableWhitespace(char[] ch, int start, int length)
246 throws SAXException {
254 * @param name {@inheritDoc}
255 * @throws SAXException {@inheritDoc}
258 public void skippedEntity(String name) throws SAXException{