1 //------------------------------------------------------------------------------
2 // <copyright file="XmlName.cs" company="Microsoft">
3 // Copyright (c) Microsoft Corporation. All rights reserved.
5 // <owner current="true" primary="true">Microsoft</owner>
6 //------------------------------------------------------------------------------
10 using System.Diagnostics;
11 using System.Xml.Schema;
13 internal class XmlName : IXmlSchemaInfo {
19 internal XmlDocument ownerDoc;
20 internal XmlName next;
22 public static XmlName Create(string prefix, string localName, string ns, int hashCode, XmlDocument ownerDoc, XmlName next, IXmlSchemaInfo schemaInfo) {
23 if (schemaInfo == null) {
24 return new XmlName(prefix, localName, ns, hashCode, ownerDoc, next);
27 return new XmlNameEx(prefix, localName, ns, hashCode, ownerDoc, next, schemaInfo);
31 internal XmlName(string prefix, string localName, string ns, int hashCode, XmlDocument ownerDoc, XmlName next) {
33 this.localName = localName;
36 this.hashCode = hashCode;
37 this.ownerDoc = ownerDoc;
41 public string LocalName {
47 public string NamespaceURI {
53 public string Prefix {
65 public XmlDocument OwnerDocument {
74 Debug.Assert( prefix != null );
75 if ( prefix.Length > 0 ) {
76 if ( localName.Length > 0 ) {
77 string n = string.Concat( prefix, ":", localName );
78 lock ( ownerDoc.NameTable ) {
80 name = ownerDoc.NameTable.Add( n );
91 Debug.Assert( Ref.Equal( name, ownerDoc.NameTable.Get( name ) ) );
97 public virtual XmlSchemaValidity Validity {
99 return XmlSchemaValidity.NotKnown;
103 public virtual bool IsDefault {
109 public virtual bool IsNil {
115 public virtual XmlSchemaSimpleType MemberType {
121 public virtual XmlSchemaType SchemaType {
127 public virtual XmlSchemaElement SchemaElement {
133 public virtual XmlSchemaAttribute SchemaAttribute {
139 public virtual bool Equals(IXmlSchemaInfo schemaInfo) {
140 return schemaInfo == null;
143 public static int GetHashCode(string name) {
146 for (int i = name.Length - 1; i >= 0; i--) {
148 if (ch == ':') break;
149 hashCode += (hashCode << 7) ^ ch;
151 hashCode -= hashCode >> 17;
152 hashCode -= hashCode >> 11;
153 hashCode -= hashCode >> 5;
159 internal sealed class XmlNameEx : XmlName {
161 XmlSchemaSimpleType memberType;
162 XmlSchemaType schemaType;
169 const byte ValidityMask = 0x03;
170 const byte IsDefaultBit = 0x04;
171 const byte IsNilBit = 0x08;
173 internal XmlNameEx(string prefix, string localName, string ns, int hashCode, XmlDocument ownerDoc, XmlName next, IXmlSchemaInfo schemaInfo) : base(prefix, localName, ns, hashCode, ownerDoc, next) {
174 SetValidity(schemaInfo.Validity);
175 SetIsDefault(schemaInfo.IsDefault);
176 SetIsNil(schemaInfo.IsNil);
177 memberType = schemaInfo.MemberType;
178 schemaType = schemaInfo.SchemaType;
179 decl = schemaInfo.SchemaElement != null
180 ? (object)schemaInfo.SchemaElement
181 : (object)schemaInfo.SchemaAttribute;
184 public override XmlSchemaValidity Validity {
186 return ownerDoc.CanReportValidity ? (XmlSchemaValidity)(flags & ValidityMask) : XmlSchemaValidity.NotKnown;
190 public override bool IsDefault {
192 return (flags & IsDefaultBit) != 0;
196 public override bool IsNil {
198 return (flags & IsNilBit) != 0;
202 public override XmlSchemaSimpleType MemberType {
208 public override XmlSchemaType SchemaType {
214 public override XmlSchemaElement SchemaElement {
216 return decl as XmlSchemaElement;
220 public override XmlSchemaAttribute SchemaAttribute {
222 return decl as XmlSchemaAttribute;
226 public void SetValidity(XmlSchemaValidity value) {
227 flags = (byte)((flags & ~ValidityMask) | (byte)(value));
230 public void SetIsDefault(bool value) {
231 if (value) flags = (byte)(flags | IsDefaultBit);
232 else flags = (byte)(flags & ~IsDefaultBit);
235 public void SetIsNil(bool value) {
236 if (value) flags = (byte)(flags | IsNilBit);
237 else flags = (byte)(flags & ~IsNilBit);
240 public override bool Equals(IXmlSchemaInfo schemaInfo) {
241 if (schemaInfo != null
242 && schemaInfo.Validity == (XmlSchemaValidity)(flags & ValidityMask)
243 && schemaInfo.IsDefault == ((flags & IsDefaultBit) != 0)
244 && schemaInfo.IsNil == ((flags & IsNilBit) != 0)
245 && (object)schemaInfo.MemberType == (object)memberType
246 && (object)schemaInfo.SchemaType == (object)schemaType
247 && (object)schemaInfo.SchemaElement == (object)(decl as XmlSchemaElement)
248 && (object)schemaInfo.SchemaAttribute == (object)(decl as XmlSchemaAttribute)) {