Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
WriterFactory |
|
| 1.0;1 |
1 | package org.apache.maven.shared.utils; | |
2 | ||
3 | /* | |
4 | * Licensed to the Apache Software Foundation (ASF) under one | |
5 | * or more contributor license agreements. See the NOTICE file | |
6 | * distributed with this work for additional information | |
7 | * regarding copyright ownership. The ASF licenses this file | |
8 | * to you under the Apache License, Version 2.0 (the | |
9 | * "License"); you may not use this file except in compliance | |
10 | * with the License. You may obtain a copy of the License at | |
11 | * | |
12 | * http://www.apache.org/licenses/LICENSE-2.0 | |
13 | * | |
14 | * Unless required by applicable law or agreed to in writing, | |
15 | * software distributed under the License is distributed on an | |
16 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
17 | * KIND, either express or implied. See the License for the | |
18 | * specific language governing permissions and limitations | |
19 | * under the License. | |
20 | */ | |
21 | ||
22 | import java.io.File; | |
23 | import java.io.FileNotFoundException; | |
24 | import java.io.FileOutputStream; | |
25 | import java.io.FileWriter; | |
26 | import java.io.IOException; | |
27 | import java.io.OutputStream; | |
28 | import java.io.OutputStreamWriter; | |
29 | import java.io.UnsupportedEncodingException; | |
30 | import java.io.Writer; | |
31 | import java.nio.charset.Charset; | |
32 | import org.apache.maven.shared.utils.xml.XmlStreamWriter; | |
33 | ||
34 | ||
35 | import javax.annotation.Nonnull; | |
36 | ||
37 | /** | |
38 | * Utility to create Writers, with explicit encoding choice: platform default, | |
39 | * XML, or specified. | |
40 | * | |
41 | * @author Hervé Boutemy | |
42 | * @see Charset | |
43 | * @see <a href="http://docs.oracle.com/javase/1.5.0/docs/guide/intl/encoding.doc.html">Supported encodings</a> | |
44 | * @version $Id: WriterFactory.java 1400677 2012-10-21 17:00:51Z rfscholte $ | |
45 | */ | |
46 | 0 | public class WriterFactory |
47 | { | |
48 | /** | |
49 | * ISO Latin Alphabet #1, also known as ISO-LATIN-1. | |
50 | * Every implementation of the Java platform is required to support this character encoding. | |
51 | * @see Charset | |
52 | */ | |
53 | public static final String ISO_8859_1 = "ISO-8859-1"; | |
54 | ||
55 | /** | |
56 | * Seven-bit ASCII, also known as ISO646-US, also known as the Basic Latin block of the Unicode character set. | |
57 | * Every implementation of the Java platform is required to support this character encoding. | |
58 | * @see Charset | |
59 | */ | |
60 | public static final String US_ASCII = "US-ASCII"; | |
61 | ||
62 | /** | |
63 | * Sixteen-bit Unicode Transformation Format, byte order specified by a mandatory initial byte-order mark (either | |
64 | * order accepted on input, big-endian used on output). | |
65 | * Every implementation of the Java platform is required to support this character encoding. | |
66 | * @see Charset | |
67 | */ | |
68 | public static final String UTF_16 = "UTF-16"; | |
69 | ||
70 | /** | |
71 | * Sixteen-bit Unicode Transformation Format, big-endian byte order. | |
72 | * Every implementation of the Java platform is required to support this character encoding. | |
73 | * @see Charset | |
74 | */ | |
75 | public static final String UTF_16BE = "UTF-16BE"; | |
76 | ||
77 | /** | |
78 | * Sixteen-bit Unicode Transformation Format, little-endian byte order. | |
79 | * Every implementation of the Java platform is required to support this character encoding. | |
80 | * @see Charset | |
81 | */ | |
82 | public static final String UTF_16LE = "UTF-16LE"; | |
83 | ||
84 | /** | |
85 | * Eight-bit Unicode Transformation Format. | |
86 | * Every implementation of the Java platform is required to support this character encoding. | |
87 | * @see Charset | |
88 | */ | |
89 | public static final String UTF_8 = "UTF-8"; | |
90 | ||
91 | /** | |
92 | * The <code>file.encoding</code> System Property. | |
93 | */ | |
94 | 1 | public static final String FILE_ENCODING = System.getProperty( "file.encoding" ); |
95 | ||
96 | /** | |
97 | * Create a new Writer with XML encoding detection rules. | |
98 | * | |
99 | * @param out not null output stream. | |
100 | * @return an XML writer instance for the output stream. | |
101 | * @throws IOException if any. | |
102 | * @see XmlStreamWriter | |
103 | */ | |
104 | public static XmlStreamWriter newXmlWriter( @Nonnull OutputStream out ) | |
105 | throws IOException | |
106 | { | |
107 | 23 | return new XmlStreamWriter( out ); |
108 | } | |
109 | ||
110 | /** | |
111 | * Create a new Writer with XML encoding detection rules. | |
112 | * | |
113 | * @param file not null file. | |
114 | * @return an XML writer instance for the output file. | |
115 | * @throws IOException if any. | |
116 | * @see XmlStreamWriter | |
117 | */ | |
118 | public static XmlStreamWriter newXmlWriter( @Nonnull File file ) | |
119 | throws IOException | |
120 | { | |
121 | 0 | return new XmlStreamWriter( file ); |
122 | } | |
123 | ||
124 | /** | |
125 | * Create a new Writer with default platform encoding. | |
126 | * | |
127 | * @param out not null output stream. | |
128 | * @return a writer instance for the output stream using the default platform charset. | |
129 | * @see Charset#defaultCharset() | |
130 | */ | |
131 | public static Writer newPlatformWriter( @Nonnull OutputStream out ) | |
132 | { | |
133 | 0 | return new OutputStreamWriter( out ); |
134 | } | |
135 | ||
136 | /** | |
137 | * Create a new Writer with default platform encoding. | |
138 | * | |
139 | * @param file not null file. | |
140 | * @return a writer instance for the output file using the default platform charset. | |
141 | * @throws IOException if any. | |
142 | * @see Charset#defaultCharset() | |
143 | */ | |
144 | public static Writer newPlatformWriter( @Nonnull File file ) | |
145 | throws IOException | |
146 | { | |
147 | 0 | return new FileWriter( file ); |
148 | } | |
149 | ||
150 | /** | |
151 | * Create a new Writer with specified encoding. | |
152 | * | |
153 | * @param out not null output stream. | |
154 | * @param encoding not null supported encoding. | |
155 | * @return a writer instance for the output stream using the given encoding. | |
156 | * @throws UnsupportedEncodingException if any. | |
157 | * @see <a href="http://docs.oracle.com/javase/1.5.0/docs/guide/intl/encoding.doc.html">Supported encodings</a> | |
158 | */ | |
159 | public static Writer newWriter( @Nonnull OutputStream out, @Nonnull String encoding ) | |
160 | throws UnsupportedEncodingException | |
161 | { | |
162 | 0 | return new OutputStreamWriter( out, encoding ); |
163 | } | |
164 | ||
165 | /** | |
166 | * Create a new Writer with specified encoding. | |
167 | * | |
168 | * @param file not null file. | |
169 | * @param encoding not null supported encoding. | |
170 | * @return a writer instance for the output file using the given encoding. | |
171 | * @throws UnsupportedEncodingException if any. | |
172 | * @throws FileNotFoundException if any. | |
173 | * @see <a href="http://docs.oracle.com/javase/1.5.0/docs/guide/intl/encoding.doc.html">Supported encodings</a> | |
174 | */ | |
175 | public static Writer newWriter( @Nonnull File file, @Nonnull String encoding ) | |
176 | throws UnsupportedEncodingException, FileNotFoundException | |
177 | { | |
178 | 0 | return newWriter( new FileOutputStream( file ), encoding ); |
179 | } | |
180 | } |