-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnative_sparx_importer.js
843 lines (753 loc) · 35.8 KB
/
native_sparx_importer.js
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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
var xmlDoc = null;
var extracted = {};
var objectIdsToBeExported = [];
//#region Sparx Importer
function convert_guid(guid) {
// strip the curly braces, convert to uppercase, then add the 'ea' prefix
guid = guid.replace(/[{}]/g, '').toUpperCase();
return "EA_" + guid;
}
function importObjects(xmlDoc, tableName) {
let tables = xmlDoc.getElementsByTagName('Table');
let totalObjects = {};
for (let i = 0; i < tables.length; i++) {
if (tables[i].getAttribute('name') === tableName) {
// analyse all rows in the table
let rows = tables[i].getElementsByTagName('Row');
let objects = [];
Array.from(rows).forEach(row => {
// analyse all columns in the row
let columns = row.getElementsByTagName('Column');
let dict = {};
Array.from(columns).forEach(column => {
let name = column.getAttribute('name');
let value = column.getAttribute('value');
dict[name] = value;
});
// analyse the Extension row
let extension = row.getElementsByTagName('Extension');
if (extension.length > 0) {
// get all attributes in the Extension tag, if present. For example get Start_Object_ID and End_Object_ID from <Extension Start_Object_ID="{C1E7FC41-C7D7-411a-A4BB-058C1E85A3EC}" End_Object_ID="{D6C559A3-F112-4f97-9404-AE5082639A37}"/>
let attributes = extension[0].attributes;
let attributeValues = {};
for(let i = 0; i < attributes.length; i++) {
extensionName = attributes[i].name;
extensionValue = attributes[i].value;
if (extensionName == 'Object_ID') {
// convert Object_ID to Element_ID to avoid confusion with the Object_ID of the current object
extensionName = 'Element_ID';
} else if (extensionName == 'Diagram_ID') {
// convert Object_ID to Object_in_Diagram_ID to avoid confusion with the Diagram_ID of the current object
extensionName = 'Object_in_Diagram_ID';
}
if (extensionValue.startsWith('{')) {
extensionValue = convert_guid(extensionValue);
}
dict[extensionName] = extensionValue;
}
}
// convert GUIDs to the format used in Innovator
if (dict['Start_Object_ID'] != null) {
if (dict['Start_Object_ID'].startsWith('{')) {
dict['Start_Object_ID'] = convert_guid(dict['Start_Object_ID']);
}
}
if (dict['End_Object_ID'] != null) {
if (dict['End_Object_ID'].startsWith('{')) {
dict['End_Object_ID'] = convert_guid(dict['End_Object_ID']);
}
}
if (dict['Diagram_ID'] != null) {
diagId = dict['Diagram_ID'];
if (diagId.startsWith('{')) {
diagId = convert_guid(diagId);
}
dict['Diagram_ID'] = diagId;
}
if (dict['DiagramID'] != null) {
diagId = dict['DiagramID'];
if (diagId.startsWith('{')) {
diagId = convert_guid(diagId);
}
dict['Diagram_ID'] = diagId;
}
// store the object in the dictionary
if (dict['ea_guid'] != null) {
dict['ea_guid'] = convert_guid(dict['ea_guid']);
objects[dict['ea_guid']] = dict;
} else if (dict['Object_ID'] != null) {
dict['Object_ID'] = convert_guid(dict['Object_ID']);
if (tableName == 't_diagramobjects') {
key = dict['Diagram_ID'] + "-" + dict['Object_ID'];
objects[key] = dict;
} else {
objects[dict['Object_ID']] = dict;
}
} else if (dict['Client'] != null) {
dict['Client'] = convert_guid(dict['Client']);
objects[dict['Client']] = dict;
} else if (dict['ConnectorID'] != null) {
if (dict['ConnectorID'].startsWith('{')) {
dict['Connector_ID'] = convert_guid(dict['ConnectorID']);
} else {
dict['Connector_ID'] = dict['ConnectorID'];
}
objects[dict['Connector_ID']] = dict;
} else {
console.log('No GUID or Object_ID found for object');
}
console.log('Imported ' + tableName + ' object with GUID ' + dict['ea_guid']);
});
totalObjects = Object.assign(totalObjects, objects);
}
}
let count = Object.keys(totalObjects).length;
console.log(`Number of rows in ${tableName}: ${count}`);
return totalObjects;
}
function importNativeFile(xmlDoc) {
objectIdsToBeExported = [];
extracted.packages = importObjects(xmlDoc, 't_package');
extracted.elements = importObjects(xmlDoc, 't_object');
extracted.connectors = importObjects(xmlDoc, 't_connector');
extracted.attributes = importObjects(xmlDoc, 't_attribute');
extracted.operations = importObjects(xmlDoc, 't_operation');
extracted.diagrams = importObjects(xmlDoc, 't_diagram');
extracted.diagramobjects = importObjects(xmlDoc, 't_diagramobjects');
extracted.diagramlinks = importObjects(xmlDoc, 't_diagramlinks');
extracted.taggedvalues = importObjects(xmlDoc, 't_taggedvalue');
extracted.xrefs = importObjects(xmlDoc, 't_xref');
}
//#endregion
//#region Diagrams table
function fillDiagramsTable() {
let table = document.getElementById('diagramsTable');
let tbody = table.getElementsByTagName('tbody')[0];
let decoder = new TextDecoder('utf-8');
for (let key in extracted.diagrams) {
// use the existing row with id="diagramrow-template" as a template
let diagram = extracted.diagrams[key];
let row = document.getElementById('diagramrow-template').cloneNode(true);
row.removeAttribute('id');
row.style.display = 'table-row';
row.style.hidden = false;
// fill the row with the diagram data:
// the first column contains an input checkbox, it should be enabled as it is in the template
// the second column contains the diagram name
let cells = row.getElementsByTagName('td');
cells[1].textContent = diagram['Name'];
cells[2].textContent = diagram['ea_guid'];
tbody.appendChild(row);
}
}
//#endregion
//#region Innovator Exporter
function formatXml(xml) {
var reg = /(>)(<)(\/*)/g;
var wsexp = / *(.*) +\n/g;
var contexp = /(<.+>)(.+\n)/g;
xml = xml.replace(reg, '$1\n$2$3').replace(wsexp, '$1\n').replace(contexp, '$1$2');
var pad = 0;
var formatted = '';
var lines = xml.split('\n');
var indent = 0;
var lastType = 'other';
// 4 types of tags - single, closing, opening, other (text, doctype, comment) - 4*4 = 16 transitions
var transitions = {
'single->single' : 0,
'single->closing' : -1,
'single->opening' : 0,
'single->other' : 0,
'closing->single' : 0,
'closing->closing' : -1,
'closing->opening' : 0,
'closing->other' : 0,
'opening->single' : 1,
'opening->closing' : 0,
'opening->opening' : 1,
'opening->other' : 1,
'other->single' : 0,
'other->closing' : -1,
'other->opening' : 0,
'other->other' : 0
};
for (var i=0; i < lines.length; i++) {
var ln = lines[i];
var single = Boolean(ln.match(/<.+\/>/)); // is this line a single tag? ex. <br />
var closing = Boolean(ln.match(/<\/.+>/)); // is this a closing tag? ex. </a>
var opening = Boolean(ln.match(/<[^!].*>/)); // is this even a tag (that's not <!something>)
var type = single ? 'single' : closing ? 'closing' : opening ? 'opening' : 'other';
var fromTo = lastType + '->' + type;
lastType = type;
var padding = '';
indent += transitions[fromTo];
for (var j = 0; j < indent; j++) {
padding += ' ';
}
formatted += padding + ln + '\n';
}
return formatted;
};
// fills the objectIdsToBeExported object with the GUIDs of the diagrams, elements and connectors to be exported
function identifyExportedObjects() {
// identify which diagrams, elements and connector to export
let diagrams = document.getElementById('diagramsTable').getElementsByTagName('input');
let exportedDiagrams = []; // make sure to declare this array
for (let i = 0; i < diagrams.length; i++) {
if (diagrams[i].checked) {
if (diagrams[i].parentElement.nextElementSibling.textContent == 'Diagram Name') continue;
if (diagrams[i].parentElement.nextElementSibling.textContent == 'Select/unselect all diagrams') continue;
let ea_guid = diagrams[i].parentElement.nextElementSibling.nextElementSibling.textContent;
if (ea_guid == 'Enterprise Architect Global Unique Identifier') continue;
exportedDiagrams.push(ea_guid);
}
}
// use the exported diagrams to identify the elements, diagrams and connectors to export
objectIdsToBeExported = {
'elements': [],
'connectors': [],
'diagrams': []
};
for (let diagramkey in extracted.diagrams) {
// include all selected diagram IDs
if (exportedDiagrams.includes(extracted.diagrams[diagramkey]['ea_guid'])) {
objectIdsToBeExported.diagrams.push(extracted.diagrams[diagramkey]['ea_guid']);
}
// include all selected elements that appear on the diagram. To find out which
// elements appear on the diagram, we need to look at the diagramobjects table
for (let dokey in extracted.diagramobjects) {
let diagramobject = extracted.diagramobjects[dokey];
let diagramobject_diagram_id = diagramobject['Object_in_Diagram_ID'];
if (objectIdsToBeExported.diagrams.includes(diagramobject_diagram_id)) {
let element = extracted.elements[diagramobject['Element_ID']]; // this is coming from the Extension part of the diagramobjects table, there it's called Object_ID but it's renamed to Element_ID to avoid overriding the Object_ID of the t_diagramobjects table
if (!objectIdsToBeExported.elements.includes(diagramobject['Element_ID'])) {
objectIdsToBeExported.elements.push(diagramobject['Element_ID']);
}
}
}
// include all selected connectors that appear on the diagram. To find out which
// connectors appear on the diagram, we need to look at the diagramlinks table
for (let dlkey in extracted.diagramlinks) {
let diagramlink = extracted.diagramlinks[dlkey];
let diagramlink_diagram_id = diagramlink['Diagram_ID'];
if (objectIdsToBeExported.diagrams.includes(diagramlink_diagram_id)) {
if (!objectIdsToBeExported.connectors.includes(diagramlink['Connector_ID'])) {
objectIdsToBeExported.connectors.push(diagramlink['Connector_ID']);
}
}
}
}
}
function exportToInnovator(filter) {
// identify which diagrams, elements and connector to export
identifyExportedObjects();
// prepare basic export XMI structure
let doc = new DOMParser().parseFromString('<model></model>', 'application/xml');
let model = doc.firstChild;
// <?xml version="1.0" encoding="utf-8" standalone="no"?>
// <model xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.opengroup.org/xsd/archimate/3.0/ http://www.opengroup.org/xsd/archimate/3.1/archimate3_Diagram.xsd http://purl.org/dc/elements/1.1/ http://www.opengroup.org/xsd/archimate/3.1/dc.xsd" version="16.0.1.21019" identifier="id-085c6229-1eec-4881-889f-99b109ade5b8" xmlns="http://www.opengroup.org/xsd/archimate/3.0/">
model.setAttribute('xmlns:dc', 'http://purl.org/dc/elements/1.1/');
model.setAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
model.setAttribute('xsi:schemaLocation', 'http://www.opengroup.org/xsd/archimate/3.0/ http://www.opengroup.org/xsd/archimate/3.1/archimate3_Diagram.xsd http://purl.org/dc/elements/1.1/ http://www.opengroup.org/xsd/archimate/3.1/dc.xsd');
model.setAttribute('version', '16.0.1.21019');
model.setAttribute('identifier', 'id-085c6229-1eec-4881-889f-99b109ade5b8');
model.setAttribute('xmlns', 'http://www.opengroup.org/xsd/archimate/3.0/');
// <name xml:lang="de">Innovator</name>
let name = doc.createElement('name');
name.setAttribute('xml:lang', 'de');
name.textContent = 'Innovator';
model.appendChild(name);
// Export elements
exportElements(doc, model, filter);
// Export connectors
exportConnectors(doc, model, filter);
// Export properties
exportProperties(doc, model, filter);
// Export diagrams
exportDiagrams(doc, model, filter);
// export all elements into a single diagram without connectors
exportAllElements(doc, model, filter);
// Serialize XML DOM to string
let serializer = new XMLSerializer();
let xmlStr = serializer.serializeToString(doc);
let xmlPretty = formatXml(xmlStr, " ");
return xmlPretty;
}
// TODO: enable again if needed:
const elementTypeBlackList = ['DataType', 'Package'];
function exportElements(doc, model, filter) {
// <elements>
let elements = doc.createElement('elements');
model.appendChild(elements);
// add all elements
for (let key in extracted.elements) {
if (filter && !objectIdsToBeExported.elements.includes(key))
continue;
let element = extracted.elements[key];
if (elementTypeBlackList.includes(element['Object_Type']))
continue;
if (element['Object_Type'] == 'Port')
continue;
// <element identifier="EAID_94620B68_C54A_435d_899D_652653D6D95F" xsi:type="Actor">
let el = doc.createElement('element');
elements.appendChild(el);
el.setAttribute('identifier', element['ea_guid']);
el.setAttribute('xsi:type', convertElementType(element['Object_Type']));
// <name xml:lang="de">Actor1</name>
let elName = doc.createElement('name');
elName.setAttribute('xml:lang', 'de');
elName.textContent = element['Name'];
el.appendChild(elName);
// <properties>
// <property propertyDefinitionRef="id-Stereotype">
// <value xml:lang="de">ApplicationBusinessApplication</value>
// </property>
// </properties>
let mmb_stereotype = convertElementStereotype(element['Object_Type'], element['Stereotype']);
let properties = doc.createElement('properties');
el.appendChild(properties);
let property = doc.createElement('property');
properties.appendChild(property);
property.setAttribute('propertyDefinitionRef', 'id-Stereotype');
let value = doc.createElement('value');
value.setAttribute('xml:lang', 'de');
value.textContent = mmb_stereotype;
property.appendChild(value);
}
}
function convertElementType(sparxtype) {
switch (sparxtype) {
case 'Component':
innovatortype = 'ApplicationCollaboration';
break;
case 'Class':
innovatortype = 'BusinessObject';
break;
case 'Actor':
innovatortype = 'BusinessRole';
break;
case 'Activity':
innovatortype = 'BusinessProcess';
break;
default:
innovatortype = sparxtype;
break;
}
return innovatortype;
}
function convertElementStereotype(sparxtype, sparxstereotype) {
switch (sparxstereotype) {
case 'bag_InfSys':
innovatorstereotype = 'ApplicationBusinessApplication';
break;
case 'BusinessProcess':
innovatorstereotype = 'BusinessProcess';
default:
innovatorstereotype = sparxstereotype;
break;
}
return innovatorstereotype;
}
function exportProperties(doc, model, filter) {
// add all properties
// <propertyDefinitions>
// <propertyDefinition identifier="id-Stereotype" type="string">
// <name>Stereotype</name>
// <name xml:lang="en">Stereotype</name>
// <name xml:lang="de">Stereotyp</name>
// </propertyDefinition>
// </propertyDefinitions>
let propertyDefinitions = doc.createElement('propertyDefinitions');
model.appendChild(propertyDefinitions);
let propertyDefinition = doc.createElement('propertyDefinition');
propertyDefinitions.appendChild(propertyDefinition);
propertyDefinition.setAttribute('identifier', 'id-Stereotype');
propertyDefinition.setAttribute('type', 'string');
let name = doc.createElement('name');
name.textContent = 'Stereotype';
propertyDefinition.appendChild(name);
let name_en = doc.createElement('name');
name_en.setAttribute('xml:lang', 'en');
name_en.textContent = 'Stereotype';
propertyDefinition.appendChild(name_en);
let name_de = doc.createElement('name');
name_de.setAttribute('xml:lang', 'de');
name_de.textContent = 'Stereotyp';
propertyDefinition.appendChild(name_de);
}
function exportConnectors(doc, model, filter) {
if (extracted.connectors == null || extracted.connectors.length == 0) return;
// <relationships>
let relationships = doc.createElement('relationships');
model.appendChild(relationships);
// add all connectors
for (let key in extracted.connectors) {
if (filter && !objectIdsToBeExported.connectors.includes(key)) continue;
let connector = extracted.connectors[key];
let sourceid = connector['Start_Object_ID'];
let targetid = connector['End_Object_ID'];
let mmb_stereotype = convertConnectorStereotype(connector['Connector_Type'], connector['Stereotype'], extracted.elements[sourceid], extracted.elements[targetid]);
// <relationship identifier="EAID_1CF9F3EF_2625_4d19_AC65_BBFE9D37CAAD" xsi:type="Association" source="EAID_94620B68_C54A_435d_899D_652653D6D95F" target="EAID_AB5B300A_4BB6_4def_96B1_BC69E66A68D0">
let rel = doc.createElement('relationship');
relationships.appendChild(rel);
rel.setAttribute('identifier', connector['ea_guid']);
rel.setAttribute('xsi:type', convertConnectorType(connector['Connector_Type']));
// handle the case where the source or target is a port
let sourceelement = extracted.elements[connector['Start_Object_ID']];
if (sourceelement != null && 'Object_Type' in sourceelement && sourceelement['Object_Type'] == 'Port') {
sourceelement = extracted.elements[sourceelement['ParentID']];
sourceid = sourceelement['ea_guid'];
}
let targetelement = extracted.elements[connector['End_Object_ID']];
if (targetelement != null && 'Object_Type' in targetelement && targetelement['Object_Type'] == 'Port') {
targetelement = extracted.elements[targetelement['ParentID']];
targetid = targetelement['ea_guid'];
}
// source and target identifiers
rel.setAttribute('source', sourceid);
rel.setAttribute('target', targetid);
// <name xml:lang="de">ApplicationBusinessApplication</name>
let name = doc.createElement('name');
name.setAttribute('xml:lang', 'de');
connector_name = connector['Name'];
if (connector_name == null || connector_name == '') {
connector_name = mmb_stereotype;
}
name.textContent = connector_name;
rel.appendChild(name);
// <properties>
// <property propertyDefinitionRef="id-Stereotype">
// <value xml:lang="de">ApplicationBusinessApplication</value>
// </property>
// </properties>
let properties = doc.createElement('properties');
rel.appendChild(properties);
let property = doc.createElement('property');
properties.appendChild(property);
property.setAttribute('propertyDefinitionRef', 'id-Stereotype');
let value = doc.createElement('value');
value.setAttribute('xml:lang', 'de');
value.textContent = mmb_stereotype;
property.appendChild(value);
}
}
function convertConnectorType(sparxtype) {
switch (sparxtype) {
case 'Dependency':
innovatortype = 'Association';
break;
case 'Realisation':
case 'Realization':
innovatortype = 'Realization';
break;
case 'InformationFlow':
innovatortype = 'Flow';
break;
case 'Usage':
innovatortype = 'Access';
break;
default:
innovatortype = sparxtype;
break;
}
return innovatortype;
}
function convertConnectorStereotype(sparxtype, sparxstereotype, sourceelement, targetelement) {
if (sourceelement == null || targetelement == null) return;
innovatorstereotype = convertElementStereotype(sparxtype, sparxstereotype); // default to the element stereotype conversion (in all 'default' switch cases)
sourceelementtype = sourceelement['Object_Type'];
targetelementtype = targetelement['Object_Type'];
switch (sparxtype) {
case 'Dependency':
switch (sparxstereotype) {
case 'ReplacedBy':
innovatorstereotype = 'AssociationApplicationDirected_ToApplicationReplacedByApplication';
break;
default:
break;
}
break;
case 'Realisation':
case 'Realization':
switch (sparxstereotype) {
default:
if (sourceelementtype == 'Component' && targetelementtype == 'Activity') {
innovatorstereotype = 'RealizationBusiness_OfBusinessProcess';
}
break;
}
default:
break;
}
return innovatorstereotype;
}
function exportDiagrams(doc, model, filter) {
// <views>
let views = doc.createElement('views');
model.appendChild(views);
// <views>
// <diagrams>
let diagrams = doc.createElement('diagrams');
views.appendChild(diagrams);
// add diagrams and their elements
for (let key in extracted.diagrams) {
let diagram = extracted.diagrams[key];
if (filter && !objectIdsToBeExported.diagrams.includes(diagram['ea_guid'])) continue;
// <diagrams>
// <view identifier="ABC-123" xsi:type="Diagram" viewpoint="ArchiMate Diagram">
let view = doc.createElement('view');
diagrams.appendChild(view);
view.setAttribute('identifier', diagram['ea_guid']);
view.setAttribute('xsi:type', 'Diagram');
view.setAttribute('viewpoint', 'ArchiMate Diagram');
// <view identifier="ABC-123" xsi:type="Diagram" viewpoint="ArchiMate Diagram">
// <name xml:lang="de">Prova1</name>
let viewname = doc.createElement('name');
viewname.setAttribute('xml:lang', 'de');
viewname.textContent = diagram['Name'];
view.appendChild(viewname);
// <properties>
// <property propertyDefinitionRef="id-Stereotype">
// <value xml:lang="de">DiagramEAEnterpriseArchitecture</value>
// </property>
// </properties>
let mmb_stereotype = "DiagramEAEnterpriseArchitecture";
let properties = doc.createElement('properties');
view.appendChild(properties);
let property = doc.createElement('property');
properties.appendChild(property);
property.setAttribute('propertyDefinitionRef', 'id-Stereotype');
let value = doc.createElement('value');
value.setAttribute('xml:lang', 'de');
value.textContent = mmb_stereotype;
property.appendChild(value);
let nodes = {};
for (let key in extracted.diagramobjects) {
let diagramelement = extracted.diagramobjects[key];
if (diagramelement['Diagram_ID'] != diagram['Diagram_ID']) continue;
let element = extracted.elements[diagramelement['Element_ID']];
if (element != null) {
if (element['Object_Type'] == 'Port') continue;
// <view identifier="ABC-123" xsi:type="Diagram" viewpoint="ArchiMate Diagram">
// <node identifier="EAID_94620B68_C54A_435d_899D_652653D6D95F" xsi:type="Container" x="0" y="40" w="220" h="50">
// <label xml:lang="de">Actor1</label>
// </node>
let node = doc.createElement('node');
view.appendChild(node);
node.setAttribute('identifier', "id-" + diagramelement['Instance_ID']);
node.setAttribute('elementRef', diagramelement['Element_ID']);
node.setAttribute('xsi:type', "Element");
node.setAttribute('x', diagramelement['RectLeft']);
node.setAttribute('y', -diagramelement['RectTop']);
node.setAttribute('w', parseInt(diagramelement['RectRight'])-parseInt(diagramelement['RectLeft']));
node.setAttribute('h', -parseInt(diagramelement['RectBottom'])+parseInt(diagramelement['RectTop']));
let label = doc.createElement('label');
label.setAttribute('xml:lang', 'de');
label.textContent = element['Name'];
node.appendChild(label);
nodes[element['ea_guid']] = "id-" + diagramelement['Instance_ID'];
}
}
// diagram links (the connectors that appear in the diagram)
for (let key in extracted.diagramlinks) {
let diagramlink = extracted.diagramlinks[key];
if (diagramlink['Diagram_ID'] != diagram['ea_guid']) continue;
if (diagramlink != null) {
// <view identifier="ABC-123" xsi:type="Diagram" viewpoint="ArchiMate Diagram">
// <connection identifier="id-2fc9c902-8a05-9be6-d8e7-ce6a02a401f7" relationshipRef="id-fb2e3752-de21-f0ca-b8b6-bfcc1c76f131" xsi:type="Relationship" source="id-57c7c9fc-2722-58a0-6327-9810885b4d26" target="id-ebcfaa3c-2443-b965-4c38-082cbc24290f">
// <sourceAttachment x="150" y="45" />
// <targetAttachment x="410" y="45" />
// </connection>
let connector = extracted.connectors[diagramlink['Connector_ID']];
if (connector == null) continue;
let connection = doc.createElement('connection');
view.appendChild(connection);
let sourceid = connector['Start_Object_ID'];
let source = extracted.elements[sourceid];
if (source != null && 'Object_Type' in source && source['Object_Type'] == 'Port') {
sourceelement = extracted.elements[sourceid];
sourceid = sourceelement['ParentID'];
}
let targetid = connector['End_Object_ID'];
let target = extracted.elements[targetid];
if (target != null && 'Object_Type' in target && target['Object_Type'] == 'Port') {
targetelement = extracted.elements[targetid];
targetid = targetelement['ParentID'];
targetelement = extracted.elements[targetid];
}
connection.setAttribute('relationshipRef', diagramlink['Connector_ID']);
connection.setAttribute('identifier', "DL-" + diagramlink['Instance_ID']);
connection.setAttribute('xsi:type', 'Relationship');
connection.setAttribute('source', nodes[sourceid]);
connection.setAttribute('target', nodes[targetid]);
// connector geometry
let geometry = diagramlink['Geometry'].split(';'); // SX=0;SY=0;EX=0;EY=0;EDGE=2;$LLB=;LLT=;LMT=;LMB=;LRT=;LRB=;IRHS=;ILHS=;Path=;
let sx = undefined;
let sy = undefined;
let ex = undefined;
let ey = undefined;
for (let geometryItem of geometry) {
if (geometryItem.startsWith('SX=')) {
sx = geometryItem.split('=')[1];
}
if (geometryItem.startsWith('SY=')) {
sy = geometryItem.split('=')[1];
}
if (geometryItem.startsWith('EX=')) {
ex = geometryItem.split('=')[1];
}
if (geometryItem.startsWith('EY=')) {
ey = geometryItem.split('=')[1];
}
}
if (sx != undefined && sy != undefined) {
sx = Math.max(sx, 0);
sy = Math.max(sy, 0);
let sourceAttachment = doc.createElement('sourceAttachment');
sourceAttachment.setAttribute('x', sx);
sourceAttachment.setAttribute('y', sy);
connection.appendChild(sourceAttachment);
}
if (ex != undefined && ey != undefined) {
ex = Math.max(ex, 0);
ey = Math.max(ey, 0);
let targetAttachment = doc.createElement('targetAttachment');
targetAttachment.setAttribute('x', ex);
targetAttachment.setAttribute('y', ey);
connection.appendChild(targetAttachment);
}
}
}
}
}
function exportAllElements(doc, model) {
const N = 20; // number of horizontal nodes
// <views>
// <diagrams>
let diagrams = doc.getElementsByTagName('diagrams')[0];
// <diagrams>
// <view identifier="ABC-123" xsi:type="Diagram" viewpoint="ArchiMate Diagram">
let view = doc.createElement('view');
diagrams.appendChild(view);
view.setAttribute('identifier', 'AllElements');
view.setAttribute('xsi:type', 'Diagram');
view.setAttribute('viewpoint', 'ArchiMate Diagram');
// <view identifier="ABC-123" xsi:type="Diagram" viewpoint="ArchiMate Diagram">
// <name xml:lang="de">All Elements</name>
let viewname = doc.createElement('name');
viewname.setAttribute('xml:lang', 'de');
viewname.textContent = 'All Elements';
view.appendChild(viewname);
// <properties>
// <property propertyDefinitionRef="id-Stereotype">
// <value xml:lang="de">DiagramEAEnterpriseArchitecture</value>
// </property>
// </properties>
let mmb_stereotype = "DiagramEAEnterpriseArchitecture";
let properties = doc.createElement('properties');
view.appendChild(properties);
let property = doc.createElement('property');
properties.appendChild(property);
property.setAttribute('propertyDefinitionRef', 'id-Stereotype');
let value = doc.createElement('value');
value.setAttribute('xml:lang', 'de');
value.textContent = mmb_stereotype;
property.appendChild(value);
let nodes = [];
instanceid = 0;
for (let key in objectIdsToBeExported.elements) {
let elementKey = objectIdsToBeExported.elements[key];
let element = extracted.elements[elementKey];
if (elementTypeBlackList.includes(element['Object_Type'])) continue;
if (element['Object_Type'] == 'Port') continue;
// <view identifier="ABC-123" xsi:type="Diagram" viewpoint="ArchiMate Diagram">
// <node identifier="EAID_94620B68_C54A_435d_899D_652653D6D95F" xsi:type="Container" x="0" y="40" w="220" h="50">
// <label xml:lang="de">Actor1</label>
// </node>
let node = doc.createElement('node');
view.appendChild(node);
node.setAttribute('identifier', "id-" + ++instanceid);
node.setAttribute('elementRef', element['ea_guid']);
node.setAttribute('xsi:type', "Element");
// position the node in the diagram, each node is positioned in a matrix with N horizontal nodes, with 50px separation between nodes horizontally and vertically
let i = Object.keys(nodes).length;
let x = 50 + (i % N) * 250;
let y = 50 + Math.floor(i / N) * 100;
node.setAttribute('x', x);
node.setAttribute('y', y);
node.setAttribute('w', 220);
node.setAttribute('h', 50);
let label = doc.createElement('label');
label.setAttribute('xml:lang', 'de');
label.textContent = element['Name'];
node.appendChild(label);
nodes[element['ea_guid']] = element['ea_guid'];
}
}
//#endregion
//#region main
function convert_filename(filename) {
if (filename.includes("Sparx.xml")) {
return filename.replace('Sparx.xml', 'Innovator.xml');
} else {
return filename.replace('.xml', '-Innovator.xml');
}
}
function processNativeFile(file) {
var reader = new FileReader();
reader.onload = function(e) {
var binaryString = e.target.result;
var encoding = getEncodingFromXml(binaryString) || 'windows-1252'; // Default to 'windows-1252' if encoding is not found
var readerWithEncoding = new FileReader();
readerWithEncoding.onload = function(e) {
console.log('Reading file...');
var contents = e.target.result;
// Process contents here
var parser = new DOMParser();
var xmlDoc = parser.parseFromString(contents, 'text/xml');
console.log('...file read');
importNativeFile(xmlDoc);
exportToInnovator(false);
fillDiagramsTable();
};
readerWithEncoding.readAsText(file, encoding);
};
reader.readAsBinaryString(file);
}
function convertNativeFile(file) {
var reader = new FileReader();
reader.onload = function(e) {
var binaryString = e.target.result;
var encoding = getEncodingFromXml(binaryString) || 'windows-1252'; // Default to 'windows-1252' if encoding is not found
var readerWithEncoding = new FileReader();
readerWithEncoding.onload = function(e) {
// Process contents here
var contents = e.target.result;
var parser = new DOMParser();
var xmlDoc = parser.parseFromString(contents, 'text/xml');
importNativeFile(xmlDoc);
var innovatorXmi = exportToInnovator(true);
var blob = new Blob([innovatorXmi], {type: 'text/xml'});
// Create a URL for the Blob
var url = URL.createObjectURL(blob);
// Set the href of the download link to the Blob URL and show the link
var downloadLink = document.getElementById('downloadLink');
downloadLink.href = url;
downloadLink.download = convert_filename(file.name);
downloadLink.style.display = 'block';
};
readerWithEncoding.readAsText(file, encoding);
};
reader.readAsBinaryString(file);
}
function getEncodingFromXml(binaryString) {
var xmlDeclaration = binaryString.match(/<\?xml.*?\?>/);
if (xmlDeclaration) {
var encodingMatch = xmlDeclaration[0].match(/encoding=["'](.*?)["']/);
if (encodingMatch) {
return encodingMatch[1];
}
}
return null;
}
//#endregion