Use XML to convert machine inner code into information that people can understand

Source: Internet
Author: User
Tags xquery
There are usually some error codes or identifiers in the program. for convenience in the program, Chinese characters are usually not used, and some FileError or numbers are often used in the program. In addition, enumeration is often used in encoding to identify the object state. Generally, this information is directly or indirectly provided to users. what users need is the easy-to-understand description of Chinese characters. In the past, there were usually some error codes or identifiers in this program. for convenience in the program, Chinese characters are usually not used, and some FileError or numbers are often used in the program. In addition, enumeration is often used in encoding to identify the object state. Generally, this information is directly or indirectly provided to users. what users need is the easy-to-understand description of Chinese characters. Previously
There are usually some error codes or identifiers in the program. for convenience in the program, Chinese characters are usually not used, and some FileError or numbers are often used in the program. In addition, enumeration is often used in encoding to identify the object state. Generally, this information is directly or indirectly provided to users. what users need is the easy-to-understand description of Chinese characters. Previously, the conversion rules for these identifiers and enumerations were either hardcoded into the program or prompted to the user directly. The former has no good scalability, while the latter makes users confused. Now you can use popular XML (configuration file) to save the prompt information, and then use an object to convert the inner code in the machine into easy-to-understand information.
The conversion object is as follows:
  

/** // Translation class, which translates internal codes into easy-to-understand Chinese characters // according to the information in the configuration file, translate the system internal code (error code, success Code) into Chinese (or a language that is easy to understand ). /// PublicstaticclassTranslation... {privatestaticSystem. IO. fileSystemWatcherwatcher; privatestaticXmlDocumentcontent; privatestaticstringconfigFile; metadata = newobject ();/*** // load the configuration file //// publicstaticvoidConfigure (stringconfigFile )... {LoadFile (configFile); if (watcher! = Null )... {watcher. dispose ();} watcher = newFileSystemWatcher (Path. getDirectoryName (configFile), Path. getFileName (configFile); watcher. changed = newFileSystemEventHandler (watcher_Changed);}/*** // load the default configuration file /// publicstaticvoidConfigure ()... {if (System. web. httpContext. current! = Null)... {Configure (System. Web. HttpContext. Current. Server. MapPath ("~ /Translation. config ");} else... {Configure (System. appDomain. currentDomain. setupInformation. applicationBase "\" "translation. config "); }}/** // load the file content /// privatestaticvoidLoadFile (stringconfigFile )... {lock (locker )... {XmlDocumentdoc = newXmlDocument (); doc. load (configFile); content = doc; Translation. configFile = configFile ;}}/** // when the file is changed, the file is loaded. // privatestaticvoidwatche R_Changed (objectsender, FileSystemEventArgse )... {LoadFile (configFile);}/** // Obtain the Enum explanation. if the Enum has a Flag, separate the explanations with commas // publicstaticstringGetEnumDescription (EnumenumValue )... {returnGetEnumDescription (enumValue, ",");}/** // Obtain the Enum explanation. if Enum has a Flag, then use sparator to separate the interpretations //////// publicstaticstringGetEnumDescription (EnumenumValue, stringsparator )... {Typetype = enumValue. getType (); // Check whether the Flags feature object [] attrs = type. getCustomAttributes (typeof (flagsattrites), false); if (attrs. length> 0 )... {StringBuilderbuilder = newStringBuilder (); Arrayarr = Enum. getValues (type); foreach (Enumenuinarr) // get the explanation of each value in a loop... {if (Convert. toUInt64 (enumValue) & Convert. toUInt64 (enu) = Convert. toUInt64 (enu) // determine whether this value exists... {builder. append (GetEnumDes (type, enu. toString (); builder. append (sparator );}} If (builder. Length! = 0) // remove the last separator builder. remove (builder. length-sparator.Length, sparator. length); returnbuilder. toString ();} else... {returnGetEnumDes (type, enumValue. toString ());}} /** // Obtain the description of an Enum value // privatestaticstringGetEnumDes (Typetype, stringvalue )... {stringxquery = "/translation/enum/" type. fullName "/" value; XmlNodenode = content. selectSingleNode (xquery); if (node! = Null) returnnode. innerText; else returnvalue;}/*** // Translation of the specified value // publicstaticstringGetValueDescription (objectobj )... {returnGetValueDescription ("default", obj );} /** // translate the specified value in the specified group // publicstaticstringGetValueDescription (stringgroup, objectobj )... {if (obj = null) return "null"; stringxquery = "/translation/description [@ group = '" group "']/add [@ key = '" obj. toString () "']/@ value"; XmlNodenode = content. selectSingleNode (xquery); if (node = null) returnobj. toString (); else returnnode. value ;}# p #

Before using this object, you must use the Configure method to load the xml configuration file. the default configuration file name is translation. config. The conversion object uses the Singleton mode. a FileSystemWatcher object is used to monitor the XML file. if the XML file changes, it is loaded again. The XML query uses an XPath expression.
Then, you can use the GetEnumDescription and GetValueDescription methods to translate enumeration and identification. If no value can be translated is found, the return value of the ToString method of the object is returned.
Sample XML configuration:

  
  
   
    
    
    
     
Unknown
    Post-paid users 
    
     
Prepaid users
     
    
   
     
      
        
    
   
  
 

The above section describes how to use XML to convert machine inner code into information case studies that people can understand. For more information, see other related articles in the first PHP community!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.