blob: 18f684091a68a8f191cfd22ec07bf73bce9a26ae [file] [log] [blame]
Daniel Veillard5e5c2d02002-02-09 18:03:01 +00001import libxml2mod
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002
Daniel Veillard1971ee22002-01-31 20:29:19 +00003#
Daniel Veillard8d24cc12002-03-05 15:41:29 +00004# Errors raised by the wrappers when some tree handling failed.
5#
6class treeError:
7 def __init__(self, msg):
8 self.msg = msg
9 def __str__(self):
10 return self.msg
11
12class parserError:
13 def __init__(self, msg):
14 self.msg = msg
15 def __str__(self):
16 return self.msg
17
18class uriError:
19 def __init__(self, msg):
20 self.msg = msg
21 def __str__(self):
22 return self.msg
23
24class 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#
33class 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 Veillard1971ee22002-01-31 20:29:19 +0000132# 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 Veillard36ed5292002-01-30 23:49:06 +0000137class xmlCore:
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000138 def __init__(self, _obj=None):
139 if _obj != None:
Daniel Veillard01a6d412002-02-11 18:42:20 +0000140 self._o = _obj;
141 return
142 self._o = None
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000143
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000144 def __getattr__(self, attr):
145 if attr == "parent":
Daniel Veillard01a6d412002-02-11 18:42:20 +0000146 ret = libxml2mod.parent(self._o)
147 if ret == None:
148 return None
149 return xmlNode(_obj=ret)
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000150 elif attr == "properties":
Daniel Veillard01a6d412002-02-11 18:42:20 +0000151 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 Veillardd2897fd2002-01-30 16:37:32 +0000187
Daniel Veillard01a6d412002-02-11 18:42:20 +0000188 #
189 # Those are common attributes to nearly all type of nodes
190 #
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000191 def get_parent(self):
Daniel Veillard01a6d412002-02-11 18:42:20 +0000192 ret = libxml2mod.parent(self._o)
193 if ret == None:
194 return None
195 return xmlNode(_obj=ret)
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000196 def get_children(self):
Daniel Veillard01a6d412002-02-11 18:42:20 +0000197 ret = libxml2mod.children(self._o)
198 if ret == None:
199 return None
200 return xmlNode(_obj=ret)
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000201 def get_last(self):
Daniel Veillard01a6d412002-02-11 18:42:20 +0000202 ret = libxml2mod.last(self._o)
203 if ret == None:
204 return None
205 return xmlNode(_obj=ret)
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000206 def get_next(self):
Daniel Veillard01a6d412002-02-11 18:42:20 +0000207 ret = libxml2mod.next(self._o)
208 if ret == None:
209 return None
210 return xmlNode(_obj=ret)
Daniel Veillard1971ee22002-01-31 20:29:19 +0000211 def get_properties(self):
Daniel Veillard01a6d412002-02-11 18:42:20 +0000212 ret = libxml2mod.properties(self._o)
213 if ret == None:
214 return None
215 return xmlAttr(_obj=ret)
Daniel Veillard1971ee22002-01-31 20:29:19 +0000216 def get_doc(self):
Daniel Veillard01a6d412002-02-11 18:42:20 +0000217 ret = libxml2mod.doc(self._o)
218 if ret == None:
219 return None
220 return xmlDoc(_obj=ret)
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000221 def get_prev(self):
Daniel Veillard01a6d412002-02-11 18:42:20 +0000222 ret = libxml2mod.prev(self._o)
223 if ret == None:
224 return None
225 return xmlNode(_obj=ret)
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000226 def get_content(self):
Daniel Veillard01a6d412002-02-11 18:42:20 +0000227 return libxml2mod.xmlNodeGetContent(self._o)
Daniel Veillard1971ee22002-01-31 20:29:19 +0000228 def getContent(self):
Daniel Veillard01a6d412002-02-11 18:42:20 +0000229 return libxml2mod.xmlNodeGetContent(self._o)
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000230 def get_name(self):
Daniel Veillard01a6d412002-02-11 18:42:20 +0000231 return libxml2mod.name(self._o)
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000232 def get_type(self):
Daniel Veillard01a6d412002-02-11 18:42:20 +0000233 return libxml2mod.type(self._o)
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000234 def get_doc(self):
Daniel Veillard01a6d412002-02-11 18:42:20 +0000235 ret = libxml2mod.doc(self._o)
236 if ret == None:
237 return None
238 return xmlDoc(_doc=ret)
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000239 def free(self):
Daniel Veillard5e5c2d02002-02-09 18:03:01 +0000240 libxml2mod.freeDoc(self._o)
Daniel Veillard01a6d412002-02-11 18:42:20 +0000241
Daniel Veillard36ed5292002-01-30 23:49:06 +0000242#
Daniel Veillard1971ee22002-01-31 20:29:19 +0000243# converters to present a nicer view of the XPath returns
244#
245def nodeWrap(o):
246 # TODO try to cast to the most appropriate node class
Daniel Veillard5e5c2d02002-02-09 18:03:01 +0000247 name = libxml2mod.name(o)
Daniel Veillard1971ee22002-01-31 20:29:19 +0000248 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
266def xpathObjectRet(o):
267 if type(o) == type([]) or type(o) == type(()):
268 ret = map(lambda x: nodeWrap(x), o)
Daniel Veillard01a6d412002-02-11 18:42:20 +0000269 return ret
Daniel Veillard1971ee22002-01-31 20:29:19 +0000270 return o
271
272#
Daniel Veillarda7340c82002-02-01 17:56:45 +0000273# register an XPath function
274#
275def registerXPathFunction(ctxt, name, ns_uri, f):
Daniel Veillard5e5c2d02002-02-09 18:03:01 +0000276 ret = libxml2mod.xmlRegisterXPathFunction(ctxt, name, ns_uri, f)
Daniel Veillarda7340c82002-02-01 17:56:45 +0000277
Daniel Veillard797a5652002-02-12 13:46:21 +0000278
Daniel Veillarda7340c82002-02-01 17:56:45 +0000279#
Daniel Veillard36ed5292002-01-30 23:49:06 +0000280# Everything below this point is automatically generated
281#
Daniel Veillard1971ee22002-01-31 20:29:19 +0000282