﻿// Decompiled with JetBrains decompiler
// Type: System.Xml.Schema.QNameFacetsChecker
// Assembly: System.Xml, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
// MVID: 50ABAB51-7DC3-4F0A-A797-0D0C2D124D60
// Assembly location: C:\Windows\Microsoft.NET\Framework\v4.0.30319\System.Xml.dll

using System;
using System.Collections;
using System.Runtime;
using System.Xml;

namespace System.Xml.Schema
{
  internal class QNameFacetsChecker : FacetsChecker
  {
    [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
    public QNameFacetsChecker()
    {
    }

    internal override Exception CheckValueFacets(object value, XmlSchemaDatatype datatype)
    {
      return this.CheckValueFacets((XmlQualifiedName) datatype.ValueConverter.ChangeType(value, typeof (XmlQualifiedName)), datatype);
    }

    internal override Exception CheckValueFacets(XmlQualifiedName value, XmlSchemaDatatype datatype)
    {
      RestrictionFacets restriction = datatype.Restriction;
      RestrictionFlags restrictionFlags = restriction != null ? restriction.Flags : (RestrictionFlags) 0;
      if (restrictionFlags != (RestrictionFlags) 0)
      {
        int length = value.ToString().Length;
        if ((restrictionFlags & RestrictionFlags.Length) != (RestrictionFlags) 0 && restriction.Length != length)
          return (Exception) new XmlSchemaException("Sch_LengthConstraintFailed", string.Empty);
        if ((restrictionFlags & RestrictionFlags.MinLength) != (RestrictionFlags) 0 && length < restriction.MinLength)
          return (Exception) new XmlSchemaException("Sch_MinLengthConstraintFailed", string.Empty);
        if ((restrictionFlags & RestrictionFlags.MaxLength) != (RestrictionFlags) 0 && restriction.MaxLength < length)
          return (Exception) new XmlSchemaException("Sch_MaxLengthConstraintFailed", string.Empty);
        if ((restrictionFlags & RestrictionFlags.Enumeration) != (RestrictionFlags) 0 && !this.MatchEnumeration(value, restriction.Enumeration))
          return (Exception) new XmlSchemaException("Sch_EnumerationConstraintFailed", string.Empty);
      }
      return (Exception) null;
    }

    internal override bool MatchEnumeration(object value, ArrayList enumeration, XmlSchemaDatatype datatype)
    {
      return this.MatchEnumeration((XmlQualifiedName) datatype.ValueConverter.ChangeType(value, typeof (XmlQualifiedName)), enumeration);
    }

    private bool MatchEnumeration(XmlQualifiedName value, ArrayList enumeration)
    {
      for (int index = 0; index < enumeration.Count; ++index)
      {
        if (value.Equals((object) (XmlQualifiedName) enumeration[index]))
          return true;
      }
      return false;
    }
  }
}
