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, |
| 135 | # doc reference and content access |
| 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: |
| 184 | return None |
| 185 | return xmlDoc(_doc=ret) |
| 186 | raise AttributeError,attr |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 187 | |
Daniel Veillard | 01a6d41 | 2002-02-11 18:42:20 +0000 | [diff] [blame] | 188 | # |
| 189 | # Those are common attributes to nearly all type of nodes |
| 190 | # |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 191 | def get_parent(self): |
Daniel Veillard | 01a6d41 | 2002-02-11 18:42:20 +0000 | [diff] [blame] | 192 | ret = libxml2mod.parent(self._o) |
| 193 | if ret == None: |
| 194 | return None |
| 195 | return xmlNode(_obj=ret) |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 196 | def get_children(self): |
Daniel Veillard | 01a6d41 | 2002-02-11 18:42:20 +0000 | [diff] [blame] | 197 | ret = libxml2mod.children(self._o) |
| 198 | if ret == None: |
| 199 | return None |
| 200 | return xmlNode(_obj=ret) |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 201 | def get_last(self): |
Daniel Veillard | 01a6d41 | 2002-02-11 18:42:20 +0000 | [diff] [blame] | 202 | ret = libxml2mod.last(self._o) |
| 203 | if ret == None: |
| 204 | return None |
| 205 | return xmlNode(_obj=ret) |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 206 | def get_next(self): |
Daniel Veillard | 01a6d41 | 2002-02-11 18:42:20 +0000 | [diff] [blame] | 207 | ret = libxml2mod.next(self._o) |
| 208 | if ret == None: |
| 209 | return None |
| 210 | return xmlNode(_obj=ret) |
Daniel Veillard | 1971ee2 | 2002-01-31 20:29:19 +0000 | [diff] [blame] | 211 | def get_properties(self): |
Daniel Veillard | 01a6d41 | 2002-02-11 18:42:20 +0000 | [diff] [blame] | 212 | ret = libxml2mod.properties(self._o) |
| 213 | if ret == None: |
| 214 | return None |
| 215 | return xmlAttr(_obj=ret) |
Daniel Veillard | 1971ee2 | 2002-01-31 20:29:19 +0000 | [diff] [blame] | 216 | def get_doc(self): |
Daniel Veillard | 01a6d41 | 2002-02-11 18:42:20 +0000 | [diff] [blame] | 217 | ret = libxml2mod.doc(self._o) |
| 218 | if ret == None: |
| 219 | return None |
| 220 | return xmlDoc(_obj=ret) |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 221 | def get_prev(self): |
Daniel Veillard | 01a6d41 | 2002-02-11 18:42:20 +0000 | [diff] [blame] | 222 | ret = libxml2mod.prev(self._o) |
| 223 | if ret == None: |
| 224 | return None |
| 225 | return xmlNode(_obj=ret) |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 226 | def get_content(self): |
Daniel Veillard | 01a6d41 | 2002-02-11 18:42:20 +0000 | [diff] [blame] | 227 | return libxml2mod.xmlNodeGetContent(self._o) |
Daniel Veillard | 1971ee2 | 2002-01-31 20:29:19 +0000 | [diff] [blame] | 228 | def getContent(self): |
Daniel Veillard | 01a6d41 | 2002-02-11 18:42:20 +0000 | [diff] [blame] | 229 | return libxml2mod.xmlNodeGetContent(self._o) |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 230 | def get_name(self): |
Daniel Veillard | 01a6d41 | 2002-02-11 18:42:20 +0000 | [diff] [blame] | 231 | return libxml2mod.name(self._o) |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 232 | def get_type(self): |
Daniel Veillard | 01a6d41 | 2002-02-11 18:42:20 +0000 | [diff] [blame] | 233 | return libxml2mod.type(self._o) |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 234 | def get_doc(self): |
Daniel Veillard | 01a6d41 | 2002-02-11 18:42:20 +0000 | [diff] [blame] | 235 | ret = libxml2mod.doc(self._o) |
| 236 | if ret == None: |
| 237 | return None |
| 238 | return xmlDoc(_doc=ret) |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 239 | def free(self): |
Daniel Veillard | 5e5c2d0 | 2002-02-09 18:03:01 +0000 | [diff] [blame] | 240 | libxml2mod.freeDoc(self._o) |
Daniel Veillard | 01a6d41 | 2002-02-11 18:42:20 +0000 | [diff] [blame] | 241 | |
Daniel Veillard | 36ed529 | 2002-01-30 23:49:06 +0000 | [diff] [blame] | 242 | # |
Daniel Veillard | 1971ee2 | 2002-01-31 20:29:19 +0000 | [diff] [blame] | 243 | # converters to present a nicer view of the XPath returns |
| 244 | # |
| 245 | def nodeWrap(o): |
| 246 | # TODO try to cast to the most appropriate node class |
Daniel Veillard | 5e5c2d0 | 2002-02-09 18:03:01 +0000 | [diff] [blame] | 247 | name = libxml2mod.name(o) |
Daniel Veillard | 1971ee2 | 2002-01-31 20:29:19 +0000 | [diff] [blame] | 248 | if name == "element" or name == "text": |
| 249 | return xmlNode(_obj=o) |
| 250 | if name == "attribute": |
| 251 | return xmlAttr(_obj=o) |
| 252 | if name[0:8] == "document": |
| 253 | return xmlDoc(_obj=o) |
| 254 | if name[0:8] == "namespace": |
| 255 | return xmlNs(_obj=o) |
| 256 | if name == "elem_decl": |
| 257 | return xmlElement(_obj=o) |
| 258 | if name == "attribute_decl": |
| 259 | return xmlAtribute(_obj=o) |
| 260 | if name == "entity_decl": |
| 261 | return xmlEntity(_obj=o) |
| 262 | if name == "dtd": |
| 263 | return xmlAttr(_obj=o) |
| 264 | return xmlNode(_obj=o) |
| 265 | |
| 266 | def xpathObjectRet(o): |
| 267 | if type(o) == type([]) or type(o) == type(()): |
| 268 | ret = map(lambda x: nodeWrap(x), o) |
Daniel Veillard | 01a6d41 | 2002-02-11 18:42:20 +0000 | [diff] [blame] | 269 | return ret |
Daniel Veillard | 1971ee2 | 2002-01-31 20:29:19 +0000 | [diff] [blame] | 270 | return o |
| 271 | |
| 272 | # |
Daniel Veillard | a7340c8 | 2002-02-01 17:56:45 +0000 | [diff] [blame] | 273 | # register an XPath function |
| 274 | # |
| 275 | def registerXPathFunction(ctxt, name, ns_uri, f): |
Daniel Veillard | 5e5c2d0 | 2002-02-09 18:03:01 +0000 | [diff] [blame] | 276 | ret = libxml2mod.xmlRegisterXPathFunction(ctxt, name, ns_uri, f) |
Daniel Veillard | a7340c8 | 2002-02-01 17:56:45 +0000 | [diff] [blame] | 277 | |
Daniel Veillard | 797a565 | 2002-02-12 13:46:21 +0000 | [diff] [blame] | 278 | |
Daniel Veillard | a7340c8 | 2002-02-01 17:56:45 +0000 | [diff] [blame] | 279 | # |
Daniel Veillard | 36ed529 | 2002-01-30 23:49:06 +0000 | [diff] [blame] | 280 | # Everything below this point is automatically generated |
| 281 | # |
Daniel Veillard | 1971ee2 | 2002-01-31 20:29:19 +0000 | [diff] [blame] | 282 | |