Skip to content

Commit

Permalink
Add original XSD elements to INamingProvider methods
Browse files Browse the repository at this point in the history
  • Loading branch information
aparshin committed Nov 30, 2021
1 parent 9c6f1dc commit f1bc752
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 35 deletions.
74 changes: 63 additions & 11 deletions XmlSchemaClassGenerator/INamingProvider.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
namespace XmlSchemaClassGenerator
{
using System.Xml;
using System.Xml.Schema;

public interface INamingProvider
{
Expand All @@ -9,39 +10,90 @@ public interface INamingProvider
/// </summary>
/// <param name="typeModelName">Name of the typeModel</param>
/// <param name="attributeName">Attribute name</param>
/// <param name="attribute">Original XSD attribute</param>
/// <returns>Name of the property</returns>
string PropertyNameFromAttribute(string typeModelName, string attributeName);
string PropertyNameFromAttribute(string typeModelName, string attributeName, XmlSchemaAttribute attribute);

/// <summary>
/// Creates a name for a property from an element name
/// </summary>
/// <param name="typeModelName">Name of the typeModel</param>
/// <param name="elementName">Element name</param>
/// <param name="element">Original XSD element</param>
/// <returns>Name of the property</returns>
string PropertyNameFromElement(string typeModelName, string elementName);
string PropertyNameFromElement(string typeModelName, string elementName, XmlSchemaElement element);

/// <summary>
/// Creates a name for an enum member based on a value
/// </summary>
/// <param name="enumName">Name of the enum</param>
/// <param name="value">Value name</param>
/// <param name="xmlFacet">Original XSD enumeration facet</param>
/// <returns>Name of the enum member</returns>
string EnumMemberNameFromValue(string enumName, string value);
string EnumMemberNameFromValue(string enumName, string value, XmlSchemaEnumerationFacet xmlFacet);

string ComplexTypeNameFromQualifiedName(XmlQualifiedName qualifiedName);
/// <summary>
/// Define the name to be used when a ComplexType is found in the XSD.
/// </summary>
/// <param name="qualifiedName">The name as defined in the XSD if present.</param>
/// <param name="complexType">Original XSD ComplexType</param>
/// <returns>A string with a valid C# identifier name.</returns>
string ComplexTypeNameFromQualifiedName(XmlQualifiedName qualifiedName, XmlSchemaComplexType complexType);

string AttributeGroupTypeNameFromQualifiedName(XmlQualifiedName qualifiedName);
/// <summary>
/// Define the name to be used when a AttributeGroup is found in the XSD.
/// </summary>
/// <param name="qualifiedName">The name as defined in the XSD if present.</param>
/// <param name="attributeGroup">Original XSD AttributeGroup</param>
/// <returns>A string with a valid C# identifier name.</returns>
string AttributeGroupTypeNameFromQualifiedName(XmlQualifiedName qualifiedName, XmlSchemaAttributeGroup attributeGroup);

string GroupTypeNameFromQualifiedName(XmlQualifiedName qualifiedName);
/// <summary>
/// Define the name to be used when a GroupType is found in the XSD.
/// </summary>
/// <param name="qualifiedName">The name as defined in the XSD if present.</param>
/// <param name="group">Original XSD group</param>
/// <returns>A string with a valid C# identifier name.</returns>
string GroupTypeNameFromQualifiedName(XmlQualifiedName qualifiedName, XmlSchemaGroup group);

string SimpleTypeNameFromQualifiedName(XmlQualifiedName qualifiedName);
/// <summary>
/// Define the name to be used when a SimpleType is found in the XSD.
/// </summary>
/// <param name="qualifiedName">The name as defined in the XSD if present.</param>
/// <param name="simpleType">Original XSD SimpleType</param>
/// <returns>A string with a valid C# identifier name.</returns>
string SimpleTypeNameFromQualifiedName(XmlQualifiedName qualifiedName, XmlSchemaSimpleType simpleType);

string RootClassNameFromQualifiedName(XmlQualifiedName qualifiedName);
/// <summary>
/// Define the name to be used for the root class.
/// </summary>
/// <param name="qualifiedName">The name as defined in the XSD if present.</param>
/// <param name="xmlElement">Original XSD element</param>
/// <returns>A string with a valid C# identifier name.</returns>
string RootClassNameFromQualifiedName(XmlQualifiedName qualifiedName, XmlSchemaElement xmlElement);

string EnumTypeNameFromQualifiedName(XmlQualifiedName qualifiedName);
/// <summary>
/// Define the name to be used when an enum type is found in the XSD.
/// </summary>
/// <param name="qualifiedName">The name as defined in the XSD if present.</param>
/// <param name="xmlSimpleType">Original XSD SimpleType</param>
/// <returns>A string with a valid C# identifier name.</returns>
string EnumTypeNameFromQualifiedName(XmlQualifiedName qualifiedName, XmlSchemaSimpleType xmlSimpleType);

string AttributeNameFromQualifiedName(XmlQualifiedName qualifiedName);
/// <summary>
/// Define the name to be used when an attribute is found in the XSD.
/// </summary>
/// <param name="qualifiedName">The name as defined in the XSD if present.</param>
/// <param name="xmlAttribute">Original XSD attribute</param>
/// <returns>A string with a valid C# identifier name.</returns>
string AttributeNameFromQualifiedName(XmlQualifiedName qualifiedName, XmlSchemaAttribute xmlAttribute);

string ElementNameFromQualifiedName(XmlQualifiedName qualifiedName);
/// <summary>
/// Define the name of the C# class property from the element name in XSD.
/// </summary>
/// <param name="qualifiedName">The name as defined in the XSD if present.</param>
/// <param name="xmlElement">Original XSD element</param>
/// <returns>A string with a valid C# identifier name.</returns>
string ElementNameFromQualifiedName(XmlQualifiedName qualifiedName, XmlSchemaElement xmlElement);
}
}
24 changes: 12 additions & 12 deletions XmlSchemaClassGenerator/ModelBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ private void CreateElements(IEnumerable<XmlSchemaElement> elements)

derivedClassModel = new ClassModel(_configuration)
{
Name = _configuration.NamingProvider.RootClassNameFromQualifiedName(rootElement.QualifiedName),
Name = _configuration.NamingProvider.RootClassNameFromQualifiedName(rootElement.QualifiedName, rootElement),
Namespace = CreateNamespaceModel(elementSource, rootElement.QualifiedName)
};

Expand All @@ -291,7 +291,7 @@ private void CreateElements(IEnumerable<XmlSchemaElement> elements)

var originalClassModel = new ClassModel(_configuration)
{
Name = _configuration.NamingProvider.RootClassNameFromQualifiedName(type.RootElementName),
Name = _configuration.NamingProvider.RootClassNameFromQualifiedName(type.RootElementName, rootElement),
Namespace = classModel.Namespace
};

Expand Down Expand Up @@ -404,7 +404,7 @@ private TypeModel CreateTypeModel(Uri source, XmlSchemaAnnotated type, XmlQualif

private TypeModel CreateTypeModel(Uri source, XmlSchemaGroup group, NamespaceModel namespaceModel, XmlQualifiedName qualifiedName, List<DocumentationModel> docs)
{
var name = "I" + _configuration.NamingProvider.GroupTypeNameFromQualifiedName(qualifiedName);
var name = "I" + _configuration.NamingProvider.GroupTypeNameFromQualifiedName(qualifiedName, group);
if (namespaceModel != null) { name = namespaceModel.GetUniqueTypeName(name); }

var interfaceModel = new InterfaceModel(_configuration)
Expand Down Expand Up @@ -438,7 +438,7 @@ private TypeModel CreateTypeModel(Uri source, XmlSchemaGroup group, NamespaceMod

private TypeModel CreateTypeModel(Uri source, XmlSchemaAttributeGroup attributeGroup, NamespaceModel namespaceModel, XmlQualifiedName qualifiedName, List<DocumentationModel> docs)
{
var name = "I" + _configuration.NamingProvider.AttributeGroupTypeNameFromQualifiedName(qualifiedName);
var name = "I" + _configuration.NamingProvider.AttributeGroupTypeNameFromQualifiedName(qualifiedName, attributeGroup);
if (namespaceModel != null) { name = namespaceModel.GetUniqueTypeName(name); }

var interfaceModel = new InterfaceModel(_configuration)
Expand Down Expand Up @@ -470,7 +470,7 @@ private TypeModel CreateTypeModel(Uri source, XmlSchemaAttributeGroup attributeG

private TypeModel CreateTypeModel(Uri source, XmlSchemaComplexType complexType, NamespaceModel namespaceModel, XmlQualifiedName qualifiedName, List<DocumentationModel> docs)
{
var name = _configuration.NamingProvider.ComplexTypeNameFromQualifiedName(qualifiedName);
var name = _configuration.NamingProvider.ComplexTypeNameFromQualifiedName(qualifiedName, complexType);
if (namespaceModel != null)
{
name = namespaceModel.GetUniqueTypeName(name);
Expand Down Expand Up @@ -649,7 +649,7 @@ private TypeModel CreateTypeModel(XmlSchemaSimpleType simpleType, NamespaceModel
if (isEnum)
{
// we got an enum
var name = _configuration.NamingProvider.EnumTypeNameFromQualifiedName(qualifiedName);
var name = _configuration.NamingProvider.EnumTypeNameFromQualifiedName(qualifiedName, simpleType);
if (namespaceModel != null) { name = namespaceModel.GetUniqueTypeName(name); }

var enumModel = new EnumModel(_configuration)
Expand All @@ -667,7 +667,7 @@ private TypeModel CreateTypeModel(XmlSchemaSimpleType simpleType, NamespaceModel
{
var value = new EnumValueModel
{
Name = _configuration.NamingProvider.EnumMemberNameFromValue(enumModel.Name, facet.Value),
Name = _configuration.NamingProvider.EnumMemberNameFromValue(enumModel.Name, facet.Value, facet),
Value = facet.Value
};

Expand Down Expand Up @@ -699,7 +699,7 @@ private TypeModel CreateTypeModel(XmlSchemaSimpleType simpleType, NamespaceModel
restrictions = GetRestrictions(facets, simpleType).Where(r => r != null).Sanitize().ToList();
}

var simpleModelName = _configuration.NamingProvider.SimpleTypeNameFromQualifiedName(qualifiedName);
var simpleModelName = _configuration.NamingProvider.SimpleTypeNameFromQualifiedName(qualifiedName, simpleType);
if (namespaceModel != null) { simpleModelName = namespaceModel.GetUniqueTypeName(simpleModelName); }

var simpleModel = new SimpleModel(_configuration)
Expand Down Expand Up @@ -756,7 +756,7 @@ private IEnumerable<PropertyModel> CreatePropertiesForAttributes(Uri source, Typ
if (attribute.Use != XmlSchemaUse.Prohibited)
{
var attributeQualifiedName = attribute.AttributeSchemaType.QualifiedName;
var attributeName = _configuration.NamingProvider.AttributeNameFromQualifiedName(attribute.QualifiedName);
var attributeName = _configuration.NamingProvider.AttributeNameFromQualifiedName(attribute.QualifiedName, attribute);

if (attribute.Parent is XmlSchemaAttributeGroup attributeGroup
&& attributeGroup.QualifiedName != typeModel.XmlSchemaName
Expand All @@ -776,7 +776,7 @@ private IEnumerable<PropertyModel> CreatePropertiesForAttributes(Uri source, Typ
if (attributeQualifiedName.IsEmpty || string.IsNullOrEmpty(attributeQualifiedName.Namespace))
{
// inner type, have to generate a type name
var typeName = _configuration.NamingProvider.PropertyNameFromAttribute(typeModel.Name, attribute.QualifiedName.Name);
var typeName = _configuration.NamingProvider.PropertyNameFromAttribute(typeModel.Name, attribute.QualifiedName.Name, attribute);
attributeQualifiedName = new XmlQualifiedName(typeName, typeModel.XmlSchemaName.Namespace);
// try to avoid name clashes
if (NameExists(attributeQualifiedName))
Expand Down Expand Up @@ -864,7 +864,7 @@ private IEnumerable<PropertyModel> CreatePropertiesForElements(Uri source, TypeM
{
// inner type, have to generate a type name
var typeModelName = xmlParticle is XmlSchemaGroupRef groupRef ? groupRef.RefName : typeModel.XmlSchemaName;
var typeName = _configuration.NamingProvider.PropertyNameFromElement(typeModelName.Name, element.QualifiedName.Name);
var typeName = _configuration.NamingProvider.PropertyNameFromElement(typeModelName.Name, element.QualifiedName.Name, element);
elementQualifiedName = new XmlQualifiedName(typeName, typeModel.XmlSchemaName.Namespace);
// try to avoid name clashes
if (NameExists(elementQualifiedName))
Expand All @@ -877,7 +877,7 @@ private IEnumerable<PropertyModel> CreatePropertiesForElements(Uri source, TypeM
}

var effectiveElement = substitute?.Element ?? element;
var propertyName = _configuration.NamingProvider.ElementNameFromQualifiedName(effectiveElement.QualifiedName);
var propertyName = _configuration.NamingProvider.ElementNameFromQualifiedName(effectiveElement.QualifiedName, effectiveElement);
var originalPropertyName = propertyName;
if (propertyName == typeModel.Name)
{
Expand Down
Loading

0 comments on commit f1bc752

Please sign in to comment.