Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 1 | import _libxml |
| 2 | |
Daniel Veillard | 1971ee2 | 2002-01-31 20:29:19 +0000 | [diff] [blame] | 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 | # |
Daniel Veillard | 36ed529 | 2002-01-30 23:49:06 +0000 | [diff] [blame] | 9 | class xmlCore: |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 10 | def __init__(self, _obj=None): |
| 11 | if _obj != None: |
| 12 | self._o = _obj; |
| 13 | return |
| 14 | self._o = None |
| 15 | |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 16 | def __getattr__(self, attr): |
| 17 | if attr == "parent": |
| 18 | ret = _libxml.parent(self._o) |
| 19 | if ret == None: |
| 20 | return None |
| 21 | return xmlNode(_obj=ret) |
| 22 | elif attr == "properties": |
| 23 | ret = _libxml.properties(self._o) |
| 24 | if ret == None: |
| 25 | return None |
Daniel Veillard | 1971ee2 | 2002-01-31 20:29:19 +0000 | [diff] [blame] | 26 | return xmlAttr(_obj=ret) |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 27 | elif attr == "children": |
| 28 | ret = _libxml.children(self._o) |
| 29 | if ret == None: |
| 30 | return None |
| 31 | return xmlNode(_obj=ret) |
| 32 | elif attr == "last": |
| 33 | ret = _libxml.last(self._o) |
| 34 | if ret == None: |
| 35 | return None |
| 36 | return xmlNode(_obj=ret) |
| 37 | elif attr == "next": |
| 38 | ret = _libxml.next(self._o) |
| 39 | if ret == None: |
| 40 | return None |
| 41 | return xmlNode(_obj=ret) |
| 42 | elif attr == "prev": |
| 43 | ret = _libxml.prev(self._o) |
| 44 | if ret == None: |
| 45 | return None |
| 46 | return xmlNode(_obj=ret) |
| 47 | elif attr == "content": |
| 48 | return self.content() |
| 49 | elif attr == "name": |
| 50 | return _libxml.name(self._o) |
| 51 | elif attr == "type": |
| 52 | return _libxml.type(self._o) |
| 53 | elif attr == "doc": |
| 54 | ret = _libxml.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 = _libxml.parent(self._o) |
| 65 | if ret == None: |
| 66 | return None |
| 67 | return xmlNode(_obj=ret) |
| 68 | def get_children(self): |
| 69 | ret = _libxml.children(self._o) |
| 70 | if ret == None: |
| 71 | return None |
| 72 | return xmlNode(_obj=ret) |
| 73 | def get_last(self): |
| 74 | ret = _libxml.last(self._o) |
| 75 | if ret == None: |
| 76 | return None |
| 77 | return xmlNode(_obj=ret) |
| 78 | def get_next(self): |
| 79 | ret = _libxml.next(self._o) |
| 80 | if ret == None: |
| 81 | return None |
| 82 | return xmlNode(_obj=ret) |
Daniel Veillard | 1971ee2 | 2002-01-31 20:29:19 +0000 | [diff] [blame] | 83 | def get_properties(self): |
| 84 | ret = _libxml.properties(self._o) |
| 85 | if ret == None: |
| 86 | return None |
| 87 | return xmlAttr(_obj=ret) |
| 88 | def get_doc(self): |
| 89 | ret = _libxml.doc(self._o) |
| 90 | if ret == None: |
| 91 | return None |
| 92 | return xmlDoc(_obj=ret) |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 93 | def get_prev(self): |
| 94 | ret = _libxml.prev(self._o) |
| 95 | if ret == None: |
| 96 | return None |
| 97 | return xmlNode(_obj=ret) |
| 98 | def get_content(self): |
Daniel Veillard | 1971ee2 | 2002-01-31 20:29:19 +0000 | [diff] [blame] | 99 | return _libxml.xmlNodeGetContent(self._o) |
| 100 | def getContent(self): |
| 101 | return _libxml.xmlNodeGetContent(self._o) |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 102 | def get_name(self): |
| 103 | return _libxml.name(self._o) |
| 104 | def get_type(self): |
| 105 | return _libxml.type(self._o) |
| 106 | def get_doc(self): |
| 107 | ret = _libxml.doc(self._o) |
| 108 | if ret == None: |
| 109 | return None |
| 110 | return xmlDoc(_doc=ret) |
| 111 | def free(self): |
| 112 | _libxml.freeDoc(self._o) |
| 113 | |
Daniel Veillard | 36ed529 | 2002-01-30 23:49:06 +0000 | [diff] [blame] | 114 | # |
Daniel Veillard | 1971ee2 | 2002-01-31 20:29:19 +0000 | [diff] [blame] | 115 | # converters to present a nicer view of the XPath returns |
| 116 | # |
| 117 | def nodeWrap(o): |
| 118 | # TODO try to cast to the most appropriate node class |
| 119 | name = _libxml.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 | |
| 138 | def 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 | # |
Daniel Veillard | a7340c8 | 2002-02-01 17:56:45 +0000 | [diff] [blame^] | 145 | # register an XPath function |
| 146 | # |
| 147 | def registerXPathFunction(ctxt, name, ns_uri, f): |
| 148 | ret = _libxml.registerXPathFunction(ctxt, name, ns_uri, f) |
| 149 | |
| 150 | # |
Daniel Veillard | 36ed529 | 2002-01-30 23:49:06 +0000 | [diff] [blame] | 151 | # Everything below this point is automatically generated |
| 152 | # |
Daniel Veillard | 1971ee2 | 2002-01-31 20:29:19 +0000 | [diff] [blame] | 153 | |