16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package java.util; 27 28 import java.io.*; 29 import java.math.*; 30 import java.nio.*; 31 import java.nio.channels.*; 32 import java.nio.charset.*; 33 import java.nio.file.Path; 34 import java.nio.file.Files; 35 import java.text.*; 36 import java.util.function.Consumer; 37 import java.util.regex.*; 38 import java.util.stream.Stream; 39 import java.util.stream.StreamSupport; 40 41 /** 42 * A simple text scanner which can parse primitive types and strings using 43 * regular expressions. 44 * 45 * <p>A {@code Scanner} breaks its input into tokens using a 46 * delimiter pattern, which by default matches whitespace. The resulting 47 * tokens may then be converted into values of different types using the 48 * various {@code next} methods. 49 * 50 * <p>For example, this code allows a user to read a number from 51 * {@code System.in}: 52 * <blockquote><pre>{@code 53 * Scanner sc = new Scanner(System.in); 54 * int i = sc.nextInt(); 55 * }</pre></blockquote> 56 * 57 * <p>As another example, this code allows {@code long} types to be 58 * assigned from entries in a file {@code myNumbers}: 59 * <blockquote><pre>{@code 1169 1170 /** 1171 * Sets this scanner's locale to the specified locale. 1172 * 1173 * <p>A scanner's locale affects many elements of its default 1174 * primitive matching regular expressions; see 1175 * <a href= "#localized-numbers">localized numbers</a> above. 1176 * 1177 * <p>Invoking the {@link #reset} method will set the scanner's locale to 1178 * the <a href= "#initial-locale">initial locale</a>. 1179 * 1180 * @param locale A string specifying the locale to use 1181 * @return this scanner 1182 */ 1183 public Scanner useLocale(Locale locale) { 1184 if (locale.equals(this.locale)) 1185 return this; 1186 1187 modCount++; 1188 this.locale = locale; 1189 DecimalFormat df = 1190 (DecimalFormat)NumberFormat.getNumberInstance(locale); 1191 DecimalFormatSymbols dfs = DecimalFormatSymbols.getInstance(locale); 1192 1193 // These must be literalized to avoid collision with regex 1194 // metacharacters such as dot or parenthesis 1195 groupSeparator = "\\" + dfs.getGroupingSeparator(); 1196 decimalSeparator = "\\" + dfs.getDecimalSeparator(); 1197 1198 // Quoting the nonzero length locale-specific things 1199 // to avoid potential conflict with metacharacters 1200 nanString = "\\Q" + dfs.getNaN() + "\\E"; 1201 infinityString = "\\Q" + dfs.getInfinity() + "\\E"; 1202 positivePrefix = df.getPositivePrefix(); 1203 if (positivePrefix.length() > 0) 1204 positivePrefix = "\\Q" + positivePrefix + "\\E"; 1205 negativePrefix = df.getNegativePrefix(); 1206 if (negativePrefix.length() > 0) 1207 negativePrefix = "\\Q" + negativePrefix + "\\E"; 1208 positiveSuffix = df.getPositiveSuffix(); 1209 if (positiveSuffix.length() > 0) 1210 positiveSuffix = "\\Q" + positiveSuffix + "\\E"; 1211 negativeSuffix = df.getNegativeSuffix(); | 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package java.util; 27 28 import java.io.*; 29 import java.math.*; 30 import java.nio.*; 31 import java.nio.channels.*; 32 import java.nio.charset.*; 33 import java.nio.file.Path; 34 import java.nio.file.Files; 35 import java.text.*; 36 import java.text.spi.NumberFormatProvider; 37 import java.util.function.Consumer; 38 import java.util.regex.*; 39 import java.util.stream.Stream; 40 import java.util.stream.StreamSupport; 41 import sun.util.locale.provider.LocaleProviderAdapter; 42 import sun.util.locale.provider.ResourceBundleBasedAdapter; 43 44 /** 45 * A simple text scanner which can parse primitive types and strings using 46 * regular expressions. 47 * 48 * <p>A {@code Scanner} breaks its input into tokens using a 49 * delimiter pattern, which by default matches whitespace. The resulting 50 * tokens may then be converted into values of different types using the 51 * various {@code next} methods. 52 * 53 * <p>For example, this code allows a user to read a number from 54 * {@code System.in}: 55 * <blockquote><pre>{@code 56 * Scanner sc = new Scanner(System.in); 57 * int i = sc.nextInt(); 58 * }</pre></blockquote> 59 * 60 * <p>As another example, this code allows {@code long} types to be 61 * assigned from entries in a file {@code myNumbers}: 62 * <blockquote><pre>{@code 1172 1173 /** 1174 * Sets this scanner's locale to the specified locale. 1175 * 1176 * <p>A scanner's locale affects many elements of its default 1177 * primitive matching regular expressions; see 1178 * <a href= "#localized-numbers">localized numbers</a> above. 1179 * 1180 * <p>Invoking the {@link #reset} method will set the scanner's locale to 1181 * the <a href= "#initial-locale">initial locale</a>. 1182 * 1183 * @param locale A string specifying the locale to use 1184 * @return this scanner 1185 */ 1186 public Scanner useLocale(Locale locale) { 1187 if (locale.equals(this.locale)) 1188 return this; 1189 1190 modCount++; 1191 this.locale = locale; 1192 1193 DecimalFormat df = null; 1194 NumberFormat nf = NumberFormat.getNumberInstance(locale); 1195 DecimalFormatSymbols dfs = DecimalFormatSymbols.getInstance(locale); 1196 if (nf instanceof DecimalFormat) { 1197 df = (DecimalFormat) nf; 1198 } else { 1199 1200 // In case where NumberFormat.getNumberInstance() returns 1201 // other instance (non DecimalFormat) based on the provider 1202 // used and java.text.spi.NumberFormatProvider implementations, 1203 // DecimalFormat constructor is used to obtain the instance 1204 LocaleProviderAdapter adapter = LocaleProviderAdapter 1205 .getAdapter(NumberFormatProvider.class, locale); 1206 if (!(adapter instanceof ResourceBundleBasedAdapter)) { 1207 adapter = LocaleProviderAdapter.getResourceBundleBased(); 1208 } 1209 String[] all = adapter.getLocaleResources(locale) 1210 .getNumberPatterns(); 1211 df = new DecimalFormat(all[0], dfs); 1212 } 1213 1214 // These must be literalized to avoid collision with regex 1215 // metacharacters such as dot or parenthesis 1216 groupSeparator = "\\" + dfs.getGroupingSeparator(); 1217 decimalSeparator = "\\" + dfs.getDecimalSeparator(); 1218 1219 // Quoting the nonzero length locale-specific things 1220 // to avoid potential conflict with metacharacters 1221 nanString = "\\Q" + dfs.getNaN() + "\\E"; 1222 infinityString = "\\Q" + dfs.getInfinity() + "\\E"; 1223 positivePrefix = df.getPositivePrefix(); 1224 if (positivePrefix.length() > 0) 1225 positivePrefix = "\\Q" + positivePrefix + "\\E"; 1226 negativePrefix = df.getNegativePrefix(); 1227 if (negativePrefix.length() > 0) 1228 negativePrefix = "\\Q" + negativePrefix + "\\E"; 1229 positiveSuffix = df.getPositiveSuffix(); 1230 if (positiveSuffix.length() > 0) 1231 positiveSuffix = "\\Q" + positiveSuffix + "\\E"; 1232 negativeSuffix = df.getNegativeSuffix(); |