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 org.apache.maven.shared.utils.xml.XmlStreamWriter; 32 33 34 import javax.annotation.Nonnull; 35 36 /** 37 * Utility to create Writers, with explicit encoding choice: platform default, 38 * XML, or specified. 39 * 40 * @author Hervé Boutemy 41 * @see java.nio.charset.Charset 42 * @see <a href="http://docs.oracle.com/javase/1.5.0/docs/guide/intl/encoding.doc.html">Supported encodings</a> 43 * @version $Id: WriterFactory.java 1627926 2014-09-27 07:01:33Z hboutemy $ 44 */ 45 public class WriterFactory 46 { 47 /** 48 * ISO Latin Alphabet #1, also known as ISO-LATIN-1. 49 * Every implementation of the Java platform is required to support this character encoding. 50 * @see java.nio.charset.Charset 51 */ 52 public static final String ISO_8859_1 = "ISO-8859-1"; 53 54 /** 55 * Seven-bit ASCII, also known as ISO646-US, also known as the Basic Latin block of the Unicode character set. 56 * Every implementation of the Java platform is required to support this character encoding. 57 * @see java.nio.charset.Charset 58 */ 59 public static final String US_ASCII = "US-ASCII"; 60 61 /** 62 * Sixteen-bit Unicode Transformation Format, byte order specified by a mandatory initial byte-order mark (either 63 * order accepted on input, big-endian used on output). 64 * Every implementation of the Java platform is required to support this character encoding. 65 * @see java.nio.charset.Charset 66 */ 67 public static final String UTF_16 = "UTF-16"; 68 69 /** 70 * Sixteen-bit Unicode Transformation Format, big-endian byte order. 71 * Every implementation of the Java platform is required to support this character encoding. 72 * @see java.nio.charset.Charset 73 */ 74 public static final String UTF_16BE = "UTF-16BE"; 75 76 /** 77 * Sixteen-bit Unicode Transformation Format, little-endian byte order. 78 * Every implementation of the Java platform is required to support this character encoding. 79 * @see java.nio.charset.Charset 80 */ 81 public static final String UTF_16LE = "UTF-16LE"; 82 83 /** 84 * Eight-bit Unicode Transformation Format. 85 * Every implementation of the Java platform is required to support this character encoding. 86 * @see java.nio.charset.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( @Nonnull 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( @Nonnull 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 java.nio.charset.Charset#defaultCharset() 129 */ 130 public static Writer newPlatformWriter( @Nonnull 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 java.nio.charset.Charset#defaultCharset() 142 */ 143 public static Writer newPlatformWriter( @Nonnull File file ) 144 throws IOException 145 { 146 return new FileWriter( file ); 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://docs.oracle.com/javase/1.5.0/docs/guide/intl/encoding.doc.html">Supported encodings</a> 157 */ 158 public static Writer newWriter( @Nonnull OutputStream out, @Nonnull 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 UnsupportedEncodingException if any. 171 * @throws FileNotFoundException if any. 172 * @see <a href="http://docs.oracle.com/javase/1.5.0/docs/guide/intl/encoding.doc.html">Supported encodings</a> 173 */ 174 public static Writer newWriter( @Nonnull File file, @Nonnull String encoding ) 175 throws UnsupportedEncodingException, FileNotFoundException 176 { 177 return newWriter( new FileOutputStream( file ), encoding ); 178 } 179 }