Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 1 | /*********************************************************** |
| 2 | Copyright 2000 by Stichting Mathematisch Centrum, Amsterdam, |
| 3 | The Netherlands. |
| 4 | |
| 5 | All Rights Reserved |
| 6 | |
Guido van Rossum | fd71b9e | 2000-06-30 23:50:40 +0000 | [diff] [blame] | 7 | Copyright (c) 2000, BeOpen.com. |
| 8 | Copyright (c) 1995-2000, Corporation for National Research Initiatives. |
| 9 | Copyright (c) 1990-1995, Stichting Mathematisch Centrum. |
| 10 | All rights reserved. |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 11 | |
Guido van Rossum | fd71b9e | 2000-06-30 23:50:40 +0000 | [diff] [blame] | 12 | See the file "Misc/COPYRIGHT" for information on usage and |
| 13 | redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 14 | |
| 15 | ******************************************************************/ |
| 16 | |
| 17 | #include "Python.h" |
| 18 | #include "xmlparse.h" |
| 19 | |
| 20 | /* |
| 21 | ** The version number should match the one in _checkversion |
| 22 | */ |
| 23 | #define VERSION "1.9" |
| 24 | |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 25 | enum HandlerTypes { |
| 26 | StartElement, |
| 27 | EndElement, |
| 28 | ProcessingInstruction, |
| 29 | CharacterData, |
| 30 | UnparsedEntityDecl, |
| 31 | NotationDecl, |
| 32 | StartNamespaceDecl, |
| 33 | EndNamespaceDecl, |
| 34 | Comment, |
| 35 | StartCdataSection, |
| 36 | EndCdataSection, |
| 37 | Default, |
| 38 | DefaultHandlerExpand, |
| 39 | NotStandalone, |
| 40 | ExternalEntityRef |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 41 | }; |
| 42 | |
| 43 | static PyObject *ErrorObject; |
| 44 | |
| 45 | /* ----------------------------------------------------- */ |
| 46 | |
| 47 | /* Declarations for objects of type xmlparser */ |
| 48 | |
| 49 | typedef struct { |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 50 | PyObject_HEAD |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 51 | |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 52 | XML_Parser itself; |
| 53 | int returns_unicode; /* True if Unicode strings are returned; |
| 54 | if false, UTF-8 strings are returned */ |
| 55 | PyObject **handlers; |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 56 | } xmlparseobject; |
| 57 | |
| 58 | staticforward PyTypeObject Xmlparsetype; |
| 59 | |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 60 | typedef void (*xmlhandlersetter)(XML_Parser *self, void *meth); |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 61 | typedef void* xmlhandler; |
| 62 | |
Andrew M. Kuchling | beba056 | 2000-06-27 00:33:30 +0000 | [diff] [blame] | 63 | struct HandlerInfo { |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 64 | const char *name; |
| 65 | xmlhandlersetter setter; |
| 66 | xmlhandler handler; |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 67 | }; |
| 68 | |
Andrew M. Kuchling | 637f664 | 2000-07-04 14:53:43 +0000 | [diff] [blame] | 69 | staticforward struct HandlerInfo handler_info[64]; |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 70 | |
Andrew M. Kuchling | beba056 | 2000-06-27 00:33:30 +0000 | [diff] [blame] | 71 | /* Convert an array of attributes and their values into a Python dict */ |
| 72 | |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 73 | static PyObject * |
| 74 | conv_atts_using_string(XML_Char **atts) |
Andrew M. Kuchling | a4e75d7 | 2000-07-12 00:53:41 +0000 | [diff] [blame] | 75 | { |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 76 | PyObject *attrs_obj = NULL; |
| 77 | XML_Char **attrs_p, **attrs_k = NULL; |
| 78 | int attrs_len; |
| 79 | PyObject *rv; |
Andrew M. Kuchling | beba056 | 2000-06-27 00:33:30 +0000 | [diff] [blame] | 80 | |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 81 | if ((attrs_obj = PyDict_New()) == NULL) |
| 82 | goto finally; |
| 83 | for (attrs_len = 0, attrs_p = atts; |
| 84 | *attrs_p; |
| 85 | attrs_p++, attrs_len++) { |
| 86 | if (attrs_len % 2) { |
| 87 | rv = PyString_FromString(*attrs_p); |
| 88 | if (!rv) { |
| 89 | Py_DECREF(attrs_obj); |
| 90 | attrs_obj = NULL; |
| 91 | goto finally; |
| 92 | } |
| 93 | if (PyDict_SetItemString(attrs_obj, |
| 94 | (char*)*attrs_k, rv) < 0) { |
| 95 | Py_DECREF(attrs_obj); |
| 96 | attrs_obj = NULL; |
| 97 | goto finally; |
| 98 | } |
| 99 | Py_DECREF(rv); |
| 100 | } |
| 101 | else |
| 102 | attrs_k = attrs_p; |
| 103 | } |
| 104 | finally: |
| 105 | return attrs_obj; |
Andrew M. Kuchling | beba056 | 2000-06-27 00:33:30 +0000 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | #if !(PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6) |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 109 | static PyObject * |
| 110 | conv_atts_using_unicode(XML_Char **atts) |
| 111 | { |
| 112 | PyObject *attrs_obj = NULL; |
| 113 | XML_Char **attrs_p, **attrs_k = NULL; |
| 114 | int attrs_len; |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 115 | |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 116 | if ((attrs_obj = PyDict_New()) == NULL) |
| 117 | goto finally; |
| 118 | for (attrs_len = 0, attrs_p = atts; |
| 119 | *attrs_p; |
| 120 | attrs_p++, attrs_len++) { |
| 121 | if (attrs_len % 2) { |
| 122 | PyObject *attr_str, *value_str; |
| 123 | const char *p = (const char *) (*attrs_k); |
| 124 | attr_str = PyUnicode_DecodeUTF8(p, strlen(p), "strict"); |
| 125 | if (!attr_str) { |
| 126 | Py_DECREF(attrs_obj); |
| 127 | attrs_obj = NULL; |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 128 | goto finally; |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 129 | } |
| 130 | p = (const char *) *attrs_p; |
| 131 | value_str = PyUnicode_DecodeUTF8(p, strlen(p), "strict"); |
| 132 | if (!value_str) { |
| 133 | Py_DECREF(attrs_obj); |
| 134 | Py_DECREF(attr_str); |
| 135 | attrs_obj = NULL; |
| 136 | goto finally; |
| 137 | } |
| 138 | if (PyDict_SetItem(attrs_obj, attr_str, value_str) < 0) { |
| 139 | Py_DECREF(attrs_obj); |
| 140 | attrs_obj = NULL; |
| 141 | goto finally; |
| 142 | } |
| 143 | Py_DECREF(attr_str); |
| 144 | Py_DECREF(value_str); |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 145 | } |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 146 | else |
| 147 | attrs_k = attrs_p; |
| 148 | } |
| 149 | finally: |
| 150 | return attrs_obj; |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 151 | } |
| 152 | |
Andrew M. Kuchling | beba056 | 2000-06-27 00:33:30 +0000 | [diff] [blame] | 153 | /* Convert a string of XML_Chars into a Unicode string. |
| 154 | Returns None if str is a null pointer. */ |
| 155 | |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 156 | static PyObject * |
| 157 | conv_string_to_unicode(XML_Char *str) |
| 158 | { |
| 159 | /* XXX currently this code assumes that XML_Char is 8-bit, |
| 160 | and hence in UTF-8. */ |
| 161 | /* UTF-8 from Expat, Unicode desired */ |
| 162 | if (str == NULL) { |
| 163 | Py_INCREF(Py_None); |
| 164 | return Py_None; |
| 165 | } |
| 166 | return PyUnicode_DecodeUTF8((const char *)str, |
| 167 | strlen((const char *)str), |
| 168 | "strict"); |
Andrew M. Kuchling | beba056 | 2000-06-27 00:33:30 +0000 | [diff] [blame] | 169 | } |
| 170 | |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 171 | static PyObject * |
| 172 | conv_string_len_to_unicode(const XML_Char *str, int len) |
| 173 | { |
| 174 | /* XXX currently this code assumes that XML_Char is 8-bit, |
| 175 | and hence in UTF-8. */ |
| 176 | /* UTF-8 from Expat, Unicode desired */ |
| 177 | if (str == NULL) { |
| 178 | Py_INCREF(Py_None); |
| 179 | return Py_None; |
| 180 | } |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 181 | return PyUnicode_DecodeUTF8((const char *)str, len, "strict"); |
Andrew M. Kuchling | beba056 | 2000-06-27 00:33:30 +0000 | [diff] [blame] | 182 | } |
| 183 | #endif |
| 184 | |
| 185 | /* Convert a string of XML_Chars into an 8-bit Python string. |
| 186 | Returns None if str is a null pointer. */ |
| 187 | |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 188 | static PyObject * |
| 189 | conv_string_to_utf8(XML_Char *str) |
| 190 | { |
| 191 | /* XXX currently this code assumes that XML_Char is 8-bit, |
| 192 | and hence in UTF-8. */ |
| 193 | /* UTF-8 from Expat, UTF-8 desired */ |
| 194 | if (str == NULL) { |
| 195 | Py_INCREF(Py_None); |
| 196 | return Py_None; |
| 197 | } |
| 198 | return PyString_FromString((const char *)str); |
Andrew M. Kuchling | beba056 | 2000-06-27 00:33:30 +0000 | [diff] [blame] | 199 | } |
| 200 | |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 201 | static PyObject * |
| 202 | conv_string_len_to_utf8(const XML_Char *str, int len) |
Andrew M. Kuchling | beba056 | 2000-06-27 00:33:30 +0000 | [diff] [blame] | 203 | { |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 204 | /* XXX currently this code assumes that XML_Char is 8-bit, |
| 205 | and hence in UTF-8. */ |
| 206 | /* UTF-8 from Expat, UTF-8 desired */ |
| 207 | if (str == NULL) { |
| 208 | Py_INCREF(Py_None); |
| 209 | return Py_None; |
| 210 | } |
| 211 | return PyString_FromStringAndSize((const char *)str, len); |
Andrew M. Kuchling | beba056 | 2000-06-27 00:33:30 +0000 | [diff] [blame] | 212 | } |
| 213 | |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 214 | /* Callback routines */ |
| 215 | |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 216 | static void clear_handlers(xmlparseobject *self); |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 217 | |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 218 | static void |
| 219 | flag_error(xmlparseobject *self) |
| 220 | { |
| 221 | clear_handlers(self); |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 222 | } |
| 223 | |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 224 | #define RC_HANDLER(RC, NAME, PARAMS, INIT, PARAM_FORMAT, CONVERSION, \ |
Andrew M. Kuchling | beba056 | 2000-06-27 00:33:30 +0000 | [diff] [blame] | 225 | RETURN, GETUSERDATA) \ |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 226 | \ |
| 227 | static RC my_##NAME##Handler PARAMS {\ |
Andrew M. Kuchling | beba056 | 2000-06-27 00:33:30 +0000 | [diff] [blame] | 228 | xmlparseobject *self = GETUSERDATA ; \ |
| 229 | PyObject *args=NULL; \ |
| 230 | PyObject *rv=NULL; \ |
| 231 | INIT \ |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 232 | \ |
Andrew M. Kuchling | beba056 | 2000-06-27 00:33:30 +0000 | [diff] [blame] | 233 | if (self->handlers[NAME] \ |
| 234 | && self->handlers[NAME] != Py_None) { \ |
| 235 | args = Py_BuildValue PARAM_FORMAT ;\ |
| 236 | if (!args) return RETURN; \ |
| 237 | rv = PyEval_CallObject(self->handlers[NAME], args); \ |
| 238 | Py_DECREF(args); \ |
| 239 | if (rv == NULL) { \ |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 240 | flag_error(self); \ |
Andrew M. Kuchling | beba056 | 2000-06-27 00:33:30 +0000 | [diff] [blame] | 241 | return RETURN; \ |
| 242 | } \ |
| 243 | CONVERSION \ |
| 244 | Py_DECREF(rv); \ |
| 245 | } \ |
| 246 | return RETURN; \ |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 247 | } |
| 248 | |
Andrew M. Kuchling | beba056 | 2000-06-27 00:33:30 +0000 | [diff] [blame] | 249 | #if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6 |
| 250 | #define STRING_CONV_FUNC conv_string_to_utf8 |
| 251 | #else |
| 252 | /* Python 1.6 and later versions */ |
| 253 | #define STRING_CONV_FUNC (self->returns_unicode \ |
| 254 | ? conv_string_to_unicode : conv_string_to_utf8) |
| 255 | #endif |
Guido van Rossum | 5961f5a | 2000-03-31 16:18:11 +0000 | [diff] [blame] | 256 | |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 257 | #define VOID_HANDLER(NAME, PARAMS, PARAM_FORMAT) \ |
| 258 | RC_HANDLER(void, NAME, PARAMS, ;, PARAM_FORMAT, ;, ;,\ |
| 259 | (xmlparseobject *)userData) |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 260 | |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 261 | #define INT_HANDLER(NAME, PARAMS, PARAM_FORMAT)\ |
| 262 | RC_HANDLER(int, NAME, PARAMS, int rc=0;, PARAM_FORMAT, \ |
| 263 | rc = PyInt_AsLong(rv);, rc, \ |
| 264 | (xmlparseobject *)userData) |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 265 | |
Andrew M. Kuchling | beba056 | 2000-06-27 00:33:30 +0000 | [diff] [blame] | 266 | #if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6 |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 267 | VOID_HANDLER(StartElement, |
| 268 | (void *userData, const XML_Char *name, const XML_Char **atts), |
Andrew M. Kuchling | beba056 | 2000-06-27 00:33:30 +0000 | [diff] [blame] | 269 | ("(O&O&)", STRING_CONV_FUNC, name, |
| 270 | conv_atts_using_string, atts ) ) |
| 271 | #else |
| 272 | /* Python 1.6 and later */ |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 273 | VOID_HANDLER(StartElement, |
| 274 | (void *userData, const XML_Char *name, const XML_Char **atts), |
Andrew M. Kuchling | beba056 | 2000-06-27 00:33:30 +0000 | [diff] [blame] | 275 | ("(O&O&)", STRING_CONV_FUNC, name, |
| 276 | (self->returns_unicode |
| 277 | ? conv_atts_using_unicode |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 278 | : conv_atts_using_string), atts)) |
Andrew M. Kuchling | beba056 | 2000-06-27 00:33:30 +0000 | [diff] [blame] | 279 | #endif |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 280 | |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 281 | VOID_HANDLER(EndElement, |
| 282 | (void *userData, const XML_Char *name), |
| 283 | ("(O&)", STRING_CONV_FUNC, name)) |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 284 | |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 285 | VOID_HANDLER(ProcessingInstruction, |
Andrew M. Kuchling | beba056 | 2000-06-27 00:33:30 +0000 | [diff] [blame] | 286 | (void *userData, |
| 287 | const XML_Char *target, |
| 288 | const XML_Char *data), |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 289 | ("(O&O&)",STRING_CONV_FUNC,target, STRING_CONV_FUNC,data)) |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 290 | |
Andrew M. Kuchling | beba056 | 2000-06-27 00:33:30 +0000 | [diff] [blame] | 291 | #if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6 |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 292 | VOID_HANDLER(CharacterData, |
Andrew M. Kuchling | beba056 | 2000-06-27 00:33:30 +0000 | [diff] [blame] | 293 | (void *userData, const XML_Char *data, int len), |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 294 | ("(O)", conv_string_len_to_utf8(data,len))) |
Andrew M. Kuchling | beba056 | 2000-06-27 00:33:30 +0000 | [diff] [blame] | 295 | #else |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 296 | VOID_HANDLER(CharacterData, |
Andrew M. Kuchling | beba056 | 2000-06-27 00:33:30 +0000 | [diff] [blame] | 297 | (void *userData, const XML_Char *data, int len), |
| 298 | ("(O)", (self->returns_unicode |
| 299 | ? conv_string_len_to_unicode(data,len) |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 300 | : conv_string_len_to_utf8(data,len)))) |
Andrew M. Kuchling | beba056 | 2000-06-27 00:33:30 +0000 | [diff] [blame] | 301 | #endif |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 302 | |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 303 | VOID_HANDLER(UnparsedEntityDecl, |
Andrew M. Kuchling | beba056 | 2000-06-27 00:33:30 +0000 | [diff] [blame] | 304 | (void *userData, |
| 305 | const XML_Char *entityName, |
| 306 | const XML_Char *base, |
| 307 | const XML_Char *systemId, |
| 308 | const XML_Char *publicId, |
| 309 | const XML_Char *notationName), |
| 310 | ("(O&O&O&O&O&)", |
| 311 | STRING_CONV_FUNC,entityName, STRING_CONV_FUNC,base, |
| 312 | STRING_CONV_FUNC,systemId, STRING_CONV_FUNC,publicId, |
| 313 | STRING_CONV_FUNC,notationName)) |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 314 | |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 315 | VOID_HANDLER(NotationDecl, |
Andrew M. Kuchling | beba056 | 2000-06-27 00:33:30 +0000 | [diff] [blame] | 316 | (void *userData, |
| 317 | const XML_Char *notationName, |
| 318 | const XML_Char *base, |
| 319 | const XML_Char *systemId, |
| 320 | const XML_Char *publicId), |
| 321 | ("(O&O&O&O&)", |
| 322 | STRING_CONV_FUNC,notationName, STRING_CONV_FUNC,base, |
| 323 | STRING_CONV_FUNC,systemId, STRING_CONV_FUNC,publicId)) |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 324 | |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 325 | VOID_HANDLER(StartNamespaceDecl, |
Andrew M. Kuchling | beba056 | 2000-06-27 00:33:30 +0000 | [diff] [blame] | 326 | (void *userData, |
| 327 | const XML_Char *prefix, |
| 328 | const XML_Char *uri), |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 329 | ("(O&O&)", STRING_CONV_FUNC,prefix, STRING_CONV_FUNC,uri)) |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 330 | |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 331 | VOID_HANDLER(EndNamespaceDecl, |
Andrew M. Kuchling | beba056 | 2000-06-27 00:33:30 +0000 | [diff] [blame] | 332 | (void *userData, |
| 333 | const XML_Char *prefix), |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 334 | ("(O&)", STRING_CONV_FUNC,prefix)) |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 335 | |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 336 | VOID_HANDLER(Comment, |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 337 | (void *userData, const XML_Char *prefix), |
Andrew M. Kuchling | beba056 | 2000-06-27 00:33:30 +0000 | [diff] [blame] | 338 | ("(O&)", STRING_CONV_FUNC,prefix)) |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 339 | |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 340 | VOID_HANDLER(StartCdataSection, |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 341 | (void *userData), |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 342 | ("()")) |
Andrew M. Kuchling | beba056 | 2000-06-27 00:33:30 +0000 | [diff] [blame] | 343 | |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 344 | VOID_HANDLER(EndCdataSection, |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 345 | (void *userData), |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 346 | ("()")) |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 347 | |
Andrew M. Kuchling | beba056 | 2000-06-27 00:33:30 +0000 | [diff] [blame] | 348 | #if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6 |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 349 | VOID_HANDLER(Default, |
Andrew M. Kuchling | beba056 | 2000-06-27 00:33:30 +0000 | [diff] [blame] | 350 | (void *userData, const XML_Char *s, int len), |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 351 | ("(O)", conv_string_len_to_utf8(s,len))) |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 352 | |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 353 | VOID_HANDLER(DefaultHandlerExpand, |
Andrew M. Kuchling | beba056 | 2000-06-27 00:33:30 +0000 | [diff] [blame] | 354 | (void *userData, const XML_Char *s, int len), |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 355 | ("(O)", conv_string_len_to_utf8(s,len))) |
Andrew M. Kuchling | beba056 | 2000-06-27 00:33:30 +0000 | [diff] [blame] | 356 | #else |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 357 | VOID_HANDLER(Default, |
Andrew M. Kuchling | beba056 | 2000-06-27 00:33:30 +0000 | [diff] [blame] | 358 | (void *userData, const XML_Char *s, int len), |
| 359 | ("(O)", (self->returns_unicode |
| 360 | ? conv_string_len_to_unicode(s,len) |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 361 | : conv_string_len_to_utf8(s,len)))) |
Andrew M. Kuchling | beba056 | 2000-06-27 00:33:30 +0000 | [diff] [blame] | 362 | |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 363 | VOID_HANDLER(DefaultHandlerExpand, |
Andrew M. Kuchling | beba056 | 2000-06-27 00:33:30 +0000 | [diff] [blame] | 364 | (void *userData, const XML_Char *s, int len), |
| 365 | ("(O)", (self->returns_unicode |
| 366 | ? conv_string_len_to_unicode(s,len) |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 367 | : conv_string_len_to_utf8(s,len)))) |
Andrew M. Kuchling | beba056 | 2000-06-27 00:33:30 +0000 | [diff] [blame] | 368 | #endif |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 369 | |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 370 | INT_HANDLER(NotStandalone, |
Andrew M. Kuchling | beba056 | 2000-06-27 00:33:30 +0000 | [diff] [blame] | 371 | (void *userData), |
| 372 | ("()")) |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 373 | |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 374 | RC_HANDLER(int, ExternalEntityRef, |
Andrew M. Kuchling | beba056 | 2000-06-27 00:33:30 +0000 | [diff] [blame] | 375 | (XML_Parser parser, |
| 376 | const XML_Char *context, |
| 377 | const XML_Char *base, |
| 378 | const XML_Char *systemId, |
| 379 | const XML_Char *publicId), |
| 380 | int rc=0;, |
| 381 | ("(O&O&O&O&)", |
| 382 | STRING_CONV_FUNC,context, STRING_CONV_FUNC,base, |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 383 | STRING_CONV_FUNC,systemId, STRING_CONV_FUNC,publicId), |
| 384 | rc = PyInt_AsLong(rv);, rc, |
| 385 | XML_GetUserData(parser)) |
Andrew M. Kuchling | beba056 | 2000-06-27 00:33:30 +0000 | [diff] [blame] | 386 | |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 387 | |
| 388 | |
| 389 | /* File reading copied from cPickle */ |
| 390 | |
| 391 | #define UNLESS(E) if (!(E)) |
| 392 | |
| 393 | /* |
| 394 | static int |
| 395 | read_other(xmlparseobject *self, char **s, int n) { |
| 396 | PyObject *bytes=NULL, *str=NULL, *arg=NULL; |
| 397 | int res = -1; |
| 398 | |
| 399 | UNLESS(bytes = PyInt_FromLong(n)) { |
| 400 | if (!PyErr_Occurred()) |
| 401 | PyErr_SetNone(PyExc_EOFError); |
| 402 | |
| 403 | goto finally; |
| 404 | } |
| 405 | |
| 406 | UNLESS(arg) |
| 407 | UNLESS(arg = PyTuple_New(1)) |
| 408 | goto finally; |
| 409 | |
| 410 | Py_INCREF(bytes); |
| 411 | if (PyTuple_SetItem(arg, 0, bytes) < 0) |
| 412 | goto finally; |
| 413 | |
| 414 | UNLESS(str = PyObject_CallObject(self->read, arg)) |
| 415 | goto finally; |
| 416 | |
| 417 | *s = PyString_AsString(str); |
| 418 | |
| 419 | res = n; |
| 420 | |
| 421 | finally: |
| 422 | Py_XDECREF(arg); |
| 423 | Py_XDECREF(bytes); |
| 424 | |
| 425 | return res; |
| 426 | } |
| 427 | |
| 428 | */ |
| 429 | |
| 430 | |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 431 | |
| 432 | /* ---------------------------------------------------------------- */ |
| 433 | |
| 434 | static char xmlparse_Parse__doc__[] = |
Thomas Wouters | 3531730 | 2000-07-22 16:34:15 +0000 | [diff] [blame] | 435 | "Parse(data[, isfinal])\n\ |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 436 | Parse XML data. `isfinal' should be true at end of input."; |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 437 | |
| 438 | static PyObject * |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 439 | xmlparse_Parse(xmlparseobject *self, PyObject *args) |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 440 | { |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 441 | char *s; |
| 442 | int slen; |
| 443 | int isFinal = 0; |
| 444 | int rv; |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 445 | |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 446 | if (!PyArg_ParseTuple(args, "s#|i:Parse", &s, &slen, &isFinal)) |
| 447 | return NULL; |
| 448 | rv = XML_Parse(self->itself, s, slen, isFinal); |
| 449 | if (PyErr_Occurred()) { |
| 450 | return NULL; |
| 451 | } |
| 452 | else if (rv == 0) { |
| 453 | PyErr_Format(ErrorObject, "%.200s: line %i, column %i", |
| 454 | XML_ErrorString(XML_GetErrorCode(self->itself)), |
| 455 | XML_GetErrorLineNumber(self->itself), |
| 456 | XML_GetErrorColumnNumber(self->itself)); |
| 457 | return NULL; |
| 458 | } |
| 459 | return PyInt_FromLong(rv); |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | #define BUF_SIZE 2048 |
| 463 | |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 464 | static int |
| 465 | readinst(char *buf, int buf_size, PyObject *meth) |
| 466 | { |
| 467 | PyObject *arg = NULL; |
| 468 | PyObject *bytes = NULL; |
| 469 | PyObject *str = NULL; |
| 470 | int len = -1; |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 471 | |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 472 | UNLESS(bytes = PyInt_FromLong(buf_size)) { |
| 473 | if (!PyErr_Occurred()) |
| 474 | PyErr_SetNone(PyExc_EOFError); |
| 475 | goto finally; |
| 476 | } |
| 477 | UNLESS(arg) |
| 478 | UNLESS(arg = PyTuple_New(1)) |
| 479 | goto finally; |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 480 | |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 481 | if (PyTuple_SetItem(arg, 0, bytes) < 0) |
| 482 | goto finally; |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 483 | |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 484 | UNLESS(str = PyObject_CallObject(meth, arg)) |
| 485 | goto finally; |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 486 | |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 487 | /* XXX what to do if it returns a Unicode string? */ |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 488 | UNLESS(PyString_Check(str)) { |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 489 | PyErr_Format(PyExc_TypeError, |
| 490 | "read() did not return a string object (type=%.400s)", |
| 491 | str->ob_type->tp_name); |
| 492 | goto finally; |
| 493 | } |
| 494 | len = PyString_GET_SIZE(str); |
| 495 | if (len > buf_size) { |
| 496 | PyErr_Format(PyExc_ValueError, |
| 497 | "read() returned too much data: " |
| 498 | "%i bytes requested, %i returned", |
| 499 | buf_size, len); |
| 500 | Py_DECREF(str); |
| 501 | goto finally; |
| 502 | } |
| 503 | memcpy(buf, PyString_AsString(str), len); |
| 504 | Py_XDECREF(str); |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 505 | finally: |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 506 | Py_XDECREF(arg); |
| 507 | return len; |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 508 | } |
| 509 | |
| 510 | static char xmlparse_ParseFile__doc__[] = |
Thomas Wouters | 3531730 | 2000-07-22 16:34:15 +0000 | [diff] [blame] | 511 | "ParseFile(file)\n\ |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 512 | Parse XML data from file-like object."; |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 513 | |
| 514 | static PyObject * |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 515 | xmlparse_ParseFile(xmlparseobject *self, PyObject *args) |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 516 | { |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 517 | int rv = 1; |
| 518 | PyObject *f; |
| 519 | FILE *fp; |
| 520 | PyObject *readmethod = NULL; |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 521 | |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 522 | if (!PyArg_ParseTuple(args, "O:ParseFile", &f)) |
| 523 | return NULL; |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 524 | |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 525 | if (PyFile_Check(f)) { |
| 526 | fp = PyFile_AsFile(f); |
| 527 | } |
| 528 | else{ |
| 529 | fp = NULL; |
| 530 | UNLESS(readmethod = PyObject_GetAttrString(f, "read")) { |
| 531 | PyErr_Clear(); |
| 532 | PyErr_SetString(PyExc_TypeError, |
| 533 | "argument must have 'read' attribute"); |
| 534 | return 0; |
| 535 | } |
| 536 | } |
| 537 | for (;;) { |
| 538 | int bytes_read; |
| 539 | void *buf = XML_GetBuffer(self->itself, BUF_SIZE); |
| 540 | if (buf == NULL) |
| 541 | return PyErr_NoMemory(); |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 542 | |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 543 | if (fp) { |
| 544 | bytes_read = fread(buf, sizeof(char), BUF_SIZE, fp); |
| 545 | if (bytes_read < 0) { |
| 546 | PyErr_SetFromErrno(PyExc_IOError); |
| 547 | return NULL; |
| 548 | } |
| 549 | } |
| 550 | else { |
| 551 | bytes_read = readinst(buf, BUF_SIZE, readmethod); |
| 552 | if (bytes_read < 0) |
| 553 | return NULL; |
| 554 | } |
| 555 | rv = XML_ParseBuffer(self->itself, bytes_read, bytes_read == 0); |
| 556 | if (PyErr_Occurred()) |
| 557 | return NULL; |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 558 | |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 559 | if (!rv || bytes_read == 0) |
| 560 | break; |
| 561 | } |
| 562 | return Py_BuildValue("i", rv); |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 563 | } |
| 564 | |
| 565 | static char xmlparse_SetBase__doc__[] = |
Thomas Wouters | 3531730 | 2000-07-22 16:34:15 +0000 | [diff] [blame] | 566 | "SetBase(base_url)\n\ |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 567 | Set the base URL for the parser."; |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 568 | |
| 569 | static PyObject * |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 570 | xmlparse_SetBase(xmlparseobject *self, PyObject *args) |
| 571 | { |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 572 | char *base; |
| 573 | |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 574 | if (!PyArg_ParseTuple(args, "s:SetBase", &base)) |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 575 | return NULL; |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 576 | if (!XML_SetBase(self->itself, base)) { |
| 577 | return PyErr_NoMemory(); |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 578 | } |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 579 | Py_INCREF(Py_None); |
| 580 | return Py_None; |
| 581 | } |
| 582 | |
| 583 | static char xmlparse_GetBase__doc__[] = |
Thomas Wouters | 3531730 | 2000-07-22 16:34:15 +0000 | [diff] [blame] | 584 | "GetBase() -> url\n\ |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 585 | Return base URL string for the parser."; |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 586 | |
| 587 | static PyObject * |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 588 | xmlparse_GetBase(xmlparseobject *self, PyObject *args) |
| 589 | { |
| 590 | if (!PyArg_ParseTuple(args, ":GetBase")) |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 591 | return NULL; |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 592 | |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 593 | return Py_BuildValue("z", XML_GetBase(self->itself)); |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 594 | } |
| 595 | |
| 596 | static struct PyMethodDef xmlparse_methods[] = { |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 597 | {"Parse", (PyCFunction)xmlparse_Parse, |
| 598 | METH_VARARGS, xmlparse_Parse__doc__}, |
| 599 | {"ParseFile", (PyCFunction)xmlparse_ParseFile, |
| 600 | METH_VARARGS, xmlparse_ParseFile__doc__}, |
| 601 | {"SetBase", (PyCFunction)xmlparse_SetBase, |
| 602 | METH_VARARGS, xmlparse_SetBase__doc__}, |
| 603 | {"GetBase", (PyCFunction)xmlparse_GetBase, |
| 604 | METH_VARARGS, xmlparse_GetBase__doc__}, |
Andrew M. Kuchling | beba056 | 2000-06-27 00:33:30 +0000 | [diff] [blame] | 605 | {NULL, NULL} /* sentinel */ |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 606 | }; |
| 607 | |
| 608 | /* ---------- */ |
| 609 | |
| 610 | |
| 611 | static xmlparseobject * |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 612 | newxmlparseobject(char *encoding, char *namespace_separator) |
| 613 | { |
| 614 | int i; |
| 615 | xmlparseobject *self; |
Andrew M. Kuchling | beba056 | 2000-06-27 00:33:30 +0000 | [diff] [blame] | 616 | |
| 617 | #if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6 |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 618 | self = PyObject_NEW(xmlparseobject, &Xmlparsetype); |
| 619 | if (self == NULL) |
| 620 | return NULL; |
Andrew M. Kuchling | beba056 | 2000-06-27 00:33:30 +0000 | [diff] [blame] | 621 | |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 622 | self->returns_unicode = 0; |
Andrew M. Kuchling | beba056 | 2000-06-27 00:33:30 +0000 | [diff] [blame] | 623 | #else |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 624 | /* Code for versions 1.6 and later */ |
| 625 | self = PyObject_New(xmlparseobject, &Xmlparsetype); |
| 626 | if (self == NULL) |
| 627 | return NULL; |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 628 | |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 629 | self->returns_unicode = 1; |
Andrew M. Kuchling | beba056 | 2000-06-27 00:33:30 +0000 | [diff] [blame] | 630 | #endif |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 631 | if (namespace_separator) { |
| 632 | self->itself = XML_ParserCreateNS(encoding, *namespace_separator); |
| 633 | } |
| 634 | else{ |
| 635 | self->itself = XML_ParserCreate(encoding); |
| 636 | } |
| 637 | if (self->itself == NULL) { |
| 638 | PyErr_SetString(PyExc_RuntimeError, |
| 639 | "XML_ParserCreate failed"); |
| 640 | Py_DECREF(self); |
| 641 | return NULL; |
| 642 | } |
| 643 | XML_SetUserData(self->itself, (void *)self); |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 644 | |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 645 | for(i = 0; handler_info[i].name != NULL; i++) |
| 646 | /* do nothing */; |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 647 | |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 648 | self->handlers = malloc(sizeof(PyObject *)*i); |
| 649 | clear_handlers(self); |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 650 | |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 651 | return self; |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 652 | } |
| 653 | |
| 654 | |
| 655 | static void |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 656 | xmlparse_dealloc(xmlparseobject *self) |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 657 | { |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 658 | int i; |
| 659 | if (self->itself) |
| 660 | XML_ParserFree(self->itself); |
| 661 | self->itself = NULL; |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 662 | |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 663 | for (i=0; handler_info[i].name != NULL; i++) { |
| 664 | Py_XDECREF(self->handlers[i]); |
| 665 | } |
Andrew M. Kuchling | beba056 | 2000-06-27 00:33:30 +0000 | [diff] [blame] | 666 | #if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6 |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 667 | /* Code for versions before 1.6 */ |
| 668 | free(self); |
Andrew M. Kuchling | beba056 | 2000-06-27 00:33:30 +0000 | [diff] [blame] | 669 | #else |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 670 | /* Code for versions 1.6 and later */ |
| 671 | PyObject_Del(self); |
Andrew M. Kuchling | beba056 | 2000-06-27 00:33:30 +0000 | [diff] [blame] | 672 | #endif |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 673 | } |
| 674 | |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 675 | static int |
| 676 | handlername2int(const char *name) |
| 677 | { |
| 678 | int i; |
| 679 | for (i=0; handler_info[i].name != NULL; i++) { |
| 680 | if (strcmp(name, handler_info[i].name) == 0) { |
| 681 | return i; |
| 682 | } |
| 683 | } |
| 684 | return -1; |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 685 | } |
| 686 | |
| 687 | static PyObject * |
| 688 | xmlparse_getattr(xmlparseobject *self, char *name) |
| 689 | { |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 690 | int handlernum; |
| 691 | if (strcmp(name, "ErrorCode") == 0) |
| 692 | return Py_BuildValue("l", |
| 693 | (long)XML_GetErrorCode(self->itself)); |
| 694 | if (strcmp(name, "ErrorLineNumber") == 0) |
| 695 | return Py_BuildValue("l", |
| 696 | (long)XML_GetErrorLineNumber(self->itself)); |
| 697 | if (strcmp(name, "ErrorColumnNumber") == 0) |
| 698 | return Py_BuildValue("l", |
| 699 | (long)XML_GetErrorColumnNumber(self->itself)); |
| 700 | if (strcmp(name, "ErrorByteIndex") == 0) |
| 701 | return Py_BuildValue("l", |
| 702 | XML_GetErrorByteIndex(self->itself)); |
| 703 | if (strcmp(name, "returns_unicode") == 0) |
| 704 | return Py_BuildValue("i", self->returns_unicode); |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 705 | |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 706 | handlernum = handlername2int(name); |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 707 | |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 708 | if (handlernum != -1 && self->handlers[handlernum] != NULL) { |
| 709 | Py_INCREF(self->handlers[handlernum]); |
| 710 | return self->handlers[handlernum]; |
| 711 | } |
| 712 | if (strcmp(name, "__members__") == 0) { |
| 713 | int i; |
| 714 | PyObject *rc = PyList_New(0); |
| 715 | for(i = 0; handler_info[i].name!=NULL; i++) { |
| 716 | PyList_Append(rc, |
| 717 | PyString_FromString(handler_info[i].name)); |
| 718 | } |
| 719 | PyList_Append(rc, PyString_FromString("ErrorCode")); |
| 720 | PyList_Append(rc, PyString_FromString("ErrorLineNumber")); |
| 721 | PyList_Append(rc, PyString_FromString("ErrorColumnNumber")); |
| 722 | PyList_Append(rc, PyString_FromString("ErrorByteIndex")); |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 723 | |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 724 | return rc; |
| 725 | } |
| 726 | return Py_FindMethod(xmlparse_methods, (PyObject *)self, name); |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 727 | } |
| 728 | |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 729 | static int |
| 730 | sethandler(xmlparseobject *self, const char *name, PyObject* v) |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 731 | { |
| 732 | int handlernum = handlername2int(name); |
| 733 | if (handlernum != -1) { |
| 734 | Py_INCREF(v); |
| 735 | Py_XDECREF(self->handlers[handlernum]); |
| 736 | self->handlers[handlernum] = v; |
| 737 | handler_info[handlernum].setter(self->itself, |
| 738 | handler_info[handlernum].handler); |
| 739 | return 1; |
| 740 | } |
| 741 | return 0; |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 742 | } |
| 743 | |
| 744 | static int |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 745 | xmlparse_setattr(xmlparseobject *self, char *name, PyObject *v) |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 746 | { |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 747 | /* Set attribute 'name' to value 'v'. v==NULL means delete */ |
| 748 | if (v==NULL) { |
| 749 | PyErr_SetString(PyExc_RuntimeError, "Cannot delete attribute"); |
| 750 | return -1; |
| 751 | } |
| 752 | if (strcmp(name, "returns_unicode") == 0) { |
| 753 | PyObject *intobj = PyNumber_Int(v); |
| 754 | if (intobj == NULL) return -1; |
| 755 | if (PyInt_AsLong(intobj)) { |
Andrew M. Kuchling | beba056 | 2000-06-27 00:33:30 +0000 | [diff] [blame] | 756 | #if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6 |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 757 | PyErr_SetString(PyExc_ValueError, |
| 758 | "Cannot return Unicode strings in Python 1.5"); |
| 759 | return -1; |
Andrew M. Kuchling | beba056 | 2000-06-27 00:33:30 +0000 | [diff] [blame] | 760 | #else |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 761 | self->returns_unicode = 1; |
Andrew M. Kuchling | beba056 | 2000-06-27 00:33:30 +0000 | [diff] [blame] | 762 | #endif |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 763 | } |
| 764 | else |
| 765 | self->returns_unicode = 0; |
| 766 | Py_DECREF(intobj); |
| 767 | return 0; |
| 768 | } |
| 769 | if (sethandler(self, name, v)) { |
| 770 | return 0; |
| 771 | } |
| 772 | PyErr_SetString(PyExc_AttributeError, name); |
| 773 | return -1; |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 774 | } |
| 775 | |
| 776 | static char Xmlparsetype__doc__[] = |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 777 | "XML parser"; |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 778 | |
| 779 | static PyTypeObject Xmlparsetype = { |
Andrew M. Kuchling | beba056 | 2000-06-27 00:33:30 +0000 | [diff] [blame] | 780 | PyObject_HEAD_INIT(NULL) |
| 781 | 0, /*ob_size*/ |
| 782 | "xmlparser", /*tp_name*/ |
| 783 | sizeof(xmlparseobject), /*tp_basicsize*/ |
| 784 | 0, /*tp_itemsize*/ |
| 785 | /* methods */ |
| 786 | (destructor)xmlparse_dealloc, /*tp_dealloc*/ |
| 787 | (printfunc)0, /*tp_print*/ |
| 788 | (getattrfunc)xmlparse_getattr, /*tp_getattr*/ |
| 789 | (setattrfunc)xmlparse_setattr, /*tp_setattr*/ |
| 790 | (cmpfunc)0, /*tp_compare*/ |
| 791 | (reprfunc)0, /*tp_repr*/ |
| 792 | 0, /*tp_as_number*/ |
| 793 | 0, /*tp_as_sequence*/ |
| 794 | 0, /*tp_as_mapping*/ |
| 795 | (hashfunc)0, /*tp_hash*/ |
| 796 | (ternaryfunc)0, /*tp_call*/ |
| 797 | (reprfunc)0, /*tp_str*/ |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 798 | |
Andrew M. Kuchling | beba056 | 2000-06-27 00:33:30 +0000 | [diff] [blame] | 799 | /* Space for future expansion */ |
| 800 | 0L,0L,0L,0L, |
| 801 | Xmlparsetype__doc__ /* Documentation string */ |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 802 | }; |
| 803 | |
| 804 | /* End of code for xmlparser objects */ |
| 805 | /* -------------------------------------------------------- */ |
| 806 | |
| 807 | |
| 808 | static char pyexpat_ParserCreate__doc__[] = |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 809 | "ParserCreate([encoding[, namespace_separator]]) -> parser\n\ |
| 810 | Return a new XML parser object."; |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 811 | |
| 812 | static PyObject * |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 813 | pyexpat_ParserCreate(PyObject *notused, PyObject *args, PyObject *kw) |
| 814 | { |
| 815 | char *encoding = NULL; |
| 816 | char *namespace_separator = NULL; |
Andrew M. Kuchling | beba056 | 2000-06-27 00:33:30 +0000 | [diff] [blame] | 817 | static char *kwlist[] = {"encoding", "namespace_separator", NULL}; |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 818 | |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 819 | if (!PyArg_ParseTupleAndKeywords(args, kw, "|zz:ParserCreate", kwlist, |
Andrew M. Kuchling | beba056 | 2000-06-27 00:33:30 +0000 | [diff] [blame] | 820 | &encoding, &namespace_separator)) |
| 821 | return NULL; |
| 822 | return (PyObject *)newxmlparseobject(encoding, namespace_separator); |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 823 | } |
| 824 | |
| 825 | static char pyexpat_ErrorString__doc__[] = |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 826 | "ErrorString(errno) -> string\n\ |
| 827 | Returns string error for given number."; |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 828 | |
| 829 | static PyObject * |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 830 | pyexpat_ErrorString(PyObject *self, PyObject *args) |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 831 | { |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 832 | long code = 0; |
| 833 | |
| 834 | if (!PyArg_ParseTuple(args, "l:ErrorString", &code)) |
| 835 | return NULL; |
| 836 | return Py_BuildValue("z", XML_ErrorString((int)code)); |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 837 | } |
| 838 | |
| 839 | /* List of methods defined in the module */ |
| 840 | |
| 841 | static struct PyMethodDef pyexpat_methods[] = { |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 842 | {"ParserCreate", (PyCFunction)pyexpat_ParserCreate, |
| 843 | METH_VARARGS|METH_KEYWORDS, pyexpat_ParserCreate__doc__}, |
| 844 | {"ErrorString", (PyCFunction)pyexpat_ErrorString, |
| 845 | METH_VARARGS, pyexpat_ErrorString__doc__}, |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 846 | |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 847 | {NULL, (PyCFunction)NULL, 0, NULL} /* sentinel */ |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 848 | }; |
| 849 | |
Andrew M. Kuchling | beba056 | 2000-06-27 00:33:30 +0000 | [diff] [blame] | 850 | /* Module docstring */ |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 851 | |
| 852 | static char pyexpat_module_documentation[] = |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 853 | "Python wrapper for Expat parser."; |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 854 | |
Andrew M. Kuchling | beba056 | 2000-06-27 00:33:30 +0000 | [diff] [blame] | 855 | /* Initialization function for the module */ |
| 856 | |
Tim Peters | 6d7c442 | 2000-08-26 07:38:06 +0000 | [diff] [blame] | 857 | DL_EXPORT(void) initpyexpat(void); /* supply a prototype */ |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 858 | |
| 859 | DL_EXPORT(void) |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 860 | initpyexpat(void) |
| 861 | { |
| 862 | PyObject *m, *d; |
| 863 | char *rev = "$Revision$"; |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 864 | PyObject *errmod_name = PyString_FromString("pyexpat.errors"); |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 865 | PyObject *errors_module, *errors_dict; |
| 866 | PyObject *sys_modules; |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 867 | |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 868 | if (errmod_name == NULL) |
| 869 | return; |
| 870 | |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 871 | Xmlparsetype.ob_type = &PyType_Type; |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 872 | |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 873 | /* Create the module and add the functions */ |
| 874 | m = Py_InitModule4("pyexpat", pyexpat_methods, |
| 875 | pyexpat_module_documentation, |
| 876 | (PyObject*)NULL, PYTHON_API_VERSION); |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 877 | |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 878 | /* Add some symbolic constants to the module */ |
| 879 | d = PyModule_GetDict(m); |
Fred Drake | c23b523 | 2000-08-24 21:57:43 +0000 | [diff] [blame] | 880 | if (ErrorObject == NULL) |
| 881 | ErrorObject = PyErr_NewException("pyexpat.error", NULL, NULL); |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 882 | PyDict_SetItemString(d, "error", ErrorObject); |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 883 | |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 884 | PyDict_SetItemString(d, "__version__", |
| 885 | PyString_FromStringAndSize(rev+11, |
| 886 | strlen(rev+11)-2)); |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 887 | |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 888 | /* XXX When Expat supports some way of figuring out how it was |
| 889 | compiled, this should check and set native_encoding |
| 890 | appropriately. |
| 891 | */ |
| 892 | PyDict_SetItemString(d, "native_encoding", |
| 893 | PyString_FromString("UTF-8")); |
Fred Drake | c23b523 | 2000-08-24 21:57:43 +0000 | [diff] [blame] | 894 | |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 895 | errors_module = PyDict_GetItem(d, errmod_name); |
| 896 | if (errors_module == NULL) { |
| 897 | errors_module = PyModule_New("pyexpat.errors"); |
| 898 | if (errors_module != NULL) { |
| 899 | sys_modules = PySys_GetObject("modules"); |
| 900 | PyDict_SetItemString(d, "errors", errors_module); |
| 901 | PyDict_SetItem(sys_modules, errmod_name, errors_module); |
Fred Drake | c23b523 | 2000-08-24 21:57:43 +0000 | [diff] [blame] | 902 | } |
| 903 | } |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 904 | Py_DECREF(errmod_name); |
| 905 | if (errors_module == NULL) |
| 906 | /* Don't code dump later! */ |
| 907 | return; |
| 908 | |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 909 | errors_dict = PyModule_GetDict(errors_module); |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 910 | |
| 911 | #define MYCONST(name) \ |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 912 | PyDict_SetItemString(errors_dict, #name, \ |
| 913 | PyString_FromString(XML_ErrorString(name))) |
Fred Drake | 7bd9f41 | 2000-07-04 23:51:31 +0000 | [diff] [blame] | 914 | |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 915 | MYCONST(XML_ERROR_NO_MEMORY); |
| 916 | MYCONST(XML_ERROR_SYNTAX); |
| 917 | MYCONST(XML_ERROR_NO_ELEMENTS); |
| 918 | MYCONST(XML_ERROR_INVALID_TOKEN); |
| 919 | MYCONST(XML_ERROR_UNCLOSED_TOKEN); |
| 920 | MYCONST(XML_ERROR_PARTIAL_CHAR); |
| 921 | MYCONST(XML_ERROR_TAG_MISMATCH); |
| 922 | MYCONST(XML_ERROR_DUPLICATE_ATTRIBUTE); |
| 923 | MYCONST(XML_ERROR_JUNK_AFTER_DOC_ELEMENT); |
| 924 | MYCONST(XML_ERROR_PARAM_ENTITY_REF); |
| 925 | MYCONST(XML_ERROR_UNDEFINED_ENTITY); |
| 926 | MYCONST(XML_ERROR_RECURSIVE_ENTITY_REF); |
| 927 | MYCONST(XML_ERROR_ASYNC_ENTITY); |
| 928 | MYCONST(XML_ERROR_BAD_CHAR_REF); |
| 929 | MYCONST(XML_ERROR_BINARY_ENTITY_REF); |
| 930 | MYCONST(XML_ERROR_ATTRIBUTE_EXTERNAL_ENTITY_REF); |
| 931 | MYCONST(XML_ERROR_MISPLACED_XML_PI); |
| 932 | MYCONST(XML_ERROR_UNKNOWN_ENCODING); |
| 933 | MYCONST(XML_ERROR_INCORRECT_ENCODING); |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 934 | } |
| 935 | |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 936 | static void |
| 937 | clear_handlers(xmlparseobject *self) |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 938 | { |
| 939 | int i = 0; |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 940 | |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 941 | for (; handler_info[i].name!=NULL; i++) { |
| 942 | self->handlers[i]=NULL; |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 943 | handler_info[i].setter(self->itself, NULL); |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 944 | } |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 945 | } |
| 946 | |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 947 | typedef void (*pairsetter)(XML_Parser, void *handler1, void *handler2); |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 948 | |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 949 | static void |
| 950 | pyxml_UpdatePairedHandlers(xmlparseobject *self, |
| 951 | int startHandler, |
| 952 | int endHandler, |
| 953 | pairsetter setter) |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 954 | { |
| 955 | void *start_handler=NULL; |
| 956 | void *end_handler=NULL; |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 957 | |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 958 | if (self->handlers[startHandler] |
| 959 | && self->handlers[endHandler]!=Py_None) { |
| 960 | start_handler=handler_info[startHandler].handler; |
| 961 | } |
| 962 | if (self->handlers[EndElement] |
| 963 | && self->handlers[EndElement] !=Py_None) { |
| 964 | end_handler=handler_info[endHandler].handler; |
| 965 | } |
| 966 | setter(self->itself, start_handler, end_handler); |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 967 | } |
| 968 | |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 969 | static void |
| 970 | pyxml_SetStartElementHandler(XML_Parser *parser, void *junk) |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 971 | { |
| 972 | pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser), |
| 973 | StartElement, EndElement, |
| 974 | (pairsetter)XML_SetElementHandler); |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 975 | } |
| 976 | |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 977 | static void |
| 978 | pyxml_SetEndElementHandler(XML_Parser *parser, void *junk) |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 979 | { |
| 980 | pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser), |
| 981 | StartElement, EndElement, |
| 982 | (pairsetter)XML_SetElementHandler); |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 983 | } |
| 984 | |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 985 | static void |
| 986 | pyxml_SetStartNamespaceDeclHandler(XML_Parser *parser, void *junk) |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 987 | { |
| 988 | pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser), |
| 989 | StartNamespaceDecl, EndNamespaceDecl, |
| 990 | (pairsetter)XML_SetNamespaceDeclHandler); |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 991 | } |
| 992 | |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 993 | static void |
| 994 | pyxml_SetEndNamespaceDeclHandler(XML_Parser *parser, void *junk) |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 995 | { |
| 996 | pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser), |
| 997 | StartNamespaceDecl, EndNamespaceDecl, |
| 998 | (pairsetter)XML_SetNamespaceDeclHandler); |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 999 | } |
| 1000 | |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 1001 | static void |
| 1002 | pyxml_SetStartCdataSection(XML_Parser *parser, void *junk) |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 1003 | { |
| 1004 | pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser), |
| 1005 | StartCdataSection, EndCdataSection, |
| 1006 | (pairsetter)XML_SetCdataSectionHandler); |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 1007 | } |
| 1008 | |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 1009 | static void |
| 1010 | pyxml_SetEndCdataSection(XML_Parser *parser, void *junk) |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 1011 | { |
| 1012 | pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser), |
| 1013 | StartCdataSection, EndCdataSection, |
| 1014 | (pairsetter)XML_SetCdataSectionHandler); |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 1015 | } |
| 1016 | |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 1017 | statichere struct HandlerInfo handler_info[] = { |
| 1018 | {"StartElementHandler", |
| 1019 | pyxml_SetStartElementHandler, |
| 1020 | (xmlhandler)my_StartElementHandler}, |
| 1021 | {"EndElementHandler", |
| 1022 | pyxml_SetEndElementHandler, |
| 1023 | (xmlhandler)my_EndElementHandler}, |
| 1024 | {"ProcessingInstructionHandler", |
| 1025 | (xmlhandlersetter)XML_SetProcessingInstructionHandler, |
| 1026 | (xmlhandler)my_ProcessingInstructionHandler}, |
| 1027 | {"CharacterDataHandler", |
| 1028 | (xmlhandlersetter)XML_SetCharacterDataHandler, |
| 1029 | (xmlhandler)my_CharacterDataHandler}, |
| 1030 | {"UnparsedEntityDeclHandler", |
| 1031 | (xmlhandlersetter)XML_SetUnparsedEntityDeclHandler, |
| 1032 | (xmlhandler)my_UnparsedEntityDeclHandler }, |
| 1033 | {"NotationDeclHandler", |
| 1034 | (xmlhandlersetter)XML_SetNotationDeclHandler, |
| 1035 | (xmlhandler)my_NotationDeclHandler }, |
| 1036 | {"StartNamespaceDeclHandler", |
| 1037 | pyxml_SetStartNamespaceDeclHandler, |
| 1038 | (xmlhandler)my_StartNamespaceDeclHandler }, |
| 1039 | {"EndNamespaceDeclHandler", |
| 1040 | pyxml_SetEndNamespaceDeclHandler, |
| 1041 | (xmlhandler)my_EndNamespaceDeclHandler }, |
| 1042 | {"CommentHandler", |
| 1043 | (xmlhandlersetter)XML_SetCommentHandler, |
| 1044 | (xmlhandler)my_CommentHandler}, |
| 1045 | {"StartCdataSectionHandler", |
| 1046 | pyxml_SetStartCdataSection, |
| 1047 | (xmlhandler)my_StartCdataSectionHandler}, |
| 1048 | {"EndCdataSectionHandler", |
| 1049 | pyxml_SetEndCdataSection, |
| 1050 | (xmlhandler)my_EndCdataSectionHandler}, |
| 1051 | {"DefaultHandler", |
| 1052 | (xmlhandlersetter)XML_SetDefaultHandler, |
| 1053 | (xmlhandler)my_DefaultHandler}, |
| 1054 | {"DefaultHandlerExpand", |
| 1055 | (xmlhandlersetter)XML_SetDefaultHandlerExpand, |
| 1056 | (xmlhandler)my_DefaultHandlerExpandHandler}, |
| 1057 | {"NotStandaloneHandler", |
| 1058 | (xmlhandlersetter)XML_SetNotStandaloneHandler, |
| 1059 | (xmlhandler)my_NotStandaloneHandler}, |
| 1060 | {"ExternalEntityRefHandler", |
| 1061 | (xmlhandlersetter)XML_SetExternalEntityRefHandler, |
| 1062 | (xmlhandler)my_ExternalEntityRefHandler }, |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 1063 | |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 1064 | {NULL, NULL, NULL} /* sentinel */ |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 1065 | }; |