KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > xml > stream > XMLEventReaderImpl


1 /*
2 * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3 *
4 * This file is part of Resin(R) Open Source
5 *
6 * Each copy or derived work must preserve the copyright notice and this
7 * notice unmodified.
8 *
9 * Resin Open Source is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * Resin Open Source is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17 * of NON-INFRINGEMENT. See the GNU General Public License for more
18 * details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with Resin Open Source; if not, write to the
22 *
23 * Free Software Foundation, Inc.
24 * 59 Temple Place, Suite 330
25 * Boston, MA 02111-1307 USA
26 *
27 * @author Emil Ong
28 */

29
30 package com.caucho.xml.stream;
31
32 import com.caucho.util.L10N;
33 import com.caucho.xml.stream.events.*;
34
35 import javax.xml.namespace.QName JavaDoc;
36 import javax.xml.stream.XMLEventReader;
37 import javax.xml.stream.XMLStreamConstants;
38 import javax.xml.stream.XMLStreamException;
39 import javax.xml.stream.XMLStreamReader;
40 import javax.xml.stream.events.Attribute;
41 import javax.xml.stream.events.Namespace;
42 import javax.xml.stream.events.XMLEvent;
43 import java.util.HashMap JavaDoc;
44
45 public class XMLEventReaderImpl implements XMLEventReader, XMLStreamConstants {
46   public static final L10N L = new L10N(XMLEventReaderImpl.class);
47
48   private final XMLStreamReader _in;
49   private XMLEvent _current = null;
50   private XMLEvent _next = null;
51
52   public XMLEventReaderImpl(XMLStreamReader in)
53     throws XMLStreamException
54   {
55     _in = in;
56     _next = getEvent();
57   }
58
59   public void close()
60     throws XMLStreamException
61   {
62     _in.close();
63   }
64
65   public String JavaDoc getElementText()
66     throws XMLStreamException
67   {
68     return _in.getElementText();
69   }
70
71   public Object JavaDoc getProperty(String JavaDoc name)
72     throws IllegalArgumentException JavaDoc
73   {
74     throw new IllegalArgumentException JavaDoc(name);
75   }
76
77   public boolean hasNext()
78   {
79     try {
80       return _next != null || _in.hasNext();
81     }
82     catch (XMLStreamException e) {
83       return false;
84     }
85   }
86
87   public XMLEvent nextEvent() throws XMLStreamException
88   {
89     if (_next != null) {
90       _current = _next;
91       _next = null;
92     }
93     else {
94       _in.next();
95       _current = getEvent();
96     }
97
98     return _current;
99   }
100
101   public XMLEvent nextTag() throws XMLStreamException
102   {
103     if (_next != null) {
104       _current = _next;
105       _next = null;
106     }
107     else {
108       _in.nextTag();
109       _current = getEvent();
110     }
111
112     return _current;
113   }
114
115   public XMLEvent peek() throws XMLStreamException
116   {
117     if (_next == null) {
118       _in.next();
119       _next = getEvent();
120     }
121
122     return _next;
123   }
124
125   public void remove()
126   {
127     throw new UnsupportedOperationException JavaDoc();
128   }
129
130   public XMLEvent next()
131   {
132     try {
133       return nextEvent();
134     }
135     catch (XMLStreamException e) {
136       return null;
137     }
138   }
139
140   private XMLEvent getEvent()
141     throws XMLStreamException
142   {
143     switch (_in.getEventType()) {
144       case ATTRIBUTE:
145         // won't happen: our stream reader does not return attributes
146
// independent of start elements/empty elements
147
break;
148
149       case CDATA:
150         return new CharactersImpl(_in.getText(), true, false, false);
151
152       case CHARACTERS:
153         return new CharactersImpl(_in.getText(), false, false, false);
154
155       case COMMENT:
156         return new CommentImpl(_in.getText());
157
158       case DTD:
159         // XXX
160
break;
161
162       case END_DOCUMENT:
163         return new EndDocumentImpl();
164
165       case END_ELEMENT:
166         return new EndElementImpl(_in.getName());
167
168       case ENTITY_DECLARATION:
169         // XXX
170
break;
171
172       case ENTITY_REFERENCE:
173         // XXX
174
break;
175
176       case NAMESPACE:
177         // won't happen: our stream reader does not return attributes
178
// independent of start elements/empty elements
179
break;
180
181       case NOTATION_DECLARATION:
182         // XXX
183
break;
184
185       case PROCESSING_INSTRUCTION:
186         return new ProcessingInstructionImpl(_in.getPIData(),
187                                              _in.getPITarget());
188
189       case SPACE:
190         return new CharactersImpl(_in.getText(), false, true, true);
191
192       case START_DOCUMENT:
193         boolean encodingSet = true;
194         String JavaDoc encoding = _in.getCharacterEncodingScheme();
195
196         if (encoding == null) {
197           encoding = "utf-8"; // XXX
198
encodingSet = false;
199         }
200
201         return new StartDocumentImpl(encodingSet, encoding,
202                                      null /* XXX: system id */,
203                                      _in.getVersion(),
204                                      _in.isStandalone(), _in.standaloneSet());
205
206       case START_ELEMENT:
207         HashMap JavaDoc<QName JavaDoc,Attribute> attributes = new HashMap JavaDoc<QName JavaDoc,Attribute>();
208
209         for (int i = 0; i < _in.getAttributeCount(); i++) {
210           Attribute attribute = new AttributeImpl(_in.getAttributeName(i),
211                                                   _in.getAttributeValue(i));
212           attributes.put(_in.getAttributeName(i), attribute);
213         }
214
215         HashMap JavaDoc<String JavaDoc,Namespace> namespaces= new HashMap JavaDoc<String JavaDoc,Namespace>();
216
217         for (int i = 0; i < _in.getNamespaceCount(); i++) {
218           Namespace namespace = new NamespaceImpl(_in.getNamespacePrefix(i),
219                                                   _in.getNamespaceURI(i));
220           namespaces.put(_in.getNamespacePrefix(i), namespace);
221         }
222
223         return new StartElementImpl(_in.getName(), attributes, namespaces,
224                                     _in.getNamespaceContext());
225     }
226
227     throw new XMLStreamException("Event type = " + _in.getEventType());
228   }
229 }
230
231
Popular Tags