1/*
2 * Copyright (c) 2005, 2017, 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
26package com.sun.xml.internal.stream;
27
28import java.util.NoSuchElementException;
29import javax.xml.stream.EventFilter;
30import javax.xml.stream.XMLEventReader;
31import javax.xml.stream.XMLStreamException;
32import javax.xml.stream.events.XMLEvent;
33import javax.xml.stream.util.EventReaderDelegate;
34
35/**
36 *
37 * @author  Neeraj Bajaj, Sun Microsystems
38 *
39 */
40public class EventFilterSupport extends EventReaderDelegate {
41
42    //maintain a reference to EventFilter
43    EventFilter fEventFilter ;
44    /** Creates a new instance of EventFilterSupport */
45    public EventFilterSupport(XMLEventReader eventReader, EventFilter eventFilter) {
46        setParent(eventReader);
47        fEventFilter = eventFilter;
48    }
49
50    public Object next(){
51        try{
52            return nextEvent();
53        }catch(XMLStreamException ex){
54            throw new NoSuchElementException();
55        }
56    }
57
58    public boolean hasNext(){
59        try{
60            return peek() != null ? true : false ;
61        }catch(XMLStreamException ex){
62            return false;
63        }
64    }
65
66    public XMLEvent nextEvent()throws XMLStreamException{
67        while (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        }
76        throw new NoSuchElementException();
77    }//nextEvent()
78
79     public XMLEvent nextTag() throws XMLStreamException{
80         while (super.hasNext()) {
81             XMLEvent event = super.nextTag();
82             //if the filter accepts this event return this event.
83             if(fEventFilter.accept(event)){
84                return event;
85             }
86         }
87         throw new NoSuchElementException();
88     }
89
90     public XMLEvent peek() throws XMLStreamException{
91         while (true) {
92             XMLEvent event = super.peek();
93             if(event == null)return null;
94             //if the filter accepts this event return this event.
95             if(fEventFilter.accept(event)){
96                return event;
97             }
98             //call super.next(), and then peek again.
99             super.next();
100         }
101     }
102
103}//EventFilterSupport
104