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.Arrays; 029import java.util.Locale; 030import java.util.MissingResourceException; 031import java.util.Objects; 032import java.util.ResourceBundle; 033 034//import org.apache.commons.compress.harmony.kernel.vm.VM; 035 036/** 037 * This class retrieves strings from a resource bundle and returns them, formatting them with MessageFormat when 038 * required. 039 * <p> 040 * It is used by the system classes to provide national language support, by looking up messages in the <code> 041 * org.apache.commons.compress.harmony.archive.internal.nls.messages 042 * </code> resource bundle. Note that if this file is not available, or an invalid key is looked up, or resource bundle 043 * support is not available, the key itself will be returned as the associated message. This means that the <em>KEY</em> 044 * should a reasonable human-readable (english) string. 045 * 046 */ 047public class Messages { 048 049 // ResourceBundle holding the system messages. 050 static private ResourceBundle bundle = null; 051 052 /** 053 * Retrieves a message which has no arguments. 054 * 055 * @param msg String the key to look up. 056 * @return String the message for that key in the system message bundle. 057 */ 058 static public String getString(final String msg) { 059 if (bundle == null) { 060 return msg; 061 } 062 try { 063 return bundle.getString(msg); 064 } catch (final MissingResourceException e) { 065 return "Missing message: " + msg; //$NON-NLS-1$ 066 } 067 } 068 069 /** 070 * Retrieves a message which takes 1 argument. 071 * 072 * @param msg String the key to look up. 073 * @param arg Object the object to insert in the formatted output. 074 * @return String the message for that key in the system message bundle. 075 */ 076 static public String getString(final String msg, final Object arg) { 077 return getString(msg, new Object[] {arg}); 078 } 079 080 /** 081 * Retrieves a message which takes 1 integer argument. 082 * 083 * @param msg String the key to look up. 084 * @param arg int the integer to insert in the formatted output. 085 * @return String the message for that key in the system message bundle. 086 */ 087 static public String getString(final String msg, final int arg) { 088 return getString(msg, new Object[] {Integer.toString(arg)}); 089 } 090 091 /** 092 * Retrieves a message which takes 1 character argument. 093 * 094 * @param msg String the key to look up. 095 * @param arg char the character to insert in the formatted output. 096 * @return String the message for that key in the system message bundle. 097 */ 098 static public String getString(final String msg, final char arg) { 099 return getString(msg, new Object[] {String.valueOf(arg)}); 100 } 101 102 /** 103 * Retrieves a message which takes 2 arguments. 104 * 105 * @param msg String the key to look up. 106 * @param arg1 Object an object to insert in the formatted output. 107 * @param arg2 Object another object to insert in the formatted output. 108 * @return String the message for that key in the system message bundle. 109 */ 110 static public String getString(final String msg, final Object arg1, final Object arg2) { 111 return getString(msg, new Object[] {arg1, arg2}); 112 } 113 114 /** 115 * Retrieves a message which takes several arguments. 116 * 117 * @param msg String the key to look up. 118 * @param args Object[] the objects to insert in the formatted output. 119 * @return String the message for that key in the system message bundle. 120 */ 121 static public String getString(final String msg, final Object[] args) { 122 String format = msg; 123 124 if (bundle != null) { 125 try { 126 format = bundle.getString(msg); 127 } catch (final MissingResourceException e) { 128 // ignore 129 } 130 } 131 132 return format(format, args); 133 } 134 135 /** 136 * Generates a formatted text string given a source string containing "argument markers" of the form "{argNum}" 137 * where each argNum must be in the range 0..9. The result is generated by inserting the toString of each argument 138 * into the position indicated in the string. 139 * <p> 140 * To insert the "{" character into the output, use a single backslash character to escape it (i.e. "\{"). The "}" 141 * character does not need to be escaped. 142 * 143 * @param format String the format to use when printing. 144 * @param args Object[] the arguments to use. 145 * @return String the formatted message. 146 */ 147 public static String format(final String format, final Object[] args) { 148 final StringBuilder answer = new StringBuilder(format.length() + (args.length * 20)); 149 final String[] argStrings = new String[args.length]; 150 Arrays.setAll(argStrings, i -> Objects.toString(args[i], "<null>")); //$NON-NLS-1$ 151 int lastI = 0; 152 for (int i = format.indexOf('{', 0); i >= 0; i = format.indexOf('{', lastI)) { 153 if (i != 0 && format.charAt(i - 1) == '\\') { 154 // It's escaped, just print and loop. 155 if (i != 1) { 156 answer.append(format.substring(lastI, i - 1)); 157 } 158 answer.append('{'); 159 lastI = i + 1; 160 } else // It's a format character. 161 if (i > format.length() - 3) { 162 // Bad format, just print and loop. 163 answer.append(format.substring(lastI)); 164 lastI = format.length(); 165 } else { 166 final int argnum = (byte) Character.digit(format.charAt(i + 1), 10); 167 if (argnum < 0 || format.charAt(i + 2) != '}') { 168 // Bad format, just print and loop. 169 answer.append(format.substring(lastI, i + 1)); 170 lastI = i + 1; 171 } else { 172 // Got a good one! 173 answer.append(format.substring(lastI, i)); 174 if (argnum >= argStrings.length) { 175 answer.append("<missing argument>"); //$NON-NLS-1$ 176 } else { 177 answer.append(argStrings[argnum]); 178 } 179 lastI = i + 3; 180 } 181 } 182 } 183 if (lastI < format.length()) { 184 answer.append(format.substring(lastI)); 185 } 186 return answer.toString(); 187 } 188 189 /** 190 * Changes the locale of the messages. 191 * 192 * @param locale Locale the locale to change to. 193 * @param resource resource name. 194 * @return The ResourceBundle. 195 */ 196 static public ResourceBundle setLocale(final Locale locale, final String resource) { 197 try { 198 // VM.bootCallerClassLoader() returns null 199 final ClassLoader loader = null;// VM.bootCallerClassLoader(); 200 return (ResourceBundle) AccessController.doPrivileged((PrivilegedAction<Object>) () -> ResourceBundle 201 .getBundle(resource, locale, loader != null ? loader : ClassLoader.getSystemClassLoader())); 202 } catch (final MissingResourceException e) { 203 // ignore 204 } 205 return null; 206 } 207 208 static { 209 // Attempt to load the messages. 210 try { 211 bundle = setLocale(Locale.getDefault(), 212 "org.apache.commons.compress.harmony.archive.internal.nls.messages"); //$NON-NLS-1$ 213 } catch (final Throwable e) { 214 e.printStackTrace(); 215 } 216 } 217}