blob: 0c2a19ad21103a7ebeb06a534ec9f8d3ec967033 [file] [log] [blame]
Daniel Veillard5e5c2d02002-02-09 18:03:01 +00001import libxml2mod
Daniel Veillard85bb5b02003-12-04 14:12:05 +00002import types
Daniel Veillardd2897fd2002-01-30 16:37:32 +00003
Daniel Veillarda81355e2004-09-28 11:08:27 +00004# The root of all libxml2 errors.
5class libxmlError(Exception): pass
6
Daniel Veillard1971ee22002-01-31 20:29:19 +00007#
Daniel Veillard8d24cc12002-03-05 15:41:29 +00008# Errors raised by the wrappers when some tree handling failed.
9#
Daniel Veillarda81355e2004-09-28 11:08:27 +000010class treeError(libxmlError):
Daniel Veillard8d24cc12002-03-05 15:41:29 +000011 def __init__(self, msg):
12 self.msg = msg
13 def __str__(self):
14 return self.msg
15
Daniel Veillarda81355e2004-09-28 11:08:27 +000016class parserError(libxmlError):
Daniel Veillard8d24cc12002-03-05 15:41:29 +000017 def __init__(self, msg):
18 self.msg = msg
19 def __str__(self):
20 return self.msg
21
Daniel Veillarda81355e2004-09-28 11:08:27 +000022class uriError(libxmlError):
Daniel Veillard8d24cc12002-03-05 15:41:29 +000023 def __init__(self, msg):
24 self.msg = msg
25 def __str__(self):
26 return self.msg
27
Daniel Veillarda81355e2004-09-28 11:08:27 +000028class xpathError(libxmlError):
Daniel Veillard8d24cc12002-03-05 15:41:29 +000029 def __init__(self, msg):
30 self.msg = msg
31 def __str__(self):
32 return self.msg
33
Daniel Veillardc6d4a932002-09-12 15:00:57 +000034class ioWrapper:
35 def __init__(self, _obj):
36 self.__io = _obj
37 self._o = None
38
39 def io_close(self):
40 if self.__io == None:
William M. Brack1d75c8a2003-10-27 13:48:16 +000041 return(-1)
42 self.__io.close()
43 self.__io = None
44 return(0)
Daniel Veillardc6d4a932002-09-12 15:00:57 +000045
46 def io_flush(self):
47 if self.__io == None:
William M. Brack1d75c8a2003-10-27 13:48:16 +000048 return(-1)
49 self.__io.flush()
50 return(0)
Daniel Veillardc6d4a932002-09-12 15:00:57 +000051
52 def io_read(self, len = -1):
53 if self.__io == None:
William M. Brack1d75c8a2003-10-27 13:48:16 +000054 return(-1)
Daniel Veillardc6d4a932002-09-12 15:00:57 +000055 if len < 0:
William M. Brack1d75c8a2003-10-27 13:48:16 +000056 return(self.__io.read())
57 return(self.__io.read(len))
Daniel Veillardc6d4a932002-09-12 15:00:57 +000058
59 def io_write(self, str, len = -1):
60 if self.__io == None:
William M. Brack1d75c8a2003-10-27 13:48:16 +000061 return(-1)
Daniel Veillardc6d4a932002-09-12 15:00:57 +000062 if len < 0:
William M. Brack1d75c8a2003-10-27 13:48:16 +000063 return(self.__io.write(str))
64 return(self.__io.write(str, len))
Daniel Veillardc6d4a932002-09-12 15:00:57 +000065
66class ioReadWrapper(ioWrapper):
67 def __init__(self, _obj, enc = ""):
68 ioWrapper.__init__(self, _obj)
69 self._o = libxml2mod.xmlCreateInputBuffer(self, enc)
70
71 def __del__(self):
72 print "__del__"
73 self.io_close()
74 if self._o != None:
75 libxml2mod.xmlFreeParserInputBuffer(self._o)
76 self._o = None
77
78 def close(self):
79 self.io_close()
80 if self._o != None:
81 libxml2mod.xmlFreeParserInputBuffer(self._o)
82 self._o = None
83
84class ioWriteWrapper(ioWrapper):
85 def __init__(self, _obj, enc = ""):
Daniel Veillard85bb5b02003-12-04 14:12:05 +000086# print "ioWriteWrapper.__init__", _obj
87 if type(_obj) == type(''):
William M. Brack37e63942004-07-12 16:27:37 +000088 print "write io from a string"
89 self.o = None
90 elif type(_obj) == types.InstanceType:
91 print "write io from instance of %s" % (_obj.__class__)
92 ioWrapper.__init__(self, _obj)
93 self._o = libxml2mod.xmlCreateOutputBuffer(self, enc)
94 else:
95 file = libxml2mod.outputBufferGetPythonFile(_obj)
96 if file != None:
97 ioWrapper.__init__(self, file)
98 else:
99 ioWrapper.__init__(self, _obj)
100 self._o = _obj
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000101
102 def __del__(self):
Daniel Veillard85bb5b02003-12-04 14:12:05 +0000103# print "__del__"
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000104 self.io_close()
105 if self._o != None:
106 libxml2mod.xmlOutputBufferClose(self._o)
107 self._o = None
108
Daniel Veillard85bb5b02003-12-04 14:12:05 +0000109 def flush(self):
110 self.io_flush()
111 if self._o != None:
112 libxml2mod.xmlOutputBufferClose(self._o)
113 self._o = None
114
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000115 def close(self):
Daniel Veillard85bb5b02003-12-04 14:12:05 +0000116 self.io_flush()
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000117 if self._o != None:
118 libxml2mod.xmlOutputBufferClose(self._o)
119 self._o = None
120
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000121#
122# Example of a class to handle SAX events
123#
124class SAXCallback:
125 """Base class for SAX handlers"""
126 def startDocument(self):
127 """called at the start of the document"""
128 pass
129
130 def endDocument(self):
131 """called at the end of the document"""
132 pass
133
134 def startElement(self, tag, attrs):
135 """called at the start of every element, tag is the name of
William M. Brack1d75c8a2003-10-27 13:48:16 +0000136 the element, attrs is a dictionary of the element's attributes"""
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000137 pass
138
139 def endElement(self, tag):
140 """called at the start of every element, tag is the name of
William M. Brack1d75c8a2003-10-27 13:48:16 +0000141 the element"""
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000142 pass
143
144 def characters(self, data):
145 """called when character data have been read, data is the string
William M. Brack1d75c8a2003-10-27 13:48:16 +0000146 containing the data, multiple consecutive characters() callback
147 are possible."""
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000148 pass
149
150 def cdataBlock(self, data):
151 """called when CDATA section have been read, data is the string
William M. Brack1d75c8a2003-10-27 13:48:16 +0000152 containing the data, multiple consecutive cdataBlock() callback
153 are possible."""
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000154 pass
155
156 def reference(self, name):
157 """called when an entity reference has been found"""
158 pass
159
160 def ignorableWhitespace(self, data):
161 """called when potentially ignorable white spaces have been found"""
162 pass
163
164 def processingInstruction(self, target, data):
165 """called when a PI has been found, target contains the PI name and
William M. Brack1d75c8a2003-10-27 13:48:16 +0000166 data is the associated data in the PI"""
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000167 pass
168
169 def comment(self, content):
170 """called when a comment has been found, content contains the comment"""
171 pass
172
173 def externalSubset(self, name, externalID, systemID):
174 """called when a DOCTYPE declaration has been found, name is the
William M. Brack1d75c8a2003-10-27 13:48:16 +0000175 DTD name and externalID, systemID are the DTD public and system
176 identifier for that DTd if available"""
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000177 pass
178
179 def internalSubset(self, name, externalID, systemID):
180 """called when a DOCTYPE declaration has been found, name is the
William M. Brack1d75c8a2003-10-27 13:48:16 +0000181 DTD name and externalID, systemID are the DTD public and system
182 identifier for that DTD if available"""
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000183 pass
184
185 def entityDecl(self, name, type, externalID, systemID, content):
186 """called when an ENTITY declaration has been found, name is the
William M. Brack1d75c8a2003-10-27 13:48:16 +0000187 entity name and externalID, systemID are the entity public and
188 system identifier for that entity if available, type indicates
189 the entity type, and content reports it's string content"""
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000190 pass
191
192 def notationDecl(self, name, externalID, systemID):
193 """called when an NOTATION declaration has been found, name is the
William M. Brack1d75c8a2003-10-27 13:48:16 +0000194 notation name and externalID, systemID are the notation public and
195 system identifier for that notation if available"""
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000196 pass
197
198 def attributeDecl(self, elem, name, type, defi, defaultValue, nameList):
199 """called when an ATTRIBUTE definition has been found"""
William M. Brack1d75c8a2003-10-27 13:48:16 +0000200 pass
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000201
202 def elementDecl(self, name, type, content):
203 """called when an ELEMENT definition has been found"""
William M. Brack1d75c8a2003-10-27 13:48:16 +0000204 pass
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000205
206 def entityDecl(self, name, publicId, systemID, notationName):
207 """called when an unparsed ENTITY declaration has been found,
William M. Brack1d75c8a2003-10-27 13:48:16 +0000208 name is the entity name and publicId,, systemID are the entity
209 public and system identifier for that entity if available,
210 and notationName indicate the associated NOTATION"""
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000211 pass
212
213 def warning(self, msg):
Daniel Veillardeaccdc62005-10-27 14:10:52 +0000214 #print msg
215 pass
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000216
217 def error(self, msg):
218 raise parserError(msg)
219
220 def fatalError(self, msg):
221 raise parserError(msg)
222
223#
Daniel Veillard1971ee22002-01-31 20:29:19 +0000224# This class is the ancestor of all the Node classes. It provides
225# the basic functionalities shared by all nodes (and handle
226# gracefylly the exception), like name, navigation in the tree,
Daniel Veillard1e774382002-03-06 17:35:40 +0000227# doc reference, content access and serializing to a string or URI
Daniel Veillard1971ee22002-01-31 20:29:19 +0000228#
Daniel Veillard36ed5292002-01-30 23:49:06 +0000229class xmlCore:
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000230 def __init__(self, _obj=None):
231 if _obj != None:
Daniel Veillard01a6d412002-02-11 18:42:20 +0000232 self._o = _obj;
233 return
234 self._o = None
Daniel Veillard1cd4dae2005-01-15 17:45:28 +0000235 def __str__(self):
236 return self.serialize()
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000237 def get_parent(self):
Daniel Veillard01a6d412002-02-11 18:42:20 +0000238 ret = libxml2mod.parent(self._o)
239 if ret == None:
240 return None
241 return xmlNode(_obj=ret)
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000242 def get_children(self):
Daniel Veillard01a6d412002-02-11 18:42:20 +0000243 ret = libxml2mod.children(self._o)
244 if ret == None:
245 return None
246 return xmlNode(_obj=ret)
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000247 def get_last(self):
Daniel Veillard01a6d412002-02-11 18:42:20 +0000248 ret = libxml2mod.last(self._o)
249 if ret == None:
250 return None
251 return xmlNode(_obj=ret)
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000252 def get_next(self):
Daniel Veillard01a6d412002-02-11 18:42:20 +0000253 ret = libxml2mod.next(self._o)
254 if ret == None:
255 return None
256 return xmlNode(_obj=ret)
Daniel Veillard1971ee22002-01-31 20:29:19 +0000257 def get_properties(self):
Daniel Veillard01a6d412002-02-11 18:42:20 +0000258 ret = libxml2mod.properties(self._o)
259 if ret == None:
260 return None
261 return xmlAttr(_obj=ret)
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000262 def get_prev(self):
Daniel Veillard01a6d412002-02-11 18:42:20 +0000263 ret = libxml2mod.prev(self._o)
264 if ret == None:
265 return None
266 return xmlNode(_obj=ret)
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000267 def get_content(self):
Daniel Veillard01a6d412002-02-11 18:42:20 +0000268 return libxml2mod.xmlNodeGetContent(self._o)
Daniel Veillard51a447a2003-01-04 19:42:46 +0000269 getContent = get_content # why is this duplicate naming needed ?
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000270 def get_name(self):
Daniel Veillard01a6d412002-02-11 18:42:20 +0000271 return libxml2mod.name(self._o)
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000272 def get_type(self):
Daniel Veillard01a6d412002-02-11 18:42:20 +0000273 return libxml2mod.type(self._o)
Daniel Veillard51a447a2003-01-04 19:42:46 +0000274 def get_doc(self):
275 ret = libxml2mod.doc(self._o)
276 if ret == None:
277 if self.type in ["document_xml", "document_html"]:
278 return xmlDoc(_obj=self._o)
279 else:
280 return None
281 return xmlDoc(_obj=ret)
282 #
283 # Those are common attributes to nearly all type of nodes
284 # defined as python2 properties
285 #
286 import sys
287 if float(sys.version[0:3]) < 2.2:
William M. Brack1d75c8a2003-10-27 13:48:16 +0000288 def __getattr__(self, attr):
289 if attr == "parent":
290 ret = libxml2mod.parent(self._o)
291 if ret == None:
292 return None
293 return xmlNode(_obj=ret)
294 elif attr == "properties":
295 ret = libxml2mod.properties(self._o)
296 if ret == None:
297 return None
298 return xmlAttr(_obj=ret)
299 elif attr == "children":
300 ret = libxml2mod.children(self._o)
301 if ret == None:
302 return None
303 return xmlNode(_obj=ret)
304 elif attr == "last":
305 ret = libxml2mod.last(self._o)
306 if ret == None:
307 return None
308 return xmlNode(_obj=ret)
309 elif attr == "next":
310 ret = libxml2mod.next(self._o)
311 if ret == None:
312 return None
313 return xmlNode(_obj=ret)
314 elif attr == "prev":
315 ret = libxml2mod.prev(self._o)
316 if ret == None:
317 return None
318 return xmlNode(_obj=ret)
319 elif attr == "content":
320 return libxml2mod.xmlNodeGetContent(self._o)
321 elif attr == "name":
322 return libxml2mod.name(self._o)
323 elif attr == "type":
324 return libxml2mod.type(self._o)
325 elif attr == "doc":
326 ret = libxml2mod.doc(self._o)
327 if ret == None:
328 if self.type == "document_xml" or self.type == "document_html":
329 return xmlDoc(_obj=self._o)
330 else:
331 return None
332 return xmlDoc(_obj=ret)
333 raise AttributeError,attr
Daniel Veillard51a447a2003-01-04 19:42:46 +0000334 else:
William M. Brack1d75c8a2003-10-27 13:48:16 +0000335 parent = property(get_parent, None, None, "Parent node")
336 children = property(get_children, None, None, "First child node")
337 last = property(get_last, None, None, "Last sibling node")
338 next = property(get_next, None, None, "Next sibling node")
339 prev = property(get_prev, None, None, "Previous sibling node")
340 properties = property(get_properties, None, None, "List of properies")
341 content = property(get_content, None, None, "Content of this node")
342 name = property(get_name, None, None, "Node name")
343 type = property(get_type, None, None, "Node type")
344 doc = property(get_doc, None, None, "The document this node belongs to")
Daniel Veillard1e774382002-03-06 17:35:40 +0000345
346 #
347 # Serialization routines, the optional arguments have the following
348 # meaning:
349 # encoding: string to ask saving in a specific encoding
Daniel Veillard51a447a2003-01-04 19:42:46 +0000350 # indent: if 1 the serializer is asked to indent the output
Daniel Veillard1e774382002-03-06 17:35:40 +0000351 #
352 def serialize(self, encoding = None, format = 0):
353 return libxml2mod.serializeNode(self._o, encoding, format)
354 def saveTo(self, file, encoding = None, format = 0):
355 return libxml2mod.saveNodeTo(self._o, file, encoding, format)
Daniel Veillard01a6d412002-02-11 18:42:20 +0000356
Daniel Veillardf742d342002-03-07 00:05:35 +0000357 #
Daniel Veillardd5e198a2004-03-09 09:03:28 +0000358 # Canonicalization routines:
359 #
360 # nodes: the node set (tuple or list) to be included in the
361 # canonized image or None if all document nodes should be
362 # included.
363 # exclusive: the exclusive flag (0 - non-exclusive
364 # canonicalization; otherwise - exclusive canonicalization)
365 # prefixes: the list of inclusive namespace prefixes (strings),
366 # or None if there is no inclusive namespaces (only for
367 # exclusive canonicalization, ignored otherwise)
368 # with_comments: include comments in the result (!=0) or not
369 # (==0)
370 def c14nMemory(self,
371 nodes=None,
372 exclusive=0,
373 prefixes=None,
374 with_comments=0):
375 if nodes:
376 nodes = map(lambda n: n._o, nodes)
377 return libxml2mod.xmlC14NDocDumpMemory(
378 self.get_doc()._o,
379 nodes,
380 exclusive != 0,
381 prefixes,
382 with_comments != 0)
383 def c14nSaveTo(self,
384 file,
385 nodes=None,
386 exclusive=0,
387 prefixes=None,
388 with_comments=0):
389 if nodes:
390 nodes = map(lambda n: n._o, nodes)
391 return libxml2mod.xmlC14NDocSaveTo(
392 self.get_doc()._o,
393 nodes,
394 exclusive != 0,
395 prefixes,
396 with_comments != 0,
397 file)
398
399 #
Daniel Veillardf742d342002-03-07 00:05:35 +0000400 # Selecting nodes using XPath, a bit slow because the context
401 # is allocated/freed every time but convenient.
402 #
403 def xpathEval(self, expr):
William M. Brack1d75c8a2003-10-27 13:48:16 +0000404 doc = self.doc
405 if doc == None:
406 return None
407 ctxt = doc.xpathNewContext()
408 ctxt.setContextNode(self)
409 res = ctxt.xpathEval(expr)
410 ctxt.xpathFreeContext()
411 return res
Daniel Veillard51a447a2003-01-04 19:42:46 +0000412
Daniel Veillardf88d8cf2003-12-08 10:25:02 +0000413# #
414# # Selecting nodes using XPath, faster because the context
415# # is allocated just once per xmlDoc.
416# #
417# # Removed: DV memleaks c.f. #126735
418# #
419# def xpathEval2(self, expr):
420# doc = self.doc
421# if doc == None:
422# return None
423# try:
424# doc._ctxt.setContextNode(self)
425# except:
426# doc._ctxt = doc.xpathNewContext()
427# doc._ctxt.setContextNode(self)
428# res = doc._ctxt.xpathEval(expr)
429# return res
Daniel Veillard51a447a2003-01-04 19:42:46 +0000430 def xpathEval2(self, expr):
Daniel Veillardf88d8cf2003-12-08 10:25:02 +0000431 return self.xpathEval(expr)
Daniel Veillard51a447a2003-01-04 19:42:46 +0000432
Daniel Veillardf9cf6f52005-04-12 01:02:29 +0000433 # Remove namespaces
434 def removeNsDef(self, href):
435 """
436 Remove a namespace definition from a node. If href is None,
437 remove all of the ns definitions on that node. The removed
438 namespaces are returned as a linked list.
439
440 Note: If any child nodes referred to the removed namespaces,
441 they will be left with dangling links. You should call
442 renciliateNs() to fix those pointers.
443
444 Note: This method does not free memory taken by the ns
445 definitions. You will need to free it manually with the
446 freeNsList() method on the returns xmlNs object.
447 """
448
449 ret = libxml2mod.xmlNodeRemoveNsDef(self._o, href)
450 if ret is None:return None
451 __tmp = xmlNs(_obj=ret)
452 return __tmp
453
Daniel Veillard51a447a2003-01-04 19:42:46 +0000454 # support for python2 iterators
455 def walk_depth_first(self):
456 return xmlCoreDepthFirstItertor(self)
457 def walk_breadth_first(self):
458 return xmlCoreBreadthFirstItertor(self)
459 __iter__ = walk_depth_first
460
461 def free(self):
462 try:
463 self.doc._ctxt.xpathFreeContext()
464 except:
465 pass
Daniel Veillardf88d8cf2003-12-08 10:25:02 +0000466 libxml2mod.xmlFreeDoc(self._o)
Daniel Veillard51a447a2003-01-04 19:42:46 +0000467
468
469#
470# implements the depth-first iterator for libxml2 DOM tree
471#
472class xmlCoreDepthFirstItertor:
473 def __init__(self, node):
474 self.node = node
475 self.parents = []
476 def __iter__(self):
477 return self
478 def next(self):
479 while 1:
480 if self.node:
481 ret = self.node
482 self.parents.append(self.node)
483 self.node = self.node.children
484 return ret
485 try:
486 parent = self.parents.pop()
487 except IndexError:
488 raise StopIteration
489 self.node = parent.next
490
491#
492# implements the breadth-first iterator for libxml2 DOM tree
493#
494class xmlCoreBreadthFirstItertor:
495 def __init__(self, node):
496 self.node = node
497 self.parents = []
498 def __iter__(self):
499 return self
500 def next(self):
501 while 1:
502 if self.node:
503 ret = self.node
504 self.parents.append(self.node)
505 self.node = self.node.next
506 return ret
507 try:
508 parent = self.parents.pop()
509 except IndexError:
510 raise StopIteration
511 self.node = parent.children
512
Daniel Veillard36ed5292002-01-30 23:49:06 +0000513#
Daniel Veillard1971ee22002-01-31 20:29:19 +0000514# converters to present a nicer view of the XPath returns
515#
516def nodeWrap(o):
517 # TODO try to cast to the most appropriate node class
Daniel Veillard1f8658a2004-08-14 21:46:31 +0000518 name = libxml2mod.type(o)
Daniel Veillard1971ee22002-01-31 20:29:19 +0000519 if name == "element" or name == "text":
520 return xmlNode(_obj=o)
521 if name == "attribute":
522 return xmlAttr(_obj=o)
523 if name[0:8] == "document":
524 return xmlDoc(_obj=o)
Daniel Veillard1f8658a2004-08-14 21:46:31 +0000525 if name == "namespace":
Daniel Veillard1971ee22002-01-31 20:29:19 +0000526 return xmlNs(_obj=o)
527 if name == "elem_decl":
528 return xmlElement(_obj=o)
529 if name == "attribute_decl":
Daniel Veillard1f8658a2004-08-14 21:46:31 +0000530 return xmlAttribute(_obj=o)
Daniel Veillard1971ee22002-01-31 20:29:19 +0000531 if name == "entity_decl":
532 return xmlEntity(_obj=o)
533 if name == "dtd":
Daniel Veillarde59494f2003-01-04 16:35:29 +0000534 return xmlDtd(_obj=o)
Daniel Veillard1971ee22002-01-31 20:29:19 +0000535 return xmlNode(_obj=o)
536
537def xpathObjectRet(o):
538 if type(o) == type([]) or type(o) == type(()):
539 ret = map(lambda x: nodeWrap(x), o)
Daniel Veillard01a6d412002-02-11 18:42:20 +0000540 return ret
Daniel Veillard1971ee22002-01-31 20:29:19 +0000541 return o
542
543#
Daniel Veillarda7340c82002-02-01 17:56:45 +0000544# register an XPath function
545#
546def registerXPathFunction(ctxt, name, ns_uri, f):
Daniel Veillard5e5c2d02002-02-09 18:03:01 +0000547 ret = libxml2mod.xmlRegisterXPathFunction(ctxt, name, ns_uri, f)
Daniel Veillarda7340c82002-02-01 17:56:45 +0000548
549#
Daniel Veillardf25b4ca2002-12-27 15:18:35 +0000550# For the xmlTextReader parser configuration
551#
552PARSER_LOADDTD=1
553PARSER_DEFAULTATTRS=2
554PARSER_VALIDATE=3
Daniel Veillarde18fc182002-12-28 22:56:33 +0000555PARSER_SUBST_ENTITIES=4
Daniel Veillardf25b4ca2002-12-27 15:18:35 +0000556
557#
Daniel Veillard417be3a2003-01-20 21:26:34 +0000558# For the error callback severities
Daniel Veillard26f70262003-01-16 22:45:08 +0000559#
Daniel Veillard417be3a2003-01-20 21:26:34 +0000560PARSER_SEVERITY_VALIDITY_WARNING=1
561PARSER_SEVERITY_VALIDITY_ERROR=2
562PARSER_SEVERITY_WARNING=3
563PARSER_SEVERITY_ERROR=4
Daniel Veillard26f70262003-01-16 22:45:08 +0000564
565#
Daniel Veillard3e20a292003-01-10 13:14:40 +0000566# register the libxml2 error handler
Daniel Veillard36ed5292002-01-30 23:49:06 +0000567#
Daniel Veillard3e20a292003-01-10 13:14:40 +0000568def registerErrorHandler(f, ctx):
569 """Register a Python written function to for error reporting.
570 The function is called back as f(ctx, error). """
571 import sys
572 if not sys.modules.has_key('libxslt'):
573 # normal behaviour when libxslt is not imported
574 ret = libxml2mod.xmlRegisterErrorHandler(f,ctx)
575 else:
576 # when libxslt is already imported, one must
577 # use libxst's error handler instead
578 import libxslt
579 ret = libxslt.registerErrorHandler(f,ctx)
580 return ret
581
Daniel Veillarde6227e02003-01-14 11:42:39 +0000582class parserCtxtCore:
583
584 def __init__(self, _obj=None):
585 if _obj != None:
586 self._o = _obj;
587 return
588 self._o = None
589
590 def __del__(self):
591 if self._o != None:
592 libxml2mod.xmlFreeParserCtxt(self._o)
William M. Brack1d75c8a2003-10-27 13:48:16 +0000593 self._o = None
Daniel Veillarde6227e02003-01-14 11:42:39 +0000594
Daniel Veillard417be3a2003-01-20 21:26:34 +0000595 def setErrorHandler(self,f,arg):
596 """Register an error handler that will be called back as
597 f(arg,msg,severity,reserved).
598
599 @reserved is currently always None."""
600 libxml2mod.xmlParserCtxtSetErrorHandler(self._o,f,arg)
Daniel Veillarde6227e02003-01-14 11:42:39 +0000601
Daniel Veillard417be3a2003-01-20 21:26:34 +0000602 def getErrorHandler(self):
603 """Return (f,arg) as previously registered with setErrorHandler
604 or (None,None)."""
605 return libxml2mod.xmlParserCtxtGetErrorHandler(self._o)
606
Daniel Veillard54396242003-04-23 07:36:50 +0000607 def addLocalCatalog(self, uri):
608 """Register a local catalog with the parser"""
609 return libxml2mod.addLocalCatalog(self._o, uri)
610
611
Daniel Veillard0e460da2005-03-30 22:47:10 +0000612class ValidCtxtCore:
613
614 def __init__(self, *args, **kw):
615 pass
616
617 def setValidityErrorHandler(self, err_func, warn_func, arg=None):
618 """
619 Register error and warning handlers for DTD validation.
620 These will be called back as f(msg,arg)
621 """
622 libxml2mod.xmlSetValidErrors(self._o, err_func, warn_func, arg)
623
624
625class SchemaValidCtxtCore:
626
627 def __init__(self, *args, **kw):
628 pass
629
630 def setValidityErrorHandler(self, err_func, warn_func, arg=None):
631 """
632 Register error and warning handlers for Schema validation.
633 These will be called back as f(msg,arg)
634 """
635 libxml2mod.xmlSchemaSetValidErrors(self._o, err_func, warn_func, arg)
636
637
638class relaxNgValidCtxtCore:
639
640 def __init__(self, *args, **kw):
641 pass
642
643 def setValidityErrorHandler(self, err_func, warn_func, arg=None):
644 """
645 Register error and warning handlers for RelaxNG validation.
646 These will be called back as f(msg,arg)
647 """
648 libxml2mod.xmlRelaxNGSetValidErrors(self._o, err_func, warn_func, arg)
649
650
Daniel Veillard417be3a2003-01-20 21:26:34 +0000651def _xmlTextReaderErrorFunc((f,arg),msg,severity,locator):
652 """Intermediate callback to wrap the locator"""
653 return f(arg,msg,severity,xmlTextReaderLocator(locator))
Daniel Veillarde6227e02003-01-14 11:42:39 +0000654
Daniel Veillard26f70262003-01-16 22:45:08 +0000655class xmlTextReaderCore:
656
657 def __init__(self, _obj=None):
658 self.input = None
659 if _obj != None:self._o = _obj;return
660 self._o = None
661
662 def __del__(self):
663 if self._o != None:
664 libxml2mod.xmlFreeTextReader(self._o)
665 self._o = None
666
Daniel Veillard417be3a2003-01-20 21:26:34 +0000667 def SetErrorHandler(self,f,arg):
Daniel Veillard26f70262003-01-16 22:45:08 +0000668 """Register an error handler that will be called back as
Daniel Veillard417be3a2003-01-20 21:26:34 +0000669 f(arg,msg,severity,locator)."""
670 if f is None:
671 libxml2mod.xmlTextReaderSetErrorHandler(\
672 self._o,None,None)
673 else:
674 libxml2mod.xmlTextReaderSetErrorHandler(\
675 self._o,_xmlTextReaderErrorFunc,(f,arg))
Daniel Veillard26f70262003-01-16 22:45:08 +0000676
Daniel Veillard417be3a2003-01-20 21:26:34 +0000677 def GetErrorHandler(self):
Daniel Veillard26f70262003-01-16 22:45:08 +0000678 """Return (f,arg) as previously registered with setErrorHandler
679 or (None,None)."""
Daniel Veillard417be3a2003-01-20 21:26:34 +0000680 f,arg = libxml2mod.xmlTextReaderGetErrorHandler(self._o)
681 if f is None:
682 return None,None
683 else:
684 # assert f is _xmlTextReaderErrorFunc
685 return arg
Daniel Veillard26f70262003-01-16 22:45:08 +0000686
Daniel Veillardf93a8662004-07-01 12:56:30 +0000687#
688# The cleanup now goes though a wrappe in libxml.c
689#
690def cleanupParser():
691 libxml2mod.xmlPythonCleanupParser()
Daniel Veillard87ab1c12003-12-21 13:01:56 +0000692
Daniel Veillard3e20a292003-01-10 13:14:40 +0000693# WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
694#
695# Everything before this line comes from libxml.py
696# Everything after this line is automatically generated
697#
698# WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
Daniel Veillard1971ee22002-01-31 20:29:19 +0000699