Skip to content

Commit

Permalink
Generate namespace according to its type's source (fixes #296)
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Ganss committed Dec 16, 2021
1 parent 9c6f1dc commit 8450da9
Showing 1 changed file with 16 additions and 17 deletions.
33 changes: 16 additions & 17 deletions XmlSchemaClassGenerator/ModelBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ private void CreateSubstitutes()

private void AddXmlRootAttributeToAmbiguousTypes()
{
var ambiguousTypes = Types.Values.Where(t => t.RootElementName == null && !t.IsAbstractRoot && !(t is InterfaceModel)).GroupBy(t => t.Name);
var ambiguousTypes = Types.Values.Where(t => t.RootElementName == null && !t.IsAbstractRoot && t is not InterfaceModel).GroupBy(t => t.Name);
foreach (var ambiguousTypeGroup in ambiguousTypes)
{
var types = ambiguousTypeGroup.ToList();
Expand Down Expand Up @@ -238,9 +238,7 @@ private void CreateTypes(IEnumerable<XmlSchemaType> types)
{
foreach (var globalType in types)
{
var schema = globalType.GetSchema();
var source = CodeUtilities.CreateUri(schema?.SourceUri);
CreateTypeModel(source, globalType, globalType.QualifiedName);
CreateTypeModel(globalType, globalType.QualifiedName);
}
}

Expand All @@ -251,7 +249,7 @@ private void CreateElements(IEnumerable<XmlSchemaElement> elements)
var typeSource = CodeUtilities.CreateUri(rootElement.ElementSchemaType.SourceUri);
var qualifiedName = rootElement.ElementSchemaType.QualifiedName;
if (qualifiedName.IsEmpty) { qualifiedName = rootElement.QualifiedName; }
var type = CreateTypeModel(typeSource, rootElement.ElementSchemaType, qualifiedName);
var type = CreateTypeModel(rootElement.ElementSchemaType, qualifiedName);
ClassModel derivedClassModel = null;

if (type.RootElementName != null || type.IsAbstractRoot)
Expand Down Expand Up @@ -371,14 +369,15 @@ private IEnumerable<Substitute> GetSubstitutedElements(XmlQualifiedName name)
}
}

private TypeModel CreateTypeModel(Uri source, XmlSchemaAnnotated type, XmlQualifiedName qualifiedName)
private TypeModel CreateTypeModel(XmlSchemaAnnotated type, XmlQualifiedName qualifiedName)
{
var key = BuildKey(type, qualifiedName);
if (!qualifiedName.IsEmpty && Types.TryGetValue(key, out TypeModel typeModel))
{
return typeModel;
}

var source = CodeUtilities.CreateUri(type.SourceUri);
var namespaceModel = CreateNamespaceModel(source, qualifiedName);
var docs = GetDocumentation(type);

Expand Down Expand Up @@ -427,10 +426,10 @@ private TypeModel CreateTypeModel(Uri source, XmlSchemaGroup group, NamespaceMod
var xmlParticle = group.Particle;
var particle = new Particle(xmlParticle, group.Parent);
var items = GetElements(xmlParticle);
var properties = CreatePropertiesForElements(source, interfaceModel, particle, items.Where(i => !(i.XmlParticle is XmlSchemaGroupRef)));
var properties = CreatePropertiesForElements(source, interfaceModel, particle, items.Where(i => i.XmlParticle is not XmlSchemaGroupRef));
interfaceModel.Properties.AddRange(properties);
var interfaces = items.Select(i => i.XmlParticle).OfType<XmlSchemaGroupRef>()
.Select(i => (InterfaceModel)CreateTypeModel(CodeUtilities.CreateUri(i.SourceUri), Groups[i.RefName], i.RefName));
.Select(i => (InterfaceModel)CreateTypeModel(Groups[i.RefName], i.RefName));
interfaceModel.AddInterfaces(interfaces);

return interfaceModel;
Expand Down Expand Up @@ -462,7 +461,7 @@ private TypeModel CreateTypeModel(Uri source, XmlSchemaAttributeGroup attributeG
var properties = CreatePropertiesForAttributes(source, interfaceModel, items.OfType<XmlSchemaAttribute>());
interfaceModel.Properties.AddRange(properties);
var interfaces = items.OfType<XmlSchemaAttributeGroupRef>()
.Select(a => (InterfaceModel)CreateTypeModel(CodeUtilities.CreateUri(a.SourceUri), AttributeGroups[a.RefName], a.RefName));
.Select(a => (InterfaceModel)CreateTypeModel(AttributeGroups[a.RefName], a.RefName));
interfaceModel.AddInterfaces(interfaces);

return interfaceModel;
Expand Down Expand Up @@ -503,7 +502,7 @@ private TypeModel CreateTypeModel(Uri source, XmlSchemaComplexType complexType,

if (complexType.BaseXmlSchemaType != null && complexType.BaseXmlSchemaType.QualifiedName != AnyType)
{
var baseModel = CreateTypeModel(source, complexType.BaseXmlSchemaType, complexType.BaseXmlSchemaType.QualifiedName);
var baseModel = CreateTypeModel(complexType.BaseXmlSchemaType, complexType.BaseXmlSchemaType.QualifiedName);
classModel.BaseClass = baseModel;
if (baseModel is ClassModel baseClassModel) { baseClassModel.DerivedTypes.Add(classModel); }
}
Expand All @@ -526,7 +525,7 @@ private TypeModel CreateTypeModel(Uri source, XmlSchemaComplexType complexType,
if (_configuration.GenerateInterfaces)
{
var interfaces = items.Select(i => i.XmlParticle).OfType<XmlSchemaGroupRef>()
.Select(i => (InterfaceModel)CreateTypeModel(CodeUtilities.CreateUri(i.SourceUri), Groups[i.RefName], i.RefName)).ToList();
.Select(i => (InterfaceModel)CreateTypeModel(Groups[i.RefName], i.RefName)).ToList();

classModel.AddInterfaces(interfaces);
}
Expand Down Expand Up @@ -573,7 +572,7 @@ private TypeModel CreateTypeModel(Uri source, XmlSchemaComplexType complexType,
if (_configuration.GenerateInterfaces)
{
var attributeInterfaces = attributes.OfType<XmlSchemaAttributeGroupRef>()
.Select(i => (InterfaceModel)CreateTypeModel(CodeUtilities.CreateUri(i.SourceUri), AttributeGroups[i.RefName], i.RefName));
.Select(i => (InterfaceModel)CreateTypeModel(AttributeGroups[i.RefName], i.RefName));
classModel.AddInterfaces(attributeInterfaces);
}
}
Expand Down Expand Up @@ -801,7 +800,7 @@ private IEnumerable<PropertyModel> CreatePropertiesForAttributes(Uri source, Typ
OwningType = typeModel,
Name = attributeName,
XmlSchemaName = attribute.QualifiedName,
Type = CreateTypeModel(source, attribute.AttributeSchemaType, attributeQualifiedName),
Type = CreateTypeModel(attribute.AttributeSchemaType, attributeQualifiedName),
IsAttribute = true,
IsNullable = attribute.Use != XmlSchemaUse.Required,
DefaultValue = attribute.DefaultValue ?? (attribute.Use != XmlSchemaUse.Optional ? attribute.FixedValue : null),
Expand Down Expand Up @@ -829,7 +828,7 @@ private IEnumerable<PropertyModel> CreatePropertiesForAttributes(Uri source, Typ
{
if (_configuration.GenerateInterfaces)
{
CreateTypeModel(CodeUtilities.CreateUri(attributeGroupRef.SourceUri), AttributeGroups[attributeGroupRef.RefName], attributeGroupRef.RefName);
CreateTypeModel(AttributeGroups[attributeGroupRef.RefName], attributeGroupRef.RefName);
}

var groupItems = AttributeGroups[attributeGroupRef.RefName].Attributes;
Expand Down Expand Up @@ -892,11 +891,11 @@ private IEnumerable<PropertyModel> CreatePropertiesForElements(Uri source, TypeM
XmlSchemaName = effectiveElement.QualifiedName,
Name = propertyName,
OriginalPropertyName = originalPropertyName,
Type = substitute?.Type ?? CreateTypeModel(source, element.ElementSchemaType, elementQualifiedName),
Type = substitute?.Type ?? CreateTypeModel(element.ElementSchemaType, elementQualifiedName),
IsNillable = element.IsNillable,
IsNullable = item.MinOccurs < 1.0m || (item.XmlParent is XmlSchemaChoice),
IsCollection = item.MaxOccurs > 1.0m || particle.MaxOccurs > 1.0m, // http://msdn.microsoft.com/en-us/library/vstudio/d3hx2s7e(v=vs.100).aspx
DefaultValue = element.DefaultValue ?? ((item.MinOccurs >= 1.0m && !(item.XmlParent is XmlSchemaChoice)) ? element.FixedValue : null),
DefaultValue = element.DefaultValue ?? ((item.MinOccurs >= 1.0m && item.XmlParent is not XmlSchemaChoice) ? element.FixedValue : null),
FixedValue = element.FixedValue,
Form = element.Form == XmlSchemaForm.None ? element.GetSchema().ElementFormDefault : element.Form,
XmlNamespace = !string.IsNullOrEmpty(effectiveElement.QualifiedName.Namespace) && effectiveElement.QualifiedName.Namespace != typeModel.XmlSchemaName.Namespace
Expand Down Expand Up @@ -936,7 +935,7 @@ private IEnumerable<PropertyModel> CreatePropertiesForElements(Uri source, TypeM

if (_configuration.GenerateInterfaces)
{
CreateTypeModel(CodeUtilities.CreateUri(groupRef.SourceUri), group, groupRef.RefName);
CreateTypeModel(group, groupRef.RefName);
}

var groupItems = GetElements(group.Particle);
Expand Down

0 comments on commit 8450da9

Please sign in to comment.