Email Encoding

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.IO;  
  4. using System.Linq;  
  5. using System.Text.RegularExpressions;  
  6.   
  7. namespace Program  
  8. {  
  9.     public class EmailEncoding  
  10.     {  
  11.         private static Dictionary<stringstring> m_keyValue = new Dictionary<stringstring>();  
  12.   
  13.         static void Main(string[] args)  
  14.         {  
  15.             try  
  16.             {  
  17.                 string INPUT_FILE = @"input.txt";  
  18.                 string OUTPUT_FILE = @"output.txt";  
  19.   
  20.                 if (!File.Exists(INPUT_FILE))  
  21.                 {  
  22.                     File.Create(INPUT_FILE).Close();  
  23.                     return;  
  24.                 }  
  25.   
  26.                 if (!File.Exists(OUTPUT_FILE))  
  27.                 {  
  28.                     File.Create(OUTPUT_FILE).Close();  
  29.                 }  
  30.   
  31.                 StreamReader streamReader = new StreamReader(INPUT_FILE);  
  32.                 using (streamReader)  
  33.                 {  
  34.                     using (StreamWriter streamWriter = new StreamWriter(OUTPUT_FILE, false))  
  35.                     {  
  36.                         string line;  
  37.                         while ((line = streamReader.ReadLine()) != null)  
  38.                         {  
  39.                             string output = string.Empty;  
  40.                             if (ValidateInput(line, out output))  
  41.                             {  
  42.                                 streamWriter.WriteLine(output);  
  43.                             }  
  44.                             else  
  45.                             {  
  46.                                 streamWriter.WriteLine("ERROR");  
  47.                             }  
  48.                         }  
  49.                     }  
  50.                 }  
  51.                 streamReader.Close();  
  52.                 streamReader.Dispose();  
  53.   
  54.             }  
  55.             catch (Exception)  
  56.             {  
  57.   
  58.             }  
  59.         }  
  60.   
  61.         public static bool ValidateInput(string input, out string modifiedValue)  
  62.         {  
  63.             modifiedValue = input;  
  64.             bool result = EmailValidate(input);  
  65.             if (!result)  
  66.             {  
  67.                 var match = ValidateKey(input);  
  68.                 if (match != null && match.Success)  
  69.                 {  
  70.                     modifiedValue = UpdateKeyValue(match.Value.ToString(), modifiedValue);  
  71.   
  72.                     while (true)  
  73.                     {  
  74.                         Match nextMatch = match.NextMatch();  
  75.                         if (nextMatch == null || !nextMatch.Success)  
  76.                             break;  
  77.                         modifiedValue = UpdateKeyValue(nextMatch.Value.ToString(), modifiedValue);  
  78.   
  79.                         match = nextMatch;  
  80.                     }  
  81.                     result = true;  
  82.                 }  
  83.   
  84.                 var keyMatch = ValidateReplaceKey(input);  
  85.   
  86.                 //if (keyMatch.Success && match.Success)  
  87.                 //    return false;  
  88.   
  89.                 if (keyMatch != null && keyMatch.Success)  
  90.                 {  
  91.                     modifiedValue = ReplaceKeyToValue(keyMatch.Value.ToString(), modifiedValue);  
  92.                     while (true)  
  93.                     {  
  94.                         Match nextMatch = keyMatch.NextMatch();  
  95.                         if (nextMatch == null || !nextMatch.Success)  
  96.                             break;  
  97.   
  98.                         modifiedValue = ReplaceKeyToValue(nextMatch.Value.ToString(), modifiedValue);  
  99.   
  100.                         keyMatch = nextMatch;  
  101.                     }  
  102.   
  103.                     result = true;  
  104.                 }  
  105.                 // After success, check whether modfied string is valid email id.  
  106.                 if (result && EmailValidate(modifiedValue))  
  107.                     return result;  
  108.                 else  
  109.                     result = false;  
  110.             }  
  111.             else  
  112.                 return true;  
  113.   
  114.             return result;  
  115.         }  
  116.   
  117.         private static string UpdateKeyValue(string matchValue, string originalItem)  
  118.         {  
  119.             string modifiedValue = originalItem;  
  120.             string captureValue = matchValue.Replace("{""").Replace("}""");  
  121.             string[] values = captureValue.Split(' ');  
  122.             if (values.Length > 1)  
  123.             {  
  124.                 if (m_keyValue.ContainsKey(values[0]))  
  125.                     m_keyValue[values[0]] = values[1];  
  126.                 else  
  127.                     m_keyValue.Add(values[0], values[1]);  
  128.             }  
  129.             modifiedValue = modifiedValue.Replace(matchValue, values[1]);  
  130.   
  131.             return modifiedValue;  
  132.         }  
  133.   
  134.         private static string ReplaceKeyToValue(string matchValue, string originalItem)  
  135.         {  
  136.             string modifiedValue = originalItem;  
  137.             string captureKey = matchValue.Replace("{""").Replace("}""");  
  138.             if (!m_keyValue.ContainsKey(captureKey))  
  139.                 m_keyValue.Add(captureKey, string.Empty);  
  140.             modifiedValue = modifiedValue.Replace(matchValue, m_keyValue[captureKey]);  
  141.   
  142.             return modifiedValue;  
  143.         }  
  144.   
  145.         private static bool EmailValidate(string line)  
  146.         {  
  147.             string pattern = @"^[a-z][a-z|0-9|]*?@[a-z][a-z|0-9|]*\.([a-z][a-z|0-9]*(\.[a-z][a-z|0-9]*)?)$";  
  148.   
  149.             Match match = Regex.Match(line.Trim(), pattern, RegexOptions.IgnoreCase);  
  150.             if (match.Success)  
  151.                 return true;  
  152.   
  153.             return false;  
  154.   
  155.         }  
  156.   
  157.         private static Match ValidateKey(string line)  
  158.         {  
  159.             string pattern = @"{([a-z|0-9|]+ [a-z|0-9|.|]+)}";  
  160.             Match match = Regex.Match(line.Trim(), pattern, RegexOptions.IgnoreCase);  
  161.             if (match.Success)  
  162.                 return match;  
  163.   
  164.             return null;  
  165.   
  166.         }  
  167.   
  168.         public static Match ValidateReplaceKey(string line)  
  169.         {  
  170.             string pattern = @"{([a-z|0-9|]+)}";  
  171.             Match match = Regex.Match(line.Trim(), pattern, RegexOptions.IgnoreCase);  
  172.             if (match.Success)  
  173.                 return match;  
  174.   
  175.             return null;  
  176.   
  177.         }  
  178.     }  
  179. }