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