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