001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */package org.apache.cxf.staxutils;
019:
020: import javax.xml.stream.XMLStreamException;
021: import javax.xml.stream.XMLStreamReader;
022:
023: /**
024: * Wraps a XMLStreamReader and provides START_DOCUMENT and END_DOCUMENT events.
025: *
026: * @author <a href="mailto:dan@envoisolutions.com">Dan Diephouse</a>
027: */
028: public class FragmentStreamReader extends DepthXMLStreamReader {
029: private boolean startDoc;
030: private boolean startElement;
031: private boolean middle = true;
032: private boolean endDoc;
033:
034: private int depth;
035: private int current = -1;
036: private boolean filter = true;
037: private boolean advanceAtEnd = true;
038:
039: public FragmentStreamReader(XMLStreamReader reader) {
040: super (reader);
041: }
042:
043: public int getEventType() {
044: return current;
045: }
046:
047: public boolean hasNext() throws XMLStreamException {
048: if (!startDoc) {
049: return true;
050: }
051:
052: if (endDoc) {
053: return false;
054: }
055:
056: return reader.hasNext();
057: }
058:
059: public int next() throws XMLStreamException {
060: if (!startDoc) {
061: startDoc = true;
062: current = START_DOCUMENT;
063: } else if (!startElement) {
064: depth = getDepth();
065:
066: current = reader.getEventType();
067:
068: if (filter) {
069: while (current != START_ELEMENT && depth >= getDepth()
070: && super .hasNext()) {
071: current = super .next();
072: }
073:
074: filter = false;
075: }
076:
077: startElement = true;
078: current = START_ELEMENT;
079: } else if (middle) {
080: current = super .next();
081:
082: if (current == END_ELEMENT && getDepth() < depth) {
083: middle = false;
084: }
085: } else if (!endDoc) {
086: // Move past the END_ELEMENT token.
087: if (advanceAtEnd) {
088: super .next();
089: }
090:
091: endDoc = true;
092: current = END_DOCUMENT;
093: } else {
094: throw new XMLStreamException(
095: "Already at the end of the document.");
096: }
097:
098: return current;
099: }
100:
101: public boolean isAdvanceAtEnd() {
102: return advanceAtEnd;
103: }
104:
105: /**
106: * Set whether or not the FragmentStreamReader should move past the END_ELEMENT
107: * when it is done parsing.
108: * @param advanceAtEnd
109: */
110: public void setAdvanceAtEnd(boolean a) {
111: this.advanceAtEnd = a;
112: }
113:
114: }
|