Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

xmlutils.py 1.1KB

1 year ago
1234567891011121314151617181920212223242526272829303132333435
  1. """
  2. Utilities for XML generation/parsing.
  3. """
  4. import re
  5. from xml.sax.saxutils import XMLGenerator
  6. class UnserializableContentError(ValueError):
  7. pass
  8. class SimplerXMLGenerator(XMLGenerator):
  9. def addQuickElement(self, name, contents=None, attrs=None):
  10. "Convenience method for adding an element with no children"
  11. if attrs is None:
  12. attrs = {}
  13. self.startElement(name, attrs)
  14. if contents is not None:
  15. self.characters(contents)
  16. self.endElement(name)
  17. def characters(self, content):
  18. if content and re.search(r"[\x00-\x08\x0B-\x0C\x0E-\x1F]", content):
  19. # Fail loudly when content has control chars (unsupported in XML 1.0)
  20. # See https://www.w3.org/International/questions/qa-controls
  21. raise UnserializableContentError(
  22. "Control characters are not supported in XML 1.0"
  23. )
  24. XMLGenerator.characters(self, content)
  25. def startElement(self, name, attrs):
  26. # Sort attrs for a deterministic output.
  27. sorted_attrs = dict(sorted(attrs.items())) if attrs else attrs
  28. super().startElement(name, sorted_attrs)