/
Generator_CodeJson.ump
386 lines (334 loc) · 12.8 KB
/
Generator_CodeJson.ump
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
380
381
382
383
384
385
386
/*
Copyright: All contributers to the Umple Project
This file is made available subject to the open source license found at:
http://umple.org/license
*/
namespace cruise.umple.compiler;
class JsonGenerator
{
public void generate()
{
String jsonForClasses = StringFormatter.format("{0}umpleClasses{0}:[", "\"");
String jsonForInterfaces = StringFormatter.format("{0}umpleInterfaces{0}:[", "\"");
String jsonForAssociations = StringFormatter.format("{0}umpleAssociations{0}:[", "\"");
boolean isFirst = true;
for (UmpleClass aClass : model.getUmpleClasses())
{
String x = aClass.getCoordinates().getX() + "";
String y = aClass.getCoordinates().getY() + "";
String width = aClass.getCoordinates().getWidth() + "";
String height = aClass.getCoordinates().getHeight() + "";
String name = aClass.getName();
String displayColor = aClass.getDisplayColor();
boolean isAbstract = aClass.getIsAbstract();
// Strip off leading and trailing quotes if they exist
if(displayColor.startsWith("\""))
displayColor=displayColor.substring(1,displayColor.length());
if(displayColor.endsWith("\""))
displayColor=displayColor.substring(0,displayColor.length()-1);
String jsonForAttributes = "";
String jsonForMethods = "";
for (Attribute attribute : aClass.getAttributes())
{
if (jsonForAttributes.length() > 0)
{
jsonForAttributes += ", ";
}
String attributeType = attribute.getFullType();
String modifier = "";
if (attribute.isConstant())
{
modifier = "const";
}
String traceColor = "black";
for( TraceDirective tc : aClass.getAllTraceDirectives() )
for( AttributeTraceItem ati : tc.getAttributeTraceItems() )
for( UmpleVariable aVar : ati.getUmpleVariables() )
if( aVar.getName().equals(attribute.getName()) )
traceColor = "red";
jsonForAttributes += StringFormatter.format(
"{ {0}type{0} : {0}{1}{0}, {0}name{0} : {0}{2}{0}, " +
"{0}modifier{0} : {0}{3}{0}, {0}traceColor{0} : {0}{4}{0} }",
"\"",
attributeType,
attribute.getName(),
modifier,
traceColor);
}
for(Method method : aClass.getMethods())
{
if (method.getSource() == Method.Source.fAutoAPI)
continue;
if(jsonForMethods.length() > 0)
{
jsonForMethods += ", ";
}
String methodModifier = method.getModifier();
String methodReturnType = method.getType();
String visibility="";
if(methodModifier.matches(".*public.*"))
visibility = "public";
else if(methodModifier.matches(".*private.*"))
visibility = "private";
else if(methodModifier.matches(".*protected.*"))
visibility = "protected";
String methodParams = "";
for(MethodParameter param : method.getMethodParameters())
{
if(methodParams.length() > 0)
{
methodParams += ",";
}
methodParams += param.getFullType().trim();
}
jsonForMethods += StringFormatter.format(
"{ {0}type{0} : {0}{1}{0}, {0}name{0} : {0}{2}{0}, " +
"{0}parameters{0} : {0}{3}{0}, {0}visibility{0} : {0}{4}{0}, {0}isAbstract{0} : {0}{5}{0}}",
"\"",
methodReturnType,
method.getName(),
methodParams,
visibility,
method.getIsAbstract());
}
if (!isFirst)
{
jsonForClasses += ", ";
}
String extendsJson = "";
if (aClass.getExtendsClass() != null)
{
String extendsClassName = aClass.getExtendsClass().getName();
extendsJson = StringFormatter.format(", {0}extendsClass{0}: {0}{1}{0}","\"",extendsClassName);
}
//Add implemented interfaces in jsonForClasses: if a class implements one or multiple interfaces, the name of thoese interfaces will be included in Json
String implementedInterfaces = "";
if (aClass.hasParentInterface() == true)
{
int index = 0;
implementedInterfaces = StringFormatter.format(", {0}implementedInterfaces{0}: [ ","\"");
for (UmpleInterface uInterface : aClass.getParentInterface())
{
if (index > 0)
{
implementedInterfaces += ", ";
}
implementedInterfaces += StringFormatter.format("{ {0}interfacesName{0}: {0}{1}{0} }","\"",uInterface.getName());
index ++;
}
implementedInterfaces += " ]";
}
boolean isInterface = false;
jsonForClasses += StringFormatter.format(
"{{0}position{0}: {{0}x{0}: {1}, {0}y{0}: {2}, {0}width{0}: {3}, " +
"{0}height{0}: {4}}, {0}attributes{0}: [{7}],{0}methods{0}: [{8}], " +
"{0}id{0}: {0}{5}{0}, {0}name{0}: {0}{6}{0}, {0}isAbstract{0}: {0}{13}{0}, {0}displayColor{0}: {0}{10}{0} {9}, " +
"{0}isInterface{0}: {0}{11}{0}{12}}",
"\"",
x,
y,
width,
height,
name,
name,
jsonForAttributes,
jsonForMethods,
extendsJson,
displayColor,
isInterface,
implementedInterfaces,
isAbstract);
isFirst = false;
}
boolean isFirstInterface = true;
for (UmpleInterface aInterface : model.getUmpleInterfaces())
{
String x = aInterface.getCoordinates().getX() + "";
String y = aInterface.getCoordinates().getY() + "";
String width = aInterface.getCoordinates().getWidth() + "";
String height = aInterface.getCoordinates().getHeight() + "";
String name = aInterface.getName();
//String displayColor = aInterface.getDisplayColor();
String displayColor = "";
String jsonForAttributes = "";
String jsonForMethods = "";
String extendsJson = "";
boolean isInterface = true;
String implementedInterfaces = "";
boolean isAbstract = false;
//Fix for issue #911 - UmpleOnline in Editable diagram mode was not displaying methods for interfaces
for(Method method : aInterface.getMethods())
{
if (method.getSource() == Method.Source.fAutoAPI)
continue;
if(jsonForMethods.length() > 0)
{
jsonForMethods += ", ";
}
String methodModifier = method.getModifier();
String methodReturnType = method.getType();
String visibility="";
if(methodModifier.matches(".*public.*"))
visibility = "public";
else if(methodModifier.matches(".*private.*"))
visibility = "private";
else if(methodModifier.matches(".*protected.*"))
visibility = "protected";
String methodParams = "";
for(MethodParameter param : method.getMethodParameters())
{
if(methodParams.length() > 0)
{
methodParams += ",";
}
methodParams += param.getFullType().trim();
}
jsonForMethods += StringFormatter.format(
"{ {0}type{0} : {0}{1}{0}, {0}name{0} : {0}{2}{0}, " +
"{0}parameters{0} : {0}{3}{0}, {0}visibility{0} : {0}{4}{0}, {0}isAbstract{0} : {0}{5}{0}}",
"\"",
methodReturnType,
method.getName(),
methodParams,
visibility,
method.getIsAbstract());
}
//End of fix
if (!isFirst)
{
jsonForClasses += ", ";
}
jsonForClasses += StringFormatter.format(
"{{0}position{0}: {{0}x{0}: {1}, {0}y{0}: {2}, {0}width{0}: {3}, " +
"{0}height{0}: {4}}, {0}attributes{0}: [{7}],{0}methods{0}: [{8}], " +
"{0}id{0}: {0}{5}{0}, {0}name{0}: {0}{6}{0}, {0}isAbstract{0}: {0}{13}{0}, {0}displayColor{0}: {0}{10}{0} {9}, " +
"{0}isInterface{0}: {0}{11}{0}{12}}", "\"",
x,
y,
width,
height,
name,
name,
jsonForAttributes,
jsonForMethods,
extendsJson,
displayColor,
isInterface,
implementedInterfaces,
isAbstract);
isFirst = false;
}
int index = 0;
for (Association aAssoc : model.getAssociations())
{
String c1Name = extractClassName(aAssoc.getEnd(0));
String c2Name = extractClassName(aAssoc.getEnd(1));
// Skip rendering associations containing a trait
// Issue 1101
if (c1Name == null || c2Name == null) {
continue;
}
if (index > 0)
{
jsonForAssociations += ", ";
}
int x1 = -1;
int y1 = -1;
int x2 = -1;
int y2 = -1;
String name = aAssoc.getName();
String multOne = aAssoc.getEnd(0).toSimpleString();
String multTwo = aAssoc.getEnd(1).toSimpleString();
String roleOne = aAssoc.getEnd(0).getRoleName();
String roleTwo = aAssoc.getEnd(1).getRoleName();
String modifierOne = aAssoc.getEnd(0).getModifier();
String modifierTwo = aAssoc.getEnd(1).getModifier();
if (aAssoc.getEnd(0).getIsDefaultRoleName()) roleOne = "";
if (aAssoc.getEnd(1).getIsDefaultRoleName()) roleTwo = "";
if (aAssoc.numberOfPositions() > 0)
{
Coordinate position = aAssoc.getPosition(0);
x1 = position.getX();
y1 = position.getY();
}
if (aAssoc.numberOfPositions() > 1)
{
Coordinate position = aAssoc.getPosition(1);
x2 = position.getX();
y2 = position.getY();
}
String traceColor = "black";
for (UmpleClass aClass : model.getUmpleClasses())
for( TraceDirective tc : aClass.getAllTraceDirectives() )
for( AssociationTraceItem asi : tc.getAssociationTraceItems() )
for( AssociationVariable aVar : asi.getAssociationVariables() )
if( aVar.getName().equals(roleOne) || aVar.getName().equals(roleTwo) )
traceColor = "red";
String associationId = "umpleAssociation_" + index;
String baseAssociationJson =
"{\"offsetOnePosition\": {\"x\": {2}, \"y\": {3}, \"width\": 0, \"height\": 0}, " +
"\"offsetTwoPosition\": {\"x\": {4}, \"y\": {5}, \"width\": 0, \"height\": 0}, " +
"\"id\": \"{6}\", \"classOneId\": \"{0}\", \"classTwoId\": \"{1}\", " +
"\"multiplicityOne\": \"{7}\", \"multiplicityTwo\": \"{8}\", " +
"\"name\": \"{9}\", \"roleOne\": \"{10}\", \"roleTwo\": \"{11}\", " +
"\"isLeftNavigable\":\"{12}\", \"isRightNavigable\":\"{13}\", " +
"\"isLeftComposition\":\"{14}\", \"isRightComposition\":\"{15}\", " +
"\"isSymmetricReflexive\":\"{16}\", \"isTraced\":\"{17}\"}";
String isSymmetricReflexive;
if ("symmetricreflexive".equals(modifierOne) && "symmetricreflexive".equals(modifierTwo))
{
roleOne = roleTwo;
roleTwo = "";
isSymmetricReflexive = "true";
}
else
{
isSymmetricReflexive = "false";
}
jsonForAssociations += StringFormatter.format(
baseAssociationJson,
c1Name,
c2Name,
x1,
y1,
x2,
y2,
associationId,
multOne,
multTwo,
name,
roleOne,
roleTwo,
aAssoc.getIsLeftNavigable(),
aAssoc.getIsRightNavigable(),
aAssoc.getIsLeftComposition(),
aAssoc.getIsRightComposition(),
isSymmetricReflexive,
traceColor);
index += 1;
}
jsonForClasses += "]";
jsonForAssociations += "]";
jsonForInterfaces += "]";
//String json = "{" + jsonForClasses + ", " + jsonForInterfaces + ", " + jsonForAssociations + " }";
String json = "{" + jsonForClasses + ", " + jsonForAssociations + " }";
model.setCode(json);
}
//Method to deal with association ends being different entities
//(eg. class or interface)
private String extractClassName(AssociationEnd aAssocEnd)
{
String assocEndName = aAssocEnd.getClassName();
UmpleClass umpClass = model.getUmpleClass(assocEndName);
if(umpClass != null)
{
return umpClass.getName();
}
UmpleInterface umpInterface = model.getUmpleInterface(assocEndName);
if (umpInterface != null) {
return umpInterface.getName();
}
// The associationEnd is a Trait and we don't want to draw a line
return null;
}
}