/
parser.py
273 lines (203 loc) · 8.61 KB
/
parser.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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Hive Colony Framework
# Copyright (c) 2008-2020 Hive Solutions Lda.
#
# This file is part of Hive Colony Framework.
#
# Hive Colony Framework is free software: you can redistribute it and/or modify
# it under the terms of the Apache License as published by the Apache
# Foundation, either version 2.0 of the License, or (at your option) any
# later version.
#
# Hive Colony Framework is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# Apache License for more details.
#
# You should have received a copy of the Apache License along with
# Hive Colony Framework. If not, see <http://www.apache.org/licenses/>.
__author__ = "João Magalhães <joamag@hive.pt>"
""" The author(s) of the module """
__version__ = "1.0.0"
""" The version of the module """
__revision__ = "$LastChangedRevision$"
""" The revision number of the module """
__date__ = "$LastChangedDate$"
""" The last change date of the module """
__copyright__ = "Copyright (c) 2008-2020 Hive Solutions Lda."
""" The copyright for the module """
__license__ = "Apache License, Version 2.0"
""" The license for the module """
import xml.dom.minidom
import colony
from . import ast
DEFAULT_CHARSET = "utf-8"
""" The default charset """
class Parser(object):
"""
The abstract parser class
"""
def __init__(self):
"""
Constructor of the class
"""
pass
def parse(self):
"""
Parses the defined file
"""
pass
def get_value(self):
"""
Retrieves the result of the parse
:rtype: Object
:return: The result of the parse
"""
pass
def parse_element_attributes(self, node, element):
attributes = node.attributes
for index in colony.legacy.xrange(attributes.length):
attribute_node = attributes.item(index)
attribute_node_name = attribute_node.name
attribute_node_value = attribute_node.value
setattr(element, attribute_node_name, attribute_node_value)
class PrintingLanguageParser(Parser):
"""
The printing language parser class.
"""
file = None
""" The file path """
string = None
""" The string contents """
printing_document = None
""" The printing document """
def __init__(self, file = None, string = "none"):
Parser.__init__(self)
self.file = file
self.string = string
def parse(self):
self.load_printing_language_file(self.file)
def parse_string(self):
self.load_printing_language_string(self.string)
def get_value(self):
return self.printing_document
def get_build_automation(self):
return self.printing_document
def load_printing_language_file(self, file):
# creates the XML document DOM object
xml_document = xml.dom.minidom.parse(file)
self.load_printing_language(xml_document)
def load_printing_language_string(self, string):
# creates the XML document DOM object, the provided
# string is encoded to avoid possible parsing problems
string = type(string) == colony.legacy.UNICODE and string.encode(DEFAULT_CHARSET) or string
xml_document = xml.dom.minidom.parseString(string)
self.load_printing_language(xml_document)
def load_printing_language(self, xml_document):
child_nodes = xml_document.childNodes
for child_node in child_nodes:
if valid_node(child_node):
self.printing_document = self.parse_printing_document(child_node)
def parse_printing_document(self, printing_document):
printing_document_structure = ast.PrintingDocument()
child_nodes = printing_document.childNodes
# parses the element attributes
self.parse_element_attributes(printing_document, printing_document_structure)
for child_node in child_nodes:
if valid_node(child_node):
self.parse_printing_document_element(child_node, printing_document_structure)
return printing_document_structure
def parse_printing_document_element(self, printing_document_element, printing_document):
node_name = printing_document_element.nodeName
printing_document_child_nodes = printing_document.child_nodes
if node_name == "block":
printing_document_child_nodes.append(self.parse_block(printing_document_element))
elif node_name == "paragraph":
printing_document_child_nodes.append(self.parse_paragraph(printing_document_element))
elif node_name == "line":
printing_document_child_nodes.append(self.parse_line(printing_document_element))
elif node_name == "text":
printing_document_child_nodes.append(self.parse_text(printing_document_element))
elif node_name == "image":
printing_document_child_nodes.append(self.parse_image(printing_document_element))
def parse_block(self, block):
block_structure = ast.Block()
child_nodes = block.childNodes
# parses the element attributes
self.parse_element_attributes(block, block_structure)
for child_node in child_nodes:
if valid_node(child_node):
self.parse_block_element(child_node, block_structure)
return block_structure
def parse_block_element(self, block_element, block):
node_name = block_element.nodeName
block_child_nodes = block.child_nodes
if node_name == "text":
block_child_nodes.append(self.parse_text(block_element))
elif node_name == "image":
block_child_nodes.append(self.parse_image(block_element))
def parse_paragraph(self, paragraph):
paragraph_structure = ast.Paragraph()
child_nodes = paragraph.childNodes
# parses the element attributes
self.parse_element_attributes(paragraph, paragraph_structure)
for child_node in child_nodes:
if valid_node(child_node):
self.parse_paragraph_element(child_node, paragraph_structure)
return paragraph_structure
def parse_paragraph_element(self, paragraph_element, paragraph):
node_name = paragraph_element.nodeName
paragraph_child_nodes = paragraph.child_nodes
if node_name == "line":
paragraph_child_nodes.append(self.parse_line(paragraph_element))
elif node_name == "text":
paragraph_child_nodes.append(self.parse_text(paragraph_element))
elif node_name == "image":
paragraph_child_nodes.append(self.parse_image(paragraph_element))
def parse_line(self, line):
line_structure = ast.Line()
child_nodes = line.childNodes
# parses the element attributes
self.parse_element_attributes(line, line_structure)
for child_node in child_nodes:
if valid_node(child_node):
self.parse_line_elements(child_node, line_structure)
return line_structure
def parse_line_elements(self, line_element, line):
node_name = line_element.nodeName
line_child_nodes = line.child_nodes
if node_name == "text":
line_child_nodes.append(self.parse_text(line_element))
elif node_name == "image":
line_child_nodes.append(self.parse_image(line_element))
def parse_text(self, text):
text_structure = ast.Text()
# parses the element attributes
self.parse_element_attributes(text, text_structure)
if text.firstChild:
text_structure.text = text.firstChild.data.strip()
else:
text_structure.text = str()
return text_structure
def parse_image(self, image):
image_structure = ast.Image()
# parses the element attributes
self.parse_element_attributes(image, image_structure)
return image_structure
def valid_node(node):
"""
Gets if a node is valid or not for parsing.
:type node: Node
:param node: The XML node to be validated.
:rtype: bool
:return: The valid or not valid value.
"""
# in case the node is of type element
if node.nodeType == xml.dom.minidom.Node.ELEMENT_NODE:
# returns true (valid)
return True
# otherwise
else:
# returns false (invalid)
return False