File: ValidatorUtils.cs

package info (click to toggle)
mono 6.12.0.199%2Bdfsg-6
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 1,296,836 kB
  • sloc: cs: 11,181,803; xml: 2,850,076; ansic: 699,709; cpp: 123,344; perl: 59,361; javascript: 30,841; asm: 21,853; makefile: 20,405; sh: 15,009; python: 4,839; pascal: 925; sql: 859; sed: 16; php: 1
file content (107 lines) | stat: -rw-r--r-- 4,760 bytes parent folder | download | duplicates (7)
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
//------------------------------------------------------------------------------
// <copyright file="ValidatorUtils.cs" company="Microsoft">
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>
//------------------------------------------------------------------------------

using System;
using System.Collections;
using System.IO;
using System.Reflection;
using System.Security.Permissions;
using System.Xml;
using System.Collections.Specialized;
using System.Globalization;
using System.ComponentModel;
using System.Security;
using System.Text;
using System.Text.RegularExpressions;

namespace System.Configuration {

    internal static class ValidatorUtils {

        public static void HelperParamValidation(object value, Type allowedType) {
            if (value == null) {
                return;
            }

            if (value.GetType() != allowedType) {
                throw new ArgumentException(SR.GetString(SR.Validator_value_type_invalid), String.Empty);
            }
        }
        
        public static void ValidateScalar<T>(T value, T min, T max, T resolution, bool exclusiveRange) where T : IComparable<T> {
            ValidateRangeImpl(value, min, max, exclusiveRange);

            // Validate the resolution
            ValidateResolution(resolution.ToString(), Convert.ToInt64(value, CultureInfo.InvariantCulture), Convert.ToInt64(resolution, CultureInfo.InvariantCulture));
        }
        private static void ValidateRangeImpl<T>( T value, T min, T max, bool exclusiveRange ) where T : IComparable<T> {
            IComparable<T>  itfValue           = (IComparable<T>)value;
            IComparable<T>  itfMax             = (IComparable<T>)max;
            bool            valueIsInRange     = false;

            // Check min range
            if ( itfValue.CompareTo( min ) >= 0 ) {
                // TRUE: value >= min
                valueIsInRange = true;
            }

            if ( valueIsInRange && ( itfValue.CompareTo( max ) > 0 ) ) {
                // TRUE: value > max
                valueIsInRange = false;
            }

            // Throw range validation error
            if ( !( valueIsInRange ^ exclusiveRange ) ) {
                string error = null;
                
                // First group of errors - the min and max range are the same. i.e. the valid value must be the same/equal to the min(max)
                if ( min.Equals( max ) ) {
                    if ( exclusiveRange ) {
                        // Valid values are outside of range. I.e has to be different then min( or max )
                        error =  SR.GetString( SR.Validation_scalar_range_violation_not_different );
                    }
                    else {
                        // Valid values are inside the range. I.e. has to be equal to min ( or max )
                        error = SR.GetString( SR.Validation_scalar_range_violation_not_equal );
                    }
                }
                // Second group of errors: min != max. I.e. its a range
                else {
                    if ( exclusiveRange ) {
                        // Valid values are outside of range. 
                        error =  SR.GetString( SR.Validation_scalar_range_violation_not_outside_range );
                    }
                    else {
                        // Valid values are inside the range. I.e. has to be equal to min ( or max )
                        error = SR.GetString( SR.Validation_scalar_range_violation_not_in_range );
                    }
                }
                
                throw new ArgumentException( String.Format( CultureInfo.InvariantCulture,
                                                            error, 
                                                            min.ToString(),
                                                            max.ToString() ) );
            }
        }

        private static void ValidateResolution(string resolutionAsString, long value, long resolution) {
            Debug.Assert(resolution > 0, "resolution > 0");

            if ((value % resolution) != 0) {
                throw new ArgumentException(SR.GetString(SR.Validator_scalar_resolution_violation, resolutionAsString));
            }
        }

        public static void ValidateScalar(TimeSpan value, TimeSpan min, TimeSpan max, long resolutionInSeconds, bool exclusiveRange) {
            ValidateRangeImpl(value, min, max, exclusiveRange);

            // Validate the resolution
            if (resolutionInSeconds > 0) {
                ValidateResolution(TimeSpan.FromSeconds( resolutionInSeconds ).ToString(), value.Ticks, resolutionInSeconds * TimeSpan.TicksPerSecond);
            }
        }
    }
}