KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > xml > fastinfoset > stax > StAXEventReader


1 /*
2  * Fast Infoset ver. 0.1 software ("Software")
3  *
4  * Copyright, 2004-2005 Sun Microsystems, Inc. All Rights Reserved.
5  *
6  * Software is licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License. You may
8  * obtain a copy of the License at:
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15  * License for the specific language governing permissions and limitations.
16  *
17  * Sun supports and benefits from the global community of open source
18  * developers, and thanks the community for its important contributions and
19  * open standards-based technology, which Sun has adopted into many of its
20  * products.
21  *
22  * Please note that portions of Software may be provided with notices and
23  * open source licenses from such communities and third parties that govern the
24  * use of those portions, and any licenses granted hereunder do not alter any
25  * rights and obligations you may have under such open source licenses,
26  * however, the disclaimer of warranty and limitation of liability provisions
27  * in this License will apply to all Software in this distribution.
28  *
29  * You acknowledge that the Software is not designed, licensed or intended
30  * for use in the design, construction, operation or maintenance of any nuclear
31  * facility.
32  *
33  * Apache License
34  * Version 2.0, January 2004
35  * http://www.apache.org/licenses/
36  *
37  */

38
39 package com.sun.xml.fastinfoset.stax;
40
41 import com.sun.xml.fastinfoset.stax.events.StAXEventAllocatorBase;
42 import com.sun.xml.fastinfoset.stax.events.StAXEventAllocator;
43 import java.util.NoSuchElementException JavaDoc;
44 import javax.xml.stream.XMLInputFactory;
45 import javax.xml.stream.XMLStreamConstants;
46 import javax.xml.stream.XMLStreamException;
47 import javax.xml.stream.XMLStreamReader;
48 import javax.xml.stream.events.EntityReference;
49 import javax.xml.stream.events.XMLEvent;
50 import javax.xml.stream.util.XMLEventAllocator;
51 import com.sun.xml.fastinfoset.CommonResourceBundle;
52
53
54
55 public class StAXEventReader implements javax.xml.stream.XMLEventReader{
56     protected XMLStreamReader _streamReader ;
57     protected XMLEventAllocator _eventAllocator;
58     private XMLEvent _currentEvent; //the current event
59
private XMLEvent[] events = new XMLEvent[3];
60     private int size = 3;
61     private int currentIndex = 0;
62     private boolean hasEvent = false; //true when current event exists, false initially & at end
63

64     //only constructor will do because we delegate everything to underlying XMLStreamReader
65
public StAXEventReader(XMLStreamReader reader) throws XMLStreamException {
66         _streamReader = reader ;
67         _eventAllocator = (XMLEventAllocator)reader.getProperty(XMLInputFactory.ALLOCATOR);
68         if(_eventAllocator == null){
69             _eventAllocator = new StAXEventAllocatorBase();
70         }
71         //initialize
72
if (_streamReader.hasNext())
73         {
74             _streamReader.next();
75             _currentEvent =_eventAllocator.allocate(_streamReader);
76             events[0] = _currentEvent;
77             System.out.println("current event:" + _currentEvent);
78             hasEvent = true;
79         } else {
80             throw new XMLStreamException(CommonResourceBundle.getInstance().getString("message.noElement"));
81         }
82     }
83         
84     public boolean hasNext() {
85         return hasEvent;
86     }
87     
88     public XMLEvent nextEvent() throws XMLStreamException {
89         XMLEvent event = null;
90         XMLEvent nextEvent = null;
91         if (hasEvent)
92         {
93             event = events[currentIndex];
94             events[currentIndex] = null;
95             if (_streamReader.hasNext())
96             {
97                 //advance and read the next
98
_streamReader.next();
99                 nextEvent = _eventAllocator.allocate(_streamReader);
100                 if (++currentIndex==size)
101                     currentIndex = 0;
102                 events[currentIndex] = nextEvent;
103                 hasEvent = true;
104             } else {
105                 _currentEvent = null;
106                 hasEvent = false;
107             }
108             return event;
109         }
110         else{
111             throw new NoSuchElementException JavaDoc();
112         }
113     }
114     
115     public void remove(){
116         //stream reader is read-only.
117
throw new java.lang.UnsupportedOperationException JavaDoc();
118     }
119     
120     
121     public void close() throws XMLStreamException {
122         _streamReader.close();
123     }
124     
125     /** Reads the content of a text-only element. Precondition:
126      * the current event is START_ELEMENT. Postcondition:
127      * The current event is the corresponding END_ELEMENT.
128      * @throws XMLStreamException if the current event is not a START_ELEMENT
129      * or if a non text element is encountered
130      */

131     public String JavaDoc getElementText() throws XMLStreamException {
132         if(!hasEvent) {
133             throw new NoSuchElementException JavaDoc();
134         }
135                 
136         if(!_currentEvent.isStartElement()) {
137             StAXDocumentParser parser = (StAXDocumentParser)_streamReader;
138             return parser.getElementText(true);
139         } else {
140             return _streamReader.getElementText();
141         }
142     }
143     
144     /** Get the value of a feature/property from the underlying implementation
145      * @param name The name of the property
146      * @return The value of the property
147      * @throws IllegalArgumentException if the property is not supported
148      */

149     public Object JavaDoc getProperty(java.lang.String JavaDoc name) throws java.lang.IllegalArgumentException JavaDoc {
150         return _streamReader.getProperty(name) ;
151     }
152     
153     /** Skips any insignificant space events until a START_ELEMENT or
154      * END_ELEMENT is reached. If anything other than space characters are
155      * encountered, an exception is thrown. This method should
156      * be used when processing element-only content because
157      * the parser is not able to recognize ignorable whitespace if
158      * the DTD is missing or not interpreted.
159      * @throws XMLStreamException if anything other than space characters are encountered
160      */

161     public XMLEvent nextTag() throws XMLStreamException {
162         if(!hasEvent) {
163             throw new NoSuchElementException JavaDoc();
164         }
165         StAXDocumentParser parser = (StAXDocumentParser)_streamReader;
166         parser.nextTag(true);
167         return _eventAllocator.allocate(_streamReader);
168     }
169     
170     //XMLEventReader extends Iterator;
171
public Object JavaDoc next() {
172         try{
173             return nextEvent();
174         }catch(XMLStreamException streamException){
175             return null;
176         }
177     }
178     
179     public XMLEvent peek() throws XMLStreamException{
180         if (!hasEvent)
181              throw new XMLStreamException(CommonResourceBundle.getInstance().getString("message.noElement"));
182         _currentEvent = events[currentIndex];
183         return _currentEvent;
184     }
185
186     public void setAllocator(XMLEventAllocator allocator) {
187         if (allocator == null)
188             throw new IllegalArgumentException JavaDoc(CommonResourceBundle.getInstance().getString("message.nullXMLEventAllocator"));
189
190         _eventAllocator = allocator;
191     }
192     
193     
194 }
195
Popular Tags