KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xerces > internal > impl > XMLStreamFilterImpl


1 /*
2  * $Id: XMLStreamFilterImpl.java,v 1.5.2.3 2007/01/08 17:04:41 spericas Exp $
3  */

4
5 /*
6  * The contents of this file are subject to the terms
7  * of the Common Development and Distribution License
8  * (the License). You may not use this file except in
9  * compliance with the License.
10  *
11  * You can obtain a copy of the license at
12  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
13  * See the License for the specific language governing
14  * permissions and limitations under the License.
15  *
16  * When distributing Covered Code, include this CDDL
17  * Header Notice in each file and include the License file
18  * at https://glassfish.dev.java.net/public/CDDLv1.0.html.
19  * If applicable, add the following below the CDDL Header,
20  * with the fields enclosed by brackets [] replaced by
21  * you own identifying information:
22  * "Portions Copyrighted [year] [name of copyright owner]"
23  *
24  * [Name of File] [ver.__] [Date]
25  *
26  * Copyright 2006 Sun Microsystems Inc. All Rights Reserved
27  */

28
29
30 package com.sun.org.apache.xerces.internal.impl;
31
32 import javax.xml.XMLConstants JavaDoc;
33 import javax.xml.stream.Location;
34 import javax.xml.stream.XMLStreamReader;
35 import javax.xml.stream.StreamFilter;
36 import javax.xml.stream.XMLStreamException;
37 import javax.xml.namespace.QName JavaDoc;
38 import javax.xml.stream.events.XMLEvent;
39
40
41 /**
42  *
43  * @author Joe Wang:
44  * This is a rewrite of the original class. The focus is on removing caching, and make the filtered
45  * stream reader more compatible with those in other implementations. Note however, that this version
46  * will not solve all the issues related to the undefined condition in the spec. The priority is
47  * to pass the TCK. Issues arising due to the requirement, that is, (1) should it initiate at BEGIN_DOCUMENT
48  * or an accepted event; (2) should hasNext() advance the underlining stream in order to find an acceptable
49  * event, would have to wait until 1.1 of StAX in which the filtered stream reader would be defined more clearly.
50  */

51
52 public class XMLStreamFilterImpl implements javax.xml.stream.XMLStreamReader {
53     
54     private StreamFilter fStreamFilter = null;
55     private XMLStreamReader fStreamReader = null;
56     private int fCurrentEvent;
57     private boolean fEventAccepted = false;
58     
59     /**the very issue around a long discussion. but since we must pass the TCK, we have to allow
60      * hasNext() to advance the underlining stream in order to find the next acceptable event
61      */

62     private boolean fStreamAdvancedByHasNext = false;
63     
64     /** Creates a new instance of XMLStreamFilterImpl */
65     
66     public XMLStreamFilterImpl(XMLStreamReader reader,StreamFilter filter){
67         fStreamReader = reader;
68         this.fStreamFilter = filter;
69         
70         //this is debatable to initiate at an acceptable event,
71
//but it's neccessary in order to pass the TCK and yet avoid skipping element
72
try {
73             if (fStreamFilter.accept(fStreamReader)) {
74                 fEventAccepted = true;
75             } else {
76                 findNextEvent();
77             }
78         }catch(XMLStreamException xs){
79             System.err.println("Error while creating a stream Filter"+xs);
80         }
81     }
82     
83     /**
84      *
85      * @param sf
86      */

87     protected void setStreamFilter(StreamFilter sf){
88         this.fStreamFilter = sf;
89     }
90     
91     /**
92      *
93      * @return
94      * @throws XMLStreamException
95      */

96     public int next() throws XMLStreamException {
97         if (fStreamAdvancedByHasNext && fEventAccepted) {
98             fStreamAdvancedByHasNext = false;
99             return fCurrentEvent;
100         }
101         int event = findNextEvent();
102         if (event != -1) {
103             return event;
104         }
105         
106         throw new IllegalStateException JavaDoc("The stream reader has reached the end of the document, or there are no more "+
107                                     " items to return");
108     }
109     /**
110      *
111      * @throws XMLStreamException
112      * @return
113      */

114     public int nextTag() throws XMLStreamException {
115         if (fStreamAdvancedByHasNext && fEventAccepted &&
116                 (fCurrentEvent == XMLEvent.START_ELEMENT || fCurrentEvent == XMLEvent.START_ELEMENT)) {
117             fStreamAdvancedByHasNext = false;
118             return fCurrentEvent;
119         }
120         
121         int event = findNextTag();
122         if (event != -1) {
123             return event;
124         }
125         throw new IllegalStateException JavaDoc("The stream reader has reached the end of the document, or there are no more "+
126                                     " items to return");
127     }
128
129     /**
130      *
131      * @throws XMLStreamException
132      * @return
133      */

134     public boolean hasNext() throws XMLStreamException {
135         if (fStreamReader.hasNext()) {
136             if (!fEventAccepted) {
137                 if ((fCurrentEvent = findNextEvent()) == -1) {
138                     return false;
139                 } else {
140                     fStreamAdvancedByHasNext = true;
141                 }
142             }
143             return true;
144         }
145         return false;
146     }
147
148     private int findNextEvent() throws XMLStreamException {
149         fStreamAdvancedByHasNext = false;
150         while(fStreamReader.hasNext()){
151             fCurrentEvent = fStreamReader.next();
152             if(fStreamFilter.accept(fStreamReader)){
153                 fEventAccepted = true;
154                 return fCurrentEvent;
155             }
156         }
157         //although it seems that IllegalStateException should be thrown when next() is called
158
//on a stream that has no more items, we have to assume END_DOCUMENT is always accepted
159
//in order to pass the TCK
160
if (fCurrentEvent == XMLEvent.END_DOCUMENT)
161             return fCurrentEvent;
162         else
163             return -1;
164     }
165     private int findNextTag() throws XMLStreamException {
166         fStreamAdvancedByHasNext = false;
167         while(fStreamReader.hasNext()){
168             fCurrentEvent = fStreamReader.nextTag();
169             if(fStreamFilter.accept(fStreamReader)){
170                 fEventAccepted = true;
171                 return fCurrentEvent;
172             }
173         }
174         if (fCurrentEvent == XMLEvent.END_DOCUMENT)
175             return fCurrentEvent;
176         else
177             return -1;
178     }
179     /**
180      *
181      * @throws XMLStreamException
182      */

183     public void close() throws XMLStreamException {
184         fStreamReader.close();
185     }
186     
187     /**
188      *
189      * @return
190      */

191     public int getAttributeCount() {
192         return fStreamReader.getAttributeCount();
193     }
194     
195     /**
196      *
197      * @param index
198      * @return
199      */

200     public QName JavaDoc getAttributeName(int index) {
201         return fStreamReader.getAttributeName(index);
202     }
203     
204     /**
205      *
206      * @param index
207      * @return
208      */

209     public String JavaDoc getAttributeNamespace(int index) {
210         return fStreamReader.getAttributeNamespace(index);
211     }
212     
213     /**
214      *
215      * @param index
216      * @return
217      */

218     public String JavaDoc getAttributePrefix(int index) {
219         return fStreamReader.getAttributePrefix(index);
220     }
221     
222     /**
223      *
224      * @param index
225      * @return
226      */

227     public String JavaDoc getAttributeType(int index) {
228         return fStreamReader.getAttributeType(index);
229     }
230     
231     /**
232      *
233      * @param index
234      * @return
235      */

236     public String JavaDoc getAttributeValue(int index) {
237         return fStreamReader.getAttributeValue(index);
238     }
239     
240     /**
241      *
242      * @param namespaceURI
243      * @param localName
244      * @return
245      */

246     public String JavaDoc getAttributeValue(String JavaDoc namespaceURI, String JavaDoc localName) {
247         return fStreamReader.getAttributeValue(namespaceURI,localName);
248     }
249     
250     /**
251      *
252      * @return
253      */

254     public String JavaDoc getCharacterEncodingScheme() {
255         return fStreamReader.getCharacterEncodingScheme();
256     }
257     
258     /**
259      *
260      * @throws XMLStreamException
261      * @return
262      */

263     public String JavaDoc getElementText() throws XMLStreamException {
264         return fStreamReader.getElementText();
265     }
266     
267     /**
268      *
269      * @return
270      */

271     public String JavaDoc getEncoding() {
272         return fStreamReader.getEncoding();
273     }
274     
275     /**
276      *
277      * @return
278      */

279     public int getEventType() {
280         return fStreamReader.getEventType();
281     }
282     
283     /**
284      *
285      * @return
286      */

287     public String JavaDoc getLocalName() {
288         return fStreamReader.getLocalName();
289     }
290     
291     /**
292      *
293      * @return
294      */

295     public javax.xml.stream.Location getLocation() {
296         return fStreamReader.getLocation();
297     }
298     
299     /**
300      *
301      * @return
302      */

303     public javax.xml.namespace.QName JavaDoc getName() {
304         return fStreamReader.getName();
305     }
306     
307     /**
308      *
309      * @return
310      */

311     public javax.xml.namespace.NamespaceContext JavaDoc getNamespaceContext() {
312         return fStreamReader.getNamespaceContext();
313     }
314     
315     /**
316      *
317      * @return
318      */

319     public int getNamespaceCount() {
320         return fStreamReader.getNamespaceCount();
321     }
322     
323     /**
324      *
325      * @param index
326      * @return
327      */

328     public String JavaDoc getNamespacePrefix(int index) {
329         return fStreamReader.getNamespacePrefix(index);
330     }
331     
332     /**
333      *
334      * @return
335      */

336     public String JavaDoc getNamespaceURI() {
337         return fStreamReader.getNamespaceURI();
338     }
339     
340     /**
341      *
342      * @param index
343      * @return
344      */

345     public String JavaDoc getNamespaceURI(int index) {
346         return fStreamReader.getNamespaceURI(index);
347     }
348     
349     /**
350      *
351      * @param prefix
352      * @return
353      */

354     public String JavaDoc getNamespaceURI(String JavaDoc prefix) {
355         return fStreamReader.getNamespaceURI(prefix);
356     }
357     
358     /**
359      *
360      * @return
361      */

362     public String JavaDoc getPIData() {
363         return fStreamReader.getPIData();
364     }
365     
366     /**
367      *
368      * @return
369      */

370     public String JavaDoc getPITarget() {
371         return fStreamReader.getPITarget();
372     }
373     
374     /**
375      *
376      * @return
377      */

378     public String JavaDoc getPrefix() {
379         return fStreamReader.getPrefix();
380     }
381     
382     /**
383      *
384      * @param name
385      * @throws IllegalArgumentException
386      * @return
387      */

388     public Object JavaDoc getProperty(java.lang.String JavaDoc name) throws java.lang.IllegalArgumentException JavaDoc {
389         return fStreamReader.getProperty(name);
390     }
391     
392     /**
393      *
394      * @return
395      */

396     public String JavaDoc getText() {
397         return fStreamReader.getText();
398     }
399     
400     /**
401      *
402      * @return
403      */

404     public char[] getTextCharacters() {
405         return fStreamReader.getTextCharacters();
406     }
407     
408     /**
409      *
410      * @param sourceStart
411      * @param target
412      * @param targetStart
413      * @param length
414      * @throws XMLStreamException
415      * @return
416      */

417     public int getTextCharacters(int sourceStart, char[] target, int targetStart, int length) throws XMLStreamException {
418         return fStreamReader.getTextCharacters(sourceStart, target,targetStart,length);
419     }
420     
421     /**
422      *
423      * @return
424      */

425     public int getTextLength() {
426         return fStreamReader.getTextLength();
427     }
428     
429     /**
430      *
431      * @return
432      */

433     public int getTextStart() {
434         return fStreamReader.getTextStart();
435     }
436     
437     /**
438      *
439      * @return
440      */

441     public String JavaDoc getVersion() {
442         return fStreamReader.getVersion();
443         
444     }
445     
446     /**
447      *
448      * @return
449      */

450     public boolean hasName() {
451         return fStreamReader.hasName();
452     }
453     
454     /**
455      *
456      * @return
457      */

458     public boolean hasText() {
459         return fStreamReader.hasText();
460     }
461     
462     /**
463      *
464      * @return
465      * @param index
466      */

467     public boolean isAttributeSpecified(int index) {
468         return fStreamReader.isAttributeSpecified(index);
469     }
470     
471     /**
472      *
473      * @return
474      */

475     public boolean isCharacters() {
476         return fStreamReader.isCharacters();
477     }
478     
479     /**
480      *
481      * @return
482      */

483     public boolean isEndElement() {
484         return fStreamReader.isEndElement();
485     }
486     
487     /**
488      *
489      * @return
490      */

491     public boolean isStandalone() {
492         return fStreamReader.isStandalone();
493     }
494     
495     /**
496      *
497      * @return
498      */

499     public boolean isStartElement() {
500         return fStreamReader.isStartElement();
501     }
502     
503     /**
504      *
505      * @return
506      */

507     public boolean isWhiteSpace() {
508         return fStreamReader.isWhiteSpace();
509     }
510     
511         
512     /**
513      *
514      * @param type
515      * @param namespaceURI
516      * @param localName
517      * @throws XMLStreamException
518      */

519     public void require(int type, String JavaDoc namespaceURI, String JavaDoc localName) throws XMLStreamException {
520         fStreamReader.require(type,namespaceURI,localName);
521     }
522     
523     /**
524      *
525      * @return
526      */

527     public boolean standaloneSet() {
528         return fStreamReader.standaloneSet();
529     }
530     
531     /**
532      *
533      * @param index
534      * @return
535      */

536     public String JavaDoc getAttributeLocalName(int index){
537         return fStreamReader.getAttributeLocalName(index);
538     }
539         
540 }
541
Popular Tags