﻿// Decompiled with JetBrains decompiler
// Type: System.Xml.Schema.XmlBooleanConverter
// 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.Runtime;
using System.Xml;
using System.Xml.XPath;

namespace System.Xml.Schema
{
  internal class XmlBooleanConverter : XmlBaseConverter
  {
    [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
    protected XmlBooleanConverter(XmlSchemaType schemaType)
      : base(schemaType)
    {
    }

    public static XmlValueConverter Create(XmlSchemaType schemaType)
    {
      return (XmlValueConverter) new XmlBooleanConverter(schemaType);
    }

    public override bool ToBoolean(bool value)
    {
      return value;
    }

    public override bool ToBoolean(string value)
    {
      if (value == null)
        throw new ArgumentNullException("value");
      return XmlConvert.ToBoolean(value);
    }

    public override bool ToBoolean(object value)
    {
      if (value == null)
        throw new ArgumentNullException("value");
      Type type = value.GetType();
      if (type == XmlBaseConverter.BooleanType)
        return (bool) value;
      if (type == XmlBaseConverter.StringType)
        return XmlConvert.ToBoolean((string) value);
      if (type == XmlBaseConverter.XmlAtomicValueType)
        return ((XPathItem) value).ValueAsBoolean;
      return (bool) this.ChangeListType(value, XmlBaseConverter.BooleanType, (IXmlNamespaceResolver) null);
    }

    public override string ToString(bool value)
    {
      return XmlConvert.ToString(value);
    }

    public override string ToString(string value, IXmlNamespaceResolver nsResolver)
    {
      if (value == null)
        throw new ArgumentNullException("value");
      return value;
    }

    public override string ToString(object value, IXmlNamespaceResolver nsResolver)
    {
      if (value == null)
        throw new ArgumentNullException("value");
      Type type = value.GetType();
      if (type == XmlBaseConverter.BooleanType)
        return XmlConvert.ToString((bool) value);
      if (type == XmlBaseConverter.StringType)
        return (string) value;
      if (type == XmlBaseConverter.XmlAtomicValueType)
        return ((XPathItem) value).Value;
      return (string) this.ChangeListType(value, XmlBaseConverter.StringType, nsResolver);
    }

    public override object ChangeType(bool value, Type destinationType)
    {
      if (destinationType == (Type) null)
        throw new ArgumentNullException("destinationType");
      if (destinationType == XmlBaseConverter.ObjectType)
        destinationType = this.DefaultClrType;
      if (destinationType == XmlBaseConverter.BooleanType)
        return (object) (bool) (value ? 1 : 0);
      if (destinationType == XmlBaseConverter.StringType)
        return (object) XmlConvert.ToString(value);
      if (destinationType == XmlBaseConverter.XmlAtomicValueType)
        return (object) new XmlAtomicValue(this.SchemaType, value);
      if (destinationType == XmlBaseConverter.XPathItemType)
        return (object) new XmlAtomicValue(this.SchemaType, value);
      return this.ChangeListType((object) (bool) (value ? 1 : 0), destinationType, (IXmlNamespaceResolver) null);
    }

    public override object ChangeType(string value, Type destinationType, IXmlNamespaceResolver nsResolver)
    {
      if (value == null)
        throw new ArgumentNullException("value");
      if (destinationType == (Type) null)
        throw new ArgumentNullException("destinationType");
      if (destinationType == XmlBaseConverter.ObjectType)
        destinationType = this.DefaultClrType;
      if (destinationType == XmlBaseConverter.BooleanType)
        return (object) (bool) (XmlConvert.ToBoolean(value) ? 1 : 0);
      if (destinationType == XmlBaseConverter.StringType)
        return (object) value;
      if (destinationType == XmlBaseConverter.XmlAtomicValueType)
        return (object) new XmlAtomicValue(this.SchemaType, value);
      if (destinationType == XmlBaseConverter.XPathItemType)
        return (object) new XmlAtomicValue(this.SchemaType, value);
      return this.ChangeListType((object) value, destinationType, nsResolver);
    }

    public override object ChangeType(object value, Type destinationType, IXmlNamespaceResolver nsResolver)
    {
      if (value == null)
        throw new ArgumentNullException("value");
      if (destinationType == (Type) null)
        throw new ArgumentNullException("destinationType");
      Type type = value.GetType();
      if (destinationType == XmlBaseConverter.ObjectType)
        destinationType = this.DefaultClrType;
      if (destinationType == XmlBaseConverter.BooleanType)
        return (object) (bool) (this.ToBoolean(value) ? 1 : 0);
      if (destinationType == XmlBaseConverter.StringType)
        return (object) this.ToString(value, nsResolver);
      if (destinationType == XmlBaseConverter.XmlAtomicValueType)
      {
        if (type == XmlBaseConverter.BooleanType)
          return (object) new XmlAtomicValue(this.SchemaType, (bool) value);
        if (type == XmlBaseConverter.StringType)
          return (object) new XmlAtomicValue(this.SchemaType, (string) value);
        if (type == XmlBaseConverter.XmlAtomicValueType)
          return (object) (XmlAtomicValue) value;
      }
      if (destinationType == XmlBaseConverter.XPathItemType)
      {
        if (type == XmlBaseConverter.BooleanType)
          return (object) new XmlAtomicValue(this.SchemaType, (bool) value);
        if (type == XmlBaseConverter.StringType)
          return (object) new XmlAtomicValue(this.SchemaType, (string) value);
        if (type == XmlBaseConverter.XmlAtomicValueType)
          return (object) (XmlAtomicValue) value;
      }
      return this.ChangeListType(value, destinationType, nsResolver);
    }
  }
}
