View Javadoc

1   /*   Copyright 2004 BEA Systems, Inc.
2    *
3    *   Licensed under the Apache License, Version 2.0 (the "License");
4    *   you may not use this file except in compliance with the License.
5    *   You may obtain a copy of the License at
6    *
7    *       http://www.apache.org/licenses/LICENSE-2.0
8    *
9    *   Unless required by applicable law or agreed to in writing, software
10   *   distributed under the License is distributed on an "AS IS" BASIS,
11   *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   *   See the License for the specific language governing permissions and
13   *   limitations under the License.
14   */
15  
16  package com.bea.xml.stream;
17  
18  import com.bea.xml.stream.util.CircularQueue;
19  import com.bea.xml.stream.util.ElementTypeNames;
20  
21  import javax.xml.stream.events.XMLEvent;
22  import javax.xml.stream.events.Characters;
23  import javax.xml.stream.XMLStreamReader;
24  import javax.xml.stream.XMLEventReader;
25  import javax.xml.stream.util.XMLEventAllocator;
26  import javax.xml.stream.util.XMLEventConsumer;
27  import javax.xml.stream.XMLStreamException;
28  import javax.xml.stream.XMLInputFactory;
29  
30  /***
31   * <p>The base reader class.</p>
32   */
33  
34  public class XMLEventReaderBase 
35    implements XMLEventReader, XMLEventConsumer
36  {
37    private CircularQueue elementQ = new CircularQueue();
38    private boolean open = true;
39    protected XMLStreamReader reader;
40    protected XMLEventAllocator allocator;
41    private boolean reachedEOF=false;
42    private ConfigurationContextBase configurationContext;
43  
44    public XMLEventReaderBase(XMLStreamReader reader)
45  
46      throws XMLStreamException
47    {
48      this(reader, new XMLEventAllocatorBase());
49    }
50  
51    public XMLEventReaderBase(XMLStreamReader reader,
52                              XMLEventAllocator alloc) 
53      throws XMLStreamException
54    {
55      if (reader==null) 
56        throw new IllegalArgumentException("XMLStreamReader"+
57                                           " may not be null");
58      if (alloc==null) 
59        throw new IllegalArgumentException("XMLEvent Allocator"+
60                                           " may not be null");
61  
62      this.reader = reader;
63      open = true;
64      // create the allocator
65      this.allocator = alloc;
66      //    System.out.println("Allocator->"+allocator);
67  
68      // This check fills the information from the XMLDeclaration
69      // into the startdocument event
70      if (reader.getEventType()==XMLEvent.START_DOCUMENT) {
71        XMLEvent e = allocator.allocate(reader);
72        reader.next();
73        add(e);
74      }
75    }
76  
77  
78    public void setAllocator(XMLEventAllocator allocator) {
79      if (allocator == null)
80        throw new IllegalArgumentException("XMLEvent Allocator"+
81                                           " may not be null");
82  
83      this.allocator = allocator;
84    }
85  
86    public String getElementText() throws XMLStreamException {
87      StringBuffer buf = new StringBuffer();
88      XMLEvent e = nextEvent();
89      if (!e.isStartElement())
90        throw new XMLStreamException(
91                                     "Precondition for readText is"+
92                                     " nextEvent().getTypeEventType() == START_ELEMENT");
93      while(hasNext()) {
94        e = peek();
95        if(e.isStartElement())
96          throw new XMLStreamException("Unexpected Element start");
97        if(e.isCharacters())
98          buf.append(((Characters) e).getData());
99        if(e.isEndElement())
100         return buf.toString();
101       nextEvent();
102     } 
103     throw new XMLStreamException("Unexpected end of Document");
104 
105   }
106 
107   public XMLEvent nextTag() throws XMLStreamException {
108     while(hasNext()) {
109       XMLEvent e = nextEvent();
110       if (e.isCharacters() && !((Characters) e).isWhiteSpace())
111         throw new XMLStreamException("Unexpected text");
112       if (e.isStartElement() || e.isEndElement())
113         return e;
114     }
115     throw new XMLStreamException("Unexpected end of Document");
116   }
117 
118   public Object next() {
119     try {
120       return nextEvent();
121     } catch (XMLStreamException e) {
122       return null;
123     }
124   }
125 
126   public XMLEvent nextEvent()
127     throws XMLStreamException
128   {
129     if (!open) 
130       throw new XMLStreamException("Attempt to read from a stream "+
131                                    "that is not open");
132 
133     // FIXME cfry throw error if parseSome fails
134     if (needsMore()) 
135       if (!parseSome())
136         throw new java.util.NoSuchElementException("Attempt to call nextEvent()"+
137                                                    " on a stream with no"+
138                                                    " more elements");
139     return get();
140   }
141 
142   public boolean hasNext() 
143   {
144     if (!open) return false;
145     if (!elementQ.isEmpty()) return true;
146     try {
147       if (reader.hasNext()) return true;
148     } catch (XMLStreamException e) {
149       return false;
150     }
151     open = false;
152     return false;
153   }
154 
155   public XMLEvent peek() 
156     throws XMLStreamException
157   {
158     if (!elementQ.isEmpty()) 
159       return (XMLEvent) elementQ.peek();
160     if (parseSome()) 
161       return (XMLEvent) elementQ.peek();
162     
163     throw new java.util.NoSuchElementException("Attempt to peek() on a " +
164                                                " stream that has no more " +
165                                                " elements.");
166   }
167 
168   public void add(XMLEvent event) 
169     throws XMLStreamException
170   {
171     elementQ.add(event);
172   }
173 
174   protected boolean needsMore() {
175     return elementQ.isEmpty();
176   }
177 
178   protected XMLEvent get() 
179     throws XMLStreamException
180   {
181     return (XMLEvent) elementQ.remove();
182   }
183 
184   protected boolean isOpen() {
185     return !reachedEOF;
186   }
187 
188   protected void internal_close() {
189     reachedEOF = true;
190   }
191 
192   public void close() 
193     throws XMLStreamException
194   {
195     internal_close();
196   }
197    
198   protected boolean parseSome() 
199     throws XMLStreamException
200   {
201     //    System.out.println("Allocator->"+allocator);
202     allocator.allocate(reader,this);
203     if (reader.hasNext())
204       reader.next();
205     if (!reachedEOF && reader.getEventType() == XMLEvent.END_DOCUMENT) {
206       allocator.allocate(reader,this);
207       reachedEOF = true;
208     }
209     return !needsMore();
210   }
211 
212   public void setConfigurationContext(ConfigurationContextBase base) {
213     configurationContext = base;
214   }
215   
216   public Object getProperty(String name) {
217     return configurationContext.getProperty(name);
218   }
219   public void remove() {
220     throw new java.lang.UnsupportedOperationException();
221   }
222   public static void main(String args[]) throws Exception {
223 
224     System.setProperty("javax.xml.stream.XMLInputFactory", 
225                        "com.bea.xml.stream.MXParserFactory");
226     System.setProperty("javax.xml.stream.XMLEventFactory", 
227                        "com.bea.xml.stream.EventFactory");
228 
229     XMLInputFactory factory = XMLInputFactory.newInstance();
230     XMLEventReader xmlr = factory.createXMLEventReader(new java.io.FileReader(args[0]));
231     
232     while(xmlr.hasNext()) {
233       XMLEvent e = xmlr.nextEvent();
234       System.out.println("["+
235                          ElementTypeNames.getEventTypeString(e.getEventType())
236                          +"]["+
237                          e+"]");
238     }
239   }
240   //  public ConfigurationContext getConfigurationContext() {
241   //  return new ConfigurationContextBase();
242   // }
243 
244 }