1 /*
2 * Copyright (c) 2012, 2019, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
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 sun.util.locale.provider;
27
28 import java.lang.reflect.InvocationTargetException;
29 import java.lang.reflect.Method;
30 import java.text.DecimalFormat;
31 import java.util.spi.LocaleServiceProvider;
32
33 /**
34 * LocaleProviderAdapter implementation for the host locale data.
35 * Currently it is only implemented on Windows Vista or later.
36 *
37 * @author Naoto Sato
38 */
39 public class HostLocaleProviderAdapter extends AuxLocaleProviderAdapter {
40
41 /**
42 * Returns the type of this LocaleProviderAdapter
43 */
44 @Override
45 public LocaleProviderAdapter.Type getAdapterType() {
46 return LocaleProviderAdapter.Type.HOST;
47 }
48
49 @Override
50 @SuppressWarnings("unchecked")
51 protected <P extends LocaleServiceProvider> P findInstalledProvider(final Class<P> c) {
52 try {
53 Method getter = HostLocaleProviderAdapterImpl.class.getMethod(
54 "get" + c.getSimpleName(), (Class<?>[]) null);
55 return (P)getter.invoke(null, (Object[]) null);
56 } catch (NoSuchMethodException |
57 IllegalAccessException |
58 IllegalArgumentException |
59 InvocationTargetException ex) {
60 LocaleServiceProviderPool.config(HostLocaleProviderAdapter.class, ex.toString());
61 }
62 return null;
63 }
64
65 /**
66 * Utility to make the decimal format specific to integer, called
67 * by the platform dependent adapter implementations.
68 *
69 * @param df A DecimalFormat object
70 * @return The same DecimalFormat object in the argument, modified
71 * to allow integer formatting/parsing only.
72 */
73 static DecimalFormat makeIntegerFormatter(DecimalFormat df) {
74 df.setMaximumFractionDigits(0);
75 df.setDecimalSeparatorAlwaysShown(false);
76 df.setParseIntegerOnly(true);
77 return df;
78 }
79 }
|
1 /*
2 * Copyright (c) 2012, 2020, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
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 sun.util.locale.provider;
27
28 import java.lang.reflect.InvocationTargetException;
29 import java.text.DecimalFormat;
30 import java.util.ServiceConfigurationError;
31 import java.util.spi.LocaleServiceProvider;
32
33 /**
34 * LocaleProviderAdapter implementation for the host locale data.
35 * Currently it is only implemented on Windows Vista or later.
36 *
37 * @author Naoto Sato
38 */
39 public class HostLocaleProviderAdapter extends AuxLocaleProviderAdapter {
40
41 /**
42 * Returns the type of this LocaleProviderAdapter
43 */
44 @Override
45 public LocaleProviderAdapter.Type getAdapterType() {
46 return LocaleProviderAdapter.Type.HOST;
47 }
48
49 @Override
50 @SuppressWarnings("unchecked")
51 protected <P extends LocaleServiceProvider> P findInstalledProvider(final Class<P> c) {
52 try {
53 return (P)Class.forName(
54 "sun.util.locale.provider.HostLocaleProviderAdapterImpl")
55 .getMethod("get" + c.getSimpleName(), (Class<?>[]) null)
56 .invoke(null, (Object[]) null);
57 } catch (ClassNotFoundException |
58 NoSuchMethodException ex) {
59 // permissible exceptions as platform may not support host adapter
60 return null;
61 } catch (IllegalAccessException |
62 IllegalArgumentException |
63 InvocationTargetException ex) {
64 throw new ServiceConfigurationError(
65 "Host locale provider cannot be located.", ex);
66 }
67 }
68
69 /**
70 * Utility to make the decimal format specific to integer, called
71 * by the platform dependent adapter implementations.
72 *
73 * @param df A DecimalFormat object
74 * @return The same DecimalFormat object in the argument, modified
75 * to allow integer formatting/parsing only.
76 */
77 static DecimalFormat makeIntegerFormatter(DecimalFormat df) {
78 df.setMaximumFractionDigits(0);
79 df.setDecimalSeparatorAlwaysShown(false);
80 df.setParseIntegerOnly(true);
81 return df;
82 }
83 }
|