author | joehw |
Wed, 04 Oct 2017 10:54:18 -0700 | |
changeset 47312 | d4f959806fe9 |
parent 47216 | 71c04702a3d5 |
child 48409 | 5ab69533994b |
permissions | -rw-r--r-- |
6 | 1 |
/* |
47312 | 2 |
* Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. |
3 |
* @LastModified: Sep 2017 |
|
6 | 4 |
*/ |
5 |
/* |
|
44797
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
6 |
* Licensed to the Apache Software Foundation (ASF) under one or more |
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
7 |
* contributor license agreements. See the NOTICE file distributed with |
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
8 |
* this work for additional information regarding copyright ownership. |
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
9 |
* The ASF licenses this file to You under the Apache License, Version 2.0 |
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
10 |
* (the "License"); you may not use this file except in compliance with |
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
11 |
* the License. You may obtain a copy of the License at |
6 | 12 |
* |
13 |
* http://www.apache.org/licenses/LICENSE-2.0 |
|
14 |
* |
|
15 |
* Unless required by applicable law or agreed to in writing, software |
|
16 |
* distributed under the License is distributed on an "AS IS" BASIS, |
|
17 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
18 |
* See the License for the specific language governing permissions and |
|
19 |
* limitations under the License. |
|
20 |
*/ |
|
21 |
||
22 |
package com.sun.org.apache.xerces.internal.jaxp.validation; |
|
23 |
||
24 |
import java.util.Locale; |
|
25 |
import java.util.MissingResourceException; |
|
26 |
import java.util.ResourceBundle; |
|
47312 | 27 |
import jdk.xml.internal.SecuritySupport; |
6 | 28 |
|
29 |
/** |
|
30 |
* <p>Used to format JAXP Validation API error messages using a specified locale.</p> |
|
31 |
* |
|
32 |
* @author Michael Glavassevich, IBM |
|
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 = |
|
16953 | 58 |
SecuritySupport.getResourceBundle("com.sun.org.apache.xerces.internal.impl.msg.JAXPValidationMessages", locale); |
6 | 59 |
} |
60 |
else { |
|
61 |
resourceBundle = |
|
16953 | 62 |
SecuritySupport.getResourceBundle("com.sun.org.apache.xerces.internal.impl.msg.JAXPValidationMessages"); |
6 | 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 |
} |