-
Notifications
You must be signed in to change notification settings - Fork 925
/
Generator.cs
230 lines (196 loc) · 6.64 KB
/
Generator.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
using System;
using System.Collections;
using System.Collections.Specialized;
using System.IO;
using System.Reflection;
using log4net;
using NHibernate.Util;
using Element = System.Xml.XmlElement;
namespace NHibernate.Tool.hbm2net
{
/// <summary> </summary>
public class Generator
{
private static readonly ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
virtual public string BaseDirName
{
get
{
return baseDirName;
}
set
{
if ((Object) value != null)
{
this.baseDirName = value;
}
}
}
private string rendererClass = "NHibernate.Tool.hbm2net.VelocityRenderer";
private string baseDirName = "generated";
private string packageName = null;
private string suffix;
private string prefix;
private string extension = "cs";
private bool lowerFirstLetter = false;
private DirectoryInfo workingDirectory;
public NameValueCollection params_Renamed = new NameValueCollection();
/// <summary> Constructs a new Generator using the defaults.</summary>
public Generator(DirectoryInfo workingDirectory)
{
this.workingDirectory = workingDirectory;
this.suffix = string.Empty;
this.prefix = string.Empty;
}
/// <summary> Constructs a new Generator, configured from XML.</summary>
public Generator(DirectoryInfo workingDirectory, Element generateElement) : this(workingDirectory)
{
// InitBlock();
string value_Renamed = null;
// set rendererClass field
if ((Object) (this.rendererClass = (generateElement.Attributes["renderer"] == null?null:generateElement.Attributes["renderer"].Value)) == null)
{
throw new Exception("attribute renderer is required.");
}
// set dirName field
if ((Object) (value_Renamed = (generateElement.Attributes["dir"] == null?null:generateElement.Attributes["dir"].Value)) != null)
{
this.baseDirName = value_Renamed;
}
// set packageName field
this.packageName = (generateElement.Attributes["package"] == null?null:generateElement.Attributes["package"].Value);
// set prefix
if ((Object) (value_Renamed = (generateElement.Attributes["prefix"] == null?null:generateElement.Attributes["prefix"].Value)) != null)
{
this.prefix = value_Renamed;
}
// set suffix
if ((Object) (value_Renamed = (generateElement.Attributes["suffix"] == null?null:generateElement.Attributes["suffix"].Value)) != null)
{
this.suffix = value_Renamed;
}
// set extension
if ((Object) (value_Renamed = (generateElement.Attributes["extension"] == null?null:generateElement.Attributes["extension"].Value)) != null)
{
this.extension = value_Renamed;
}
// set lowerFirstLetter
value_Renamed = (generateElement.Attributes["lowerFirstLetter"] == null?null:generateElement.Attributes["lowerFirstLetter"].Value);
try
{
this.lowerFirstLetter = Boolean.Parse(value_Renamed);
}
catch{}
IEnumerator iter = generateElement.SelectNodes("param").GetEnumerator();
while (iter.MoveNext())
{
Element childNode = (Element) iter.Current;
params_Renamed[childNode.Attributes["name"].Value] = childNode.InnerText;
}
}
/// <summary> </summary>
public virtual void generate(IDictionary classMappingsCol)
{
log.Info("Generating " + classMappingsCol.Count + " in " + BaseDirName);
Renderer renderer = (Renderer) SupportClass.CreateNewInstance(System.Type.GetType(this.rendererClass));
//Configure renderer
renderer.configure(workingDirectory, params_Renamed);
//Running through actual classes
for (IEnumerator classMappings = classMappingsCol.Values.GetEnumerator(); classMappings.MoveNext(); )
{
ClassMapping classMapping = (ClassMapping) classMappings.Current;
writeRecur(classMapping, classMappingsCol, renderer);
}
//Running through components
for (IEnumerator cmpMappings = ClassMapping.Components; cmpMappings.MoveNext(); )
{
ClassMapping mapping = (ClassMapping) cmpMappings.Current;
write(mapping, classMappingsCol, renderer);
}
}
private void writeRecur(ClassMapping classMapping, IDictionary class2classmap, Renderer renderer)
{
write(classMapping, class2classmap, renderer);
if (!(classMapping.Subclasses.Count == 0))
{
IEnumerator it = classMapping.Subclasses.GetEnumerator();
while (it.MoveNext())
{
writeRecur((ClassMapping) it.Current, class2classmap, renderer);
}
}
}
/// <summary> </summary>
private void write(ClassMapping classMapping, IDictionary class2classmap, Renderer renderer)
{
string saveToPackage = renderer.getSaveToPackage(classMapping);
string saveToClassName = renderer.getSaveToClassName(classMapping);
FileInfo dir = this.getDir(saveToPackage);
FileInfo file = new FileInfo(Path.Combine(dir.FullName, this.getFileName(saveToClassName)));
log.Debug("Writing " + file);
StreamWriter writer = new StreamWriter(new FileStream(file.FullName, FileMode.Create));
renderer.render(getPackageName(saveToPackage), getName(saveToClassName), classMapping, class2classmap, writer);
writer.Close();
}
/// <summary> </summary>
private string getFileName(string className)
{
return this.getName(className) + "." + this.extension;
}
/// <summary> </summary>
private string getName(string className)
{
string name = null;
if (this.lowerFirstLetter)
{
name = className.Substring(0, (1) - (0)).ToLower() + className.Substring(1, (className.Length) - (1));
}
else
{
name = className;
}
return this.prefix + name + this.suffix;
}
private string getPackageName(string packageName)
{
if ((Object) this.packageName == null)
{
return (Object) packageName == null?string.Empty:packageName;
}
else
{
return this.packageName;
}
}
/// <summary> </summary>
private FileInfo getDir(string packageName)
{
FileInfo baseDir = new FileInfo(this.baseDirName);
FileInfo dir = null;
string p = getPackageName(packageName);
dir = new FileInfo(Path.Combine(baseDir.FullName, p.Replace(StringHelper.Dot, Path.DirectorySeparatorChar)));
// if the directory exists, make sure it is a directory
bool tmpBool;
if (File.Exists(dir.FullName))
tmpBool = true;
else
tmpBool = Directory.Exists(dir.FullName);
if (tmpBool)
{
if (!Directory.Exists(dir.FullName))
{
throw new Exception("The path: " + dir.FullName + " exists, but is not a directory");
}
}
// else make the directory and any non-existent parent directories
else
{
if (!Directory.CreateDirectory(dir.FullName).Exists)
{
throw new Exception("unable to create directory: " + dir.FullName);
}
}
return dir;
}
}
}