OSDN Git Service

how to mirror to SourceForge.jp
[xerial/xerial-core.git] / src / main / java / org / xerial / util / xml / pullparser / ProgressiveSAXParser.java
1 /*--------------------------------------------------------------------------
2  *  Copyright 2004 Taro L. Saito
3  *
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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 //--------------------------------------
17 // XerialJ Project
18 //
19 // ProgressiveSAXParser.java
20 // Since: 2005/06/03
21 //
22 // $URL$ 
23 // $Author$
24 //--------------------------------------
25 package org.xerial.util.xml.pullparser;
26
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;
32
33 import java.io.IOException;
34 import java.util.List;
35 import java.util.Vector;
36
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;
42
43 /**
44  * SAX Parser that can control parsing steps by its own
45  * 
46  * <pre>
47  * XMLInputSource xmlSource = new XMLInputSource(&quot;booklist.xml&quot;);
48  * ProgressiveSAXParser parser = new ProgressiveSAXParser(handler, xmlSource);
49  * 
50  * int state;
51  * while ((state = parser.parseStep()) != XmlPullParser.END_DOCUMENT)
52  * {}
53  * 
54  * </pre>
55  * 
56  * @author leo
57  * 
58  */
59 public class ProgressiveSAXParser
60 {
61     private Vector<SAXEventHandler> _handlerList = new Vector<SAXEventHandler>();
62     private boolean _keepParserStatusWhileHandlingSAXEvents = true;
63
64     private XMLInputSource _inputSource = null;
65
66     private XmlPullParser _parser = null;
67
68     /**
69      * @throws XMLParserException
70      * 
71      */
72     public ProgressiveSAXParser(SAXEventHandler handler, XMLInputSource inputSource) throws XMLException
73     {
74         _handlerList.add(handler);
75         setXMLInputSource(inputSource);
76     }
77
78     public ProgressiveSAXParser(List<SAXEventHandler> handlerList, XMLInputSource inputSource) throws XMLException
79     {
80         for (SAXEventHandler handler : handlerList)
81             _handlerList.add(handler);
82
83         setXMLInputSource(inputSource);
84     }
85
86     private void enablePullParsingWhileHandlingEvents()
87     {
88         _keepParserStatusWhileHandlingSAXEvents = false;
89     }
90
91     private void setXMLInputSource(XMLInputSource inputSource) throws XMLException
92     {
93         _inputSource = inputSource;
94         _parser = PullParserUtil.newParser(_inputSource.getReader());
95         _parser = _keepParserStatusWhileHandlingSAXEvents ? new ParseContext(_parser) : _parser;
96     }
97
98     public int parseStep() throws Exception, IOException
99     {
100         assert _parser != null;
101         try
102         {
103             int state = _parser.next();
104             switch (state)
105             {
106             case START_TAG:
107                 for (SAXEventHandler handler : _handlerList)
108                     handler.startTag(_parser);
109                 break;
110             case END_TAG:
111                 break;
112             case TEXT:
113                 for (SAXEventHandler handler : _handlerList)
114                     handler.text(_parser);
115                 break;
116             case END_DOCUMENT:
117                 for (SAXEventHandler handler : _handlerList)
118                     handler.endDocument(_parser);
119                 break;
120             case START_DOCUMENT:
121                 for (SAXEventHandler handler : _handlerList)
122                     handler.startDocument(_parser);
123                 break;
124             }
125
126             return state;
127         }
128         catch (XmlPullParserException e)
129         {
130             throw new XMLException(XMLErrorCode.PARSE_ERROR, e);
131         }
132     }
133
134 }