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.XMLEventFactory;
20 import javax.xml.stream.XMLEventWriter;
21 import javax.xml.stream.XMLStreamWriter;
22 import javax.xml.transform.sax.SAXResult;
23
24 import org.springframework.xml.stream.StaxEventContentHandler;
25 import org.springframework.xml.stream.StaxStreamContentHandler;
26 import org.xml.sax.ContentHandler;
27
28 /**
29 * Implementation of the <code>Result</code> tagging interface for StAX writers. Can be constructed with a
30 * <code>XMLEventConsumer</code> or a <code>XMLStreamWriter</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>StaxResult</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>StaxResult</code> extends from <code>SAXResult</code>, calling the methods of
37 * <code>SAXResult</code> is <strong>not supported</strong>. In general, the only supported operation on this class is
38 * to use the <code>ContentHandler</code> obtained via {@link #getHandler()} to parse an input source using an
39 * <code>XMLReader</code>. Calling {@link #setHandler(org.xml.sax.ContentHandler)} will result in
40 * <code>UnsupportedOperationException</code>s.
41 *
42 * @author Arjen Poutsma
43 * @see XMLEventWriter
44 * @see XMLStreamWriter
45 * @see javax.xml.transform.Transformer
46 * @since 1.0.0
47 */
48 public class StaxResult extends SAXResult {
49
50 private XMLEventWriter eventWriter;
51
52 private XMLStreamWriter streamWriter;
53
54 /**
55 * Constructs a new instance of the <code>StaxResult</code> with the specified <code>XMLStreamWriter</code>.
56 *
57 * @param streamWriter the <code>XMLStreamWriter</code> to write to
58 */
59 public StaxResult(XMLStreamWriter streamWriter) {
60 super.setHandler(new StaxStreamContentHandler(streamWriter));
61 this.streamWriter = streamWriter;
62 }
63
64 /**
65 * Constructs a new instance of the <code>StaxResult</code> with the specified <code>XMLEventWriter</code>.
66 *
67 * @param eventWriter the <code>XMLEventWriter</code> to write to
68 */
69 public StaxResult(XMLEventWriter eventWriter) {
70 super.setHandler(new StaxEventContentHandler(eventWriter));
71 this.eventWriter = eventWriter;
72 }
73
74 /**
75 * Constructs a new instance of the <code>StaxResult</code> with the specified <code>XMLEventWriter</code> and
76 * <code>XMLEventFactory</code>.
77 *
78 * @param eventWriter the <code>XMLEventWriter</code> to write to
79 * @param eventFactory the <code>XMLEventFactory</code> to use for creating events
80 */
81 public StaxResult(XMLEventWriter eventWriter, XMLEventFactory eventFactory) {
82 super.setHandler(new StaxEventContentHandler(eventWriter, eventFactory));
83 this.eventWriter = eventWriter;
84 }
85
86 /**
87 * Returns the <code>XMLEventWriter</code> used by this <code>StaxResult</code>. If this <code>StaxResult</code> was
88 * created with an <code>XMLStreamWriter</code>, the result will be <code>null</code>.
89 *
90 * @return the StAX event writer used by this result
91 * @see #StaxResult(javax.xml.stream.XMLEventWriter)
92 */
93 public XMLEventWriter getXMLEventWriter() {
94 return eventWriter;
95 }
96
97 /**
98 * Returns the <code>XMLStreamWriter</code> used by this <code>StaxResult</code>. If this <code>StaxResult</code>
99 * was created with an <code>XMLEventConsumer</code>, the result will be <code>null</code>.
100 *
101 * @return the StAX stream writer used by this result
102 * @see #StaxResult(javax.xml.stream.XMLStreamWriter)
103 */
104 public XMLStreamWriter getXMLStreamWriter() {
105 return streamWriter;
106 }
107
108 /**
109 * Throws a <code>UnsupportedOperationException</code>.
110 *
111 * @throws UnsupportedOperationException always
112 */
113 public void setHandler(ContentHandler handler) {
114 throw new UnsupportedOperationException("setHandler is not supported");
115 }
116 }