View Javadoc

1   /*
2    * Copyright 2005-2010 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.ws.soap.axiom;
18  
19  import java.io.ByteArrayInputStream;
20  import java.io.ByteArrayOutputStream;
21  import java.io.OutputStream;
22  import java.io.Writer;
23  import javax.xml.stream.XMLStreamException;
24  import javax.xml.stream.XMLStreamReader;
25  import javax.xml.stream.XMLStreamWriter;
26  
27  import org.springframework.util.Assert;
28  import org.springframework.util.StringUtils;
29  import org.springframework.ws.stream.StreamingPayload;
30  
31  import org.apache.axiom.om.OMDataSource;
32  import org.apache.axiom.om.OMOutputFormat;
33  import org.apache.axiom.om.util.StAXUtils;
34  
35  /**
36   * Implementation of {@link OMDataSource} that wraps a {@link StreamingPayload}.
37   *
38   * @author Arjen Poutsma
39   * @since 2.0
40   */
41  class StreamingOMDataSource implements OMDataSource {
42  
43      private final StreamingPayload payload;
44  
45      StreamingOMDataSource(StreamingPayload payload) {
46          Assert.notNull(payload, "'payload' must not be null");
47          this.payload = payload;
48      }
49  
50      public void serialize(OutputStream output, OMOutputFormat format) throws XMLStreamException {
51          XMLStreamWriter streamWriter;
52          if (format != null && StringUtils.hasLength(format.getCharSetEncoding())) {
53              streamWriter = StAXUtils.createXMLStreamWriter(output, format.getCharSetEncoding());
54          }
55          else {
56              streamWriter = StAXUtils.createXMLStreamWriter(output);
57          }
58          serialize(streamWriter);
59      }
60  
61      public void serialize(Writer writer, OMOutputFormat format) throws XMLStreamException {
62          XMLStreamWriter streamWriter = StAXUtils.createXMLStreamWriter(writer);
63          serialize(streamWriter);
64      }
65  
66      public void serialize(XMLStreamWriter xmlWriter) throws XMLStreamException {
67          payload.writeTo(xmlWriter);
68          xmlWriter.flush();
69      }
70  
71      public XMLStreamReader getReader() throws XMLStreamException {
72          ByteArrayOutputStream bos = new ByteArrayOutputStream();
73          serialize(bos, null);
74  
75          ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
76          return StAXUtils.createXMLStreamReader(bis);
77      }
78  
79  }