Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
MessageUtil |
|
| 0.0;0 |
1 | /* | |
2 | * Copyright 2004-2008 the Seasar Foundation and the Others. | |
3 | * | |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); | |
5 | * you may not use this file except in compliance with the License. | |
6 | * You may obtain a copy of the License at | |
7 | * | |
8 | * http://www.apache.org/licenses/LICENSE-2.0 | |
9 | * | |
10 | * Unless required by applicable law or agreed to in writing, software | |
11 | * distributed under the License is distributed on an "AS IS" BASIS, | |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, | |
13 | * either express or implied. See the License for the specific language | |
14 | * governing permissions and limitations under the License. | |
15 | */ | |
16 | package org.seasar.uruma.util; | |
17 | ||
18 | import java.text.MessageFormat; | |
19 | ||
20 | import org.seasar.framework.exception.ResourceNotFoundRuntimeException; | |
21 | import org.seasar.framework.message.MessageFormatter; | |
22 | import org.seasar.framework.message.MessageResourceBundle; | |
23 | import org.seasar.framework.message.MessageResourceBundleFactory; | |
24 | import org.seasar.uruma.core.UrumaConstants; | |
25 | import org.seasar.uruma.core.UrumaMessageCodes; | |
26 | import org.seasar.uruma.exception.NotFoundException; | |
27 | ||
28 | /** | |
29 | * Uruma のユーザアプリケーションから使用できるメッセージリソースのためのユーティリティクラスです。<br /> | |
30 | * | |
31 | * @author y-komori | |
32 | */ | |
33 | public class MessageUtil { | |
34 | 0 | private MessageUtil() { |
35 | ||
36 | 0 | } |
37 | ||
38 | /** | |
39 | * メッセージをデフォルトのリソースバンドルから取得して返します。<br /> | |
40 | * デフォルトのリソースバンドル名は {@link UrumaConstants#USER_MESSAGE_BASE} が使用されます。 | |
41 | * | |
42 | * @param key | |
43 | * キー | |
44 | * @return メッセージ | |
45 | * @see UrumaConstants#USER_MESSAGE_BASE | |
46 | */ | |
47 | public static String getMessage(final String key) { | |
48 | 4 | return getMessage(key, (Object[]) null); |
49 | } | |
50 | ||
51 | /** | |
52 | * メッセージをデフォルトのリソースバンドルから取得して返します。<br /> | |
53 | * デフォルトのリソースバンドル名は {@link UrumaConstants#USER_MESSAGE_BASE} が使用されます。 | |
54 | * | |
55 | * @param key | |
56 | * キー | |
57 | * @param args | |
58 | * メッセージ引数 | |
59 | * @return メッセージ | |
60 | */ | |
61 | public static String getMessage(final String key, final Object... args) { | |
62 | 8 | return getMessageWithBundleName(UrumaConstants.USER_MESSAGE_BASE, key, |
63 | args); | |
64 | } | |
65 | ||
66 | /** | |
67 | * メッセージを指定したリソースバンドルから取得して返します。<br /> | |
68 | * | |
69 | * @param bundleName | |
70 | * リソースバンドル名称 | |
71 | * @param key | |
72 | * キー | |
73 | * @return メッセージ | |
74 | */ | |
75 | public static String getMessageWithBundleName(final String bundleName, | |
76 | final String key) { | |
77 | 8 | return getMessageWithBundleName(bundleName, key, (Object[]) null); |
78 | } | |
79 | ||
80 | /** | |
81 | * メッセージを指定したリソースバンドルから取得して返します。<br /> | |
82 | * | |
83 | * @param bundleName | |
84 | * リソースバンドル名称 | |
85 | * @param key | |
86 | * キー | |
87 | * @param args | |
88 | * メッセージ引数 | |
89 | * @return メッセージ | |
90 | * @throws NotFoundException | |
91 | * リソースバンドルが見つからなかった場合 | |
92 | */ | |
93 | public static String getMessageWithBundleName(final String bundleName, | |
94 | final String key, final Object... args) { | |
95 | try { | |
96 | ||
97 | 24 | MessageResourceBundle bundle = MessageResourceBundleFactory |
98 | .getBundle(bundleName); | |
99 | 20 | String message = bundle.get(key); |
100 | 20 | if (message != null) { |
101 | 16 | return MessageFormat.format(message, args); |
102 | } else { | |
103 | 4 | return MessageFormatter.getSimpleMessage( |
104 | UrumaMessageCodes.MESSAGE_KEY_NOT_FOUND, new Object[] { | |
105 | bundleName, key }); | |
106 | } | |
107 | 4 | } catch (ResourceNotFoundRuntimeException ex) { |
108 | 4 | throw new NotFoundException( |
109 | UrumaMessageCodes.MESSAGE_RESOURCE_NOT_FOUND, bundleName); | |
110 | } | |
111 | } | |
112 | } |