Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
XMLOutputStreamWriter |
|
| 1.0;1 |
1 | /* | |
2 | * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons-sandbox//xmlio/src/java/org/apache/commons/xmlio/out/XMLOutputStreamWriter.java,v 1.1 2004/10/08 11:56:20 ozeigermann Exp $ | |
3 | * $Revision: 155476 $ | |
4 | * $Date: 2005-02-26 13:31:24 +0000 (Sat, 26 Feb 2005) $ | |
5 | * | |
6 | * ==================================================================== | |
7 | * | |
8 | * Copyright 2004 The Apache Software Foundation | |
9 | * | |
10 | * Licensed under the Apache License, Version 2.0 (the "License"); | |
11 | * you may not use this file except in compliance with the License. | |
12 | * You may obtain a copy of the License at | |
13 | * | |
14 | * http://www.apache.org/licenses/LICENSE-2.0 | |
15 | * | |
16 | * Unless required by applicable law or agreed to in writing, software | |
17 | * distributed under the License is distributed on an "AS IS" BASIS, | |
18 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
19 | * See the License for the specific language governing permissions and | |
20 | * limitations under the License. | |
21 | * | |
22 | */ | |
23 | ||
24 | package org.apache.commons.xmlio.out; | |
25 | ||
26 | import java.io.*; | |
27 | ||
28 | /** | |
29 | * Adds XML export functionality to the underlying output stream. Formatting and | |
30 | * encoding is done as straight forward as possible. <br> | |
31 | * Everything you know better than this class must be done by you, e.g. you will | |
32 | * have to tell <code>XMLOutputStreamWriter</code> where you wish to have | |
33 | * newlines.In effect, no unexpected so called | |
34 | * <em>intelligent</em> behavior is to be feared. Another effect is high speed. | |
35 | * <br> | |
36 | * <br> | |
37 | * <em>Caution</em>: Do not forget to call {@link #flush} at the end of your | |
38 | * exporting process as otherwise no data might be written. | |
39 | * | |
40 | * <em>Warning</em>: When using two byte encoding (e.g. UTF-16) underlying | |
41 | * OutputStream can | |
42 | * not savely be brought to string. Do <em>not</em> use | |
43 | * {@link ByteArrayOutputStream} with two byte encoding, as XML declaration | |
44 | * will be in single byte encoding (according to XML spec) and the rest will be | |
45 | * in double byte totally confusing ByteArrayOutputStream encoding to string. | |
46 | * <b>If you want to have string output use {@link XMLWriter} filtering | |
47 | * {@link StringWriter} or for convenience {@link XMLStringWriter}.</b> | |
48 | * | |
49 | */ | |
50 | public class XMLOutputStreamWriter extends XMLWriter { | |
51 | /** Name of UTF-8 encoding */ | |
52 | 0 | public static String ENCODING_UTF_8 = "UTF-8"; |
53 | /** Name of UTF-16 encoding */ | |
54 | 0 | public static String ENCODING_UTF_16 = "UTF-16"; |
55 | /** Name of ISO-8859-1 encoding */ | |
56 | 0 | public static String ENCODING_ISO_8859_1 = "ISO-8859-1"; |
57 | ||
58 | /** Name of standard encoding */ | |
59 | 0 | public static String ENCODING_STANDARD = ENCODING_UTF_8; |
60 | /** Alias for ISO-8859-1 encoding */ | |
61 | 0 | public static String ENCODING_ISO_LATIN1 = ENCODING_ISO_8859_1; |
62 | ||
63 | protected OutputStream os; | |
64 | protected String encodingName; | |
65 | ||
66 | /** Creates a new output stream writer for XML export. | |
67 | * @param os the underlying output stream the XML is exported to | |
68 | * @param encodingName name of the encoding used to write XML as well as | |
69 | * for the XML declataration (e.g. UTF-8, ISO-8859-1, ...) | |
70 | */ | |
71 | public XMLOutputStreamWriter(OutputStream os, String encodingName) throws UnsupportedEncodingException { | |
72 | 0 | super(new OutputStreamWriter(os, encodingName)); |
73 | 0 | this.encodingName = encodingName; |
74 | 0 | this.os = os; |
75 | 0 | } |
76 | ||
77 | /** Creates a new output stream writer for XML export. Standard encoding | |
78 | * will be used as found in {@link #ENCODING_STANDARD}, which usually is | |
79 | * UTF-8. | |
80 | * @param os the underlying output stream the XML is exported to | |
81 | * @see #XMLOutputStreamWriter(OutputStream, String) | |
82 | */ | |
83 | public XMLOutputStreamWriter(OutputStream os) throws UnsupportedEncodingException { | |
84 | 0 | this(os, ENCODING_STANDARD); |
85 | 0 | } |
86 | ||
87 | /** Gets the name of the encoding as it would be inserted into the | |
88 | * XML declaration. {@link OutputStreamWriter#getEncoding} may return something less verbose. | |
89 | * @see OutputStreamWriter#getEncoding | |
90 | */ | |
91 | public String getEncodingName() { | |
92 | 0 | return encodingName; |
93 | } | |
94 | ||
95 | /** Writes XML delcaration using version 1.0 and encoding specified in | |
96 | * constructor. | |
97 | * <em>Caution</em>: As XML declaration must be in plain text (no UNICODE) | |
98 | * it will not be passed to writer, but directly to stream! | |
99 | */ | |
100 | public void writeXMLDeclaration() throws IOException { | |
101 | 0 | String xmlDecl = "<?xml version=\"1.0\" encoding=\"" + getEncodingName() + "\"?>\n"; |
102 | 0 | byte[] xmlDeclBytes = xmlDecl.getBytes("US-ASCII"); |
103 | ||
104 | // flush to ensure correct sequence | |
105 | 0 | flush(); |
106 | 0 | os.write(xmlDeclBytes); |
107 | 0 | } |
108 | ||
109 | } |