001: /*
002: * Copyright 2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.xml.stream;
018:
019: import javax.xml.namespace.QName;
020: import javax.xml.stream.XMLStreamConstants;
021: import javax.xml.stream.XMLStreamException;
022: import javax.xml.stream.XMLStreamReader;
023:
024: import org.springframework.util.Assert;
025:
026: /**
027: * Abstract base class for <code>XMLStreamReader</code>s.
028: *
029: * @author Arjen Poutsma
030: * @since 1.0.0
031: */
032: public abstract class AbstractXmlStreamReader implements
033: XMLStreamReader {
034:
035: public String getElementText() throws XMLStreamException {
036: if (getEventType() != XMLStreamConstants.START_ELEMENT) {
037: throw new XMLStreamException(
038: "parser must be on START_ELEMENT to read next text",
039: getLocation());
040: }
041: int eventType = next();
042: StringBuffer buffer = new StringBuffer();
043: while (eventType != XMLStreamConstants.END_ELEMENT) {
044: if (eventType == XMLStreamConstants.CHARACTERS
045: || eventType == XMLStreamConstants.CDATA
046: || eventType == XMLStreamConstants.SPACE
047: || eventType == XMLStreamConstants.ENTITY_REFERENCE) {
048: buffer.append(getText());
049: } else if (eventType == XMLStreamConstants.PROCESSING_INSTRUCTION
050: || eventType == XMLStreamConstants.COMMENT) {
051: // skipping
052: } else if (eventType == XMLStreamConstants.END_DOCUMENT) {
053: throw new XMLStreamException(
054: "unexpected end of document when reading element text content",
055: getLocation());
056: } else if (eventType == XMLStreamConstants.START_ELEMENT) {
057: throw new XMLStreamException(
058: "element text content may not contain START_ELEMENT",
059: getLocation());
060: } else {
061: throw new XMLStreamException("Unexpected event type "
062: + eventType, getLocation());
063: }
064: eventType = next();
065: }
066: return buffer.toString();
067: }
068:
069: public String getAttributeLocalName(int index) {
070: return getAttributeName(index).getLocalPart();
071: }
072:
073: public String getAttributeNamespace(int index) {
074: return getAttributeName(index).getNamespaceURI();
075: }
076:
077: public String getAttributePrefix(int index) {
078: return getAttributeName(index).getPrefix();
079: }
080:
081: public String getNamespaceURI() {
082: int eventType = getEventType();
083: if (eventType == XMLStreamConstants.START_ELEMENT
084: || eventType == XMLStreamConstants.END_ELEMENT) {
085: return getName().getNamespaceURI();
086: } else {
087: throw new IllegalStateException(
088: "parser must be on START_ELEMENT or END_ELEMENT state");
089: }
090: }
091:
092: public String getNamespaceURI(String prefix) {
093: Assert.notNull(prefix, "No prefix given");
094: return getNamespaceContext().getNamespaceURI(prefix);
095: }
096:
097: public boolean hasText() {
098: int eventType = getEventType();
099: return eventType == XMLStreamConstants.SPACE
100: || eventType == XMLStreamConstants.CHARACTERS
101: || eventType == XMLStreamConstants.COMMENT
102: || eventType == XMLStreamConstants.CDATA
103: || eventType == XMLStreamConstants.ENTITY_REFERENCE;
104: }
105:
106: public String getPrefix() {
107: int eventType = getEventType();
108: if (eventType == XMLStreamConstants.START_ELEMENT
109: || eventType == XMLStreamConstants.END_ELEMENT) {
110: return getName().getPrefix();
111: } else {
112: throw new IllegalStateException(
113: "parser must be on START_ELEMENT or END_ELEMENT state");
114: }
115: }
116:
117: public boolean hasName() {
118: int eventType = getEventType();
119: return eventType == XMLStreamConstants.START_ELEMENT
120: || eventType == XMLStreamConstants.END_ELEMENT;
121: }
122:
123: public boolean isWhiteSpace() {
124: return getEventType() == XMLStreamConstants.SPACE;
125: }
126:
127: public boolean isStartElement() {
128: return getEventType() == XMLStreamConstants.START_ELEMENT;
129: }
130:
131: public boolean isEndElement() {
132: return getEventType() == XMLStreamConstants.END_ELEMENT;
133: }
134:
135: public boolean isCharacters() {
136: return getEventType() == XMLStreamConstants.CHARACTERS;
137: }
138:
139: public int nextTag() throws XMLStreamException {
140: int eventType = next();
141: while (eventType == XMLStreamConstants.CHARACTERS
142: && isWhiteSpace()
143: || eventType == XMLStreamConstants.CDATA
144: && isWhiteSpace()
145: || eventType == XMLStreamConstants.SPACE
146: || eventType == XMLStreamConstants.PROCESSING_INSTRUCTION
147: || eventType == XMLStreamConstants.COMMENT) {
148: eventType = next();
149: }
150: if (eventType != XMLStreamConstants.START_ELEMENT
151: && eventType != XMLStreamConstants.END_ELEMENT) {
152: throw new XMLStreamException("expected start or end tag",
153: getLocation());
154: }
155: return eventType;
156: }
157:
158: public void require(int expectedType, String namespaceURI,
159: String localName) throws XMLStreamException {
160: int eventType = getEventType();
161: if (eventType != expectedType) {
162: throw new XMLStreamException("Expected [" + expectedType
163: + "] but read [" + eventType + "]");
164: }
165: }
166:
167: public String getAttributeValue(String namespaceURI,
168: String localName) {
169: for (int i = 0; i < getAttributeCount(); i++) {
170: QName name = getAttributeName(i);
171: if (name.getNamespaceURI().equals(namespaceURI)
172: && name.getLocalPart().equals(localName)) {
173: return getAttributeValue(i);
174: }
175: }
176: return null;
177: }
178:
179: public boolean hasNext() throws XMLStreamException {
180: return getEventType() != END_DOCUMENT;
181: }
182:
183: public String getLocalName() {
184: return getName().getLocalPart();
185: }
186:
187: public char[] getTextCharacters() {
188: return getText().toCharArray();
189: }
190:
191: public int getTextCharacters(int sourceStart, char[] target,
192: int targetStart, int length) throws XMLStreamException {
193: char[] source = getTextCharacters();
194: length = Math.min(length, source.length);
195: System.arraycopy(source, sourceStart, target, targetStart,
196: length);
197: return length;
198: }
199:
200: public int getTextLength() {
201: return getText().length();
202: }
203: }
|