001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Emil Ong
028: */
029:
030: package com.caucho.xml.stream;
031:
032: import com.caucho.util.L10N;
033: import com.caucho.xml.stream.events.*;
034:
035: import javax.xml.namespace.QName;
036: import javax.xml.stream.XMLEventFactory;
037: import javax.xml.stream.XMLEventReader;
038: import static javax.xml.stream.XMLStreamConstants.*;
039: import javax.xml.stream.XMLStreamException;
040: import javax.xml.stream.XMLStreamReader;
041: import javax.xml.stream.events.*;
042: import javax.xml.stream.util.XMLEventAllocator;
043:
044: import java.util.HashMap;
045:
046: public class XMLEventReaderImpl implements XMLEventReader {
047: public static final L10N L = new L10N(XMLEventReaderImpl.class);
048:
049: private final XMLEventAllocator _allocator;
050: private final XMLStreamReader _in;
051: private XMLEvent _current = null;
052: private XMLEvent _next = null;
053:
054: public XMLEventReaderImpl(XMLEventAllocator allocator,
055: XMLStreamReader in) throws XMLStreamException {
056: _in = in;
057: _allocator = allocator;
058: _next = _allocator.allocate(_in);
059: }
060:
061: public XMLStreamReader getXMLStreamReader() {
062: return _in;
063: }
064:
065: public void close() throws XMLStreamException {
066: _in.close();
067: }
068:
069: public String getElementText() throws XMLStreamException {
070: return _in.getElementText();
071: }
072:
073: public Object getProperty(String name)
074: throws IllegalArgumentException {
075: throw new IllegalArgumentException(name);
076: }
077:
078: public boolean hasNext() {
079: try {
080: return _next != null || _in.hasNext();
081: } catch (XMLStreamException e) {
082: return false;
083: }
084: }
085:
086: public XMLEvent nextEvent() throws XMLStreamException {
087: if (_next != null) {
088: _current = _next;
089: _next = null;
090: } else {
091: _in.next();
092: _current = _allocator.allocate(_in);
093: }
094:
095: return _current;
096: }
097:
098: public XMLEvent nextTag() throws XMLStreamException {
099: if (_next != null) {
100: _current = _next;
101: _next = null;
102: } else {
103: _in.nextTag();
104: _current = _allocator.allocate(_in);
105: }
106:
107: return _current;
108: }
109:
110: public XMLEvent peek() throws XMLStreamException {
111: if (_next == null) {
112: _in.next();
113: _next = _allocator.allocate(_in);
114: }
115:
116: return _next;
117: }
118:
119: public void remove() {
120: throw new UnsupportedOperationException();
121: }
122:
123: public XMLEvent next() {
124: try {
125: return nextEvent();
126: } catch (XMLStreamException e) {
127: return null;
128: }
129: }
130: }
|