PageRenderTime 37ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/src/robot/utils/abstractxmlwriter.py

https://code.google.com/p/robotframework/
Python | 66 lines | 43 code | 8 blank | 15 comment | 2 complexity | 1fae01cf1f310e361c9789030d7ac3a5 MD5 | raw file
Possible License(s): Apache-2.0
  1. # Copyright 2008-2011 Nokia Siemens Networks Oyj
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import re
  15. from .unic import unic
  16. class AbstractXmlWriter:
  17. _illegal_chars = re.compile(u'[\x00-\x08\x0B\x0C\x0E-\x1F\uFFFE\uFFFF]')
  18. def start(self, name, attributes={}, newline=True):
  19. self._start(name, self._escape_attrs(attributes))
  20. if newline:
  21. self.content('\n')
  22. def _start(self, name, attrs):
  23. raise NotImplementedError
  24. def _escape_attrs(self, attrs):
  25. return dict((n, self._escape(v)) for n, v in attrs.items())
  26. def _escape(self, content):
  27. # TODO: Test is the IPY bug below still valid with new implementation:
  28. # http://ironpython.codeplex.com/workitem/29402
  29. return self._illegal_chars.sub('', unic(content))
  30. def content(self, content):
  31. if content is not None:
  32. self._content(self._escape(content))
  33. def _content(self, content):
  34. raise NotImplementedError
  35. def end(self, name, newline=True):
  36. self._end(name)
  37. if newline:
  38. self.content('\n')
  39. def _end(self, name):
  40. raise NotImplementedError
  41. def element(self, name, content=None, attributes={}, newline=True):
  42. self.start(name, attributes, newline=False)
  43. self.content(content)
  44. self.end(name, newline)
  45. def close(self):
  46. if not self.closed:
  47. self._close()
  48. self.closed = True
  49. def _close(self):
  50. self._writer.endDocument()
  51. self._output.close()