Daniel Veillard | 0eb38c7 | 2002-12-14 23:00:35 +0000 | [diff] [blame] | 1 | #!/usr/bin/python -u |
| 2 | import libxml2 |
| 3 | import StringIO |
| 4 | import sys |
| 5 | |
| 6 | # Memory debug specific |
| 7 | libxml2.debugMemory(1) |
| 8 | |
| 9 | f = StringIO.StringIO("""<a><b b1="b1"/><c>content of c</c></a>""") |
| 10 | input = libxml2.inputBuffer(f) |
| 11 | reader = input.newTextReader() |
| 12 | ret = reader.read() |
| 13 | if ret != 1: |
| 14 | print "Error reading to first element" |
| 15 | sys.exit(1) |
| 16 | if reader.name() != "a" or reader.isEmptyElement() != 0 or \ |
| 17 | reader.nodeType() != 1 or reader.hasAttributes() != 0: |
| 18 | print "Error reading the first element" |
| 19 | sys.exit(1) |
| 20 | ret = reader.read() |
| 21 | if ret != 1: |
| 22 | print "Error reading to second element" |
| 23 | sys.exit(1) |
| 24 | if reader.name() != "b" or reader.isEmptyElement() != 1 or \ |
| 25 | reader.nodeType() != 1 or reader.hasAttributes() != 1: |
| 26 | print "Error reading the second element" |
| 27 | sys.exit(1) |
| 28 | ret = reader.read() |
| 29 | if ret != 1: |
| 30 | print "Error reading to third element" |
| 31 | sys.exit(1) |
| 32 | if reader.name() != "c" or reader.isEmptyElement() != 0 or \ |
| 33 | reader.nodeType() != 1 or reader.hasAttributes() != 0: |
| 34 | print "Error reading the third element" |
| 35 | sys.exit(1) |
| 36 | ret = reader.read() |
| 37 | if ret != 1: |
| 38 | print "Error reading to text node" |
| 39 | sys.exit(1) |
| 40 | if reader.name() != "#text" or reader.isEmptyElement() != 0 or \ |
| 41 | reader.nodeType() != 3 or reader.hasAttributes() != 0 or \ |
| 42 | reader.value() != "content of c": |
| 43 | print "Error reading the text node" |
| 44 | sys.exit(1) |
| 45 | ret = reader.read() |
| 46 | if ret != 1: |
| 47 | print "Error reading to end of third element" |
| 48 | sys.exit(1) |
| 49 | if reader.name() != "c" or reader.isEmptyElement() != 0 or \ |
| 50 | reader.nodeType() != 15 or reader.hasAttributes() != 0: |
| 51 | print "Error reading the end of third element" |
| 52 | sys.exit(1) |
| 53 | ret = reader.read() |
| 54 | if ret != 1: |
| 55 | print "Error reading to end of first element" |
| 56 | sys.exit(1) |
| 57 | if reader.name() != "a" or reader.isEmptyElement() != 0 or \ |
| 58 | reader.nodeType() != 15 or reader.hasAttributes() != 0: |
| 59 | print "Error reading the end of first element" |
| 60 | sys.exit(1) |
| 61 | ret = reader.read() |
| 62 | if ret != 0: |
| 63 | print "Error reading to end of document" |
| 64 | sys.exit(1) |
| 65 | |
| 66 | # |
| 67 | # example from the XmlTextReader docs |
| 68 | # |
| 69 | f = StringIO.StringIO("""<test xmlns:dt="urn:datatypes" dt:type="int"/>""") |
| 70 | input = libxml2.inputBuffer(f) |
| 71 | reader = input.newTextReader() |
| 72 | |
| 73 | ret = reader.read() |
| 74 | if ret != 1: |
| 75 | print "Error reading test element" |
| 76 | sys.exit(1) |
| 77 | if reader.getAttributeNo(0) != "urn:datatypes" or \ |
| 78 | reader.getAttributeNo(1) != "int" or \ |
| 79 | reader.getAttributeNs("type", "urn:datatypes") != "int" or \ |
| 80 | reader.getAttribute("dt:type") != "int": |
| 81 | print "error reading test attributes" |
| 82 | sys.exit(1) |
| 83 | |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame^] | 84 | # |
| 85 | # example from the XmlTextReader docs |
| 86 | # |
| 87 | f = StringIO.StringIO("""<root xmlns:a="urn:456"> |
| 88 | <item> |
| 89 | <ref href="a:b"/> |
| 90 | </item> |
| 91 | </root>""") |
| 92 | input = libxml2.inputBuffer(f) |
| 93 | reader = input.newTextReader() |
| 94 | |
| 95 | ret = reader.read() |
| 96 | while ret == 1: |
| 97 | if reader.name() == "ref": |
| 98 | if reader.lookupNamespace("a") != "urn:456": |
| 99 | print "error resolving namespace prefix" |
| 100 | sys.exit(1) |
| 101 | break |
| 102 | ret = reader.read() |
| 103 | if ret != 1: |
| 104 | print "Error finding the ref element" |
| 105 | sys.exit(1) |
| 106 | |
| 107 | # |
| 108 | # Home made example for the various attribute access functions |
| 109 | # |
| 110 | f = StringIO.StringIO("""<testattr xmlns="urn:1" xmlns:a="urn:2" b="b" a:b="a:b"/>""") |
| 111 | input = libxml2.inputBuffer(f) |
| 112 | reader = input.newTextReader() |
| 113 | ret = reader.read() |
| 114 | if ret != 1: |
| 115 | print "Error reading the testattr element" |
| 116 | sys.exit(1) |
| 117 | # |
| 118 | # Attribute exploration by index |
| 119 | # |
| 120 | if reader.moveToAttributeNo(0) != 1: |
| 121 | print "Failed moveToAttribute(0)" |
| 122 | sys.exit(1) |
| 123 | if reader.value() != "urn:1": |
| 124 | print "Failed to read attribute(0)" |
| 125 | sys.exit(1) |
| 126 | if reader.name() != "xmlns": |
| 127 | print "Failed to read attribute(0) name" |
| 128 | sys.exit(1) |
| 129 | if reader.moveToAttributeNo(1) != 1: |
| 130 | print "Failed moveToAttribute(1)" |
| 131 | sys.exit(1) |
| 132 | if reader.value() != "urn:2": |
| 133 | print "Failed to read attribute(1)" |
| 134 | sys.exit(1) |
| 135 | if reader.name() != "xmlns:a": |
| 136 | print "Failed to read attribute(1) name" |
| 137 | sys.exit(1) |
| 138 | if reader.moveToAttributeNo(2) != 1: |
| 139 | print "Failed moveToAttribute(2)" |
| 140 | sys.exit(1) |
| 141 | if reader.value() != "b": |
| 142 | print "Failed to read attribute(2)" |
| 143 | sys.exit(1) |
| 144 | if reader.name() != "b": |
| 145 | print "Failed to read attribute(2) name" |
| 146 | sys.exit(1) |
| 147 | if reader.moveToAttributeNo(3) != 1: |
| 148 | print "Failed moveToAttribute(3)" |
| 149 | sys.exit(1) |
| 150 | if reader.value() != "a:b": |
| 151 | print "Failed to read attribute(3)" |
| 152 | sys.exit(1) |
| 153 | if reader.name() != "a:b": |
| 154 | print "Failed to read attribute(3) name" |
| 155 | sys.exit(1) |
| 156 | # |
| 157 | # Attribute exploration by name |
| 158 | # |
| 159 | if reader.moveToAttribute("xmlns") != 1: |
| 160 | print "Failed moveToAttribute('xmlns')" |
| 161 | sys.exit(1) |
| 162 | if reader.value() != "urn:1": |
| 163 | print "Failed to read attribute('xmlns')" |
| 164 | sys.exit(1) |
| 165 | if reader.moveToAttribute("xmlns:a") != 1: |
| 166 | print "Failed moveToAttribute('xmlns')" |
| 167 | sys.exit(1) |
| 168 | if reader.value() != "urn:2": |
| 169 | print "Failed to read attribute('xmlns:a')" |
| 170 | sys.exit(1) |
| 171 | if reader.moveToAttribute("b") != 1: |
| 172 | print "Failed moveToAttribute('b')" |
| 173 | sys.exit(1) |
| 174 | if reader.value() != "b": |
| 175 | print "Failed to read attribute('b')" |
| 176 | sys.exit(1) |
| 177 | if reader.moveToAttribute("a:b") != 1: |
| 178 | print "Failed moveToAttribute('a:b')" |
| 179 | sys.exit(1) |
| 180 | if reader.value() != "a:b": |
| 181 | print "Failed to read attribute('a:b')" |
| 182 | sys.exit(1) |
| 183 | if reader.moveToAttributeNs("b", "urn:2") != 1: |
| 184 | print "Failed moveToAttribute('b', 'urn:2')" |
| 185 | sys.exit(1) |
| 186 | if reader.value() != "a:b": |
| 187 | print "Failed to read attribute('b', 'urn:2')" |
| 188 | sys.exit(1) |
| 189 | # |
| 190 | # Go back and read in sequence |
| 191 | # |
| 192 | if reader.moveToElement() != 1: |
| 193 | print "Failed to move back to element" |
| 194 | sys.exit(1) |
| 195 | if reader.moveToFirstAttribute() != 1: |
| 196 | print "Failed to move to first attribute" |
| 197 | sys.exit(1) |
| 198 | if reader.value() != "urn:1": |
| 199 | print "Failed to read attribute(0)" |
| 200 | sys.exit(1) |
| 201 | if reader.name() != "xmlns": |
| 202 | print "Failed to read attribute(0) name" |
| 203 | sys.exit(1) |
| 204 | if reader.moveToNextAttribute() != 1: |
| 205 | print "Failed to move to next attribute" |
| 206 | sys.exit(1) |
| 207 | if reader.value() != "urn:2": |
| 208 | print "Failed to read attribute(1)" |
| 209 | sys.exit(1) |
| 210 | if reader.name() != "xmlns:a": |
| 211 | print "Failed to read attribute(1) name" |
| 212 | sys.exit(1) |
| 213 | if reader.moveToNextAttribute() != 1: |
| 214 | print "Failed to move to next attribute" |
| 215 | sys.exit(1) |
| 216 | if reader.value() != "b": |
| 217 | print "Failed to read attribute(2)" |
| 218 | sys.exit(1) |
| 219 | if reader.name() != "b": |
| 220 | print "Failed to read attribute(2) name" |
| 221 | sys.exit(1) |
| 222 | if reader.moveToNextAttribute() != 1: |
| 223 | print "Failed to move to next attribute" |
| 224 | sys.exit(1) |
| 225 | if reader.value() != "a:b": |
| 226 | print "Failed to read attribute(3)" |
| 227 | sys.exit(1) |
| 228 | if reader.name() != "a:b": |
| 229 | print "Failed to read attribute(3) name" |
| 230 | sys.exit(1) |
| 231 | if reader.moveToNextAttribute() != 0: |
| 232 | print "Failed to detect last attribute" |
| 233 | sys.exit(1) |
| 234 | |
Daniel Veillard | 0eb38c7 | 2002-12-14 23:00:35 +0000 | [diff] [blame] | 235 | del f |
| 236 | del input |
| 237 | del reader |
| 238 | |
| 239 | # Memory debug specific |
| 240 | libxml2.cleanupParser() |
| 241 | if libxml2.debugMemory(1) == 0: |
| 242 | print "OK" |
| 243 | else: |
| 244 | print "Memory leak %d bytes" % (libxml2.debugMemory(1)) |
| 245 | libxml2.dumpMemory() |