001: /*
002: * The contents of this file are subject to the terms
003: * of the Common Development and Distribution License
004: * (the "License"). You may not use this file except
005: * in compliance with the License.
006: *
007: * You can obtain a copy of the license at
008: * https://jwsdp.dev.java.net/CDDLv1.0.html
009: * See the License for the specific language governing
010: * permissions and limitations under the License.
011: *
012: * When distributing Covered Code, include this CDDL
013: * HEADER in each file and include the License file at
014: * https://jwsdp.dev.java.net/CDDLv1.0.html If applicable,
015: * add the following below this CDDL HEADER, with the
016: * fields enclosed by brackets "[]" replaced with your
017: * own identifying information: Portions Copyright [yyyy]
018: * [name of copyright owner]
019: */
020:
021: package com.sun.xml.txw2.output;
022:
023: import com.sun.xml.txw2.TxwException;
024:
025: import javax.xml.transform.stream.StreamResult;
026: import java.io.BufferedWriter;
027: import java.io.FileOutputStream;
028: import java.io.IOException;
029: import java.io.OutputStream;
030: import java.io.OutputStreamWriter;
031: import java.io.Writer;
032: import java.io.UnsupportedEncodingException;
033:
034: /**
035: * {@link XmlSerializer} for {@link javax.xml.transform.stream.StreamResult}.
036: *
037: * @author Ryan.Shoemaker@Sun.COM
038: */
039: public class StreamSerializer implements XmlSerializer {
040:
041: // delegate to SaxSerializer
042: private final SaxSerializer serializer;
043:
044: private final XMLWriter writer;
045:
046: public StreamSerializer(OutputStream out) {
047: this (createWriter(out));
048: }
049:
050: public StreamSerializer(OutputStream out, String encoding)
051: throws UnsupportedEncodingException {
052: this (createWriter(out, encoding));
053: }
054:
055: public StreamSerializer(Writer out) {
056: this (new StreamResult(out));
057: }
058:
059: public StreamSerializer(StreamResult streamResult) {
060: // if this method opened a stream, let it close it
061: final OutputStream[] autoClose = new OutputStream[1];
062:
063: if (streamResult.getWriter() != null)
064: writer = createWriter(streamResult.getWriter());
065: else if (streamResult.getOutputStream() != null)
066: writer = createWriter(streamResult.getOutputStream());
067: else if (streamResult.getSystemId() != null) {
068: String fileURL = streamResult.getSystemId();
069:
070: fileURL = convertURL(fileURL);
071:
072: try {
073: FileOutputStream fos = new FileOutputStream(fileURL);
074: autoClose[0] = fos;
075: writer = createWriter(fos);
076: } catch (IOException e) {
077: throw new TxwException(e);
078: }
079: } else
080: throw new IllegalArgumentException();
081:
082: // now delegate to the SaxSerializer
083: serializer = new SaxSerializer(writer, writer, false) {
084: public void endDocument() {
085: super .endDocument();
086: if (autoClose[0] != null) {
087: try {
088: autoClose[0].close();
089: } catch (IOException e) {
090: throw new TxwException(e);
091: }
092: autoClose[0] = null;
093: }
094: }
095: };
096: }
097:
098: private StreamSerializer(XMLWriter writer) {
099: this .writer = writer;
100: // now delegate to the SaxSerializer
101: serializer = new SaxSerializer(writer, writer, false);
102: }
103:
104: private String convertURL(String url) {
105: url = url.replace('\\', '/');
106: url = url.replaceAll("//", "/");
107: url = url.replaceAll("//", "/");
108: if (url.startsWith("file:/")) {
109: if (url.substring(6).indexOf(":") > 0)
110: url = url.substring(6);
111: else
112: url = url.substring(5);
113: } // otherwise assume that it's a file name
114: return url;
115: }
116:
117: // XmlSerializer api's - delegate to SaxSerializer
118: public void startDocument() {
119: serializer.startDocument();
120: }
121:
122: public void beginStartTag(String uri, String localName,
123: String prefix) {
124: serializer.beginStartTag(uri, localName, prefix);
125: }
126:
127: public void writeAttribute(String uri, String localName,
128: String prefix, StringBuilder value) {
129: serializer.writeAttribute(uri, localName, prefix, value);
130: }
131:
132: public void writeXmlns(String prefix, String uri) {
133: serializer.writeXmlns(prefix, uri);
134: }
135:
136: public void endStartTag(String uri, String localName, String prefix) {
137: serializer.endStartTag(uri, localName, prefix);
138: }
139:
140: public void endTag() {
141: serializer.endTag();
142: }
143:
144: public void text(StringBuilder text) {
145: serializer.text(text);
146: }
147:
148: public void cdata(StringBuilder text) {
149: serializer.cdata(text);
150: }
151:
152: public void comment(StringBuilder comment) {
153: serializer.comment(comment);
154: }
155:
156: public void endDocument() {
157: serializer.endDocument();
158: }
159:
160: public void flush() {
161: serializer.flush();
162: try {
163: writer.flush();
164: } catch (IOException e) {
165: throw new TxwException(e);
166: }
167: }
168:
169: // other supporting code
170: private static XMLWriter createWriter(Writer w) {
171: // buffering improves the performance
172: DataWriter dw = new DataWriter(new BufferedWriter(w));
173: dw.setIndentStep(" ");
174: return dw;
175: }
176:
177: private static XMLWriter createWriter(OutputStream os,
178: String encoding) throws UnsupportedEncodingException {
179: XMLWriter writer = createWriter(new OutputStreamWriter(os,
180: encoding));
181: writer.setEncoding(encoding);
182: return writer;
183: }
184:
185: private static XMLWriter createWriter(OutputStream os) {
186: try {
187: return createWriter(os, "UTF-8");
188: } catch (UnsupportedEncodingException e) {
189: // UTF-8 is supported on all platforms.
190: throw new Error(e);
191: }
192: }
193:
194: }
|