Skip to content

Commit

Permalink
Order for AnyAttribute, optional DebuggerStepThroughAttribute (new co…
Browse files Browse the repository at this point in the history
…mmand line option)
  • Loading branch information
Henrik Fuchs committed May 11, 2017
1 parent d94aead commit 9e372e2
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 6 deletions.
8 changes: 6 additions & 2 deletions XmlSchemaClassGenerator.Console/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ static void Main(string[] args)
Type collectionImplementationType = null;
var codeTypeReferenceOptions = default(CodeTypeReferenceOptions);
string textValuePropertyName = "Value";
var generateDebuggerStepThroughAttribute = true;

var options = new OptionSet {
{ "h|help", "show this message and exit", v => showHelp = v != null },
Expand Down Expand Up @@ -72,7 +73,8 @@ A file name may be given by appending a pipe sign (|) followed by a file name (l
{ "ct|collectionType=", "collection type to use (default is " + typeof(Collection<>).FullName + ")", v => collectionType = v == null ? typeof(Collection<>) : Type.GetType(v, true) },
{ "cit|collectionImplementationType=", "the default collection type implementation to use (default is null)", v => collectionImplementationType = v == null ? null : Type.GetType(v, true) },
{ "ctro|codeTypeReferenceOptions=", "the default CodeTypeReferenceOptions Flags to use (default is unset; can be: {GlobalReference, GenericTypeParameter})", v => codeTypeReferenceOptions = v == null ? default(CodeTypeReferenceOptions) : (CodeTypeReferenceOptions)Enum.Parse(typeof(CodeTypeReferenceOptions), v, false) },
{ "tvpn|textValuePropertyName=", "the name of the property that holds the text value of an element (default is Value)", v => textValuePropertyName = v }
{ "tvpn|textValuePropertyName=", "the name of the property that holds the text value of an element (default is Value)", v => textValuePropertyName = v },
{ "dst|debuggerStepThrough", "generate DebuggerStepThroughAttribute (default is enabled)", v => generateDebuggerStepThroughAttribute = v != null }
};

var files = options.Parse(args);
Expand Down Expand Up @@ -113,14 +115,16 @@ A file name may be given by appending a pipe sign (|) followed by a file name (l
CollectionType = collectionType,
CollectionImplementationType = collectionImplementationType,
CodeTypeReferenceOptions = codeTypeReferenceOptions,
TextValuePropertyName = textValuePropertyName
TextValuePropertyName = textValuePropertyName,
GenerateDebuggerStepThroughAttribute = generateDebuggerStepThroughAttribute
};

if (pclCompatible)
{
generator.UseXElementForAny = true;
generator.GenerateDesignerCategoryAttribute = false;
generator.GenerateSerializableAttribute = false;
generator.GenerateDebuggerStepThroughAttribute = false;
generator.DataAnnotationMode = DataAnnotationMode.None;
}

Expand Down
22 changes: 21 additions & 1 deletion XmlSchemaClassGenerator/Generator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,12 @@ public bool GenerateSerializableAttribute
set { _configuration.GenerateSerializableAttribute = value; }
}

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

public bool GenerateDesignerCategoryAttribute
{
get { return _configuration.GenerateDesignerCategoryAttribute; }
Expand Down Expand Up @@ -772,6 +778,7 @@ private IEnumerable<PropertyModel> CreatePropertiesForElements(Uri source, TypeM
{
var properties = new List<PropertyModel>();
var order = 0;
var count = 0;
foreach (var item in items)
{
PropertyModel property = null;
Expand Down Expand Up @@ -802,6 +809,7 @@ private IEnumerable<PropertyModel> CreatePropertiesForElements(Uri source, TypeM
}

var propertyName = ToTitleCase(element.QualifiedName.Name);
propertyName = CreatePrefix(count, propertyName);
if (propertyName == typeModel.Name)
{
propertyName += "Property"; // member names cannot be the same as their enclosing type
Expand All @@ -811,7 +819,8 @@ private IEnumerable<PropertyModel> CreatePropertiesForElements(Uri source, TypeM
{
OwningType = typeModel,
XmlSchemaName = element.QualifiedName,
Name = propertyName,
Name = propertyName, // TODO: Name = originalName,
// TODO: OrderName = propertyName,
Type = CreateTypeModel(source, element.ElementSchemaType, elementQualifiedName),
IsNillable = element.IsNillable,
IsNullable = item.MinOccurs < 1.0m,
Expand Down Expand Up @@ -869,11 +878,22 @@ private IEnumerable<PropertyModel> CreatePropertiesForElements(Uri source, TypeM

properties.Add(property);
}

count++;
}

return properties;
}

private static string CreatePrefix(int count, string propertyName)
{
if ((char)('a' + count) <= 'z')
propertyName = (char)('a' + count) + "_" + propertyName;
else
propertyName = "z" + (char)('a' + (count - 26)) + "_" + propertyName;
return propertyName;
}

private NamespaceModel CreateNamespaceModel(Uri source, XmlQualifiedName qualifiedName)
{
NamespaceModel namespaceModel = null;
Expand Down
4 changes: 4 additions & 0 deletions XmlSchemaClassGenerator/GeneratorConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ public GeneratorConfiguration()
/// </summary>
public bool GenerateSerializableAttribute { get; set; }
/// <summary>
/// Generate the DebuggerStepThroughAttribute?
/// </summary>
public bool GenerateDebuggerStepThroughAttribute { get; set; }
/// <summary>
/// Generate the DesignerCategoryAttribute?
/// </summary>
public bool GenerateDesignerCategoryAttribute { get; set; }
Expand Down
11 changes: 8 additions & 3 deletions XmlSchemaClassGenerator/TypeModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -361,8 +361,10 @@ public override CodeTypeDeclaration Generate()
classDeclaration.Members.Add(text);
}

classDeclaration.CustomAttributes.Add(
new CodeAttributeDeclaration(new CodeTypeReference(typeof(DebuggerStepThroughAttribute), Configuration.CodeTypeReferenceOptions)));
if (Configuration.GenerateDebuggerStepThroughAttribute)
classDeclaration.CustomAttributes.Add(
new CodeAttributeDeclaration(new CodeTypeReference(typeof(DebuggerStepThroughAttribute), Configuration.CodeTypeReferenceOptions)));

if (Configuration.GenerateDesignerCategoryAttribute)
{
classDeclaration.CustomAttributes.Add(
Expand Down Expand Up @@ -864,7 +866,10 @@ private IEnumerable<CodeAttributeDeclaration> GetAttributes(bool isArray)
{
if (IsAny)
{
attributes.Add(new CodeAttributeDeclaration(new CodeTypeReference(typeof(XmlAnyAttributeAttribute), Configuration.CodeTypeReferenceOptions)));
var anyAttribute = new CodeAttributeDeclaration(new CodeTypeReference(typeof(XmlAnyElementAttribute), Configuration.CodeTypeReferenceOptions));
if (Order != null)
anyAttribute.Arguments.Add(new CodeAttributeArgument("Order", new CodePrimitiveExpression(Order.Value)));
attributes.Add(anyAttribute);
}
else
{
Expand Down

0 comments on commit 9e372e2

Please sign in to comment.