Code:
/ WCF / WCF / 3.5.30729.1 / untmp / Orcas / SP / ndp / cdf / src / WCF / Serialization / System / Runtime / Serialization / XmlReaderDelegator.cs / 2 / XmlReaderDelegator.cs
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------
namespace System.Runtime.Serialization
{
using System;
using System.Diagnostics.CodeAnalysis;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;
using System.Reflection;
using System.Globalization;
using System.Collections.Generic;
#if USE_REFEMIT
public class XmlReaderDelegator
#else
internal class XmlReaderDelegator
#endif
{
protected XmlReader reader;
protected XmlDictionaryReader dictionaryReader;
protected bool isEndOfEmptyElement = false;
public XmlReaderDelegator(XmlReader reader)
{
XmlObjectSerializer.CheckNull(reader, "reader");
this.reader = reader;
this.dictionaryReader = reader as XmlDictionaryReader;
}
internal XmlReader UnderlyingReader
{
get { return reader; }
}
internal ExtensionDataReader UnderlyingExtensionDataReader
{
get { return reader as ExtensionDataReader; }
}
internal int AttributeCount
{
get { return isEndOfEmptyElement ? 0 : reader.AttributeCount; }
}
internal string GetAttribute(string name)
{
return isEndOfEmptyElement ? null : reader.GetAttribute(name);
}
internal string GetAttribute(string name, string namespaceUri)
{
return isEndOfEmptyElement ? null : reader.GetAttribute(name, namespaceUri);
}
internal string GetAttribute(int i)
{
if (isEndOfEmptyElement)
throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("i", SR.GetString(SR.XmlElementAttributes)));
return reader.GetAttribute(i);
}
internal bool IsEmptyElement
{
get { return false; }
}
internal bool IsNamespaceURI(string ns)
{
if (dictionaryReader == null)
return ns == reader.NamespaceURI;
else
return dictionaryReader.IsNamespaceUri(ns);
}
internal bool IsLocalName(string localName)
{
if (dictionaryReader == null)
return localName == reader.LocalName;
else
return dictionaryReader.IsLocalName(localName);
}
internal bool IsNamespaceUri(XmlDictionaryString ns)
{
if (dictionaryReader == null)
return ns.Value == reader.NamespaceURI;
else
return dictionaryReader.IsNamespaceUri(ns);
}
internal bool IsLocalName(XmlDictionaryString localName)
{
if (dictionaryReader == null)
return localName.Value == reader.LocalName;
else
return dictionaryReader.IsLocalName(localName);
}
internal int IndexOfLocalName(XmlDictionaryString[] localNames, XmlDictionaryString ns)
{
if (dictionaryReader != null)
return dictionaryReader.IndexOfLocalName(localNames, ns);
if (reader.NamespaceURI == ns.Value)
{
string localName = this.LocalName;
for (int i = 0; i < localNames.Length; i++)
{
if (localName == localNames[i].Value)
{
return i;
}
}
}
return -1;
}
#if USE_REFEMIT
public bool IsStartElement()
#else
internal bool IsStartElement()
#endif
{
return !isEndOfEmptyElement && reader.IsStartElement();
}
internal bool IsStartElement(string localname, string ns)
{
return !isEndOfEmptyElement && reader.IsStartElement(localname, ns);
}
#if USE_REFEMIT
public bool IsStartElement(XmlDictionaryString localname, XmlDictionaryString ns)
#else
internal bool IsStartElement(XmlDictionaryString localname, XmlDictionaryString ns)
#endif
{
if (dictionaryReader == null)
return !isEndOfEmptyElement && reader.IsStartElement(localname.Value, ns.Value);
else
return !isEndOfEmptyElement && dictionaryReader.IsStartElement(localname, ns);
}
internal bool MoveToAttribute(string name)
{
return isEndOfEmptyElement ? false : reader.MoveToAttribute(name);
}
internal bool MoveToAttribute(string name, string ns)
{
return isEndOfEmptyElement ? false : reader.MoveToAttribute(name, ns);
}
internal void MoveToAttribute(int i)
{
if (isEndOfEmptyElement)
throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("i", SR.GetString(SR.XmlElementAttributes)));
reader.MoveToAttribute(i);
}
internal bool MoveToElement()
{
return isEndOfEmptyElement ? false : reader.MoveToElement();
}
internal bool MoveToFirstAttribute()
{
return isEndOfEmptyElement ? false : reader.MoveToFirstAttribute();
}
internal bool MoveToNextAttribute()
{
return isEndOfEmptyElement ? false : reader.MoveToNextAttribute();
}
#if USE_REFEMIT
public XmlNodeType NodeType
#else
internal XmlNodeType NodeType
#endif
{
get { return isEndOfEmptyElement ? XmlNodeType.EndElement : reader.NodeType; }
}
internal bool Read()
{
//reader.MoveToFirstAttribute();
//if (NodeType == XmlNodeType.Attribute)
reader.MoveToElement();
if (!reader.IsEmptyElement)
return reader.Read();
if (isEndOfEmptyElement)
{
isEndOfEmptyElement = false;
return reader.Read();
}
isEndOfEmptyElement = true;
return true;
}
internal XmlNodeType MoveToContent()
{
if (isEndOfEmptyElement)
return XmlNodeType.EndElement;
return reader.MoveToContent();
}
internal bool ReadAttributeValue()
{
return isEndOfEmptyElement ? false : reader.ReadAttributeValue();
}
#if USE_REFEMIT
public void ReadEndElement()
#else
internal void ReadEndElement()
#endif
{
if (isEndOfEmptyElement)
Read();
else
reader.ReadEndElement();
}
Exception CreateInvalidPrimitiveTypeException(Type type)
{
return new InvalidDataContractException(SR.GetString(
type.IsInterface ? SR.InterfaceTypeCannotBeCreated : SR.InvalidPrimitiveType,
DataContract.GetClrTypeFullName(type)));
}
#if USE_REFEMIT
public object ReadElementContentAsAnyType(Type valueType)
#else
internal object ReadElementContentAsAnyType(Type valueType)
#endif
{
Read();
object o = ReadContentAsAnyType(valueType);
ReadEndElement();
return o;
}
internal object ReadContentAsAnyType(Type valueType)
{
switch (Type.GetTypeCode(valueType))
{
case TypeCode.Boolean:
return ReadContentAsBoolean();
case TypeCode.Char:
return ReadContentAsChar();
case TypeCode.Byte:
return ReadContentAsUnsignedByte();
case TypeCode.Int16:
return ReadContentAsShort();
case TypeCode.Int32:
return ReadContentAsInt();
case TypeCode.Int64:
return ReadContentAsLong();
case TypeCode.Single:
return ReadContentAsSingle();
case TypeCode.Double:
return ReadContentAsDouble();
case TypeCode.Decimal:
return ReadContentAsDecimal();
case TypeCode.DateTime:
return ReadContentAsDateTime();
case TypeCode.String:
return ReadContentAsString();
case TypeCode.SByte:
return ReadContentAsSignedByte();
case TypeCode.UInt16:
return ReadContentAsUnsignedShort();
case TypeCode.UInt32:
return ReadContentAsUnsignedInt();
case TypeCode.UInt64:
return ReadContentAsUnsignedLong();
case TypeCode.Empty:
case TypeCode.DBNull:
case TypeCode.Object:
default:
if (valueType == Globals.TypeOfByteArray)
return ReadContentAsBase64();
else if (valueType == Globals.TypeOfObject)
return new object();
else if (valueType == Globals.TypeOfTimeSpan)
return ReadContentAsTimeSpan();
else if (valueType == Globals.TypeOfGuid)
return ReadContentAsGuid();
else if (valueType == Globals.TypeOfUri)
return ReadContentAsUri();
else if (valueType == Globals.TypeOfXmlQualifiedName)
return ReadContentAsQName();
break;
}
throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(CreateInvalidPrimitiveTypeException(valueType));
}
internal IDataNode ReadExtensionData(Type valueType)
{
switch (Type.GetTypeCode(valueType))
{
case TypeCode.Boolean:
return new DataNode(ReadContentAsBoolean());
case TypeCode.Char:
return new DataNode(ReadContentAsChar());
case TypeCode.Byte:
return new DataNode(ReadContentAsUnsignedByte());
case TypeCode.Int16:
return new DataNode(ReadContentAsShort());
case TypeCode.Int32:
return new DataNode(ReadContentAsInt());
case TypeCode.Int64:
return new DataNode(ReadContentAsLong());
case TypeCode.Single:
return new DataNode(ReadContentAsSingle());
case TypeCode.Double:
return new DataNode(ReadContentAsDouble());
case TypeCode.Decimal:
return new DataNode(ReadContentAsDecimal());
case TypeCode.DateTime:
return new DataNode(ReadContentAsDateTime());
case TypeCode.String:
return new DataNode(ReadContentAsString());
case TypeCode.SByte:
return new DataNode(ReadContentAsSignedByte());
case TypeCode.UInt16:
return new DataNode(ReadContentAsUnsignedShort());
case TypeCode.UInt32:
return new DataNode(ReadContentAsUnsignedInt());
case TypeCode.UInt64:
return new DataNode(ReadContentAsUnsignedLong());
case TypeCode.Empty:
case TypeCode.DBNull:
case TypeCode.Object:
default:
if (valueType == Globals.TypeOfByteArray)
return new DataNode(ReadContentAsBase64());
else if (valueType == Globals.TypeOfObject)
return new DataNode