BaseCompareValidator.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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.Web;
  31. using System.Web.UI.WebControls;
  32. using System.Globalization;
  33. using System.ComponentModel;
  34. namespace System.Web.UI.WebControls {
  35. public abstract class BaseCompareValidator : BaseValidator {
  36. ValidationDataType type;
  37. public BaseCompareValidator ()
  38. {
  39. type = ValidationDataType.String;
  40. }
  41. protected override void AddAttributesToRender (HtmlTextWriter w)
  42. {
  43. if (RenderUplevel) {
  44. w.AddAttribute ("datatype", type.ToString());
  45. }
  46. base.AddAttributesToRender (w);
  47. }
  48. public static bool CanConvert (string text,
  49. ValidationDataType type)
  50. {
  51. object value;
  52. return Convert (text, type, out value);
  53. }
  54. protected static bool Convert (string text,
  55. ValidationDataType type,
  56. out object value)
  57. {
  58. try {
  59. switch (type) {
  60. case ValidationDataType.String:
  61. if (text == null) {
  62. value = null;
  63. return false;
  64. }
  65. else {
  66. value = text;
  67. return true;
  68. }
  69. case ValidationDataType.Integer:
  70. value = Int32.Parse (text);
  71. return true;
  72. case ValidationDataType.Double:
  73. value = Double.Parse(text);
  74. return true;
  75. case ValidationDataType.Date:
  76. value = DateTime.Parse(text);
  77. return true;
  78. case ValidationDataType.Currency:
  79. value = Decimal.Parse(text, NumberStyles.Currency);
  80. return true;
  81. default:
  82. value = null;
  83. return false;
  84. }
  85. }
  86. catch {
  87. value = null;
  88. return false;
  89. }
  90. }
  91. protected static bool Compare (string left,
  92. string right,
  93. ValidationCompareOperator op,
  94. ValidationDataType type)
  95. {
  96. object lo, ro;
  97. if (!Convert (left, type, out lo))
  98. return false;
  99. /* DataTypeCheck is a unary operator that only
  100. * depends on the lhs */
  101. if (op == ValidationCompareOperator.DataTypeCheck)
  102. return true;
  103. /* pretty crackladen, but if we're unable to
  104. * convert the rhs to @type, the comparison
  105. * succeeds */
  106. if (!Convert (right, type, out ro))
  107. return true;
  108. int comp = ((IComparable)lo).CompareTo ((IComparable)ro);
  109. switch (op) {
  110. case ValidationCompareOperator.Equal:
  111. return comp == 0;
  112. case ValidationCompareOperator.NotEqual:
  113. return comp != 0;
  114. case ValidationCompareOperator.LessThan:
  115. return comp < 0;
  116. case ValidationCompareOperator.LessThanEqual:
  117. return comp <= 0;
  118. case ValidationCompareOperator.GreaterThan:
  119. return comp > 0;
  120. case ValidationCompareOperator.GreaterThanEqual:
  121. return comp >= 0;
  122. default:
  123. return false;
  124. }
  125. }
  126. [MonoTODO ("why override?")]
  127. protected override bool DetermineRenderUplevel ()
  128. {
  129. return base.DetermineRenderUplevel();
  130. }
  131. protected static string GetDateElementOrder ()
  132. {
  133. // I hope there's a better way to implement this...
  134. string pattern = Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortDatePattern;
  135. StringBuilder order = new StringBuilder();
  136. bool seen_date = false;
  137. bool seen_year = false;
  138. bool seen_month = false;
  139. pattern = pattern.ToLower ();
  140. for (int i = 0; i < pattern.Length; i ++) {
  141. char c = pattern[ i ];
  142. if (c != 'm' && c != 'd' && c != 'y')
  143. continue;
  144. if (c == 'm') {
  145. if (!seen_month) order.Append ("m");
  146. seen_month = true;
  147. }
  148. else if (c == 'y') {
  149. if (!seen_year) order.Append ("y");
  150. seen_year = true;
  151. }
  152. else /* (c == 'd') */ {
  153. if (!seen_date) order.Append ("d");
  154. seen_date = true;
  155. }
  156. }
  157. return order.ToString ();
  158. }
  159. protected static int GetFullYear (int two_digit_year)
  160. {
  161. #if NET_2_0
  162. /* This is an implementation that matches the
  163. * docs on msdn, but MS doesn't seem to go by
  164. * their docs (at least in 1.0). */
  165. int cutoff = CutoffYear;
  166. int twodigitcutoff = cutoff % 100;
  167. if (two_digit_year <= twodigitcutoff) {
  168. return cutoff - twodigitcutoff + two_digit_year;
  169. }
  170. else {
  171. return cutoff - twodigitcutoff - 100 + two_digit_year;
  172. }
  173. #else
  174. /* This is the broken implementation in 1.0 */
  175. int cutoff = CutoffYear;
  176. int twodigitcutoff = cutoff % 100;
  177. return cutoff - twodigitcutoff + two_digit_year;
  178. #endif
  179. }
  180. protected static int CutoffYear {
  181. get {
  182. return CultureInfo.CurrentCulture.Calendar.TwoDigitYearMax;
  183. }
  184. }
  185. [DefaultValue(ValidationDataType.String)]
  186. [WebSysDescription("")]
  187. [WebCategory("Behavior")]
  188. public ValidationDataType Type {
  189. get {
  190. return type;
  191. }
  192. set {
  193. type = value;
  194. }
  195. }
  196. }
  197. }