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.xml.internal.res; 22 23 import java.util.Locale; 24 import java.util.ResourceBundle; 25 import jdk.xml.internal.SecuritySupport; 26 27 /** 28 * A utility class for issuing XML error messages. 29 * @xsl.usage internal 30 * @LastModified: Sep 2017 31 */ 32 public class XMLMessages 33 { 34 35 /** The local object to use. */ 36 protected Locale fLocale = Locale.getDefault(); 37 38 /** The language specific resource object for XML messages. */ 39 private static ResourceBundle XMLBundle = null; 40 41 /** The class name of the XML error message string table. */ 42 private static final String XML_ERROR_RESOURCES = 43 "com.sun.org.apache.xml.internal.res.XMLErrorResources"; 44 45 /** String to use if a bad message code is used. */ 46 protected static final String BAD_CODE = "BAD_CODE"; 47 48 /** String to use if the message format operation failed. */ 49 protected static final String FORMAT_FAILED = "FORMAT_FAILED"; 50 51 /** 52 * Set the Locale object to use. 53 * 54 * @param locale non-null reference to Locale object. 55 */ 56 public void setLocale(Locale locale) 57 { 58 fLocale = locale; 59 } 60 61 /** 62 * Get the Locale object that is being used. 63 * 64 * @return non-null reference to Locale object. 65 */ 66 public Locale getLocale() 67 { 68 return fLocale; 69 } 70 71 /** 72 * Creates a message from the specified key and replacement 73 * arguments, localized to the given locale. 74 * 75 * @param msgKey The key for the message text. 76 * @param args The arguments to be used as replacement text 77 * in the message created. 78 * 79 * @return The formatted message string. 80 */ 81 public static final String createXMLMessage(String msgKey, Object args[]) 82 { 83 if (XMLBundle == null) { 84 XMLBundle = SecuritySupport.getResourceBundle(XML_ERROR_RESOURCES); 85 } 86 87 if (XMLBundle != null) 88 { 89 return createMsg(XMLBundle, msgKey, args); 90 } 91 else 92 return "Could not load any resource bundles."; 93 } 94 95 /** 96 * Creates a message from the specified key and replacement 97 * arguments, localized to the given locale. 98 * 99 * @param fResourceBundle The resource bundle to use. 100 * @param msgKey The message key to use. 101 * @param args The arguments to be used as replacement text 102 * in the message created. 103 * 104 * @return The formatted message string. 105 */ 106 public static final String createMsg(ResourceBundle fResourceBundle, 107 String msgKey, Object args[]) //throws Exception 108 { 109 110 String fmsg = null; 111 boolean throwex = false; 112 String msg = null; 113 114 if (msgKey != null) 115 msg = fResourceBundle.getString(msgKey); 116 117 if (msg == null) 118 { 119 msg = fResourceBundle.getString(BAD_CODE); 120 throwex = true; 121 } 122 123 if (args != null) 124 { 125 try 126 { 127 128 // Do this to keep format from crying. 129 // This is better than making a bunch of conditional 130 // code all over the place. 131 int n = args.length; 132 133 for (int i = 0; i < n; i++) 134 { 135 if (null == args[i]) 136 args[i] = ""; 137 } 138 139 fmsg = java.text.MessageFormat.format(msg, args); 140 } 141 catch (Exception e) 142 { 143 fmsg = fResourceBundle.getString(FORMAT_FAILED); 144 fmsg += " " + msg; 145 } 146 } 147 else 148 fmsg = msg; 149 150 if (throwex) 151 { 152 throw new RuntimeException(fmsg); 153 } 154 155 return fmsg; 156 } 157 158 }