testxmlgen.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # Copyright (C) 2009 Søren Roug, European Environment Agency
  4. #
  5. # This is free software. You may redistribute it under the terms
  6. # of the Apache license and the GNU General Public License Version
  7. # 2 or at your option any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public
  15. # License along with this program; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. #
  18. # Contributor(s):
  19. #
  20. import xml.sax, xml.sax.saxutils
  21. import io
  22. import tempfile
  23. import unittest
  24. import sys
  25. class MyGen(xml.sax.saxutils.XMLGenerator):
  26. def _qname(self, name):
  27. """Builds a qualified name from a (ns_url, localname) pair"""
  28. if name[0]:
  29. if name[0] == u'http://www.w3.org/XML/1998/namespace':
  30. return u'xml' + ":" + name[1]
  31. # The name is in a non-empty namespace
  32. prefix = self._current_context[name[0]]
  33. if prefix:
  34. # If it is not the default namespace, prepend the prefix
  35. return prefix + ":" + name[1]
  36. # Return the unqualified name
  37. return name[1]
  38. class TestXMLGenerator(unittest.TestCase):
  39. def test_xmlgenerator(self):
  40. """ Test that the xml namespace is understood by XMLGenerator """
  41. outfp = tempfile.TemporaryFile()
  42. c = xml.sax.saxutils.XMLGenerator(outfp,'utf-8')
  43. parser = xml.sax.make_parser()
  44. parser.setFeature(xml.sax.handler.feature_namespaces, 1)
  45. parser.setContentHandler(c)
  46. testcontent="""<?xml version="1.0"?>
  47. <a:greetings xmlns:a="http://example.com/ns" xmlns:xml="http://www.w3.org/XML/1998/namespace">
  48. <a:greet xml:lang="en">Hello world</a:greet>
  49. </a:greetings>"""
  50. parser.feed(testcontent)
  51. parser.close()
  52. expectedresult = """<?xml version="1.0" encoding="utf-8"?>
  53. <a:greetings xmlns:a="http://example.com/ns" xmlns:xml="http://www.w3.org/XML/1998/namespace">
  54. <a:greet xml:lang="en">Hello world</a:greet>
  55. </a:greetings>"""
  56. outfp.seek(0)
  57. self.assertEqual( outfp.read().decode('utf-8'), expectedresult)
  58. outfp.close()
  59. def test_xmlgenerator_wo_ns(self):
  60. """ Test that the missing xml namespace is understood by XMLGenerator """
  61. outfp = tempfile.TemporaryFile()
  62. c = xml.sax.saxutils.XMLGenerator(outfp,'utf-8')
  63. parser = xml.sax.make_parser()
  64. parser.setFeature(xml.sax.handler.feature_namespaces, 1)
  65. parser.setContentHandler(c)
  66. testcontent="""<?xml version="1.0"?>
  67. <a:greetings xmlns:a="http://example.com/ns">
  68. <a:greet xml:lang="en">Hello world</a:greet>
  69. </a:greetings>"""
  70. # There is a bug in older versions of saxutils
  71. if sys.version_info[0] == 2 and sys.version_info[1] == 6:
  72. self.assertRaises(KeyError, parser.feed, testcontent)
  73. else:
  74. parser.feed(testcontent)
  75. parser.close()
  76. expectedresult="""<?xml version="1.0" encoding="utf-8"?>
  77. <a:greetings xmlns:a="http://example.com/ns">
  78. <a:greet xml:lang="en">Hello world</a:greet>
  79. </a:greetings>"""
  80. outfp.seek(0)
  81. self.assertEqual( outfp.read().decode('utf-8'), expectedresult)
  82. outfp.close()
  83. def test_myxml(self):
  84. """ Test that my patch works """
  85. outfp = tempfile.TemporaryFile()
  86. c = MyGen(outfp,'utf-8')
  87. parser = xml.sax.make_parser()
  88. parser.setFeature(xml.sax.handler.feature_namespaces, 1)
  89. parser.setContentHandler(c)
  90. testcontent="""<?xml version="1.0"?>
  91. <a:greetings xmlns:a="http://example.com/ns" xmlns:xml="http://www.w3.org/XML/1998/namespace">
  92. <a:greet xml:lang="en">Hello world</a:greet>
  93. </a:greetings>"""
  94. parser.feed(testcontent)
  95. parser.close()
  96. outfp.seek(0)
  97. expectedresult = """<?xml version="1.0" encoding="utf-8"?>
  98. <a:greetings xmlns:a="http://example.com/ns" xmlns:xml="http://www.w3.org/XML/1998/namespace">
  99. <a:greet xml:lang="en">Hello world</a:greet>
  100. </a:greetings>"""
  101. self.assertEqual( outfp.read().decode('utf-8'), expectedresult)
  102. outfp.close()
  103. def test_myxml_wo_xml(self):
  104. """ Test that my patch understands the missing xml namespace """
  105. outfp = tempfile.TemporaryFile()
  106. c = MyGen(outfp,'utf-8')
  107. parser = xml.sax.make_parser()
  108. parser.setFeature(xml.sax.handler.feature_namespaces, 1)
  109. parser.setContentHandler(c)
  110. testcontent="""<?xml version="1.0"?>
  111. <a:greetings xmlns:a="http://example.com/ns" xmlns:xml="http://www.w3.org/XML/1998/namespace">
  112. <a:greet xml:lang="en">Hello world</a:greet>
  113. </a:greetings>"""
  114. parser.feed(testcontent)
  115. parser.close()
  116. outfp.seek(0)
  117. expectedresult = """<?xml version="1.0" encoding="utf-8"?>
  118. <a:greetings xmlns:a="http://example.com/ns" xmlns:xml="http://www.w3.org/XML/1998/namespace">
  119. <a:greet xml:lang="en">Hello world</a:greet>
  120. </a:greetings>"""
  121. self.assertEqual( outfp.read().decode('utf-8'), expectedresult)
  122. outfp.close()
  123. if __name__ == '__main__':
  124. unittest.main()