Skip to content

Commit 6a53ec8

Browse files
committed
Merge PR #577 changes
2 parents 9212607 + 7cb7392 commit 6a53ec8

4 files changed

Lines changed: 174 additions & 34 deletions

File tree

XmlSchemaClassGenerator.Tests/XmlTests.cs

Lines changed: 59 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -186,40 +186,65 @@ public void TestGuid()
186186

187187
[Fact, TestPriority(1)]
188188
[UseCulture("en-US")]
189-
public void TestUnion()
190-
{
191-
var assembly = Compiler.Generate("Union", UnionPattern, new Generator
192-
{
193-
NamespacePrefix = "Union",
194-
IntegerDataType = typeof(int),
195-
MapUnionToWidestCommonType = true
196-
});
197-
198-
Assert.NotNull(assembly);
199-
200-
SharedTestFunctions.TestSamples(Output, "Union", UnionPattern);
201-
202-
var snapshotType = assembly.GetType("Union.Snapshot");
203-
Assert.NotNull(snapshotType);
204-
205-
var date = snapshotType.GetProperty("Date");
206-
Assert.NotNull(date);
207-
Assert.Equal(typeof(DateTime), date.PropertyType);
208-
209-
var count = snapshotType.GetProperty("Count");
210-
Assert.NotNull(count);
211-
Assert.Equal(typeof(int), count.PropertyType);
212-
213-
var num = snapshotType.GetProperty("Num");
214-
Assert.NotNull(num);
215-
Assert.Equal(typeof(decimal), num.PropertyType);
216-
}
217-
218-
[Fact, TestPriority(1)]
219-
[UseCulture("en-US")]
220-
public void TestList()
221-
{
222-
Compiler.Generate("List", ListPattern);
189+
public void TestUnion()
190+
{
191+
var assembly = Compiler.Generate("Union", UnionPattern, new Generator
192+
{
193+
NamespacePrefix = "Union",
194+
IntegerDataType = typeof(int),
195+
MapUnionToWidestCommonType = true
196+
});
197+
198+
Assert.NotNull(assembly);
199+
200+
SharedTestFunctions.TestSamples(Output, "Union", UnionPattern);
201+
202+
var snapshotType = assembly.GetType("Union.Snapshot");
203+
Assert.NotNull(snapshotType);
204+
205+
var date = snapshotType.GetProperty("Date");
206+
Assert.NotNull(date);
207+
Assert.Equal(typeof(DateTime), date.PropertyType);
208+
209+
var count = snapshotType.GetProperty("Count");
210+
Assert.NotNull(count);
211+
Assert.Equal(typeof(int), count.PropertyType);
212+
213+
var num = snapshotType.GetProperty("Num");
214+
Assert.NotNull(num);
215+
Assert.Equal(typeof(decimal), num.PropertyType);
216+
}
217+
218+
[Fact, TestPriority(1)]
219+
[UseCulture("en-US")]
220+
public void TestSimpleContentEnum()
221+
{
222+
var assembly = Compiler.Generate("SimpleContentEnum", "xsd/simple/simplecontent-enum.xsd");
223+
224+
const string ns = "SimpleContentEnum.Simplecontent";
225+
226+
var enumType = assembly.GetType($"{ns}.TransConfirmationCodeTypeEnum");
227+
if (enumType == null)
228+
{
229+
var names = string.Join(", ", assembly.GetTypes().Select(t => t.FullName));
230+
Assert.Fail($"Enum type not found. Available types: {names}");
231+
}
232+
233+
var type = assembly.GetType($"{ns}.TransConfirmationCodeType");
234+
Assert.NotNull(type);
235+
236+
var baseType = assembly.GetType($"{ns}.CodeType");
237+
Assert.Equal(baseType, type.BaseType);
238+
239+
var valueProperty = type.GetProperties().Single(p => p.PropertyType == enumType);
240+
Assert.Equal("Value", valueProperty.Name);
241+
}
242+
243+
[Fact, TestPriority(1)]
244+
[UseCulture("en-US")]
245+
public void TestList()
246+
{
247+
Compiler.Generate("List", ListPattern);
223248
SharedTestFunctions.TestSamples(Output, "List", ListPattern);
224249
}
225250

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://example.com/simplecontent" targetNamespace="http://example.com/simplecontent" elementFormDefault="qualified">
3+
<xs:complexType name="CodeType">
4+
<xs:simpleContent>
5+
<xs:extension base="xs:string">
6+
<xs:attribute name="listID" type="xs:string" use="optional"/>
7+
</xs:extension>
8+
</xs:simpleContent>
9+
</xs:complexType>
10+
11+
<xs:complexType name="TransConfirmationCodeType">
12+
<xs:simpleContent>
13+
<xs:restriction base="CodeType">
14+
<xs:enumeration value="Always"/>
15+
<xs:enumeration value="Never"/>
16+
<xs:enumeration value="OnError"/>
17+
</xs:restriction>
18+
</xs:simpleContent>
19+
</xs:complexType>
20+
21+
<xs:element name="Root" type="TransConfirmationCodeType"/>
22+
</xs:schema>

XmlSchemaClassGenerator/ModelBuilder.cs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,15 @@ private TypeModel CreateTypeModel(XmlSchemaComplexType complexType)
551551
}
552552
}
553553

554+
if (complexType.ContentModel?.Content is XmlSchemaSimpleContentRestriction simpleContentRestriction)
555+
{
556+
var enumFacets = simpleContentRestriction.Facets?.OfType<XmlSchemaEnumerationFacet>().ToList();
557+
if (enumFacets?.Count > 0 && !_configuration.EnumAsString)
558+
{
559+
classModel.TextValueType = CreateSimpleContentEnumModel(classModel, enumFacets);
560+
}
561+
}
562+
554563
XmlSchemaParticle xmlParticle = null;
555564
if (classModel.BaseClass != null)
556565
{
@@ -723,6 +732,49 @@ private static List<EnumValueModel> EnsureEnumValuesUnique(List<EnumValueModel>
723732
return enumModelValues;
724733
}
725734

735+
private EnumModel CreateSimpleContentEnumModel(ClassModel classModel, List<XmlSchemaEnumerationFacet> enumFacets)
736+
{
737+
var enumNamespace = namespaceModel?.Key.XmlSchemaNamespace ?? qualifiedName.Namespace;
738+
var enumQualifiedName = qualifiedName.IsEmpty
739+
? new XmlQualifiedName($"{classModel.Name}Enum", enumNamespace)
740+
: new XmlQualifiedName($"{qualifiedName.Name}Enum", enumNamespace);
741+
742+
var enumName = $"{classModel.Name}Enum";
743+
if (namespaceModel != null)
744+
enumName = namespaceModel.GetUniqueTypeName(enumName);
745+
746+
var enumModel = new EnumModel(_configuration)
747+
{
748+
Name = enumName,
749+
Namespace = namespaceModel,
750+
XmlSchemaName = enumQualifiedName,
751+
IsAnonymous = false,
752+
};
753+
754+
foreach (var facet in enumFacets.DistinctBy(f => f.Value))
755+
{
756+
var value = new EnumValueModel
757+
{
758+
Name = _configuration.NamingProvider.EnumMemberNameFromValue(enumModel.Name, facet.Value, facet),
759+
Value = facet.Value
760+
};
761+
762+
var valueDocs = GetDocumentation(facet);
763+
value.Documentation.AddRange(valueDocs);
764+
765+
value.IsDeprecated = facet.Annotation?.Items.OfType<XmlSchemaAppInfo>()
766+
.Any(a => Array.Exists(a.Markup, m => m.Name == "annox:annotate" && m.HasChildNodes && m.FirstChild.Name == "jl:Deprecated")) == true;
767+
768+
enumModel.Values.Add(value);
769+
}
770+
771+
enumModel.Values = EnsureEnumValuesUnique(enumModel.Values);
772+
if (namespaceModel != null)
773+
namespaceModel.Types[enumModel.Name] = enumModel;
774+
775+
return enumModel;
776+
}
777+
726778
private EnumModel CreateEnumModel(XmlSchemaSimpleType simpleType, List<XmlSchemaEnumerationFacet> enumFacets)
727779
{
728780
// we got an enum

XmlSchemaClassGenerator/Models/ClassModel.cs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public class ClassModel(GeneratorConfiguration configuration) : ReferenceTypeMod
1717
public bool IsMixed { get; set; }
1818
public bool IsSubstitution { get; set; }
1919
public TypeModel BaseClass { get; set; }
20+
public TypeModel TextValueType { get; set; }
2021
public List<ClassModel> DerivedTypes { get; set; } = [];
2122
public override bool IsSubtype => BaseClass != null;
2223

@@ -95,6 +96,46 @@ public override CodeTypeDeclaration Generate()
9596
if (BaseClass is ClassModel)
9697
{
9798
classDeclaration.BaseTypes.Add(BaseClass.GetReferenceFor(Namespace));
99+
100+
if (TextValueType != null && !string.IsNullOrEmpty(Configuration.TextValuePropertyName))
101+
{
102+
var textName = Configuration.TextValuePropertyName;
103+
var enableDataBinding = Configuration.EnableDataBinding;
104+
var typeReference = TextValueType.GetReferenceFor(Namespace);
105+
106+
CodeMemberField backingFieldMember = null;
107+
if (enableDataBinding)
108+
{
109+
backingFieldMember = new CodeMemberField(typeReference, textName.ToBackingField(Configuration.PrivateMemberPrefix))
110+
{
111+
Attributes = MemberAttributes.Private
112+
};
113+
classDeclaration.Members.Add(backingFieldMember);
114+
}
115+
116+
CodeMemberField text = new(typeReference, textName + PropertyModel.GetAccessors(backingFieldMember, enableDataBinding, TextValueType.GetPropertyValueTypeCode()))
117+
{
118+
Attributes = MemberAttributes.Public | MemberAttributes.New,
119+
};
120+
121+
var docs = new List<DocumentationModel> {
122+
new() { Language = English, Text = "Gets or sets the text value." },
123+
new() { Language = German, Text = "Ruft den Text ab oder legt diesen fest." }
124+
};
125+
126+
docs.AddRange(TextValueType.Documentation);
127+
128+
var attribute = AttributeDecl<XmlTextAttribute>();
129+
130+
text.Comments.AddRange(GetComments(docs).ToArray());
131+
132+
text.CustomAttributes.Add(attribute);
133+
classDeclaration.Members.Add(text);
134+
135+
var valuePropertyModel = new PropertyModel(Configuration, textName, TextValueType, this);
136+
137+
Configuration.MemberVisitor(text, valuePropertyModel);
138+
}
98139
}
99140
else if (!string.IsNullOrEmpty(Configuration.TextValuePropertyName))
100141
{

0 commit comments

Comments
 (0)