001: /*
002: * $Id: EventFilterSupport.java,v 1.2 2006/04/01 06:01:48 jeffsuttor Exp $
003: */
004:
005: /*
006: * The contents of this file are subject to the terms
007: * of the Common Development and Distribution License
008: * (the License). You may not use this file except in
009: * compliance with the License.
010: *
011: * You can obtain a copy of the license at
012: * https://glassfish.dev.java.net/public/CDDLv1.0.html.
013: * See the License for the specific language governing
014: * permissions and limitations under the License.
015: *
016: * When distributing Covered Code, include this CDDL
017: * Header Notice in each file and include the License file
018: * at https://glassfish.dev.java.net/public/CDDLv1.0.html.
019: * If applicable, add the following below the CDDL Header,
020: * with the fields enclosed by brackets [] replaced by
021: * you own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * [Name of File] [ver.__] [Date]
025: *
026: * Copyright 2006 Sun Microsystems Inc. All Rights Reserved
027: */
028:
029: package com.sun.xml.stream;
030:
031: import java.util.NoSuchElementException;
032: import javax.xml.stream.EventFilter;
033: import javax.xml.stream.XMLEventReader;
034: import javax.xml.stream.XMLStreamException;
035: import javax.xml.stream.events.XMLEvent;
036: import javax.xml.stream.util.EventReaderDelegate;
037:
038: /**
039: *
040: * @author Neeraj Bajaj, Sun Microsystems
041: *
042: */
043: public class EventFilterSupport extends EventReaderDelegate {
044:
045: //maintain a reference to EventFilter
046: EventFilter fEventFilter;
047:
048: /** Creates a new instance of EventFilterSupport */
049: public EventFilterSupport(XMLEventReader eventReader,
050: EventFilter eventFilter) {
051: setParent(eventReader);
052: fEventFilter = eventFilter;
053: }
054:
055: public Object next() {
056: try {
057: return nextEvent();
058: } catch (XMLStreamException ex) {
059: throw new NoSuchElementException();
060: }
061: }
062:
063: public boolean hasNext() {
064: try {
065: return peek() != null ? true : false;
066: } catch (XMLStreamException ex) {
067: return false;
068: }
069: }
070:
071: public XMLEvent nextEvent() throws XMLStreamException {
072: if (super .hasNext()) {
073: //get the next event by calling XMLEventReader
074: XMLEvent event = super .nextEvent();
075:
076: //if this filter accepts this event then return this event
077: if (fEventFilter.accept(event)) {
078: return event;
079: } else {
080: return nextEvent();
081: }
082: } else {
083: throw new NoSuchElementException();
084: }
085: }//nextEvent()
086:
087: public XMLEvent nextTag() throws XMLStreamException {
088: if (super .hasNext()) {
089: XMLEvent event = super .nextTag();
090: //if the filter accepts this event return this event.
091: if (fEventFilter.accept(event)) {
092: return event;
093: } else {
094: return nextTag();
095: }
096: } else {
097: throw new NoSuchElementException();
098: }
099: }
100:
101: public XMLEvent peek() throws XMLStreamException {
102:
103: XMLEvent event = super .peek();
104: if (event == null)
105: return null;
106: //if the filter accepts this event return this event.
107: if (fEventFilter.accept(event)) {
108: return event;
109: } else {
110: //call super.next(), and then peek again.
111: super .next();
112: return peek();
113: }
114:
115: }
116:
117: }//EventFilterSupport
|