libxml.py revision 01a6d4170c87368621db478b4d98530265bb9b1f
1import libxml2mod
2
3#
4# This class is the ancestor of all the Node classes. It provides
5# the basic functionalities shared by all nodes (and handle
6# gracefylly the exception), like name, navigation in the tree,
7# doc reference and content access
8#
9class xmlCore:
10    def __init__(self, _obj=None):
11        if _obj != None:
12            self._o = _obj;
13            return
14        self._o = None
15
16    def __getattr__(self, attr):
17        if attr == "parent":
18            ret = libxml2mod.parent(self._o)
19            if ret == None:
20                return None
21            return xmlNode(_obj=ret)
22        elif attr == "properties":
23            ret = libxml2mod.properties(self._o)
24            if ret == None:
25                return None
26            return xmlAttr(_obj=ret)
27        elif attr == "children":
28            ret = libxml2mod.children(self._o)
29            if ret == None:
30                return None
31            return xmlNode(_obj=ret)
32        elif attr == "last":
33            ret = libxml2mod.last(self._o)
34            if ret == None:
35                return None
36            return xmlNode(_obj=ret)
37        elif attr == "next":
38            ret = libxml2mod.next(self._o)
39            if ret == None:
40                return None
41            return xmlNode(_obj=ret)
42        elif attr == "prev":
43            ret = libxml2mod.prev(self._o)
44            if ret == None:
45                return None
46            return xmlNode(_obj=ret)
47        elif attr == "content":
48            return libxml2mod.xmlNodeGetContent(self._o)
49        elif attr == "name":
50            return libxml2mod.name(self._o)
51        elif attr == "type":
52            return libxml2mod.type(self._o)
53        elif attr == "doc":
54            ret = libxml2mod.doc(self._o)
55            if ret == None:
56                return None
57            return xmlDoc(_doc=ret)
58        raise AttributeError,attr
59
60        #
61        # Those are common attributes to nearly all type of nodes
62        #
63    def get_parent(self):
64        ret = libxml2mod.parent(self._o)
65        if ret == None:
66            return None
67        return xmlNode(_obj=ret)
68    def get_children(self):
69        ret = libxml2mod.children(self._o)
70        if ret == None:
71            return None
72        return xmlNode(_obj=ret)
73    def get_last(self):
74        ret = libxml2mod.last(self._o)
75        if ret == None:
76            return None
77        return xmlNode(_obj=ret)
78    def get_next(self):
79        ret = libxml2mod.next(self._o)
80        if ret == None:
81            return None
82        return xmlNode(_obj=ret)
83    def get_properties(self):
84        ret = libxml2mod.properties(self._o)
85        if ret == None:
86            return None
87        return xmlAttr(_obj=ret)
88    def get_doc(self):
89        ret = libxml2mod.doc(self._o)
90        if ret == None:
91            return None
92        return xmlDoc(_obj=ret)
93    def get_prev(self):
94        ret = libxml2mod.prev(self._o)
95        if ret == None:
96            return None
97        return xmlNode(_obj=ret)
98    def get_content(self):
99        return libxml2mod.xmlNodeGetContent(self._o)
100    def getContent(self):
101        return libxml2mod.xmlNodeGetContent(self._o)
102    def get_name(self):
103        return libxml2mod.name(self._o)
104    def get_type(self):
105        return libxml2mod.type(self._o)
106    def get_doc(self):
107        ret = libxml2mod.doc(self._o)
108        if ret == None:
109            return None
110        return xmlDoc(_doc=ret)
111    def free(self):
112        libxml2mod.freeDoc(self._o)
113
114#
115# converters to present a nicer view of the XPath returns
116#
117def nodeWrap(o):
118    # TODO try to cast to the most appropriate node class
119    name = libxml2mod.name(o)
120    if name == "element" or name == "text":
121        return xmlNode(_obj=o)
122    if name == "attribute":
123        return xmlAttr(_obj=o)
124    if name[0:8] == "document":
125        return xmlDoc(_obj=o)
126    if name[0:8] == "namespace":
127        return xmlNs(_obj=o)
128    if name == "elem_decl":
129        return xmlElement(_obj=o)
130    if name == "attribute_decl":
131        return xmlAtribute(_obj=o)
132    if name == "entity_decl":
133        return xmlEntity(_obj=o)
134    if name == "dtd":
135        return xmlAttr(_obj=o)
136    return xmlNode(_obj=o)
137
138def xpathObjectRet(o):
139    if type(o) == type([]) or type(o) == type(()):
140        ret = map(lambda x: nodeWrap(x), o)
141        return ret
142    return o
143
144#
145# register an XPath function
146#
147def registerXPathFunction(ctxt, name, ns_uri, f):
148    ret = libxml2mod.xmlRegisterXPathFunction(ctxt, name, ns_uri, f)
149
150#
151# Everything below this point is automatically generated
152#
153
154