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 18 /* 19 * THE FILE HAS BEEN AUTOGENERATED BY MSGTOOL TOOL. 20 * All changes made to this file manually will be overwritten 21 * if this tool runs again. Better make changes in the template file. 22 */ 23 24 package org.apache.commons.compress.harmony.archive.internal.nls; 25 26 import java.security.AccessController; 27 import java.security.PrivilegedAction; 28 import java.util.Arrays; 29 import java.util.Locale; 30 import java.util.MissingResourceException; 31 import java.util.Objects; 32 import java.util.ResourceBundle; 33 34 //import org.apache.commons.compress.harmony.kernel.vm.VM; 35 36 /** 37 * This class retrieves strings from a resource bundle and returns them, formatting them with MessageFormat when required. 38 * <p> 39 * It is used by the system classes to provide national language support, by looking up messages in the <code> 40 * org.apache.commons.compress.harmony.archive.internal.nls.messages 41 * </code> resource bundle. Note that if this file is not available, or an invalid key is looked up, or resource bundle support is not available, the key itself 42 * will be returned as the associated message. This means that the <em>KEY</em> should a reasonable human-readable (english) string. 43 */ 44 public class Messages { 45 46 // ResourceBundle holding the system messages. 47 private static ResourceBundle bundle; 48 49 static { 50 // Attempt to load the messages. 51 try { 52 bundle = setLocale(Locale.getDefault(), "org.apache.commons.compress.harmony.archive.internal.nls.messages"); //$NON-NLS-1$ 53 } catch (final Throwable e) { 54 e.printStackTrace(); 55 } 56 } 57 58 /** 59 * Generates a formatted text string given a source string containing "argument markers" of the form "{argNum}" where each argNum must be in the range 0..9. 60 * The result is generated by inserting the toString of each argument into the position indicated in the string. 61 * <p> 62 * To insert the "{" character into the output, use a single backslash character to escape it (i.e. "\{"). The "}" character does not need to be escaped. 63 * 64 * @param format String the format to use when printing. 65 * @param args Object[] the arguments to use. 66 * @return String the formatted message. 67 */ 68 public static String format(final String format, final Object[] args) { 69 final StringBuilder answer = new StringBuilder(format.length() + args.length * 20); 70 final String[] argStrings = new String[args.length]; 71 Arrays.setAll(argStrings, i -> Objects.toString(args[i], "<null>")); //$NON-NLS-1$ 72 int lastI = 0; 73 for (int i = format.indexOf('{', 0); i >= 0; i = format.indexOf('{', lastI)) { 74 if (i != 0 && format.charAt(i - 1) == '\\') { 75 // It's escaped, just print and loop. 76 if (i != 1) { 77 answer.append(format.substring(lastI, i - 1)); 78 } 79 answer.append('{'); 80 lastI = i + 1; 81 } else // It's a format character. 82 if (i > format.length() - 3) { 83 // Bad format, just print and loop. 84 answer.append(format.substring(lastI)); 85 lastI = format.length(); 86 } else { 87 final int argnum = (byte) Character.digit(format.charAt(i + 1), 10); 88 if (argnum < 0 || format.charAt(i + 2) != '}') { 89 // Bad format, just print and loop. 90 answer.append(format.substring(lastI, i + 1)); 91 lastI = i + 1; 92 } else { 93 // Got a good one! 94 answer.append(format.substring(lastI, i)); 95 if (argnum >= argStrings.length) { 96 answer.append("<missing argument>"); //$NON-NLS-1$ 97 } else { 98 answer.append(argStrings[argnum]); 99 } 100 lastI = i + 3; 101 } 102 } 103 } 104 if (lastI < format.length()) { 105 answer.append(format.substring(lastI)); 106 } 107 return answer.toString(); 108 } 109 110 /** 111 * Retrieves a message which has no arguments. 112 * 113 * @param msg String the key to look up. 114 * @return String the message for that key in the system message bundle. 115 */ 116 public static String getString(final String msg) { 117 if (bundle == null) { 118 return msg; 119 } 120 try { 121 return bundle.getString(msg); 122 } catch (final MissingResourceException e) { 123 return "Missing message: " + msg; //$NON-NLS-1$ 124 } 125 } 126 127 /** 128 * Retrieves a message which takes 1 character argument. 129 * 130 * @param msg String the key to look up. 131 * @param arg char the character to insert in the formatted output. 132 * @return String the message for that key in the system message bundle. 133 */ 134 public static String getString(final String msg, final char arg) { 135 return getString(msg, new Object[] { String.valueOf(arg) }); 136 } 137 138 /** 139 * Retrieves a message which takes 1 integer argument. 140 * 141 * @param msg String the key to look up. 142 * @param arg int the integer to insert in the formatted output. 143 * @return String the message for that key in the system message bundle. 144 */ 145 public static String getString(final String msg, final int arg) { 146 return getString(msg, new Object[] { Integer.toString(arg) }); 147 } 148 149 /** 150 * Retrieves a message which takes 1 argument. 151 * 152 * @param msg String the key to look up. 153 * @param arg Object the object to insert in the formatted output. 154 * @return String the message for that key in the system message bundle. 155 */ 156 public static String getString(final String msg, final Object arg) { 157 return getString(msg, new Object[] { arg }); 158 } 159 160 /** 161 * Retrieves a message which takes 2 arguments. 162 * 163 * @param msg String the key to look up. 164 * @param arg1 Object an object to insert in the formatted output. 165 * @param arg2 Object another object to insert in the formatted output. 166 * @return String the message for that key in the system message bundle. 167 */ 168 public static String getString(final String msg, final Object arg1, final Object arg2) { 169 return getString(msg, new Object[] { arg1, arg2 }); 170 } 171 172 /** 173 * Retrieves a message which takes several arguments. 174 * 175 * @param msg String the key to look up. 176 * @param args Object[] the objects to insert in the formatted output. 177 * @return String the message for that key in the system message bundle. 178 */ 179 public static String getString(final String msg, final Object[] args) { 180 String format = msg; 181 182 if (bundle != null) { 183 try { 184 format = bundle.getString(msg); 185 } catch (final MissingResourceException e) { 186 // ignore 187 } 188 } 189 190 return format(format, args); 191 } 192 193 /** 194 * Changes the locale of the messages. 195 * 196 * @param locale Locale the locale to change to. 197 * @param resource resource name. 198 * @return The ResourceBundle. 199 */ 200 public static ResourceBundle setLocale(final Locale locale, final String resource) { 201 try { 202 // VM.bootCallerClassLoader() returns null 203 final ClassLoader loader = null; // VM.bootCallerClassLoader(); 204 return (ResourceBundle) AccessController.doPrivileged( 205 (PrivilegedAction<Object>) () -> ResourceBundle.getBundle(resource, locale, loader != null ? loader : ClassLoader.getSystemClassLoader())); 206 } catch (final MissingResourceException e) { 207 // ignore 208 } 209 return null; 210 } 211 }