summaryrefslogtreecommitdiffstats
path: root/sca-java-2.x/tags/2.0.1-RC1/modules/databinding-jaxb-axiom/src/main/java/org/apache/tuscany/sca/databinding/jaxb/axiom/ext/XMLStreamWriterWithOS.java
blob: 44e34c364775dab302075ce3f1a4c67532b2d59c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package org.apache.tuscany.sca.databinding.jaxb.axiom.ext;

import java.io.OutputStream;

import javax.xml.namespace.NamespaceContext;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;

import org.apache.axiom.om.util.StAXUtils;

/**
 * XMLStreamReader that exposes direct access to the OutputStream.
 * Writing to the output stream is faster in some cases.
 */
public class XMLStreamWriterWithOS implements XMLStreamWriter {
    private XMLStreamWriter writer;
    private String charSetEncoding;
    private OutputStream os;

    public XMLStreamWriterWithOS(OutputStream os, String charSetEncoding) throws XMLStreamException {
        super();
        writer = null; // Writer is created when needed
        this.os = os;
        this.charSetEncoding = charSetEncoding;
    }

    /**
     * The writer is created lazily. 
     * If only the output stream is used, then the writer is never created.
     */
    private void createWriter() throws XMLStreamException {
        if (writer == null) {
            writer = StAXUtils.createXMLStreamWriter(os, charSetEncoding);
        }
    }

    public void close() throws XMLStreamException {
        if (writer != null) {
            writer.close();
        }
    }

    public void flush() throws XMLStreamException {
        if (writer != null) {
            writer.flush();
        }
    }

    public NamespaceContext getNamespaceContext() {
        try {
            createWriter();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return writer.getNamespaceContext();
    }

    public String getPrefix(String arg0) throws XMLStreamException {
        createWriter();
        return writer.getPrefix(arg0);
    }

    public Object getProperty(String arg0) throws IllegalArgumentException {
        try {
            createWriter();
        } catch (XMLStreamException e) {
            throw new IllegalArgumentException(e);
        }
        return writer.getProperty(arg0);
    }

    public void setDefaultNamespace(String arg0) throws XMLStreamException {
        createWriter();
        writer.setDefaultNamespace(arg0);
    }

    public void setNamespaceContext(NamespaceContext arg0) throws XMLStreamException {
        createWriter();
        writer.setNamespaceContext(arg0);
    }

    public void setPrefix(String arg0, String arg1) throws XMLStreamException {
        createWriter();
        writer.setPrefix(arg0, arg1);
    }

    public void writeAttribute(String arg0, String arg1, String arg2, String arg3) throws XMLStreamException {
        createWriter();
        writer.writeAttribute(arg0, arg1, arg2, arg3);
    }

    public void writeAttribute(String arg0, String arg1, String arg2) throws XMLStreamException {
        createWriter();
        writer.writeAttribute(arg0, arg1, arg2);
    }

    public void writeAttribute(String arg0, String arg1) throws XMLStreamException {
        createWriter();
        writer.writeAttribute(arg0, arg1);
    }

    public void writeCData(String arg0) throws XMLStreamException {
        createWriter();
        writer.writeCData(arg0);
    }

    public void writeCharacters(char[] arg0, int arg1, int arg2) throws XMLStreamException {
        createWriter();
        writer.writeCharacters(arg0, arg1, arg2);
    }

    public void writeCharacters(String arg0) throws XMLStreamException {
        createWriter();
        writer.writeCharacters(arg0);
    }

    public void writeComment(String arg0) throws XMLStreamException {
        createWriter();
        writer.writeComment(arg0);
    }

    public void writeDefaultNamespace(String arg0) throws XMLStreamException {
        createWriter();
        writer.writeDefaultNamespace(arg0);
    }

    public void writeDTD(String arg0) throws XMLStreamException {
        createWriter();
        writer.writeDTD(arg0);
    }

    public void writeEmptyElement(String arg0, String arg1, String arg2) throws XMLStreamException {
        createWriter();
        writer.writeEmptyElement(arg0, arg1, arg2);
    }

    public void writeEmptyElement(String arg0, String arg1) throws XMLStreamException {
        createWriter();
        writer.writeEmptyElement(arg0, arg1);
    }

    public void writeEmptyElement(String arg0) throws XMLStreamException {
        createWriter();
        writer.writeEmptyElement(arg0);
    }

    public void writeEndDocument() throws XMLStreamException {
        createWriter();
        writer.writeEndDocument();
    }

    public void writeEndElement() throws XMLStreamException {
        createWriter();
        writer.writeEndElement();
    }

    public void writeEntityRef(String arg0) throws XMLStreamException {
        createWriter();
        writer.writeEntityRef(arg0);
    }

    public void writeNamespace(String arg0, String arg1) throws XMLStreamException {
        createWriter();
        writer.writeNamespace(arg0, arg1);
    }

    public void writeProcessingInstruction(String arg0, String arg1) throws XMLStreamException {
        createWriter();
        writer.writeProcessingInstruction(arg0, arg1);
    }

    public void writeProcessingInstruction(String arg0) throws XMLStreamException {
        createWriter();
        writer.writeProcessingInstruction(arg0);
    }

    public void writeStartDocument() throws XMLStreamException {
        createWriter();
        writer.writeStartDocument();
    }

    public void writeStartDocument(String arg0, String arg1) throws XMLStreamException {
        createWriter();
        writer.writeStartDocument(arg0, arg1);
    }

    public void writeStartDocument(String arg0) throws XMLStreamException {
        createWriter();
        writer.writeStartDocument(arg0);
    }

    public void writeStartElement(String arg0, String arg1, String arg2) throws XMLStreamException {
        createWriter();
        writer.writeStartElement(arg0, arg1, arg2);
    }

    public void writeStartElement(String arg0, String arg1) throws XMLStreamException {
        createWriter();
        writer.writeStartElement(arg0, arg1);
    }

    public void writeStartElement(String arg0) throws XMLStreamException {
        createWriter();
        writer.writeStartElement(arg0);
    }

    /**
     * If this XMLStreamWriter is connected to an OutputStream
     * then the OutputStream is returned.  This allows a node
     * (perhaps an OMSourcedElement) to write its content
     * directly to the OutputStream.
     * @return OutputStream or null
     */
    public OutputStream getOutputStream() throws XMLStreamException {

        if (os != null) {
            // Flush the state of the writer..Many times the 
            // write defers the writing of tag characters (>)
            // until the next write.  Flush out this character
            if (writer != null) {
                this.writeCharacters("");
                this.flush();
            }
        }
        return os;
    }
}