Clover.NET coverage report - Coverage for s2container.net

Coverage timestamp: 2006年5月30日 11:21:29

File Stats: LOC: 132   Methods: 9
NCLOC: 92 Classes: 1
 
Source File Conditionals Statements Methods TOTAL
Seasar.Framework.Message\MessageFormatter.cs 66.7% 90.0% 88.9% 84.3%
coverage coverage
1   #region Copyright
2   /*
3   * Copyright 2005 the Seasar Foundation and the Others.
4   *
5   * Licensed under the Apache License, Version 2.0 (the "License");
6   * you may not use this file except in compliance with the License.
7   * You may obtain a copy of the License at
8   *
9   * http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
14   * either express or implied. See the License for the specific language
15   * governing permissions and limitations under the License.
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   /// <summary>
28   /// メッセージコードと引数をプロパティに登録されている
29   /// パターンに適用し、メッセージを組み立てます。
30   /// メッセージコードは、8桁で構成され最初の1桁がメッセージの種別で、
31   /// E:エラー、W:ワーニング、I:インフォメーションで構成されます。
32   /// 次の3桁がシステム名でSeasarの場合は、SSRになります。
33   /// 最後の4桁は連番です。
34   /// メッセージ定義ファイルは、システム名 + Messages.resourcesになります。
35   /// SSRMessages.ja-JP.resourcesなどを用意することで他言語に対応できます。
36   /// </summary>
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