corrected rendering: styles are applied correct
[mono.git] / mcs / class / System.Web / System.Web.UI.WebControls / BaseCompareValidator.cs
1 //
2 // System.Web.UI.WebControls.BaseCompareValidator
3 //
4 // Authors:
5 //      Chris Toshok (toshok@novell.com)
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
29 using System.Text;
30 using System.Threading;
31 using System.Globalization;
32 using System.ComponentModel;
33 using System.Security.Permissions;
34
35 namespace System.Web.UI.WebControls {
36
37         // CAS
38         [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
39         [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
40         public abstract class BaseCompareValidator : BaseValidator {
41
42                 ValidationDataType type;
43
44 #if NET_2_0
45                 protected
46 #else
47                 public
48 #endif
49                 BaseCompareValidator ()
50                 {
51                         type = ValidationDataType.String;
52                 }
53
54                 protected override void AddAttributesToRender (HtmlTextWriter w)
55                 {
56                         if (RenderUplevel) {
57                                 w.AddAttribute ("datatype", type.ToString());
58                         }
59
60                         base.AddAttributesToRender (w);
61                 }
62
63                 public static bool CanConvert (string text,
64                                                ValidationDataType type)
65                 {
66                         object value;
67
68                         return Convert (text, type, out value);
69                 }
70
71                 protected static bool Convert (string text,
72                                                ValidationDataType type,
73                                                out object value)
74                 {
75                         try {
76                                 switch (type) {
77                                 case ValidationDataType.String:
78                                         value = text;
79                                         return value != null;
80                                 case ValidationDataType.Integer:
81                                         value = Int32.Parse (text);
82                                         return true;
83                                 case ValidationDataType.Double:
84                                         value = Double.Parse(text);
85                                         return true;
86                                 case ValidationDataType.Date:
87                                         value = DateTime.Parse(text);
88                                         return true;
89                                 case ValidationDataType.Currency:
90                                         value = Decimal.Parse(text, NumberStyles.Currency);
91                                         return true;
92                                 default:
93                                         value = null;
94                                         return false;
95                                 }
96                         }
97                         catch {
98                                 value = null;
99                                 return false;
100                         }
101
102                 }
103
104                 protected static bool Compare (string left,
105                                                string right,
106                                                ValidationCompareOperator op,
107                                                ValidationDataType type)
108                 {
109                         object lo, ro;
110
111                         if (!Convert (left, type, out lo))
112                                 return false;
113
114                         /* DataTypeCheck is a unary operator that only
115                          * depends on the lhs */
116                         if (op == ValidationCompareOperator.DataTypeCheck)
117                                 return true;
118
119                         /* pretty crackladen, but if we're unable to
120                          * convert the rhs to @type, the comparison
121                          * succeeds */
122                         if (!Convert (right, type, out ro))
123                                 return true;
124
125                         int comp = ((IComparable)lo).CompareTo ((IComparable)ro);
126
127                         switch (op) {
128                         case ValidationCompareOperator.Equal:
129                                 return comp == 0;
130                         case ValidationCompareOperator.NotEqual:
131                                 return comp != 0;
132                         case ValidationCompareOperator.LessThan:
133                                 return comp < 0;
134                         case ValidationCompareOperator.LessThanEqual:
135                                 return comp <= 0;
136                         case ValidationCompareOperator.GreaterThan:
137                                 return comp > 0;
138                         case ValidationCompareOperator.GreaterThanEqual:
139                                 return comp >= 0;
140                         default:
141                                 return false;
142                         }
143                 }
144
145                 protected override bool DetermineRenderUplevel ()
146                 {
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
152                          */
153                         return base.DetermineRenderUplevel();
154                 }
155
156                 protected static string GetDateElementOrder ()
157                 {
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;
164
165                         pattern = pattern.ToLower ();
166
167                         for (int i = 0; i < pattern.Length; i ++) {
168                                 char c = pattern[ i ];
169                                 if (c != 'm' && c != 'd' && c != 'y')
170                                         continue;
171
172                                 if (c == 'm') {
173                                         if (!seen_month) order.Append ("m");
174                                         seen_month = true;
175                                 }
176                                 else if (c == 'y') {
177                                         if (!seen_year) order.Append ("y");
178                                         seen_year = true;
179                                 }
180                                 else /* (c == 'd') */ {
181                                         if (!seen_date) order.Append ("d");
182                                         seen_date = true;
183                                 }
184                         }
185
186                         return order.ToString ();
187                 }
188
189                 protected static int GetFullYear (int two_digit_year)
190                 {
191 #if NET_2_0
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;
197
198                         if (two_digit_year <= twodigitcutoff) {
199                                 return cutoff - twodigitcutoff + two_digit_year;
200                         }
201                         else {
202                                 return cutoff - twodigitcutoff - 100 + two_digit_year;
203                         }
204 #else
205                         /* This is the broken implementation in 1.0 */
206                         int cutoff = CutoffYear;
207                         int twodigitcutoff = cutoff % 100;
208
209                         return cutoff - twodigitcutoff + two_digit_year;
210 #endif
211                 }
212
213 #if NET_2_0
214                 [MonoTODO]
215                 [DefaultValue (false)]
216                 [Themeable (false)]
217                 public bool CultureInvariantValues {
218                         get {
219                                 throw new NotImplementedException ();
220                         }
221                         set {
222                                 throw new NotImplementedException ();
223                         }
224                 }
225 #endif
226
227                 protected static int CutoffYear {
228                         get {
229                                 return CultureInfo.CurrentCulture.Calendar.TwoDigitYearMax;
230                         }
231                 }
232
233                 [DefaultValue(ValidationDataType.String)]
234 #if NET_2_0
235                 [Themeable (false)]
236 #endif
237                 [WebSysDescription("")]
238                 [WebCategory("Behavior")]
239                 public ValidationDataType Type {
240                         get {
241                                 return type;
242                         }
243                         set {
244                                 type = value;
245                         }
246                 }
247
248 #if NET_2_0
249                 [MonoTODO]
250                 public static bool CanConvert (string text, 
251                                                ValidationDataType type, 
252                                                bool cultureInvariant)
253                 {
254                         throw new NotImplementedException ();
255                 }
256
257                 [MonoTODO]
258                 protected static bool Compare (string leftText, 
259                                                bool cultureInvariantLeftText, 
260                                                string rightText, 
261                                                bool cultureInvariantRightText, 
262                                                ValidationCompareOperator op, 
263                                                ValidationDataType type)
264                 {
265                         throw new NotImplementedException ();
266                 }
267
268                 [MonoTODO]
269                 protected static bool Convert (string text,
270                                                ValidationDataType type,
271                                                bool cultureInvariant,
272                                                out object value)
273                 {
274                         throw new NotImplementedException ();
275                 }
276 #endif
277         }
278
279 }
280