View Javadoc
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.FileNotFoundException;
21  import java.io.FileOutputStream;
22  import java.io.FileWriter;
23  import java.io.IOException;
24  import java.io.OutputStream;
25  import java.io.OutputStreamWriter;
26  import java.io.UnsupportedEncodingException;
27  import java.io.Writer;
28  import java.nio.charset.Charset;
29  
30  import org.codehaus.plexus.util.xml.XmlStreamWriter;
31  
32  /**
33   * Utility to create Writers, with explicit encoding choice: platform default, XML, or specified.
34   *
35   * @author <a href="mailto:hboutemy@codehaus.org">Herve Boutemy</a>
36   * @see Charset
37   * @see <a href="http://java.sun.com/j2se/1.4.2/docs/guide/intl/encoding.doc.html">Supported encodings</a>
38   * @version $Id$
39   * @since 1.4.4
40   */
41  public class WriterFactory
42  {
43      /**
44       * ISO Latin Alphabet #1, also known as ISO-LATIN-1. Every implementation of the Java platform is required to
45       * support this character encoding.
46       * 
47       * @see Charset
48       */
49      public static final String ISO_8859_1 = "ISO-8859-1";
50  
51      /**
52       * Seven-bit ASCII, also known as ISO646-US, also known as the Basic Latin block of the Unicode character set. Every
53       * implementation of the Java platform is required to support this character encoding.
54       * 
55       * @see Charset
56       */
57      public static final String US_ASCII = "US-ASCII";
58  
59      /**
60       * Sixteen-bit Unicode Transformation Format, byte order specified by a mandatory initial byte-order mark (either
61       * order accepted on input, big-endian used on output). Every implementation of the Java platform is required to
62       * support this character encoding.
63       * 
64       * @see Charset
65       */
66      public static final String UTF_16 = "UTF-16";
67  
68      /**
69       * Sixteen-bit Unicode Transformation Format, big-endian byte order. Every implementation of the Java platform is
70       * required to support this character encoding.
71       * 
72       * @see Charset
73       */
74      public static final String UTF_16BE = "UTF-16BE";
75  
76      /**
77       * Sixteen-bit Unicode Transformation Format, little-endian byte order. Every implementation of the Java platform is
78       * required to support this character encoding.
79       * 
80       * @see Charset
81       */
82      public static final String UTF_16LE = "UTF-16LE";
83  
84      /**
85       * Eight-bit Unicode Transformation Format. Every implementation of the Java platform is required to support this
86       * character encoding.
87       * 
88       * @see Charset
89       */
90      public static final String UTF_8 = "UTF-8";
91  
92      /**
93       * The <code>file.encoding</code> System Property.
94       */
95      public static final String FILE_ENCODING = System.getProperty( "file.encoding" );
96  
97      /**
98       * Create a new Writer with XML encoding detection rules.
99       *
100      * @param out not null output stream.
101      * @return an XML writer instance for the output stream.
102      * @throws IOException if any.
103      * @see XmlStreamWriter
104      */
105     public static XmlStreamWriter newXmlWriter( OutputStream out )
106         throws IOException
107     {
108         return new XmlStreamWriter( out );
109     }
110 
111     /**
112      * Create a new Writer with XML encoding detection rules.
113      *
114      * @param file not null file.
115      * @return an XML writer instance for the output file.
116      * @throws IOException if any.
117      * @see XmlStreamWriter
118      */
119     public static XmlStreamWriter newXmlWriter( File file )
120         throws IOException
121     {
122         return new XmlStreamWriter( file );
123     }
124 
125     /**
126      * Create a new Writer with default platform encoding.
127      *
128      * @param out not null output stream.
129      * @return a writer instance for the output stream using the default platform charset.
130      * @throws IOException if any.
131      * @see Charset#defaultCharset()
132      */
133     public static Writer newPlatformWriter( OutputStream out )
134     {
135         return new OutputStreamWriter( out );
136     }
137 
138     /**
139      * Create a new Writer with default platform encoding.
140      *
141      * @param file not null file.
142      * @return a writer instance for the output file using the default platform charset.
143      * @throws IOException if any.
144      * @see Charset#defaultCharset()
145      */
146     public static Writer newPlatformWriter( File file )
147         throws IOException
148     {
149         return new FileWriter( file );
150     }
151 
152     /**
153      * Create a new Writer with specified encoding.
154      *
155      * @param out not null output stream.
156      * @param encoding not null supported encoding.
157      * @return a writer instance for the output stream using the given encoding.
158      * @throws UnsupportedEncodingException if any.
159      * @see <a href="http://java.sun.com/j2se/1.4.2/docs/guide/intl/encoding.doc.html">Supported encodings</a>
160      */
161     public static Writer newWriter( OutputStream out, String encoding )
162         throws UnsupportedEncodingException
163     {
164         return new OutputStreamWriter( out, encoding );
165     }
166 
167     /**
168      * Create a new Writer with specified encoding.
169      *
170      * @param file not null file.
171      * @param encoding not null supported encoding.
172      * @return a writer instance for the output file using the given encoding.
173      * @throws UnsupportedEncodingException if any.
174      * @throws FileNotFoundException if any.
175      * @see <a href="http://java.sun.com/j2se/1.4.2/docs/guide/intl/encoding.doc.html">Supported encodings</a>
176      */
177     public static Writer newWriter( File file, String encoding )
178         throws UnsupportedEncodingException, FileNotFoundException
179     {
180         return newWriter( new FileOutputStream( file ), encoding );
181     }
182 }