1
|
1 |
|
from lxml import etree |
2
|
|
|
|
3
|
1 |
|
from ..namespaces import NAMESPACES |
4
|
|
|
|
5
|
|
|
|
6
|
1 |
|
class DescriptionParser(): |
7
|
1 |
|
def __init__(self, ref_values): |
8
|
1 |
|
self.ref_values = ref_values |
9
|
|
|
|
10
|
1 |
|
def replace_sub_tag(self, tag): |
11
|
1 |
|
return self.ref_values.get(tag.get("idref")) |
12
|
|
|
|
13
|
1 |
|
@staticmethod |
14
|
1 |
|
def get_html_attributes_as_string(attributes): |
15
|
1 |
|
out = "" |
16
|
1 |
|
for key, value in attributes.items(): |
17
|
1 |
|
out += f" {key}=\"{value}\"" |
18
|
1 |
|
return out |
19
|
|
|
|
20
|
1 |
|
def get_html_tag_as_string(self, tag): |
21
|
1 |
|
tag_name = etree.QName(tag).localname |
22
|
1 |
|
tag_text = tag.text |
23
|
1 |
|
tag_attributes = self.get_html_attributes_as_string(tag.attrib) |
24
|
1 |
|
for child in tag: |
25
|
1 |
|
if tag_text is None: |
26
|
1 |
|
tag_text = "" |
27
|
1 |
|
if child.prefix == "html": |
28
|
1 |
|
tag_text += self.get_html_tag_as_string(child) |
29
|
1 |
|
if etree.QName(child).localname == "sub": |
30
|
1 |
|
tag_text += self.replace_sub_tag(child) |
31
|
1 |
|
tag_text += child.tail if child.tail is not None else "" |
32
|
1 |
|
if tag_text is not None: |
33
|
1 |
|
return f"<{tag_name}{tag_attributes}>{tag_text}</{tag_name}>" |
34
|
1 |
|
return f"<{tag_name}{tag_attributes}>" |
35
|
|
|
|
36
|
1 |
|
def get_full_description(self, rule): |
37
|
1 |
|
description = rule.find(".//xccdf:description", NAMESPACES) |
38
|
1 |
|
if description is None: |
39
|
1 |
|
return None |
40
|
1 |
|
str_description = description.text |
41
|
1 |
|
for child in description: |
42
|
1 |
|
if str_description is None: |
43
|
1 |
|
str_description = "" |
44
|
1 |
|
if child.prefix == "html": |
45
|
1 |
|
str_description += self.get_html_tag_as_string(child) |
46
|
1 |
|
if etree.QName(child).localname == "sub": |
47
|
1 |
|
str_description += self.replace_sub_tag(child) |
48
|
1 |
|
str_description += child.tail if child.tail is not None else "" |
49
|
|
|
return str_description |
50
|
|
|
|