001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 018/* 019 * THE FILE HAS BEEN AUTOGENERATED BY MSGTOOL TOOL. 020 * All changes made to this file manually will be overwritten 021 * if this tool runs again. Better make changes in the template file. 022 */ 023 024package org.apache.commons.compress.harmony.archive.internal.nls; 025 026import java.security.AccessController; 027import java.security.PrivilegedAction; 028import java.util.Locale; 029import java.util.MissingResourceException; 030import java.util.ResourceBundle; 031 032//import org.apache.commons.compress.harmony.kernel.vm.VM; 033 034/** 035 * This class retrieves strings from a resource bundle and returns them, formatting them with MessageFormat when 036 * required. 037 * <p> 038 * It is used by the system classes to provide national language support, by looking up messages in the <code> 039 * org.apache.commons.compress.harmony.archive.internal.nls.messages 040 * </code> resource bundle. Note that if this file is not available, or an invalid key is looked up, or resource bundle 041 * support is not available, the key itself will be returned as the associated message. This means that the <em>KEY</em> 042 * should a reasonable human-readable (english) string. 043 * 044 */ 045public class Messages { 046 047 // ResourceBundle holding the system messages. 048 static private ResourceBundle bundle = null; 049 050 /** 051 * Retrieves a message which has no arguments. 052 * 053 * @param msg String the key to look up. 054 * @return String the message for that key in the system message bundle. 055 */ 056 static public String getString(final String msg) { 057 if (bundle == null) { 058 return msg; 059 } 060 try { 061 return bundle.getString(msg); 062 } catch (final MissingResourceException e) { 063 return "Missing message: " + msg; //$NON-NLS-1$ 064 } 065 } 066 067 /** 068 * Retrieves a message which takes 1 argument. 069 * 070 * @param msg String the key to look up. 071 * @param arg Object the object to insert in the formatted output. 072 * @return String the message for that key in the system message bundle. 073 */ 074 static public String getString(final String msg, final Object arg) { 075 return getString(msg, new Object[] {arg}); 076 } 077 078 /** 079 * Retrieves a message which takes 1 integer argument. 080 * 081 * @param msg String the key to look up. 082 * @param arg int the integer to insert in the formatted output. 083 * @return String the message for that key in the system message bundle. 084 */ 085 static public String getString(final String msg, final int arg) { 086 return getString(msg, new Object[] {Integer.toString(arg)}); 087 } 088 089 /** 090 * Retrieves a message which takes 1 character argument. 091 * 092 * @param msg String the key to look up. 093 * @param arg char the character to insert in the formatted output. 094 * @return String the message for that key in the system message bundle. 095 */ 096 static public String getString(final String msg, final char arg) { 097 return getString(msg, new Object[] {String.valueOf(arg)}); 098 } 099 100 /** 101 * Retrieves a message which takes 2 arguments. 102 * 103 * @param msg String the key to look up. 104 * @param arg1 Object an object to insert in the formatted output. 105 * @param arg2 Object another object to insert in the formatted output. 106 * @return String the message for that key in the system message bundle. 107 */ 108 static public String getString(final String msg, final Object arg1, final Object arg2) { 109 return getString(msg, new Object[] {arg1, arg2}); 110 } 111 112 /** 113 * Retrieves a message which takes several arguments. 114 * 115 * @param msg String the key to look up. 116 * @param args Object[] the objects to insert in the formatted output. 117 * @return String the message for that key in the system message bundle. 118 */ 119 static public String getString(final String msg, final Object[] args) { 120 String format = msg; 121 122 if (bundle != null) { 123 try { 124 format = bundle.getString(msg); 125 } catch (final MissingResourceException e) { 126 // ignore 127 } 128 } 129 130 return format(format, args); 131 } 132 133 /** 134 * Generates a formatted text string given a source string containing "argument markers" of the form "{argNum}" 135 * where each argNum must be in the range 0..9. The result is generated by inserting the toString of each argument 136 * into the position indicated in the string. 137 * <p> 138 * To insert the "{" character into the output, use a single backslash character to escape it (i.e. "\{"). The "}" 139 * character does not need to be escaped. 140 * 141 * @param format String the format to use when printing. 142 * @param args Object[] the arguments to use. 143 * @return String the formatted message. 144 */ 145 public static String format(final String format, final Object[] args) { 146 final StringBuilder answer = new StringBuilder(format.length() + (args.length * 20)); 147 final String[] argStrings = new String[args.length]; 148 for (int i = 0; i < args.length; ++i) { 149 if (args[i] == null) { 150 argStrings[i] = "<null>"; //$NON-NLS-1$ 151 } else { 152 argStrings[i] = args[i].toString(); 153 } 154 } 155 int lastI = 0; 156 for (int i = format.indexOf('{', 0); i >= 0; i = format.indexOf('{', lastI)) { 157 if (i != 0 && format.charAt(i - 1) == '\\') { 158 // It's escaped, just print and loop. 159 if (i != 1) { 160 answer.append(format.substring(lastI, i - 1)); 161 } 162 answer.append('{'); 163 lastI = i + 1; 164 } else // It's a format character. 165 if (i > format.length() - 3) { 166 // Bad format, just print and loop. 167 answer.append(format.substring(lastI)); 168 lastI = format.length(); 169 } else { 170 final int argnum = (byte) Character.digit(format.charAt(i + 1), 10); 171 if (argnum < 0 || format.charAt(i + 2) != '}') { 172 // Bad format, just print and loop. 173 answer.append(format.substring(lastI, i + 1)); 174 lastI = i + 1; 175 } else { 176 // Got a good one! 177 answer.append(format.substring(lastI, i)); 178 if (argnum >= argStrings.length) { 179 answer.append("<missing argument>"); //$NON-NLS-1$ 180 } else { 181 answer.append(argStrings[argnum]); 182 } 183 lastI = i + 3; 184 } 185 } 186 } 187 if (lastI < format.length()) { 188 answer.append(format.substring(lastI)); 189 } 190 return answer.toString(); 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 static public 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((PrivilegedAction<Object>) () -> ResourceBundle 205 .getBundle(resource, locale, loader != null ? loader : ClassLoader.getSystemClassLoader())); 206 } catch (final MissingResourceException e) { 207 // ignore 208 } 209 return null; 210 } 211 212 static { 213 // Attempt to load the messages. 214 try { 215 bundle = setLocale(Locale.getDefault(), 216 "org.apache.commons.compress.harmony.archive.internal.nls.messages"); //$NON-NLS-1$ 217 } catch (final Throwable e) { 218 e.printStackTrace(); 219 } 220 } 221}