BaseCompareValidator.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /**
  2. * Namespace: System.Web.UI.WebControls
  3. * Class: BaseCompareValidator
  4. *
  5. * Author: Gaurav Vaish
  6. * Maintainer: [email protected]
  7. * Implementation: yes
  8. * Contact: <[email protected]>
  9. * Status: 30%
  10. *
  11. * (C) Gaurav Vaish (2001)
  12. */
  13. using System;
  14. using System.Collections;
  15. using System.Globalization;
  16. using System.Text;
  17. using System.Text.RegularExpressions;
  18. using System.Web;
  19. using System.Web.UI;
  20. namespace System.Web.UI.WebControls
  21. {
  22. public abstract class BaseCompareValidator: BaseValidator
  23. {
  24. protected BaseCompareValidator(): base()
  25. {
  26. }
  27. public static bool CanConvert(string text, ValidationDataType type)
  28. {
  29. object o = null;
  30. return Convert(text, type, out o);
  31. }
  32. public ValidationDataType Type
  33. {
  34. get
  35. {
  36. object o = ViewState["Type"];
  37. if(o!=null)
  38. return (ValidationDataType)o;
  39. return ValidationDataType.String;
  40. }
  41. set
  42. {
  43. if(!System.Enum.IsDefined(typeof(ValidationDataType), value))
  44. throw new ArgumentException();
  45. ViewState["Type"] = value;
  46. }
  47. }
  48. protected static int CutoffYear
  49. {
  50. get
  51. {
  52. return DateTimeFormatInfo.CurrentInfo.Calendar.TwoDigitYearMax;
  53. }
  54. }
  55. protected static int GetFullYear(int shortYear)
  56. {
  57. int century = DateTime.Today.Year - (DateTime.Today.Year % 100);
  58. if(century < CutoffYear)
  59. {
  60. return (shortYear + century);
  61. }
  62. return (shortYear + century - 100);
  63. }
  64. protected override void AddAttributesToRender(HtmlTextWriter writer)
  65. {
  66. /*
  67. ValidationDataType vdt;
  68. NumberFormatInfo nfi;
  69. DateTime dt;
  70. */
  71. base.AddAttributesToRender(writer);
  72. if(base.RenderUplevel)
  73. {
  74. // TODO: The Lost World
  75. }
  76. throw new NotImplementedException();
  77. }
  78. /// <summary>
  79. /// Undocumented
  80. /// </summary>
  81. protected static bool Compare(string leftText, string rightText, ValidationCompareOperator op, ValidationDataType type)
  82. {
  83. object left = null, right = null;
  84. if(!Convert(leftText, type, out left))
  85. {
  86. return false;
  87. }
  88. if(op == ValidationCompareOperator.DataTypeCheck)
  89. {
  90. return true;
  91. }
  92. if(!Convert(rightText, type, out right))
  93. {
  94. return true;
  95. }
  96. int compareResult;
  97. switch(type)
  98. {
  99. case ValidationDataType.String:
  100. compareResult = ((String)left).CompareTo(right);
  101. break;
  102. }
  103. throw new NotImplementedException();
  104. return false;
  105. }
  106. /// <summary>
  107. /// Undocumented
  108. /// </summary>
  109. protected static string GetDateElementOrder()
  110. {
  111. string pattern = DateTimeFormatInfo.CurrentInfo.ShortDatePattern;
  112. //TODO: What are the various possibilities?
  113. // I can think of only y*/M*/d*, d*/M*/y*, M*/d*/y*
  114. if(pattern.IndexOf('y') < pattern.IndexOf('M'))
  115. {
  116. return "ymd";
  117. }
  118. if(pattern.IndexOf('M') < pattern.IndexOf('d'))
  119. {
  120. return "mdy";
  121. }
  122. return "dmy";
  123. }
  124. // Uncodumented
  125. protected static bool Convert(string text, ValidationDataType type, out object convertedValue)
  126. {
  127. //throw new NotImplementedException();
  128. convertedValue = null;
  129. try
  130. {
  131. switch(type)
  132. {
  133. case ValidationDataType.String: convertedValue = text;
  134. break;
  135. case ValidationDataType.Integer: convertedValue = Int32.Parse(text, CultureInfo.InvariantCulture);
  136. break;
  137. case ValidationDataType.Double:
  138. Match matchDouble = Regex.Match(text, @"^\s*([-\+])?(\d+)?(\"
  139. + NumberFormatInfo.CurrentInfo.NumberDecimalSeparator
  140. + @"(\d+))?\s*$");
  141. if(matchDouble.Success)
  142. {
  143. string sign = (matchDouble.Groups[1].Success ? matchDouble.Groups[1].Value : "+");
  144. string decPart = (matchDouble.Groups[2].Success ? matchDouble.Groups[2].Value : "0");
  145. string mantissa = (matchDouble.Groups[4].Success ? matchDouble.Groups[4].Value : "0");
  146. convertedValue = Double.Parse(sign + decPart + "." + mantissa, CultureInfo.InvariantCulture);
  147. }
  148. break;
  149. case ValidationDataType.Date:
  150. if(DateTimeFormatInfo.CurrentInfo.Calendar.GetType() != typeof(GregorianCalendar))
  151. {
  152. convertedValue = DateTime.Parse(text);
  153. break;
  154. }
  155. string order = GetDateElementOrder();
  156. int date = 0, mth = 0, year = 0;
  157. Match matchDate = Regex.Match(text, @"^\s*((\d{4})|(\d{2}))([\.\/-])(\d{1,2})\4(\d{1,2})\s*$");
  158. if(matchDate.Success && order == "ymd")
  159. {
  160. date = Int32.Parse(matchDate.Groups[6].Value, CultureInfo.InvariantCulture);
  161. mth = Int32.Parse(matchDate.Groups[5].Value, CultureInfo.InvariantCulture);
  162. year = Int32.Parse((matchDate.Groups[2].Success ? matchDate.Groups[2].Value : matchDate.Groups[3].Value), CultureInfo.InvariantCulture);
  163. } else
  164. {
  165. matchDate = Regex.Match(text, @"^\s*(\d{1,2})([\.\/-])(\d{1,2})\2((\d{4}|\d{2}))\s*$");
  166. if(matchDate.Success)
  167. {
  168. if(order == "dmy")
  169. {
  170. date = Int32.Parse(matchDate.Groups[1].Value, CultureInfo.InvariantCulture);
  171. mth = Int32.Parse(matchDate.Groups[3].Value, CultureInfo.InvariantCulture);
  172. year = Int32.Parse((matchDate.Groups[5].Success ? matchDate.Groups[5].Value : matchDate.Groups[6].Value), CultureInfo.InvariantCulture);
  173. }
  174. if(order == "mdy")
  175. {
  176. date = Int32.Parse(matchDate.Groups[3].Value, CultureInfo.InvariantCulture);
  177. mth = Int32.Parse(matchDate.Groups[1].Value, CultureInfo.InvariantCulture);
  178. year = Int32.Parse((matchDate.Groups[5].Success ? matchDate.Groups[5].Value : matchDate.Groups[6].Value), CultureInfo.InvariantCulture);
  179. }
  180. }
  181. }
  182. year = (year < 100 ? GetFullYear(year) : year);
  183. if(matchDate.Success && date!=0 && mth!=0 && year!=0)
  184. {
  185. convertedValue = new DateTime(year, mth, date);
  186. }
  187. break;
  188. case ValidationDataType.Currency:
  189. string decSep = NumberFormatInfo.CurrentInfo.CurrencyDecimalSeparator;
  190. string grpSep = NumberFormatInfo.CurrentInfo.CurrencyGroupSeparator;
  191. int decDig = NumberFormatInfo.CurrentInfo.CurrencyDecimalDigits;
  192. if(grpSep[0] > 0xA0)
  193. {
  194. grpSep = " ";
  195. }
  196. string[] patternArray = new string[5];
  197. patternArray[0] = "^\\s*([-\\+])?(((\\d+)\\";
  198. patternArray[1] = grpSep;
  199. patternArray[2] = @")*)(\d+)";
  200. if(decDig > 0)
  201. {
  202. string[] decPattern = new string[5];
  203. decPattern[0] = "(\\";
  204. decPattern[1] = decSep;
  205. decPattern[2] = @"(\d{1,";
  206. decPattern[3] = decDig.ToString(NumberFormatInfo.InvariantInfo);
  207. decPattern[4] = @"}))";
  208. patternArray[3] = String.Concat(decPattern);
  209. } else
  210. {
  211. patternArray[3] = String.Empty;
  212. }
  213. patternArray[4] = @"?\s*$";
  214. Match matchCurrency = Regex.Match(text, String.Concat(patternArray));
  215. if(matchCurrency.Success)
  216. {
  217. StringBuilder sb = new StringBuilder();
  218. sb.Append(matchCurrency.Groups[1]);
  219. CaptureCollection cc = matchCurrency.Groups[4].Captures;
  220. foreach(IEnumerable current in cc)
  221. {
  222. sb.Append((Capture)current);
  223. }
  224. sb.Append(matchCurrency.Groups[5]);
  225. if(decDig > 0)
  226. {
  227. sb.Append(".");
  228. sb.Append(matchCurrency.Groups[7]);
  229. }
  230. convertedValue = Decimal.Parse(sb.ToString(), CultureInfo.InvariantCulture);
  231. }
  232. break;
  233. }
  234. } catch(Exception e)
  235. {
  236. convertedValue = null;
  237. }
  238. return (convertedValue != null);
  239. }
  240. }
  241. }