1
|
|
#region Copyright
|
2
|
|
|
3
|
|
|
4
|
|
|
5
|
|
|
6
|
|
|
7
|
|
|
8
|
|
|
9
|
|
|
10
|
|
|
11
|
|
|
12
|
|
|
13
|
|
|
14
|
|
|
15
|
|
|
16
|
|
|
17
|
|
|
18
|
|
|
19
|
|
|
20
|
|
|
21
|
|
|
22
|
|
#endregion
|
23
|
|
|
24
|
|
using System;
|
25
|
|
using System.Xml;
|
26
|
|
using System.Collections;
|
27
|
|
using System.Reflection;
|
28
|
|
using log4net;
|
29
|
|
|
30
|
|
namespace ShowX.Model.XmlModel
|
31
|
|
{
|
32
|
|
|
33
|
|
|
34
|
|
|
35
|
|
public class XmlClassMap
|
36
|
|
{
|
37
|
|
private static readonly ILog log = LogManager.GetLogger(
|
38
|
|
typeof(XmlClassMap));
|
39
|
|
|
40
|
|
|
41
|
|
|
42
|
|
|
43
|
|
Type mappedType;
|
44
|
|
|
45
|
|
|
46
|
|
|
47
|
|
|
48
|
|
Type handlerType;
|
49
|
|
|
50
|
|
|
51
|
|
|
52
|
|
|
53
|
|
XmlIdentifierMap pkProperty;
|
54
|
|
|
55
|
|
|
56
|
|
|
57
|
|
|
58
|
|
|
59
|
|
|
60
|
|
Hashtable propertyMappingInfo;
|
61
|
|
|
62
|
|
|
63
|
|
|
64
|
|
ArrayList propertyMappingList;
|
65
|
|
|
66
|
|
|
67
|
|
|
68
|
|
|
69
|
|
XmlViewCollection viewCollection;
|
70
|
|
|
71
|
|
|
72
|
|
|
73
|
|
|
74
|
|
|
75
|
|
protected static string classMappingSchemeEmbeddedPath =
|
76
|
|
"ShowX.Model.XmlModel.Schema.class-mapping.xsd";
|
77
|
|
|
78
|
|
|
79
|
|
|
80
|
|
|
81
|
|
|
82
|
|
|
83
|
32
|
protected static bool validate(XmlNode section)
|
84
|
|
{
|
85
|
32
|
return new XmlFileValidator()
|
86
|
|
.Validate(section,classMappingSchemeEmbeddedPath);
|
87
|
|
}
|
88
|
|
|
89
|
|
|
90
|
|
|
91
|
|
|
92
|
|
|
93
|
|
|
94
|
|
|
95
|
|
|
96
|
30
|
protected Type LoadType(string typename)
|
97
|
|
{
|
98
|
30
|
typename = typename.Replace(" ","");
|
99
|
30
|
string[] split = typename.Split(new char[]{','});
|
100
|
|
|
101
|
30
|
Assembly asm = (split.Length == 2)
|
102
|
|
? Assembly.LoadWithPartialName (split[1])
|
103
|
|
: this.mappedType.UnderlyingSystemType.Assembly;
|
104
|
|
|
105
|
30
|
if(asm == null) {
|
106
|
|
|
107
|
1
|
string errorMsg = string.Format(
|
108
|
|
"Unable to load assembly for type '{0}'.",typename);
|
109
|
|
|
110
|
1
|
log.Fatal(errorMsg);
|
111
|
1
|
throw new ModelException(errorMsg);
|
112
|
|
}
|
113
|
|
|
114
|
29
|
return asm.GetType(split[0],false);
|
115
|
|
}
|
116
|
|
|
117
|
|
|
118
|
|
|
119
|
|
|
120
|
|
|
121
|
|
|
122
|
|
|
123
|
35
|
public XmlClassMap(Type t, XmlDocument mapping)
|
124
|
|
{
|
125
|
35
|
mappedType = t;
|
126
|
35
|
propertyMappingInfo = new Hashtable();
|
127
|
35
|
propertyMappingList = new ArrayList();
|
128
|
|
|
129
|
35
|
if (mapping == null) {
|
130
|
|
|
131
|
1
|
log.Fatal("Detected a null Xml mapping document.");
|
132
|
1
|
throw new ModelException("Xml mapping document cannot be null");
|
133
|
|
}
|
134
|
|
|
135
|
34
|
if (mappedType == null) {
|
136
|
|
|
137
|
1
|
log.Fatal("Detected a null type.");
|
138
|
1
|
throw new ModelException("Mapped type cannot be null.");
|
139
|
|
}
|
140
|
|
|
141
|
33
|
XmlNode rootTag = mapping
|
142
|
|
.SelectSingleNode("//" + XmlMappingFileConst.rootTag);
|
143
|
|
|
144
|
33
|
if (rootTag == null) {
|
145
|
|
|
146
|
1
|
string errorMsg = string.Format(
|
147
|
|
"Mapping settings does not contains the '{0}' tag",
|
148
|
|
XmlMappingFileConst.rootTag);
|
149
|
|
|
150
|
1
|
log.Fatal(errorMsg);
|
151
|
1
|
throw new ModelException(errorMsg);
|
152
|
|
}
|
153
|
|
|
154
|
32
|
if (!validate(rootTag)) {
|
155
|
|
|
156
|
1
|
string errorMsg = string.Format(
|
157
|
|
"Error validating class mapping file for type '{0}'.",t);
|
158
|
|
|
159
|
1
|
log.Fatal(errorMsg);
|
160
|
1
|
throw new ModelException(errorMsg);
|
161
|
|
}
|
162
|
|
|
163
|
31
|
string mappedClassNameValue = rootTag
|
164
|
|
.Attributes[XmlMappingFileConst.propertyMappingNameAttr].Value;
|
165
|
|
|
166
|
31
|
if (t.FullName != mappedClassNameValue) {
|
167
|
|
|
168
|
1
|
string errorMsg = string.Format(
|
169
|
|
"Attempt to load type '{0}' from type '{1}' mapping file.",
|
170
|
|
t,mappedClassNameValue);
|
171
|
|
|
172
|
1
|
log.Fatal(errorMsg);
|
173
|
1
|
throw new ModelException(errorMsg);
|
174
|
|
}
|
175
|
|
|
176
|
|
|
177
|
30
|
string mappedObjectHandlerValue = rootTag
|
178
|
|
.Attributes[XmlMappingFileConst.propertyMappingHandlerClassAttr].Value;
|
179
|
|
|
180
|
30
|
this.handlerType = LoadType(mappedObjectHandlerValue);
|
181
|
|
|
182
|
29
|
if (handlerType == null) {
|
183
|
|
|
184
|
1
|
string errorMsg = string.Format(
|
185
|
|
"Unable to load handler class '{0}' for type '{1}'.",
|
186
|
|
mappedObjectHandlerValue,mappedClassNameValue);
|
187
|
|
|
188
|
1
|
log.Fatal(errorMsg);
|
189
|
1
|
throw new ModelException(errorMsg);
|
190
|
|
}
|
191
|
|
|
192
|
|
|
193
|
28
|
foreach (XmlNode node in rootTag.SelectSingleNode("//" +
|
194
|
|
XmlMappingFileConst.propertyMappingTag).ChildNodes) {
|
195
|
|
|
196
|
147
|
if (node.Name == XmlMappingFileConst.idTag) {
|
197
|
|
|
198
|
|
|
199
|
28
|
this.pkProperty = new XmlIdentifierMap(t,node);
|
200
|
|
}
|
201
|
119
|
else if (node.Name == XmlMappingFileConst.propertyTag) {
|
202
|
|
|
203
|
|
|
204
|
100
|
string propertyName = node.Attributes
|
205
|
|
[XmlMappingFileConst.propertyMappingNameAttr].Value;
|
206
|
|
|
207
|
100
|
XmlPropertyMap pm = new XmlPropertyMap(t,node);
|
208
|
|
|
209
|
100
|
propertyMappingInfo[propertyName] = pm;
|
210
|
100
|
propertyMappingList.Add(pm.PropertyInfo);
|
211
|
|
}
|
212
|
19
|
else if (node.Name == XmlMappingFileConst.fkPropertyTag) {
|
213
|
|
|
214
|
|
|
215
|
8
|
string propertyName = node.Attributes
|
216
|
|
[XmlMappingFileConst.propertyMappingNameAttr].Value;
|
217
|
|
|
218
|
8
|
XmlForeignIdentifierMap fk = new XmlForeignIdentifierMap(t,node);
|
219
|
|
|
220
|
8
|
propertyMappingInfo[propertyName] = fk;
|
221
|
8
|
propertyMappingList.Add(fk.PropertyInfo);
|
222
|
|
}
|
223
|
11
|
else if (node.Name == XmlMappingFileConst.collectionPropertyTag) {
|
224
|
|
|
225
|
|
|
226
|
10
|
string propertyName = node.Attributes
|
227
|
|
[XmlMappingFileConst.propertyMappingNameAttr].Value;
|
228
|
|
|
229
|
10
|
XmlCollectionMap cm = new XmlCollectionMap(t,node);
|
230
|
|
|
231
|
10
|
propertyMappingInfo[propertyName] = cm;
|
232
|
10
|
propertyMappingList.Add(cm.PropertyInfo);
|
233
|
|
}
|
234
|
1
|
else if (node.Name == XmlMappingFileConst.xpercentPropertyTag) {
|
235
|
|
|
236
|
1
|
string propertyName = node.Attributes
|
237
|
|
[XmlMappingFileConst.propertyMappingNameAttr].Value;
|
238
|
|
|
239
|
1
|
XmlXPercentMap cm = new XmlXPercentMap(t,node);
|
240
|
|
|
241
|
1
|
propertyMappingInfo[propertyName] = cm;
|
242
|
1
|
propertyMappingList.Add(cm.PropertyInfo);
|
243
|
|
|
244
|
|
}
|
245
|
|
}
|
246
|
|
|
247
|
28
|
XmlNode viewTag = mapping
|
248
|
|
.SelectSingleNode("//" + XmlMappingFileConst.viewsTag);
|
249
|
|
|
250
|
28
|
viewCollection = (viewTag == null)
|
251
|
|
? new XmlViewCollection()
|
252
|
|
: new XmlViewCollection(viewTag);
|
253
|
|
}
|
254
|
|
|
255
|
|
|
256
|
|
|
257
|
|
|
258
|
|
public Type MappedType
|
259
|
|
{
|
260
|
1
|
get { return mappedType; }
|
261
|
|
}
|
262
|
|
|
263
|
|
|
264
|
|
|
265
|
|
|
266
|
|
|
267
|
|
public Type HandlerType
|
268
|
|
{
|
269
|
144
|
get { return handlerType; }
|
270
|
|
}
|
271
|
|
|
272
|
|
|
273
|
|
|
274
|
|
|
275
|
|
|
276
|
|
public Hashtable PropertyMaps
|
277
|
|
{
|
278
|
2153
|
get { return this.propertyMappingInfo; }
|
279
|
|
}
|
280
|
|
|
281
|
|
|
282
|
|
|
283
|
|
|
284
|
|
|
285
|
|
public ArrayList PropertyList
|
286
|
|
{
|
287
|
61
|
get { return this.propertyMappingList; }
|
288
|
|
}
|
289
|
|
|
290
|
|
|
291
|
|
|
292
|
|
|
293
|
|
|
294
|
|
public XmlPropertyMap IdentifierMap
|
295
|
|
{
|
296
|
2945
|
get { return this.pkProperty; }
|
297
|
|
}
|
298
|
|
|
299
|
|
|
300
|
|
|
301
|
|
|
302
|
|
|
303
|
|
public IViewCollection Views
|
304
|
|
{
|
305
|
36
|
get { return this.viewCollection; }
|
306
|
|
}
|
307
|
|
}
|
308
|
|
}
|