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