Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Doors: a yet another example from a user: XHTML tags with their own namespaces #119

Merged
merged 1 commit into from
Apr 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 31 additions & 4 deletions reqif/helpers/lxml.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import re
from copy import deepcopy
from itertools import chain

Expand Down Expand Up @@ -54,15 +55,41 @@ def lxml_stringify_namespaced_children(node, namespace_tag=None) -> str:
nskey = namespace_tag

def _lxml_stringify_reqif_ns_node(node):
assert node is not None
output = ""
node_no_ns_tag = etree.QName(node).localname

# There are ReqIF files where the XHTML-based tags can have their own
# namespace that overrides the document-level namespace. Making the best
# effort to detect this case. Example:
# <THE-VALUE>
# <div xmlns="http://www.w3.org/1999/xhtml">
# &quot;New Header&quot; Template reqFile
# </div>
# </THE-VALUE>
# FIXME: Such tricks can introduce further edge cases. Not clear how to
# implement this properly.
node_has_its_own_ns = False
node_ns_match = re.search("{(.+?)}", node.tag)
if node_ns_match:
node_ns = node_ns_match.group(1)

for nskey_, ns_ in node.nsmap.items():
if ns_ == node_ns and nskey_ is None:
node_has_its_own_ns = True

tag = (
f"{nskey}:{node_no_ns_tag}"
if node.tag[0] == "{" or namespace_tag is not None
else node.tag
node_no_ns_tag
if node_has_its_own_ns
else (
f"{nskey}:{node_no_ns_tag}"
if node.tag[0] == "{" or namespace_tag is not None
else node.tag
)
)
output += f"<{tag}"
if node_has_its_own_ns:
output += f' xmlns="{node.nsmap[None]}"'

for attribute, attribute_value in node.attrib.items():
output += f' {attribute}="{lxml_escape_for_html(attribute_value)}"'
# <object> is surprisingly a tag that must have a closing tag even
Expand Down
12 changes: 9 additions & 3 deletions reqif/models/reqif_reqif_header.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
from typing import Optional
from typing import Optional, Union

from reqif.helpers.debug import auto_described


# Sometimes, the ReqIFs have empty tags. Example:
# <REPOSITORY-ID/>. Creating a custom class to catch this case.
class EmptyTag:
pass


@auto_described
class ReqIFReqIFHeader: # pylint: disable=too-many-instance-attributes
def __init__( # pylint: disable=too-many-arguments
self,
identifier: Optional[str] = None,
comment: Optional[str] = None,
creation_time: Optional[str] = None,
repository_id: Optional[str] = None,
repository_id: Union[None, str, EmptyTag] = None,
req_if_tool_id: Optional[str] = None,
req_if_version: Optional[str] = None,
source_tool_id: Optional[str] = None,
Expand All @@ -19,7 +25,7 @@ def __init__( # pylint: disable=too-many-arguments
self.identifier: Optional[str] = identifier
self.comment: Optional[str] = comment
self.creation_time: Optional[str] = creation_time
self.repository_id: Optional[str] = repository_id
self.repository_id: Union[None, str, EmptyTag] = repository_id
self.req_if_tool_id: Optional[str] = req_if_tool_id
self.req_if_version: Optional[str] = req_if_version
self.source_tool_id: Optional[str] = source_tool_id
Expand Down
28 changes: 17 additions & 11 deletions reqif/parsers/header_parser.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from typing import Optional
from typing import Union

from reqif.helpers.lxml import lxml_escape_title
from reqif.models.reqif_reqif_header import ReqIFReqIFHeader
from reqif.models.reqif_reqif_header import EmptyTag, ReqIFReqIFHeader


class ReqIFHeaderParser:
Expand All @@ -21,7 +21,7 @@ def parse(xml_header) -> ReqIFReqIFHeader:

comment = None
creation_time = None
repository_id: Optional[str] = None
repository_id: Union[None, str, EmptyTag] = None
req_if_tool_id = None
req_if_version = None
source_tool_id = None
Expand All @@ -37,7 +37,10 @@ def parse(xml_header) -> ReqIFReqIFHeader:

xml_repository_id = xml_reqif_header.find("REPOSITORY-ID")
if xml_repository_id is not None:
repository_id = xml_repository_id.text
if xml_repository_id.text is not None:
repository_id = xml_repository_id.text
else:
repository_id = EmptyTag()

xml_req_if_tool_id = xml_reqif_header.find("REQ-IF-TOOL-ID")
if xml_req_if_tool_id is not None:
Expand Down Expand Up @@ -84,13 +87,16 @@ def unparse(header: ReqIFReqIFHeader) -> str:
f"{header.creation_time}"
"</CREATION-TIME>\n"
)
if header.repository_id:
output += (
" "
"<REPOSITORY-ID>"
f"{header.repository_id}"
"</REPOSITORY-ID>\n"
)
if header.repository_id is not None:
if isinstance(header.repository_id, str):
output += (
" "
"<REPOSITORY-ID>"
f"{header.repository_id}"
"</REPOSITORY-ID>\n"
)
else:
output += " <REPOSITORY-ID/>\n"
if header.req_if_tool_id:
output += (
" "
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?xml version="1.0" encoding="UTF-8"?>
<REQ-IF xmlns="http://www.omg.org/spec/ReqIF/20110401/reqif.xsd" xmlns:reqif-common="http://www.prostep.org/reqif" xmlns:rm-reqif="http://www.ibm.com/rm/reqif" xmlns:reqif="http://www.omg.org/spec/ReqIF/20110401/reqif.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:rm="http://www.ibm.com/rm" xmlns:xhtml="http://www.w3.org/1999/xhtml" xsi:schemaLocation="http://www.omg.org/spec/ReqIF/20110401/reqif.xsd reqif.xsd">
<THE-HEADER>
<REQ-IF-HEADER IDENTIFIER="_c9f16ebe-c3b5-4966-a5f8-b1090d3a6c2d">
<CREATION-TIME>2022-01-01T00:01:01.260Z</CREATION-TIME>
<REPOSITORY-ID/>
<REQ-IF-TOOL-ID>IBM</REQ-IF-TOOL-ID>
<REQ-IF-VERSION>1.0</REQ-IF-VERSION>
<SOURCE-TOOL-ID>IBM</SOURCE-TOOL-ID>
<TITLE>IBM Engineering Requirements</TITLE>
</REQ-IF-HEADER>
</THE-HEADER>
<CORE-CONTENT>
<REQ-IF-CONTENT>
<SPEC-OBJECTS>
<SPEC-OBJECT IDENTIFIER="_f05294de-d4cf-455d-9307-c130376dfd44" LAST-CHANGE="2022-01-01T00:01:01.260Z">
<TYPE>
<SPEC-OBJECT-TYPE-REF>_ee420d2b-599c-49ea-a298-6a732ece21eb</SPEC-OBJECT-TYPE-REF>
</TYPE>
<VALUES>
<ATTRIBUTE-VALUE-XHTML>
<DEFINITION>
<ATTRIBUTE-DEFINITION-XHTML-REF>_3ca983d9-5ec5-4752-898f-37ab52beafd0</ATTRIBUTE-DEFINITION-XHTML-REF>
</DEFINITION>
<THE-VALUE>
<div xmlns="http://www.w3.org/1999/xhtml">&lt;FOOOOO&gt; BAR</div>
</THE-VALUE>
</ATTRIBUTE-VALUE-XHTML>
<ATTRIBUTE-VALUE-XHTML>
<DEFINITION>
<ATTRIBUTE-DEFINITION-XHTML-REF>_1f20e97d-8b0f-4f64-a093-deedc195e91a</ATTRIBUTE-DEFINITION-XHTML-REF>
</DEFINITION>
<THE-VALUE>
<div xmlns="http://www.w3.org/1999/xhtml">&quot;New Header&quot; Template reqFile</div>
</THE-VALUE>
</ATTRIBUTE-VALUE-XHTML>
<ATTRIBUTE-VALUE-DATE THE-VALUE="2022-01-01T00:01:01.260Z">
<DEFINITION>
<ATTRIBUTE-DEFINITION-DATE-REF>_dc18f331-d466-4de2-8182-0ef4ced9e174</ATTRIBUTE-DEFINITION-DATE-REF>
</DEFINITION>
</ATTRIBUTE-VALUE-DATE>
<ATTRIBUTE-VALUE-STRING THE-VALUE="XSX_6qwe82_363">
<DEFINITION>
<ATTRIBUTE-DEFINITION-STRING-REF>_d2325dd8-82f3-43d6-b50b-6eb92820fd55</ATTRIBUTE-DEFINITION-STRING-REF>
</DEFINITION>
</ATTRIBUTE-VALUE-STRING>
<ATTRIBUTE-VALUE-BOOLEAN THE-VALUE="false">
<DEFINITION>
<ATTRIBUTE-DEFINITION-BOOLEAN-REF>_d09cedc9-c249-4ff3-affb-e863dcb732b9</ATTRIBUTE-DEFINITION-BOOLEAN-REF>
</DEFINITION>
</ATTRIBUTE-VALUE-BOOLEAN>
<ATTRIBUTE-VALUE-XHTML>
<DEFINITION>
<ATTRIBUTE-DEFINITION-XHTML-REF>_05ee3f1d-6c0c-4e86-8bca-67cc5355db9c</ATTRIBUTE-DEFINITION-XHTML-REF>
</DEFINITION>
<THE-VALUE>
<div xmlns="http://www.w3.org/1999/xhtml"/>
</THE-VALUE>
</ATTRIBUTE-VALUE-XHTML>
<ATTRIBUTE-VALUE-ENUMERATION>
<VALUES>
<ENUM-VALUE-REF>_91651ec0-8832-4553-a201-cad184aa6fdb</ENUM-VALUE-REF>
</VALUES>
<DEFINITION>
<ATTRIBUTE-DEFINITION-ENUMERATION-REF>_03fdb162-e81a-4724-809e-2b660994e18f</ATTRIBUTE-DEFINITION-ENUMERATION-REF>
</DEFINITION>
</ATTRIBUTE-VALUE-ENUMERATION>
<ATTRIBUTE-VALUE-DATE THE-VALUE="2022-01-01T00:01:01.260Z">
<DEFINITION>
<ATTRIBUTE-DEFINITION-DATE-REF>_136696e2-ada5-407a-b65b-e271a96ab983</ATTRIBUTE-DEFINITION-DATE-REF>
</DEFINITION>
</ATTRIBUTE-VALUE-DATE>
<ATTRIBUTE-VALUE-STRING THE-VALUE="0848764">
<DEFINITION>
<ATTRIBUTE-DEFINITION-STRING-REF>_a450fa62-c5ac-43fc-9d08-c90432810d15</ATTRIBUTE-DEFINITION-STRING-REF>
</DEFINITION>
</ATTRIBUTE-VALUE-STRING>
<ATTRIBUTE-VALUE-STRING THE-VALUE="SE6DAW">
<DEFINITION>
<ATTRIBUTE-DEFINITION-STRING-REF>_977a9dd7-8efb-40dd-8456-6e1b548a27c1</ATTRIBUTE-DEFINITION-STRING-REF>
</DEFINITION>
</ATTRIBUTE-VALUE-STRING>
<ATTRIBUTE-VALUE-STRING THE-VALUE="VZ6KYN">
<DEFINITION>
<ATTRIBUTE-DEFINITION-STRING-REF>_318a9d27-fdd6-44ea-8117-5ecc5b6b422a</ATTRIBUTE-DEFINITION-STRING-REF>
</DEFINITION>
</ATTRIBUTE-VALUE-STRING>
</VALUES>
</SPEC-OBJECT>
</SPEC-OBJECTS>
<SPEC-RELATION-GROUPS>
</SPEC-RELATION-GROUPS>
</REQ-IF-CONTENT>
</CORE-CONTENT>
</REQ-IF>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
RUN: mkdir -p %S/output
RUN: %reqif passthrough %S/sample.reqif %S/output/sample.reqif
RUN: diff %S/sample.reqif %S/output/sample.reqif