View Javadoc

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