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