2
0

BaseCompareValidator.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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: 100%
  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. using System.ComponentModel;
  21. namespace System.Web.UI.WebControls
  22. {
  23. public abstract class BaseCompareValidator: BaseValidator
  24. {
  25. protected BaseCompareValidator(): base()
  26. {
  27. }
  28. public static bool CanConvert(string text, ValidationDataType type)
  29. {
  30. object o = null;
  31. return Convert(text, type, out o);
  32. }
  33. [DefaultValue(ValidationDataType.String)]
  34. [WebCategory("Behaviour")]
  35. [WebSysDescription("RangeValidator_Type")]
  36. public ValidationDataType Type
  37. {
  38. get
  39. {
  40. object o = ViewState["Type"];
  41. if(o!=null)
  42. return (ValidationDataType)o;
  43. return ValidationDataType.String;
  44. }
  45. set
  46. {
  47. if(!System.Enum.IsDefined(typeof(ValidationDataType), value))
  48. throw new ArgumentException();
  49. ViewState["Type"] = value;
  50. }
  51. }
  52. protected static int CutoffYear
  53. {
  54. get
  55. {
  56. return DateTimeFormatInfo.CurrentInfo.Calendar.TwoDigitYearMax;
  57. }
  58. }
  59. protected static int GetFullYear(int shortYear)
  60. {
  61. int century = DateTime.Today.Year - (DateTime.Today.Year % 100);
  62. if(century < CutoffYear)
  63. {
  64. return (shortYear + century);
  65. }
  66. return (shortYear + century - 100);
  67. }
  68. protected override void AddAttributesToRender(HtmlTextWriter writer)
  69. {
  70. base.AddAttributesToRender(writer);
  71. if(RenderUplevel)
  72. {
  73. writer.AddAttribute("type", PropertyConverter.EnumToString(typeof(ValidationDataType), Type));
  74. NumberFormatInfo currInfo = NumberFormatInfo.CurrentInfo;
  75. if(Type == ValidationDataType.Double)
  76. {
  77. writer.AddAttribute("decimalchar", currInfo.NumberDecimalSeparator);
  78. return;
  79. }
  80. if(Type == ValidationDataType.Currency)
  81. {
  82. writer.AddAttribute("decimalchar", currInfo.CurrencyDecimalSeparator);
  83. string grpSep = currInfo.CurrencyGroupSeparator;
  84. if(grpSep[0] == 0xA0)
  85. {
  86. grpSep = " ";
  87. }
  88. writer.AddAttribute("groupchar", grpSep);
  89. writer.AddAttribute("digits", currInfo.CurrencyDecimalDigits.ToString(NumberFormatInfo.InvariantInfo));
  90. return;
  91. }
  92. if(Type == ValidationDataType.Date)
  93. {
  94. writer.AddAttribute("cutoffyear", CutoffYear.ToString());
  95. writer.AddAttribute("century", ( DateTime.Today.Year - (DateTime.Today.Year % 100) ).ToString());
  96. return;
  97. }
  98. }
  99. }
  100. protected override bool DetermineRenderUplevel()
  101. {
  102. if(Type == ValidationDataType.Date && DateTimeFormatInfo.CurrentInfo.Calendar.GetType() != typeof(GregorianCalendar))
  103. {
  104. return false;
  105. }
  106. return base.DetermineRenderUplevel();
  107. }
  108. /// <summary>
  109. /// Undocumented
  110. /// </summary>
  111. protected static bool Compare(string leftText, string rightText, ValidationCompareOperator op, ValidationDataType type)
  112. {
  113. object left = null, right = null;
  114. if(!Convert(leftText, type, out left))
  115. {
  116. return false;
  117. }
  118. if(op == ValidationCompareOperator.DataTypeCheck)
  119. {
  120. return true;
  121. }
  122. if(!Convert(rightText, type, out right))
  123. {
  124. return true;
  125. }
  126. int compareResult = 0;
  127. switch(type)
  128. {
  129. case ValidationDataType.String:
  130. compareResult = ((String)left).CompareTo(right);
  131. break;
  132. case ValidationDataType.Integer:
  133. compareResult = ((int)left).CompareTo(right);
  134. break;
  135. case ValidationDataType.Double:
  136. compareResult = ((Double)left).CompareTo(right);
  137. break;
  138. case ValidationDataType.Date:
  139. compareResult = ((DateTime)left).CompareTo(right);
  140. break;
  141. case ValidationDataType.Currency:
  142. compareResult = ((Decimal)left).CompareTo(right);
  143. break;
  144. }
  145. switch(op)
  146. {
  147. case ValidationCompareOperator.Equal:
  148. return (compareResult == 0);
  149. case ValidationCompareOperator.NotEqual:
  150. return (compareResult != 0);
  151. case ValidationCompareOperator.GreaterThan:
  152. return (compareResult > 0);
  153. case ValidationCompareOperator.GreaterThanEqual:
  154. return (compareResult >= 0);
  155. case ValidationCompareOperator.LessThan:
  156. return (compareResult < 0);
  157. case ValidationCompareOperator.LessThanEqual:
  158. return (compareResult <= 0);
  159. }
  160. return false;
  161. }
  162. /// <summary>
  163. /// Undocumented
  164. /// </summary>
  165. protected static string GetDateElementOrder()
  166. {
  167. string pattern = DateTimeFormatInfo.CurrentInfo.ShortDatePattern;
  168. //TODO: What are the various possibilities?
  169. // I can think of only y*/M*/d*, d*/M*/y*, M*/d*/y*
  170. if(pattern.IndexOf('y') < pattern.IndexOf('M'))
  171. {
  172. return "ymd";
  173. }
  174. if(pattern.IndexOf('M') < pattern.IndexOf('d'))
  175. {
  176. return "mdy";
  177. }
  178. return "dmy";
  179. }
  180. static bool ConvertDate (string text, ValidationDataType type, ref object convertedValue)
  181. {
  182. //Console.WriteLine (DateTimeFormatInfo.CurrentInfo.Calendar.GetType ());
  183. // FIXME: sometime, somehow, the condition is true even when GetType () says
  184. // it's a GregorianCalendar.
  185. if (DateTimeFormatInfo.CurrentInfo.Calendar.GetType () != typeof (GregorianCalendar)) {
  186. convertedValue = DateTime.Parse (text);
  187. return true;
  188. }
  189. string order = GetDateElementOrder ();
  190. int date = 0, mth = 0, year = 0;
  191. string dateStr = null;
  192. string mthStr = null;
  193. string yearStr = null;
  194. Match match = Regex.Match (text, @"^\s*((\d{4})|(\d{2}))([\.\/-])(\d{1,2})\4(\d{1,2})\s*$");
  195. if (match.Success || order == "ymd") {
  196. dateStr = match.Groups [6].Value;
  197. mthStr = match.Groups [5].Value;
  198. if (match.Groups [2].Success)
  199. yearStr = match.Groups [2].Value;
  200. else
  201. yearStr = match.Groups [3].Value;
  202. } else {
  203. match = Regex.Match(text, @"^\s*(\d{1,2})([\.\/-])(\d{1,2})\2((\d{4}|\d{2}))\s*$");
  204. if (!match.Success)
  205. return false;
  206. if (order == "dmy") {
  207. dateStr = match.Groups [1].Value;
  208. mthStr = match.Groups [3].Value;
  209. if (match.Groups [5].Success)
  210. yearStr = match.Groups [5].Value;
  211. else
  212. yearStr = match.Groups [6].Value;
  213. } else if (order == "mdy") {
  214. dateStr = match.Groups [3].Value;
  215. mthStr = match.Groups [1].Value;
  216. if (match.Groups [5].Success)
  217. yearStr = match.Groups [5].Value;
  218. else
  219. yearStr = match.Groups [6].Value;
  220. }
  221. }
  222. if (dateStr == null || mthStr == null || yearStr == null) {
  223. return false;
  224. }
  225. CultureInfo inv = CultureInfo.InvariantCulture;
  226. date = Int32.Parse (dateStr, inv);
  227. mth = Int32.Parse (mthStr, inv);
  228. year = Int32.Parse (yearStr, inv);
  229. year = (year < 100 ? GetFullYear (year) : year);
  230. if (date != 0 && mth != 0 && year != 0) {
  231. convertedValue = new DateTime (year, mth, date);
  232. return true;
  233. }
  234. return false;
  235. }
  236. static bool ConvertDouble (string text, ValidationDataType type, ref object convertedValue)
  237. {
  238. Match match = Regex.Match (text, @"^\s*([-\+])?(\d+)?(\" +
  239. NumberFormatInfo.CurrentInfo.NumberDecimalSeparator +
  240. @"(\d+))?\s*$");
  241. if (!match.Success)
  242. return false;
  243. string sign = (match.Groups [1].Success ? match.Groups [1].Value : "+");
  244. string decPart = (match.Groups [2].Success ? match.Groups [2].Value : "0");
  245. string mantissa = (match.Groups [4].Success ? match.Groups [4].Value : "0");
  246. string num = sign + decPart + "." + mantissa;
  247. convertedValue = Double.Parse (num, CultureInfo.InvariantCulture);
  248. return true;
  249. }
  250. static bool ConvertCurrency (string text, ValidationDataType type, ref object convertedValue)
  251. {
  252. string decSep = NumberFormatInfo.CurrentInfo.CurrencyDecimalSeparator;
  253. string grpSep = NumberFormatInfo.CurrentInfo.CurrencyGroupSeparator;
  254. int decDig = NumberFormatInfo.CurrentInfo.CurrencyDecimalDigits;
  255. if (grpSep [0] == 0xA0)
  256. grpSep = " ";
  257. string [] patternArray = new string [5];
  258. patternArray [0] = "^\\s*([-\\+])?(((\\d+)\\";
  259. patternArray [1] = grpSep;
  260. patternArray [2] = @")*)(\d+)";
  261. if (decDig > 0) {
  262. string [] decPattern = new string [5];
  263. decPattern [0] = "(\\";
  264. decPattern [1] = decSep;
  265. decPattern [2] = @"(\d{1,";
  266. decPattern [3] = decDig.ToString (NumberFormatInfo.InvariantInfo);
  267. decPattern [4] = @"}))";
  268. patternArray [3] = String.Concat (decPattern);
  269. } else {
  270. patternArray [3] = String.Empty;
  271. }
  272. patternArray [4] = @"?\s*$";
  273. Match match = Regex.Match (text, String.Concat (patternArray));
  274. if (!match.Success)
  275. return false;
  276. StringBuilder sb = new StringBuilder ();
  277. sb.Append (match.Groups [1]);
  278. CaptureCollection cc = match.Groups [4].Captures;
  279. foreach (IEnumerable current in cc)
  280. sb.Append ((Capture) current);
  281. sb.Append (match.Groups [5]);
  282. if (decDig > 0) {
  283. sb.Append (".");
  284. sb.Append (match.Groups [7]);
  285. }
  286. convertedValue = Decimal.Parse (sb.ToString (), CultureInfo.InvariantCulture);
  287. return true;
  288. }
  289. /// <summary>
  290. /// Undocumented
  291. /// </summary>
  292. protected static bool Convert (string text, ValidationDataType type, out object convertedValue)
  293. {
  294. CultureInfo inv = CultureInfo.InvariantCulture;
  295. convertedValue = null;
  296. try {
  297. switch(type) {
  298. case ValidationDataType.String:
  299. convertedValue = text;
  300. break;
  301. case ValidationDataType.Integer:
  302. convertedValue = Int32.Parse (text, inv);
  303. break;
  304. case ValidationDataType.Double:
  305. return ConvertDouble (text, type, ref convertedValue);
  306. case ValidationDataType.Date:
  307. return ConvertDate (text, type, ref convertedValue);
  308. case ValidationDataType.Currency:
  309. return ConvertCurrency (text, type, ref convertedValue);
  310. }
  311. } catch (Exception e) {
  312. convertedValue = null;
  313. }
  314. return (convertedValue != null);
  315. }
  316. }
  317. }