| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| ResourceHelper |
|
| 1.625;1.625 |
| 1 | /* | |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one or more | |
| 3 | * contributor license agreements. See the NOTICE file distributed with | |
| 4 | * this work for additional information regarding copyright ownership. | |
| 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 | |
| 6 | * (the "License"); you may not use this file except in compliance with | |
| 7 | * the License. You may obtain a copy of the License at | |
| 8 | * | |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 | |
| 10 | * | |
| 11 | * Unless required by applicable law or agreed to in writing, software | |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, | |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 14 | * See the License for the specific language governing permissions and | |
| 15 | * limitations under the License. | |
| 16 | */ | |
| 17 | package org.apache.commons.cli2.resource; | |
| 18 | ||
| 19 | import java.text.MessageFormat; | |
| 20 | ||
| 21 | import java.util.Locale; | |
| 22 | import java.util.MissingResourceException; | |
| 23 | import java.util.ResourceBundle; | |
| 24 | ||
| 25 | /** | |
| 26 | * A utility class used to provide internationalisation support. | |
| 27 | * | |
| 28 | * @author John Keyes | |
| 29 | */ | |
| 30 | public class ResourceHelper { | |
| 31 | /** system property */ | |
| 32 | private static final String PROP_LOCALE = "org.apache.commons.cli2.resource.bundle"; | |
| 33 | ||
| 34 | /** default package name */ | |
| 35 | private static final String DEFAULT_BUNDLE = | |
| 36 | "org.apache.commons.cli2.resource.CLIMessageBundle_en_US"; | |
| 37 | private static ResourceHelper helper; | |
| 38 | ||
| 39 | /** resource bundle */ | |
| 40 | private ResourceBundle bundle; | |
| 41 | ||
| 42 | private String prop; | |
| 43 | ||
| 44 | /** | |
| 45 | * Create a new ResourceHelper for the current locale. | |
| 46 | */ | |
| 47 | 145 | private ResourceHelper() { |
| 48 | 145 | String bundleName = System.getProperty(PROP_LOCALE); |
| 49 | ||
| 50 | 145 | if (bundleName == null) { |
| 51 | 142 | bundleName = DEFAULT_BUNDLE; |
| 52 | } | |
| 53 | ||
| 54 | 145 | this.prop = bundleName; |
| 55 | ||
| 56 | 145 | int firstUnderscore = bundleName.indexOf('_'); |
| 57 | 145 | int secondUnderscore = bundleName.indexOf('_', firstUnderscore + 1); |
| 58 | ||
| 59 | Locale locale; | |
| 60 | 145 | if (firstUnderscore != -1) { |
| 61 | 143 | String language = bundleName.substring(firstUnderscore + 1, secondUnderscore); |
| 62 | 143 | String country = bundleName.substring(secondUnderscore + 1); |
| 63 | 143 | locale = new Locale(language, country); |
| 64 | 143 | } |
| 65 | else { | |
| 66 | 2 | locale = Locale.getDefault(); |
| 67 | } | |
| 68 | // initialize the bundle | |
| 69 | try { | |
| 70 | 145 | bundle = ResourceBundle.getBundle(bundleName, locale); |
| 71 | 2 | } catch (MissingResourceException exp) { |
| 72 | 2 | bundle = ResourceBundle.getBundle(DEFAULT_BUNDLE, locale); |
| 73 | 143 | } |
| 74 | 145 | } |
| 75 | ||
| 76 | public String getBundleName() { | |
| 77 | 369 | return this.prop; |
| 78 | } | |
| 79 | ||
| 80 | /** | |
| 81 | * Gets the ResourceHelper appropriate to the current locale. | |
| 82 | * @return a ResourceHelper | |
| 83 | */ | |
| 84 | public static ResourceHelper getResourceHelper() { | |
| 85 | 370 | String bundleName = System.getProperty(PROP_LOCALE); |
| 86 | 370 | if (helper == null || !helper.getBundleName().equals(bundleName)) { |
| 87 | 145 | helper = new ResourceHelper(); |
| 88 | } | |
| 89 | ||
| 90 | 370 | return helper; |
| 91 | } | |
| 92 | ||
| 93 | /** | |
| 94 | * Returns the message for the specified key. | |
| 95 | * | |
| 96 | * @param key the unique identifier of the message | |
| 97 | * @return String the formatted String | |
| 98 | */ | |
| 99 | public String getMessage(final String key) { | |
| 100 | 125 | return getMessage(key, new Object[] { }); |
| 101 | } | |
| 102 | ||
| 103 | /** | |
| 104 | * Returns the message for the specified key and argument. | |
| 105 | * | |
| 106 | * @param key the unique identifier of the message | |
| 107 | * @param value the argument value | |
| 108 | * @return String the formatted String | |
| 109 | */ | |
| 110 | public String getMessage(final String key, | |
| 111 | final Object value) { | |
| 112 | 43 | return getMessage(key, new Object[] { value }); |
| 113 | } | |
| 114 | ||
| 115 | /** | |
| 116 | * Returns the message for the specified key and arguments. | |
| 117 | * | |
| 118 | * @param key the unique identifier of the message | |
| 119 | * @param value1 an argument value | |
| 120 | * @param value2 an argument value | |
| 121 | * @return String the formatted String | |
| 122 | */ | |
| 123 | public String getMessage(final String key, | |
| 124 | final Object value1, | |
| 125 | final Object value2) { | |
| 126 | 3 | return getMessage(key, new Object[] { value1, value2 }); |
| 127 | } | |
| 128 | ||
| 129 | /** | |
| 130 | * Returns the message for the specified key and arguments. | |
| 131 | * | |
| 132 | * @param key the unique identifier of the message | |
| 133 | * @param value1 an argument value | |
| 134 | * @param value2 an argument value | |
| 135 | * @param value3 an argument value | |
| 136 | * | |
| 137 | * @return String the formatted String | |
| 138 | */ | |
| 139 | public String getMessage(final String key, | |
| 140 | final Object value1, | |
| 141 | final Object value2, | |
| 142 | final Object value3) { | |
| 143 | 1 | return getMessage(key, new Object[] { value1, value2, value3 }); |
| 144 | } | |
| 145 | ||
| 146 | /** | |
| 147 | * Returns the message for the specified key and arguments. | |
| 148 | * | |
| 149 | * @param key the unique identifier of the message | |
| 150 | * @param values argument values | |
| 151 | * @return String the formatted String | |
| 152 | */ | |
| 153 | public String getMessage(final String key, | |
| 154 | final Object[] values) { | |
| 155 | 182 | final String msgFormatStr = bundle.getString(key); |
| 156 | 182 | final MessageFormat msgFormat = new MessageFormat(msgFormatStr); |
| 157 | ||
| 158 | 182 | return msgFormat.format(values); |
| 159 | } | |
| 160 | } |