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.XMLEventFactory;
020: import javax.xml.stream.XMLEventWriter;
021: import javax.xml.stream.XMLStreamWriter;
022: import javax.xml.transform.sax.SAXResult;
023:
024: import org.springframework.xml.stream.StaxEventContentHandler;
025: import org.springframework.xml.stream.StaxStreamContentHandler;
026: import org.xml.sax.ContentHandler;
027:
028: /**
029: * Implementation of the <code>Result</code> tagging interface for StAX writers. Can be constructed with a
030: * <code>XMLEventConsumer</code> or a <code>XMLStreamWriter</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>StaxResult</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>StaxResult</code> extends from <code>SAXResult</code>, calling the methods of
037: * <code>SAXResult</code> is <strong>not supported</strong>. In general, the only supported operation on this class is
038: * to use the <code>ContentHandler</code> obtained via {@link #getHandler()} to parse an input source using an
039: * <code>XMLReader</code>. Calling {@link #setHandler(org.xml.sax.ContentHandler)} will result in
040: * <code>UnsupportedOperationException</code>s.
041: *
042: * @author Arjen Poutsma
043: * @see XMLEventWriter
044: * @see XMLStreamWriter
045: * @see javax.xml.transform.Transformer
046: * @since 1.0.0
047: */
048: public class StaxResult extends SAXResult {
049:
050: private XMLEventWriter eventWriter;
051:
052: private XMLStreamWriter streamWriter;
053:
054: /**
055: * Constructs a new instance of the <code>StaxResult</code> with the specified <code>XMLStreamWriter</code>.
056: *
057: * @param streamWriter the <code>XMLStreamWriter</code> to write to
058: */
059: public StaxResult(XMLStreamWriter streamWriter) {
060: super .setHandler(new StaxStreamContentHandler(streamWriter));
061: this .streamWriter = streamWriter;
062: }
063:
064: /**
065: * Constructs a new instance of the <code>StaxResult</code> with the specified <code>XMLEventWriter</code>.
066: *
067: * @param eventWriter the <code>XMLEventWriter</code> to write to
068: */
069: public StaxResult(XMLEventWriter eventWriter) {
070: super .setHandler(new StaxEventContentHandler(eventWriter));
071: this .eventWriter = eventWriter;
072: }
073:
074: /**
075: * Constructs a new instance of the <code>StaxResult</code> with the specified <code>XMLEventWriter</code> and
076: * <code>XMLEventFactory</code>.
077: *
078: * @param eventWriter the <code>XMLEventWriter</code> to write to
079: * @param eventFactory the <code>XMLEventFactory</code> to use for creating events
080: */
081: public StaxResult(XMLEventWriter eventWriter,
082: XMLEventFactory eventFactory) {
083: super .setHandler(new StaxEventContentHandler(eventWriter,
084: eventFactory));
085: this .eventWriter = eventWriter;
086: }
087:
088: /**
089: * Returns the <code>XMLEventWriter</code> used by this <code>StaxResult</code>. If this <code>StaxResult</code> was
090: * created with an <code>XMLStreamWriter</code>, the result will be <code>null</code>.
091: *
092: * @return the StAX event writer used by this result
093: * @see #StaxResult(javax.xml.stream.XMLEventWriter)
094: */
095: public XMLEventWriter getXMLEventWriter() {
096: return eventWriter;
097: }
098:
099: /**
100: * Returns the <code>XMLStreamWriter</code> used by this <code>StaxResult</code>. If this <code>StaxResult</code>
101: * was created with an <code>XMLEventConsumer</code>, the result will be <code>null</code>.
102: *
103: * @return the StAX stream writer used by this result
104: * @see #StaxResult(javax.xml.stream.XMLStreamWriter)
105: */
106: public XMLStreamWriter getXMLStreamWriter() {
107: return streamWriter;
108: }
109:
110: /**
111: * Throws a <code>UnsupportedOperationException</code>.
112: *
113: * @throws UnsupportedOperationException always
114: */
115: public void setHandler(ContentHandler handler) {
116: throw new UnsupportedOperationException(
117: "setHandler is not supported");
118: }
119: }
|