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 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368
|
//------------------------------------------------------------------------------
// <copyright file="XmlSchemaSet.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
// <owner current="true" primary="true">Microsoft</owner>
//------------------------------------------------------------------------------
using System.Diagnostics;
using System.Collections;
using System.Threading;
using System.Collections.Generic;
using System.Runtime.Versioning;
namespace System.Xml.Schema {
#if SILVERLIGHT
public class XmlSchemaSet
{
//Empty XmlSchemaSet class to enable backward compatibility of XmlSchemaProvideAttribute
//Add private ctor to prevent constructing of this class
XmlSchemaSet() { }
}
#else
/// <include file='doc\XmlSchemaSet.uex' path='docs/doc[@for="XmlSchemaSet"]/*' />
/// <devdoc>
/// <para>The XmlSchemaSet contains a set of namespace URI's.
/// Each namespace also have an associated private data cache
/// corresponding to the XML-Data Schema or W3C XML Schema.
/// The XmlSchemaSet will able to load only XSD schemas,
/// and compile them into an internal "cooked schema representation".
/// The Validate method then uses this internal representation for
/// efficient runtime validation of any given subtree.</para>
/// </devdoc>
public class XmlSchemaSet {
XmlNameTable nameTable;
SchemaNames schemaNames;
SortedList schemas; // List of source schemas
//Event handling
ValidationEventHandler internalEventHandler;
ValidationEventHandler eventHandler;
bool isCompiled = false;
//Dictionary<Uri, XmlSchema> schemaLocations;
//Dictionary<ChameleonKey, XmlSchema> chameleonSchemas;
Hashtable schemaLocations;
Hashtable chameleonSchemas;
Hashtable targetNamespaces;
bool compileAll;
//Cached Compiled Info
SchemaInfo cachedCompiledInfo;
//Reader settings to parse schema
XmlReaderSettings readerSettings;
XmlSchema schemaForSchema; //Only one schema for schema per set
//Schema compilation settings
XmlSchemaCompilationSettings compilationSettings;
internal XmlSchemaObjectTable elements;
internal XmlSchemaObjectTable attributes;
internal XmlSchemaObjectTable schemaTypes;
internal XmlSchemaObjectTable substitutionGroups;
private XmlSchemaObjectTable typeExtensions;
//Thread safety
private Object internalSyncObject;
internal Object InternalSyncObject {
get {
if (internalSyncObject == null) {
Object o = new Object();
Interlocked.CompareExchange<Object>(ref internalSyncObject, o, null);
}
return internalSyncObject;
}
}
//Constructors
/// <include file='doc\XmlSchemaSet.uex' path='docs/doc[@for="XmlSchemaSet.XmlSchemaSet"]/*' />
/// <devdoc>
/// <para>Construct a new empty schema schemas.</para>
/// </devdoc>
public XmlSchemaSet() : this(new NameTable()) {
}
/// <include file='doc\XmlSchemaSet.uex' path='docs/doc[@for="XmlSchemaSet.XmlSchemaSet1"]/*' />
/// <devdoc>
/// <para>Construct a new empty schema schemas with associated XmlNameTable.
/// The XmlNameTable is used when loading schemas</para>
/// </devdoc>
public XmlSchemaSet(XmlNameTable nameTable) {
if (nameTable == null) {
throw new ArgumentNullException("nameTable");
}
this.nameTable = nameTable;
schemas = new SortedList();
/*schemaLocations = new Dictionary<Uri, XmlSchema>();
chameleonSchemas = new Dictionary<ChameleonKey, XmlSchema>();*/
schemaLocations = new Hashtable();
chameleonSchemas = new Hashtable();
targetNamespaces = new Hashtable();
internalEventHandler = new ValidationEventHandler(InternalValidationCallback);
eventHandler = internalEventHandler;
readerSettings = new XmlReaderSettings();
// we don't have to check XmlReaderSettings.EnableLegacyXmlSettings() here because the following
// code will return same result either we are running on v4.5 or later
if (readerSettings.GetXmlResolver() == null)
{
// The created resolver will be used in the schema validation only
readerSettings.XmlResolver = new XmlUrlResolver();
readerSettings.IsXmlResolverSet = false;
}
readerSettings.NameTable = nameTable;
readerSettings.DtdProcessing = DtdProcessing.Prohibit;
compilationSettings = new XmlSchemaCompilationSettings();
cachedCompiledInfo = new SchemaInfo();
compileAll = true;
}
//Public Properties
/// <include file='doc\XmlSchemaSet.uex' path='docs/doc[@for="XmlSchemaSet.NameTable"]/*' />
/// <devdoc>
/// <para>The default XmlNameTable used by the XmlSchemaSet when loading new schemas.</para>
/// </devdoc>
public XmlNameTable NameTable {
get { return nameTable;}
}
/// <include file='doc\XmlSchemaSet.uex' path='docs/doc[@for="XmlSchemaSet.ValidationEventHandler"]/*' />
public event ValidationEventHandler ValidationEventHandler {
add {
eventHandler -= internalEventHandler;
eventHandler += value;
if (eventHandler == null) {
eventHandler = internalEventHandler;
}
}
remove {
eventHandler -= value;
if (eventHandler == null) {
eventHandler = internalEventHandler;
}
}
}
/// <include file='doc\XmlSchemaSet.uex' path='docs/doc[@for="XmlSchemaSet.IsCompiled"]/*' />
/// <devdoc>
/// <para>IsCompiled is true when the schema set is in compiled state</para>
/// </devdoc>
public bool IsCompiled {
get {
return isCompiled;
}
}
/// <include file='doc\XmlSchemaSet.uex' path='docs/doc[@for="XmlSchemaSet.XmlResolver"]/*' />
/// <devdoc>
/// <para></para>
/// </devdoc>
public XmlResolver XmlResolver {
set {
readerSettings.XmlResolver = value;
}
}
/// <include file='doc\XmlSchemaSet.uex' path='docs/doc[@for="XmlSchemaSet.CompilationSettings"]/*' />
/// <devdoc>
/// <para></para>
/// </devdoc>
public XmlSchemaCompilationSettings CompilationSettings {
get {
return compilationSettings;
}
set {
compilationSettings = value;
}
}
/// <include file='doc\XmlSchemaSet.uex' path='docs/doc[@for="XmlSchemaSet.Count"]/*' />
/// <devdoc>
/// <para>Returns the count of schemas in the set</para>
/// </devdoc>
public int Count {
get {
return schemas.Count;
}
}
/// <include file='doc\XmlSchemaSet.uex' path='docs/doc[@for="XmlSchemaSet.GlobalElements"]/*' />
/// <devdoc>
/// <para></para>
/// </devdoc>
public XmlSchemaObjectTable GlobalElements {
get {
if (elements == null) {
elements = new XmlSchemaObjectTable();
}
return elements;
}
}
/// <include file='doc\XmlSchemaSet.uex' path='docs/doc[@for="XmlSchemaSet.GlobalAttributes"]/*' />
/// <devdoc>
/// <para></para>
/// </devdoc>
public XmlSchemaObjectTable GlobalAttributes {
get {
if (attributes == null) {
attributes = new XmlSchemaObjectTable();
}
return attributes;
}
}
/// <include file='doc\XmlSchemaSet.uex' path='docs/doc[@for="XmlSchemaSet.GlobalTypes"]/*' />
/// <devdoc>
/// <para></para>
/// </devdoc>
public XmlSchemaObjectTable GlobalTypes {
get {
if (schemaTypes == null) {
schemaTypes = new XmlSchemaObjectTable();
}
return schemaTypes;
}
}
/// <include file='doc\XmlSchemaSet.uex' path='docs/doc[@for="XmlSchemaSet.SubstitutionGroups"]/*' />
/// <devdoc>
/// <para></para>
/// </devdoc>
///
internal XmlSchemaObjectTable SubstitutionGroups {
get {
if (substitutionGroups == null) {
substitutionGroups = new XmlSchemaObjectTable();
}
return substitutionGroups;
}
}
/// <summary>
/// Table of all types extensions
/// </summary>
internal Hashtable SchemaLocations {
get {
return schemaLocations;
}
}
/// <summary>
/// Table of all types extensions
/// </summary>
internal XmlSchemaObjectTable TypeExtensions {
get {
if (typeExtensions == null) {
typeExtensions = new XmlSchemaObjectTable();
}
return typeExtensions;
}
}
//Public Methods
/// <include file='doc\XmlSchemaSet.uex' path='docs/doc[@for="XmlSchemaSet.Add1"]/*' />
/// <devdoc>
/// <para>Add the schema located by the given URL into the schema schemas.
/// If the given schema references other namespaces, the schemas for those other
/// namespaces are NOT automatically loaded.</para>
/// </devdoc>
[ResourceConsumption(ResourceScope.Machine)]
[ResourceExposure(ResourceScope.Machine)]
public XmlSchema Add(String targetNamespace, String schemaUri) {
if (schemaUri == null || schemaUri.Length == 0) {
throw new ArgumentNullException("schemaUri");
}
if (targetNamespace != null) {
targetNamespace = XmlComplianceUtil.CDataNormalize(targetNamespace);
}
XmlSchema schema = null;
lock (InternalSyncObject) {
//Check if schema from url has already been added
XmlResolver tempResolver = readerSettings.GetXmlResolver();
if ( tempResolver == null ) {
tempResolver = new XmlUrlResolver();
}
Uri tempSchemaUri = tempResolver.ResolveUri(null, schemaUri);
if (IsSchemaLoaded(tempSchemaUri, targetNamespace, out schema)) {
return schema;
}
else {
//Url already not processed; Load SOM from url
XmlReader reader = XmlReader.Create(schemaUri, readerSettings);
try {
schema = Add(targetNamespace, ParseSchema(targetNamespace, reader)); //
while(reader.Read());// wellformness check;
}
finally {
reader.Close();
}
}
}
return schema;
}
/// <include file='doc\XmlSchemaSet.uex' path='docs/doc[@for="XmlSchemaSet.Add4"]/*' />
/// <devdoc>
/// <para>Add the given schema into the schema schemas.
/// If the given schema references other namespaces, the schemas for those
/// other namespaces are NOT automatically loaded.</para>
/// </devdoc>
public XmlSchema Add(String targetNamespace, XmlReader schemaDocument) {
if (schemaDocument == null) {
throw new ArgumentNullException("schemaDocument");
}
if (targetNamespace != null) {
targetNamespace = XmlComplianceUtil.CDataNormalize(targetNamespace);
}
lock (InternalSyncObject) {
XmlSchema schema = null;
Uri schemaUri = new Uri(schemaDocument.BaseURI, UriKind.RelativeOrAbsolute);
if (IsSchemaLoaded(schemaUri, targetNamespace, out schema)) {
return schema;
}
else {
DtdProcessing dtdProcessing = this.readerSettings.DtdProcessing;
SetDtdProcessing(schemaDocument);
schema = Add(targetNamespace, ParseSchema(targetNamespace, schemaDocument));
this.readerSettings.DtdProcessing = dtdProcessing; //reset dtdProcessing setting
return schema;
}
}
}
/// <include file='doc\XmlSchemaSet.uex' path='docs/doc[@for="XmlSchemaSet.Add5"]/*' />
/// <devdoc>
/// <para>Adds all the namespaces defined in the given schemas
/// (including their associated schemas) to this schemas.</para>
/// </devdoc>
public void Add(XmlSchemaSet schemas) {
if (schemas == null) {
throw new ArgumentNullException("schemas");
}
if (this == schemas) {
return;
}
bool thisLockObtained = false;
bool schemasLockObtained = false;
try {
while(true) {
Monitor.TryEnter(InternalSyncObject, ref thisLockObtained);
if (thisLockObtained) {
Monitor.TryEnter(schemas.InternalSyncObject, ref schemasLockObtained);
if (schemasLockObtained) {
break;
}
else {
Monitor.Exit(InternalSyncObject); //Give up this lock and try both again
thisLockObtained = false;
Thread.Yield(); //Let the thread that holds the lock run
continue;
}
}
}
XmlSchema currentSchema;
//
if (schemas.IsCompiled) {
CopyFromCompiledSet(schemas);
}
else {
bool remove = false;
string tns = null;
foreach(XmlSchema schema in schemas.SortedSchemas.Values) {
tns = schema.TargetNamespace;
if (tns == null) {
tns = string.Empty;
}
if (this.schemas.ContainsKey(schema.SchemaId) || FindSchemaByNSAndUrl(schema.BaseUri, tns, null) != null) { //Do not already existing url
continue;
}
currentSchema = Add(schema.TargetNamespace, schema);
if(currentSchema == null) {
remove = true;
break;
}
}
//Remove all from the set if even one schema in the passed in set is not preprocessed.
if (remove) {
foreach(XmlSchema schema in schemas.SortedSchemas.Values) { //Remove all previously added schemas from the set
this.schemas.Remove(schema.SchemaId); //Might remove schema that was already there and was not added thru this operation
schemaLocations.Remove(schema.BaseUri);
}
}
}
}
finally { //release locks on sets
if (thisLockObtained) {
Monitor.Exit(InternalSyncObject);
}
if (schemasLockObtained) {
Monitor.Exit(schemas.InternalSyncObject);
}
}
}
/// <include file='doc\XmlSchemaSet.uex' path='docs/doc[@for="XmlSchemaSet.Add6"]/*' />
public XmlSchema Add(XmlSchema schema) {
if (schema == null) {
throw new ArgumentNullException("schema");
}
lock (InternalSyncObject) {
if (schemas.ContainsKey(schema.SchemaId)) {
return schema;
}
return Add(schema.TargetNamespace, schema);
}
}
/// <include file='doc\XmlSchemaSet.uex' path='docs/doc[@for="XmlSchemaSet.Remove"]/*' />
public XmlSchema Remove(XmlSchema schema) {
return Remove(schema, true);
}
/// <include file='doc\XmlSchemaSet.uex' path='docs/doc[@for="XmlSchemaSet.RemoveRecursive"]/*' />
public bool RemoveRecursive(XmlSchema schemaToRemove) {
if (schemaToRemove == null) {
throw new ArgumentNullException("schemaToRemove");
}
if (!schemas.ContainsKey(schemaToRemove.SchemaId)) {
return false;
}
lock (InternalSyncObject) { //Need to lock here so that remove cannot be called while the set is being compiled
if (schemas.ContainsKey(schemaToRemove.SchemaId)) { //Need to check again
//Build disallowedNamespaces list
Hashtable disallowedNamespaces = new Hashtable();
disallowedNamespaces.Add(GetTargetNamespace(schemaToRemove), schemaToRemove);
string importedNS;
for (int i = 0; i < schemaToRemove.ImportedNamespaces.Count; i++) {
importedNS = (string)schemaToRemove.ImportedNamespaces[i];
if (disallowedNamespaces[importedNS] == null) {
disallowedNamespaces.Add(importedNS, importedNS);
}
}
//Removal list is all schemas imported by this schema directly or indirectly
//Need to check if other schemas in the set import schemaToRemove / any of its imports
ArrayList needToCheckSchemaList = new ArrayList();
XmlSchema mainSchema;
for (int i =0; i < schemas.Count; i++) {
mainSchema = (XmlSchema)schemas.GetByIndex(i);
if (mainSchema == schemaToRemove ||
schemaToRemove.ImportedSchemas.Contains(mainSchema)) {
continue;
}
needToCheckSchemaList.Add(mainSchema);
}
mainSchema = null;
for (int i = 0; i < needToCheckSchemaList.Count; i++) { //Perf: Not using nested foreach here
mainSchema = (XmlSchema)needToCheckSchemaList[i];
if (mainSchema.ImportedNamespaces.Count > 0) {
foreach(string tns in disallowedNamespaces.Keys) {
if (mainSchema.ImportedNamespaces.Contains(tns)) {
SendValidationEvent(new XmlSchemaException(Res.Sch_SchemaNotRemoved, string.Empty), XmlSeverityType.Warning);
return false;
}
}
}
}
Remove(schemaToRemove, true);
for (int i = 0; i < schemaToRemove.ImportedSchemas.Count; ++i) {
XmlSchema impSchema = (XmlSchema)schemaToRemove.ImportedSchemas[i];
Remove(impSchema, true);
}
return true;
}
}
return false;
}
/// <include file='doc\XmlSchemaSet.uex' path='docs/doc[@for="XmlSchemaSet.Contains1"]/*' />
public bool Contains(String targetNamespace) {
if (targetNamespace == null) {
targetNamespace = string.Empty;
}
return targetNamespaces[targetNamespace] != null;
}
/// <include file='doc\XmlSchemaSet.uex' path='docs/doc[@for="XmlSchemaSet.Contains2"]/*' />
public bool Contains(XmlSchema schema) {
if (schema == null) {
throw new ArgumentNullException("schema");
}
return schemas.ContainsValue(schema);
}
/// <include file='doc\XmlSchemaSet.uex' path='docs/doc[@for="XmlSchemaSet.Compile"]/*' />
/// <devdoc>
/// <para>[To be supplied.]</para>
/// </devdoc>
public void Compile() {
if (isCompiled) {
return;
}
if (schemas.Count == 0) {
ClearTables(); //Clear any previously present compiled state left by calling just Remove() on the set
cachedCompiledInfo = new SchemaInfo();
isCompiled = true;
compileAll = false;
return;
}
lock (InternalSyncObject) {
if (!isCompiled) { //Locking before checking isCompiled to avoid problems with double locking
Compiler compiler = new Compiler(nameTable, eventHandler, schemaForSchema, compilationSettings);
SchemaInfo newCompiledInfo = new SchemaInfo();
int schemaIndex = 0;
if (!compileAll) { //if we are not compiling everything again, Move the pre-compiled schemas to the compiler's tables
compiler.ImportAllCompiledSchemas(this);
}
try { //First thing to do in the try block is to acquire locks since finally will try to release them.
//If we dont accuire the locks first, and an exception occurs in the code before the locking code, then Threading.SynchronizationLockException will be thrown
//when attempting to release it in the finally block
XmlSchema currentSchema;
XmlSchema xmlNSSchema = Preprocessor.GetBuildInSchema();
for (schemaIndex = 0; schemaIndex < schemas.Count; schemaIndex++) {
currentSchema = (XmlSchema)schemas.GetByIndex(schemaIndex);
//Lock schema to be compiled
#pragma warning disable 0618
//@
Monitor.Enter(currentSchema);
#pragma warning restore 0618
if (!currentSchema.IsPreprocessed)
{
SendValidationEvent(new XmlSchemaException(Res.Sch_SchemaNotPreprocessed, string.Empty), XmlSeverityType.Error);
isCompiled = false;
return;
}
if (currentSchema.IsCompiledBySet) {
if (!compileAll) {
continue;
}
else if ((object)currentSchema == (object)xmlNSSchema) { // prepare for xml namespace schema without cleanup
compiler.Prepare(currentSchema, false);
continue;
}
}
compiler.Prepare(currentSchema, true);
}
isCompiled = compiler.Execute(this, newCompiledInfo);
if (isCompiled) {
if (!compileAll) {
newCompiledInfo.Add(cachedCompiledInfo, eventHandler); //Add all the items from the old to the new compiled object
}
compileAll = false;
cachedCompiledInfo = newCompiledInfo; //Replace the compiled info in the set after successful compilation
}
}
finally {
//Release locks on all schemas
XmlSchema currentSchema;
if (schemaIndex == schemas.Count) {
schemaIndex--;
}
for (int i = schemaIndex; i >= 0; i--) {
currentSchema = (XmlSchema)schemas.GetByIndex(i);
if (currentSchema == Preprocessor.GetBuildInSchema()) { //dont re-set compiled flags for xml namespace schema
Monitor.Exit(currentSchema);
continue;
}
currentSchema.IsCompiledBySet = isCompiled;
Monitor.Exit(currentSchema);
}
}
}
}
return;
}
/// <include file='doc\XmlSchemaSet.uex' path='docs/doc[@for="XmlSchemaSet.Reprocess"]/*' />
/// <devdoc>
/// <para>[To be supplied.]</para>
/// </devdoc>
public XmlSchema Reprocess(XmlSchema schema) {
// Due to bug 644477 - this method is tightly coupled (THE CODE IS BASICALLY COPIED) to Remove, Add and AddSchemaToSet
// methods. If you change anything here *make sure* to update Remove/Add/AddSchemaToSet method(s) accordingly.
// The only difference is that we don't touch .schemas collection here to not break a code like this:
// foreach(XmlSchema s in schemaset.schemas) { schemaset.Reprocess(s); }
// This is by purpose.
if (schema == null) {
throw new ArgumentNullException("schema");
}
if (!schemas.ContainsKey(schema.SchemaId)) {
throw new ArgumentException(Res.GetString(Res.Sch_SchemaDoesNotExist), "schema");
}
XmlSchema originalSchema = schema;
lock (InternalSyncObject) { //Lock set so that set cannot be compiled in another thread
// This code is copied from method:
// Remove(XmlSchema schema, bool forceCompile)
// If you changed anything here go and change the same in Remove(XmlSchema schema, bool forceCompile) method
#region Copied from Remove(XmlSchema schema, bool forceCompile)
RemoveSchemaFromGlobalTables(schema);
RemoveSchemaFromCaches(schema);
if (schema.BaseUri != null) {
schemaLocations.Remove(schema.BaseUri);
}
string tns = GetTargetNamespace(schema);
if (Schemas(tns).Count == 0) { //This is the only schema for that namespace
targetNamespaces.Remove(tns);
}
isCompiled = false;
compileAll = true; //Force compilation of the whole set; This is when the set is not completely thread-safe
#endregion //Copied from Remove(XmlSchema schema, bool forceCompile)
// This code is copied from method:
// Add(string targetNamespace, XmlSchema schema)
// If you changed anything here go and change the same in Add(string targetNamespace, XmlSchema schema) method
#region Copied from Add(string targetNamespace, XmlSchema schema)
if (schema.ErrorCount != 0) { //Schema with parsing errors cannot be loaded
return originalSchema;
}
if (PreprocessSchema(ref schema, schema.TargetNamespace)) { //No perf opt for already compiled schemas
// This code is copied from method:
// AddSchemaToSet(XmlSchema schema)
// If you changed anything here go and change the same in AddSchemaToSet(XmlSchema schema) method
#region Copied from AddSchemaToSet(XmlSchema schema)
//Add to targetNamespaces table
if (targetNamespaces[tns] == null) {
targetNamespaces.Add(tns, tns);
}
if (schemaForSchema == null && tns == XmlReservedNs.NsXs && schema.SchemaTypes[DatatypeImplementation.QnAnyType] != null) { //it has xs:anyType
schemaForSchema = schema;
}
for (int i = 0; i < schema.ImportedSchemas.Count; ++i) { //Once preprocessed external schemas property is set
XmlSchema s = (XmlSchema)schema.ImportedSchemas[i];
if (!schemas.ContainsKey(s.SchemaId)) {
schemas.Add(s.SchemaId, s);
}
tns = GetTargetNamespace(s);
if (targetNamespaces[tns] == null) {
targetNamespaces.Add(tns, tns);
}
if (schemaForSchema == null && tns == XmlReservedNs.NsXs && schema.SchemaTypes[DatatypeImplementation.QnAnyType] != null) { //it has xs:anyType
schemaForSchema = schema;
}
}
#endregion //Copied from AddSchemaToSet(XmlSchema schema)
return schema;
}
#endregion // Copied from Add(string targetNamespace, XmlSchema schema)
return originalSchema;
}
}
/// <include file='doc\XmlSchemaSet.uex' path='docs/doc[@for="XmlSchemaSet.CopyTo"]/*' />
/// <devdoc>
/// <para>[To be supplied.]</para>
/// </devdoc>
public void CopyTo(XmlSchema[] schemas, int index) {
if (schemas == null)
throw new ArgumentNullException("schemas");
if (index < 0 || index > schemas.Length -1 )
throw new ArgumentOutOfRangeException("index");
this.schemas.Values.CopyTo(schemas, index);
}
/// <include file='doc\XmlSchemaSet.uex' path='docs/doc[@for="XmlSchemaSet.Schemas1"]/*' />
/// <devdoc>
/// <para>[To be supplied.]</para>
/// </devdoc>
public ICollection Schemas() {
return schemas.Values;
}
/// <include file='doc\XmlSchemaSet.uex' path='docs/doc[@for="XmlSchemaSet.Schemas1"]/*' />
/// <devdoc>
/// <para>[To be supplied.]</para>
/// </devdoc>
public ICollection Schemas(String targetNamespace) {
ArrayList tnsSchemas = new ArrayList();
XmlSchema currentSchema;
if (targetNamespace == null) {
targetNamespace = string.Empty;
}
for (int i=0; i < schemas.Count; i++) {
currentSchema = (XmlSchema)schemas.GetByIndex(i);
if (GetTargetNamespace(currentSchema) == targetNamespace) {
tnsSchemas.Add(currentSchema);
}
}
return tnsSchemas;
}
//Internal Methods
private XmlSchema Add(string targetNamespace, XmlSchema schema) {
// Due to bug 644477 - this method is tightly coupled (THE CODE IS BASICALLY COPIED) to Reprocess
// method. If you change anything here *make sure* to update Reprocess method accordingly.
if (schema == null || schema.ErrorCount != 0) { //Schema with parsing errors cannot be loaded
return null;
}
// This code is copied to method:
// Reprocess(XmlSchema schema)
// If you changed anything here go and change the same in Reprocess(XmlSchema schema) method
if (PreprocessSchema(ref schema, targetNamespace)) { //No perf opt for already compiled schemas
AddSchemaToSet(schema);
isCompiled = false;
return schema;
}
return null;
}
#if TRUST_COMPILE_STATE
private void AddCompiledSchema(XmlSchema schema) {
if (schema.IsCompiledBySet ) { //trust compiled state always if it is not a chameleon schema
VerifyTables();
SchemaInfo newCompiledInfo = new SchemaInfo();
XmlSchemaObjectTable substitutionGroupsTable = null;
if (!AddToCompiledInfo(schema, newCompiledInfo, ref substitutionGroupsTable)) { //Error while adding main schema
return null;
}
foreach (XmlSchema impSchema in schema.ImportedSchemas) {
if (!AddToCompiledInfo(impSchema, newCompiledInfo, ref substitutionGroupsTable)) { //Error while adding imports
return null;
}
}
newCompiledInfo.Add(cachedCompiledInfo, eventHandler); //Add existing compiled info
cachedCompiledInfo = newCompiledInfo;
if (substitutionGroupsTable != null) {
ProcessNewSubstitutionGroups(substitutionGroupsTable, true);
}
if (schemas.Count == 0) { //If its the first compiled schema being added, then set doesnt need to be compiled
isCompiled = true;
compileAll = false;
}
AddSchemaToSet(schema);
return schema;
}
}
private bool AddToCompiledInfo(XmlSchema schema, SchemaInfo newCompiledInfo, ref XmlSchemaObjectTable substTable) {
//Add schema's compiled tables to the set
if (schema.BaseUri != null && schemaLocations[schema.BaseUri] == null) { //Update schemaLocations table
schemaLocations.Add(schema.BaseUri, schema);
}
foreach (XmlSchemaElement element in schema.Elements.Values) {
if(!AddToTable(elements, element.QualifiedName, element)) {
RemoveSchemaFromGlobalTables(schema);
return false;
}
XmlQualifiedName head = element.SubstitutionGroup;
if (!head.IsEmpty) {
if (substTable == null) {
substTable = new XmlSchemaObjectTable();
}
XmlSchemaSubstitutionGroup substitutionGroup = (XmlSchemaSubstitutionGroup)substTable[head];
if (substitutionGroup == null) {
substitutionGroup = new XmlSchemaSubstitutionGroup();
substitutionGroup.Examplar = head;
substTable.Add(head, substitutionGroup);
}
ArrayList members = substitutionGroup.Members;
if (!members.Contains(element)) { //Members might contain element if the same schema is included and imported through different paths. Imp, hence will be added to set directly
members.Add(element);
}
}
}
foreach (XmlSchemaAttribute attribute in schema.Attributes.Values) {
if (!AddToTable(attributes, attribute.QualifiedName, attribute)) {
RemoveSchemaFromGlobalTables(schema);
return false;
}
}
foreach (XmlSchemaType schemaType in schema.SchemaTypes.Values) {
if (!AddToTable(schemaTypes, schemaType.QualifiedName, schemaType)) {
RemoveSchemaFromGlobalTables(schema);
return false;
}
}
schema.AddCompiledInfo(newCompiledInfo);
return true;
}
#endif
//For use by the validator when loading schemaLocations in the instance
internal void Add(String targetNamespace, XmlReader reader, Hashtable validatedNamespaces) {
if (reader == null) {
throw new ArgumentNullException("reader");
}
if (targetNamespace == null) {
targetNamespace = string.Empty;
}
if (validatedNamespaces[targetNamespace] != null) {
if (FindSchemaByNSAndUrl(new Uri(reader.BaseURI, UriKind.RelativeOrAbsolute), targetNamespace, null) != null) {
return;
}
else {
throw new XmlSchemaException(Res.Sch_ComponentAlreadySeenForNS, targetNamespace);
}
}
//Not locking set as this will not be accessible outside the validator
XmlSchema schema;
if (IsSchemaLoaded(new Uri(reader.BaseURI, UriKind.RelativeOrAbsolute), targetNamespace, out schema)) {
return;
}
else { //top-level schema not present for same url
schema = ParseSchema(targetNamespace, reader);
//Store the previous locations
DictionaryEntry[] oldLocations = new DictionaryEntry[schemaLocations.Count];
schemaLocations.CopyTo(oldLocations, 0);
//Add to set
Add(targetNamespace, schema);
if (schema.ImportedSchemas.Count > 0) { //Check imports
string tns;
for (int i = 0; i < schema.ImportedSchemas.Count; ++i) {
XmlSchema impSchema = (XmlSchema)schema.ImportedSchemas[i];
tns = impSchema.TargetNamespace;
if (tns == null) {
tns = string.Empty;
}
if (validatedNamespaces[tns] != null && (FindSchemaByNSAndUrl(impSchema.BaseUri, tns, oldLocations) == null) ) {
RemoveRecursive(schema);
throw new XmlSchemaException(Res.Sch_ComponentAlreadySeenForNS, tns);
}
}
}
}
}
internal XmlSchema FindSchemaByNSAndUrl(Uri schemaUri, string ns, DictionaryEntry[] locationsTable) {
if (schemaUri == null || schemaUri.OriginalString.Length == 0) {
return null;
}
XmlSchema schema = null;
if (locationsTable == null) {
schema = (XmlSchema)schemaLocations[schemaUri];
}
else {
for (int i = 0; i < locationsTable.Length; i++) {
if (schemaUri.Equals(locationsTable[i].Key)) {
schema = (XmlSchema)locationsTable[i].Value;
break;
}
}
}
if (schema != null) {
Debug.Assert(ns != null);
string tns = schema.TargetNamespace == null ? string.Empty : schema.TargetNamespace;
if (tns == ns) {
return schema;
}
else if (tns == string.Empty) { //There could be a chameleon for same ns
// It is OK to pass in the schema we have found so far, since it must have the schemaUri we're looking for
// (we found it that way above) and it must be the original chameleon schema (the one without target ns)
// as we don't add the chameleon copies into the locations tables above.
Debug.Assert(schema.BaseUri.Equals(schemaUri));
ChameleonKey cKey = new ChameleonKey(ns, schema);
schema = (XmlSchema)chameleonSchemas[cKey]; //Need not clone if a schema for that namespace already exists
}
else {
schema = null;
}
}
return schema;
}
private void SetDtdProcessing(XmlReader reader) {
if (reader.Settings != null) {
this.readerSettings.DtdProcessing = reader.Settings.DtdProcessing;
}
else {
XmlTextReader v1Reader = reader as XmlTextReader;
if (v1Reader != null) {
this.readerSettings.DtdProcessing = v1Reader.DtdProcessing;
}
}
}
private void AddSchemaToSet(XmlSchema schema) {
// Due to bug 644477 - this method is tightly coupled (THE CODE IS BASICALLY COPIED) to Reprocess
// method. If you change anything here *make sure* to update Reprocess method accordingly.
schemas.Add(schema.SchemaId, schema);
//Add to targetNamespaces table
// This code is copied to method:
// Reprocess(XmlSchema schema)
// If you changed anything here go and change the same in Reprocess(XmlSchema schema) method
#region This code is copied to Reprocess(XmlSchema schema) method
string tns = GetTargetNamespace(schema);
if (targetNamespaces[tns] == null) {
targetNamespaces.Add(tns, tns);
}
if (schemaForSchema == null && tns == XmlReservedNs.NsXs && schema.SchemaTypes[DatatypeImplementation.QnAnyType] != null) { //it has xs:anyType
schemaForSchema = schema;
}
for (int i = 0; i < schema.ImportedSchemas.Count; ++i) { //Once preprocessed external schemas property is set
XmlSchema s = (XmlSchema)schema.ImportedSchemas[i];
if (!schemas.ContainsKey(s.SchemaId)) {
schemas.Add(s.SchemaId, s);
}
tns = GetTargetNamespace(s);
if (targetNamespaces[tns] == null) {
targetNamespaces.Add(tns, tns);
}
if (schemaForSchema == null && tns == XmlReservedNs.NsXs && schema.SchemaTypes[DatatypeImplementation.QnAnyType] != null) { //it has xs:anyType
schemaForSchema = schema;
}
}
#endregion // This code is copied to Reprocess(XmlSchema schema) method
}
private void ProcessNewSubstitutionGroups(XmlSchemaObjectTable substitutionGroupsTable, bool resolve) {
foreach(XmlSchemaSubstitutionGroup substGroup in substitutionGroupsTable.Values) {
if (resolve) { //Resolve substitutionGroups within this schema
ResolveSubstitutionGroup(substGroup, substitutionGroupsTable);
}
//Add or Merge new substitutionGroups with those that already exist in the set
XmlQualifiedName head = substGroup.Examplar;
XmlSchemaSubstitutionGroup oldSubstGroup = (XmlSchemaSubstitutionGroup)substitutionGroups[head];
if (oldSubstGroup != null) {
for (int i = 0; i < substGroup.Members.Count; ++i) {
if (!oldSubstGroup.Members.Contains(substGroup.Members[i])) {
oldSubstGroup.Members.Add(substGroup.Members[i]);
}
}
}
else {
AddToTable(substitutionGroups, head, substGroup);
}
}
}
private void ResolveSubstitutionGroup(XmlSchemaSubstitutionGroup substitutionGroup, XmlSchemaObjectTable substTable) {
List<XmlSchemaElement> newMembers = null;
XmlSchemaElement headElement = (XmlSchemaElement)elements[substitutionGroup.Examplar];
if (substitutionGroup.Members.Contains(headElement)) {// already checked
return;
}
for (int i = 0; i < substitutionGroup.Members.Count; ++i) {
XmlSchemaElement element = (XmlSchemaElement)substitutionGroup.Members[i];
//Chain to other head's that are members of this head's substGroup
XmlSchemaSubstitutionGroup g = (XmlSchemaSubstitutionGroup)substTable[element.QualifiedName];
if (g != null) {
ResolveSubstitutionGroup(g, substTable);
for (int j = 0; j < g.Members.Count; ++j) {
XmlSchemaElement element1 = (XmlSchemaElement)g.Members[j];
if (element1 != element) { //Exclude the head
if (newMembers == null) {
newMembers = new List<XmlSchemaElement>();
}
newMembers.Add(element1);
}
}
}
}
if (newMembers != null) {
for (int i = 0; i < newMembers.Count; ++i) {
substitutionGroup.Members.Add(newMembers[i]);
}
}
substitutionGroup.Members.Add(headElement);
}
internal XmlSchema Remove(XmlSchema schema, bool forceCompile) {
// Due to bug 644477 - this method is tightly coupled (THE CODE IS BASICALLY COPIED) to Reprocess
// method. If you change anything here *make sure* to update Reprocess method accordingly.
if (schema == null) {
throw new ArgumentNullException("schema");
}
lock (InternalSyncObject) { //Need to lock here so that remove cannot be called while the set is being compiled
if (schemas.ContainsKey(schema.SchemaId)) {
// This code is copied to method:
// Reprocess(XmlSchema schema)
// If you changed anything here go and change the same in Reprocess(XmlSchema schema) method
#region This code is copied to Reprocess(XmlSchema schema) method
if (forceCompile) {
RemoveSchemaFromGlobalTables(schema);
RemoveSchemaFromCaches(schema);
}
schemas.Remove(schema.SchemaId);
if (schema.BaseUri != null) {
schemaLocations.Remove(schema.BaseUri);
}
string tns = GetTargetNamespace(schema);
if (Schemas(tns).Count == 0) { //This is the only schema for that namespace
targetNamespaces.Remove(tns);
}
if (forceCompile) {
isCompiled = false;
compileAll = true; //Force compilation of the whole set; This is when the set is not completely thread-safe
}
return schema;
#endregion // This code is copied to Reprocess(XmlSchema schema) method
}
}
return null;
}
private void ClearTables() {
GlobalElements.Clear();
GlobalAttributes.Clear();
GlobalTypes.Clear();
SubstitutionGroups.Clear();
TypeExtensions.Clear();
}
internal bool PreprocessSchema(ref XmlSchema schema, string targetNamespace) {
Preprocessor prep = new Preprocessor(nameTable, GetSchemaNames(nameTable), eventHandler, compilationSettings);
prep.XmlResolver = readerSettings.GetXmlResolver_CheckConfig();
prep.ReaderSettings = readerSettings;
prep.SchemaLocations = schemaLocations;
prep.ChameleonSchemas = chameleonSchemas;
bool hasErrors = prep.Execute(schema, targetNamespace, true);
schema = prep.RootSchema; //For any root level chameleon cloned
return hasErrors;
}
internal XmlSchema ParseSchema(string targetNamespace, XmlReader reader) {
XmlNameTable readerNameTable = reader.NameTable;
SchemaNames schemaNames = GetSchemaNames(readerNameTable);
Parser parser = new Parser(SchemaType.XSD, readerNameTable, schemaNames, eventHandler);
parser.XmlResolver = readerSettings.GetXmlResolver_CheckConfig();
SchemaType schemaType;
try {
schemaType = parser.Parse(reader, targetNamespace);
}
catch(XmlSchemaException e) {
SendValidationEvent(e, XmlSeverityType.Error);
return null;
}
return parser.XmlSchema;
}
internal void CopyFromCompiledSet(XmlSchemaSet otherSet) {
XmlSchema currentSchema;
SortedList copyFromList = otherSet.SortedSchemas;
bool setIsCompiled = schemas.Count == 0 ? true : false;
ArrayList existingSchemas = new ArrayList();
SchemaInfo newCompiledInfo = new SchemaInfo();
Uri baseUri;
for(int i=0; i < copyFromList.Count; i++) {
currentSchema = (XmlSchema)copyFromList.GetByIndex(i);
baseUri = currentSchema.BaseUri;
if (schemas.ContainsKey(currentSchema.SchemaId) || (baseUri != null && baseUri.OriginalString.Length != 0 && schemaLocations[baseUri] != null)) {
existingSchemas.Add(currentSchema);
continue;
}
schemas.Add(currentSchema.SchemaId, currentSchema);
if (baseUri != null && baseUri.OriginalString.Length != 0) {
schemaLocations.Add(baseUri, currentSchema);
}
string tns = GetTargetNamespace(currentSchema);
if (targetNamespaces[tns] == null) {
targetNamespaces.Add(tns, tns);
}
}
VerifyTables();
foreach (XmlSchemaElement element in otherSet.GlobalElements.Values) {
if(!AddToTable(elements, element.QualifiedName, element)) {
goto RemoveAll;
}
}
foreach (XmlSchemaAttribute attribute in otherSet.GlobalAttributes.Values) {
if (!AddToTable(attributes, attribute.QualifiedName, attribute)) {
goto RemoveAll;
}
}
foreach (XmlSchemaType schemaType in otherSet.GlobalTypes.Values) {
if (!AddToTable(schemaTypes, schemaType.QualifiedName, schemaType)) {
goto RemoveAll;
}
}
//
ProcessNewSubstitutionGroups(otherSet.SubstitutionGroups, false);
newCompiledInfo.Add(cachedCompiledInfo, eventHandler); //Add all the items from the old to the new compiled object
newCompiledInfo.Add(otherSet.CompiledInfo,eventHandler); //
cachedCompiledInfo = newCompiledInfo; //Replace the compiled info in the set after successful compilation
if (setIsCompiled) {
isCompiled = true;
compileAll = false;
}
return;
RemoveAll:
foreach (XmlSchema schemaToRemove in copyFromList.Values) {
if (!existingSchemas.Contains(schemaToRemove)) {
Remove(schemaToRemove, false);
}
}
foreach (XmlSchemaElement elementToRemove in otherSet.GlobalElements.Values) {
if(!existingSchemas.Contains((XmlSchema)elementToRemove.Parent)) {
elements.Remove(elementToRemove.QualifiedName);
}
}
foreach (XmlSchemaAttribute attributeToRemove in otherSet.GlobalAttributes.Values) {
if(!existingSchemas.Contains((XmlSchema)attributeToRemove.Parent)) {
attributes.Remove(attributeToRemove.QualifiedName);
}
}
foreach (XmlSchemaType schemaTypeToRemove in otherSet.GlobalTypes.Values) {
if(!existingSchemas.Contains((XmlSchema)schemaTypeToRemove.Parent)) {
schemaTypes.Remove(schemaTypeToRemove.QualifiedName);
}
}
}
internal SchemaInfo CompiledInfo {
get {
return cachedCompiledInfo;
}
}
internal XmlReaderSettings ReaderSettings {
get {
return readerSettings;
}
}
internal XmlResolver GetResolver() {
return readerSettings.GetXmlResolver_CheckConfig();
}
internal ValidationEventHandler GetEventHandler() {
return eventHandler;
}
internal SchemaNames GetSchemaNames(XmlNameTable nt) {
if (nameTable != nt) {
return new SchemaNames(nt);
}
else {
if (schemaNames == null) {
schemaNames = new SchemaNames( nameTable );
}
return schemaNames;
}
}
internal bool IsSchemaLoaded(Uri schemaUri, string targetNamespace, out XmlSchema schema) {
schema = null;
if (targetNamespace == null) {
targetNamespace = string.Empty;
}
if (GetSchemaByUri(schemaUri, out schema)) {
if (schemas.ContainsKey(schema.SchemaId) && (targetNamespace.Length == 0 || targetNamespace == schema.TargetNamespace)) { //schema is present in set
//Schema found
}
else if (schema.TargetNamespace == null) { //If schema not in set or namespace doesnt match, then it might be a chameleon
XmlSchema chameleonSchema = FindSchemaByNSAndUrl(schemaUri, targetNamespace, null);
if (chameleonSchema != null && schemas.ContainsKey(chameleonSchema.SchemaId)) {
schema = chameleonSchema;
}
else {
schema = Add(targetNamespace, schema);
}
}
else if (targetNamespace.Length != 0 && targetNamespace != schema.TargetNamespace) {
SendValidationEvent(new XmlSchemaException(Res.Sch_MismatchTargetNamespaceEx, new string[] { targetNamespace, schema.TargetNamespace }), XmlSeverityType.Error);
schema = null;
}
else {
//If here, schema not present in set but in loc and might be added in loc through an earlier include
//S.TNS != null && ( tns == null or tns == s.TNS)
AddSchemaToSet(schema);
}
return true; //Schema Found
}
return false;
}
internal bool GetSchemaByUri(Uri schemaUri, out XmlSchema schema) {
schema = null;
if (schemaUri == null || schemaUri.OriginalString.Length == 0) {
return false;
}
schema = (XmlSchema)schemaLocations[schemaUri];
if (schema != null) {
return true;
}
return false;
}
internal string GetTargetNamespace(XmlSchema schema) {
return schema.TargetNamespace == null ? string.Empty : schema.TargetNamespace;
}
internal SortedList SortedSchemas {
get {
return schemas;
}
}
internal bool CompileAll {
get {
return compileAll;
}
}
//Private Methods
private void RemoveSchemaFromCaches(XmlSchema schema) {
//Remove From ChameleonSchemas and schemaLocations cache
List<XmlSchema> reprocessList = new List<XmlSchema>();
schema.GetExternalSchemasList(reprocessList, schema);
for (int i = 0; i < reprocessList.Count; ++i) { //Remove schema from schemaLocations & chameleonSchemas tables
if (reprocessList[i].BaseUri != null && reprocessList[i].BaseUri.OriginalString.Length != 0) {
schemaLocations.Remove(reprocessList[i].BaseUri);
}
//Remove from chameleon table
ICollection chameleonKeys = chameleonSchemas.Keys;
ArrayList removalList = new ArrayList();
foreach(ChameleonKey cKey in chameleonKeys) {
if (cKey.chameleonLocation.Equals(reprocessList[i].BaseUri)) {
// The key will have the originalSchema set to null if the location was not empty
// otherwise we need to care about it as there may be more chameleon schemas without
// a base URI and we want to remove only those which were created as a clone of the one
// we're removing.
if (cKey.originalSchema == null || Ref.ReferenceEquals(cKey.originalSchema, reprocessList[i])) {
removalList.Add(cKey);
}
}
}
for (int j = 0; j < removalList.Count; ++j) {
chameleonSchemas.Remove(removalList[j]);
}
}
}
private void RemoveSchemaFromGlobalTables(XmlSchema schema) {
if (schemas.Count == 0) {
return;
}
VerifyTables();
foreach (XmlSchemaElement elementToRemove in schema.Elements.Values) {
XmlSchemaElement elem = (XmlSchemaElement)elements[elementToRemove.QualifiedName];
if (elem == elementToRemove) {
elements.Remove(elementToRemove.QualifiedName);
}
}
foreach (XmlSchemaAttribute attributeToRemove in schema.Attributes.Values) {
XmlSchemaAttribute attr = (XmlSchemaAttribute)attributes[attributeToRemove.QualifiedName];
if (attr == attributeToRemove) {
attributes.Remove(attributeToRemove.QualifiedName);
}
}
foreach (XmlSchemaType schemaTypeToRemove in schema.SchemaTypes.Values) {
XmlSchemaType schemaType = (XmlSchemaType)schemaTypes[schemaTypeToRemove.QualifiedName];
if (schemaType == schemaTypeToRemove) {
schemaTypes.Remove(schemaTypeToRemove.QualifiedName);
}
}
}
private bool AddToTable(XmlSchemaObjectTable table, XmlQualifiedName qname, XmlSchemaObject item) {
if (qname.Name.Length == 0) {
return true;
}
XmlSchemaObject existingObject = (XmlSchemaObject)table[qname];
if (existingObject != null) {
if (existingObject == item || existingObject.SourceUri == item.SourceUri) {
return true;
}
string code = string.Empty;
if (item is XmlSchemaComplexType) {
code = Res.Sch_DupComplexType;
}
else if (item is XmlSchemaSimpleType) {
code = Res.Sch_DupSimpleType;
}
else if (item is XmlSchemaElement) {
code = Res.Sch_DupGlobalElement;
}
else if (item is XmlSchemaAttribute) {
if (qname.Namespace == XmlReservedNs.NsXml) {
XmlSchema schemaForXmlNS = Preprocessor.GetBuildInSchema();
XmlSchemaObject builtInAttribute = schemaForXmlNS.Attributes[qname];
if (existingObject == builtInAttribute) { //replace built-in one
table.Insert(qname, item);
return true;
}
else if (item == builtInAttribute) { //trying to overwrite customer's component with built-in, ignore built-in
return true;
}
}
code = Res.Sch_DupGlobalAttribute;
}
SendValidationEvent(new XmlSchemaException(code,qname.ToString()), XmlSeverityType.Error);
return false;
}
else {
table.Add(qname, item);
return true;
}
}
private void VerifyTables() {
if (elements == null) {
elements = new XmlSchemaObjectTable();
}
if (attributes == null) {
attributes = new XmlSchemaObjectTable();
}
if (schemaTypes == null) {
schemaTypes = new XmlSchemaObjectTable();
}
if (substitutionGroups == null) {
substitutionGroups = new XmlSchemaObjectTable();
}
}
private void InternalValidationCallback(object sender, ValidationEventArgs e ) {
if (e.Severity == XmlSeverityType.Error) {
throw e.Exception;
}
}
private void SendValidationEvent(XmlSchemaException e, XmlSeverityType severity) {
if (eventHandler != null) {
eventHandler(this, new ValidationEventArgs(e, severity));
}
else {
throw e;
}
}
};
#endif
}
|