KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > xml > xqueryevaluator > eval > XPPReader


1 /*
2  * This file belongs to the XQuark distribution.
3  * Copyright (C) 2003 XQuark Group.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307.
18  * You can also get it at http://www.gnu.org/licenses/lgpl.html
19  *
20  * For more information on this software, see http://www.xquark.org.
21  */

22
23 package org.xquark.xml.xqueryevaluator.eval;
24
25 import java.io.IOException JavaDoc;
26 import java.io.InputStream JavaDoc;
27
28 import org.xml.sax.*;
29 import org.xml.sax.ext.LexicalHandler JavaDoc;
30 import org.xmlpull.v1.XmlPullParser;
31 import org.xmlpull.v1.XmlPullParserException;
32 import org.xmlpull.v1.XmlPullParserFactory;
33
34 /**
35  * XML Pull Parser reader that allow the incremental processing of an XML document,
36  * while providing content informatioon through the standard SAX2 ContentHandler and
37  * LexicalHandler APIs
38  */

39
40 public class XPPReader implements Locator, Attributes {
41     protected XmlPullParser xpp;
42     protected boolean beforeFirstTag = true;
43
44     protected ContentHandler contentHandler = null;
45     protected ErrorHandler errorHandler = null;
46     protected LexicalHandler JavaDoc lexicalHandler = null;
47     protected String JavaDoc systemId;
48     protected InputStream JavaDoc input = null;
49
50     private int[] charsBounds = new int[2];
51
52     protected static XmlPullParserFactory factory;
53
54     static {
55         try {
56             factory = XmlPullParserFactory.newInstance();
57             factory.setNamespaceAware(true);
58         } catch (XmlPullParserException ex) {}
59     }
60
61     public XPPReader() throws SAXException {
62         if (factory == null)
63             throw new SAXException("No XmlPullParser factory available");
64         try {
65             xpp = factory.newPullParser();
66         } catch (XmlPullParserException ex) {
67             throw new SAXException(ex);
68         }
69     }
70
71     // -- Attributes interface
72

73     public String JavaDoc getURI(int index) {
74         return xpp.getAttributeNamespace(index);
75     }
76
77     public String JavaDoc getLocalName(int index) {
78         return xpp.getAttributeName(index);
79     }
80
81     public String JavaDoc getQName(int index) {
82         String JavaDoc prefix = xpp.getAttributePrefix(index);
83         if (prefix != null) {
84             return prefix + ':' + xpp.getAttributeName(index);
85         } else {
86             return xpp.getAttributeName(index);
87         }
88     }
89
90     public String JavaDoc getValue(String JavaDoc uri, String JavaDoc localName) {
91         return xpp.getAttributeValue(uri, localName);
92     }
93
94     public String JavaDoc getValue(String JavaDoc qName) {
95         return xpp.getAttributeValue(null, qName);
96     }
97
98     public String JavaDoc getType(int index) {
99         return xpp.getAttributeType(index);
100     }
101
102     public String JavaDoc getValue(int index) {
103         return xpp.getAttributeValue(index);
104     }
105
106     public int getLength() {
107         return xpp.getAttributeCount();
108     }
109
110     public int getIndex(String JavaDoc uri, String JavaDoc localName) {
111         for (int i = 0; i < xpp.getAttributeCount(); i++) {
112             if (xpp.getAttributeNamespace(i).equals(uri) && xpp.getAttributeName(i).equals(localName)) {
113                 return i;
114             }
115         }
116         return -1;
117     }
118
119     public int getIndex(String JavaDoc qName) {
120         for (int i = 0; i < xpp.getAttributeCount(); i++) {
121             if (xpp.getAttributeName(i).equals(qName)) {
122                 return i;
123             }
124         }
125         return -1;
126     }
127
128     public String JavaDoc getType(String JavaDoc uri, String JavaDoc localName) {
129         int index = getIndex(uri, localName);
130         if (index == -1)
131             return null;
132         else
133             return xpp.getAttributeType(index);
134     }
135
136     public String JavaDoc getType(String JavaDoc qName) {
137         int index = getIndex(qName);
138         if (index == -1)
139             return null;
140         else
141             return xpp.getAttributeType(index);
142     }
143
144     // -- Locator interface
145

146     public String JavaDoc getPublicId() {
147         return null;
148     }
149
150     public String JavaDoc getSystemId() {
151         return systemId;
152     }
153
154     public int getLineNumber() {
155         return xpp.getLineNumber();
156     }
157
158     public int getColumnNumber() {
159         return xpp.getColumnNumber();
160     }
161
162     public void setContentHandler(ContentHandler handler) {
163         this.contentHandler = handler;
164     }
165
166     public ContentHandler getContentHandler() {
167         return contentHandler;
168     }
169
170     public void setLexicalHandler(LexicalHandler JavaDoc handler) {
171         this.lexicalHandler = handler;
172     }
173
174     public LexicalHandler JavaDoc getLexicalHandler() {
175         return lexicalHandler;
176     }
177
178     public void setErrorHandler(ErrorHandler handler) {
179         this.errorHandler = handler;
180     }
181
182     public ErrorHandler getErrorHandler() {
183         return errorHandler;
184     }
185
186     public void setInput(String JavaDoc systemId, InputStream JavaDoc input) throws SAXException {
187         this.systemId = systemId;
188         try {
189             xpp.setInput(input, null);
190         } catch (XmlPullParserException ex) {
191             throw new SAXException(ex);
192         }
193     }
194
195     public boolean isAtStart() throws SAXException {
196         try {
197             return xpp.getEventType() == XmlPullParser.START_DOCUMENT;
198         } catch (XmlPullParserException ex) {
199             throw new SAXException(ex);
200         }
201     }
202     
203     public boolean parseNextElement() throws SAXException, IOException JavaDoc {
204         try {
205             int prevCount = 0;
206             int curCount = 0;
207             int depth = 0;
208             char[] chars;
209             while (true) {
210                 switch (xpp.nextToken()) {
211                     case XmlPullParser.START_TAG :
212                         beforeFirstTag = false;
213                         depth = xpp.getDepth();
214                         prevCount = xpp.getNamespaceCount(depth - 1);
215                         curCount = xpp.getNamespaceCount(depth);
216                         for (int i = prevCount; i < curCount; i++) {
217                             contentHandler.startPrefixMapping(xpp.getNamespacePrefix(i), xpp.getNamespaceUri(i));
218                         }
219                         contentHandler.startElement(xpp.getNamespace(), xpp.getName(), null, this);
220                         break;
221                     case XmlPullParser.TEXT :
222                     case XmlPullParser.IGNORABLE_WHITESPACE :
223                     case XmlPullParser.CDSECT :
224                         if (!beforeFirstTag) {
225                             chars = xpp.getTextCharacters(charsBounds);
226                             contentHandler.characters(chars, charsBounds[0], charsBounds[1]);
227                         }
228                         break;
229                     case XmlPullParser.COMMENT :
230                         if (lexicalHandler != null) {
231                             chars = xpp.getTextCharacters(charsBounds);
232                             lexicalHandler.comment(chars, charsBounds[0], charsBounds[1]);
233                         }
234                         break;
235                     case XmlPullParser.PROCESSING_INSTRUCTION :
236                         String JavaDoc pi = xpp.getText().trim();
237                         int index = pi.indexOf(' ');
238                         if (index != -1) contentHandler.processingInstruction(pi.substring(0, index), pi.substring(index+1));
239                         else contentHandler.processingInstruction(pi, "");
240                         break;
241                     case XmlPullParser.END_TAG :
242                         contentHandler.endElement(xpp.getNamespace(), xpp.getName(), null);
243                         depth = xpp.getDepth();
244                         prevCount = xpp.getNamespaceCount(depth - 1);
245                         curCount = xpp.getNamespaceCount(depth);
246                         for (int i = curCount - 1; i >= prevCount; i--) {
247                             contentHandler.endPrefixMapping(xpp.getNamespacePrefix(i));
248                         }
249                         return false;
250                     case XmlPullParser.END_DOCUMENT :
251                         return true;
252                     default :
253                         break;
254                 }
255             }
256         } catch (XmlPullParserException ex) {
257             SAXParseException saxException = new SAXParseException("", this, ex);
258             if (errorHandler != null)
259                 errorHandler.fatalError(saxException);
260             throw saxException;
261         }
262     }
263
264 }
265
Popular Tags