private void Preprocess(XmlSchema schema, string targetNamespace, Compositor compositor) {
if (schema.IsProcessing) {
return;
}
schema.IsProcessing = true;
string tns = schema.TargetNamespace;
if (tns != null) {
schema.TargetNamespace = tns = NameTable.Add(tns);
if (tns.Length == 0) {
SendValidationEvent(Res.Sch_InvalidTargetNamespaceAttribute, schema);
}
else {
try {
XmlConvert.ToUri(tns); // can throw
}
catch {
SendValidationEvent(Res.Sch_InvalidNamespace, schema.TargetNamespace, schema);
}
}
}
if (schema.Version != null) {
try {
XmlConvert.VerifyTOKEN(schema.Version); // can throw
}
catch (Exception) {
SendValidationEvent(Res.Sch_AttributeValueDataType, "version", schema);
}
}
switch (compositor) {
case Compositor.Root:
if (targetNamespace == null && schema.TargetNamespace != null) { // not specified
targetNamespace = schema.TargetNamespace;
}
else if (schema.TargetNamespace == null && targetNamespace != null && targetNamespace.Length == 0) { // no namespace schema
targetNamespace = null;
}
if (targetNamespace != schema.TargetNamespace) {
SendValidationEvent(Res.Sch_MismatchTargetNamespaceEx, targetNamespace, schema.TargetNamespace, schema);
}
break;
case Compositor.Import:
if (targetNamespace != schema.TargetNamespace) {
SendValidationEvent(Res.Sch_MismatchTargetNamespaceImport, targetNamespace, schema.TargetNamespace, schema);
}
break;
case Compositor.Include:
if (schema.TargetNamespace != null) {
if (targetNamespace != schema.TargetNamespace) {
SendValidationEvent(Res.Sch_MismatchTargetNamespaceInclude, targetNamespace, schema.TargetNamespace, schema);
}
}
break;
}
foreach(XmlSchemaExternal include in schema.Includes) {
SetParent(include, schema);
PreprocessAnnotation(include);
string loc = include.SchemaLocation;
if (loc != null) {
try {
XmlConvert.ToUri(loc); // can throw
}
catch {
SendValidationEvent(Res.Sch_InvalidSchemaLocation, loc, include);
}
}
else if((include is XmlSchemaRedefine || include is XmlSchemaInclude) && include.Schema == null) {
SendValidationEvent(Res.Sch_MissRequiredAttribute, "schemaLocation", include);
}
if (include.Schema != null) {
if (include is XmlSchemaRedefine) {
Preprocess(include.Schema, schema.TargetNamespace, Compositor.Include);
}
else if (include is XmlSchemaImport) {
if (((XmlSchemaImport)include).Namespace == null && schema.TargetNamespace == null) {
SendValidationEvent(Res.Sch_ImportTargetNamespaceNull, include);
}
else if (((XmlSchemaImport)include).Namespace == schema.TargetNamespace) {
SendValidationEvent(Res.Sch_ImportTargetNamespace, include);
}
Preprocess(include.Schema, ((XmlSchemaImport)include).Namespace, Compositor.Import);
}
else {
Preprocess(include.Schema, schema.TargetNamespace, Compositor.Include);
}
}
else if (include is XmlSchemaImport) {
string ns = ((XmlSchemaImport)include).Namespace;
if (ns != null) {
if (ns.Length == 0) {
SendValidationEvent(Res.Sch_InvalidNamespaceAttribute, ns, include);
}
else {
try {
XmlConvert.ToUri(ns); //can throw
}
catch(FormatException) {
SendValidationEvent(Res.Sch_InvalidNamespace, ns, include);
}
}
}
}
}
//Begin processing the current schema passed to preprocess
//Build the namespaces that can be referenced in the current schema
BuildRefNamespaces(schema);
this.targetNamespace = targetNamespace == null ? string.Empty : targetNamespace;
if (schema.BlockDefault == XmlSchemaDerivationMethod.All) {
this.blockDefault = XmlSchemaDerivationMethod.All;
}
else if (schema.BlockDefault == XmlSchemaDerivationMethod.None) {
this.blockDefault = XmlSchemaDerivationMethod.Empty;
}
else {
if ((schema.BlockDefault & ~schemaBlockDefaultAllowed) != 0) {
SendValidationEvent(Res.Sch_InvalidBlockDefaultValue, schema);
}
this.blockDefault = schema.BlockDefault & schemaBlockDefaultAllowed;
}
if (schema.FinalDefault == XmlSchemaDerivationMethod.All) {
this.finalDefault = XmlSchemaDerivationMethod.All;
}
else if (schema.FinalDefault == XmlSchemaDerivationMethod.None) {
this.finalDefault = XmlSchemaDerivationMethod.Empty;
}
else {
if ((schema.FinalDefault & ~schemaFinalDefaultAllowed) != 0) {
SendValidationEvent(Res.Sch_InvalidFinalDefaultValue, schema);
}
this.finalDefault = schema.FinalDefault & schemaFinalDefaultAllowed;
}
this.elementFormDefault = schema.ElementFormDefault;
if (this.elementFormDefault == XmlSchemaForm.None) {
this.elementFormDefault = XmlSchemaForm.Unqualified;
}
this.attributeFormDefault = schema.AttributeFormDefault;
if (this.attributeFormDefault == XmlSchemaForm.None) {
this.attributeFormDefault = XmlSchemaForm.Unqualified;
}
foreach(XmlSchemaExternal include in schema.Includes) {
if (include is XmlSchemaRedefine) {
XmlSchemaRedefine redefine = (XmlSchemaRedefine)include;
if (include.Schema != null) {
PreprocessRedefine(redefine);
}
else {
foreach(XmlSchemaObject item in redefine.Items) {
if (!(item is XmlSchemaAnnotation)) {
SendValidationEvent(Res.Sch_RedefineNoSchema, redefine);
break;
}
}
}
}
XmlSchema includedSchema = include.Schema;
if (includedSchema != null) {
foreach (XmlSchemaElement element in includedSchema.Elements.Values) {
AddToTable(schema.Elements, element.QualifiedName, element);
}
foreach (XmlSchemaAttribute attribute in includedSchema.Attributes.Values) {
AddToTable(schema.Attributes, attribute.QualifiedName, attribute);
}
foreach (XmlSchemaGroup group in includedSchema.Groups.Values) {
AddToTable(schema.Groups, group.QualifiedName, group);
}
foreach (XmlSchemaAttributeGroup attributeGroup in includedSchema.AttributeGroups.Values) {
AddToTable(schema.AttributeGroups, attributeGroup.QualifiedName, attributeGroup);
}
foreach (XmlSchemaType type in includedSchema.SchemaTypes.Values) {
AddToTable(schema.SchemaTypes, type.QualifiedName, type);
}
foreach (XmlSchemaNotation notation in includedSchema.Notations.Values) {
AddToTable(schema.Notations, notation.QualifiedName, notation);
}
}
ValidateIdAttribute(include);
}
ArrayList removeItemsList = new ArrayList();
foreach(XmlSchemaObject item in schema.Items) {
SetParent(item, schema);
if (item is XmlSchemaAttribute) {
XmlSchemaAttribute attribute = (XmlSchemaAttribute)item;
PreprocessAttribute(attribute);
AddToTable(schema.Attributes, attribute.QualifiedName, attribute);
}
else if (item is XmlSchemaAttributeGroup) {
XmlSchemaAttributeGroup attributeGroup = (XmlSchemaAttributeGroup)item;
PreprocessAttributeGroup(attributeGroup);
AddToTable(schema.AttributeGroups, attributeGroup.QualifiedName, attributeGroup);
}
else if (item is XmlSchemaComplexType) {
XmlSchemaComplexType complexType = (XmlSchemaComplexType)item;
PreprocessComplexType(complexType, false);
AddToTable(schema.SchemaTypes, complexType.QualifiedName, complexType);
}
else if (item is XmlSchemaSimpleType) {
XmlSchemaSimpleType simpleType = (XmlSchemaSimpleType)item;
PreprocessSimpleType(simpleType, false);
AddToTable(schema.SchemaTypes, simpleType.QualifiedName, simpleType);
}
else if (item is XmlSchemaElement) {
XmlSchemaElement element = (XmlSchemaElement)item;
PreprocessElement(element);
AddToTable(schema.Elements, element.QualifiedName, element);
}
else if (item is XmlSchemaGroup) {
XmlSchemaGroup group = (XmlSchemaGroup)item;
PreprocessGroup(group);
AddToTable(schema.Groups, group.QualifiedName, group);
}
else if (item is XmlSchemaNotation) {
XmlSchemaNotation notation = (XmlSchemaNotation)item;
PreprocessNotation(notation);
AddToTable(schema.Notations, notation.QualifiedName, notation);
}
else if(!(item is XmlSchemaAnnotation)) {
SendValidationEvent(Res.Sch_InvalidCollection,(XmlSchemaObject)item);
removeItemsList.Add(item);
}
}
foreach(XmlSchemaObject item in removeItemsList) {
schema.Items.Remove(item);
}
schema.IsProcessing = false;
}