1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
|
//------------------------------------------------------------------------------
// <copyright file="SchemaInfo.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
// <owner current="true" primary="true">Microsoft</owner>
//------------------------------------------------------------------------------
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 {
Dictionary<XmlQualifiedName, SchemaElementDecl> elementDecls = new Dictionary<XmlQualifiedName, SchemaElementDecl>();
Dictionary<XmlQualifiedName, SchemaElementDecl> undeclaredElementDecls = new Dictionary<XmlQualifiedName, SchemaElementDecl>();
Dictionary<XmlQualifiedName, SchemaEntity> generalEntities;
Dictionary<XmlQualifiedName, SchemaEntity> parameterEntities;
XmlQualifiedName docTypeName = XmlQualifiedName.Empty;
string internalDtdSubset = string.Empty;
bool hasNonCDataAttributes = false;
bool hasDefaultAttributes = false;
#if !SILVERLIGHT
Dictionary<string, bool> targetNamespaces = new Dictionary<string, bool>();
Dictionary<XmlQualifiedName, SchemaAttDef> attributeDecls = new Dictionary<XmlQualifiedName, SchemaAttDef>();
int errorCount;
SchemaType schemaType;
Dictionary<XmlQualifiedName, SchemaElementDecl> elementDeclsByType = new Dictionary<XmlQualifiedName, SchemaElementDecl>();
Dictionary<string, SchemaNotation> 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<XmlQualifiedName, SchemaElementDecl> ElementDecls {
get { return elementDecls; }
}
internal Dictionary<XmlQualifiedName, SchemaElementDecl> UndeclaredElementDecls {
get { return undeclaredElementDecls; }
}
internal Dictionary<XmlQualifiedName, SchemaEntity> GeneralEntities {
get {
if (this.generalEntities == null) {
this.generalEntities = new Dictionary<XmlQualifiedName, SchemaEntity>();
}
return this.generalEntities;
}
}
internal Dictionary<XmlQualifiedName, SchemaEntity> ParameterEntities {
get {
if (this.parameterEntities == null) {
this.parameterEntities = new Dictionary<XmlQualifiedName, SchemaEntity>();
}
return this.parameterEntities;
}
}
#if !SILVERLIGHT
internal SchemaType SchemaType {
get { return schemaType;}
set { schemaType = value;}
}
internal Dictionary<string, bool> TargetNamespaces {
get { return targetNamespaces; }
}
internal Dictionary<XmlQualifiedName, SchemaElementDecl> ElementDeclsByType {
get { return elementDeclsByType; }
}
internal Dictionary<XmlQualifiedName, SchemaAttDef> AttributeDecls {
get { return attributeDecls; }
}
internal Dictionary<string, SchemaNotation> Notations {
get {
if (this.notations == null) {
this.notations = new Dictionary<string, SchemaNotation>();
}
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<XmlQualifiedName, SchemaElementDecl> entry in sinfo.elementDecls) {
if (!elementDecls.ContainsKey(entry.Key)) {
elementDecls.Add(entry.Key, entry.Value);
}
}
foreach(KeyValuePair<XmlQualifiedName, SchemaElementDecl> 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<XmlQualifiedName, SchemaElementDecl> 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<IDtdAttributeListInfo> 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
}
}
|