Skip to content

Commit

Permalink
Merge branch 'master' of github.com:mganss/XmlSchemaClassGenerator
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Ganss committed Apr 9, 2018
2 parents 56d8428 + 4f812a9 commit 1b8d119
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 20 deletions.
4 changes: 3 additions & 1 deletion XmlSchemaClassGenerator.Console/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ static void Main(string[] args)
string textValuePropertyName = "Value";
var generateDebuggerStepThroughAttribute = true;
var disableComments = false;
var doNotUseUnderscoreInPrivateMemberNames = false;

var options = new OptionSet {
{ "h|help", "show this message and exit", v => showHelp = v != null },
Expand Down Expand Up @@ -77,6 +78,7 @@ A file name may be given by appending a pipe sign (|) followed by a file name (l
{ "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 },
{ "dc|disableComments", "do not include comments from xsd", v => disableComments = v != null },
{ "nu|noUnderscore", "do not generate underscore in private member name (default is false)", v => doNotUseUnderscoreInPrivateMemberNames = v != null },
};

var files = options.Parse(args);
Expand Down Expand Up @@ -130,7 +132,7 @@ A file name may be given by appending a pipe sign (|) followed by a file name (l
generator.GenerateDebuggerStepThroughAttribute = false;
generator.DataAnnotationMode = DataAnnotationMode.None;
}

generator.DoNotUseUnderscoreInPrivateMemberNames = doNotUseUnderscoreInPrivateMemberNames;
if (verbose) { generator.Log = s => System.Console.Out.WriteLine(s); }

generator.Generate(files);
Expand Down
42 changes: 35 additions & 7 deletions XmlSchemaClassGenerator/CodeUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ public static string ToCamelCase(this string s)
return char.ToLowerInvariant(s[0]) + s.Substring(1);
}

public static string ToBackingField(this string propertyName)
public static string ToBackingField(this string propertyName, bool doNotUseUnderscoreInPrivateMemberNames)
{
return string.Concat("_", propertyName.ToCamelCase());
return doNotUseUnderscoreInPrivateMemberNames ? propertyName.ToCamelCase() : string.Concat("_", propertyName.ToCamelCase());
}

private static bool? IsDataTypeAttributeAllowed(XmlTypeCode typeCode, GeneratorConfiguration configuration)
Expand Down Expand Up @@ -171,17 +171,21 @@ public static string GetUniqueTypeName(this NamespaceModel model, string name)

public static string GetUniqueFieldName(this TypeModel typeModel, PropertyModel propertyModel)
{
var propBackingFieldName = propertyModel.Name.ToBackingField();
var classModel = typeModel as ClassModel;
var propBackingFieldName = propertyModel.Name.ToBackingField(classModel?.Configuration.DoNotUseUnderscoreInPrivateMemberNames == true);

if (CSharpKeywords.Contains(propBackingFieldName.ToLower()))
propBackingFieldName = "@" + propBackingFieldName;

if (classModel == null)
{
return propBackingFieldName;
}

var i = 0;
foreach (var prop in classModel.Properties)
{
if (!classModel.EnableDataBinding && !(prop.Type is SimpleModel))
if (!classModel.Configuration.EnableDataBinding && !(prop.Type is SimpleModel))
{
continue;
}
Expand All @@ -192,7 +196,7 @@ public static string GetUniqueFieldName(this TypeModel typeModel, PropertyModel
break;
}

var backingFieldName = prop.Name.ToBackingField();
var backingFieldName = prop.Name.ToBackingField(classModel.Configuration.DoNotUseUnderscoreInPrivateMemberNames);
if (backingFieldName == propBackingFieldName)
{
i += 1;
Expand All @@ -207,11 +211,35 @@ public static string GetUniqueFieldName(this TypeModel typeModel, PropertyModel
return string.Format("{0}{1}", propBackingFieldName, i);
}

static readonly Regex NormalizeNewlinesRegex = new Regex(@"(^|[^\r])\n",RegexOptions.Compiled);
static readonly Regex NormalizeNewlinesRegex = new Regex(@"(^|[^\r])\n", RegexOptions.Compiled);

internal static string NormalizeNewlines(string text)
{
return NormalizeNewlinesRegex.Replace(text, "$1\r\n");
}

static readonly List<string> CSharpKeywords = new List<string>
{
"abstract", "as", "base", "bool",
"break", "byte", "case", "catch",
"char", "checked", "class", "const",
"continue", "decimal", "default", "delegate",
"do", "double", "else", "enum",
"event", " explicit", "extern", "false",
"finally", "fixed", "float", "for",
"foreach", "goto", "if", "implicit",
"in", "int", "interface", "internal",
"is", "lock", "long", "namespace",
"new", "null", "object", "operator",
"out", "override", "params", "private",
"protected", "public", "readonly", "ref",
"return", "sbyte", "sealed", "short",
"sizeof", "stackalloc", "static", "string",
"struct", "switch", "this", "throw",
"true", "try", "typeof", "uint",
"ulong", "unchecked", "unsafe", "ushort",
"using", "using static", "virtual", "void",
"volatile", "while"
};
}
}
13 changes: 9 additions & 4 deletions XmlSchemaClassGenerator/Generator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,12 @@ public bool DisableComments
get { return _configuration.DisableComments; }
set { _configuration.DisableComments = value; }
}

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

private readonly XmlSchemaSet Set = new XmlSchemaSet();
private Dictionary<XmlQualifiedName, XmlSchemaAttributeGroup> AttributeGroups;
Expand All @@ -188,8 +194,8 @@ public void Generate(IEnumerable<string> files)
}));

foreach (var s in schemas)
{
Set.Add(s);
{
Set.Add(s.TargetNamespace, s.SourceUri);
}

Set.Compile();
Expand Down Expand Up @@ -413,8 +419,7 @@ private TypeModel CreateTypeModel(Uri source, XmlSchemaAnnotated type, XmlQualif
IsAbstract = complexType.IsAbstract,
IsAnonymous = complexType.QualifiedName.Name == "",
IsMixed = complexType.IsMixed,
IsSubstitution = complexType.Parent is XmlSchemaElement && !((XmlSchemaElement)complexType.Parent).SubstitutionGroup.IsEmpty,
EnableDataBinding = EnableDataBinding,
IsSubstitution = complexType.Parent is XmlSchemaElement && !((XmlSchemaElement)complexType.Parent).SubstitutionGroup.IsEmpty
};

classModel.Documentation.AddRange(docs);
Expand Down
2 changes: 2 additions & 0 deletions XmlSchemaClassGenerator/GeneratorConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,5 +158,7 @@ public void WriteLog(string message)
public NamingProvider NamingProvider { get; set; }

public bool DisableComments { get; set; }
public bool DoNotUseUnderscoreInPrivateMemberNames { get; set; }

}
}
15 changes: 7 additions & 8 deletions XmlSchemaClassGenerator/TypeModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public static CodeNamespace Generate(string namespaceName, IEnumerable<Namespace
codeNamespace.Imports.Add(new CodeNamespaceImport("System.Xml.Serialization"));

var typeModels = parts.SelectMany(x => x.Types.Values).ToList();
if (typeModels.OfType<ClassModel>().Any(x => x.EnableDataBinding))
if (typeModels.OfType<ClassModel>().Any(x => x.Configuration.EnableDataBinding))
{
codeNamespace.Imports.Add(new CodeNamespaceImport("System.Linq"));
codeNamespace.Imports.Add(new CodeNamespaceImport("System.ComponentModel"));
Expand Down Expand Up @@ -210,8 +210,6 @@ public class ClassModel : TypeModel
public List<PropertyModel> Properties { get; set; }
public List<InterfaceModel> Interfaces { get; set; }
public List<ClassModel> DerivedTypes { get; set; }
public bool EnableDataBinding { get; set; }

public ClassModel(GeneratorConfiguration configuration)
: base(configuration)
{
Expand Down Expand Up @@ -243,7 +241,7 @@ public override CodeTypeDeclaration Generate()
classDeclaration.IsClass = true;
classDeclaration.IsPartial = true;

if (EnableDataBinding)
if (Configuration.EnableDataBinding)
{
classDeclaration.Members.Add(new CodeMemberEvent()
{
Expand Down Expand Up @@ -290,9 +288,9 @@ public override CodeTypeDeclaration Generate()
Attributes = MemberAttributes.Public,
};

if (EnableDataBinding)
if (Configuration.EnableDataBinding)
{
var backingFieldMember = new CodeMemberField(typeReference, member.Name.ToBackingField())
var backingFieldMember = new CodeMemberField(typeReference, member.Name.ToBackingField(Configuration.DoNotUseUnderscoreInPrivateMemberNames))
{
Attributes = MemberAttributes.Private
};
Expand Down Expand Up @@ -325,7 +323,7 @@ public override CodeTypeDeclaration Generate()
}
}

if (EnableDataBinding)
if (Configuration.EnableDataBinding)
{
classDeclaration.BaseTypes.Add(new CodeTypeReference(typeof(INotifyPropertyChanged), Configuration.CodeTypeReferenceOptions));
}
Expand Down Expand Up @@ -353,7 +351,7 @@ public override CodeTypeDeclaration Generate()
}

foreach (var property in Properties)
property.AddMembersTo(classDeclaration, EnableDataBinding);
property.AddMembersTo(classDeclaration, Configuration.EnableDataBinding);

if (IsMixed && (BaseClass == null || (BaseClass is ClassModel && !AllBaseClasses.Any(b => b.IsMixed))))
{
Expand Down Expand Up @@ -692,6 +690,7 @@ public void AddMembersTo(CodeTypeDeclaration typeDeclaration, bool withDataBindi
member = new CodeMemberField(typeReference, propertyName);

var isPrivateSetter = IsCollection || isArray;

if (requiresBackingField)
{
member.Name += GetAccessors(member.Name, backingField.Name,
Expand Down

0 comments on commit 1b8d119

Please sign in to comment.