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