Daniel Veillard | 5e5c2d0 | 2002-02-09 18:03:01 +0000 | [diff] [blame] | 1 | import libxml2mod |
Daniel Veillard | 85bb5b0 | 2003-12-04 14:12:05 +0000 | [diff] [blame] | 2 | import types |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 3 | |
Daniel Veillard | a81355e | 2004-09-28 11:08:27 +0000 | [diff] [blame] | 4 | # The root of all libxml2 errors. |
| 5 | class libxmlError(Exception): pass |
| 6 | |
Daniel Veillard | 1971ee2 | 2002-01-31 20:29:19 +0000 | [diff] [blame] | 7 | # |
Daniel Veillard | 8d24cc1 | 2002-03-05 15:41:29 +0000 | [diff] [blame] | 8 | # Errors raised by the wrappers when some tree handling failed. |
| 9 | # |
Daniel Veillard | a81355e | 2004-09-28 11:08:27 +0000 | [diff] [blame] | 10 | class treeError(libxmlError): |
Daniel Veillard | 8d24cc1 | 2002-03-05 15:41:29 +0000 | [diff] [blame] | 11 | def __init__(self, msg): |
| 12 | self.msg = msg |
| 13 | def __str__(self): |
| 14 | return self.msg |
| 15 | |
Daniel Veillard | a81355e | 2004-09-28 11:08:27 +0000 | [diff] [blame] | 16 | class parserError(libxmlError): |
Daniel Veillard | 8d24cc1 | 2002-03-05 15:41:29 +0000 | [diff] [blame] | 17 | def __init__(self, msg): |
| 18 | self.msg = msg |
| 19 | def __str__(self): |
| 20 | return self.msg |
| 21 | |
Daniel Veillard | a81355e | 2004-09-28 11:08:27 +0000 | [diff] [blame] | 22 | class uriError(libxmlError): |
Daniel Veillard | 8d24cc1 | 2002-03-05 15:41:29 +0000 | [diff] [blame] | 23 | def __init__(self, msg): |
| 24 | self.msg = msg |
| 25 | def __str__(self): |
| 26 | return self.msg |
| 27 | |
Daniel Veillard | a81355e | 2004-09-28 11:08:27 +0000 | [diff] [blame] | 28 | class xpathError(libxmlError): |
Daniel Veillard | 8d24cc1 | 2002-03-05 15:41:29 +0000 | [diff] [blame] | 29 | def __init__(self, msg): |
| 30 | self.msg = msg |
| 31 | def __str__(self): |
| 32 | return self.msg |
| 33 | |
Daniel Veillard | c6d4a93 | 2002-09-12 15:00:57 +0000 | [diff] [blame] | 34 | class ioWrapper: |
| 35 | def __init__(self, _obj): |
| 36 | self.__io = _obj |
| 37 | self._o = None |
| 38 | |
| 39 | def io_close(self): |
| 40 | if self.__io == None: |
William M. Brack | 1d75c8a | 2003-10-27 13:48:16 +0000 | [diff] [blame] | 41 | return(-1) |
| 42 | self.__io.close() |
| 43 | self.__io = None |
| 44 | return(0) |
Daniel Veillard | c6d4a93 | 2002-09-12 15:00:57 +0000 | [diff] [blame] | 45 | |
| 46 | def io_flush(self): |
| 47 | if self.__io == None: |
William M. Brack | 1d75c8a | 2003-10-27 13:48:16 +0000 | [diff] [blame] | 48 | return(-1) |
| 49 | self.__io.flush() |
| 50 | return(0) |
Daniel Veillard | c6d4a93 | 2002-09-12 15:00:57 +0000 | [diff] [blame] | 51 | |
| 52 | def io_read(self, len = -1): |
| 53 | if self.__io == None: |
William M. Brack | 1d75c8a | 2003-10-27 13:48:16 +0000 | [diff] [blame] | 54 | return(-1) |
Daniel Veillard | c6d4a93 | 2002-09-12 15:00:57 +0000 | [diff] [blame] | 55 | if len < 0: |
William M. Brack | 1d75c8a | 2003-10-27 13:48:16 +0000 | [diff] [blame] | 56 | return(self.__io.read()) |
| 57 | return(self.__io.read(len)) |
Daniel Veillard | c6d4a93 | 2002-09-12 15:00:57 +0000 | [diff] [blame] | 58 | |
| 59 | def io_write(self, str, len = -1): |
| 60 | if self.__io == None: |
William M. Brack | 1d75c8a | 2003-10-27 13:48:16 +0000 | [diff] [blame] | 61 | return(-1) |
Daniel Veillard | c6d4a93 | 2002-09-12 15:00:57 +0000 | [diff] [blame] | 62 | if len < 0: |
William M. Brack | 1d75c8a | 2003-10-27 13:48:16 +0000 | [diff] [blame] | 63 | return(self.__io.write(str)) |
| 64 | return(self.__io.write(str, len)) |
Daniel Veillard | c6d4a93 | 2002-09-12 15:00:57 +0000 | [diff] [blame] | 65 | |
| 66 | class ioReadWrapper(ioWrapper): |
| 67 | def __init__(self, _obj, enc = ""): |
| 68 | ioWrapper.__init__(self, _obj) |
| 69 | self._o = libxml2mod.xmlCreateInputBuffer(self, enc) |
| 70 | |
| 71 | def __del__(self): |
| 72 | print "__del__" |
| 73 | self.io_close() |
| 74 | if self._o != None: |
| 75 | libxml2mod.xmlFreeParserInputBuffer(self._o) |
| 76 | self._o = None |
| 77 | |
| 78 | def close(self): |
| 79 | self.io_close() |
| 80 | if self._o != None: |
| 81 | libxml2mod.xmlFreeParserInputBuffer(self._o) |
| 82 | self._o = None |
| 83 | |
| 84 | class ioWriteWrapper(ioWrapper): |
| 85 | def __init__(self, _obj, enc = ""): |
Daniel Veillard | 85bb5b0 | 2003-12-04 14:12:05 +0000 | [diff] [blame] | 86 | # print "ioWriteWrapper.__init__", _obj |
| 87 | if type(_obj) == type(''): |
William M. Brack | 37e6394 | 2004-07-12 16:27:37 +0000 | [diff] [blame] | 88 | print "write io from a string" |
| 89 | self.o = None |
| 90 | elif type(_obj) == types.InstanceType: |
| 91 | print "write io from instance of %s" % (_obj.__class__) |
| 92 | ioWrapper.__init__(self, _obj) |
| 93 | self._o = libxml2mod.xmlCreateOutputBuffer(self, enc) |
| 94 | else: |
| 95 | file = libxml2mod.outputBufferGetPythonFile(_obj) |
| 96 | if file != None: |
| 97 | ioWrapper.__init__(self, file) |
| 98 | else: |
| 99 | ioWrapper.__init__(self, _obj) |
| 100 | self._o = _obj |
Daniel Veillard | c6d4a93 | 2002-09-12 15:00:57 +0000 | [diff] [blame] | 101 | |
| 102 | def __del__(self): |
Daniel Veillard | 85bb5b0 | 2003-12-04 14:12:05 +0000 | [diff] [blame] | 103 | # print "__del__" |
Daniel Veillard | c6d4a93 | 2002-09-12 15:00:57 +0000 | [diff] [blame] | 104 | self.io_close() |
| 105 | if self._o != None: |
| 106 | libxml2mod.xmlOutputBufferClose(self._o) |
| 107 | self._o = None |
| 108 | |
Daniel Veillard | 85bb5b0 | 2003-12-04 14:12:05 +0000 | [diff] [blame] | 109 | def flush(self): |
| 110 | self.io_flush() |
| 111 | if self._o != None: |
| 112 | libxml2mod.xmlOutputBufferClose(self._o) |
| 113 | self._o = None |
| 114 | |
Daniel Veillard | c6d4a93 | 2002-09-12 15:00:57 +0000 | [diff] [blame] | 115 | def close(self): |
Daniel Veillard | 85bb5b0 | 2003-12-04 14:12:05 +0000 | [diff] [blame] | 116 | self.io_flush() |
Daniel Veillard | c6d4a93 | 2002-09-12 15:00:57 +0000 | [diff] [blame] | 117 | if self._o != None: |
| 118 | libxml2mod.xmlOutputBufferClose(self._o) |
| 119 | self._o = None |
| 120 | |
Daniel Veillard | 8d24cc1 | 2002-03-05 15:41:29 +0000 | [diff] [blame] | 121 | # |
| 122 | # Example of a class to handle SAX events |
| 123 | # |
| 124 | class SAXCallback: |
| 125 | """Base class for SAX handlers""" |
| 126 | def startDocument(self): |
| 127 | """called at the start of the document""" |
| 128 | pass |
| 129 | |
| 130 | def endDocument(self): |
| 131 | """called at the end of the document""" |
| 132 | pass |
| 133 | |
| 134 | def startElement(self, tag, attrs): |
| 135 | """called at the start of every element, tag is the name of |
William M. Brack | 1d75c8a | 2003-10-27 13:48:16 +0000 | [diff] [blame] | 136 | the element, attrs is a dictionary of the element's attributes""" |
Daniel Veillard | 8d24cc1 | 2002-03-05 15:41:29 +0000 | [diff] [blame] | 137 | pass |
| 138 | |
| 139 | def endElement(self, tag): |
| 140 | """called at the start of every element, tag is the name of |
William M. Brack | 1d75c8a | 2003-10-27 13:48:16 +0000 | [diff] [blame] | 141 | the element""" |
Daniel Veillard | 8d24cc1 | 2002-03-05 15:41:29 +0000 | [diff] [blame] | 142 | pass |
| 143 | |
| 144 | def characters(self, data): |
| 145 | """called when character data have been read, data is the string |
William M. Brack | 1d75c8a | 2003-10-27 13:48:16 +0000 | [diff] [blame] | 146 | containing the data, multiple consecutive characters() callback |
| 147 | are possible.""" |
Daniel Veillard | 8d24cc1 | 2002-03-05 15:41:29 +0000 | [diff] [blame] | 148 | pass |
| 149 | |
| 150 | def cdataBlock(self, data): |
| 151 | """called when CDATA section have been read, data is the string |
William M. Brack | 1d75c8a | 2003-10-27 13:48:16 +0000 | [diff] [blame] | 152 | containing the data, multiple consecutive cdataBlock() callback |
| 153 | are possible.""" |
Daniel Veillard | 8d24cc1 | 2002-03-05 15:41:29 +0000 | [diff] [blame] | 154 | pass |
| 155 | |
| 156 | def reference(self, name): |
| 157 | """called when an entity reference has been found""" |
| 158 | pass |
| 159 | |
| 160 | def ignorableWhitespace(self, data): |
| 161 | """called when potentially ignorable white spaces have been found""" |
| 162 | pass |
| 163 | |
| 164 | def processingInstruction(self, target, data): |
| 165 | """called when a PI has been found, target contains the PI name and |
William M. Brack | 1d75c8a | 2003-10-27 13:48:16 +0000 | [diff] [blame] | 166 | data is the associated data in the PI""" |
Daniel Veillard | 8d24cc1 | 2002-03-05 15:41:29 +0000 | [diff] [blame] | 167 | pass |
| 168 | |
| 169 | def comment(self, content): |
| 170 | """called when a comment has been found, content contains the comment""" |
| 171 | pass |
| 172 | |
| 173 | def externalSubset(self, name, externalID, systemID): |
| 174 | """called when a DOCTYPE declaration has been found, name is the |
William M. Brack | 1d75c8a | 2003-10-27 13:48:16 +0000 | [diff] [blame] | 175 | DTD name and externalID, systemID are the DTD public and system |
| 176 | identifier for that DTd if available""" |
Daniel Veillard | 8d24cc1 | 2002-03-05 15:41:29 +0000 | [diff] [blame] | 177 | pass |
| 178 | |
| 179 | def internalSubset(self, name, externalID, systemID): |
| 180 | """called when a DOCTYPE declaration has been found, name is the |
William M. Brack | 1d75c8a | 2003-10-27 13:48:16 +0000 | [diff] [blame] | 181 | DTD name and externalID, systemID are the DTD public and system |
| 182 | identifier for that DTD if available""" |
Daniel Veillard | 8d24cc1 | 2002-03-05 15:41:29 +0000 | [diff] [blame] | 183 | pass |
| 184 | |
| 185 | def entityDecl(self, name, type, externalID, systemID, content): |
| 186 | """called when an ENTITY declaration has been found, name is the |
William M. Brack | 1d75c8a | 2003-10-27 13:48:16 +0000 | [diff] [blame] | 187 | entity name and externalID, systemID are the entity public and |
| 188 | system identifier for that entity if available, type indicates |
| 189 | the entity type, and content reports it's string content""" |
Daniel Veillard | 8d24cc1 | 2002-03-05 15:41:29 +0000 | [diff] [blame] | 190 | pass |
| 191 | |
| 192 | def notationDecl(self, name, externalID, systemID): |
| 193 | """called when an NOTATION declaration has been found, name is the |
William M. Brack | 1d75c8a | 2003-10-27 13:48:16 +0000 | [diff] [blame] | 194 | notation name and externalID, systemID are the notation public and |
| 195 | system identifier for that notation if available""" |
Daniel Veillard | 8d24cc1 | 2002-03-05 15:41:29 +0000 | [diff] [blame] | 196 | pass |
| 197 | |
| 198 | def attributeDecl(self, elem, name, type, defi, defaultValue, nameList): |
| 199 | """called when an ATTRIBUTE definition has been found""" |
William M. Brack | 1d75c8a | 2003-10-27 13:48:16 +0000 | [diff] [blame] | 200 | pass |
Daniel Veillard | 8d24cc1 | 2002-03-05 15:41:29 +0000 | [diff] [blame] | 201 | |
| 202 | def elementDecl(self, name, type, content): |
| 203 | """called when an ELEMENT definition has been found""" |
William M. Brack | 1d75c8a | 2003-10-27 13:48:16 +0000 | [diff] [blame] | 204 | pass |
Daniel Veillard | 8d24cc1 | 2002-03-05 15:41:29 +0000 | [diff] [blame] | 205 | |
| 206 | def entityDecl(self, name, publicId, systemID, notationName): |
| 207 | """called when an unparsed ENTITY declaration has been found, |
William M. Brack | 1d75c8a | 2003-10-27 13:48:16 +0000 | [diff] [blame] | 208 | name is the entity name and publicId,, systemID are the entity |
| 209 | public and system identifier for that entity if available, |
| 210 | and notationName indicate the associated NOTATION""" |
Daniel Veillard | 8d24cc1 | 2002-03-05 15:41:29 +0000 | [diff] [blame] | 211 | pass |
| 212 | |
| 213 | def warning(self, msg): |
Daniel Veillard | eaccdc6 | 2005-10-27 14:10:52 +0000 | [diff] [blame] | 214 | #print msg |
Daniel Veillard | 745648b | 2006-02-27 09:59:30 +0000 | [diff] [blame] | 215 | pass |
Daniel Veillard | 8d24cc1 | 2002-03-05 15:41:29 +0000 | [diff] [blame] | 216 | |
| 217 | def error(self, msg): |
| 218 | raise parserError(msg) |
| 219 | |
| 220 | def fatalError(self, msg): |
| 221 | raise parserError(msg) |
| 222 | |
| 223 | # |
Daniel Veillard | 1971ee2 | 2002-01-31 20:29:19 +0000 | [diff] [blame] | 224 | # This class is the ancestor of all the Node classes. It provides |
| 225 | # the basic functionalities shared by all nodes (and handle |
| 226 | # gracefylly the exception), like name, navigation in the tree, |
Daniel Veillard | 1e77438 | 2002-03-06 17:35:40 +0000 | [diff] [blame] | 227 | # doc reference, content access and serializing to a string or URI |
Daniel Veillard | 1971ee2 | 2002-01-31 20:29:19 +0000 | [diff] [blame] | 228 | # |
Daniel Veillard | 36ed529 | 2002-01-30 23:49:06 +0000 | [diff] [blame] | 229 | class xmlCore: |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 230 | def __init__(self, _obj=None): |
| 231 | if _obj != None: |
Daniel Veillard | 01a6d41 | 2002-02-11 18:42:20 +0000 | [diff] [blame] | 232 | self._o = _obj; |
| 233 | return |
| 234 | self._o = None |
William M. Brack | 40cca61 | 2006-06-26 18:25:40 +0000 | [diff] [blame] | 235 | |
| 236 | def __eq__(self, other): |
| 237 | if other == None: |
| 238 | return False |
| 239 | ret = libxml2mod.compareNodesEqual(self._o, other._o) |
| 240 | if ret == None: |
| 241 | return False |
| 242 | return ret == True |
| 243 | def __ne__(self, other): |
| 244 | if other == None: |
| 245 | return True |
| 246 | ret = libxml2mod.compareNodesEqual(self._o, other._o) |
| 247 | return not ret |
| 248 | def __hash__(self): |
| 249 | ret = libxml2mod.nodeHash(self._o) |
| 250 | return ret |
| 251 | |
Daniel Veillard | 1cd4dae | 2005-01-15 17:45:28 +0000 | [diff] [blame] | 252 | def __str__(self): |
| 253 | return self.serialize() |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 254 | def get_parent(self): |
Daniel Veillard | 01a6d41 | 2002-02-11 18:42:20 +0000 | [diff] [blame] | 255 | ret = libxml2mod.parent(self._o) |
| 256 | if ret == None: |
| 257 | return None |
| 258 | return xmlNode(_obj=ret) |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 259 | def get_children(self): |
Daniel Veillard | 01a6d41 | 2002-02-11 18:42:20 +0000 | [diff] [blame] | 260 | ret = libxml2mod.children(self._o) |
| 261 | if ret == None: |
| 262 | return None |
| 263 | return xmlNode(_obj=ret) |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 264 | def get_last(self): |
Daniel Veillard | 01a6d41 | 2002-02-11 18:42:20 +0000 | [diff] [blame] | 265 | ret = libxml2mod.last(self._o) |
| 266 | if ret == None: |
| 267 | return None |
| 268 | return xmlNode(_obj=ret) |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 269 | def get_next(self): |
Daniel Veillard | 01a6d41 | 2002-02-11 18:42:20 +0000 | [diff] [blame] | 270 | ret = libxml2mod.next(self._o) |
| 271 | if ret == None: |
| 272 | return None |
| 273 | return xmlNode(_obj=ret) |
Daniel Veillard | 1971ee2 | 2002-01-31 20:29:19 +0000 | [diff] [blame] | 274 | def get_properties(self): |
Daniel Veillard | 01a6d41 | 2002-02-11 18:42:20 +0000 | [diff] [blame] | 275 | ret = libxml2mod.properties(self._o) |
| 276 | if ret == None: |
| 277 | return None |
| 278 | return xmlAttr(_obj=ret) |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 279 | def get_prev(self): |
Daniel Veillard | 01a6d41 | 2002-02-11 18:42:20 +0000 | [diff] [blame] | 280 | ret = libxml2mod.prev(self._o) |
| 281 | if ret == None: |
| 282 | return None |
| 283 | return xmlNode(_obj=ret) |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 284 | def get_content(self): |
Daniel Veillard | 01a6d41 | 2002-02-11 18:42:20 +0000 | [diff] [blame] | 285 | return libxml2mod.xmlNodeGetContent(self._o) |
Daniel Veillard | 51a447a | 2003-01-04 19:42:46 +0000 | [diff] [blame] | 286 | getContent = get_content # why is this duplicate naming needed ? |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 287 | def get_name(self): |
Daniel Veillard | 01a6d41 | 2002-02-11 18:42:20 +0000 | [diff] [blame] | 288 | return libxml2mod.name(self._o) |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 289 | def get_type(self): |
Daniel Veillard | 01a6d41 | 2002-02-11 18:42:20 +0000 | [diff] [blame] | 290 | return libxml2mod.type(self._o) |
Daniel Veillard | 51a447a | 2003-01-04 19:42:46 +0000 | [diff] [blame] | 291 | def get_doc(self): |
| 292 | ret = libxml2mod.doc(self._o) |
| 293 | if ret == None: |
| 294 | if self.type in ["document_xml", "document_html"]: |
| 295 | return xmlDoc(_obj=self._o) |
| 296 | else: |
| 297 | return None |
| 298 | return xmlDoc(_obj=ret) |
| 299 | # |
| 300 | # Those are common attributes to nearly all type of nodes |
| 301 | # defined as python2 properties |
| 302 | # |
| 303 | import sys |
| 304 | if float(sys.version[0:3]) < 2.2: |
William M. Brack | 1d75c8a | 2003-10-27 13:48:16 +0000 | [diff] [blame] | 305 | def __getattr__(self, attr): |
| 306 | if attr == "parent": |
| 307 | ret = libxml2mod.parent(self._o) |
| 308 | if ret == None: |
| 309 | return None |
| 310 | return xmlNode(_obj=ret) |
| 311 | elif attr == "properties": |
| 312 | ret = libxml2mod.properties(self._o) |
| 313 | if ret == None: |
| 314 | return None |
| 315 | return xmlAttr(_obj=ret) |
| 316 | elif attr == "children": |
| 317 | ret = libxml2mod.children(self._o) |
| 318 | if ret == None: |
| 319 | return None |
| 320 | return xmlNode(_obj=ret) |
| 321 | elif attr == "last": |
| 322 | ret = libxml2mod.last(self._o) |
| 323 | if ret == None: |
| 324 | return None |
| 325 | return xmlNode(_obj=ret) |
| 326 | elif attr == "next": |
| 327 | ret = libxml2mod.next(self._o) |
| 328 | if ret == None: |
| 329 | return None |
| 330 | return xmlNode(_obj=ret) |
| 331 | elif attr == "prev": |
| 332 | ret = libxml2mod.prev(self._o) |
| 333 | if ret == None: |
| 334 | return None |
| 335 | return xmlNode(_obj=ret) |
| 336 | elif attr == "content": |
| 337 | return libxml2mod.xmlNodeGetContent(self._o) |
| 338 | elif attr == "name": |
| 339 | return libxml2mod.name(self._o) |
| 340 | elif attr == "type": |
| 341 | return libxml2mod.type(self._o) |
| 342 | elif attr == "doc": |
| 343 | ret = libxml2mod.doc(self._o) |
| 344 | if ret == None: |
| 345 | if self.type == "document_xml" or self.type == "document_html": |
| 346 | return xmlDoc(_obj=self._o) |
| 347 | else: |
| 348 | return None |
| 349 | return xmlDoc(_obj=ret) |
| 350 | raise AttributeError,attr |
Daniel Veillard | 51a447a | 2003-01-04 19:42:46 +0000 | [diff] [blame] | 351 | else: |
William M. Brack | 1d75c8a | 2003-10-27 13:48:16 +0000 | [diff] [blame] | 352 | parent = property(get_parent, None, None, "Parent node") |
| 353 | children = property(get_children, None, None, "First child node") |
| 354 | last = property(get_last, None, None, "Last sibling node") |
| 355 | next = property(get_next, None, None, "Next sibling node") |
| 356 | prev = property(get_prev, None, None, "Previous sibling node") |
| 357 | properties = property(get_properties, None, None, "List of properies") |
| 358 | content = property(get_content, None, None, "Content of this node") |
| 359 | name = property(get_name, None, None, "Node name") |
| 360 | type = property(get_type, None, None, "Node type") |
| 361 | doc = property(get_doc, None, None, "The document this node belongs to") |
Daniel Veillard | 1e77438 | 2002-03-06 17:35:40 +0000 | [diff] [blame] | 362 | |
| 363 | # |
| 364 | # Serialization routines, the optional arguments have the following |
| 365 | # meaning: |
| 366 | # encoding: string to ask saving in a specific encoding |
Daniel Veillard | 51a447a | 2003-01-04 19:42:46 +0000 | [diff] [blame] | 367 | # indent: if 1 the serializer is asked to indent the output |
Daniel Veillard | 1e77438 | 2002-03-06 17:35:40 +0000 | [diff] [blame] | 368 | # |
| 369 | def serialize(self, encoding = None, format = 0): |
| 370 | return libxml2mod.serializeNode(self._o, encoding, format) |
| 371 | def saveTo(self, file, encoding = None, format = 0): |
| 372 | return libxml2mod.saveNodeTo(self._o, file, encoding, format) |
Daniel Veillard | 01a6d41 | 2002-02-11 18:42:20 +0000 | [diff] [blame] | 373 | |
Daniel Veillard | f742d34 | 2002-03-07 00:05:35 +0000 | [diff] [blame] | 374 | # |
Daniel Veillard | d5e198a | 2004-03-09 09:03:28 +0000 | [diff] [blame] | 375 | # Canonicalization routines: |
| 376 | # |
| 377 | # nodes: the node set (tuple or list) to be included in the |
| 378 | # canonized image or None if all document nodes should be |
| 379 | # included. |
| 380 | # exclusive: the exclusive flag (0 - non-exclusive |
| 381 | # canonicalization; otherwise - exclusive canonicalization) |
| 382 | # prefixes: the list of inclusive namespace prefixes (strings), |
| 383 | # or None if there is no inclusive namespaces (only for |
| 384 | # exclusive canonicalization, ignored otherwise) |
| 385 | # with_comments: include comments in the result (!=0) or not |
| 386 | # (==0) |
| 387 | def c14nMemory(self, |
| 388 | nodes=None, |
| 389 | exclusive=0, |
| 390 | prefixes=None, |
| 391 | with_comments=0): |
| 392 | if nodes: |
| 393 | nodes = map(lambda n: n._o, nodes) |
| 394 | return libxml2mod.xmlC14NDocDumpMemory( |
| 395 | self.get_doc()._o, |
| 396 | nodes, |
| 397 | exclusive != 0, |
| 398 | prefixes, |
| 399 | with_comments != 0) |
| 400 | def c14nSaveTo(self, |
| 401 | file, |
| 402 | nodes=None, |
| 403 | exclusive=0, |
| 404 | prefixes=None, |
| 405 | with_comments=0): |
| 406 | if nodes: |
| 407 | nodes = map(lambda n: n._o, nodes) |
| 408 | return libxml2mod.xmlC14NDocSaveTo( |
| 409 | self.get_doc()._o, |
| 410 | nodes, |
| 411 | exclusive != 0, |
| 412 | prefixes, |
| 413 | with_comments != 0, |
| 414 | file) |
| 415 | |
| 416 | # |
Daniel Veillard | f742d34 | 2002-03-07 00:05:35 +0000 | [diff] [blame] | 417 | # Selecting nodes using XPath, a bit slow because the context |
| 418 | # is allocated/freed every time but convenient. |
| 419 | # |
| 420 | def xpathEval(self, expr): |
William M. Brack | 1d75c8a | 2003-10-27 13:48:16 +0000 | [diff] [blame] | 421 | doc = self.doc |
| 422 | if doc == None: |
| 423 | return None |
| 424 | ctxt = doc.xpathNewContext() |
| 425 | ctxt.setContextNode(self) |
| 426 | res = ctxt.xpathEval(expr) |
| 427 | ctxt.xpathFreeContext() |
| 428 | return res |
Daniel Veillard | 51a447a | 2003-01-04 19:42:46 +0000 | [diff] [blame] | 429 | |
Daniel Veillard | f88d8cf | 2003-12-08 10:25:02 +0000 | [diff] [blame] | 430 | # # |
| 431 | # # Selecting nodes using XPath, faster because the context |
| 432 | # # is allocated just once per xmlDoc. |
| 433 | # # |
| 434 | # # Removed: DV memleaks c.f. #126735 |
| 435 | # # |
| 436 | # def xpathEval2(self, expr): |
| 437 | # doc = self.doc |
| 438 | # if doc == None: |
| 439 | # return None |
| 440 | # try: |
| 441 | # doc._ctxt.setContextNode(self) |
| 442 | # except: |
| 443 | # doc._ctxt = doc.xpathNewContext() |
| 444 | # doc._ctxt.setContextNode(self) |
| 445 | # res = doc._ctxt.xpathEval(expr) |
| 446 | # return res |
Daniel Veillard | 51a447a | 2003-01-04 19:42:46 +0000 | [diff] [blame] | 447 | def xpathEval2(self, expr): |
Daniel Veillard | f88d8cf | 2003-12-08 10:25:02 +0000 | [diff] [blame] | 448 | return self.xpathEval(expr) |
Daniel Veillard | 51a447a | 2003-01-04 19:42:46 +0000 | [diff] [blame] | 449 | |
Daniel Veillard | f9cf6f5 | 2005-04-12 01:02:29 +0000 | [diff] [blame] | 450 | # Remove namespaces |
| 451 | def removeNsDef(self, href): |
| 452 | """ |
| 453 | Remove a namespace definition from a node. If href is None, |
| 454 | remove all of the ns definitions on that node. The removed |
| 455 | namespaces are returned as a linked list. |
| 456 | |
| 457 | Note: If any child nodes referred to the removed namespaces, |
| 458 | they will be left with dangling links. You should call |
| 459 | renciliateNs() to fix those pointers. |
| 460 | |
| 461 | Note: This method does not free memory taken by the ns |
| 462 | definitions. You will need to free it manually with the |
| 463 | freeNsList() method on the returns xmlNs object. |
| 464 | """ |
| 465 | |
| 466 | ret = libxml2mod.xmlNodeRemoveNsDef(self._o, href) |
| 467 | if ret is None:return None |
| 468 | __tmp = xmlNs(_obj=ret) |
| 469 | return __tmp |
| 470 | |
Daniel Veillard | 51a447a | 2003-01-04 19:42:46 +0000 | [diff] [blame] | 471 | # support for python2 iterators |
| 472 | def walk_depth_first(self): |
| 473 | return xmlCoreDepthFirstItertor(self) |
| 474 | def walk_breadth_first(self): |
| 475 | return xmlCoreBreadthFirstItertor(self) |
| 476 | __iter__ = walk_depth_first |
| 477 | |
| 478 | def free(self): |
| 479 | try: |
| 480 | self.doc._ctxt.xpathFreeContext() |
| 481 | except: |
| 482 | pass |
Daniel Veillard | f88d8cf | 2003-12-08 10:25:02 +0000 | [diff] [blame] | 483 | libxml2mod.xmlFreeDoc(self._o) |
Daniel Veillard | 51a447a | 2003-01-04 19:42:46 +0000 | [diff] [blame] | 484 | |
| 485 | |
| 486 | # |
| 487 | # implements the depth-first iterator for libxml2 DOM tree |
| 488 | # |
| 489 | class xmlCoreDepthFirstItertor: |
| 490 | def __init__(self, node): |
| 491 | self.node = node |
| 492 | self.parents = [] |
| 493 | def __iter__(self): |
| 494 | return self |
| 495 | def next(self): |
| 496 | while 1: |
| 497 | if self.node: |
| 498 | ret = self.node |
| 499 | self.parents.append(self.node) |
| 500 | self.node = self.node.children |
| 501 | return ret |
| 502 | try: |
| 503 | parent = self.parents.pop() |
| 504 | except IndexError: |
| 505 | raise StopIteration |
| 506 | self.node = parent.next |
| 507 | |
| 508 | # |
| 509 | # implements the breadth-first iterator for libxml2 DOM tree |
| 510 | # |
| 511 | class xmlCoreBreadthFirstItertor: |
| 512 | def __init__(self, node): |
| 513 | self.node = node |
| 514 | self.parents = [] |
| 515 | def __iter__(self): |
| 516 | return self |
| 517 | def next(self): |
| 518 | while 1: |
| 519 | if self.node: |
| 520 | ret = self.node |
| 521 | self.parents.append(self.node) |
| 522 | self.node = self.node.next |
| 523 | return ret |
| 524 | try: |
| 525 | parent = self.parents.pop() |
| 526 | except IndexError: |
| 527 | raise StopIteration |
| 528 | self.node = parent.children |
| 529 | |
Daniel Veillard | 36ed529 | 2002-01-30 23:49:06 +0000 | [diff] [blame] | 530 | # |
Daniel Veillard | 1971ee2 | 2002-01-31 20:29:19 +0000 | [diff] [blame] | 531 | # converters to present a nicer view of the XPath returns |
| 532 | # |
| 533 | def nodeWrap(o): |
| 534 | # TODO try to cast to the most appropriate node class |
Daniel Veillard | 1f8658a | 2004-08-14 21:46:31 +0000 | [diff] [blame] | 535 | name = libxml2mod.type(o) |
Daniel Veillard | 1971ee2 | 2002-01-31 20:29:19 +0000 | [diff] [blame] | 536 | if name == "element" or name == "text": |
| 537 | return xmlNode(_obj=o) |
| 538 | if name == "attribute": |
| 539 | return xmlAttr(_obj=o) |
| 540 | if name[0:8] == "document": |
| 541 | return xmlDoc(_obj=o) |
Daniel Veillard | 1f8658a | 2004-08-14 21:46:31 +0000 | [diff] [blame] | 542 | if name == "namespace": |
Daniel Veillard | 1971ee2 | 2002-01-31 20:29:19 +0000 | [diff] [blame] | 543 | return xmlNs(_obj=o) |
| 544 | if name == "elem_decl": |
| 545 | return xmlElement(_obj=o) |
| 546 | if name == "attribute_decl": |
Daniel Veillard | 1f8658a | 2004-08-14 21:46:31 +0000 | [diff] [blame] | 547 | return xmlAttribute(_obj=o) |
Daniel Veillard | 1971ee2 | 2002-01-31 20:29:19 +0000 | [diff] [blame] | 548 | if name == "entity_decl": |
| 549 | return xmlEntity(_obj=o) |
| 550 | if name == "dtd": |
Daniel Veillard | e59494f | 2003-01-04 16:35:29 +0000 | [diff] [blame] | 551 | return xmlDtd(_obj=o) |
Daniel Veillard | 1971ee2 | 2002-01-31 20:29:19 +0000 | [diff] [blame] | 552 | return xmlNode(_obj=o) |
| 553 | |
| 554 | def xpathObjectRet(o): |
Daniel Veillard | 4645906 | 2006-10-10 08:40:04 +0000 | [diff] [blame] | 555 | otype = type(o) |
| 556 | if otype == type([]): |
| 557 | ret = map(xpathObjectRet, o) |
Daniel Veillard | 01a6d41 | 2002-02-11 18:42:20 +0000 | [diff] [blame] | 558 | return ret |
Daniel Veillard | 4645906 | 2006-10-10 08:40:04 +0000 | [diff] [blame] | 559 | elif otype == type(()): |
| 560 | ret = map(xpathObjectRet, o) |
| 561 | return tuple(ret) |
| 562 | elif otype == type('') or otype == type(0) or otype == type(0.0): |
| 563 | return o |
| 564 | else: |
| 565 | return nodeWrap(o) |
Daniel Veillard | 1971ee2 | 2002-01-31 20:29:19 +0000 | [diff] [blame] | 566 | |
| 567 | # |
Daniel Veillard | a7340c8 | 2002-02-01 17:56:45 +0000 | [diff] [blame] | 568 | # register an XPath function |
| 569 | # |
| 570 | def registerXPathFunction(ctxt, name, ns_uri, f): |
Daniel Veillard | 5e5c2d0 | 2002-02-09 18:03:01 +0000 | [diff] [blame] | 571 | ret = libxml2mod.xmlRegisterXPathFunction(ctxt, name, ns_uri, f) |
Daniel Veillard | a7340c8 | 2002-02-01 17:56:45 +0000 | [diff] [blame] | 572 | |
| 573 | # |
Daniel Veillard | f25b4ca | 2002-12-27 15:18:35 +0000 | [diff] [blame] | 574 | # For the xmlTextReader parser configuration |
| 575 | # |
| 576 | PARSER_LOADDTD=1 |
| 577 | PARSER_DEFAULTATTRS=2 |
| 578 | PARSER_VALIDATE=3 |
Daniel Veillard | e18fc18 | 2002-12-28 22:56:33 +0000 | [diff] [blame] | 579 | PARSER_SUBST_ENTITIES=4 |
Daniel Veillard | f25b4ca | 2002-12-27 15:18:35 +0000 | [diff] [blame] | 580 | |
| 581 | # |
Daniel Veillard | 417be3a | 2003-01-20 21:26:34 +0000 | [diff] [blame] | 582 | # For the error callback severities |
Daniel Veillard | 26f7026 | 2003-01-16 22:45:08 +0000 | [diff] [blame] | 583 | # |
Daniel Veillard | 417be3a | 2003-01-20 21:26:34 +0000 | [diff] [blame] | 584 | PARSER_SEVERITY_VALIDITY_WARNING=1 |
| 585 | PARSER_SEVERITY_VALIDITY_ERROR=2 |
| 586 | PARSER_SEVERITY_WARNING=3 |
| 587 | PARSER_SEVERITY_ERROR=4 |
Daniel Veillard | 26f7026 | 2003-01-16 22:45:08 +0000 | [diff] [blame] | 588 | |
| 589 | # |
Daniel Veillard | 3e20a29 | 2003-01-10 13:14:40 +0000 | [diff] [blame] | 590 | # register the libxml2 error handler |
Daniel Veillard | 36ed529 | 2002-01-30 23:49:06 +0000 | [diff] [blame] | 591 | # |
Daniel Veillard | 3e20a29 | 2003-01-10 13:14:40 +0000 | [diff] [blame] | 592 | def registerErrorHandler(f, ctx): |
| 593 | """Register a Python written function to for error reporting. |
| 594 | The function is called back as f(ctx, error). """ |
| 595 | import sys |
| 596 | if not sys.modules.has_key('libxslt'): |
| 597 | # normal behaviour when libxslt is not imported |
| 598 | ret = libxml2mod.xmlRegisterErrorHandler(f,ctx) |
| 599 | else: |
| 600 | # when libxslt is already imported, one must |
| 601 | # use libxst's error handler instead |
| 602 | import libxslt |
| 603 | ret = libxslt.registerErrorHandler(f,ctx) |
| 604 | return ret |
| 605 | |
Daniel Veillard | e6227e0 | 2003-01-14 11:42:39 +0000 | [diff] [blame] | 606 | class parserCtxtCore: |
| 607 | |
| 608 | def __init__(self, _obj=None): |
| 609 | if _obj != None: |
| 610 | self._o = _obj; |
| 611 | return |
| 612 | self._o = None |
| 613 | |
| 614 | def __del__(self): |
| 615 | if self._o != None: |
| 616 | libxml2mod.xmlFreeParserCtxt(self._o) |
William M. Brack | 1d75c8a | 2003-10-27 13:48:16 +0000 | [diff] [blame] | 617 | self._o = None |
Daniel Veillard | e6227e0 | 2003-01-14 11:42:39 +0000 | [diff] [blame] | 618 | |
Daniel Veillard | 417be3a | 2003-01-20 21:26:34 +0000 | [diff] [blame] | 619 | def setErrorHandler(self,f,arg): |
| 620 | """Register an error handler that will be called back as |
| 621 | f(arg,msg,severity,reserved). |
| 622 | |
| 623 | @reserved is currently always None.""" |
| 624 | libxml2mod.xmlParserCtxtSetErrorHandler(self._o,f,arg) |
Daniel Veillard | e6227e0 | 2003-01-14 11:42:39 +0000 | [diff] [blame] | 625 | |
Daniel Veillard | 417be3a | 2003-01-20 21:26:34 +0000 | [diff] [blame] | 626 | def getErrorHandler(self): |
| 627 | """Return (f,arg) as previously registered with setErrorHandler |
| 628 | or (None,None).""" |
| 629 | return libxml2mod.xmlParserCtxtGetErrorHandler(self._o) |
| 630 | |
Daniel Veillard | 5439624 | 2003-04-23 07:36:50 +0000 | [diff] [blame] | 631 | def addLocalCatalog(self, uri): |
| 632 | """Register a local catalog with the parser""" |
| 633 | return libxml2mod.addLocalCatalog(self._o, uri) |
| 634 | |
| 635 | |
Daniel Veillard | 0e460da | 2005-03-30 22:47:10 +0000 | [diff] [blame] | 636 | class ValidCtxtCore: |
| 637 | |
| 638 | def __init__(self, *args, **kw): |
| 639 | pass |
| 640 | |
| 641 | def setValidityErrorHandler(self, err_func, warn_func, arg=None): |
| 642 | """ |
| 643 | Register error and warning handlers for DTD validation. |
| 644 | These will be called back as f(msg,arg) |
| 645 | """ |
| 646 | libxml2mod.xmlSetValidErrors(self._o, err_func, warn_func, arg) |
| 647 | |
| 648 | |
| 649 | class SchemaValidCtxtCore: |
| 650 | |
| 651 | def __init__(self, *args, **kw): |
| 652 | pass |
| 653 | |
| 654 | def setValidityErrorHandler(self, err_func, warn_func, arg=None): |
| 655 | """ |
| 656 | Register error and warning handlers for Schema validation. |
| 657 | These will be called back as f(msg,arg) |
| 658 | """ |
| 659 | libxml2mod.xmlSchemaSetValidErrors(self._o, err_func, warn_func, arg) |
| 660 | |
| 661 | |
| 662 | class relaxNgValidCtxtCore: |
| 663 | |
| 664 | def __init__(self, *args, **kw): |
| 665 | pass |
| 666 | |
| 667 | def setValidityErrorHandler(self, err_func, warn_func, arg=None): |
| 668 | """ |
| 669 | Register error and warning handlers for RelaxNG validation. |
| 670 | These will be called back as f(msg,arg) |
| 671 | """ |
| 672 | libxml2mod.xmlRelaxNGSetValidErrors(self._o, err_func, warn_func, arg) |
| 673 | |
| 674 | |
Daniel Veillard | 417be3a | 2003-01-20 21:26:34 +0000 | [diff] [blame] | 675 | def _xmlTextReaderErrorFunc((f,arg),msg,severity,locator): |
| 676 | """Intermediate callback to wrap the locator""" |
| 677 | return f(arg,msg,severity,xmlTextReaderLocator(locator)) |
Daniel Veillard | e6227e0 | 2003-01-14 11:42:39 +0000 | [diff] [blame] | 678 | |
Daniel Veillard | 26f7026 | 2003-01-16 22:45:08 +0000 | [diff] [blame] | 679 | class xmlTextReaderCore: |
| 680 | |
| 681 | def __init__(self, _obj=None): |
| 682 | self.input = None |
| 683 | if _obj != None:self._o = _obj;return |
| 684 | self._o = None |
| 685 | |
| 686 | def __del__(self): |
| 687 | if self._o != None: |
| 688 | libxml2mod.xmlFreeTextReader(self._o) |
| 689 | self._o = None |
| 690 | |
Daniel Veillard | 417be3a | 2003-01-20 21:26:34 +0000 | [diff] [blame] | 691 | def SetErrorHandler(self,f,arg): |
Daniel Veillard | 26f7026 | 2003-01-16 22:45:08 +0000 | [diff] [blame] | 692 | """Register an error handler that will be called back as |
Daniel Veillard | 417be3a | 2003-01-20 21:26:34 +0000 | [diff] [blame] | 693 | f(arg,msg,severity,locator).""" |
| 694 | if f is None: |
| 695 | libxml2mod.xmlTextReaderSetErrorHandler(\ |
| 696 | self._o,None,None) |
| 697 | else: |
| 698 | libxml2mod.xmlTextReaderSetErrorHandler(\ |
| 699 | self._o,_xmlTextReaderErrorFunc,(f,arg)) |
Daniel Veillard | 26f7026 | 2003-01-16 22:45:08 +0000 | [diff] [blame] | 700 | |
Daniel Veillard | 417be3a | 2003-01-20 21:26:34 +0000 | [diff] [blame] | 701 | def GetErrorHandler(self): |
Daniel Veillard | 26f7026 | 2003-01-16 22:45:08 +0000 | [diff] [blame] | 702 | """Return (f,arg) as previously registered with setErrorHandler |
| 703 | or (None,None).""" |
Daniel Veillard | 417be3a | 2003-01-20 21:26:34 +0000 | [diff] [blame] | 704 | f,arg = libxml2mod.xmlTextReaderGetErrorHandler(self._o) |
| 705 | if f is None: |
| 706 | return None,None |
| 707 | else: |
| 708 | # assert f is _xmlTextReaderErrorFunc |
| 709 | return arg |
Daniel Veillard | 26f7026 | 2003-01-16 22:45:08 +0000 | [diff] [blame] | 710 | |
Daniel Veillard | f93a866 | 2004-07-01 12:56:30 +0000 | [diff] [blame] | 711 | # |
| 712 | # The cleanup now goes though a wrappe in libxml.c |
| 713 | # |
| 714 | def cleanupParser(): |
| 715 | libxml2mod.xmlPythonCleanupParser() |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 716 | |
Daniel Veillard | 3e20a29 | 2003-01-10 13:14:40 +0000 | [diff] [blame] | 717 | # WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING |
| 718 | # |
| 719 | # Everything before this line comes from libxml.py |
| 720 | # Everything after this line is automatically generated |
| 721 | # |
| 722 | # WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING |
Daniel Veillard | 1971ee2 | 2002-01-31 20:29:19 +0000 | [diff] [blame] | 723 | |