serialize.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. #!/usr/bin/python -u
  2. import sys
  3. import libxml2
  4. # Memory debug specific
  5. libxml2.debugMemory(1)
  6. #
  7. # Testing XML document serialization
  8. #
  9. doc = libxml2.parseDoc("""<root><foo>hello</foo></root>""")
  10. str = doc.serialize()
  11. if str != """<?xml version="1.0"?>
  12. <root><foo>hello</foo></root>
  13. """:
  14. print "error serializing XML document 1"
  15. sys.exit(1)
  16. str = doc.serialize("iso-8859-1")
  17. if str != """<?xml version="1.0" encoding="iso-8859-1"?>
  18. <root><foo>hello</foo></root>
  19. """:
  20. print "error serializing XML document 2"
  21. sys.exit(1)
  22. str = doc.serialize(format=1)
  23. if str != """<?xml version="1.0"?>
  24. <root>
  25. <foo>hello</foo>
  26. </root>
  27. """:
  28. print "error serializing XML document 3"
  29. sys.exit(1)
  30. str = doc.serialize("iso-8859-1", 1)
  31. if str != """<?xml version="1.0" encoding="iso-8859-1"?>
  32. <root>
  33. <foo>hello</foo>
  34. </root>
  35. """:
  36. print "error serializing XML document 4"
  37. sys.exit(1)
  38. #
  39. # Test serializing a subnode
  40. #
  41. root = doc.getRootElement()
  42. str = root.serialize()
  43. if str != """<root><foo>hello</foo></root>""":
  44. print "error serializing XML root 1"
  45. sys.exit(1)
  46. str = root.serialize("iso-8859-1")
  47. if str != """<root><foo>hello</foo></root>""":
  48. print "error serializing XML root 2"
  49. sys.exit(1)
  50. str = root.serialize(format=1)
  51. if str != """<root>
  52. <foo>hello</foo>
  53. </root>""":
  54. print "error serializing XML root 3"
  55. sys.exit(1)
  56. str = root.serialize("iso-8859-1", 1)
  57. if str != """<root>
  58. <foo>hello</foo>
  59. </root>""":
  60. print "error serializing XML root 4"
  61. sys.exit(1)
  62. doc.freeDoc()
  63. #
  64. # Testing HTML document serialization
  65. #
  66. doc = libxml2.htmlParseDoc("""<html><head><title>Hello</title><body><p>hello</body></html>""", None)
  67. str = doc.serialize()
  68. if str != """<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
  69. <html><head><title>Hello</title></head><body><p>hello</p></body></html>
  70. """:
  71. print "error serializing HTML document 1"
  72. sys.exit(1)
  73. str = doc.serialize("ISO-8859-1")
  74. if str != """<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
  75. <html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Hello</title></head><body><p>hello</p></body></html>
  76. """:
  77. print "error serializing HTML document 2"
  78. sys.exit(1)
  79. str = doc.serialize(format=1)
  80. if str != """<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
  81. <html>
  82. <head>
  83. <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
  84. <title>Hello</title>
  85. </head>
  86. <body><p>hello</p></body>
  87. </html>
  88. """:
  89. print "error serializing HTML document 3"
  90. sys.exit(1)
  91. str = doc.serialize("iso-8859-1", 1)
  92. if str != """<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
  93. <html>
  94. <head>
  95. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  96. <title>Hello</title>
  97. </head>
  98. <body><p>hello</p></body>
  99. </html>
  100. """:
  101. print "error serializing HTML document 4"
  102. sys.exit(1)
  103. #
  104. # Test serializing a subnode
  105. #
  106. doc.htmlSetMetaEncoding(None)
  107. root = doc.getRootElement()
  108. str = root.serialize()
  109. if str != """<html><head><title>Hello</title></head><body><p>hello</p></body></html>""":
  110. print "error serializing HTML root 1"
  111. sys.exit(1)
  112. str = root.serialize("ISO-8859-1")
  113. if str != """<html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Hello</title></head><body><p>hello</p></body></html>""":
  114. print "error serializing HTML root 2"
  115. sys.exit(1)
  116. str = root.serialize(format=1)
  117. if str != """<html>
  118. <head>
  119. <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
  120. <title>Hello</title>
  121. </head>
  122. <body><p>hello</p></body>
  123. </html>""":
  124. print "error serializing HTML root 3"
  125. sys.exit(1)
  126. str = root.serialize("iso-8859-1", 1)
  127. if str != """<html>
  128. <head>
  129. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  130. <title>Hello</title>
  131. </head>
  132. <body><p>hello</p></body>
  133. </html>""":
  134. print "error serializing HTML root 4"
  135. sys.exit(1)
  136. doc.freeDoc()
  137. # Memory debug specific
  138. libxml2.cleanupParser()
  139. if libxml2.debugMemory(1) == 0:
  140. print "OK"
  141. else:
  142. print "Memory leak %d bytes" % (libxml2.debugMemory(1))
  143. libxml2.dumpMemory()