Daniel Veillard | 5e5c2d0 | 2002-02-09 18:03:01 +0000 | [diff] [blame] | 1 | import libxml2mod |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 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: |
Daniel Veillard | 01a6d41 | 2002-02-11 18:42:20 +0000 | [diff] [blame] | 12 | self._o = _obj; |
| 13 | return |
| 14 | self._o = None |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 15 | |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 16 | def __getattr__(self, attr): |
| 17 | if attr == "parent": |
Daniel Veillard | 01a6d41 | 2002-02-11 18:42:20 +0000 | [diff] [blame] | 18 | ret = libxml2mod.parent(self._o) |
| 19 | if ret == None: |
| 20 | return None |
| 21 | return xmlNode(_obj=ret) |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 22 | elif attr == "properties": |
Daniel Veillard | 01a6d41 | 2002-02-11 18:42:20 +0000 | [diff] [blame] | 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 |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 59 | |
Daniel Veillard | 01a6d41 | 2002-02-11 18:42:20 +0000 | [diff] [blame] | 60 | # |
| 61 | # Those are common attributes to nearly all type of nodes |
| 62 | # |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 63 | def get_parent(self): |
Daniel Veillard | 01a6d41 | 2002-02-11 18:42:20 +0000 | [diff] [blame] | 64 | ret = libxml2mod.parent(self._o) |
| 65 | if ret == None: |
| 66 | return None |
| 67 | return xmlNode(_obj=ret) |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 68 | def get_children(self): |
Daniel Veillard | 01a6d41 | 2002-02-11 18:42:20 +0000 | [diff] [blame] | 69 | ret = libxml2mod.children(self._o) |
| 70 | if ret == None: |
| 71 | return None |
| 72 | return xmlNode(_obj=ret) |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 73 | def get_last(self): |
Daniel Veillard | 01a6d41 | 2002-02-11 18:42:20 +0000 | [diff] [blame] | 74 | ret = libxml2mod.last(self._o) |
| 75 | if ret == None: |
| 76 | return None |
| 77 | return xmlNode(_obj=ret) |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 78 | def get_next(self): |
Daniel Veillard | 01a6d41 | 2002-02-11 18:42:20 +0000 | [diff] [blame] | 79 | ret = libxml2mod.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): |
Daniel Veillard | 01a6d41 | 2002-02-11 18:42:20 +0000 | [diff] [blame] | 84 | ret = libxml2mod.properties(self._o) |
| 85 | if ret == None: |
| 86 | return None |
| 87 | return xmlAttr(_obj=ret) |
Daniel Veillard | 1971ee2 | 2002-01-31 20:29:19 +0000 | [diff] [blame] | 88 | def get_doc(self): |
Daniel Veillard | 01a6d41 | 2002-02-11 18:42:20 +0000 | [diff] [blame] | 89 | ret = libxml2mod.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): |
Daniel Veillard | 01a6d41 | 2002-02-11 18:42:20 +0000 | [diff] [blame] | 94 | ret = libxml2mod.prev(self._o) |
| 95 | if ret == None: |
| 96 | return None |
| 97 | return xmlNode(_obj=ret) |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 98 | def get_content(self): |
Daniel Veillard | 01a6d41 | 2002-02-11 18:42:20 +0000 | [diff] [blame] | 99 | return libxml2mod.xmlNodeGetContent(self._o) |
Daniel Veillard | 1971ee2 | 2002-01-31 20:29:19 +0000 | [diff] [blame] | 100 | def getContent(self): |
Daniel Veillard | 01a6d41 | 2002-02-11 18:42:20 +0000 | [diff] [blame] | 101 | return libxml2mod.xmlNodeGetContent(self._o) |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 102 | def get_name(self): |
Daniel Veillard | 01a6d41 | 2002-02-11 18:42:20 +0000 | [diff] [blame] | 103 | return libxml2mod.name(self._o) |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 104 | def get_type(self): |
Daniel Veillard | 01a6d41 | 2002-02-11 18:42:20 +0000 | [diff] [blame] | 105 | return libxml2mod.type(self._o) |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 106 | def get_doc(self): |
Daniel Veillard | 01a6d41 | 2002-02-11 18:42:20 +0000 | [diff] [blame] | 107 | ret = libxml2mod.doc(self._o) |
| 108 | if ret == None: |
| 109 | return None |
| 110 | return xmlDoc(_doc=ret) |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 111 | def free(self): |
Daniel Veillard | 5e5c2d0 | 2002-02-09 18:03:01 +0000 | [diff] [blame] | 112 | libxml2mod.freeDoc(self._o) |
Daniel Veillard | 01a6d41 | 2002-02-11 18:42:20 +0000 | [diff] [blame] | 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 |
Daniel Veillard | 5e5c2d0 | 2002-02-09 18:03:01 +0000 | [diff] [blame] | 119 | name = libxml2mod.name(o) |
Daniel Veillard | 1971ee2 | 2002-01-31 20:29:19 +0000 | [diff] [blame] | 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) |
Daniel Veillard | 01a6d41 | 2002-02-11 18:42:20 +0000 | [diff] [blame] | 141 | return ret |
Daniel Veillard | 1971ee2 | 2002-01-31 20:29:19 +0000 | [diff] [blame] | 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): |
Daniel Veillard | 5e5c2d0 | 2002-02-09 18:03:01 +0000 | [diff] [blame] | 148 | ret = libxml2mod.xmlRegisterXPathFunction(ctxt, name, ns_uri, f) |
Daniel Veillard | a7340c8 | 2002-02-01 17:56:45 +0000 | [diff] [blame] | 149 | |
Daniel Veillard | 797a565 | 2002-02-12 13:46:21 +0000 | [diff] [blame] | 150 | |
Daniel Veillard | a7340c8 | 2002-02-01 17:56:45 +0000 | [diff] [blame] | 151 | # |
Daniel Veillard | 36ed529 | 2002-01-30 23:49:06 +0000 | [diff] [blame] | 152 | # Everything below this point is automatically generated |
| 153 | # |
Daniel Veillard | 1971ee2 | 2002-01-31 20:29:19 +0000 | [diff] [blame] | 154 | |