Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / fx / src / Xml / System / Xml / schema / SchemaInfo.cs / 1305376 / SchemaInfo.cs
//------------------------------------------------------------------------------ //// Copyright (c) Microsoft Corporation. All rights reserved. // //[....] //----------------------------------------------------------------------------- using System; using System.Xml; using System.Diagnostics; using System.Collections.Generic; namespace System.Xml.Schema { #if !SILVERLIGHT internal enum AttributeMatchState { AttributeFound, AnyIdAttributeFound, UndeclaredElementAndAttribute, UndeclaredAttribute, AnyAttributeLax, AnyAttributeSkip, ProhibitedAnyAttribute, ProhibitedAttribute, AttributeNameMismatch, ValidateAttributeInvalidCall, } #endif internal class SchemaInfo : IDtdInfo { DictionaryelementDecls = new Dictionary (); Dictionary undeclaredElementDecls = new Dictionary (); Dictionary generalEntities; Dictionary parameterEntities; XmlQualifiedName docTypeName = XmlQualifiedName.Empty; string internalDtdSubset = string.Empty; bool hasNonCDataAttributes = false; bool hasDefaultAttributes = false; #if !SILVERLIGHT Dictionary targetNamespaces = new Dictionary (); Dictionary attributeDecls = new Dictionary (); int errorCount; SchemaType schemaType; Dictionary elementDeclsByType = new Dictionary (); Dictionary notations; #endif internal SchemaInfo() { #if !SILVERLIGHT schemaType = SchemaType.None; #endif } public XmlQualifiedName DocTypeName { get { return docTypeName; } set { docTypeName = value; } } internal string InternalDtdSubset { get { return internalDtdSubset; } set { internalDtdSubset = value; } } internal Dictionary ElementDecls { get { return elementDecls; } } internal Dictionary UndeclaredElementDecls { get { return undeclaredElementDecls; } } internal Dictionary GeneralEntities { get { if (this.generalEntities == null) { this.generalEntities = new Dictionary (); } return this.generalEntities; } } internal Dictionary ParameterEntities { get { if (this.parameterEntities == null) { this.parameterEntities = new Dictionary (); } return this.parameterEntities; } } #if !SILVERLIGHT internal SchemaType SchemaType { get { return schemaType;} set { schemaType = value;} } internal Dictionary TargetNamespaces { get { return targetNamespaces; } } internal Dictionary ElementDeclsByType { get { return elementDeclsByType; } } internal Dictionary AttributeDecls { get { return attributeDecls; } } internal Dictionary Notations { get { if (this.notations == null) { this.notations = new Dictionary (); } return this.notations; } } internal int ErrorCount { get { return errorCount; } set { errorCount = value; } } internal SchemaElementDecl GetElementDecl(XmlQualifiedName qname) { SchemaElementDecl elemDecl; if (elementDecls.TryGetValue(qname, out elemDecl)) { return elemDecl; } return null; } internal SchemaElementDecl GetTypeDecl(XmlQualifiedName qname) { SchemaElementDecl elemDecl; if (elementDeclsByType.TryGetValue(qname, out elemDecl)) { return elemDecl; } return null; } internal XmlSchemaElement GetElement(XmlQualifiedName qname) { SchemaElementDecl ed = GetElementDecl(qname); if (ed != null) { return ed.SchemaElement; } return null; } internal XmlSchemaAttribute GetAttribute(XmlQualifiedName qname) { SchemaAttDef attdef = (SchemaAttDef)attributeDecls[qname]; if (attdef != null) { return attdef.SchemaAttribute; } return null; } internal XmlSchemaElement GetType(XmlQualifiedName qname) { SchemaElementDecl ed = GetElementDecl(qname); if (ed != null) { return ed.SchemaElement; } return null; } internal bool HasSchema(string ns) { return targetNamespaces.ContainsKey(ns); } internal bool Contains(string ns) { return targetNamespaces.ContainsKey(ns); } internal SchemaAttDef GetAttributeXdr(SchemaElementDecl ed, XmlQualifiedName qname) { SchemaAttDef attdef = null; if (ed != null) { attdef = ed.GetAttDef(qname);; if (attdef == null) { if (!ed.ContentValidator.IsOpen || qname.Namespace.Length == 0) { throw new XmlSchemaException(Res.Sch_UndeclaredAttribute, qname.ToString()); } if (!attributeDecls.TryGetValue(qname, out attdef) && targetNamespaces.ContainsKey(qname.Namespace)) { throw new XmlSchemaException(Res.Sch_UndeclaredAttribute, qname.ToString()); } } } return attdef; } internal SchemaAttDef GetAttributeXsd(SchemaElementDecl ed, XmlQualifiedName qname, XmlSchemaObject partialValidationType, out AttributeMatchState attributeMatchState) { SchemaAttDef attdef = null; attributeMatchState = AttributeMatchState.UndeclaredAttribute; if (ed != null) { attdef = ed.GetAttDef(qname); if (attdef != null) { attributeMatchState = AttributeMatchState.AttributeFound; return attdef; } XmlSchemaAnyAttribute any = ed.AnyAttribute; if (any != null) { if (!any.NamespaceList.Allows(qname)) { attributeMatchState = AttributeMatchState.ProhibitedAnyAttribute; } else if (any.ProcessContentsCorrect != XmlSchemaContentProcessing.Skip) { if (attributeDecls.TryGetValue(qname, out attdef)) { if (attdef.Datatype.TypeCode == XmlTypeCode.Id) { //anyAttribute match whose type is ID attributeMatchState = AttributeMatchState.AnyIdAttributeFound; } else { attributeMatchState = AttributeMatchState.AttributeFound; } } else if (any.ProcessContentsCorrect == XmlSchemaContentProcessing.Lax) { attributeMatchState = AttributeMatchState.AnyAttributeLax; } } else { attributeMatchState = AttributeMatchState.AnyAttributeSkip; } } else if (ed.ProhibitedAttributes.ContainsKey(qname)) { attributeMatchState = AttributeMatchState.ProhibitedAttribute; } } else if (partialValidationType != null) { XmlSchemaAttribute attr = partialValidationType as XmlSchemaAttribute; if (attr != null) { if (qname.Equals(attr.QualifiedName)) { attdef = attr.AttDef; attributeMatchState = AttributeMatchState.AttributeFound; } else { attributeMatchState = AttributeMatchState.AttributeNameMismatch; } } else { attributeMatchState = AttributeMatchState.ValidateAttributeInvalidCall; } } else { if (attributeDecls.TryGetValue(qname, out attdef)) { attributeMatchState = AttributeMatchState.AttributeFound; } else { attributeMatchState = AttributeMatchState.UndeclaredElementAndAttribute; } } return attdef; } internal SchemaAttDef GetAttributeXsd(SchemaElementDecl ed, XmlQualifiedName qname, ref bool skip) { AttributeMatchState attributeMatchState; SchemaAttDef attDef = GetAttributeXsd(ed, qname, null, out attributeMatchState); switch(attributeMatchState) { case AttributeMatchState.UndeclaredAttribute: throw new XmlSchemaException(Res.Sch_UndeclaredAttribute, qname.ToString()); case AttributeMatchState.ProhibitedAnyAttribute: case AttributeMatchState.ProhibitedAttribute: throw new XmlSchemaException(Res.Sch_ProhibitedAttribute, qname.ToString()); case AttributeMatchState.AttributeFound: case AttributeMatchState.AnyIdAttributeFound: case AttributeMatchState.AnyAttributeLax: case AttributeMatchState.UndeclaredElementAndAttribute: break; case AttributeMatchState.AnyAttributeSkip: skip = true; break; default: Debug.Assert(false); break; } return attDef; } internal void Add(SchemaInfo sinfo, ValidationEventHandler eventhandler) { if (schemaType == SchemaType.None) { schemaType = sinfo.SchemaType; } else if (schemaType != sinfo.SchemaType) { if (eventhandler != null) { eventhandler(this, new ValidationEventArgs(new XmlSchemaException(Res.Sch_MixSchemaTypes, string.Empty))); } return; } foreach(string tns in sinfo.TargetNamespaces.Keys) { if (!targetNamespaces.ContainsKey(tns)) { targetNamespaces.Add(tns, true); } } foreach(KeyValuePair entry in sinfo.elementDecls) { if (!elementDecls.ContainsKey(entry.Key)) { elementDecls.Add(entry.Key, entry.Value); } } foreach(KeyValuePair entry in sinfo.elementDeclsByType) { if (!elementDeclsByType.ContainsKey(entry.Key)) { elementDeclsByType.Add(entry.Key, entry.Value); } } foreach (SchemaAttDef attdef in sinfo.AttributeDecls.Values) { if (!attributeDecls.ContainsKey(attdef.Name)) { attributeDecls.Add(attdef.Name, attdef); } } foreach (SchemaNotation notation in sinfo.Notations.Values) { if (!Notations.ContainsKey(notation.Name.Name)) { Notations.Add(notation.Name.Name, notation); } } } #endif internal void Finish() { Dictionary elements = elementDecls; for ( int i = 0; i < 2; i++ ) { foreach ( SchemaElementDecl e in elements.Values ) { if ( e.HasNonCDataAttribute ) { hasNonCDataAttributes = true; } if ( e.DefaultAttDefs != null ) { hasDefaultAttributes = true; } } elements = undeclaredElementDecls; } } // // IDtdInfo interface // #region IDtdInfo Members bool IDtdInfo.HasDefaultAttributes { get { return hasDefaultAttributes; } } bool IDtdInfo.HasNonCDataAttributes { get { return hasNonCDataAttributes; } } IDtdAttributeListInfo IDtdInfo.LookupAttributeList(string prefix, string localName) { XmlQualifiedName qname = new XmlQualifiedName(prefix, localName); SchemaElementDecl elementDecl; if (!elementDecls.TryGetValue(qname, out elementDecl)) { undeclaredElementDecls.TryGetValue(qname, out elementDecl); } return elementDecl; } IEnumerable IDtdInfo.GetAttributeLists() { foreach (SchemaElementDecl elemDecl in elementDecls.Values) { IDtdAttributeListInfo eleDeclAsAttList = (IDtdAttributeListInfo)elemDecl; yield return eleDeclAsAttList; } } IDtdEntityInfo IDtdInfo.LookupEntity(string name) { if (generalEntities == null) { return null; } XmlQualifiedName qname = new XmlQualifiedName(name); SchemaEntity entity; if (generalEntities.TryGetValue(qname, out entity)) { return entity; } return null; } XmlQualifiedName IDtdInfo.Name { get { return docTypeName; } } string IDtdInfo.InternalDtdSubset { get { return internalDtdSubset; } } #endregion } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. //------------------------------------------------------------------------------ // // Copyright (c) Microsoft Corporation. All rights reserved. // //[....] //----------------------------------------------------------------------------- using System; using System.Xml; using System.Diagnostics; using System.Collections.Generic; namespace System.Xml.Schema { #if !SILVERLIGHT internal enum AttributeMatchState { AttributeFound, AnyIdAttributeFound, UndeclaredElementAndAttribute, UndeclaredAttribute, AnyAttributeLax, AnyAttributeSkip, ProhibitedAnyAttribute, ProhibitedAttribute, AttributeNameMismatch, ValidateAttributeInvalidCall, } #endif internal class SchemaInfo : IDtdInfo { DictionaryelementDecls = new Dictionary (); Dictionary undeclaredElementDecls = new Dictionary (); Dictionary generalEntities; Dictionary parameterEntities; XmlQualifiedName docTypeName = XmlQualifiedName.Empty; string internalDtdSubset = string.Empty; bool hasNonCDataAttributes = false; bool hasDefaultAttributes = false; #if !SILVERLIGHT Dictionary targetNamespaces = new Dictionary (); Dictionary attributeDecls = new Dictionary (); int errorCount; SchemaType schemaType; Dictionary elementDeclsByType = new Dictionary (); Dictionary notations; #endif internal SchemaInfo() { #if !SILVERLIGHT schemaType = SchemaType.None; #endif } public XmlQualifiedName DocTypeName { get { return docTypeName; } set { docTypeName = value; } } internal string InternalDtdSubset { get { return internalDtdSubset; } set { internalDtdSubset = value; } } internal Dictionary ElementDecls { get { return elementDecls; } } internal Dictionary UndeclaredElementDecls { get { return undeclaredElementDecls; } } internal Dictionary GeneralEntities { get { if (this.generalEntities == null) { this.generalEntities = new Dictionary (); } return this.generalEntities; } } internal Dictionary ParameterEntities { get { if (this.parameterEntities == null) { this.parameterEntities = new Dictionary (); } return this.parameterEntities; } } #if !SILVERLIGHT internal SchemaType SchemaType { get { return schemaType;} set { schemaType = value;} } internal Dictionary TargetNamespaces { get { return targetNamespaces; } } internal Dictionary ElementDeclsByType { get { return elementDeclsByType; } } internal Dictionary AttributeDecls { get { return attributeDecls; } } internal Dictionary Notations { get { if (this.notations == null) { this.notations = new Dictionary (); } return this.notations; } } internal int ErrorCount { get { return errorCount; } set { errorCount = value; } } internal SchemaElementDecl GetElementDecl(XmlQualifiedName qname) { SchemaElementDecl elemDecl; if (elementDecls.TryGetValue(qname, out elemDecl)) { return elemDecl; } return null; } internal SchemaElementDecl GetTypeDecl(XmlQualifiedName qname) { SchemaElementDecl elemDecl; if (elementDeclsByType.TryGetValue(qname, out elemDecl)) { return elemDecl; } return null; } internal XmlSchemaElement GetElement(XmlQualifiedName qname) { SchemaElementDecl ed = GetElementDecl(qname); if (ed != null) { return ed.SchemaElement; } return null; } internal XmlSchemaAttribute GetAttribute(XmlQualifiedName qname) { SchemaAttDef attdef = (SchemaAttDef)attributeDecls[qname]; if (attdef != null) { return attdef.SchemaAttribute; } return null; } internal XmlSchemaElement GetType(XmlQualifiedName qname) { SchemaElementDecl ed = GetElementDecl(qname); if (ed != null) { return ed.SchemaElement; } return null; } internal bool HasSchema(string ns) { return targetNamespaces.ContainsKey(ns); } internal bool Contains(string ns) { return targetNamespaces.ContainsKey(ns); } internal SchemaAttDef GetAttributeXdr(SchemaElementDecl ed, XmlQualifiedName qname) { SchemaAttDef attdef = null; if (ed != null) { attdef = ed.GetAttDef(qname);; if (attdef == null) { if (!ed.ContentValidator.IsOpen || qname.Namespace.Length == 0) { throw new XmlSchemaException(Res.Sch_UndeclaredAttribute, qname.ToString()); } if (!attributeDecls.TryGetValue(qname, out attdef) && targetNamespaces.ContainsKey(qname.Namespace)) { throw new XmlSchemaException(Res.Sch_UndeclaredAttribute, qname.ToString()); } } } return attdef; } internal SchemaAttDef GetAttributeXsd(SchemaElementDecl ed, XmlQualifiedName qname, XmlSchemaObject partialValidationType, out AttributeMatchState attributeMatchState) { SchemaAttDef attdef = null; attributeMatchState = AttributeMatchState.UndeclaredAttribute; if (ed != null) { attdef = ed.GetAttDef(qname); if (attdef != null) { attributeMatchState = AttributeMatchState.AttributeFound; return attdef; } XmlSchemaAnyAttribute any = ed.AnyAttribute; if (any != null) { if (!any.NamespaceList.Allows(qname)) { attributeMatchState = AttributeMatchState.ProhibitedAnyAttribute; } else if (any.ProcessContentsCorrect != XmlSchemaContentProcessing.Skip) { if (attributeDecls.TryGetValue(qname, out attdef)) { if (attdef.Datatype.TypeCode == XmlTypeCode.Id) { //anyAttribute match whose type is ID attributeMatchState = AttributeMatchState.AnyIdAttributeFound; } else { attributeMatchState = AttributeMatchState.AttributeFound; } } else if (any.ProcessContentsCorrect == XmlSchemaContentProcessing.Lax) { attributeMatchState = AttributeMatchState.AnyAttributeLax; } } else { attributeMatchState = AttributeMatchState.AnyAttributeSkip; } } else if (ed.ProhibitedAttributes.ContainsKey(qname)) { attributeMatchState = AttributeMatchState.ProhibitedAttribute; } } else if (partialValidationType != null) { XmlSchemaAttribute attr = partialValidationType as XmlSchemaAttribute; if (attr != null) { if (qname.Equals(attr.QualifiedName)) { attdef = attr.AttDef; attributeMatchState = AttributeMatchState.AttributeFound; } else { attributeMatchState = AttributeMatchState.AttributeNameMismatch; } } else { attributeMatchState = AttributeMatchState.ValidateAttributeInvalidCall; } } else { if (attributeDecls.TryGetValue(qname, out attdef)) { attributeMatchState = AttributeMatchState.AttributeFound; } else { attributeMatchState = AttributeMatchState.UndeclaredElementAndAttribute; } } return attdef; } internal SchemaAttDef GetAttributeXsd(SchemaElementDecl ed, XmlQualifiedName qname, ref bool skip) { AttributeMatchState attributeMatchState; SchemaAttDef attDef = GetAttributeXsd(ed, qname, null, out attributeMatchState); switch(attributeMatchState) { case AttributeMatchState.UndeclaredAttribute: throw new XmlSchemaException(Res.Sch_UndeclaredAttribute, qname.ToString()); case AttributeMatchState.ProhibitedAnyAttribute: case AttributeMatchState.ProhibitedAttribute: throw new XmlSchemaException(Res.Sch_ProhibitedAttribute, qname.ToString()); case AttributeMatchState.AttributeFound: case AttributeMatchState.AnyIdAttributeFound: case AttributeMatchState.AnyAttributeLax: case AttributeMatchState.UndeclaredElementAndAttribute: break; case AttributeMatchState.AnyAttributeSkip: skip = true; break; default: Debug.Assert(false); break; } return attDef; } internal void Add(SchemaInfo sinfo, ValidationEventHandler eventhandler) { if (schemaType == SchemaType.None) { schemaType = sinfo.SchemaType; } else if (schemaType != sinfo.SchemaType) { if (eventhandler != null) { eventhandler(this, new ValidationEventArgs(new XmlSchemaException(Res.Sch_MixSchemaTypes, string.Empty))); } return; } foreach(string tns in sinfo.TargetNamespaces.Keys) { if (!targetNamespaces.ContainsKey(tns)) { targetNamespaces.Add(tns, true); } } foreach(KeyValuePair entry in sinfo.elementDecls) { if (!elementDecls.ContainsKey(entry.Key)) { elementDecls.Add(entry.Key, entry.Value); } } foreach(KeyValuePair entry in sinfo.elementDeclsByType) { if (!elementDeclsByType.ContainsKey(entry.Key)) { elementDeclsByType.Add(entry.Key, entry.Value); } } foreach (SchemaAttDef attdef in sinfo.AttributeDecls.Values) { if (!attributeDecls.ContainsKey(attdef.Name)) { attributeDecls.Add(attdef.Name, attdef); } } foreach (SchemaNotation notation in sinfo.Notations.Values) { if (!Notations.ContainsKey(notation.Name.Name)) { Notations.Add(notation.Name.Name, notation); } } } #endif internal void Finish() { Dictionary elements = elementDecls; for ( int i = 0; i < 2; i++ ) { foreach ( SchemaElementDecl e in elements.Values ) { if ( e.HasNonCDataAttribute ) { hasNonCDataAttributes = true; } if ( e.DefaultAttDefs != null ) { hasDefaultAttributes = true; } } elements = undeclaredElementDecls; } } // // IDtdInfo interface // #region IDtdInfo Members bool IDtdInfo.HasDefaultAttributes { get { return hasDefaultAttributes; } } bool IDtdInfo.HasNonCDataAttributes { get { return hasNonCDataAttributes; } } IDtdAttributeListInfo IDtdInfo.LookupAttributeList(string prefix, string localName) { XmlQualifiedName qname = new XmlQualifiedName(prefix, localName); SchemaElementDecl elementDecl; if (!elementDecls.TryGetValue(qname, out elementDecl)) { undeclaredElementDecls.TryGetValue(qname, out elementDecl); } return elementDecl; } IEnumerable IDtdInfo.GetAttributeLists() { foreach (SchemaElementDecl elemDecl in elementDecls.Values) { IDtdAttributeListInfo eleDeclAsAttList = (IDtdAttributeListInfo)elemDecl; yield return eleDeclAsAttList; } } IDtdEntityInfo IDtdInfo.LookupEntity(string name) { if (generalEntities == null) { return null; } XmlQualifiedName qname = new XmlQualifiedName(name); SchemaEntity entity; if (generalEntities.TryGetValue(qname, out entity)) { return entity; } return null; } XmlQualifiedName IDtdInfo.Name { get { return docTypeName; } } string IDtdInfo.InternalDtdSubset { get { return internalDtdSubset; } } #endregion } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007.
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- EncodingFallbackAwareXmlTextWriter.cs
- IntSecurity.cs
- CodeTypeParameterCollection.cs
- BufferAllocator.cs
- SystemPens.cs
- AsymmetricKeyExchangeFormatter.cs
- TransformFinalBlockRequest.cs
- CheckedListBox.cs
- GuidConverter.cs
- ADMembershipUser.cs
- KeyBinding.cs
- ParameterElementCollection.cs
- UnicodeEncoding.cs
- QilGenerator.cs
- ParentUndoUnit.cs
- WindowPatternIdentifiers.cs
- BitmapCacheBrush.cs
- BindingNavigator.cs
- ObjRef.cs
- Win32.cs
- BeginGetFileNameFromUserRequest.cs
- StorageEndPropertyMapping.cs
- SafeLibraryHandle.cs
- EmptyStringExpandableObjectConverter.cs
- Highlights.cs
- BooleanProjectedSlot.cs
- ColorPalette.cs
- AsymmetricSignatureDeformatter.cs
- FontInfo.cs
- WebPartConnectionsConnectVerb.cs
- XsdDateTime.cs
- ListBoxAutomationPeer.cs
- SqlConnectionManager.cs
- X509Chain.cs
- Char.cs
- FamilyCollection.cs
- SettingsSavedEventArgs.cs
- ExceptionUtil.cs
- UserPrincipalNameElement.cs
- ClientTargetSection.cs
- SequentialUshortCollection.cs
- CompressEmulationStream.cs
- TextBoxAutomationPeer.cs
- Graphics.cs
- ComponentRenameEvent.cs
- X509CertificateTrustedIssuerElement.cs
- IconConverter.cs
- TimeStampChecker.cs
- TypeKeyValue.cs
- IconHelper.cs
- PenLineCapValidation.cs
- HandleExceptionArgs.cs
- IChannel.cs
- DeclarativeCatalogPart.cs
- TextTreeDeleteContentUndoUnit.cs
- safemediahandle.cs
- XmlSerializerNamespaces.cs
- CustomAttributeBuilder.cs
- EntitySetBaseCollection.cs
- SpellerHighlightLayer.cs
- Listbox.cs
- DecoderReplacementFallback.cs
- XmlMemberMapping.cs
- ResourcePermissionBase.cs
- GlyphRun.cs
- SqlExpressionNullability.cs
- LocatorPart.cs
- LambdaCompiler.Generated.cs
- CodeDomLoader.cs
- BitmapEffectDrawingContent.cs
- InfoCardProofToken.cs
- XmlElementList.cs
- DataServiceHostFactory.cs
- VirtualDirectoryMappingCollection.cs
- CookieParameter.cs
- PathSegment.cs
- DispatcherBuilder.cs
- CustomDictionarySources.cs
- IDispatchConstantAttribute.cs
- PointKeyFrameCollection.cs
- HttpCapabilitiesBase.cs
- Page.cs
- FaultCode.cs
- DataColumnMappingCollection.cs
- SeekableReadStream.cs
- XamlBrushSerializer.cs
- MultiSelectRootGridEntry.cs
- PageEventArgs.cs
- ClipboardProcessor.cs
- MatchSingleFxEngineOpcode.cs
- SecUtil.cs
- ObjectParameter.cs
- HtmlInputReset.cs
- IImplicitResourceProvider.cs
- BatchParser.cs
- ReflectionHelper.cs
- LoginUtil.cs
- DataFieldConverter.cs
- SymmetricCryptoHandle.cs
- LiteralTextParser.cs