1 /*
2  * Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.  Oracle designates this
8  * particular file as subject to the "Classpath" exception as provided
9  * by Oracle in the LICENSE file that accompanied this code.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */
25 
26 package com.sun.xml.internal.stream;
27 
28 import java.util.NoSuchElementException;
29 import javax.xml.stream.EventFilter;
30 import javax.xml.stream.XMLEventReader;
31 import javax.xml.stream.XMLStreamException;
32 import javax.xml.stream.events.XMLEvent;
33 import javax.xml.stream.util.EventReaderDelegate;
34 
35 /**
36  *
37  * @author  Neeraj Bajaj, Sun Microsystems
38  *
39  */
40 public class EventFilterSupport extends EventReaderDelegate {
41 
42     //maintain a reference to EventFilter
43     EventFilter fEventFilter ;
44     /** Creates a new instance of EventFilterSupport */
EventFilterSupport(XMLEventReader eventReader, EventFilter eventFilter)45     public EventFilterSupport(XMLEventReader eventReader, EventFilter eventFilter) {
46         setParent(eventReader);
47         fEventFilter = eventFilter;
48     }
49 
next()50     public Object next(){
51         try{
52             return nextEvent();
53         }catch(XMLStreamException ex){
54             throw new NoSuchElementException();
55         }
56     }
57 
hasNext()58     public boolean hasNext(){
59         try{
60             return peek() != null ? true : false ;
61         }catch(XMLStreamException ex){
62             return false;
63         }
64     }
65 
nextEvent()66     public XMLEvent nextEvent()throws XMLStreamException{
67         if(super.hasNext()){
68             //get the next event by calling XMLEventReader
69             XMLEvent event = super.nextEvent();
70 
71             //if this filter accepts this event then return this event
72             if(fEventFilter.accept(event)){
73                 return event;
74             }
75             else{
76                 return nextEvent();
77             }
78         }else{
79             throw new NoSuchElementException();
80         }
81     }//nextEvent()
82 
nextTag()83      public XMLEvent nextTag() throws XMLStreamException{
84          if(super.hasNext()){
85              XMLEvent event = super.nextTag();
86              //if the filter accepts this event return this event.
87              if(fEventFilter.accept(event)){
88                 return event;
89              }
90              else{
91                 return nextTag();
92              }
93          }else{
94              throw new NoSuchElementException();
95          }
96      }
97 
peek()98      public XMLEvent peek() throws XMLStreamException{
99          while (true) {
100              XMLEvent event = super.peek();
101              if(event == null)return null;
102              //if the filter accepts this event return this event.
103              if(fEventFilter.accept(event)){
104                 return event;
105              }
106              //call super.next(), and then peek again.
107              super.next();
108          }
109      }
110 
111 }//EventFilterSupport
112