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.FileInputStream; 24 import java.io.FileNotFoundException; 25 import java.io.FileReader; 26 import java.io.IOException; 27 import java.io.InputStream; 28 import java.io.InputStreamReader; 29 import java.io.Reader; 30 import java.io.UnsupportedEncodingException; 31 import java.net.URL; 32 import org.apache.commons.io.input.XmlStreamReader; 33 34 import javax.annotation.Nonnull; 35 36 37 /** 38 * Utility to create Readers from streams, with explicit encoding choice: platform default, 39 * XML, or specified. 40 * 41 * @author <a href="mailto:hboutemy@apache.org">Hervé Boutemy</a> 42 * @see java.nio.charset.Charset 43 * @see <a href="http://docs.oracle.com/javase/1.5.0/docs/guide/intl/encoding.doc.html">Supported encodings</a> 44 */ 45 public class ReaderFactory 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 * 51 * @see java.nio.charset.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 * 59 * @see java.nio.charset.Charset 60 */ 61 public static final String US_ASCII = "US-ASCII"; 62 63 /** 64 * Sixteen-bit Unicode Transformation Format, byte order specified by a mandatory initial byte-order mark (either 65 * order accepted on input, big-endian used on output). 66 * Every implementation of the Java platform is required to support this character encoding. 67 * 68 * @see java.nio.charset.Charset 69 */ 70 public static final String UTF_16 = "UTF-16"; 71 72 /** 73 * Sixteen-bit Unicode Transformation Format, big-endian byte order. 74 * Every implementation of the Java platform is required to support this character encoding. 75 * 76 * @see java.nio.charset.Charset 77 */ 78 public static final String UTF_16BE = "UTF-16BE"; 79 80 /** 81 * Sixteen-bit Unicode Transformation Format, little-endian byte order. 82 * Every implementation of the Java platform is required to support this character encoding. 83 * 84 * @see java.nio.charset.Charset 85 */ 86 public static final String UTF_16LE = "UTF-16LE"; 87 88 /** 89 * Eight-bit Unicode Transformation Format. 90 * Every implementation of the Java platform is required to support this character encoding. 91 * 92 * @see java.nio.charset.Charset 93 */ 94 public static final String UTF_8 = "UTF-8"; 95 96 /** 97 * The <code>file.encoding</code> System Property. 98 */ 99 public static final String FILE_ENCODING = System.getProperty( "file.encoding" ); 100 101 /** 102 * Create a new Reader with XML encoding detection rules. 103 * 104 * @param in not null input stream. 105 * @return an XML reader instance for the input stream. 106 * @throws IOException if any. 107 * @see XmlStreamReader 108 */ 109 public static Reader newXmlReader( @Nonnull InputStream in ) 110 throws IOException 111 { 112 return new XmlStreamReader( in ); 113 } 114 115 /** 116 * Create a new Reader with XML encoding detection rules. 117 * 118 * @param file not null file. 119 * @return an XML reader instance for the input file. 120 * @throws IOException if any. 121 * @see XmlStreamReader 122 */ 123 public static Reader newXmlReader( @Nonnull File file ) 124 throws IOException 125 { 126 return new XmlStreamReader( file ); 127 } 128 129 /** 130 * Create a new Reader with XML encoding detection rules. 131 * 132 * @param url not null url. 133 * @return an XML reader instance for the input url. 134 * @throws IOException if any. 135 * @see XmlStreamReader 136 */ 137 public static Reader newXmlReader( @Nonnull URL url ) 138 throws IOException 139 { 140 return new XmlStreamReader( url ); 141 } 142 143 /** 144 * Create a new Reader with default plaform encoding. 145 * 146 * @param file not null file. 147 * @return a reader instance for the input file using the default platform charset. 148 * @throws FileNotFoundException if any. 149 * @see java.nio.charset.Charset#defaultCharset() 150 */ 151 public static Reader newPlatformReader( @Nonnull File file ) 152 throws FileNotFoundException 153 { 154 return new FileReader( file ); 155 } 156 157 /** 158 * Create a new Reader with specified encoding. 159 * 160 * @param in not null input stream. 161 * @param encoding not null supported encoding. 162 * @return a reader instance for the input stream using the given encoding. 163 * @throws UnsupportedEncodingException if any. 164 * @see <a href="http://docs.oracle.com/javase/1.5.0/docs/guide/intl/encoding.doc.html">Supported encodings</a> 165 */ 166 public static Reader newReader( @Nonnull InputStream in, @Nonnull String encoding ) 167 throws UnsupportedEncodingException 168 { 169 return new InputStreamReader( in, encoding ); 170 } 171 172 /** 173 * Create a new Reader with specified encoding. 174 * 175 * @param file not null file. 176 * @param encoding not null supported encoding. 177 * @return a reader instance for the input file using the given encoding. 178 * @throws FileNotFoundException if any. 179 * @throws UnsupportedEncodingException if any. 180 * @see <a href="http://docs.oracle.com/javase/1.5.0/docs/guide/intl/encoding.doc.html">Supported encodings</a> 181 */ 182 public static Reader newReader( @Nonnull File file, @Nonnull String encoding ) 183 throws FileNotFoundException, UnsupportedEncodingException 184 { 185 return new InputStreamReader( new FileInputStream( file ), encoding ); 186 } 187 188 /** 189 * Create a new Reader with specified encoding. 190 * 191 * @param url not null url. 192 * @param encoding not null supported encoding. 193 * @return a reader instance for the input url using the given encoding. 194 * @throws IOException if any. 195 * @see <a href="http://docs.oracle.com/javase/1.5.0/docs/guide/intl/encoding.doc.html">Supported encodings</a> 196 */ 197 public static Reader newReader( @Nonnull URL url, @Nonnull String encoding ) 198 throws IOException 199 { 200 return new InputStreamReader( url.openStream(), encoding ); 201 } 202 }