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