Skip to content

Commit 1ee816b

Browse files
committed
Re-introduced custom StaxSource and StaxResult for Spring Web Services
1 parent e6952f4 commit 1ee816b

File tree

6 files changed

+616
-32
lines changed

6 files changed

+616
-32
lines changed
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/*
2+
* Copyright 2002-2013 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.util.xml;
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+
/**
27+
* Implementation of the {@code Result} tagging interface for StAX writers. Can be constructed with
28+
* an {@code XMLEventConsumer} or an {@code XMLStreamWriter}.
29+
*
30+
* <p>This class is necessary because there is no implementation of {@code Source} for StaxReaders
31+
* in JAXP 1.3. There is a {@code StAXResult} in JAXP 1.4 (JDK 1.6), but this class is kept around
32+
* for backwards compatibility reasons.
33+
*
34+
* <p>Even though {@code StaxResult} extends from {@code SAXResult}, calling the methods of
35+
* {@code SAXResult} is <strong>not supported</strong>. In general, the only supported operation
36+
* on this class is to use the {@code ContentHandler} obtained via {@link #getHandler()} to parse an
37+
* input source using an {@code XMLReader}. Calling {@link #setHandler(org.xml.sax.ContentHandler)}
38+
* will result in {@code UnsupportedOperationException}s.
39+
*
40+
* @author Arjen Poutsma
41+
* @since 3.0
42+
* @see XMLEventWriter
43+
* @see XMLStreamWriter
44+
* @see javax.xml.transform.Transformer
45+
*/
46+
class StaxResult extends SAXResult {
47+
48+
private XMLEventWriter eventWriter;
49+
50+
private XMLStreamWriter streamWriter;
51+
52+
53+
/**
54+
* Construct a new instance of the {@code StaxResult} with the specified {@code XMLStreamWriter}.
55+
* @param streamWriter the {@code XMLStreamWriter} to write to
56+
*/
57+
StaxResult(XMLStreamWriter streamWriter) {
58+
super.setHandler(new StaxStreamContentHandler(streamWriter));
59+
this.streamWriter = streamWriter;
60+
}
61+
62+
/**
63+
* Construct a new instance of the {@code StaxResult} with the specified {@code XMLEventWriter}.
64+
* @param eventWriter the {@code XMLEventWriter} to write to
65+
*/
66+
StaxResult(XMLEventWriter eventWriter) {
67+
super.setHandler(new StaxEventContentHandler(eventWriter));
68+
this.eventWriter = eventWriter;
69+
}
70+
71+
/**
72+
* Construct a new instance of the {@code StaxResult} with the specified {@code XMLEventWriter}
73+
* and {@code XMLEventFactory}.
74+
* @param eventWriter the {@code XMLEventWriter} to write to
75+
* @param eventFactory the {@code XMLEventFactory} to use for creating events
76+
*/
77+
StaxResult(XMLEventWriter eventWriter, XMLEventFactory eventFactory) {
78+
super.setHandler(new StaxEventContentHandler(eventWriter, eventFactory));
79+
this.eventWriter = eventWriter;
80+
}
81+
82+
83+
/**
84+
* Return the {@code XMLEventWriter} used by this {@code StaxResult}. If this {@code StaxResult}
85+
* was created with an {@code XMLStreamWriter}, the result will be {@code null}.
86+
* @return the StAX event writer used by this result
87+
* @see #StaxResult(javax.xml.stream.XMLEventWriter)
88+
*/
89+
XMLEventWriter getXMLEventWriter() {
90+
return this.eventWriter;
91+
}
92+
93+
/**
94+
* Return the {@code XMLStreamWriter} used by this {@code StaxResult}. If this {@code StaxResult}
95+
* was created with an {@code XMLEventConsumer}, the result will be {@code null}.
96+
* @return the StAX stream writer used by this result
97+
* @see #StaxResult(javax.xml.stream.XMLStreamWriter)
98+
*/
99+
XMLStreamWriter getXMLStreamWriter() {
100+
return this.streamWriter;
101+
}
102+
103+
104+
/**
105+
* Throws an {@code UnsupportedOperationException}.
106+
* @throws UnsupportedOperationException always
107+
*/
108+
@Override
109+
public void setHandler(ContentHandler handler) {
110+
throw new UnsupportedOperationException("setHandler is not supported");
111+
}
112+
113+
}
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
/*
2+
* Copyright 2002-2013 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.util.xml;
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+
/**
27+
* Implementation of the {@code Source} tagging interface for StAX readers. Can be constructed with
28+
* an {@code XMLEventReader} or an {@code XMLStreamReader}.
29+
*
30+
* <p>This class is necessary because there is no implementation of {@code Source} for StAX Readers
31+
* in JAXP 1.3. There is a {@code StAXSource} in JAXP 1.4 (JDK 1.6), but this class is kept around
32+
* for backwards compatibility reasons.
33+
*
34+
* <p>Even though {@code StaxSource} extends from {@code SAXSource}, calling the methods of
35+
* {@code SAXSource} is <strong>not supported</strong>. In general, the only supported operation
36+
* on this class is to use the {@code XMLReader} obtained via {@link #getXMLReader()} to parse the
37+
* input source obtained via {@link #getInputSource()}. Calling {@link #setXMLReader(XMLReader)}
38+
* or {@link #setInputSource(InputSource)} will result in {@code UnsupportedOperationException}s.
39+
*
40+
* @author Arjen Poutsma
41+
* @since 3.0
42+
* @see XMLEventReader
43+
* @see XMLStreamReader
44+
* @see javax.xml.transform.Transformer
45+
*/
46+
class StaxSource extends SAXSource {
47+
48+
private XMLEventReader eventReader;
49+
50+
private XMLStreamReader streamReader;
51+
52+
53+
/**
54+
* Construct a new instance of the {@code StaxSource} with the specified {@code XMLStreamReader}.
55+
* The supplied stream reader must be in {@code XMLStreamConstants.START_DOCUMENT} or
56+
* {@code XMLStreamConstants.START_ELEMENT} state.
57+
* @param streamReader the {@code XMLStreamReader} to read from
58+
* @throws IllegalStateException if the reader is not at the start of a document or element
59+
*/
60+
StaxSource(XMLStreamReader streamReader) {
61+
super(new StaxStreamXMLReader(streamReader), new InputSource());
62+
this.streamReader = streamReader;
63+
}
64+
65+
/**
66+
* Construct a new instance of the {@code StaxSource} with the specified {@code XMLEventReader}.
67+
* The supplied event reader must be in {@code XMLStreamConstants.START_DOCUMENT} or
68+
* {@code XMLStreamConstants.START_ELEMENT} state.
69+
* @param eventReader the {@code XMLEventReader} to read from
70+
* @throws IllegalStateException if the reader is not at the start of a document or element
71+
*/
72+
StaxSource(XMLEventReader eventReader) {
73+
super(new StaxEventXMLReader(eventReader), new InputSource());
74+
this.eventReader = eventReader;
75+
}
76+
77+
78+
/**
79+
* Return the {@code XMLEventReader} used by this {@code StaxSource}. If this {@code StaxSource}
80+
* was created with an {@code XMLStreamReader}, the result will be {@code null}.
81+
* @return the StAX event reader used by this source
82+
* @see StaxSource#StaxSource(javax.xml.stream.XMLEventReader)
83+
*/
84+
XMLEventReader getXMLEventReader() {
85+
return this.eventReader;
86+
}
87+
88+
/**
89+
* Return the {@code XMLStreamReader} used by this {@code StaxSource}. If this {@code StaxSource}
90+
* was created with an {@code XMLEventReader}, the result will be {@code null}.
91+
* @return the StAX event reader used by this source
92+
* @see StaxSource#StaxSource(javax.xml.stream.XMLEventReader)
93+
*/
94+
XMLStreamReader getXMLStreamReader() {
95+
return this.streamReader;
96+
}
97+
98+
99+
/**
100+
* Throws an {@code UnsupportedOperationException}.
101+
* @throws UnsupportedOperationException always
102+
*/
103+
@Override
104+
public void setInputSource(InputSource inputSource) {
105+
throw new UnsupportedOperationException("setInputSource is not supported");
106+
}
107+
108+
/**
109+
* Throws an {@code UnsupportedOperationException}.
110+
* @throws UnsupportedOperationException always
111+
*/
112+
@Override
113+
public void setXMLReader(XMLReader reader) {
114+
throw new UnsupportedOperationException("setXMLReader is not supported");
115+
}
116+
117+
}

0 commit comments

Comments
 (0)