Skip to content

Commit

Permalink
Make substitution group handling configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Ganss committed Jul 29, 2020
1 parent 78e1034 commit 749117e
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 10 deletions.
5 changes: 4 additions & 1 deletion XmlSchemaClassGenerator.Console/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ static void Main(string[] args)
var generateComplexTypesForCollections = true;
var useShouldSerialize = false;
var separateClasses = false;
var separateSubstitutes = false;
var collectionSettersMode = CollectionSettersMode.Private;
var doNotForceIsNullable = false;

Expand Down Expand Up @@ -121,6 +122,7 @@ A file name may be given by appending a pipe sign (|) followed by a file name (l
{ "cc|complexTypesForCollections", "generate complex types for collections (default is true)", v => generateComplexTypesForCollections = v != null },
{ "s|useShouldSerialize", "use ShouldSerialize pattern instead of Specified pattern (default is false)", v => useShouldSerialize = v != null },
{ "sf|separateFiles", "generate a separate file for each class (default is false)", v => separateClasses = v != null },
{ "sg|separateSubstitutes", "generate a separate property for each element of a substitution group (default is false)", v => separateSubstitutes = v != null },
{ "dnfin|doNotForceIsNullable", "do not force generator to emit IsNullable = true in XmlElement annotation for nillable elements when element is nullable (minOccurs < 1 or parent element is choice) (default is false)", v => doNotForceIsNullable = v != null }
};

Expand Down Expand Up @@ -191,7 +193,8 @@ A file name may be given by appending a pipe sign (|) followed by a file name (l
UseShouldSerializePattern = useShouldSerialize,
SeparateClasses = separateClasses,
CollectionSettersMode = collectionSettersMode,
DoNotForceIsNullable = doNotForceIsNullable
DoNotForceIsNullable = doNotForceIsNullable,
SeparateSubstitutes = separateSubstitutes
};

if (pclCompatible)
Expand Down
3 changes: 2 additions & 1 deletion XmlSchemaClassGenerator.Tests/Compiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ public static Assembly GenerateFiles(string name, IEnumerable<string> files, Gen
EmitOrder = generatorPrototype.EmitOrder,
SeparateClasses = generatorPrototype.SeparateClasses,
CollectionType = generatorPrototype.CollectionType,
CollectionImplementationType = generatorPrototype.CollectionImplementationType
CollectionImplementationType = generatorPrototype.CollectionImplementationType,
SeparateSubstitutes = generatorPrototype.SeparateSubstitutes
};

output.Configuration = gen.Configuration;
Expand Down
17 changes: 16 additions & 1 deletion XmlSchemaClassGenerator.Tests/XmlTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,12 @@ public void TestIS24ImmoTransfer()
{
Compiler.Generate("IS24ImmoTransfer", IS24ImmoTransferPattern);
TestSamples("IS24ImmoTransfer", IS24ImmoTransferPattern);

Compiler.Generate("IS24ImmoTransferSeparate", IS24ImmoTransferPattern, new Generator
{
SeparateSubstitutes = true
});
TestSamples("IS24ImmoTransferSeparate", IS24ImmoTransferPattern);
}

[Fact, TestPriority(1)]
Expand Down Expand Up @@ -666,7 +672,16 @@ public void TestCustomNamespaces()
[UseCulture("en-US")]
public void TestBpmn()
{
var assembly = Compiler.Generate("Bpmn", BpmnPattern);
PerformBpmnTest("Bpmn");
PerformBpmnTest("BpmnSeparate", new Generator
{
SeparateSubstitutes = true
});
}

private void PerformBpmnTest(string name, Generator generator = null)
{
var assembly = Compiler.Generate(name, BpmnPattern, generator);
Assert.NotNull(assembly);

var type = assembly.GetTypes().SingleOrDefault(t => t.Name == "TDefinitions");
Expand Down
6 changes: 6 additions & 0 deletions XmlSchemaClassGenerator/Generator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,12 @@ public bool SeparateClasses
set { _configuration.SeparateClasses = value; }
}

public bool SeparateSubstitutes
{
get { return _configuration.SeparateSubstitutes; }
set { _configuration.SeparateSubstitutes = value; }
}

public void Generate(IEnumerable<string> files)
{
var set = new XmlSchemaSet();
Expand Down
5 changes: 5 additions & 0 deletions XmlSchemaClassGenerator/GeneratorConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -269,5 +269,10 @@ public void WriteLog(string message)
/// Separates each class into an individual file
/// </summary>
public bool SeparateClasses { get; set; } = false;

/// <summary>
/// Generates a separate property for each element of a substitution group
/// </summary>
public bool SeparateSubstitutes { get; set; } = false;
}
}
22 changes: 15 additions & 7 deletions XmlSchemaClassGenerator/ModelBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,16 +82,24 @@ private void CreateSubstitutes()
foreach (var prop in properties)
{
var substitutes = GetSubstitutedElements(prop.XmlSchemaName).ToList();
var elems = GetElements(prop.XmlParticle, prop.XmlParent);

foreach (var substitute in substitutes)
if (_configuration.SeparateSubstitutes)
{
var cls = (ClassModel)prop.OwningType;
var schema = substitute.Element.GetSchema();
var source = CodeUtilities.CreateUri(schema.SourceUri);
var props = CreatePropertiesForElements(source, cls, prop.XmlParticle, elems, substitute);
var elems = GetElements(prop.XmlParticle, prop.XmlParent);

foreach (var substitute in substitutes)
{
var cls = (ClassModel)prop.OwningType;
var schema = substitute.Element.GetSchema();
var source = CodeUtilities.CreateUri(schema.SourceUri);
var props = CreatePropertiesForElements(source, cls, prop.XmlParticle, elems, substitute);

cls.Properties.AddRange(props);
cls.Properties.AddRange(props);
}
}
else
{
prop.Substitutes = substitutes;
}
}
}
Expand Down
21 changes: 21 additions & 0 deletions XmlSchemaClassGenerator/TypeModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -587,11 +587,13 @@ public class PropertyModel
public XmlSchemaParticle XmlParticle { get; set; }
public XmlSchemaObject XmlParent { get; set; }
public GeneratorConfiguration Configuration { get; private set; }
public List<Substitute> Substitutes { get; set; }

public PropertyModel(GeneratorConfiguration configuration)
{
Configuration = configuration;
Documentation = new List<DocumentationModel>();
Substitutes = new List<Substitute>();
}

internal static string GetAccessors(string memberName, string backingFieldName, PropertyValueTypeCode typeCode, bool privateSetter, bool withDataBinding = true)
Expand Down Expand Up @@ -1185,6 +1187,25 @@ private IEnumerable<CodeAttributeDeclaration> GetAttributes(bool isArray)
}
else
{
if (!Configuration.SeparateSubstitutes && Substitutes.Any())
{
foreach (var substitute in Substitutes)
{
var substitutedAttribute = new CodeAttributeDeclaration(new CodeTypeReference(typeof(XmlElementAttribute), Configuration.CodeTypeReferenceOptions),
new CodeAttributeArgument(new CodePrimitiveExpression(substitute.Element.QualifiedName.Name)),
new CodeAttributeArgument("Type", new CodeTypeOfExpression(substitute.Type.GetReferenceFor(OwningType.Namespace))),
new CodeAttributeArgument("Namespace", new CodePrimitiveExpression(substitute.Element.QualifiedName.Namespace)));

if (Order != null)
{
substitutedAttribute.Arguments.Add(new CodeAttributeArgument("Order",
new CodePrimitiveExpression(Order.Value)));
}

attributes.Add(substitutedAttribute);
}
}

var attribute = new CodeAttributeDeclaration(new CodeTypeReference(typeof(XmlElementAttribute), Configuration.CodeTypeReferenceOptions),
new CodeAttributeArgument(new CodePrimitiveExpression(XmlSchemaName.Name)));
if (Order != null)
Expand Down

0 comments on commit 749117e

Please sign in to comment.