blob: c861a70cde4b7fefa57aae27a832ceab1bd08010 [file] [log] [blame]
Daniel Veillard5e5c2d02002-02-09 18:03:01 +00001import libxml2mod
Daniel Veillard85bb5b02003-12-04 14:12:05 +00002import types
Daniel Veillard3b6acc92006-12-14 15:49:41 +00003import sys
Daniel Veillardd2897fd2002-01-30 16:37:32 +00004
Daniel Veillarda81355e2004-09-28 11:08:27 +00005# The root of all libxml2 errors.
6class libxmlError(Exception): pass
7
Daniel Veillard1971ee22002-01-31 20:29:19 +00008#
Daniel Veillard3b6acc92006-12-14 15:49:41 +00009# id() is sometimes negative ...
10#
11def pos_id(o):
12 i = id(o)
13 if (i < 0):
14 return (sys.maxint - i)
15 return i
16
17#
Daniel Veillard8d24cc12002-03-05 15:41:29 +000018# Errors raised by the wrappers when some tree handling failed.
19#
Daniel Veillarda81355e2004-09-28 11:08:27 +000020class treeError(libxmlError):
Daniel Veillard8d24cc12002-03-05 15:41:29 +000021 def __init__(self, msg):
22 self.msg = msg
23 def __str__(self):
24 return self.msg
25
Daniel Veillarda81355e2004-09-28 11:08:27 +000026class parserError(libxmlError):
Daniel Veillard8d24cc12002-03-05 15:41:29 +000027 def __init__(self, msg):
28 self.msg = msg
29 def __str__(self):
30 return self.msg
31
Daniel Veillarda81355e2004-09-28 11:08:27 +000032class uriError(libxmlError):
Daniel Veillard8d24cc12002-03-05 15:41:29 +000033 def __init__(self, msg):
34 self.msg = msg
35 def __str__(self):
36 return self.msg
37
Daniel Veillarda81355e2004-09-28 11:08:27 +000038class xpathError(libxmlError):
Daniel Veillard8d24cc12002-03-05 15:41:29 +000039 def __init__(self, msg):
40 self.msg = msg
41 def __str__(self):
42 return self.msg
43
Daniel Veillardc6d4a932002-09-12 15:00:57 +000044class 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. Brack1d75c8a2003-10-27 13:48:16 +000051 return(-1)
52 self.__io.close()
53 self.__io = None
54 return(0)
Daniel Veillardc6d4a932002-09-12 15:00:57 +000055
56 def io_flush(self):
57 if self.__io == None:
William M. Brack1d75c8a2003-10-27 13:48:16 +000058 return(-1)
59 self.__io.flush()
60 return(0)
Daniel Veillardc6d4a932002-09-12 15:00:57 +000061
62 def io_read(self, len = -1):
63 if self.__io == None:
William M. Brack1d75c8a2003-10-27 13:48:16 +000064 return(-1)
Daniel Veillardc6d4a932002-09-12 15:00:57 +000065 if len < 0:
William M. Brack1d75c8a2003-10-27 13:48:16 +000066 return(self.__io.read())
67 return(self.__io.read(len))
Daniel Veillardc6d4a932002-09-12 15:00:57 +000068
69 def io_write(self, str, len = -1):
70 if self.__io == None:
William M. Brack1d75c8a2003-10-27 13:48:16 +000071 return(-1)
Daniel Veillardc6d4a932002-09-12 15:00:57 +000072 if len < 0:
William M. Brack1d75c8a2003-10-27 13:48:16 +000073 return(self.__io.write(str))
74 return(self.__io.write(str, len))
Daniel Veillardc6d4a932002-09-12 15:00:57 +000075
76class 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
94class ioWriteWrapper(ioWrapper):
95 def __init__(self, _obj, enc = ""):
Daniel Veillard85bb5b02003-12-04 14:12:05 +000096# print "ioWriteWrapper.__init__", _obj
97 if type(_obj) == type(''):
William M. Brack37e63942004-07-12 16:27:37 +000098 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 Veillardc6d4a932002-09-12 15:00:57 +0000111
112 def __del__(self):
Daniel Veillard85bb5b02003-12-04 14:12:05 +0000113# print "__del__"
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000114 self.io_close()
115 if self._o != None:
116 libxml2mod.xmlOutputBufferClose(self._o)
117 self._o = None
118
Daniel Veillard85bb5b02003-12-04 14:12:05 +0000119 def flush(self):
120 self.io_flush()
121 if self._o != None:
122 libxml2mod.xmlOutputBufferClose(self._o)
123 self._o = None
124
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000125 def close(self):
Daniel Veillard85bb5b02003-12-04 14:12:05 +0000126 self.io_flush()
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000127 if self._o != None:
128 libxml2mod.xmlOutputBufferClose(self._o)
129 self._o = None
130
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000131#
132# Example of a class to handle SAX events
133#
134class 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. Brack1d75c8a2003-10-27 13:48:16 +0000146 the element, attrs is a dictionary of the element's attributes"""
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000147 pass
148
149 def endElement(self, tag):
150 """called at the start of every element, tag is the name of
William M. Brack1d75c8a2003-10-27 13:48:16 +0000151 the element"""
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000152 pass
153
154 def characters(self, data):
155 """called when character data have been read, data is the string
William M. Brack1d75c8a2003-10-27 13:48:16 +0000156 containing the data, multiple consecutive characters() callback
157 are possible."""
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000158 pass
159
160 def cdataBlock(self, data):
161 """called when CDATA section have been read, data is the string
William M. Brack1d75c8a2003-10-27 13:48:16 +0000162 containing the data, multiple consecutive cdataBlock() callback
163 are possible."""
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000164 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. Brack1d75c8a2003-10-27 13:48:16 +0000176 data is the associated data in the PI"""
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000177 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. Brack1d75c8a2003-10-27 13:48:16 +0000185 DTD name and externalID, systemID are the DTD public and system
186 identifier for that DTd if available"""
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000187 pass
188
189 def internalSubset(self, name, externalID, systemID):
190 """called when a DOCTYPE declaration has been found, name is the
William M. Brack1d75c8a2003-10-27 13:48:16 +0000191 DTD name and externalID, systemID are the DTD public and system
192 identifier for that DTD if available"""
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000193 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. Brack1d75c8a2003-10-27 13:48:16 +0000197 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 Veillard8d24cc12002-03-05 15:41:29 +0000200 pass
201
202 def notationDecl(self, name, externalID, systemID):
203 """called when an NOTATION declaration has been found, name is the
William M. Brack1d75c8a2003-10-27 13:48:16 +0000204 notation name and externalID, systemID are the notation public and
205 system identifier for that notation if available"""
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000206 pass
207
208 def attributeDecl(self, elem, name, type, defi, defaultValue, nameList):
209 """called when an ATTRIBUTE definition has been found"""
William M. Brack1d75c8a2003-10-27 13:48:16 +0000210 pass
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000211
212 def elementDecl(self, name, type, content):
213 """called when an ELEMENT definition has been found"""
William M. Brack1d75c8a2003-10-27 13:48:16 +0000214 pass
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000215
216 def entityDecl(self, name, publicId, systemID, notationName):
217 """called when an unparsed ENTITY declaration has been found,
William M. Brack1d75c8a2003-10-27 13:48:16 +0000218 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 Veillard8d24cc12002-03-05 15:41:29 +0000221 pass
222
223 def warning(self, msg):
Daniel Veillardeaccdc62005-10-27 14:10:52 +0000224 #print msg
Daniel Veillard745648b2006-02-27 09:59:30 +0000225 pass
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000226
227 def error(self, msg):
228 raise parserError(msg)
229
230 def fatalError(self, msg):
231 raise parserError(msg)
232
233#
Daniel Veillard1971ee22002-01-31 20:29:19 +0000234# 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 Veillard1e774382002-03-06 17:35:40 +0000237# doc reference, content access and serializing to a string or URI
Daniel Veillard1971ee22002-01-31 20:29:19 +0000238#
Daniel Veillard36ed5292002-01-30 23:49:06 +0000239class xmlCore:
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000240 def __init__(self, _obj=None):
241 if _obj != None:
Daniel Veillard01a6d412002-02-11 18:42:20 +0000242 self._o = _obj;
243 return
244 self._o = None
William M. Brack40cca612006-06-26 18:25:40 +0000245
246 def __eq__(self, other):
247 if other == None:
William M. Brack7a12e572007-02-16 17:11:09 +0000248 return False
William M. Brack40cca612006-06-26 18:25:40 +0000249 ret = libxml2mod.compareNodesEqual(self._o, other._o)
William M. Brack7a12e572007-02-16 17:11:09 +0000250 if ret == None:
251 return False
252 return ret == True
William M. Brack40cca612006-06-26 18:25:40 +0000253 def __ne__(self, other):
254 if other == None:
William M. Brack7a12e572007-02-16 17:11:09 +0000255 return True
William M. Brack40cca612006-06-26 18:25:40 +0000256 ret = libxml2mod.compareNodesEqual(self._o, other._o)
William M. Brack7a12e572007-02-16 17:11:09 +0000257 return not ret
William M. Brack40cca612006-06-26 18:25:40 +0000258 def __hash__(self):
William M. Brack7a12e572007-02-16 17:11:09 +0000259 ret = libxml2mod.nodeHash(self._o)
260 return ret
William M. Brack40cca612006-06-26 18:25:40 +0000261
Daniel Veillard1cd4dae2005-01-15 17:45:28 +0000262 def __str__(self):
263 return self.serialize()
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000264 def get_parent(self):
Daniel Veillard01a6d412002-02-11 18:42:20 +0000265 ret = libxml2mod.parent(self._o)
266 if ret == None:
267 return None
268 return xmlNode(_obj=ret)
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000269 def get_children(self):
Daniel Veillard01a6d412002-02-11 18:42:20 +0000270 ret = libxml2mod.children(self._o)
271 if ret == None:
272 return None
273 return xmlNode(_obj=ret)
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000274 def get_last(self):
Daniel Veillard01a6d412002-02-11 18:42:20 +0000275 ret = libxml2mod.last(self._o)
276 if ret == None:
277 return None
278 return xmlNode(_obj=ret)
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000279 def get_next(self):
Daniel Veillard01a6d412002-02-11 18:42:20 +0000280 ret = libxml2mod.next(self._o)
281 if ret == None:
282 return None
283 return xmlNode(_obj=ret)
Daniel Veillard1971ee22002-01-31 20:29:19 +0000284 def get_properties(self):
Daniel Veillard01a6d412002-02-11 18:42:20 +0000285 ret = libxml2mod.properties(self._o)
286 if ret == None:
287 return None
288 return xmlAttr(_obj=ret)
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000289 def get_prev(self):
Daniel Veillard01a6d412002-02-11 18:42:20 +0000290 ret = libxml2mod.prev(self._o)
291 if ret == None:
292 return None
293 return xmlNode(_obj=ret)
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000294 def get_content(self):
Daniel Veillard01a6d412002-02-11 18:42:20 +0000295 return libxml2mod.xmlNodeGetContent(self._o)
Daniel Veillard51a447a2003-01-04 19:42:46 +0000296 getContent = get_content # why is this duplicate naming needed ?
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000297 def get_name(self):
Daniel Veillard01a6d412002-02-11 18:42:20 +0000298 return libxml2mod.name(self._o)
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000299 def get_type(self):
Daniel Veillard01a6d412002-02-11 18:42:20 +0000300 return libxml2mod.type(self._o)
Daniel Veillard51a447a2003-01-04 19:42:46 +0000301 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. Brack1d75c8a2003-10-27 13:48:16 +0000315 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 Veillard51a447a2003-01-04 19:42:46 +0000361 else:
William M. Brack1d75c8a2003-10-27 13:48:16 +0000362 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 Veillard1e774382002-03-06 17:35:40 +0000372
373 #
374 # Serialization routines, the optional arguments have the following
375 # meaning:
376 # encoding: string to ask saving in a specific encoding
Daniel Veillard51a447a2003-01-04 19:42:46 +0000377 # indent: if 1 the serializer is asked to indent the output
Daniel Veillard1e774382002-03-06 17:35:40 +0000378 #
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 Veillard01a6d412002-02-11 18:42:20 +0000383
Daniel Veillardf742d342002-03-07 00:05:35 +0000384 #
Daniel Veillardd5e198a2004-03-09 09:03:28 +0000385 # 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 Veillardf742d342002-03-07 00:05:35 +0000427 # 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. Brack1d75c8a2003-10-27 13:48:16 +0000431 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 Veillard51a447a2003-01-04 19:42:46 +0000439
Daniel Veillardf88d8cf2003-12-08 10:25:02 +0000440# #
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 Veillard51a447a2003-01-04 19:42:46 +0000457 def xpathEval2(self, expr):
Daniel Veillardf88d8cf2003-12-08 10:25:02 +0000458 return self.xpathEval(expr)
Daniel Veillard51a447a2003-01-04 19:42:46 +0000459
Daniel Veillardf9cf6f52005-04-12 01:02:29 +0000460 # 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
Leonid Evdokimov147687f2009-08-24 14:08:43 +0200469 renconciliateNs() to fix those pointers.
Daniel Veillardf9cf6f52005-04-12 01:02:29 +0000470
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 Veillard51a447a2003-01-04 19:42:46 +0000481 # 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 Veillardf88d8cf2003-12-08 10:25:02 +0000493 libxml2mod.xmlFreeDoc(self._o)
Daniel Veillard51a447a2003-01-04 19:42:46 +0000494
495
496#
497# implements the depth-first iterator for libxml2 DOM tree
498#
499class 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#
521class 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 Veillard36ed5292002-01-30 23:49:06 +0000540#
Daniel Veillard1971ee22002-01-31 20:29:19 +0000541# converters to present a nicer view of the XPath returns
542#
543def nodeWrap(o):
544 # TODO try to cast to the most appropriate node class
Daniel Veillard1f8658a2004-08-14 21:46:31 +0000545 name = libxml2mod.type(o)
Daniel Veillard1971ee22002-01-31 20:29:19 +0000546 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 Veillard1f8658a2004-08-14 21:46:31 +0000552 if name == "namespace":
Daniel Veillard1971ee22002-01-31 20:29:19 +0000553 return xmlNs(_obj=o)
554 if name == "elem_decl":
555 return xmlElement(_obj=o)
556 if name == "attribute_decl":
Daniel Veillard1f8658a2004-08-14 21:46:31 +0000557 return xmlAttribute(_obj=o)
Daniel Veillard1971ee22002-01-31 20:29:19 +0000558 if name == "entity_decl":
559 return xmlEntity(_obj=o)
560 if name == "dtd":
Daniel Veillarde59494f2003-01-04 16:35:29 +0000561 return xmlDtd(_obj=o)
Daniel Veillard1971ee22002-01-31 20:29:19 +0000562 return xmlNode(_obj=o)
563
564def xpathObjectRet(o):
Daniel Veillard46459062006-10-10 08:40:04 +0000565 otype = type(o)
566 if otype == type([]):
567 ret = map(xpathObjectRet, o)
Daniel Veillard01a6d412002-02-11 18:42:20 +0000568 return ret
Daniel Veillard46459062006-10-10 08:40:04 +0000569 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 Veillard1971ee22002-01-31 20:29:19 +0000576
577#
Daniel Veillarda7340c82002-02-01 17:56:45 +0000578# register an XPath function
579#
580def registerXPathFunction(ctxt, name, ns_uri, f):
Daniel Veillard5e5c2d02002-02-09 18:03:01 +0000581 ret = libxml2mod.xmlRegisterXPathFunction(ctxt, name, ns_uri, f)
Daniel Veillarda7340c82002-02-01 17:56:45 +0000582
583#
Daniel Veillardf25b4ca2002-12-27 15:18:35 +0000584# For the xmlTextReader parser configuration
585#
586PARSER_LOADDTD=1
587PARSER_DEFAULTATTRS=2
588PARSER_VALIDATE=3
Daniel Veillarde18fc182002-12-28 22:56:33 +0000589PARSER_SUBST_ENTITIES=4
Daniel Veillardf25b4ca2002-12-27 15:18:35 +0000590
591#
Daniel Veillard417be3a2003-01-20 21:26:34 +0000592# For the error callback severities
Daniel Veillard26f70262003-01-16 22:45:08 +0000593#
Daniel Veillard417be3a2003-01-20 21:26:34 +0000594PARSER_SEVERITY_VALIDITY_WARNING=1
595PARSER_SEVERITY_VALIDITY_ERROR=2
596PARSER_SEVERITY_WARNING=3
597PARSER_SEVERITY_ERROR=4
Daniel Veillard26f70262003-01-16 22:45:08 +0000598
599#
Daniel Veillard3e20a292003-01-10 13:14:40 +0000600# register the libxml2 error handler
Daniel Veillard36ed5292002-01-30 23:49:06 +0000601#
Daniel Veillard3e20a292003-01-10 13:14:40 +0000602def 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 Veillarde6227e02003-01-14 11:42:39 +0000616class 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. Brack1d75c8a2003-10-27 13:48:16 +0000627 self._o = None
Daniel Veillarde6227e02003-01-14 11:42:39 +0000628
Daniel Veillard417be3a2003-01-20 21:26:34 +0000629 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 Veillarde6227e02003-01-14 11:42:39 +0000635
Daniel Veillard417be3a2003-01-20 21:26:34 +0000636 def getErrorHandler(self):
637 """Return (f,arg) as previously registered with setErrorHandler
638 or (None,None)."""
639 return libxml2mod.xmlParserCtxtGetErrorHandler(self._o)
640
Daniel Veillard54396242003-04-23 07:36:50 +0000641 def addLocalCatalog(self, uri):
642 """Register a local catalog with the parser"""
643 return libxml2mod.addLocalCatalog(self._o, uri)
644
645
Daniel Veillard0e460da2005-03-30 22:47:10 +0000646class 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
659class 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
672class 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 Veillard417be3a2003-01-20 21:26:34 +0000685def _xmlTextReaderErrorFunc((f,arg),msg,severity,locator):
686 """Intermediate callback to wrap the locator"""
687 return f(arg,msg,severity,xmlTextReaderLocator(locator))
Daniel Veillarde6227e02003-01-14 11:42:39 +0000688
Daniel Veillard26f70262003-01-16 22:45:08 +0000689class 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 Veillard417be3a2003-01-20 21:26:34 +0000701 def SetErrorHandler(self,f,arg):
Daniel Veillard26f70262003-01-16 22:45:08 +0000702 """Register an error handler that will be called back as
Daniel Veillard417be3a2003-01-20 21:26:34 +0000703 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 Veillard26f70262003-01-16 22:45:08 +0000710
Daniel Veillard417be3a2003-01-20 21:26:34 +0000711 def GetErrorHandler(self):
Daniel Veillard26f70262003-01-16 22:45:08 +0000712 """Return (f,arg) as previously registered with setErrorHandler
713 or (None,None)."""
Daniel Veillard417be3a2003-01-20 21:26:34 +0000714 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 Veillard26f70262003-01-16 22:45:08 +0000720
Daniel Veillardf93a8662004-07-01 12:56:30 +0000721#
722# The cleanup now goes though a wrappe in libxml.c
723#
724def cleanupParser():
725 libxml2mod.xmlPythonCleanupParser()
Daniel Veillard87ab1c12003-12-21 13:01:56 +0000726
Daniel Veillard3e20a292003-01-10 13:14:40 +0000727# 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 Veillard1971ee22002-01-31 20:29:19 +0000733