1 /* 2 * Copyright 2006 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.xml.transform; 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 import org.springframework.xml.stream.StaxEventXmlReader; 27 import org.springframework.xml.stream.StaxStreamXmlReader; 28 29 /** 30 * Implementation of the <code>Source</code> tagging interface for StAX readers. Can be constructed with a 31 * <code>XMLEventReader</code> or a <code>XMLStreamReader</code>. 32 * <p/> 33 * This class is necessary because there is no implementation of <code>Source</code> for StAX Readers in JAXP 1.3. There 34 * is a <code>StAXSource</code> in JAXP 1.4 (JDK 1.6), but this class is kept around for back-ward compatibility 35 * reasons. 36 * <p/> 37 * Even though <code>StaxSource</code> extends from <code>SAXSource</code>, calling the methods of 38 * <code>SAXSource</code> is <strong>not supported</strong>. In general, the only supported operation on this class is 39 * to use the <code>XMLReader</code> obtained via {@link #getXMLReader()} to parse the input source obtained via {@link 40 * #getInputSource()}. Calling {@link #setXMLReader(org.xml.sax.XMLReader)} or {@link 41 * #setInputSource(org.xml.sax.InputSource)} will result in <code>UnsupportedOperationException</code>s. 42 * 43 * @author Arjen Poutsma 44 * @see XMLEventReader 45 * @see XMLStreamReader 46 * @see javax.xml.transform.Transformer 47 * @since 1.0.0 48 */ 49 public class StaxSource extends SAXSource { 50 51 private XMLEventReader eventReader; 52 53 private XMLStreamReader streamReader; 54 55 /** 56 * Constructs a new instance of the <code>StaxSource</code> with the specified <code>XMLStreamReader</code>. The 57 * supplied stream reader must be in <code>XMLStreamConstants.START_DOCUMENT</code> or 58 * <code>XMLStreamConstants.START_ELEMENT</code> state. 59 * 60 * @param streamReader the <code>XMLStreamReader</code> to read from 61 * @throws IllegalStateException if the reader is not at the start of a document or element 62 */ 63 public StaxSource(XMLStreamReader streamReader) { 64 super(new StaxStreamXmlReader(streamReader), new InputSource()); 65 this.streamReader = streamReader; 66 } 67 68 /** 69 * Constructs a new instance of the <code>StaxSource</code> with the specified <code>XMLEventReader</code>. The 70 * supplied event reader must be in <code>XMLStreamConstants.START_DOCUMENT</code> or 71 * <code>XMLStreamConstants.START_ELEMENT</code> state. 72 * 73 * @param eventReader the <code>XMLEventReader</code> to read from 74 * @throws IllegalStateException if the reader is not at the start of a document or element 75 */ 76 public StaxSource(XMLEventReader eventReader) { 77 super(new StaxEventXmlReader(eventReader), new InputSource()); 78 this.eventReader = eventReader; 79 } 80 81 /** 82 * Returns the <code>XMLEventReader</code> used by this <code>StaxSource</code>. If this <code>StaxSource</code> was 83 * created with an <code>XMLStreamReader</code>, the result will be <code>null</code>. 84 * 85 * @return the StAX event reader used by this source 86 * @see StaxSource#StaxSource(javax.xml.stream.XMLEventReader) 87 */ 88 public XMLEventReader getXMLEventReader() { 89 return eventReader; 90 } 91 92 /** 93 * Returns the <code>XMLStreamReader</code> used by this <code>StaxSource</code>. If this <code>StaxSource</code> 94 * was created with an <code>XMLEventReader</code>, the result will be <code>null</code>. 95 * 96 * @return the StAX event reader used by this source 97 * @see StaxSource#StaxSource(javax.xml.stream.XMLEventReader) 98 */ 99 public XMLStreamReader getXMLStreamReader() { 100 return streamReader; 101 } 102 103 /** 104 * Throws a <code>UnsupportedOperationException</code>. 105 * 106 * @throws UnsupportedOperationException always 107 */ 108 public void setInputSource(InputSource inputSource) { 109 throw new UnsupportedOperationException("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("setXMLReader is not supported"); 119 } 120 }