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.transform;
018:
019: import javax.xml.stream.XMLEventReader;
020: import javax.xml.stream.XMLStreamReader;
021: import javax.xml.transform.sax.SAXSource;
022:
023: import org.springframework.xml.stream.StaxEventXmlReader;
024: import org.springframework.xml.stream.StaxStreamXmlReader;
025: import org.xml.sax.InputSource;
026: import org.xml.sax.XMLReader;
027:
028: /**
029: * Implementation of the <code>Source</code> tagging interface for StAX readers. Can be constructed with a
030: * <code>XMLEventReader</code> or a <code>XMLStreamReader</code>.
031: * <p/>
032: * This class is necessary because there is no implementation of <code>Source</code> for StaxReaders in JAXP 1.3. There
033: * will be a <code>StaxSource</code> in JAXP 1.4 (JDK 1.6), and by the time that version is available, this class will
034: * probably be deprecated.
035: * <p/>
036: * Even though <code>StaxSource</code> extends from <code>SAXSource</code>, calling the methods of
037: * <code>SAXSource</code> is <strong>not supported</strong>. In general, the only supported operation on this class is
038: * to use the <code>XMLReader</code> obtained via {@link #getXMLReader()} to parse the input source obtained via {@link
039: * #getInputSource()}. Calling {@link #setXMLReader(org.xml.sax.XMLReader)} or {@link
040: * #setInputSource(org.xml.sax.InputSource)} will result in <code>UnsupportedOperationException</code>s.
041: *
042: * @author Arjen Poutsma
043: * @see XMLEventReader
044: * @see XMLStreamReader
045: * @see javax.xml.transform.Transformer
046: * @since 1.0.0
047: */
048: public class StaxSource extends SAXSource {
049:
050: private XMLEventReader eventReader;
051:
052: private XMLStreamReader streamReader;
053:
054: /**
055: * Constructs a new instance of the <code>StaxSource</code> with the specified <code>XMLStreamReader</code>. The
056: * supplied stream reader must be in <code>XMLStreamConstants.START_DOCUMENT</code> or
057: * <code>XMLStreamConstants.START_ELEMENT</code> state.
058: *
059: * @param streamReader the <code>XMLStreamReader</code> to read from
060: * @throws IllegalStateException if the reader is not at the start of a document or element
061: */
062: public StaxSource(XMLStreamReader streamReader) {
063: super (new StaxStreamXmlReader(streamReader), new InputSource());
064: this .streamReader = streamReader;
065: }
066:
067: /**
068: * Constructs a new instance of the <code>StaxSource</code> with the specified <code>XMLEventReader</code>. The
069: * supplied event reader must be in <code>XMLStreamConstants.START_DOCUMENT</code> or
070: * <code>XMLStreamConstants.START_ELEMENT</code> state.
071: *
072: * @param eventReader the <code>XMLEventReader</code> to read from
073: * @throws IllegalStateException if the reader is not at the start of a document or element
074: */
075: public StaxSource(XMLEventReader eventReader) {
076: super (new StaxEventXmlReader(eventReader), new InputSource());
077: this .eventReader = eventReader;
078: }
079:
080: /**
081: * Returns the <code>XMLEventReader</code> used by this <code>StaxSource</code>. If this <code>StaxSource</code> was
082: * created with an <code>XMLStreamReader</code>, the result will be <code>null</code>.
083: *
084: * @return the StAX event reader used by this source
085: * @see StaxSource#StaxSource(javax.xml.stream.XMLEventReader)
086: */
087: public XMLEventReader getXMLEventReader() {
088: return eventReader;
089: }
090:
091: /**
092: * Returns the <code>XMLStreamReader</code> used by this <code>StaxSource</code>. If this <code>StaxSource</code>
093: * was created with an <code>XMLEventReader</code>, the result will be <code>null</code>.
094: *
095: * @return the StAX event reader used by this source
096: * @see StaxSource#StaxSource(javax.xml.stream.XMLEventReader)
097: */
098: public XMLStreamReader getXMLStreamReader() {
099: return streamReader;
100: }
101:
102: /**
103: * Throws a <code>UnsupportedOperationException</code>.
104: *
105: * @throws UnsupportedOperationException always
106: */
107: public void setInputSource(InputSource inputSource) {
108: throw new UnsupportedOperationException(
109: "setInputSource is not supported");
110: }
111:
112: /**
113: * Throws a <code>UnsupportedOperationException</code>.
114: *
115: * @throws UnsupportedOperationException always
116: */
117: public void setXMLReader(XMLReader reader) {
118: throw new UnsupportedOperationException(
119: "setXMLReader is not supported");
120: }
121: }
|