private XmlSchemaAttribute AddAttribute(string localName, string prefix, string childURI, string attrValue, bool bCreatingNewType, XmlSchema parentSchema, XmlSchemaObjectCollection addLocation, XmlSchemaObjectTable compiledAttributes)
{
if (childURI == XmlSchema.Namespace)
{
throw new XmlSchemaInferenceException(Res.SchInf_schema, 0, 0);
}
XmlSchemaAttribute xsa = null;
int AttributeType = -1;
XmlSchemaAttribute returnedAttribute = null; //this value will change to attributeReference if childURI!= parentURI
XmlSchema xs = null;
bool add = true;
Debug.Assert(compiledAttributes != null); //AttributeUses is never null
ICollection searchCollection;
if (compiledAttributes.Count > 0) {
searchCollection = compiledAttributes.Values;
}
else {
searchCollection = addLocation;
}
if (childURI == "http://www.w3.org/XML/1998/namespace")
{
XmlSchemaAttribute attributeReference = null;
//see if the reference exists
attributeReference = FindAttributeRef(searchCollection, localName, childURI);
if (attributeReference == null)
{
attributeReference = new XmlSchemaAttribute();
attributeReference.RefName = new XmlQualifiedName(localName, childURI);
if (bCreatingNewType && this.Occurrence == InferenceOption.Restricted)
{
attributeReference.Use = XmlSchemaUse.Required;
}
else
{
attributeReference.Use = XmlSchemaUse.Optional;
}
addLocation.Add(attributeReference);
}
returnedAttribute = attributeReference;
}
else
{
if (childURI.Length == 0)
{
xs = parentSchema;
add = false;
}
else if (childURI != null && !schemaSet.Contains(childURI))
{
/*if (parentSchema.AttributeFormDefault = XmlSchemaForm.Unqualified && childURI.Length == 0)
{
xs = parentSchema;
add = false;
break;
}*/
xs = new XmlSchema();
xs.AttributeFormDefault = XmlSchemaForm.Unqualified;
xs.ElementFormDefault = XmlSchemaForm.Qualified;
if (childURI.Length != 0)
xs.TargetNamespace = childURI;
//schemas.Add(childURI, xs);
this.schemaSet.Add(xs);
if (prefix.Length != 0 && String.Compare(prefix, "xml", StringComparison.OrdinalIgnoreCase) != 0)
NamespaceManager.AddNamespace(prefix, childURI);
}
else
{
ArrayList col = this.schemaSet.Schemas(childURI) as ArrayList;
if (col != null && col.Count > 0)
{
xs = col[0] as XmlSchema;
}
}
if (childURI.Length != 0)
{
XmlSchemaAttribute attributeReference = null;
//see if the reference exists
attributeReference = FindAttributeRef(searchCollection, localName, childURI);
if (attributeReference == null)
{
attributeReference = new XmlSchemaAttribute();
attributeReference.RefName = new XmlQualifiedName(localName, childURI);
if (bCreatingNewType && this.Occurrence == InferenceOption.Restricted)
{
attributeReference.Use = XmlSchemaUse.Required;
}
else
{
attributeReference.Use = XmlSchemaUse.Optional;
}
addLocation.Add(attributeReference);
}
returnedAttribute = attributeReference;
//see if the attribute exists on the global level
xsa = FindAttribute(xs.Items, localName);
if (xsa == null)
{
xsa = new XmlSchemaAttribute();
xsa.Name = localName;
xsa.SchemaTypeName = RefineSimpleType(attrValue, ref AttributeType);
xsa.LineNumber = AttributeType; //we use LineNumber to store flags of valid types
xs.Items.Add(xsa);
}
else
{
if (xsa.Parent == null)
{
AttributeType = xsa.LineNumber; // we use LineNumber to store flags of valid types
}
else
{
AttributeType = GetSchemaType(xsa.SchemaTypeName);
xsa.Parent = null;
}
xsa.SchemaTypeName = RefineSimpleType(attrValue, ref AttributeType);
xsa.LineNumber = AttributeType; // we use LineNumber to store flags of valid types
}
}
else
{
xsa = FindAttribute(searchCollection, localName);
if (xsa == null)
{
xsa = new XmlSchemaAttribute();
xsa.Name = localName;
xsa.SchemaTypeName = RefineSimpleType(attrValue, ref AttributeType);
xsa.LineNumber = AttributeType; // we use LineNumber to store flags of valid types
if (bCreatingNewType && this.Occurrence == InferenceOption.Restricted)
xsa.Use = XmlSchemaUse.Required;
else
xsa.Use = XmlSchemaUse.Optional;
addLocation.Add(xsa);
if (xs.AttributeFormDefault != XmlSchemaForm.Unqualified)
{
xsa.Form = XmlSchemaForm.Unqualified;
}
}
else
{
if (xsa.Parent == null)
{
AttributeType = xsa.LineNumber; // we use LineNumber to store flags of valid types
}
else
{
AttributeType = GetSchemaType(xsa.SchemaTypeName);
xsa.Parent = null;
}
xsa.SchemaTypeName = RefineSimpleType(attrValue, ref AttributeType);
xsa.LineNumber = AttributeType; // we use LineNumber to store flags of valid types
}
returnedAttribute = xsa;
}
}
string nullString = null;
if (add && childURI != parentSchema.TargetNamespace)
{
foreach (XmlSchemaExternal external in parentSchema.Includes)
{
XmlSchemaImport import = external as XmlSchemaImport;
if (import == null)
{
continue;
}
if (import.Namespace == childURI)
{
add = false;
}
}
if (add)
{
XmlSchemaImport import = new XmlSchemaImport();
import.Schema = xs;
if (childURI.Length != 0)
{
nullString = childURI;
}
import.Namespace = nullString;
parentSchema.Includes.Add(import);
}
}
return returnedAttribute;
}