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