1#!/usr/bin/python -u 2# -*- coding: ISO-8859-1 -*- 3# 4# this tests the basic APIs of the XmlTextReader interface 5# 6import libxml2 7import sys 8try: 9 import StringIO 10 str_io = StringIO.StringIO 11except: 12 import io 13 str_io = io.StringIO 14 15# Memory debug specific 16libxml2.debugMemory(1) 17 18f = str_io("""<a><b b1="b1"/><c>content of c</c></a>""") 19input = libxml2.inputBuffer(f) 20reader = input.newTextReader("test1") 21ret = reader.Read() 22if ret != 1: 23 print("test1: Error reading to first element") 24 sys.exit(1) 25if reader.Name() != "a" or reader.IsEmptyElement() != 0 or \ 26 reader.NodeType() != 1 or reader.HasAttributes() != 0: 27 print("test1: Error reading the first element") 28 sys.exit(1) 29ret = reader.Read() 30if ret != 1: 31 print("test1: Error reading to second element") 32 sys.exit(1) 33if reader.Name() != "b" or reader.IsEmptyElement() != 1 or \ 34 reader.NodeType() != 1 or reader.HasAttributes() != 1: 35 print("test1: Error reading the second element") 36 sys.exit(1) 37ret = reader.Read() 38if ret != 1: 39 print("test1: Error reading to third element") 40 sys.exit(1) 41if reader.Name() != "c" or reader.IsEmptyElement() != 0 or \ 42 reader.NodeType() != 1 or reader.HasAttributes() != 0: 43 print("test1: Error reading the third element") 44 sys.exit(1) 45ret = reader.Read() 46if ret != 1: 47 print("test1: Error reading to text node") 48 sys.exit(1) 49if reader.Name() != "#text" or reader.IsEmptyElement() != 0 or \ 50 reader.NodeType() != 3 or reader.HasAttributes() != 0 or \ 51 reader.Value() != "content of c": 52 print("test1: Error reading the text node") 53 sys.exit(1) 54ret = reader.Read() 55if ret != 1: 56 print("test1: Error reading to end of third element") 57 sys.exit(1) 58if reader.Name() != "c" or reader.IsEmptyElement() != 0 or \ 59 reader.NodeType() != 15 or reader.HasAttributes() != 0: 60 print("test1: Error reading the end of third element") 61 sys.exit(1) 62ret = reader.Read() 63if ret != 1: 64 print("test1: Error reading to end of first element") 65 sys.exit(1) 66if reader.Name() != "a" or reader.IsEmptyElement() != 0 or \ 67 reader.NodeType() != 15 or reader.HasAttributes() != 0: 68 print("test1: Error reading the end of first element") 69 sys.exit(1) 70ret = reader.Read() 71if ret != 0: 72 print("test1: Error reading to end of document") 73 sys.exit(1) 74 75# 76# example from the XmlTextReader docs 77# 78f = str_io("""<test xmlns:dt="urn:datatypes" dt:type="int"/>""") 79input = libxml2.inputBuffer(f) 80reader = input.newTextReader("test2") 81 82ret = reader.Read() 83if ret != 1: 84 print("Error reading test element") 85 sys.exit(1) 86if reader.GetAttributeNo(0) != "urn:datatypes" or \ 87 reader.GetAttributeNo(1) != "int" or \ 88 reader.GetAttributeNs("type", "urn:datatypes") != "int" or \ 89 reader.GetAttribute("dt:type") != "int": 90 print("error reading test attributes") 91 sys.exit(1) 92 93# 94# example from the XmlTextReader docs 95# 96f = str_io("""<root xmlns:a="urn:456"> 97<item> 98<ref href="a:b"/> 99</item> 100</root>""") 101input = libxml2.inputBuffer(f) 102reader = input.newTextReader("test3") 103 104ret = reader.Read() 105while ret == 1: 106 if reader.Name() == "ref": 107 if reader.LookupNamespace("a") != "urn:456": 108 print("error resolving namespace prefix") 109 sys.exit(1) 110 break 111 ret = reader.Read() 112if ret != 1: 113 print("Error finding the ref element") 114 sys.exit(1) 115 116# 117# Home made example for the various attribute access functions 118# 119f = str_io("""<testattr xmlns="urn:1" xmlns:a="urn:2" b="b" a:b="a:b"/>""") 120input = libxml2.inputBuffer(f) 121reader = input.newTextReader("test4") 122ret = reader.Read() 123if ret != 1: 124 print("Error reading the testattr element") 125 sys.exit(1) 126# 127# Attribute exploration by index 128# 129if reader.MoveToAttributeNo(0) != 1: 130 print("Failed moveToAttribute(0)") 131 sys.exit(1) 132if reader.Value() != "urn:1": 133 print("Failed to read attribute(0)") 134 sys.exit(1) 135if reader.Name() != "xmlns": 136 print("Failed to read attribute(0) name") 137 sys.exit(1) 138if reader.MoveToAttributeNo(1) != 1: 139 print("Failed moveToAttribute(1)") 140 sys.exit(1) 141if reader.Value() != "urn:2": 142 print("Failed to read attribute(1)") 143 sys.exit(1) 144if reader.Name() != "xmlns:a": 145 print("Failed to read attribute(1) name") 146 sys.exit(1) 147if reader.MoveToAttributeNo(2) != 1: 148 print("Failed moveToAttribute(2)") 149 sys.exit(1) 150if reader.Value() != "b": 151 print("Failed to read attribute(2)") 152 sys.exit(1) 153if reader.Name() != "b": 154 print("Failed to read attribute(2) name") 155 sys.exit(1) 156if reader.MoveToAttributeNo(3) != 1: 157 print("Failed moveToAttribute(3)") 158 sys.exit(1) 159if reader.Value() != "a:b": 160 print("Failed to read attribute(3)") 161 sys.exit(1) 162if reader.Name() != "a:b": 163 print("Failed to read attribute(3) name") 164 sys.exit(1) 165# 166# Attribute exploration by name 167# 168if reader.MoveToAttribute("xmlns") != 1: 169 print("Failed moveToAttribute('xmlns')") 170 sys.exit(1) 171if reader.Value() != "urn:1": 172 print("Failed to read attribute('xmlns')") 173 sys.exit(1) 174if reader.MoveToAttribute("xmlns:a") != 1: 175 print("Failed moveToAttribute('xmlns')") 176 sys.exit(1) 177if reader.Value() != "urn:2": 178 print("Failed to read attribute('xmlns:a')") 179 sys.exit(1) 180if reader.MoveToAttribute("b") != 1: 181 print("Failed moveToAttribute('b')") 182 sys.exit(1) 183if reader.Value() != "b": 184 print("Failed to read attribute('b')") 185 sys.exit(1) 186if reader.MoveToAttribute("a:b") != 1: 187 print("Failed moveToAttribute('a:b')") 188 sys.exit(1) 189if reader.Value() != "a:b": 190 print("Failed to read attribute('a:b')") 191 sys.exit(1) 192if reader.MoveToAttributeNs("b", "urn:2") != 1: 193 print("Failed moveToAttribute('b', 'urn:2')") 194 sys.exit(1) 195if reader.Value() != "a:b": 196 print("Failed to read attribute('b', 'urn:2')") 197 sys.exit(1) 198# 199# Go back and read in sequence 200# 201if reader.MoveToElement() != 1: 202 print("Failed to move back to element") 203 sys.exit(1) 204if reader.MoveToFirstAttribute() != 1: 205 print("Failed to move to first attribute") 206 sys.exit(1) 207if reader.Value() != "urn:1": 208 print("Failed to read attribute(0)") 209 sys.exit(1) 210if reader.Name() != "xmlns": 211 print("Failed to read attribute(0) name") 212 sys.exit(1) 213if reader.MoveToNextAttribute() != 1: 214 print("Failed to move to next attribute") 215 sys.exit(1) 216if reader.Value() != "urn:2": 217 print("Failed to read attribute(1)") 218 sys.exit(1) 219if reader.Name() != "xmlns:a": 220 print("Failed to read attribute(1) name") 221 sys.exit(1) 222if reader.MoveToNextAttribute() != 1: 223 print("Failed to move to next attribute") 224 sys.exit(1) 225if reader.Value() != "b": 226 print("Failed to read attribute(2)") 227 sys.exit(1) 228if reader.Name() != "b": 229 print("Failed to read attribute(2) name") 230 sys.exit(1) 231if reader.MoveToNextAttribute() != 1: 232 print("Failed to move to next attribute") 233 sys.exit(1) 234if reader.Value() != "a:b": 235 print("Failed to read attribute(3)") 236 sys.exit(1) 237if reader.Name() != "a:b": 238 print("Failed to read attribute(3) name") 239 sys.exit(1) 240if reader.MoveToNextAttribute() != 0: 241 print("Failed to detect last attribute") 242 sys.exit(1) 243 244 245# 246# a couple of tests for namespace nodes 247# 248f = str_io("""<a xmlns="http://example.com/foo"/>""") 249input = libxml2.inputBuffer(f) 250reader = input.newTextReader("test6") 251ret = reader.Read() 252if ret != 1: 253 print("test6: failed to Read()") 254 sys.exit(1) 255ret = reader.MoveToFirstAttribute() 256if ret != 1: 257 print("test6: failed to MoveToFirstAttribute()") 258 sys.exit(1) 259if reader.NamespaceUri() != "http://www.w3.org/2000/xmlns/" or \ 260 reader.LocalName() != "xmlns" or reader.Name() != "xmlns" or \ 261 reader.Value() != "http://example.com/foo" or reader.NodeType() != 2: 262 print("test6: failed to read the namespace node") 263 sys.exit(1) 264 265f = str_io("""<a xmlns:prefix="http://example.com/foo"/>""") 266input = libxml2.inputBuffer(f) 267reader = input.newTextReader("test7") 268ret = reader.Read() 269if ret != 1: 270 print("test7: failed to Read()") 271 sys.exit(1) 272ret = reader.MoveToFirstAttribute() 273if ret != 1: 274 print("test7: failed to MoveToFirstAttribute()") 275 sys.exit(1) 276if reader.NamespaceUri() != "http://www.w3.org/2000/xmlns/" or \ 277 reader.LocalName() != "prefix" or reader.Name() != "xmlns:prefix" or \ 278 reader.Value() != "http://example.com/foo" or reader.NodeType() != 2: 279 print("test7: failed to read the namespace node") 280 sys.exit(1) 281 282# 283# Test for a limit case: 284# 285f = str_io("""<a/>""") 286input = libxml2.inputBuffer(f) 287reader = input.newTextReader("test8") 288ret = reader.Read() 289if ret != 1: 290 print("test8: failed to read the node") 291 sys.exit(1) 292if reader.Name() != "a" or reader.IsEmptyElement() != 1: 293 print("test8: failed to analyze the node") 294 sys.exit(1) 295ret = reader.Read() 296if ret != 0: 297 print("test8: failed to detect the EOF") 298 sys.exit(1) 299 300# 301# Another test provided by St�phane Bidoul and checked with C# 302# 303def tst_reader(s): 304 f = str_io(s) 305 input = libxml2.inputBuffer(f) 306 reader = input.newTextReader("tst") 307 res = "" 308 while reader.Read(): 309 res=res + "%s (%s) [%s] %d %d\n" % (reader.NodeType(),reader.Name(), 310 reader.Value(), reader.IsEmptyElement(), 311 reader.Depth()) 312 if reader.NodeType() == 1: # Element 313 while reader.MoveToNextAttribute(): 314 res = res + "-- %s (%s) [%s] %d %d\n" % (reader.NodeType(), 315 reader.Name(),reader.Value(), 316 reader.IsEmptyElement(), reader.Depth()) 317 return res 318 319doc="""<a><b b1="b1"/><c>content of c</c></a>""" 320expect="""1 (a) [None] 0 0 3211 (b) [None] 1 1 322-- 2 (b1) [b1] 0 2 3231 (c) [None] 0 1 3243 (#text) [content of c] 0 2 32515 (c) [None] 0 1 32615 (a) [None] 0 0 327""" 328res = tst_reader(doc) 329if res != expect: 330 print("test5 failed") 331 print(res) 332 sys.exit(1) 333 334doc="""<test><b/><c/></test>""" 335expect="""1 (test) [None] 0 0 3361 (b) [None] 1 1 3371 (c) [None] 1 1 33815 (test) [None] 0 0 339""" 340res = tst_reader(doc) 341if res != expect: 342 print("test9 failed") 343 print(res) 344 sys.exit(1) 345 346doc="""<a><b>bbb</b><c>ccc</c></a>""" 347expect="""1 (a) [None] 0 0 3481 (b) [None] 0 1 3493 (#text) [bbb] 0 2 35015 (b) [None] 0 1 3511 (c) [None] 0 1 3523 (#text) [ccc] 0 2 35315 (c) [None] 0 1 35415 (a) [None] 0 0 355""" 356res = tst_reader(doc) 357if res != expect: 358 print("test10 failed") 359 print(res) 360 sys.exit(1) 361 362doc="""<test a="a"/>""" 363expect="""1 (test) [None] 1 0 364-- 2 (a) [a] 0 1 365""" 366res = tst_reader(doc) 367if res != expect: 368 print("test11 failed") 369 print(res) 370 sys.exit(1) 371 372doc="""<test><a>aaa</a><b/></test>""" 373expect="""1 (test) [None] 0 0 3741 (a) [None] 0 1 3753 (#text) [aaa] 0 2 37615 (a) [None] 0 1 3771 (b) [None] 1 1 37815 (test) [None] 0 0 379""" 380res = tst_reader(doc) 381if res != expect: 382 print("test12 failed") 383 print(res) 384 sys.exit(1) 385 386doc="""<test><p></p></test>""" 387expect="""1 (test) [None] 0 0 3881 (p) [None] 0 1 38915 (p) [None] 0 1 39015 (test) [None] 0 0 391""" 392res = tst_reader(doc) 393if res != expect: 394 print("test13 failed") 395 print(res) 396 sys.exit(1) 397 398doc="""<p></p>""" 399expect="""1 (p) [None] 0 0 40015 (p) [None] 0 0 401""" 402res = tst_reader(doc) 403if res != expect: 404 print("test14 failed") 405 print(res) 406 sys.exit(1) 407 408# 409# test from bug #108801 410# 411doc="""<?xml version="1.0" standalone="no"?> 412<!DOCTYPE article PUBLIC "-//OASIS//DTD DocBook XML V4.1.2//EN" 413 "http://www.oasis-open.org/docbook/xml/4.1.2/docbookx.dtd" [ 414]> 415 416<article> 417xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 418</article> 419""" 420expect="""10 (article) [None] 0 0 4211 (article) [None] 0 0 4223 (#text) [ 423xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 424] 0 1 42515 (article) [None] 0 0 426""" 427res = tst_reader(doc) 428if res != expect: 429 print("test15 failed") 430 print(res) 431 sys.exit(1) 432 433# 434# cleanup for memory allocation counting 435# 436del f 437del input 438del reader 439 440# Memory debug specific 441libxml2.cleanupParser() 442if libxml2.debugMemory(1) == 0: 443 print("OK") 444else: 445 print("Memory leak %d bytes" % (libxml2.debugMemory(1))) 446 libxml2.dumpMemory() 447