01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one
03: * or more contributor license agreements. See the NOTICE file
04: * distributed with this work for additional information
05: * regarding copyright ownership. The ASF licenses this file
06: * to you under the Apache License, Version 2.0 (the
07: * "License"); you may not use this file except in compliance
08: * with the License. You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing,
13: * software distributed under the License is distributed on an
14: * * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15: * KIND, either express or implied. See the License for the
16: * specific language governing permissions and limitations
17: * under the License.
18: */
19:
20: package org.apache.synapse.util;
21:
22: import org.apache.axiom.om.OMDataSource;
23: import org.apache.axiom.om.OMOutputFormat;
24: import org.apache.axiom.om.OMFactory;
25: import org.apache.axiom.om.OMAbstractFactory;
26: import org.apache.axiom.om.impl.llom.OMSourcedElementImpl;
27: import org.apache.axiom.om.impl.serialize.StreamingOMSerializer;
28: import org.apache.synapse.SynapseException;
29: import org.apache.synapse.transport.base.BaseConstants;
30:
31: import javax.activation.DataSource;
32: import javax.activation.FileDataSource;
33: import javax.xml.stream.XMLStreamException;
34: import javax.xml.stream.XMLStreamReader;
35: import javax.xml.stream.XMLStreamWriter;
36: import java.io.*;
37:
38: public class TextFileDataSource implements OMDataSource {
39:
40: private static final byte[] empty = "<text xmlns=\"http://ws.apache.org/commons/ns/payload\"/>"
41: .getBytes();
42: private InputStream is = null;
43:
44: public TextFileDataSource(DataSource ds) {
45: try {
46: this .is = ds.getInputStream();
47: } catch (IOException e) {
48: throw new SynapseException(
49: "Unable to get an InputStream for DataSource : "
50: + ds.getName(), e);
51: }
52: }
53:
54: public TextFileDataSource(TemporaryData temporaryData) {
55: try {
56: this .is = temporaryData.getInputStream();
57: } catch (IOException e) {
58: throw new SynapseException(
59: "Unable to get an InputStream for temporary data",
60: e);
61: }
62: }
63:
64: public void serialize(OutputStream out, OMOutputFormat format)
65: throws XMLStreamException {
66: try {
67: byte[] buf = new byte[4096];
68: int len;
69: while ((len = is.read(buf)) > 0) {
70: out.write(buf, 0, len);
71: }
72: } catch (IOException e) {
73: throw new SynapseException(
74: "Error serializing TextFileDataSource to an OutputStream",
75: e);
76: }
77: }
78:
79: public void serialize(Writer writer, OMOutputFormat format)
80: throws XMLStreamException {
81: try {
82: writer.write(new String(empty));
83: } catch (IOException e) {
84: throw new XMLStreamException(e);
85: }
86: }
87:
88: public void serialize(XMLStreamWriter xmlWriter)
89: throws XMLStreamException {
90: StreamingOMSerializer serializer = new StreamingOMSerializer();
91: serializer.serialize(getReader(), xmlWriter);
92: }
93:
94: public XMLStreamReader getReader() throws XMLStreamException {
95: return new WrappedTextNodeStreamReader(
96: BaseConstants.DEFAULT_TEXT_WRAPPER,
97: new InputStreamReader(is));
98: }
99: }
|