用XML将机器内码转换为人们容易理解的信息

http://www.itjxue.com  2015-08-07 20:39  来源:未知  点击次数: 

  程序中通常会有一些错误代码或标识,为了程序中方便这些东西通常不会使用汉字,经常在程序中用的是一些FileError或数字。还有就是在编码中经常使用的枚举标识对象的状态。而通常这些信息会直接的或间接的现实给用户,可用户需要到的是容易理解的汉字描述。以前要么将这些标识和枚举的转换规则硬编码到程序中,要么就直接提示给用户。前者没有很好的扩展性,而后者则让用户一头雾水。现可以用流行的XML(配置文件)保存提示信息,然后用一个对象将机器中的内码转换为人们容易理解的信息。

  转换对象如下:

   /**//// 
   /// 翻译类,将内部码翻译成容易理解的中文
   /// 
   /// 
   /// 根据配置文件中的信息,将系统内部码(错误码、成功码)翻译成中文(或人容易理解的语言)。
   /// 
   public static class Translation
   ...{
   private static System.IO.FileSystemWatcher watcher;
   private static XmlDocument content;
   private static string configFile;
   private static object locker = new object();
  
   /**//// 
   /// 加载配置文件
   /// 
   /// 
   public static void Configure(string configFile)
   ...{
   LoadFile(configFile);
   if (watcher != null)
   ...{
   watcher.Dispose();
   }
   watcher = new FileSystemWatcher(Path.GetDirectoryName(configFile), Path.GetFileName(configFile));
   watcher.Changed += new FileSystemEventHandler(watcher_Changed);
   }
  
   /**//// 
   /// 加载默认配置文件
   /// 
   public static void Configure()
   ...{
   if (System.Web.HttpContext.Current != null)
   ...{
   Configure(System.Web.HttpContext.Current.Server.MapPath("~/translation.config"));
   }
   else
   ...{
   Configure(System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "\" + "translation.config");
   }
   }
  
   /**//// 
   /// 加载文件内容
   /// 
   /// 
   private static void LoadFile(string configFile)
   ...{
   lock (locker)
   ...{
   XmlDocument doc = new XmlDocument();
   doc.Load(configFile);
  
   content = doc;
   Translation.configFile = configFile;
   }
   }
  
   /**//// 
   /// 当文件变更时,从新加载文件
   /// 
   /// 
   /// 
   private static void watcher_Changed(object sender, FileSystemEventArgs e)
   ...{
   LoadFile(configFile);
   }
  
   /**//// 
   /// 获取Enum的解释,如果Enum有Flag标记,则使用逗号分隔各个解释
   /// 
   /// 
   /// 
   public static string GetEnumDescription(Enum enumValue)
   ...{
   return GetEnumDescription(enumValue, ",");
   }
  
   /**//// 
   /// 获取Enum的解释,如果Enum有Flag标记,则使用sparator分隔各个解释
   /// 
   /// 
   /// 
   /// 
   public static string GetEnumDescription(Enum enumValue, string sparator)
   ...{
   Type type = enumValue.GetType();
  
   //检查类型是否有Flags特性
   object[] attrs = type.GetCustomAttributes(typeof(FlagsAttribute), false);
   if (attrs.Length > 0)
   ...{
   StringBuilder builder = new StringBuilder();
   Array arr = Enum.GetValues(type);
   foreach (Enum enu in arr) //循环获取每一个值的解释
   ...{
   if ((Convert.ToUInt64(enumValue) & Convert.ToUInt64(enu)) == Convert.ToUInt64(enu)) //判断是否有这个值
   ...{
   builder.Append(GetEnumDes(type, enu.ToString()));
   builder.Append(sparator);
   }
   }
   if (builder.Length != 0) //拿掉最后的分隔符
   builder.Remove(builder.Length - sparator.Length, sparator.Length);
   return builder.ToString();
   }
   else
   ...{
   return GetEnumDes(type, enumValue.ToString());
   }
   }
  
   /**//// 
   /// 获取某一Enum类型值的解释
   /// 
   /// 
   /// 
   /// 
   private static string GetEnumDes(Type type, string value)
   ...{
   string xquery = "/translation/enum/" + type.FullName + "/" + value;
   XmlNode node = content.SelectSingleNode(xquery);
   if (node != null)
   return node.InnerText;
   else
   return value;
   }
  
   /**//// 
   /// 翻译指定值
   /// 
   /// 
   /// 
   public static string GetValueDescription(object obj)
   ...{
   return GetValueDescription("default", obj);
   }
  
   /**//// 
   /// 在指定组中翻译指定值
   /// 
   /// 
   /// 
   /// 
   public static string GetValueDescription(string group, object obj)
   ...{
   if (obj == null)
   return "null";
  
   string xquery = "/translation/description[@group='" + group + "']/add[@key='" + obj.ToString() + "']/@value";
   XmlNode node = content.SelectSingleNode(xquery);
   if (node == null)
   return obj.ToString();
   else
   return node.Value;
   }
   }

(责任编辑:IT教学网)

更多