1
|
|
#region Copyright
|
2
|
|
|
3
|
|
|
4
|
|
|
5
|
|
|
6
|
|
|
7
|
|
|
8
|
|
|
9
|
|
|
10
|
|
|
11
|
|
|
12
|
|
|
13
|
|
|
14
|
|
|
15
|
|
|
16
|
|
|
17
|
|
#endregion
|
18
|
|
|
19
|
|
using System;
|
20
|
|
using System.Collections;
|
21
|
|
using System.Reflection;
|
22
|
|
using System.Resources;
|
23
|
|
using System.Text;
|
24
|
|
|
25
|
|
namespace Seasar.Framework.Message
|
26
|
|
{
|
27
|
|
|
28
|
|
|
29
|
|
|
30
|
|
|
31
|
|
|
32
|
|
|
33
|
|
|
34
|
|
|
35
|
|
|
36
|
|
|
37
|
|
public sealed class MessageFormatter
|
38
|
|
{
|
39
|
|
private const string MESSAGES = "Messages";
|
40
|
|
private static readonly object[] EMPTY_ARRAY = new object[0];
|
41
|
|
private static Hashtable resourceManagers_ = new Hashtable();
|
42
|
|
|
43
|
0
|
private MessageFormatter()
|
44
|
|
{
|
45
|
|
}
|
46
|
|
|
47
|
3
|
public static string GetMessage(string messageCode,object[] args)
|
48
|
|
{
|
49
|
3
|
return GetMessage(messageCode, args, Assembly.GetExecutingAssembly());
|
50
|
|
}
|
51
|
|
|
52
|
4
|
public static string GetMessage(string messageCode,object[] args, Assembly assembly)
|
53
|
|
{
|
54
|
4
|
if(messageCode == null)
|
55
|
|
{
|
56
|
0
|
messageCode = "";
|
57
|
|
}
|
58
|
4
|
return "[" + messageCode + "] " + GetSimpleMessage(messageCode,args, assembly);
|
59
|
|
}
|
60
|
|
|
61
|
611
|
public static string GetSimpleMessage(string messageCode,object[] arguments)
|
62
|
|
{
|
63
|
611
|
return GetSimpleMessage(messageCode, arguments, Assembly.GetExecutingAssembly());
|
64
|
|
}
|
65
|
|
|
66
|
779
|
public static string GetSimpleMessage(string messageCode,object[] arguments, Assembly assembly)
|
67
|
|
{
|
68
|
779
|
try
|
69
|
|
{
|
70
|
779
|
string pattern = GetPattern(messageCode, assembly);
|
71
|
778
|
if(pattern != null)
|
72
|
|
{
|
73
|
778
|
if(arguments == null)
|
74
|
|
{
|
75
|
161
|
arguments = EMPTY_ARRAY;
|
76
|
|
}
|
77
|
778
|
return string.Format(pattern,arguments);
|
78
|
|
}
|
79
|
|
}
|
80
|
|
catch
|
81
|
|
{
|
82
|
|
}
|
83
|
1
|
return GetNoPatternMessage(arguments);
|
84
|
|
}
|
85
|
|
|
86
|
779
|
private static string GetPattern(string messageCode, Assembly assembly)
|
87
|
|
{
|
88
|
779
|
ResourceManager resourceManager = GetMessages(GetSystemName(messageCode), assembly);
|
89
|
779
|
if(resourceManager != null)
|
90
|
|
{
|
91
|
779
|
return resourceManager.GetString(messageCode);
|
92
|
|
}
|
93
|
0
|
return null;
|
94
|
|
}
|
95
|
|
|
96
|
|
|
97
|
779
|
private static string GetSystemName(string messageCode)
|
98
|
|
{
|
99
|
779
|
return messageCode.Substring(1,Math.Min(3,messageCode.Length));
|
100
|
|
}
|
101
|
|
|
102
|
779
|
private static ResourceManager GetMessages(string systemName, Assembly assembly)
|
103
|
|
{
|
104
|
779
|
string key = systemName + assembly.FullName;
|
105
|
779
|
if(resourceManagers_.ContainsKey(key))
|
106
|
|
{
|
107
|
776
|
return (ResourceManager) resourceManagers_[key];
|
108
|
|
}
|
109
|
|
else
|
110
|
|
{
|
111
|
3
|
ResourceManager rm = new ResourceManager(systemName + MESSAGES, assembly);
|
112
|
3
|
resourceManagers_[key] = rm;
|
113
|
3
|
return rm;
|
114
|
|
}
|
115
|
|
}
|
116
|
|
|
117
|
|
|
118
|
1
|
private static string GetNoPatternMessage(object[] args)
|
119
|
|
{
|
120
|
0
|
if(args == null || args.Length == 0) return "";
|
121
|
1
|
StringBuilder buffer = new StringBuilder();
|
122
|
1
|
foreach(object arg in args)
|
123
|
|
{
|
124
|
1
|
buffer.Append(arg + ", ");
|
125
|
|
}
|
126
|
1
|
buffer.Length = buffer.Length - 2;
|
127
|
1
|
return buffer.ToString();
|
128
|
|
}
|
129
|
|
|
130
|
|
}
|
131
|
|
}
|
132
|
|
|