BaseCompareValidator.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. //
  2. // System.Web.UI.WebControls.BaseCompareValidator
  3. //
  4. // Authors:
  5. // Chris Toshok ([email protected])
  6. //
  7. // (C) 2005 Novell, Inc (http://www.novell.com)
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System.Text;
  29. using System.Threading;
  30. using System.Globalization;
  31. using System.ComponentModel;
  32. using System.Security.Permissions;
  33. namespace System.Web.UI.WebControls {
  34. // CAS
  35. [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  36. [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  37. public abstract class BaseCompareValidator : BaseValidator {
  38. #if NET_2_0
  39. protected
  40. #else
  41. public
  42. #endif
  43. BaseCompareValidator ()
  44. {
  45. }
  46. protected override void AddAttributesToRender (HtmlTextWriter w)
  47. {
  48. if (RenderUplevel) {
  49. w.AddAttribute ("datatype", Type.ToString());
  50. #if NET_2_0
  51. if (Page!=null && Type == ValidationDataType.Date) {
  52. DateTimeFormatInfo dateTimeFormat = CultureInfo.CurrentUICulture.DateTimeFormat;
  53. string pattern = dateTimeFormat.ShortDatePattern;
  54. string dateorder = (pattern.StartsWith ("y", true, CultureInfo.InvariantCulture) ? "ymd" : (pattern.StartsWith ("m", true, CultureInfo.InvariantCulture) ? "mdy" : "dmy"));
  55. Page.ClientScript.RegisterExpandoAttribute (ClientID, "dateorder", dateorder);
  56. Page.ClientScript.RegisterExpandoAttribute (ClientID, "cutoffyear", dateTimeFormat.Calendar.TwoDigitYearMax.ToString());
  57. }
  58. #endif
  59. }
  60. base.AddAttributesToRender (w);
  61. }
  62. public static bool CanConvert (string text,
  63. ValidationDataType type)
  64. {
  65. object value;
  66. return Convert (text, type, out value);
  67. }
  68. protected static bool Convert (string text,
  69. ValidationDataType type,
  70. out object value)
  71. {
  72. try {
  73. switch (type) {
  74. case ValidationDataType.String:
  75. value = text;
  76. return value != null;
  77. case ValidationDataType.Integer:
  78. value = Int32.Parse (text);
  79. return true;
  80. case ValidationDataType.Double:
  81. value = Double.Parse(text);
  82. return true;
  83. case ValidationDataType.Date:
  84. value = DateTime.Parse(text);
  85. return true;
  86. case ValidationDataType.Currency:
  87. value = Decimal.Parse(text, NumberStyles.Currency);
  88. return true;
  89. default:
  90. value = null;
  91. return false;
  92. }
  93. }
  94. catch {
  95. value = null;
  96. return false;
  97. }
  98. }
  99. protected static bool Compare (string left,
  100. string right,
  101. ValidationCompareOperator op,
  102. ValidationDataType type)
  103. {
  104. object lo, ro;
  105. if (!Convert (left, type, out lo))
  106. return false;
  107. /* DataTypeCheck is a unary operator that only
  108. * depends on the lhs */
  109. if (op == ValidationCompareOperator.DataTypeCheck)
  110. return true;
  111. /* pretty crackladen, but if we're unable to
  112. * convert the rhs to @type, the comparison
  113. * succeeds */
  114. if (!Convert (right, type, out ro))
  115. return true;
  116. int comp = ((IComparable)lo).CompareTo ((IComparable)ro);
  117. switch (op) {
  118. case ValidationCompareOperator.Equal:
  119. return comp == 0;
  120. case ValidationCompareOperator.NotEqual:
  121. return comp != 0;
  122. case ValidationCompareOperator.LessThan:
  123. return comp < 0;
  124. case ValidationCompareOperator.LessThanEqual:
  125. return comp <= 0;
  126. case ValidationCompareOperator.GreaterThan:
  127. return comp > 0;
  128. case ValidationCompareOperator.GreaterThanEqual:
  129. return comp >= 0;
  130. default:
  131. return false;
  132. }
  133. }
  134. protected override bool DetermineRenderUplevel ()
  135. {
  136. /* presumably the CompareValidator client side
  137. * code makes use of newer dom/js stuff than
  138. * the rest of the validators. but ours
  139. * doesn't for the moment, so let's just use
  140. * our present implementation
  141. */
  142. return base.DetermineRenderUplevel();
  143. }
  144. protected static string GetDateElementOrder ()
  145. {
  146. // I hope there's a better way to implement this...
  147. string pattern = Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortDatePattern;
  148. StringBuilder order = new StringBuilder();
  149. bool seen_date = false;
  150. bool seen_year = false;
  151. bool seen_month = false;
  152. pattern = pattern.ToLower ();
  153. for (int i = 0; i < pattern.Length; i ++) {
  154. char c = pattern[ i ];
  155. if (c != 'm' && c != 'd' && c != 'y')
  156. continue;
  157. if (c == 'm') {
  158. if (!seen_month) order.Append ("m");
  159. seen_month = true;
  160. }
  161. else if (c == 'y') {
  162. if (!seen_year) order.Append ("y");
  163. seen_year = true;
  164. }
  165. else /* (c == 'd') */ {
  166. if (!seen_date) order.Append ("d");
  167. seen_date = true;
  168. }
  169. }
  170. return order.ToString ();
  171. }
  172. protected static int GetFullYear (int two_digit_year)
  173. {
  174. #if NET_2_0
  175. /* This is an implementation that matches the
  176. * docs on msdn, but MS doesn't seem to go by
  177. * their docs (at least in 1.0). */
  178. int cutoff = CutoffYear;
  179. int twodigitcutoff = cutoff % 100;
  180. if (two_digit_year <= twodigitcutoff) {
  181. return cutoff - twodigitcutoff + two_digit_year;
  182. }
  183. else {
  184. return cutoff - twodigitcutoff - 100 + two_digit_year;
  185. }
  186. #else
  187. /* This is the broken implementation in 1.0 */
  188. int cutoff = CutoffYear;
  189. int twodigitcutoff = cutoff % 100;
  190. return cutoff - twodigitcutoff + two_digit_year;
  191. #endif
  192. }
  193. #if NET_2_0
  194. [DefaultValue (false)]
  195. [Themeable (false)]
  196. public bool CultureInvariantValues {
  197. get { return ViewState.GetBool ("CultureInvariantValues", false); }
  198. set { ViewState ["CultureInvariantValues"] = value; }
  199. }
  200. #endif
  201. protected static int CutoffYear {
  202. get {
  203. return CultureInfo.CurrentCulture.Calendar.TwoDigitYearMax;
  204. }
  205. }
  206. [DefaultValue(ValidationDataType.String)]
  207. #if NET_2_0
  208. [Themeable (false)]
  209. #endif
  210. [WebSysDescription("")]
  211. [WebCategory("Behavior")]
  212. public ValidationDataType Type {
  213. get { return ViewState ["Type"] == null ? ValidationDataType.String : (ValidationDataType) ViewState ["Type"]; }
  214. set { ViewState ["Type"] = value; }
  215. }
  216. #if NET_2_0
  217. [MonoTODO ("Not implemented")]
  218. public static bool CanConvert (string text,
  219. ValidationDataType type,
  220. bool cultureInvariant)
  221. {
  222. throw new NotImplementedException ();
  223. }
  224. [MonoTODO ("Not implemented")]
  225. protected static bool Compare (string leftText,
  226. bool cultureInvariantLeftText,
  227. string rightText,
  228. bool cultureInvariantRightText,
  229. ValidationCompareOperator op,
  230. ValidationDataType type)
  231. {
  232. throw new NotImplementedException ();
  233. }
  234. [MonoTODO ("Not implemented")]
  235. protected static bool Convert (string text,
  236. ValidationDataType type,
  237. bool cultureInvariant,
  238. out object value)
  239. {
  240. throw new NotImplementedException ();
  241. }
  242. #endif
  243. }
  244. }