File: libxml.py

package info (click to toggle)
libxml2 2.4.19-4woody2
  • links: PTS
  • area: main
  • in suites: woody
  • size: 14,936 kB
  • ctags: 5,542
  • sloc: ansic: 74,548; xml: 20,808; sh: 6,898; python: 2,869; makefile: 785
file content (310 lines) | stat: -rw-r--r-- 9,524 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
import libxml2mod

#
# Errors raised by the wrappers when some tree handling failed.
#
class treeError:
    def __init__(self, msg):
        self.msg = msg
    def __str__(self):
        return self.msg

class parserError:
    def __init__(self, msg):
        self.msg = msg
    def __str__(self):
        return self.msg

class uriError:
    def __init__(self, msg):
        self.msg = msg
    def __str__(self):
        return self.msg

class xpathError:
    def __init__(self, msg):
        self.msg = msg
    def __str__(self):
        return self.msg

#
# Example of a class to handle SAX events
#
class SAXCallback:
    """Base class for SAX handlers"""
    def startDocument(self):
        """called at the start of the document"""
        pass

    def endDocument(self):
        """called at the end of the document"""
        pass

    def startElement(self, tag, attrs):
        """called at the start of every element, tag is the name of
	   the element, attrs is a dictionary of the element's attributes"""
        pass

    def endElement(self, tag):
        """called at the start of every element, tag is the name of
	   the element"""
        pass

    def characters(self, data):
        """called when character data have been read, data is the string
	   containing the data, multiple consecutive characters() callback
	   are possible."""
        pass

    def cdataBlock(self, data):
        """called when CDATA section have been read, data is the string
	   containing the data, multiple consecutive cdataBlock() callback
	   are possible."""
        pass

    def reference(self, name):
        """called when an entity reference has been found"""
        pass

    def ignorableWhitespace(self, data):
        """called when potentially ignorable white spaces have been found"""
        pass

    def processingInstruction(self, target, data):
        """called when a PI has been found, target contains the PI name and
	   data is the associated data in the PI"""
        pass

    def comment(self, content):
        """called when a comment has been found, content contains the comment"""
        pass

    def externalSubset(self, name, externalID, systemID):
        """called when a DOCTYPE declaration has been found, name is the
	   DTD name and externalID, systemID are the DTD public and system
	   identifier for that DTd if available"""
        pass

    def internalSubset(self, name, externalID, systemID):
        """called when a DOCTYPE declaration has been found, name is the
	   DTD name and externalID, systemID are the DTD public and system
	   identifier for that DTD if available"""
        pass

    def entityDecl(self, name, type, externalID, systemID, content):
        """called when an ENTITY declaration has been found, name is the
	   entity name and externalID, systemID are the entity public and
	   system identifier for that entity if available, type indicates
	   the entity type, and content reports it's string content"""
        pass

    def notationDecl(self, name, externalID, systemID):
        """called when an NOTATION declaration has been found, name is the
	   notation name and externalID, systemID are the notation public and
	   system identifier for that notation if available"""
        pass

    def attributeDecl(self, elem, name, type, defi, defaultValue, nameList):
        """called when an ATTRIBUTE definition has been found"""
	pass

    def elementDecl(self, name, type, content):
        """called when an ELEMENT definition has been found"""
	pass

    def entityDecl(self, name, publicId, systemID, notationName):
        """called when an unparsed ENTITY declaration has been found,
	   name is the entity name and publicId,, systemID are the entity
	   public and system identifier for that entity if available,
	   and notationName indicate the associated NOTATION"""
        pass

    def warning(self, msg):
        print msg

    def error(self, msg):
        raise parserError(msg)

    def fatalError(self, msg):
        raise parserError(msg)

#
# This class is the ancestor of all the Node classes. It provides
# the basic functionalities shared by all nodes (and handle
# gracefylly the exception), like name, navigation in the tree,
# doc reference, content access and serializing to a string or URI
#
class xmlCore:
    def __init__(self, _obj=None):
        if _obj != None: 
            self._o = _obj;
            return
        self._o = None

    def __getattr__(self, attr):
        if attr == "parent":
            ret = libxml2mod.parent(self._o)
            if ret == None:
                return None
            return xmlNode(_obj=ret)
        elif attr == "properties":
            ret = libxml2mod.properties(self._o)
            if ret == None:
                return None
            return xmlAttr(_obj=ret)
        elif attr == "children":
            ret = libxml2mod.children(self._o)
            if ret == None:
                return None
            return xmlNode(_obj=ret)
        elif attr == "last":
            ret = libxml2mod.last(self._o)
            if ret == None:
                return None
            return xmlNode(_obj=ret)
        elif attr == "next":
            ret = libxml2mod.next(self._o)
            if ret == None:
                return None
            return xmlNode(_obj=ret)
        elif attr == "prev":
            ret = libxml2mod.prev(self._o)
            if ret == None:
                return None
            return xmlNode(_obj=ret)
        elif attr == "content":
            return libxml2mod.xmlNodeGetContent(self._o)
        elif attr == "name":
            return libxml2mod.name(self._o)
        elif attr == "type":
            return libxml2mod.type(self._o)
        elif attr == "doc":
            ret = libxml2mod.doc(self._o)
            if ret == None:
		if self.type == "document_xml" or self.type == "document_html":
		    return xmlDoc(_obj=self._o)
		else:
		    return None
            return xmlDoc(_obj=ret)
        raise AttributeError,attr

        #
        # Those are common attributes to nearly all type of nodes
        #
    def get_parent(self):
        ret = libxml2mod.parent(self._o)
        if ret == None:
            return None
        return xmlNode(_obj=ret)
    def get_children(self):
        ret = libxml2mod.children(self._o)
        if ret == None:
            return None
        return xmlNode(_obj=ret)
    def get_last(self):
        ret = libxml2mod.last(self._o)
        if ret == None:
            return None
        return xmlNode(_obj=ret)
    def get_next(self):
        ret = libxml2mod.next(self._o)
        if ret == None:
            return None
        return xmlNode(_obj=ret)
    def get_properties(self):
        ret = libxml2mod.properties(self._o)
        if ret == None:
            return None
        return xmlAttr(_obj=ret)
    def get_doc(self):
        ret = libxml2mod.doc(self._o)
        if ret == None:
            return None
        return xmlDoc(_obj=ret)
    def get_prev(self):
        ret = libxml2mod.prev(self._o)
        if ret == None:
            return None
        return xmlNode(_obj=ret)
    def get_content(self):
        return libxml2mod.xmlNodeGetContent(self._o)
    def getContent(self):
        return libxml2mod.xmlNodeGetContent(self._o)
    def get_name(self):
        return libxml2mod.name(self._o)
    def get_type(self):
        return libxml2mod.type(self._o)
    def get_doc(self):
        ret = libxml2mod.doc(self._o)
        if ret == None:
            return None
        return xmlDoc(_obj=ret)
    def free(self):
        libxml2mod.freeDoc(self._o)

    #
    # Serialization routines, the optional arguments have the following
    # meaning:
    #     encoding: string to ask saving in a specific encoding
    #     indent: if 1 the serializer is asked to indent the output
    #
    def serialize(self, encoding = None, format = 0):
        return libxml2mod.serializeNode(self._o, encoding, format)
    def saveTo(self, file, encoding = None, format = 0):
        return libxml2mod.saveNodeTo(self._o, file, encoding, format)
            
    #
    # Selecting nodes using XPath, a bit slow because the context
    # is allocated/freed every time but convenient.
    #
    def xpathEval(self, expr):
	doc = self.doc
	if doc == None:
	    return None
	ctxt = doc.xpathNewContext()
	ctxt.setContextNode(self)
	res = ctxt.xpathEval(expr)
	ctxt.xpathFreeContext()
	return res
	
#
# converters to present a nicer view of the XPath returns
#
def nodeWrap(o):
    # TODO try to cast to the most appropriate node class
    name = libxml2mod.name(o)
    if name == "element" or name == "text":
        return xmlNode(_obj=o)
    if name == "attribute":
        return xmlAttr(_obj=o)
    if name[0:8] == "document":
        return xmlDoc(_obj=o)
    if name[0:8] == "namespace":
        return xmlNs(_obj=o)
    if name == "elem_decl":
        return xmlElement(_obj=o)
    if name == "attribute_decl":
        return xmlAtribute(_obj=o)
    if name == "entity_decl":
        return xmlEntity(_obj=o)
    if name == "dtd":
        return xmlAttr(_obj=o)
    return xmlNode(_obj=o)

def xpathObjectRet(o):
    if type(o) == type([]) or type(o) == type(()):
        ret = map(lambda x: nodeWrap(x), o)
        return ret
    return o

#
# register an XPath function
#
def registerXPathFunction(ctxt, name, ns_uri, f):
    ret = libxml2mod.xmlRegisterXPathFunction(ctxt, name, ns_uri, f)

    
#
# Everything below this point is automatically generated
#