001: /*
002: * (C) Copyright 2002-2003, Andy Clark. All rights reserved.
003: *
004: * This file is distributed under an Apache style license. Please
005: * refer to the LICENSE file for specific details.
006: */
007:
008: package org.cyberneko.pull.util;
009:
010: import org.cyberneko.pull.XMLEvent;
011: import org.cyberneko.pull.XMLEventIterator;
012: import org.cyberneko.pull.event.CDATAEvent;
013: import org.cyberneko.pull.event.CharactersEvent;
014: import org.cyberneko.pull.event.CommentEvent;
015: import org.cyberneko.pull.event.DoctypeDeclEvent;
016: import org.cyberneko.pull.event.DocumentEvent;
017: import org.cyberneko.pull.event.ElementEvent;
018: import org.cyberneko.pull.event.GeneralEntityEvent;
019: import org.cyberneko.pull.event.PrefixMappingEvent;
020: import org.cyberneko.pull.event.ProcessingInstructionEvent;
021: import org.cyberneko.pull.event.TextDeclEvent;
022:
023: import java.io.IOException;
024:
025: import org.apache.xerces.xni.XNIException;
026:
027: /**
028: * This class contains a set of utility functions to allow applications
029: * to more conveniently iterate XML events instead of having to call
030: * <code>nextEvent</code> for each event in the event stream.
031: * <p color='red'>
032: * <strong>Note:</strong>
033: * Currently, the <code>XMLEventIterator</code> interface only contains
034: * the single, low-level <code>nextToken</code> method for iterating XML
035: * events. The <code>EventIterator</code> utility class is provided to
036: * handle this shortcoming. As this API progresses, it is believed that
037: * additional methods will be added to the <code>XMLEventIterator</code>
038: * interface. However, this will be decided by actual users of the API.
039: * Please <a href='mailto:andyc@apache.org'>let me know</a> if you have
040: * any suggestions or comments.
041: * </p>
042: *
043: * @author Andy Clark
044: *
045: * @version $Id$
046: */
047: public class EventIterator implements XMLEventIterator {
048:
049: //
050: // Data
051: //
052:
053: // iterator
054:
055: /** XML event iterator. */
056: protected XMLEventIterator fEventIterator;
057:
058: // state
059:
060: /** Pushback event. */
061: protected XMLEvent fPushbackEvent;
062:
063: /** Element depth. */
064: protected int fElementDepth;
065:
066: //
067: // Constructors
068: //
069:
070: /** Constructs an event iterator from the specified event iterator. */
071: public EventIterator(XMLEventIterator iterator) {
072: fEventIterator = iterator;
073: } // <init>(XMLEventIterator)
074:
075: //
076: // XMLEventIterator methods
077: //
078:
079: /** Returns the next event. */
080: public XMLEvent nextEvent() throws IOException, XNIException {
081: XMLEvent event;
082: if (fPushbackEvent != null) {
083: event = fPushbackEvent;
084: fPushbackEvent = null;
085: } else {
086: event = fEventIterator.nextEvent();
087: }
088: if (event != null) {
089: switch (event.type) {
090: case XMLEvent.DOCUMENT: {
091: fElementDepth = 0;
092: break;
093: }
094: case XMLEvent.ELEMENT: {
095: ElementEvent elementEvent = (ElementEvent) event;
096: fElementDepth += elementEvent.start ? 1 : -1;
097: break;
098: }
099: }
100: }
101: return event;
102: } // nextEvent():XMLEvent
103:
104: //
105: // Public methods
106: //
107:
108: /** Returns the nextEvent of the given type. */
109: public XMLEvent nextEvent(short type) throws IOException,
110: XNIException {
111: do {
112: XMLEvent event = nextEvent();
113: if (event == null) {
114: break;
115: }
116: if (event.type == type) {
117: return event;
118: }
119: } while (true);
120: return null;
121: } // nextEvent(short):XMLEvent
122:
123: // TODO: [Q] What methods would be useful here? -Ac
124:
125: } // class EventIterator
|