Skip to content

Commit

Permalink
Test and fix for text attached to end XML elements (#18)
Browse files Browse the repository at this point in the history
The ElementTree's `XMLPullParser` sometimes attaches the text to the
start element and sometimes to the end element. We could not figure out
why there is a different behavior, but suspect that it has something to
do with the size of the parser buffer.

In this patch, we fix by looking for text in both events (start as well
as end element).

This is related to the issue #17, and based on the aas-core-codegen
after the pull request
aas-core-works/aas-core-codegen#443 has been
merged in, [aas-core-codegen 330f391].

[aas-core-codegen 330f391]: aas-core-works/aas-core-codegen@330f391
  • Loading branch information
mristin committed Feb 14, 2024
1 parent be62b1c commit 8b1eddd
Show file tree
Hide file tree
Showing 2 changed files with 669 additions and 10 deletions.
33 changes: 23 additions & 10 deletions aas_core3/xmlization.py
Original file line number Diff line number Diff line change
Expand Up @@ -9340,7 +9340,7 @@ def _raise_if_has_tail_or_attrib(element: Element) -> None:

def _read_end_element(
element: Element, iterator: Iterator[Tuple[str, Element]]
) -> None:
) -> Element:
"""
Read the end element corresponding to the start :paramref:`element`
from :paramref:`iterator`.
Expand All @@ -9367,6 +9367,8 @@ def _read_end_element(

_raise_if_has_tail_or_attrib(next_element)

return next_element


def _read_text_from_element(
element: Element, iterator: Iterator[Tuple[str, Element]]
Expand All @@ -9387,14 +9389,20 @@ def _read_text_from_element(
"""
_raise_if_has_tail_or_attrib(element)

if element.text is None:
raise DeserializationException(
"Expected an element with text, but got an element with no text."
)

text = element.text

_read_end_element(element, iterator)
end_element = _read_end_element(
element,
iterator,
)

if text is None:
if end_element.text is None:
raise DeserializationException(
"Expected an element with text, but got an element with no text."
)

text = end_element.text

return text

Expand Down Expand Up @@ -9520,10 +9528,15 @@ def _read_str_from_element_text(
# the ``element`` to contain *some* text. In contrast, this function
# can also deal with empty text, in which case it returns an empty string.

_raise_if_has_tail_or_attrib(element)
result = element.text if element.text is not None else ""
text = element.text

_read_end_element(element, iterator)
end_element = _read_end_element(element, iterator)

if text is None:
text = end_element.text

_raise_if_has_tail_or_attrib(element)
result = text if text is not None else ""

return result

Expand Down

0 comments on commit 8b1eddd

Please sign in to comment.