BaseCompareValidator.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. ValidationDataType type;
  39. #if NET_2_0
  40. protected
  41. #else
  42. public
  43. #endif
  44. BaseCompareValidator ()
  45. {
  46. type = ValidationDataType.String;
  47. }
  48. protected override void AddAttributesToRender (HtmlTextWriter w)
  49. {
  50. if (RenderUplevel) {
  51. w.AddAttribute ("datatype", type.ToString());
  52. }
  53. base.AddAttributesToRender (w);
  54. }
  55. public static bool CanConvert (string text,
  56. ValidationDataType type)
  57. {
  58. object value;
  59. return Convert (text, type, out value);
  60. }
  61. protected static bool Convert (string text,
  62. ValidationDataType type,
  63. out object value)
  64. {
  65. try {
  66. switch (type) {
  67. case ValidationDataType.String:
  68. if (text == null) {
  69. value = null;
  70. return false;
  71. }
  72. else {
  73. value = text;
  74. return true;
  75. }
  76. case ValidationDataType.Integer:
  77. value = Int32.Parse (text);
  78. return true;
  79. case ValidationDataType.Double:
  80. value = Double.Parse(text);
  81. return true;
  82. case ValidationDataType.Date:
  83. value = DateTime.Parse(text);
  84. return true;
  85. case ValidationDataType.Currency:
  86. value = Decimal.Parse(text, NumberStyles.Currency);
  87. return true;
  88. default:
  89. value = null;
  90. return false;
  91. }
  92. }
  93. catch {
  94. value = null;
  95. return false;
  96. }
  97. }
  98. protected static bool Compare (string left,
  99. string right,
  100. ValidationCompareOperator op,
  101. ValidationDataType type)
  102. {
  103. object lo, ro;
  104. if (!Convert (left, type, out lo))
  105. return false;
  106. /* DataTypeCheck is a unary operator that only
  107. * depends on the lhs */
  108. if (op == ValidationCompareOperator.DataTypeCheck)
  109. return true;
  110. /* pretty crackladen, but if we're unable to
  111. * convert the rhs to @type, the comparison
  112. * succeeds */
  113. if (!Convert (right, type, out ro))
  114. return true;
  115. int comp = ((IComparable)lo).CompareTo ((IComparable)ro);
  116. switch (op) {
  117. case ValidationCompareOperator.Equal:
  118. return comp == 0;
  119. case ValidationCompareOperator.NotEqual:
  120. return comp != 0;
  121. case ValidationCompareOperator.LessThan:
  122. return comp < 0;
  123. case ValidationCompareOperator.LessThanEqual:
  124. return comp <= 0;
  125. case ValidationCompareOperator.GreaterThan:
  126. return comp > 0;
  127. case ValidationCompareOperator.GreaterThanEqual:
  128. return comp >= 0;
  129. default:
  130. return false;
  131. }
  132. }
  133. protected override bool DetermineRenderUplevel ()
  134. {
  135. /* presumably the CompareValidator client side
  136. * code makes use of newer dom/js stuff than
  137. * the rest of the validators. but ours
  138. * doesn't for the moment, so let's just use
  139. * our present implementation
  140. */
  141. return base.DetermineRenderUplevel();
  142. }
  143. protected static string GetDateElementOrder ()
  144. {
  145. // I hope there's a better way to implement this...
  146. string pattern = Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortDatePattern;
  147. StringBuilder order = new StringBuilder();
  148. bool seen_date = false;
  149. bool seen_year = false;
  150. bool seen_month = false;
  151. pattern = pattern.ToLower ();
  152. for (int i = 0; i < pattern.Length; i ++) {
  153. char c = pattern[ i ];
  154. if (c != 'm' && c != 'd' && c != 'y')
  155. continue;
  156. if (c == 'm') {
  157. if (!seen_month) order.Append ("m");
  158. seen_month = true;
  159. }
  160. else if (c == 'y') {
  161. if (!seen_year) order.Append ("y");
  162. seen_year = true;
  163. }
  164. else /* (c == 'd') */ {
  165. if (!seen_date) order.Append ("d");
  166. seen_date = true;
  167. }
  168. }
  169. return order.ToString ();
  170. }
  171. protected static int GetFullYear (int two_digit_year)
  172. {
  173. #if NET_2_0
  174. /* This is an implementation that matches the
  175. * docs on msdn, but MS doesn't seem to go by
  176. * their docs (at least in 1.0). */
  177. int cutoff = CutoffYear;
  178. int twodigitcutoff = cutoff % 100;
  179. if (two_digit_year <= twodigitcutoff) {
  180. return cutoff - twodigitcutoff + two_digit_year;
  181. }
  182. else {
  183. return cutoff - twodigitcutoff - 100 + two_digit_year;
  184. }
  185. #else
  186. /* This is the broken implementation in 1.0 */
  187. int cutoff = CutoffYear;
  188. int twodigitcutoff = cutoff % 100;
  189. return cutoff - twodigitcutoff + two_digit_year;
  190. #endif
  191. }
  192. #if NET_2_0
  193. [MonoTODO]
  194. [DefaultValue (false)]
  195. [Themeable (false)]
  196. public bool CultureInvariantValues {
  197. get {
  198. throw new NotImplementedException ();
  199. }
  200. set {
  201. throw new NotImplementedException ();
  202. }
  203. }
  204. #endif
  205. protected static int CutoffYear {
  206. get {
  207. return CultureInfo.CurrentCulture.Calendar.TwoDigitYearMax;
  208. }
  209. }
  210. [DefaultValue(ValidationDataType.String)]
  211. #if NET_2_0
  212. [Themeable (false)]
  213. #endif
  214. [WebSysDescription("")]
  215. [WebCategory("Behavior")]
  216. public ValidationDataType Type {
  217. get {
  218. return type;
  219. }
  220. set {
  221. type = value;
  222. }
  223. }
  224. #if NET_2_0
  225. [MonoTODO]
  226. public static bool CanConvert (string text,
  227. ValidationDataType type,
  228. bool cultureInvariant)
  229. {
  230. throw new NotImplementedException ();
  231. }
  232. [MonoTODO]
  233. protected static bool Compare (string leftText,
  234. bool cultureInvariantLeftText,
  235. string rightText,
  236. bool cultureInvariantRightText,
  237. ValidationCompareOperator op,
  238. ValidationDataType type)
  239. {
  240. throw new NotImplementedException ();
  241. }
  242. [MonoTODO]
  243. protected static bool Convert (string text,
  244. ValidationDataType type,
  245. bool cultureInvariant,
  246. out object value)
  247. {
  248. throw new NotImplementedException ();
  249. }
  250. #endif
  251. }
  252. }