2 // System.Web.UI.WebControls.BaseCompareValidator
5 // Chris Toshok (toshok@novell.com)
7 // (C) 2005 Novell, Inc (http://www.novell.com)
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
30 using System.Threading;
31 using System.Globalization;
32 using System.ComponentModel;
33 using System.Security.Permissions;
35 namespace System.Web.UI.WebControls {
38 [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
39 [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
40 public abstract class BaseCompareValidator : BaseValidator {
42 ValidationDataType type;
49 BaseCompareValidator ()
51 type = ValidationDataType.String;
54 protected override void AddAttributesToRender (HtmlTextWriter w)
57 w.AddAttribute ("datatype", type.ToString());
60 base.AddAttributesToRender (w);
63 public static bool CanConvert (string text,
64 ValidationDataType type)
68 return Convert (text, type, out value);
71 protected static bool Convert (string text,
72 ValidationDataType type,
77 case ValidationDataType.String:
80 case ValidationDataType.Integer:
81 value = Int32.Parse (text);
83 case ValidationDataType.Double:
84 value = Double.Parse(text);
86 case ValidationDataType.Date:
87 value = DateTime.Parse(text);
89 case ValidationDataType.Currency:
90 value = Decimal.Parse(text, NumberStyles.Currency);
104 protected static bool Compare (string left,
106 ValidationCompareOperator op,
107 ValidationDataType type)
111 if (!Convert (left, type, out lo))
114 /* DataTypeCheck is a unary operator that only
115 * depends on the lhs */
116 if (op == ValidationCompareOperator.DataTypeCheck)
119 /* pretty crackladen, but if we're unable to
120 * convert the rhs to @type, the comparison
122 if (!Convert (right, type, out ro))
125 int comp = ((IComparable)lo).CompareTo ((IComparable)ro);
128 case ValidationCompareOperator.Equal:
130 case ValidationCompareOperator.NotEqual:
132 case ValidationCompareOperator.LessThan:
134 case ValidationCompareOperator.LessThanEqual:
136 case ValidationCompareOperator.GreaterThan:
138 case ValidationCompareOperator.GreaterThanEqual:
145 protected override bool DetermineRenderUplevel ()
147 /* presumably the CompareValidator client side
148 * code makes use of newer dom/js stuff than
149 * the rest of the validators. but ours
150 * doesn't for the moment, so let's just use
151 * our present implementation
153 return base.DetermineRenderUplevel();
156 protected static string GetDateElementOrder ()
158 // I hope there's a better way to implement this...
159 string pattern = Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortDatePattern;
160 StringBuilder order = new StringBuilder();
161 bool seen_date = false;
162 bool seen_year = false;
163 bool seen_month = false;
165 pattern = pattern.ToLower ();
167 for (int i = 0; i < pattern.Length; i ++) {
168 char c = pattern[ i ];
169 if (c != 'm' && c != 'd' && c != 'y')
173 if (!seen_month) order.Append ("m");
177 if (!seen_year) order.Append ("y");
180 else /* (c == 'd') */ {
181 if (!seen_date) order.Append ("d");
186 return order.ToString ();
189 protected static int GetFullYear (int two_digit_year)
192 /* This is an implementation that matches the
193 * docs on msdn, but MS doesn't seem to go by
194 * their docs (at least in 1.0). */
195 int cutoff = CutoffYear;
196 int twodigitcutoff = cutoff % 100;
198 if (two_digit_year <= twodigitcutoff) {
199 return cutoff - twodigitcutoff + two_digit_year;
202 return cutoff - twodigitcutoff - 100 + two_digit_year;
205 /* This is the broken implementation in 1.0 */
206 int cutoff = CutoffYear;
207 int twodigitcutoff = cutoff % 100;
209 return cutoff - twodigitcutoff + two_digit_year;
215 [DefaultValue (false)]
217 public bool CultureInvariantValues {
219 throw new NotImplementedException ();
222 throw new NotImplementedException ();
227 protected static int CutoffYear {
229 return CultureInfo.CurrentCulture.Calendar.TwoDigitYearMax;
233 [DefaultValue(ValidationDataType.String)]
237 [WebSysDescription("")]
238 [WebCategory("Behavior")]
239 public ValidationDataType Type {
250 public static bool CanConvert (string text,
251 ValidationDataType type,
252 bool cultureInvariant)
254 throw new NotImplementedException ();
258 protected static bool Compare (string leftText,
259 bool cultureInvariantLeftText,
261 bool cultureInvariantRightText,
262 ValidationCompareOperator op,
263 ValidationDataType type)
265 throw new NotImplementedException ();
269 protected static bool Convert (string text,
270 ValidationDataType type,
271 bool cultureInvariant,
274 throw new NotImplementedException ();