Messages.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */

  17. /*
  18.  * THE FILE HAS BEEN AUTOGENERATED BY MSGTOOL TOOL.
  19.  * All changes made to this file manually will be overwritten
  20.  * if this tool runs again. Better make changes in the template file.
  21.  */

  22. package org.apache.commons.compress.harmony.archive.internal.nls;

  23. import java.security.AccessController;
  24. import java.security.PrivilegedAction;
  25. import java.util.Arrays;
  26. import java.util.Locale;
  27. import java.util.MissingResourceException;
  28. import java.util.Objects;
  29. import java.util.ResourceBundle;

  30. //import org.apache.commons.compress.harmony.kernel.vm.VM;

  31. /**
  32.  * This class retrieves strings from a resource bundle and returns them, formatting them with MessageFormat when required.
  33.  * <p>
  34.  * It is used by the system classes to provide national language support, by looking up messages in the {@code
  35.  *    org.apache.commons.compress.harmony.archive.internal.nls.messages
  36.  * } 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
  37.  * will be returned as the associated message. This means that the <em>KEY</em> should a reasonable human-readable (english) string.
  38.  */
  39. public class Messages {

  40.     // ResourceBundle holding the system messages.
  41.     private static ResourceBundle bundle;

  42.     static {
  43.         // Attempt to load the messages.
  44.         try {
  45.             bundle = setLocale(Locale.getDefault(), "org.apache.commons.compress.harmony.archive.internal.nls.messages"); //$NON-NLS-1$
  46.         } catch (final Throwable e) {
  47.             e.printStackTrace();
  48.         }
  49.     }

  50.     /**
  51.      * 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.
  52.      * The result is generated by inserting the toString of each argument into the position indicated in the string.
  53.      * <p>
  54.      * 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.
  55.      *
  56.      * @param format String the format to use when printing.
  57.      * @param args   Object[] the arguments to use.
  58.      * @return String the formatted message.
  59.      */
  60.     public static String format(final String format, final Object[] args) {
  61.         final StringBuilder answer = new StringBuilder(format.length() + args.length * 20);
  62.         final String[] argStrings = new String[args.length];
  63.         Arrays.setAll(argStrings, i -> Objects.toString(args[i], "<null>")); //$NON-NLS-1$
  64.         int lastI = 0;
  65.         for (int i = format.indexOf('{', 0); i >= 0; i = format.indexOf('{', lastI)) {
  66.             if (i != 0 && format.charAt(i - 1) == '\\') {
  67.                 // It's escaped, just print and loop.
  68.                 if (i != 1) {
  69.                     answer.append(format.substring(lastI, i - 1));
  70.                 }
  71.                 answer.append('{');
  72.                 lastI = i + 1;
  73.             } else // It's a format character.
  74.             if (i > format.length() - 3) {
  75.                 // Bad format, just print and loop.
  76.                 answer.append(format.substring(lastI));
  77.                 lastI = format.length();
  78.             } else {
  79.                 final int argnum = (byte) Character.digit(format.charAt(i + 1), 10);
  80.                 if (argnum < 0 || format.charAt(i + 2) != '}') {
  81.                     // Bad format, just print and loop.
  82.                     answer.append(format.substring(lastI, i + 1));
  83.                     lastI = i + 1;
  84.                 } else {
  85.                     // Got a good one!
  86.                     answer.append(format.substring(lastI, i));
  87.                     if (argnum >= argStrings.length) {
  88.                         answer.append("<missing argument>"); //$NON-NLS-1$
  89.                     } else {
  90.                         answer.append(argStrings[argnum]);
  91.                     }
  92.                     lastI = i + 3;
  93.                 }
  94.             }
  95.         }
  96.         if (lastI < format.length()) {
  97.             answer.append(format.substring(lastI));
  98.         }
  99.         return answer.toString();
  100.     }

  101.     /**
  102.      * Retrieves a message which has no arguments.
  103.      *
  104.      * @param msg String the key to look up.
  105.      * @return String the message for that key in the system message bundle.
  106.      */
  107.     public static String getString(final String msg) {
  108.         if (bundle == null) {
  109.             return msg;
  110.         }
  111.         try {
  112.             return bundle.getString(msg);
  113.         } catch (final MissingResourceException e) {
  114.             return "Missing message: " + msg; //$NON-NLS-1$
  115.         }
  116.     }

  117.     /**
  118.      * Retrieves a message which takes 1 character argument.
  119.      *
  120.      * @param msg String the key to look up.
  121.      * @param arg char the character to insert in the formatted output.
  122.      * @return String the message for that key in the system message bundle.
  123.      */
  124.     public static String getString(final String msg, final char arg) {
  125.         return getString(msg, new Object[] { String.valueOf(arg) });
  126.     }

  127.     /**
  128.      * Retrieves a message which takes 1 integer argument.
  129.      *
  130.      * @param msg String the key to look up.
  131.      * @param arg int the integer 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 int arg) {
  135.         return getString(msg, new Object[] { Integer.toString(arg) });
  136.     }

  137.     /**
  138.      * Retrieves a message which takes 1 argument.
  139.      *
  140.      * @param msg String the key to look up.
  141.      * @param arg Object the object to insert in the formatted output.
  142.      * @return String the message for that key in the system message bundle.
  143.      */
  144.     public static String getString(final String msg, final Object arg) {
  145.         return getString(msg, new Object[] { arg });
  146.     }

  147.     /**
  148.      * Retrieves a message which takes 2 arguments.
  149.      *
  150.      * @param msg  String the key to look up.
  151.      * @param arg1 Object an object to insert in the formatted output.
  152.      * @param arg2 Object another object to insert in the formatted output.
  153.      * @return String the message for that key in the system message bundle.
  154.      */
  155.     public static String getString(final String msg, final Object arg1, final Object arg2) {
  156.         return getString(msg, new Object[] { arg1, arg2 });
  157.     }

  158.     /**
  159.      * Retrieves a message which takes several arguments.
  160.      *
  161.      * @param msg  String the key to look up.
  162.      * @param args Object[] the objects to insert in the formatted output.
  163.      * @return String the message for that key in the system message bundle.
  164.      */
  165.     public static String getString(final String msg, final Object[] args) {
  166.         String format = msg;

  167.         if (bundle != null) {
  168.             try {
  169.                 format = bundle.getString(msg);
  170.             } catch (final MissingResourceException ignored) {
  171.                 // ignored
  172.             }
  173.         }

  174.         return format(format, args);
  175.     }

  176.     /**
  177.      * Changes the locale of the messages.
  178.      *
  179.      * @param locale   Locale the locale to change to.
  180.      * @param resource resource name.
  181.      * @return The ResourceBundle.
  182.      */
  183.     public static ResourceBundle setLocale(final Locale locale, final String resource) {
  184.         try {
  185.             // VM.bootCallerClassLoader() returns null
  186.             final ClassLoader loader = null; // VM.bootCallerClassLoader();
  187.             return (ResourceBundle) AccessController.doPrivileged(
  188.                     (PrivilegedAction<Object>) () -> ResourceBundle.getBundle(resource, locale, loader != null ? loader : ClassLoader.getSystemClassLoader()));
  189.         } catch (final MissingResourceException ignored) {
  190.             // ignored
  191.         }
  192.         return null;
  193.     }
  194. }