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: package org.apache.axis2.jaxws.message.util;
20:
21: import org.apache.axiom.om.OMDocument;
22: import org.apache.axiom.om.OMNode;
23: import org.apache.axiom.om.impl.builder.StAXOMBuilder;
24: import org.apache.axiom.om.util.StAXUtils;
25:
26: import javax.xml.stream.XMLStreamException;
27: import javax.xml.stream.XMLStreamReader;
28: import javax.xml.stream.XMLStreamWriter;
29: import java.io.StringWriter;
30: import java.util.Iterator;
31:
32: /**
33: * Reader2Writer This is a simple converter that is constructed with an XMLStreamReader and allows
34: * you to write the contents to an XMLStreamWriter.
35: */
36: public class Reader2Writer {
37:
38: private XMLStreamReader reader;
39:
40: /**
41: * Construct from a Reader
42: *
43: * @param reader -- the input to the converter
44: */
45: public Reader2Writer(XMLStreamReader reader) {
46: this .reader = reader;
47: }
48:
49: /**
50: * outputTo the writer.
51: *
52: * @param writer -- the output of the converter
53: */
54: public void outputTo(XMLStreamWriter writer)
55: throws XMLStreamException {
56: // Using OM to convert the reader to a writer. This seems to be
57: // the safest way to make the conversion, and it promotes code re-use.
58: StAXOMBuilder builder = new StAXOMBuilder(reader);
59: OMDocument omDocument = builder.getDocument();
60: Iterator it = omDocument.getChildren();
61: while (it.hasNext()) {
62: OMNode omNode = (OMNode) it.next();
63: // TODO Using serialize and consume
64: // caused an axiom bug...falling back to serialize
65: // (which is less performant due to om caching)
66: //omNode.serializeAndConsume(writer);
67: omNode.serialize(writer);
68: }
69: // Close the reader
70: reader.close();
71: }
72:
73: /**
74: * Utility method to write the reader contents to a String
75: * @return String
76: */
77: public String getAsString() throws XMLStreamException {
78: StringWriter sw = new StringWriter();
79: XMLStreamWriter writer = StAXUtils.createXMLStreamWriter(sw);
80:
81: // Write the reader to the writer
82: outputTo(writer);
83:
84: // Flush the writer and get the String
85: writer.flush();
86: sw.flush();
87: String str = sw.toString();
88: writer.close();
89: return str;
90: }
91: }
|