1 /*--------------------------------------------------------------------------
2 * Copyright 2004 Taro L. Saito
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *--------------------------------------------------------------------------*/
16 //--------------------------------------
19 // ProgressiveSAXParser.java
24 //--------------------------------------
25 package org.xerial.util.xml.pullparser;
27 import static org.xmlpull.v1.XmlPullParser.END_DOCUMENT;
28 import static org.xmlpull.v1.XmlPullParser.END_TAG;
29 import static org.xmlpull.v1.XmlPullParser.START_DOCUMENT;
30 import static org.xmlpull.v1.XmlPullParser.START_TAG;
31 import static org.xmlpull.v1.XmlPullParser.TEXT;
33 import java.io.IOException;
34 import java.util.List;
35 import java.util.Vector;
37 import org.xerial.util.xml.XMLErrorCode;
38 import org.xerial.util.xml.XMLException;
39 import org.xerial.util.xml.XMLInputSource;
40 import org.xmlpull.v1.XmlPullParser;
41 import org.xmlpull.v1.XmlPullParserException;
44 * SAX Parser that can control parsing steps by its own
47 * XMLInputSource xmlSource = new XMLInputSource("booklist.xml");
48 * ProgressiveSAXParser parser = new ProgressiveSAXParser(handler, xmlSource);
51 * while ((state = parser.parseStep()) != XmlPullParser.END_DOCUMENT)
59 public class ProgressiveSAXParser
61 private Vector<SAXEventHandler> _handlerList = new Vector<SAXEventHandler>();
62 private boolean _keepParserStatusWhileHandlingSAXEvents = true;
64 private XMLInputSource _inputSource = null;
66 private XmlPullParser _parser = null;
69 * @throws XMLParserException
72 public ProgressiveSAXParser(SAXEventHandler handler, XMLInputSource inputSource) throws XMLException
74 _handlerList.add(handler);
75 setXMLInputSource(inputSource);
78 public ProgressiveSAXParser(List<SAXEventHandler> handlerList, XMLInputSource inputSource) throws XMLException
80 for (SAXEventHandler handler : handlerList)
81 _handlerList.add(handler);
83 setXMLInputSource(inputSource);
86 private void enablePullParsingWhileHandlingEvents()
88 _keepParserStatusWhileHandlingSAXEvents = false;
91 private void setXMLInputSource(XMLInputSource inputSource) throws XMLException
93 _inputSource = inputSource;
94 _parser = PullParserUtil.newParser(_inputSource.getReader());
95 _parser = _keepParserStatusWhileHandlingSAXEvents ? new ParseContext(_parser) : _parser;
98 public int parseStep() throws Exception, IOException
100 assert _parser != null;
103 int state = _parser.next();
107 for (SAXEventHandler handler : _handlerList)
108 handler.startTag(_parser);
113 for (SAXEventHandler handler : _handlerList)
114 handler.text(_parser);
117 for (SAXEventHandler handler : _handlerList)
118 handler.endDocument(_parser);
121 for (SAXEventHandler handler : _handlerList)
122 handler.startDocument(_parser);
128 catch (XmlPullParserException e)
130 throw new XMLException(XMLErrorCode.PARSE_ERROR, e);