|
| 1 | +/* |
| 2 | + * Copyright 2002-2013 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.util.xml; |
| 18 | + |
| 19 | +import javax.xml.stream.XMLEventReader; |
| 20 | +import javax.xml.stream.XMLStreamReader; |
| 21 | +import javax.xml.transform.sax.SAXSource; |
| 22 | + |
| 23 | +import org.xml.sax.InputSource; |
| 24 | +import org.xml.sax.XMLReader; |
| 25 | + |
| 26 | +/** |
| 27 | + * Implementation of the {@code Source} tagging interface for StAX readers. Can be constructed with |
| 28 | + * an {@code XMLEventReader} or an {@code XMLStreamReader}. |
| 29 | + * |
| 30 | + * <p>This class is necessary because there is no implementation of {@code Source} for StAX Readers |
| 31 | + * in JAXP 1.3. There is a {@code StAXSource} in JAXP 1.4 (JDK 1.6), but this class is kept around |
| 32 | + * for backwards compatibility reasons. |
| 33 | + * |
| 34 | + * <p>Even though {@code StaxSource} extends from {@code SAXSource}, calling the methods of |
| 35 | + * {@code SAXSource} is <strong>not supported</strong>. In general, the only supported operation |
| 36 | + * on this class is to use the {@code XMLReader} obtained via {@link #getXMLReader()} to parse the |
| 37 | + * input source obtained via {@link #getInputSource()}. Calling {@link #setXMLReader(XMLReader)} |
| 38 | + * or {@link #setInputSource(InputSource)} will result in {@code UnsupportedOperationException}s. |
| 39 | + * |
| 40 | + * @author Arjen Poutsma |
| 41 | + * @since 3.0 |
| 42 | + * @see XMLEventReader |
| 43 | + * @see XMLStreamReader |
| 44 | + * @see javax.xml.transform.Transformer |
| 45 | + */ |
| 46 | +class StaxSource extends SAXSource { |
| 47 | + |
| 48 | + private XMLEventReader eventReader; |
| 49 | + |
| 50 | + private XMLStreamReader streamReader; |
| 51 | + |
| 52 | + |
| 53 | + /** |
| 54 | + * Construct a new instance of the {@code StaxSource} with the specified {@code XMLStreamReader}. |
| 55 | + * The supplied stream reader must be in {@code XMLStreamConstants.START_DOCUMENT} or |
| 56 | + * {@code XMLStreamConstants.START_ELEMENT} state. |
| 57 | + * @param streamReader the {@code XMLStreamReader} to read from |
| 58 | + * @throws IllegalStateException if the reader is not at the start of a document or element |
| 59 | + */ |
| 60 | + StaxSource(XMLStreamReader streamReader) { |
| 61 | + super(new StaxStreamXMLReader(streamReader), new InputSource()); |
| 62 | + this.streamReader = streamReader; |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Construct a new instance of the {@code StaxSource} with the specified {@code XMLEventReader}. |
| 67 | + * The supplied event reader must be in {@code XMLStreamConstants.START_DOCUMENT} or |
| 68 | + * {@code XMLStreamConstants.START_ELEMENT} state. |
| 69 | + * @param eventReader the {@code XMLEventReader} to read from |
| 70 | + * @throws IllegalStateException if the reader is not at the start of a document or element |
| 71 | + */ |
| 72 | + StaxSource(XMLEventReader eventReader) { |
| 73 | + super(new StaxEventXMLReader(eventReader), new InputSource()); |
| 74 | + this.eventReader = eventReader; |
| 75 | + } |
| 76 | + |
| 77 | + |
| 78 | + /** |
| 79 | + * Return the {@code XMLEventReader} used by this {@code StaxSource}. If this {@code StaxSource} |
| 80 | + * was created with an {@code XMLStreamReader}, the result will be {@code null}. |
| 81 | + * @return the StAX event reader used by this source |
| 82 | + * @see StaxSource#StaxSource(javax.xml.stream.XMLEventReader) |
| 83 | + */ |
| 84 | + XMLEventReader getXMLEventReader() { |
| 85 | + return this.eventReader; |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Return the {@code XMLStreamReader} used by this {@code StaxSource}. If this {@code StaxSource} |
| 90 | + * was created with an {@code XMLEventReader}, the result will be {@code null}. |
| 91 | + * @return the StAX event reader used by this source |
| 92 | + * @see StaxSource#StaxSource(javax.xml.stream.XMLEventReader) |
| 93 | + */ |
| 94 | + XMLStreamReader getXMLStreamReader() { |
| 95 | + return this.streamReader; |
| 96 | + } |
| 97 | + |
| 98 | + |
| 99 | + /** |
| 100 | + * Throws an {@code UnsupportedOperationException}. |
| 101 | + * @throws UnsupportedOperationException always |
| 102 | + */ |
| 103 | + @Override |
| 104 | + public void setInputSource(InputSource inputSource) { |
| 105 | + throw new UnsupportedOperationException("setInputSource is not supported"); |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Throws an {@code UnsupportedOperationException}. |
| 110 | + * @throws UnsupportedOperationException always |
| 111 | + */ |
| 112 | + @Override |
| 113 | + public void setXMLReader(XMLReader reader) { |
| 114 | + throw new UnsupportedOperationException("setXMLReader is not supported"); |
| 115 | + } |
| 116 | + |
| 117 | +} |
0 commit comments