Daniel Veillard | 5e5c2d0 | 2002-02-09 18:03:01 +0000 | [diff] [blame] | 1 | import libxml2mod |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 2 | |
Daniel Veillard | 1971ee2 | 2002-01-31 20:29:19 +0000 | [diff] [blame] | 3 | # |
Daniel Veillard | 8d24cc1 | 2002-03-05 15:41:29 +0000 | [diff] [blame] | 4 | # Errors raised by the wrappers when some tree handling failed. |
| 5 | # |
| 6 | class treeError: |
| 7 | def __init__(self, msg): |
| 8 | self.msg = msg |
| 9 | def __str__(self): |
| 10 | return self.msg |
| 11 | |
| 12 | class parserError: |
| 13 | def __init__(self, msg): |
| 14 | self.msg = msg |
| 15 | def __str__(self): |
| 16 | return self.msg |
| 17 | |
| 18 | class uriError: |
| 19 | def __init__(self, msg): |
| 20 | self.msg = msg |
| 21 | def __str__(self): |
| 22 | return self.msg |
| 23 | |
| 24 | class xpathError: |
| 25 | def __init__(self, msg): |
| 26 | self.msg = msg |
| 27 | def __str__(self): |
| 28 | return self.msg |
| 29 | |
| 30 | # |
| 31 | # Example of a class to handle SAX events |
| 32 | # |
| 33 | class SAXCallback: |
| 34 | """Base class for SAX handlers""" |
| 35 | def startDocument(self): |
| 36 | """called at the start of the document""" |
| 37 | pass |
| 38 | |
| 39 | def endDocument(self): |
| 40 | """called at the end of the document""" |
| 41 | pass |
| 42 | |
| 43 | def startElement(self, tag, attrs): |
| 44 | """called at the start of every element, tag is the name of |
| 45 | the element, attrs is a dictionary of the element's attributes""" |
| 46 | pass |
| 47 | |
| 48 | def endElement(self, tag): |
| 49 | """called at the start of every element, tag is the name of |
| 50 | the element""" |
| 51 | pass |
| 52 | |
| 53 | def characters(self, data): |
| 54 | """called when character data have been read, data is the string |
| 55 | containing the data, multiple consecutive characters() callback |
| 56 | are possible.""" |
| 57 | pass |
| 58 | |
| 59 | def cdataBlock(self, data): |
| 60 | """called when CDATA section have been read, data is the string |
| 61 | containing the data, multiple consecutive cdataBlock() callback |
| 62 | are possible.""" |
| 63 | pass |
| 64 | |
| 65 | def reference(self, name): |
| 66 | """called when an entity reference has been found""" |
| 67 | pass |
| 68 | |
| 69 | def ignorableWhitespace(self, data): |
| 70 | """called when potentially ignorable white spaces have been found""" |
| 71 | pass |
| 72 | |
| 73 | def processingInstruction(self, target, data): |
| 74 | """called when a PI has been found, target contains the PI name and |
| 75 | data is the associated data in the PI""" |
| 76 | pass |
| 77 | |
| 78 | def comment(self, content): |
| 79 | """called when a comment has been found, content contains the comment""" |
| 80 | pass |
| 81 | |
| 82 | def externalSubset(self, name, externalID, systemID): |
| 83 | """called when a DOCTYPE declaration has been found, name is the |
| 84 | DTD name and externalID, systemID are the DTD public and system |
| 85 | identifier for that DTd if available""" |
| 86 | pass |
| 87 | |
| 88 | def internalSubset(self, name, externalID, systemID): |
| 89 | """called when a DOCTYPE declaration has been found, name is the |
| 90 | DTD name and externalID, systemID are the DTD public and system |
| 91 | identifier for that DTD if available""" |
| 92 | pass |
| 93 | |
| 94 | def entityDecl(self, name, type, externalID, systemID, content): |
| 95 | """called when an ENTITY declaration has been found, name is the |
| 96 | entity name and externalID, systemID are the entity public and |
| 97 | system identifier for that entity if available, type indicates |
| 98 | the entity type, and content reports it's string content""" |
| 99 | pass |
| 100 | |
| 101 | def notationDecl(self, name, externalID, systemID): |
| 102 | """called when an NOTATION declaration has been found, name is the |
| 103 | notation name and externalID, systemID are the notation public and |
| 104 | system identifier for that notation if available""" |
| 105 | pass |
| 106 | |
| 107 | def attributeDecl(self, elem, name, type, defi, defaultValue, nameList): |
| 108 | """called when an ATTRIBUTE definition has been found""" |
| 109 | pass |
| 110 | |
| 111 | def elementDecl(self, name, type, content): |
| 112 | """called when an ELEMENT definition has been found""" |
| 113 | pass |
| 114 | |
| 115 | def entityDecl(self, name, publicId, systemID, notationName): |
| 116 | """called when an unparsed ENTITY declaration has been found, |
| 117 | name is the entity name and publicId,, systemID are the entity |
| 118 | public and system identifier for that entity if available, |
| 119 | and notationName indicate the associated NOTATION""" |
| 120 | pass |
| 121 | |
| 122 | def warning(self, msg): |
| 123 | print msg |
| 124 | |
| 125 | def error(self, msg): |
| 126 | raise parserError(msg) |
| 127 | |
| 128 | def fatalError(self, msg): |
| 129 | raise parserError(msg) |
| 130 | |
| 131 | # |
Daniel Veillard | 1971ee2 | 2002-01-31 20:29:19 +0000 | [diff] [blame] | 132 | # This class is the ancestor of all the Node classes. It provides |
| 133 | # the basic functionalities shared by all nodes (and handle |
| 134 | # gracefylly the exception), like name, navigation in the tree, |
Daniel Veillard | 1e77438 | 2002-03-06 17:35:40 +0000 | [diff] [blame] | 135 | # doc reference, content access and serializing to a string or URI |
Daniel Veillard | 1971ee2 | 2002-01-31 20:29:19 +0000 | [diff] [blame] | 136 | # |
Daniel Veillard | 36ed529 | 2002-01-30 23:49:06 +0000 | [diff] [blame] | 137 | class xmlCore: |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 138 | def __init__(self, _obj=None): |
| 139 | if _obj != None: |
Daniel Veillard | 01a6d41 | 2002-02-11 18:42:20 +0000 | [diff] [blame] | 140 | self._o = _obj; |
| 141 | return |
| 142 | self._o = None |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 143 | |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 144 | def __getattr__(self, attr): |
| 145 | if attr == "parent": |
Daniel Veillard | 01a6d41 | 2002-02-11 18:42:20 +0000 | [diff] [blame] | 146 | ret = libxml2mod.parent(self._o) |
| 147 | if ret == None: |
| 148 | return None |
| 149 | return xmlNode(_obj=ret) |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 150 | elif attr == "properties": |
Daniel Veillard | 01a6d41 | 2002-02-11 18:42:20 +0000 | [diff] [blame] | 151 | ret = libxml2mod.properties(self._o) |
| 152 | if ret == None: |
| 153 | return None |
| 154 | return xmlAttr(_obj=ret) |
| 155 | elif attr == "children": |
| 156 | ret = libxml2mod.children(self._o) |
| 157 | if ret == None: |
| 158 | return None |
| 159 | return xmlNode(_obj=ret) |
| 160 | elif attr == "last": |
| 161 | ret = libxml2mod.last(self._o) |
| 162 | if ret == None: |
| 163 | return None |
| 164 | return xmlNode(_obj=ret) |
| 165 | elif attr == "next": |
| 166 | ret = libxml2mod.next(self._o) |
| 167 | if ret == None: |
| 168 | return None |
| 169 | return xmlNode(_obj=ret) |
| 170 | elif attr == "prev": |
| 171 | ret = libxml2mod.prev(self._o) |
| 172 | if ret == None: |
| 173 | return None |
| 174 | return xmlNode(_obj=ret) |
| 175 | elif attr == "content": |
| 176 | return libxml2mod.xmlNodeGetContent(self._o) |
| 177 | elif attr == "name": |
| 178 | return libxml2mod.name(self._o) |
| 179 | elif attr == "type": |
| 180 | return libxml2mod.type(self._o) |
| 181 | elif attr == "doc": |
| 182 | ret = libxml2mod.doc(self._o) |
| 183 | if ret == None: |
Daniel Veillard | f742d34 | 2002-03-07 00:05:35 +0000 | [diff] [blame] | 184 | if self.type == "document_xml" or self.type == "document_html": |
| 185 | return xmlDoc(_obj=self._o) |
| 186 | else: |
| 187 | return None |
| 188 | return xmlDoc(_obj=ret) |
Daniel Veillard | 01a6d41 | 2002-02-11 18:42:20 +0000 | [diff] [blame] | 189 | raise AttributeError,attr |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 190 | |
Daniel Veillard | 01a6d41 | 2002-02-11 18:42:20 +0000 | [diff] [blame] | 191 | # |
| 192 | # Those are common attributes to nearly all type of nodes |
| 193 | # |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 194 | def get_parent(self): |
Daniel Veillard | 01a6d41 | 2002-02-11 18:42:20 +0000 | [diff] [blame] | 195 | ret = libxml2mod.parent(self._o) |
| 196 | if ret == None: |
| 197 | return None |
| 198 | return xmlNode(_obj=ret) |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 199 | def get_children(self): |
Daniel Veillard | 01a6d41 | 2002-02-11 18:42:20 +0000 | [diff] [blame] | 200 | ret = libxml2mod.children(self._o) |
| 201 | if ret == None: |
| 202 | return None |
| 203 | return xmlNode(_obj=ret) |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 204 | def get_last(self): |
Daniel Veillard | 01a6d41 | 2002-02-11 18:42:20 +0000 | [diff] [blame] | 205 | ret = libxml2mod.last(self._o) |
| 206 | if ret == None: |
| 207 | return None |
| 208 | return xmlNode(_obj=ret) |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 209 | def get_next(self): |
Daniel Veillard | 01a6d41 | 2002-02-11 18:42:20 +0000 | [diff] [blame] | 210 | ret = libxml2mod.next(self._o) |
| 211 | if ret == None: |
| 212 | return None |
| 213 | return xmlNode(_obj=ret) |
Daniel Veillard | 1971ee2 | 2002-01-31 20:29:19 +0000 | [diff] [blame] | 214 | def get_properties(self): |
Daniel Veillard | 01a6d41 | 2002-02-11 18:42:20 +0000 | [diff] [blame] | 215 | ret = libxml2mod.properties(self._o) |
| 216 | if ret == None: |
| 217 | return None |
| 218 | return xmlAttr(_obj=ret) |
Daniel Veillard | 1971ee2 | 2002-01-31 20:29:19 +0000 | [diff] [blame] | 219 | def get_doc(self): |
Daniel Veillard | 01a6d41 | 2002-02-11 18:42:20 +0000 | [diff] [blame] | 220 | ret = libxml2mod.doc(self._o) |
| 221 | if ret == None: |
| 222 | return None |
| 223 | return xmlDoc(_obj=ret) |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 224 | def get_prev(self): |
Daniel Veillard | 01a6d41 | 2002-02-11 18:42:20 +0000 | [diff] [blame] | 225 | ret = libxml2mod.prev(self._o) |
| 226 | if ret == None: |
| 227 | return None |
| 228 | return xmlNode(_obj=ret) |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 229 | def get_content(self): |
Daniel Veillard | 01a6d41 | 2002-02-11 18:42:20 +0000 | [diff] [blame] | 230 | return libxml2mod.xmlNodeGetContent(self._o) |
Daniel Veillard | 1971ee2 | 2002-01-31 20:29:19 +0000 | [diff] [blame] | 231 | def getContent(self): |
Daniel Veillard | 01a6d41 | 2002-02-11 18:42:20 +0000 | [diff] [blame] | 232 | return libxml2mod.xmlNodeGetContent(self._o) |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 233 | def get_name(self): |
Daniel Veillard | 01a6d41 | 2002-02-11 18:42:20 +0000 | [diff] [blame] | 234 | return libxml2mod.name(self._o) |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 235 | def get_type(self): |
Daniel Veillard | 01a6d41 | 2002-02-11 18:42:20 +0000 | [diff] [blame] | 236 | return libxml2mod.type(self._o) |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 237 | def get_doc(self): |
Daniel Veillard | 01a6d41 | 2002-02-11 18:42:20 +0000 | [diff] [blame] | 238 | ret = libxml2mod.doc(self._o) |
| 239 | if ret == None: |
| 240 | return None |
Daniel Veillard | f742d34 | 2002-03-07 00:05:35 +0000 | [diff] [blame] | 241 | return xmlDoc(_obj=ret) |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 242 | def free(self): |
Daniel Veillard | 5e5c2d0 | 2002-02-09 18:03:01 +0000 | [diff] [blame] | 243 | libxml2mod.freeDoc(self._o) |
Daniel Veillard | 1e77438 | 2002-03-06 17:35:40 +0000 | [diff] [blame] | 244 | |
| 245 | # |
| 246 | # Serialization routines, the optional arguments have the following |
| 247 | # meaning: |
| 248 | # encoding: string to ask saving in a specific encoding |
Daniel Veillard | 12171c4 | 2002-05-29 14:43:10 +0000 | [diff] [blame] | 249 | # format: if 1 the serializer is asked to indent the output |
Daniel Veillard | 1e77438 | 2002-03-06 17:35:40 +0000 | [diff] [blame] | 250 | # |
| 251 | def serialize(self, encoding = None, format = 0): |
| 252 | return libxml2mod.serializeNode(self._o, encoding, format) |
| 253 | def saveTo(self, file, encoding = None, format = 0): |
| 254 | return libxml2mod.saveNodeTo(self._o, file, encoding, format) |
Daniel Veillard | 01a6d41 | 2002-02-11 18:42:20 +0000 | [diff] [blame] | 255 | |
Daniel Veillard | f742d34 | 2002-03-07 00:05:35 +0000 | [diff] [blame] | 256 | # |
| 257 | # Selecting nodes using XPath, a bit slow because the context |
| 258 | # is allocated/freed every time but convenient. |
| 259 | # |
| 260 | def xpathEval(self, expr): |
| 261 | doc = self.doc |
| 262 | if doc == None: |
| 263 | return None |
| 264 | ctxt = doc.xpathNewContext() |
| 265 | ctxt.setContextNode(self) |
| 266 | res = ctxt.xpathEval(expr) |
| 267 | ctxt.xpathFreeContext() |
| 268 | return res |
| 269 | |
Daniel Veillard | 36ed529 | 2002-01-30 23:49:06 +0000 | [diff] [blame] | 270 | # |
Daniel Veillard | 1971ee2 | 2002-01-31 20:29:19 +0000 | [diff] [blame] | 271 | # converters to present a nicer view of the XPath returns |
| 272 | # |
| 273 | def nodeWrap(o): |
| 274 | # TODO try to cast to the most appropriate node class |
Daniel Veillard | 5e5c2d0 | 2002-02-09 18:03:01 +0000 | [diff] [blame] | 275 | name = libxml2mod.name(o) |
Daniel Veillard | 1971ee2 | 2002-01-31 20:29:19 +0000 | [diff] [blame] | 276 | if name == "element" or name == "text": |
| 277 | return xmlNode(_obj=o) |
| 278 | if name == "attribute": |
| 279 | return xmlAttr(_obj=o) |
| 280 | if name[0:8] == "document": |
| 281 | return xmlDoc(_obj=o) |
| 282 | if name[0:8] == "namespace": |
| 283 | return xmlNs(_obj=o) |
| 284 | if name == "elem_decl": |
| 285 | return xmlElement(_obj=o) |
| 286 | if name == "attribute_decl": |
| 287 | return xmlAtribute(_obj=o) |
| 288 | if name == "entity_decl": |
| 289 | return xmlEntity(_obj=o) |
| 290 | if name == "dtd": |
| 291 | return xmlAttr(_obj=o) |
| 292 | return xmlNode(_obj=o) |
| 293 | |
| 294 | def xpathObjectRet(o): |
| 295 | if type(o) == type([]) or type(o) == type(()): |
| 296 | ret = map(lambda x: nodeWrap(x), o) |
Daniel Veillard | 01a6d41 | 2002-02-11 18:42:20 +0000 | [diff] [blame] | 297 | return ret |
Daniel Veillard | 1971ee2 | 2002-01-31 20:29:19 +0000 | [diff] [blame] | 298 | return o |
| 299 | |
| 300 | # |
Daniel Veillard | a7340c8 | 2002-02-01 17:56:45 +0000 | [diff] [blame] | 301 | # register an XPath function |
| 302 | # |
| 303 | def registerXPathFunction(ctxt, name, ns_uri, f): |
Daniel Veillard | 5e5c2d0 | 2002-02-09 18:03:01 +0000 | [diff] [blame] | 304 | ret = libxml2mod.xmlRegisterXPathFunction(ctxt, name, ns_uri, f) |
Daniel Veillard | a7340c8 | 2002-02-01 17:56:45 +0000 | [diff] [blame] | 305 | |
Daniel Veillard | 797a565 | 2002-02-12 13:46:21 +0000 | [diff] [blame] | 306 | |
Daniel Veillard | a7340c8 | 2002-02-01 17:56:45 +0000 | [diff] [blame] | 307 | # |
Daniel Veillard | 36ed529 | 2002-01-30 23:49:06 +0000 | [diff] [blame] | 308 | # Everything below this point is automatically generated |
| 309 | # |
Daniel Veillard | 1971ee2 | 2002-01-31 20:29:19 +0000 | [diff] [blame] | 310 | |