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ハンドラ。
20 * <p>下位リスナに各種通知が振り分けられる。
22 class XmlHandler implements ContentHandler{
24 private final Map<PmdTag, SaxListener> listenerMap;
25 private SaxListener currentListener = null;
27 private PmdModel pmdModel = null;
29 private String nspfx = "";
30 private String nsuri = null;
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);
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);
61 * @return ビルド対象のモデル。ビルド前ならnull
63 PmdModel getPmdModel(){
69 * @throws SAXException {@inheritDoc}
72 public void startDocument() throws SAXException{
73 this.pmdModel = new PmdModel();
75 for(SaxListener listener : this.listenerMap.values()){
76 listener.setPmdModel(this.pmdModel);
84 * @throws SAXException {@inheritDoc}
87 public void endDocument() throws SAXException{
88 assert this.pmdModel != null;
89 this.currentListener = null;
95 * @param prefix {@inheritDoc}
96 * @param uri {@inheritDoc}
97 * @throws SAXException {@inheritDoc}
100 public void startPrefixMapping(String prefix, String uri)
101 throws SAXException {
102 if( Schema101009.NS_PMDXML.equals(uri)
103 || Schema130128.NS_PMDXML.equals(uri) ){
112 * @param prefix {@inheritDoc}
113 * @throws SAXException {@inheritDoc}
116 public void endPrefixMapping(String prefix) throws SAXException {
117 if(prefix.equals(this.nspfx)){
126 * @param uri {@inheritDoc}
127 * @param localName {@inheritDoc}
128 * @param qName {@inheritDoc}
129 * @param attr {@inheritDoc}
130 * @throws SAXException {@inheritDoc}
133 public void startElement(String uri,
137 throws SAXException {
138 if( ! this.nsuri.equals(uri) ) return;
140 PmdTag tag = PmdTag.parse(localName);
141 if(tag == null) return;
145 if(this.currentListener == null) return;
146 this.currentListener.openDispatch(tag, attr);
152 * タグ出現に従い通知リスナを切り替える。
155 private void switchListener(PmdTag tag){
156 SaxListener newListener = this.listenerMap.get(tag);
157 if(newListener == null) return;
159 this.currentListener = newListener;
166 * @param uri {@inheritDoc}
167 * @param localName {@inheritDoc}
168 * @param qName {@inheritDoc}
169 * @throws SAXException {@inheritDoc}
172 public void endElement(String uri, String localName, String qName)
173 throws SAXException {
174 if( ! this.nsuri.equals(uri) ) return;
176 PmdTag tag = PmdTag.parse(localName);
177 if(tag == null) return;
179 if(this.currentListener != null){
180 this.currentListener.closeDispatch(tag);
188 * @param locator {@inheritDoc}
191 public void setDocumentLocator(Locator locator){
198 * @param target {@inheritDoc}
199 * @param data {@inheritDoc}
200 * @throws SAXException {@inheritDoc}
203 public void processingInstruction(String target, String data)
204 throws SAXException {
211 * @param ch {@inheritDoc}
212 * @param start {@inheritDoc}
213 * @param length {@inheritDoc}
214 * @throws SAXException {@inheritDoc}
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);
226 * @param ch {@inheritDoc}
227 * @param start {@inheritDoc}
228 * @param length {@inheritDoc}
229 * @throws SAXException {@inheritDoc}
232 public void ignorableWhitespace(char[] ch, int start, int length)
233 throws SAXException {
240 * @param name {@inheritDoc}
241 * @throws SAXException {@inheritDoc}
244 public void skippedEntity(String name) throws SAXException{