| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227 |
- //
- // 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;
- }
- }
- }
- }
|