-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLexDrawio.py
268 lines (143 loc) · 6.03 KB
/
LexDrawio.py
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
import xml.etree.ElementTree as ET
import html
import re
#Dokumentation erstellt mit der AI Tabine in Visual studio code
class Diagram:
"""
A class to represent a UML Diagram.
Attributes:
blocks (list): A list of blocks in the diagram.
arrows (list): A list of arrows in the diagram.
"""
def __init__(self):
self.blocks = []
self.arrows = []
def PrintArrows(self):
for cell in self.arrows:
cell.PrintData()
def PrintBlocks(self):
for cell in self.blocks:
cell.PrintData()
class Cell:
"""
A class to represent a UML Diagram cell.
Attributes:
Geometry (dict): A dictionary containing the geometry information of the cell.
Attr (dict): A dictionary containing the attributes of the cell.
"""
def __init__(self):
self.Geometry={}
self.Attr={}
def PrintData(self):
print(f"""
id: {self.Attr['id']}
parent: {self.Attr['parent']}
value: {self.Attr['value']}
style: {self.Attr['style']}
source: {self.Attr['source']}
target: {self.Attr['target']}
geometry: {self.Geometry}
------------------------------------------------------
""")
def SortBlocksByLevel(Diagram,attr):
####Bubble sort####
N=len(Diagram.blocks)
for j in range(N,2,-1):
for k in range(j-1):
if( float( Diagram.blocks[k].Geometry[attr] )>float( Diagram.blocks[k+1].Geometry[attr] )):
#swapp
dummy= Diagram.blocks[k+1]
Diagram.blocks[k+1]=Diagram.blocks[k]
Diagram.blocks[k]=dummy
#####
#################
def FormatHtmltoString(string):
"""
This function takes a string that contains HTML escape sequences and converts it to a normal string by removing the HTML tags and replacing the HTML escape sequences with their original characters.
Parameters:
string (str): The input string that contains HTML escape sequences.
Returns:
str: The output string that contains no HTML tags and only the original characters.
"""
# HTML-Escape-Sequenzen entschlüsseln
decoded_string = html.unescape(string)
#replace   and <br>
clean_string = re.sub(r' ', ' ', decoded_string)
clean_string = re.sub(r'<br>', '\n', clean_string)
# HTML-Tags entfernen
clean_string = re.sub(r'<[^>]+>', '', clean_string)
return clean_string
def ReadXmlString(file_path):
with open(file_path, 'r') as file:
xml_string = file.read()
return xml_string
def ExtractAtributes(Entry, ListAttributes ):
ExtractedAtributes={ }
for attr in ListAttributes:
ExtractedAtributes[attr]=Entry.attrib.get(attr)
return ExtractedAtributes
def ExtractCellsFromDiagram( mxGraphModel ):
"""
Extracts the cells from an mxGraphModel Element.
Args:
mxGraphModel (xml.etree.ElementTree.Element): The mxGraphModel Element.
Returns:
List[Cell]: A list of Cell objects."""
CellSet = mxGraphModel.findall('.//mxCell')
MxDiagram=[ ]
ListAttributesGeometry=["x","y", "width", "height" , "as"]
ListAttributesCell=["id","value",'style', 'parent', 'target', 'source']
for cell in CellSet:
Cell_curent=Cell()
Cell_curent.Attr=ExtractAtributes(cell, ListAttributesCell )
#Format style and value
try:
Cell_curent.Attr['value']=FormatHtmltoString( Cell_curent.Attr['value'] )
except:
pass
try:
Cell_curent.Attr['style']=Cell_curent.Attr['style'].split(";")
except:
pass
geometry=cell.find('.//mxGeometry')
for attr in ListAttributesGeometry:
try:
Cell_curent.Geometry[attr]= geometry. attrib.get(attr)
except:
pass
MxDiagram.append(Cell_curent)
return MxDiagram
def ClassifyCells(MxDiagram):
"""
This function takes a list of mxCell objects and classifies them into blocks and arrows based on their style and geometry attributes.
Parameters:
MxDiagram (list): A list of mxCell objects.
Returns:
Diagram: A Diagram object containing the classified blocks and arrows.
"""
Dia=Diagram()
for cell in MxDiagram:
if(cell.Attr['style']!=None):
if(cell.Geometry["x"]!=None or cell.Geometry["y"]!=None ):
Dia.blocks.append(cell)
else:
Dia.arrows.append(cell)
return Dia
def ParseDiagramsFromXmlFile(file_path):
"""
Parse a UML XML file and extract the diagrams contained within it.
Parameters:
file_path (str): The path to the UML XML file.
Returns:
Dict[str, Diagram]: A dictionary where the key is the name of the diagram and the value is the Diagram object.
"""
xml_string=ReadXmlString(file_path)
Diagrams={ }
mxFile = ET.fromstring(xml_string)
MxDiagrams = mxFile.findall('.//diagram')
for diagram in MxDiagrams:
DiagramName= diagram.attrib.get('name')
mxGraphModel = diagram.find('.//mxGraphModel')
MxCells=ExtractCellsFromDiagram( mxGraphModel )
Diagrams[DiagramName]=ClassifyCells(MxCells)
return Diagrams