private static bool ProcessRestrictions( string value,
SchemaValidationResult errors,
XmlSchemaSimpleTypeRestriction restriction,
string typeName,
string name)
{
bool isValid = true;
if (restriction == null)
return false;
foreach (object facet in restriction.Facets)
{
if (facet is XmlSchemaMinInclusiveFacet)
{
var lf = facet as XmlSchemaMinInclusiveFacet;
if (!string.IsNullOrEmpty( value ))
{
int iResult;
double dResult;
if (( typeName.Contains( "int" ) && int.TryParse( value, out iResult ) &&
iResult < int.Parse( lf.Value ) )
||
( typeName.Contains( "dou" ) && double.TryParse( value, out dResult ) &&
dResult < double.Parse( lf.Value ) ))
{
errors.AddError( String.Format( "The value of {0} must not be less than {1}", name, lf.Value ) );
isValid = false;
}
}
}
else if (facet is XmlSchemaMinExclusiveFacet)
{
var lf = facet as XmlSchemaMinExclusiveFacet;
if (!string.IsNullOrEmpty( value ))
{
int iResult;
double dResult;
if (( typeName.Contains( "int" ) && int.TryParse( value, out iResult ) &&
iResult <= int.Parse( lf.Value ) )
||
( typeName.Contains( "dou" ) && double.TryParse( value, out dResult ) &&
dResult <= double.Parse( lf.Value ) ))
{
errors.AddError( String.Format( "The value of {0} must not be less than or equal to {1}",
name,
lf.Value ) );
isValid = false;
}
}
}
else if (facet is XmlSchemaMaxInclusiveFacet)
{
var lf = facet as XmlSchemaMaxInclusiveFacet;
if (!string.IsNullOrEmpty( value ))
{
int iResult;
double dResult;
if (( typeName.Contains( "int" ) && int.TryParse( value, out iResult ) &&
iResult > int.Parse( lf.Value ) )
||
( typeName.Contains( "dou" ) && double.TryParse( value, out dResult ) &&
dResult > double.Parse( lf.Value ) ))
{
errors.AddError( String.Format( "The value of {0} must not be greater than {1}", name,
lf.Value ) );
isValid = false;
}
}
}
else if (facet is XmlSchemaMaxExclusiveFacet)
{
var lf = facet as XmlSchemaMaxExclusiveFacet;
if (!string.IsNullOrEmpty( value ))
{
int iResult;
double dResult;
if (( typeName.Contains( "int" ) && int.TryParse( value, out iResult ) &&
iResult >= int.Parse( lf.Value ) )
||
( typeName.Contains( "dou" ) && double.TryParse( value, out dResult ) &&
dResult >= double.Parse( lf.Value ) ))
{
errors.AddError( String.Format( "The value of {0} must not be greater than or equal to {1}",
name, lf.Value ) );
isValid = false;
}
}
}
else if (facet is XmlSchemaLengthFacet)
{
var lf = facet as XmlSchemaLengthFacet;
if (!String.IsNullOrEmpty( value ) && value.Length > int.Parse( lf.Value ))
{
errors.AddError( String.Format( "The value's length must not excede {0} characters", lf.Value ) );
isValid = false;
}
}
else if (facet is XmlSchemaMinLengthFacet)
{
var lf = facet as XmlSchemaMinLengthFacet;
if (!String.IsNullOrEmpty( value ) && value.Length < int.Parse( lf.Value ))
{
errors.AddError( String.Format( "The value's length must not be less than {0} characters",
lf.Value ) );
isValid = false;
}
}
else if (facet is XmlSchemaMaxLengthFacet)
{
var lf = facet as XmlSchemaMaxLengthFacet;
if (!String.IsNullOrEmpty( value ) && value.Length > int.Parse( lf.Value ))
{
errors.AddError( String.Format( "The value's length must not be greater than {0} characters",
lf.Value ) );
isValid = false;
}
}
else if (facet is XmlSchemaPatternFacet)
{
var spf = facet as XmlSchemaPatternFacet;
Console.WriteLine( spf.Id + @" : " + spf.Value );
if (value != null && !Regex.Match( value, spf.Value ).Success)
{
errors.AddError( String.Format( "The \"{0}\" value must match the regular expression: {1}", name,
spf.Value ) );
isValid = false;
}
}
else if (facet is XmlSchemaEnumerationFacet)
{
//TODO:XmlSchemaEnumerationFacet
int i = 0;
}
else if (facet is XmlSchemaTotalDigitsFacet)
{
//TODO:XmlSchemaTotalDigitsFacet
int i = 0;
}
else if (facet is XmlSchemaWhiteSpaceFacet)
{
//TODO:XmlSchemaWhiteSpaceFacet
int i = 0;
}
}
return isValid;
}