-
Notifications
You must be signed in to change notification settings - Fork 64
/
FormatJAV.cs
379 lines (321 loc) · 15.9 KB
/
FormatJAV.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
// Name: FormatJAV.cs
// Description: Java Code Generator
// Author: Tim Chipman
// Origination: Work performed for BuildingSmart by Constructivity.com LLC.
// Copyright: (c) 2013 BuildingSmart International Ltd.
// License: http://www.buildingsmart-tech.org/legal
using System;
using System.Collections.Generic;
using System.Text;
using IfcDoc.Schema.DOC;
namespace IfcDoc.Format.CSC
{
internal class FormatJAV : IDisposable
{
string m_filename;
DocProject m_project;
DocDefinition m_definition;
/// <summary>
/// Generates folder of definitions
/// </summary>
/// <param name="path"></param>
public static void GenerateCode(DocProject project, string path)
{
foreach (DocSection docSection in project.Sections)
{
foreach (DocSchema docSchema in docSection.Schemas)
{
foreach (DocType docType in docSchema.Types)
{
using (FormatJAV format = new FormatJAV(path + @"\" + docSchema.Name + @"\" + docType.Name + ".java"))
{
format.Instance = project;
format.Definition = docType;
format.Save();
}
}
foreach (DocEntity docType in docSchema.Entities)
{
using (FormatJAV format = new FormatJAV(path + @"\" + docSchema.Name + @"\" + docType.Name + ".java"))
{
format.Instance = project;
format.Definition = docType;
format.Save();
}
}
}
}
}
public FormatJAV(string filename)
{
this.m_filename = filename;
}
public DocProject Instance
{
get
{
return this.m_project;
}
set
{
this.m_project = value;
}
}
/// <summary>
/// Optional definition to save, or null for all definitions in project.
/// </summary>
public DocDefinition Definition
{
get
{
return this.m_definition;
}
set
{
this.m_definition = value;
}
}
public void Save()
{
string dirpath = System.IO.Path.GetDirectoryName(this.m_filename);
if (!System.IO.Directory.Exists(this.m_filename))
{
System.IO.Directory.CreateDirectory(dirpath);
}
using (System.IO.StreamWriter writer = new System.IO.StreamWriter(this.m_filename))
{
writer.WriteLine("// This file was automatically generated from IFCDOC at www.buildingsmart-tech.org.");
writer.WriteLine("// IFC content is copyright (C) 1996-2013 BuildingSMART International Ltd.");
writer.WriteLine();
if (this.m_definition != null)
{
writer.WriteLine("package buildingsmart.ifc");
writer.WriteLine("{");
if (this.m_definition is DocDefined)
{
// do nothing - don't make a separate type in Java, as that would require extra heap allocation
}
else if (this.m_definition is DocSelect)
{
writer.WriteLine("\tpublic interface " + this.m_definition.Name);
writer.WriteLine("\t{");
writer.WriteLine("\t}");
}
else if (this.m_definition is DocEnumeration)
{
DocEnumeration docEnumumeration = (DocEnumeration)this.m_definition;
writer.WriteLine("\tpublic enum " + this.m_definition.Name);
writer.WriteLine("\t{");
foreach (DocConstant docConstant in docEnumumeration.Constants)
{
writer.WriteLine("\t\t" + docConstant.Name + ",");
}
writer.WriteLine("\t}");
}
else if (this.m_definition is DocEntity)
{
DocEntity docEntity = (DocEntity)this.m_definition;
string basedef = docEntity.BaseDefinition;
if (String.IsNullOrEmpty(basedef))
{
basedef = "IfcBase";
}
writer.WriteLine("\tpublic class " + this.m_definition.Name + " extends " + basedef);
writer.WriteLine("\t{");
// fields
foreach (DocAttribute docAttribute in docEntity.Attributes)
{
string deftype = docAttribute.DefinedType;
// if defined type, use raw type (avoiding extra memory allocation)
DocDefinition docDef = GetDefinition(deftype);
if (docDef is DocDefined)
{
deftype = ((DocDefined)docDef).DefinedType;
switch(deftype)
{
case "STRING":
deftype = "string";
break;
case "INTEGER":
deftype = "int";
break;
case "REAL":
deftype = "double";
break;
case "BOOLEAN":
deftype = "bool";
break;
case "LOGICAL":
deftype = "int";
break;
case "BINARY":
deftype = "byte[]";
break;
}
}
switch (docAttribute.GetAggregation())
{
case DocAggregationEnum.SET:
writer.WriteLine("\t\tprivate " + deftype + "[] " + docAttribute.Name + ";");
break;
case DocAggregationEnum.LIST:
writer.WriteLine("\t\tprivate " + deftype + "[] " + docAttribute.Name + ";");
break;
default:
writer.WriteLine("\t\tprivate " + deftype + " " + docAttribute.Name + ";");
break;
}
}
writer.WriteLine("\t}");
}
writer.WriteLine("}");
}
else
{
#if false // TBD
writer.WriteLine("package buildingsmart.ifc.properties");
writer.WriteLine("{");
writer.WriteLine();
Dictionary<string, string[]> mapEnums = new Dictionary<string, string[]>();
foreach (DocSection docSection in this.m_project.Sections)
{
foreach (DocSchema docSchema in docSection.Schemas)
{
foreach (DocPropertySet docPset in docSchema.PropertySets)
{
writer.WriteLine(" /// <summary>");
writer.WriteLine(" /// " + docPset.Documentation.Replace('\r', ' ').Replace('\n', ' '));
writer.WriteLine(" /// </summary>");
writer.WriteLine(" public class " + docPset.Name + " : Pset");
writer.WriteLine(" {");
foreach (DocProperty docProperty in docPset.Properties)
{
writer.WriteLine(" /// <summary>");
writer.WriteLine(" /// " + docProperty.Documentation.Replace('\r', ' ').Replace('\n', ' '));
writer.WriteLine(" /// </summary>");
switch (docProperty.PropertyType)
{
case DocPropertyTemplateTypeEnum.P_SINGLEVALUE:
writer.WriteLine(" public " + docProperty.PrimaryDataType + " " + docProperty.Name + " { get { return this.GetValue<" + docProperty.PrimaryDataType + ">(\"" + docProperty.Name + "\"); } set { this.SetValue<" + docProperty.PrimaryDataType + ">(\"" + docProperty.Name + "\", value); } }");
break;
case DocPropertyTemplateTypeEnum.P_ENUMERATEDVALUE:
// record enum for later
{
string[] parts = docProperty.SecondaryDataType.Split(':');
if (parts.Length == 2)
{
string typename = parts[0];
if (!mapEnums.ContainsKey(typename))
{
string[] enums = parts[1].Split(',');
mapEnums.Add(typename, enums);
writer.WriteLine(" public " + typename + " " + docProperty.Name + " { get { return this.GetValue<" + typename + ">(\"" + docProperty.Name + "\"); } set { this.SetValue<" + typename + ">(\"" + docProperty.Name + "\", value); } }");
}
}
}
break;
case DocPropertyTemplateTypeEnum.P_BOUNDEDVALUE:
writer.WriteLine(" public PBound<" + docProperty.PrimaryDataType + "> " + docProperty.Name + " { get { return this.GetBound<" + docProperty.PrimaryDataType + ">(\"" + docProperty.Name + "\"); } }");
break;
case DocPropertyTemplateTypeEnum.P_LISTVALUE:
break;
case DocPropertyTemplateTypeEnum.P_TABLEVALUE:
writer.WriteLine(" public PTable<" + docProperty.PrimaryDataType + ", " + docProperty.SecondaryDataType + "> " + docProperty.Name + " { get { return this.GetTable<" + docProperty.PrimaryDataType + ", " + docProperty.SecondaryDataType + ">(\"" + docProperty.Name + "\"); } }");
break;
case DocPropertyTemplateTypeEnum.P_REFERENCEVALUE:
if (docProperty.PrimaryDataType.Equals("IfcTimeSeries"))
{
string datatype = docProperty.SecondaryDataType;
if (String.IsNullOrEmpty(datatype))
{
datatype = "IfcReal";
}
writer.WriteLine(" public PTimeSeries<" + datatype + "> " + docProperty.Name + " { get { return this.GetTimeSeries<" + datatype + ">(\"" + docProperty.Name + "\"); } }");
}
// ... TBD
break;
case DocPropertyTemplateTypeEnum.COMPLEX:
//... TBD
break;
}
}
writer.WriteLine(" }");
writer.WriteLine();
}
}
}
// enums
foreach (string strEnum in mapEnums.Keys)
{
string[] enums = mapEnums[strEnum];
writer.WriteLine(" /// <summary>");
writer.WriteLine(" /// </summary>");
writer.WriteLine(" public enum " + strEnum);
writer.WriteLine(" {");
int counter = 0;
foreach (string val in enums)
{
int num = 0;
string id = val.ToUpper().Trim('.').Replace('-', '_');
switch (id)
{
case "OTHER":
num = -1;
break;
case "NOTKNOWN":
num = -2;
break;
case "UNSET":
num = 0;
break;
default:
counter++;
num = counter;
break;
}
if (id[0] >= '0' && id[0] <= '9')
{
id = "_" + id; // avoid numbers
}
writer.WriteLine(" /// <summary></summary>");
writer.WriteLine(" " + id + " = " + num + ",");
}
writer.WriteLine(" }");
writer.WriteLine();
}
writer.WriteLine("}");
#endif
}
}
}
#region IDisposable Members
public void Dispose()
{
}
#endregion
private DocDefinition GetDefinition(string def)
{
foreach (DocSection docSection in this.m_project.Sections)
{
foreach (DocSchema docSchema in docSection.Schemas)
{
foreach (DocType docType in docSchema.Types)
{
if (docType.Name.Equals(def))
{
return docType;
}
}
foreach (DocEntity docType in docSchema.Entities)
{
if (docType.Name.Equals(def))
{
return docType;
}
}
}
}
return null;
}
}
}