Skip to content

Commit ae18370

Browse files
author
Jim Bolla
committed
Updated HbmXsd to change enum field values to camel case.
SVN: trunk@2886
1 parent 9d31a0c commit ae18370

7 files changed

+379
-191
lines changed

src/NHibernate.Tool.HbmXsd/HbmCodeGenerator.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ public void Execute(string outputFileName)
3131
protected override void CustomizeGeneratedCode(CodeNamespace code, XmlSchema sourceSchema)
3232
{
3333
new ImproveHbmTypeNamesCommand(code).Execute();
34+
new ImproveEnumFieldsCommand(code).Execute();
3435

35-
// TODO: Rename enum fields to better casing
3636
// TODO: Rename class fields?
3737
}
3838
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
using System;
2+
using System.CodeDom;
3+
using System.Collections.Generic;
4+
using System.ComponentModel;
5+
using System.Xml.Serialization;
6+
7+
namespace NHibernate.Tool.HbmXsd
8+
{
9+
/// <summary>
10+
/// Responsible for improving the enum field names in code generated by an
11+
/// <see cref="XsdCodeGenerator" />.
12+
/// </summary>
13+
public class ImproveEnumFieldsCommand
14+
{
15+
private readonly Dictionary<string, Dictionary<string, string>> changedEnums =
16+
new Dictionary<string, Dictionary<string, string>>();
17+
18+
private readonly CodeNamespace code;
19+
20+
public ImproveEnumFieldsCommand(CodeNamespace code)
21+
{
22+
if (code == null)
23+
throw new ArgumentNullException("code");
24+
25+
this.code = code;
26+
}
27+
28+
/// <summary>Changes enum field names to use camel casing.</summary>
29+
public void Execute()
30+
{
31+
ChangeDeclaredEnumFields();
32+
UpdateReferences();
33+
}
34+
35+
private void ChangeDeclaredEnumFields()
36+
{
37+
foreach (CodeTypeDeclaration type in code.Types)
38+
if (type.IsEnum)
39+
{
40+
changedEnums[type.Name] = new Dictionary<string, string>();
41+
42+
foreach (CodeTypeMember member in type.Members)
43+
{
44+
string xmlEnumValue = GetXmlEnumValue(member);
45+
string newEnumName = GetNewEnumName(type.Name, member.Name, xmlEnumValue);
46+
changedEnums[type.Name][member.Name] = newEnumName;
47+
member.Name = newEnumName;
48+
}
49+
}
50+
}
51+
52+
private void UpdateReferences()
53+
{
54+
foreach (CodeTypeDeclaration type in code.Types)
55+
if (type.IsClass)
56+
foreach (CodeTypeMember member in type.Members)
57+
{
58+
CodeMemberField field = member as CodeMemberField;
59+
CodeConstructor constructor = member as CodeConstructor;
60+
61+
if (field != null)
62+
UpdateFieldEnumValueReferences(field);
63+
64+
else if (constructor != null)
65+
UpdateMethodEnumValueReferences(constructor);
66+
}
67+
}
68+
69+
private static string GetXmlEnumValue(CodeTypeMember member)
70+
{
71+
if (member.CustomAttributes.Count == 1)
72+
{
73+
CodeAttributeArgument argument = member.CustomAttributes[0].Arguments[0];
74+
return ((CodePrimitiveExpression) argument.Value).Value.ToString();
75+
}
76+
else
77+
{
78+
AddXmlEnumAttribute(member);
79+
return null;
80+
}
81+
}
82+
83+
protected virtual string GetNewEnumName(string typeName, string memberName, string xmlEnumValue)
84+
{
85+
if (xmlEnumValue != null)
86+
return StringTools.CamelCase(xmlEnumValue);
87+
else
88+
return StringTools.CamelCase(memberName);
89+
}
90+
91+
private static void AddXmlEnumAttribute(CodeTypeMember member)
92+
{
93+
CodeTypeReference attributeType = new CodeTypeReference(typeof (XmlEnumAttribute));
94+
CodePrimitiveExpression argumentValue = new CodePrimitiveExpression(member.Name);
95+
CodeAttributeArgument argument = new CodeAttributeArgument(argumentValue);
96+
CodeAttributeDeclaration attribute = new CodeAttributeDeclaration(attributeType, argument);
97+
98+
member.CustomAttributes.Add(attribute);
99+
}
100+
101+
private void UpdateFieldEnumValueReferences(CodeTypeMember field)
102+
{
103+
foreach (CodeAttributeDeclaration attribute in field.CustomAttributes)
104+
if (attribute.Name == typeof (DefaultValueAttribute).FullName)
105+
{
106+
CodeFieldReferenceExpression reference = attribute.Arguments[0].Value
107+
as CodeFieldReferenceExpression;
108+
109+
if (reference != null)
110+
UpdateReference(reference);
111+
}
112+
}
113+
114+
private void UpdateMethodEnumValueReferences(CodeMemberMethod method)
115+
{
116+
foreach (CodeStatement statement in method.Statements)
117+
{
118+
CodeAssignStatement assignment = (CodeAssignStatement) statement;
119+
CodeFieldReferenceExpression right = assignment.Right as CodeFieldReferenceExpression;
120+
121+
if (right != null)
122+
UpdateReference(right);
123+
}
124+
}
125+
126+
private void UpdateReference(CodeFieldReferenceExpression reference)
127+
{
128+
string type = ((CodeTypeReferenceExpression) reference.TargetObject).Type.BaseType;
129+
130+
if (changedEnums[type].ContainsKey(reference.FieldName))
131+
reference.FieldName = changedEnums[type][reference.FieldName];
132+
}
133+
}
134+
}

src/NHibernate.Tool.HbmXsd/ImproveHbmTypeNamesCommand.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ protected override string GetNewTypeName(string originalName, string rootElement
2727

2828
case "customSQL":
2929
case "cacheType":
30-
return Prefix + CamelCase(originalName);
30+
return Prefix + StringTools.CamelCase(originalName);
3131

3232
default:
3333
return Prefix + base.GetNewTypeName(originalName, rootElementName);

src/NHibernate.Tool.HbmXsd/ImproveTypeNamesCommand.cs

+4-15
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,9 @@ private static string GetRootElementName(CodeTypeMember type)
7070
protected virtual string GetNewTypeName(string originalName, string rootElementName)
7171
{
7272
if (rootElementName == null)
73-
return CamelCase(originalName);
73+
return StringTools.CamelCase(originalName);
7474
else
75-
return CamelCase(rootElementName);
75+
return StringTools.CamelCase(rootElementName);
7676
}
7777

7878
private void UpdateFieldTypeReferences(CodeMemberField field)
@@ -90,7 +90,8 @@ private void UpdateFieldTypeReferences(CodeMemberField field)
9090
}
9191
else if (attribute.Name == typeof (DefaultValueAttribute).FullName)
9292
{
93-
CodeFieldReferenceExpression reference = attribute.Arguments[0].Value as CodeFieldReferenceExpression;
93+
CodeFieldReferenceExpression reference = attribute.Arguments[0].Value
94+
as CodeFieldReferenceExpression;
9495

9596
if (reference != null)
9697
UpdateTypeReference(((CodeTypeReferenceExpression) reference.TargetObject).Type);
@@ -114,17 +115,5 @@ private void UpdateTypeReference(CodeTypeReference type)
114115
if (changedTypeNames.ContainsKey(type.BaseType))
115116
type.BaseType = changedTypeNames[type.BaseType];
116117
}
117-
118-
protected static string CamelCase(string name)
119-
{
120-
// TODO: handle long acronyms such as SQL -> Sql
121-
122-
string[] parts = name.Split('-');
123-
124-
for (int i = 0; i < parts.Length; i++)
125-
parts[i] = parts[i].Substring(0, 1).ToUpper() + parts[i].Substring(1);
126-
127-
return string.Join("", parts);
128-
}
129118
}
130119
}

src/NHibernate.Tool.HbmXsd/NHibernate.Tool.HbmXsd.csproj

+2
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,12 @@
3838
<SubType>Designer</SubType>
3939
</EmbeddedResource>
4040
<Compile Include="HbmCodeGenerator.cs" />
41+
<Compile Include="ImproveEnumFieldsCommand.cs" />
4142
<Compile Include="ImproveHbmTypeNamesCommand.cs" />
4243
<Compile Include="Program.cs" />
4344
<Compile Include="Properties\AssemblyInfo.cs" />
4445
<Compile Include="ImproveTypeNamesCommand.cs" />
46+
<Compile Include="StringTools.cs" />
4547
<Compile Include="XsdCodeGenerator.cs" />
4648
</ItemGroup>
4749
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
namespace NHibernate.Tool.HbmXsd
2+
{
3+
public class StringTools
4+
{
5+
public static string CamelCase(string name)
6+
{
7+
// TODO: handle long acronyms such as SQL -> Sql
8+
9+
string[] parts = name.Split('-');
10+
11+
for (int i = 0; i < parts.Length; i++)
12+
parts[i] = parts[i].Substring(0, 1).ToUpper() + parts[i].Substring(1);
13+
14+
return string.Join("", parts);
15+
}
16+
}
17+
}

0 commit comments

Comments
 (0)