1 /*
   2  * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
   3  */
   4 /*
   5  * Licensed to the Apache Software Foundation (ASF) under one or more
   6  * contributor license agreements.  See the NOTICE file distributed with
   7  * this work for additional information regarding copyright ownership.
   8  * The ASF licenses this file to You under the Apache License, Version 2.0
   9  * (the "License"); you may not use this file except in compliance with
  10  * the License.  You may obtain a copy of the License at
  11  *
  12  *      http://www.apache.org/licenses/LICENSE-2.0
  13  *
  14  * Unless required by applicable law or agreed to in writing, software
  15  * distributed under the License is distributed on an "AS IS" BASIS,
  16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17  * See the License for the specific language governing permissions and
  18  * limitations under the License.
  19  */
  20 
  21 package com.sun.org.apache.xerces.internal.jaxp.validation;
  22 
  23 import java.util.Locale;
  24 import java.util.MissingResourceException;
  25 import java.util.ResourceBundle;
  26 import jdk.xml.internal.SecuritySupport;
  27 
  28 /**
  29  * <p>Used to format JAXP Validation API error messages using a specified locale.</p>
  30  *
  31  * @author Michael Glavassevich, IBM
  32  * @LastModified: Sep 2017
  33  */
  34 final class JAXPValidationMessageFormatter {
  35 
  36     /**
  37      * Formats a message with the specified arguments using the given
  38      * locale information.
  39      *
  40      * @param locale    The locale of the message.
  41      * @param key       The message key.
  42      * @param arguments The message replacement text arguments. The order
  43      *                  of the arguments must match that of the placeholders
  44      *                  in the actual message.
  45      *
  46      * @return          the formatted message.
  47      *
  48      * @throws MissingResourceException Thrown if the message with the
  49      *                                  specified key cannot be found.
  50      */
  51     public static String formatMessage(Locale locale,
  52         String key, Object[] arguments)
  53         throws MissingResourceException {
  54 
  55         ResourceBundle resourceBundle = null;
  56         if (locale != null) {
  57             resourceBundle =
  58                 SecuritySupport.getResourceBundle("com.sun.org.apache.xerces.internal.impl.msg.JAXPValidationMessages", locale);
  59         }
  60         else {
  61             resourceBundle =
  62                 SecuritySupport.getResourceBundle("com.sun.org.apache.xerces.internal.impl.msg.JAXPValidationMessages");
  63         }
  64 
  65         // format message
  66         String msg;
  67         try {
  68             msg = resourceBundle.getString(key);
  69             if (arguments != null) {
  70                 try {
  71                     msg = java.text.MessageFormat.format(msg, arguments);
  72                 }
  73                 catch (Exception e) {
  74                     msg = resourceBundle.getString("FormatFailed");
  75                     msg += " " + resourceBundle.getString(key);
  76                 }
  77             }
  78         }
  79 
  80         // error
  81         catch (MissingResourceException e) {
  82             msg = resourceBundle.getString("BadMessageKey");
  83             throw new MissingResourceException(key, msg, key);
  84         }
  85 
  86         // no message
  87         if (msg == null) {
  88             msg = key;
  89             if (arguments.length > 0) {
  90                 StringBuffer str = new StringBuffer(msg);
  91                 str.append('?');
  92                 for (int i = 0; i < arguments.length; i++) {
  93                     if (i > 0) {
  94                         str.append('&');
  95                     }
  96                     str.append(String.valueOf(arguments[i]));
  97                 }
  98             }
  99         }
 100         return msg;
 101     }
 102 }