Files
mono/mcs/class/System.Web/System.Web.UI.WebControls/BaseCompareValidator.cs
T
Chris Toshok 545c9d318f 2005-09-01 Chris Toshok <[email protected]>
* Button.cs, ButtonField.cs, FontInfo.cs, Style.cs, Xml.cs,
	CommandField.cs, HyperLinkColumn.cs, Table.cs,
	RegularExpressionValidator.cs, DataGridPagerStyle.cs, Label.cs,
	CheckBox.cs, RadioButton.cs, TableStyle.cs, ListControl.cs,
	Image.cs, BaseCompareValidator.cs, BaseDataList.cs, DataList.cs,
	TemplateColumn.cs, RangeValidator.cs, ImageField.cs,
	BaseValidator.cs, CustomValidator.cs, TextBox.cs,
	ButtonFieldBase.cs, BoundField.cs, CheckBoxList.cs,
	RadioButtonList.cs, RequiredFieldValidator.cs, AdRotator.cs,
	Repeater.cs, DropDownList.cs, CheckBoxField.cs, HyperLink.cs,
	XmlDataSource.cs, Panel.cs, DataGrid.cs, ButtonColumn.cs,
	CompareValidator.cs, Calendar.cs, ListBox.cs, TableCell.cs,
	WebControl.cs, Literal.cs, ValidationSummary.cs, ImageButton.cs,
	TableRow.cs, LinkButton.cs, DataGridColumn.cs, BoundColumn.cs,
	TableItemStyle.cs: Add some WebCategory and WebSysDescription
	attributes.


svn path=/trunk/mcs/; revision=49301
2005-09-01 21:25:48 +00:00

228 lines
5.6 KiB
C#

//
// System.Web.UI.WebControls.BaseCompareValidator
//
// Authors:
// Chris Toshok ([email protected])
//
// (C) 2005 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.Web;
using System.Web.UI.WebControls;
using System.Globalization;
using System.ComponentModel;
namespace System.Web.UI.WebControls {
public abstract class BaseCompareValidator : BaseValidator {
ValidationDataType type;
public BaseCompareValidator ()
{
type = ValidationDataType.String;
}
protected override void AddAttributesToRender (HtmlTextWriter w)
{
if (RenderUplevel) {
w.AddAttribute ("datatype", type.ToString());
}
base.AddAttributesToRender (w);
}
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)
{
try {
switch (type) {
case ValidationDataType.String:
if (text == null) {
value = null;
return false;
}
else {
value = text;
return true;
}
case ValidationDataType.Integer:
value = Int32.Parse (text);
return true;
case ValidationDataType.Double:
value = Double.Parse(text);
return true;
case ValidationDataType.Date:
value = DateTime.Parse(text);
return true;
case ValidationDataType.Currency:
value = Decimal.Parse(text, NumberStyles.Currency);
return true;
default:
value = null;
return false;
}
}
catch {
value = null;
return false;
}
}
protected static bool Compare (string left,
string right,
ValidationCompareOperator op,
ValidationDataType type)
{
object lo, ro;
if (!Convert (left, type, 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 (right, type, 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;
}
}
[MonoTODO ("why override?")]
protected override bool DetermineRenderUplevel ()
{
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 ();
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 two_digit_year)
{
#if NET_2_0
/* 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 (two_digit_year <= twodigitcutoff) {
return cutoff - twodigitcutoff + two_digit_year;
}
else {
return cutoff - twodigitcutoff - 100 + two_digit_year;
}
#else
/* This is the broken implementation in 1.0 */
int cutoff = CutoffYear;
int twodigitcutoff = cutoff % 100;
return cutoff - twodigitcutoff + two_digit_year;
#endif
}
protected static int CutoffYear {
get {
return CultureInfo.CurrentCulture.Calendar.TwoDigitYearMax;
}
}
[DefaultValue(ValidationDataType.String)]
[WebSysDescription("")]
[WebCategory("Behavior")]
public ValidationDataType Type {
get {
return type;
}
set {
type = value;
}
}
}
}