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 required.
038 * <p>
039 * It is used by the system classes to provide national language support, by looking up messages in the {@code
040 *    org.apache.commons.compress.harmony.archive.internal.nls.messages
041 * } 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
042 * will be returned as the associated message. This means that the <em>KEY</em> should a reasonable human-readable (english) string.
043 */
044public class Messages {
045
046    // ResourceBundle holding the system messages.
047    private static ResourceBundle bundle;
048
049    static {
050        // Attempt to load the messages.
051        try {
052            bundle = setLocale(Locale.getDefault(), "org.apache.commons.compress.harmony.archive.internal.nls.messages"); //$NON-NLS-1$
053        } catch (final Throwable e) {
054            e.printStackTrace();
055        }
056    }
057
058    /**
059     * 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.
060     * The result is generated by inserting the toString of each argument into the position indicated in the string.
061     * <p>
062     * 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.
063     *
064     * @param format String the format to use when printing.
065     * @param args   Object[] the arguments to use.
066     * @return String the formatted message.
067     */
068    public static String format(final String format, final Object[] args) {
069        final StringBuilder answer = new StringBuilder(format.length() + args.length * 20);
070        final String[] argStrings = new String[args.length];
071        Arrays.setAll(argStrings, i -> Objects.toString(args[i], "<null>")); //$NON-NLS-1$
072        int lastI = 0;
073        for (int i = format.indexOf('{', 0); i >= 0; i = format.indexOf('{', lastI)) {
074            if (i != 0 && format.charAt(i - 1) == '\\') {
075                // It's escaped, just print and loop.
076                if (i != 1) {
077                    answer.append(format.substring(lastI, i - 1));
078                }
079                answer.append('{');
080                lastI = i + 1;
081            } else // It's a format character.
082            if (i > format.length() - 3) {
083                // Bad format, just print and loop.
084                answer.append(format.substring(lastI));
085                lastI = format.length();
086            } else {
087                final int argnum = (byte) Character.digit(format.charAt(i + 1), 10);
088                if (argnum < 0 || format.charAt(i + 2) != '}') {
089                    // Bad format, just print and loop.
090                    answer.append(format.substring(lastI, i + 1));
091                    lastI = i + 1;
092                } else {
093                    // Got a good one!
094                    answer.append(format.substring(lastI, i));
095                    if (argnum >= argStrings.length) {
096                        answer.append("<missing argument>"); //$NON-NLS-1$
097                    } else {
098                        answer.append(argStrings[argnum]);
099                    }
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 ignored) {
186                // ignored
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 ignored) {
207            // ignored
208        }
209        return null;
210    }
211}