Daniel Veillard | 3ce5257 | 2002-02-03 15:08:05 +0000 | [diff] [blame] | 1 | /* |
| 2 | * libxml.c: this modules implements the main part of the glue of the |
| 3 | * libxml2 library and the Python interpreter. It provides the |
| 4 | * entry points where an automatically generated stub is either |
| 5 | * unpractical or would not match cleanly the Python model. |
| 6 | * |
Daniel Veillard | 0fea6f4 | 2002-02-22 22:51:13 +0000 | [diff] [blame] | 7 | * If compiled with MERGED_MODULES, the entry point will be used to |
| 8 | * initialize both the libxml2 and the libxslt wrappers |
| 9 | * |
Daniel Veillard | 3ce5257 | 2002-02-03 15:08:05 +0000 | [diff] [blame] | 10 | * See Copyright for the status of this software. |
| 11 | * |
| 12 | * daniel@veillard.com |
| 13 | */ |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 14 | #include <Python.h> |
Daniel Veillard | c6d4a93 | 2002-09-12 15:00:57 +0000 | [diff] [blame] | 15 | #include <fileobject.h> |
Daniel Veillard | a1196ed | 2002-11-23 11:22:49 +0000 | [diff] [blame] | 16 | /* #include "config.h" */ |
Daniel Veillard | f1d0e6b | 2002-01-31 23:42:44 +0000 | [diff] [blame] | 17 | #include <libxml/xmlmemory.h> |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 18 | #include <libxml/parser.h> |
| 19 | #include <libxml/tree.h> |
Daniel Veillard | a7340c8 | 2002-02-01 17:56:45 +0000 | [diff] [blame] | 20 | #include <libxml/xpath.h> |
Daniel Veillard | 5d81903 | 2002-02-02 21:49:17 +0000 | [diff] [blame] | 21 | #include <libxml/xmlerror.h> |
Daniel Veillard | a7340c8 | 2002-02-01 17:56:45 +0000 | [diff] [blame] | 22 | #include <libxml/xpathInternals.h> |
Daniel Veillard | 4e1b26c | 2002-02-03 20:13:06 +0000 | [diff] [blame] | 23 | #include <libxml/xmlmemory.h> |
Daniel Veillard | c6d4a93 | 2002-09-12 15:00:57 +0000 | [diff] [blame] | 24 | #include <libxml/xmlIO.h> |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 25 | #include "libxml_wrap.h" |
Daniel Veillard | 96fe095 | 2002-01-30 20:52:23 +0000 | [diff] [blame] | 26 | #include "libxml2-py.h" |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 27 | |
Daniel Veillard | 0d132cf | 2002-12-23 14:43:32 +0000 | [diff] [blame] | 28 | #if (defined(_MSC_VER) || defined(__MINGW32__)) && !defined(vsnprintf) |
| 29 | #define vsnprintf(b,c,f,a) _vsnprintf(b,c,f,a) |
Daniel Veillard | e4fa293 | 2003-03-26 00:38:10 +0000 | [diff] [blame] | 30 | #elif defined(WITH_TRIO) |
| 31 | #include "trio.h" |
| 32 | #define vsnprintf trio_vsnprintf |
Daniel Veillard | 0d132cf | 2002-12-23 14:43:32 +0000 | [diff] [blame] | 33 | #endif |
| 34 | |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 35 | /* #define DEBUG */ |
Daniel Veillard | 797a565 | 2002-02-12 13:46:21 +0000 | [diff] [blame] | 36 | /* #define DEBUG_SAX */ |
Daniel Veillard | a7340c8 | 2002-02-01 17:56:45 +0000 | [diff] [blame] | 37 | /* #define DEBUG_XPATH */ |
Daniel Veillard | 5d81903 | 2002-02-02 21:49:17 +0000 | [diff] [blame] | 38 | /* #define DEBUG_ERROR */ |
Daniel Veillard | 4e1b26c | 2002-02-03 20:13:06 +0000 | [diff] [blame] | 39 | /* #define DEBUG_MEMORY */ |
Daniel Veillard | c6d4a93 | 2002-09-12 15:00:57 +0000 | [diff] [blame] | 40 | /* #define DEBUG_FILES */ |
| 41 | /* #define DEBUG_LOADER */ |
Daniel Veillard | 4e1b26c | 2002-02-03 20:13:06 +0000 | [diff] [blame] | 42 | |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 43 | void initlibxml2mod(void); |
| 44 | |
Daniel Veillard | c6d4a93 | 2002-09-12 15:00:57 +0000 | [diff] [blame] | 45 | /** |
| 46 | * TODO: |
| 47 | * |
| 48 | * macro to flag unimplemented blocks |
| 49 | */ |
| 50 | #define TODO \ |
| 51 | xmlGenericError(xmlGenericErrorContext, \ |
| 52 | "Unimplemented block at %s:%d\n", \ |
| 53 | __FILE__, __LINE__); |
| 54 | |
Daniel Veillard | 4e1b26c | 2002-02-03 20:13:06 +0000 | [diff] [blame] | 55 | /************************************************************************ |
| 56 | * * |
| 57 | * Memory debug interface * |
| 58 | * * |
| 59 | ************************************************************************/ |
| 60 | |
| 61 | extern void xmlMemFree(void *ptr); |
| 62 | extern void *xmlMemMalloc(size_t size); |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 63 | extern void *xmlMemRealloc(void *ptr, size_t size); |
Daniel Veillard | 4e1b26c | 2002-02-03 20:13:06 +0000 | [diff] [blame] | 64 | extern char *xmlMemoryStrdup(const char *str); |
| 65 | |
| 66 | static int libxmlMemoryDebugActivated = 0; |
| 67 | static long libxmlMemoryAllocatedBase = 0; |
| 68 | |
| 69 | static int libxmlMemoryDebug = 0; |
| 70 | static xmlFreeFunc freeFunc = NULL; |
| 71 | static xmlMallocFunc mallocFunc = NULL; |
| 72 | static xmlReallocFunc reallocFunc = NULL; |
| 73 | static xmlStrdupFunc strdupFunc = NULL; |
| 74 | |
| 75 | PyObject * |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 76 | libxml_xmlDebugMemory(ATTRIBUTE_UNUSED PyObject * self, PyObject * args) |
| 77 | { |
Daniel Veillard | 4e1b26c | 2002-02-03 20:13:06 +0000 | [diff] [blame] | 78 | int activate; |
| 79 | PyObject *py_retval; |
| 80 | long ret; |
| 81 | |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 82 | if (!PyArg_ParseTuple(args, (char *) "i:xmlDebugMemory", &activate)) |
| 83 | return (NULL); |
Daniel Veillard | 4e1b26c | 2002-02-03 20:13:06 +0000 | [diff] [blame] | 84 | |
| 85 | #ifdef DEBUG_MEMORY |
| 86 | printf("libxml_xmlDebugMemory(%d) called\n", activate); |
| 87 | #endif |
| 88 | |
| 89 | if (activate != 0) { |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 90 | if (libxmlMemoryDebug == 0) { |
| 91 | /* |
| 92 | * First initialize the library and grab the old memory handlers |
| 93 | * and switch the library to memory debugging |
| 94 | */ |
| 95 | xmlMemGet((xmlFreeFunc *) & freeFunc, |
| 96 | (xmlMallocFunc *) & mallocFunc, |
| 97 | (xmlReallocFunc *) & reallocFunc, |
| 98 | (xmlStrdupFunc *) & strdupFunc); |
| 99 | if ((freeFunc == xmlMemFree) && (mallocFunc == xmlMemMalloc) && |
| 100 | (reallocFunc == xmlMemRealloc) && |
| 101 | (strdupFunc == xmlMemoryStrdup)) { |
| 102 | libxmlMemoryAllocatedBase = xmlMemUsed(); |
| 103 | } else { |
| 104 | ret = (long) xmlMemSetup(xmlMemFree, xmlMemMalloc, |
| 105 | xmlMemRealloc, xmlMemoryStrdup); |
| 106 | if (ret < 0) |
| 107 | goto error; |
| 108 | libxmlMemoryAllocatedBase = xmlMemUsed(); |
| 109 | } |
| 110 | xmlInitParser(); |
| 111 | ret = 0; |
| 112 | } else if (libxmlMemoryDebugActivated == 0) { |
| 113 | libxmlMemoryAllocatedBase = xmlMemUsed(); |
| 114 | ret = 0; |
| 115 | } else { |
| 116 | ret = xmlMemUsed() - libxmlMemoryAllocatedBase; |
| 117 | } |
| 118 | libxmlMemoryDebug = 1; |
| 119 | libxmlMemoryDebugActivated = 1; |
Daniel Veillard | 4e1b26c | 2002-02-03 20:13:06 +0000 | [diff] [blame] | 120 | } else { |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 121 | if (libxmlMemoryDebugActivated == 1) |
| 122 | ret = xmlMemUsed() - libxmlMemoryAllocatedBase; |
| 123 | else |
| 124 | ret = 0; |
| 125 | libxmlMemoryDebugActivated = 0; |
Daniel Veillard | 4e1b26c | 2002-02-03 20:13:06 +0000 | [diff] [blame] | 126 | } |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 127 | error: |
Daniel Veillard | 4e1b26c | 2002-02-03 20:13:06 +0000 | [diff] [blame] | 128 | py_retval = libxml_longWrap(ret); |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 129 | return (py_retval); |
Daniel Veillard | 4e1b26c | 2002-02-03 20:13:06 +0000 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | PyObject * |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 133 | libxml_xmlDumpMemory(ATTRIBUTE_UNUSED PyObject * self, |
| 134 | ATTRIBUTE_UNUSED PyObject * args) |
| 135 | { |
Daniel Veillard | 4e1b26c | 2002-02-03 20:13:06 +0000 | [diff] [blame] | 136 | |
| 137 | if (libxmlMemoryDebug != 0) |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 138 | xmlMemoryDump(); |
Daniel Veillard | 4e1b26c | 2002-02-03 20:13:06 +0000 | [diff] [blame] | 139 | Py_INCREF(Py_None); |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 140 | return (Py_None); |
Daniel Veillard | 4e1b26c | 2002-02-03 20:13:06 +0000 | [diff] [blame] | 141 | } |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 142 | |
| 143 | /************************************************************************ |
| 144 | * * |
Daniel Veillard | c6d4a93 | 2002-09-12 15:00:57 +0000 | [diff] [blame] | 145 | * Handling Python FILE I/O at the C level * |
| 146 | * The raw I/O attack diectly the File objects, while the * |
| 147 | * other routines address the ioWrapper instance instead * |
| 148 | * * |
| 149 | ************************************************************************/ |
| 150 | |
| 151 | /** |
| 152 | * xmlPythonFileCloseUnref: |
| 153 | * @context: the I/O context |
| 154 | * |
| 155 | * Close an I/O channel |
| 156 | */ |
| 157 | static int |
| 158 | xmlPythonFileCloseRaw (void * context) { |
| 159 | PyObject *file, *ret; |
| 160 | |
| 161 | #ifdef DEBUG_FILES |
| 162 | printf("xmlPythonFileCloseUnref\n"); |
| 163 | #endif |
| 164 | file = (PyObject *) context; |
| 165 | if (file == NULL) return(-1); |
Daniel Veillard | 118aed7 | 2002-09-24 14:13:13 +0000 | [diff] [blame] | 166 | ret = PyEval_CallMethod(file, (char *) "close", (char *) "()"); |
Daniel Veillard | c6d4a93 | 2002-09-12 15:00:57 +0000 | [diff] [blame] | 167 | if (ret != NULL) { |
| 168 | Py_DECREF(ret); |
| 169 | } |
| 170 | Py_DECREF(file); |
| 171 | return(0); |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * xmlPythonFileReadRaw: |
| 176 | * @context: the I/O context |
| 177 | * @buffer: where to drop data |
| 178 | * @len: number of bytes to write |
| 179 | * |
| 180 | * Read @len bytes to @buffer from the Python file in the I/O channel |
| 181 | * |
| 182 | * Returns the number of bytes read |
| 183 | */ |
| 184 | static int |
| 185 | xmlPythonFileReadRaw (void * context, char * buffer, int len) { |
| 186 | PyObject *file; |
| 187 | PyObject *ret; |
| 188 | int lenread = -1; |
| 189 | char *data; |
| 190 | |
| 191 | #ifdef DEBUG_FILES |
| 192 | printf("xmlPythonFileReadRaw: %d\n", len); |
| 193 | #endif |
| 194 | file = (PyObject *) context; |
| 195 | if (file == NULL) return(-1); |
Daniel Veillard | 118aed7 | 2002-09-24 14:13:13 +0000 | [diff] [blame] | 196 | ret = PyEval_CallMethod(file, (char *) "read", (char *) "(i)", len); |
Daniel Veillard | c6d4a93 | 2002-09-12 15:00:57 +0000 | [diff] [blame] | 197 | if (ret == NULL) { |
| 198 | printf("xmlPythonFileReadRaw: result is NULL\n"); |
| 199 | return(-1); |
| 200 | } else if (PyString_Check(ret)) { |
| 201 | lenread = PyString_Size(ret); |
| 202 | data = PyString_AsString(ret); |
| 203 | if (lenread > len) |
| 204 | memcpy(buffer, data, len); |
| 205 | else |
| 206 | memcpy(buffer, data, lenread); |
| 207 | Py_DECREF(ret); |
| 208 | } else { |
| 209 | printf("xmlPythonFileReadRaw: result is not a String\n"); |
| 210 | Py_DECREF(ret); |
| 211 | } |
| 212 | return(lenread); |
| 213 | } |
| 214 | |
| 215 | /** |
| 216 | * xmlPythonFileRead: |
| 217 | * @context: the I/O context |
| 218 | * @buffer: where to drop data |
| 219 | * @len: number of bytes to write |
| 220 | * |
| 221 | * Read @len bytes to @buffer from the I/O channel. |
| 222 | * |
| 223 | * Returns the number of bytes read |
| 224 | */ |
| 225 | static int |
| 226 | xmlPythonFileRead (void * context, char * buffer, int len) { |
| 227 | PyObject *file; |
| 228 | PyObject *ret; |
| 229 | int lenread = -1; |
| 230 | char *data; |
| 231 | |
| 232 | #ifdef DEBUG_FILES |
| 233 | printf("xmlPythonFileRead: %d\n", len); |
| 234 | #endif |
| 235 | file = (PyObject *) context; |
| 236 | if (file == NULL) return(-1); |
Daniel Veillard | 118aed7 | 2002-09-24 14:13:13 +0000 | [diff] [blame] | 237 | ret = PyEval_CallMethod(file, (char *) "io_read", (char *) "(i)", len); |
Daniel Veillard | c6d4a93 | 2002-09-12 15:00:57 +0000 | [diff] [blame] | 238 | if (ret == NULL) { |
| 239 | printf("xmlPythonFileRead: result is NULL\n"); |
| 240 | return(-1); |
| 241 | } else if (PyString_Check(ret)) { |
| 242 | lenread = PyString_Size(ret); |
| 243 | data = PyString_AsString(ret); |
| 244 | if (lenread > len) |
| 245 | memcpy(buffer, data, len); |
| 246 | else |
| 247 | memcpy(buffer, data, lenread); |
| 248 | Py_DECREF(ret); |
| 249 | } else { |
| 250 | printf("xmlPythonFileRead: result is not a String\n"); |
| 251 | Py_DECREF(ret); |
| 252 | } |
| 253 | return(lenread); |
| 254 | } |
| 255 | |
| 256 | /** |
| 257 | * xmlFileWrite: |
| 258 | * @context: the I/O context |
| 259 | * @buffer: where to drop data |
| 260 | * @len: number of bytes to write |
| 261 | * |
| 262 | * Write @len bytes from @buffer to the I/O channel. |
| 263 | * |
| 264 | * Returns the number of bytes written |
| 265 | */ |
| 266 | static int |
| 267 | xmlPythonFileWrite (void * context, const char * buffer, int len) { |
| 268 | PyObject *file; |
| 269 | PyObject *string; |
| 270 | PyObject *ret; |
| 271 | int written = -1; |
| 272 | |
| 273 | #ifdef DEBUG_FILES |
| 274 | printf("xmlPythonFileWrite: %d\n", len); |
| 275 | #endif |
| 276 | file = (PyObject *) context; |
| 277 | if (file == NULL) return(-1); |
| 278 | string = PyString_FromStringAndSize(buffer, len); |
| 279 | if (string == NULL) return(-1); |
Daniel Veillard | 118aed7 | 2002-09-24 14:13:13 +0000 | [diff] [blame] | 280 | ret = PyEval_CallMethod(file, (char *) "io_write", (char *) "(O)", string); |
Daniel Veillard | c6d4a93 | 2002-09-12 15:00:57 +0000 | [diff] [blame] | 281 | Py_DECREF(string); |
| 282 | if (ret == NULL) { |
| 283 | printf("xmlPythonFileWrite: result is NULL\n"); |
| 284 | return(-1); |
| 285 | } else if (PyInt_Check(ret)) { |
| 286 | written = (int) PyInt_AsLong(ret); |
| 287 | Py_DECREF(ret); |
| 288 | } else if (ret == Py_None) { |
| 289 | written = len; |
| 290 | Py_DECREF(ret); |
| 291 | } else { |
| 292 | printf("xmlPythonFileWrite: result is not an Int nor None\n"); |
| 293 | Py_DECREF(ret); |
| 294 | } |
| 295 | return(written); |
| 296 | } |
| 297 | |
| 298 | /** |
| 299 | * xmlPythonFileClose: |
| 300 | * @context: the I/O context |
| 301 | * |
| 302 | * Close an I/O channel |
| 303 | */ |
| 304 | static int |
| 305 | xmlPythonFileClose (void * context) { |
| 306 | PyObject *file, *ret; |
| 307 | |
| 308 | #ifdef DEBUG_FILES |
| 309 | printf("xmlPythonFileClose\n"); |
| 310 | #endif |
| 311 | file = (PyObject *) context; |
| 312 | if (file == NULL) return(-1); |
Daniel Veillard | 118aed7 | 2002-09-24 14:13:13 +0000 | [diff] [blame] | 313 | ret = PyEval_CallMethod(file, (char *) "io_close", (char *) "()"); |
Daniel Veillard | c6d4a93 | 2002-09-12 15:00:57 +0000 | [diff] [blame] | 314 | if (ret != NULL) { |
| 315 | Py_DECREF(ret); |
| 316 | } |
| 317 | return(0); |
| 318 | } |
| 319 | |
| 320 | /** |
| 321 | * xmlOutputBufferCreatePythonFile: |
| 322 | * @file: a PyFile_Type |
| 323 | * @encoder: the encoding converter or NULL |
| 324 | * |
| 325 | * Create a buffered output for the progressive saving to a PyFile_Type |
| 326 | * buffered C I/O |
| 327 | * |
| 328 | * Returns the new parser output or NULL |
| 329 | */ |
Daniel Veillard | 118aed7 | 2002-09-24 14:13:13 +0000 | [diff] [blame] | 330 | static xmlOutputBufferPtr |
Daniel Veillard | c6d4a93 | 2002-09-12 15:00:57 +0000 | [diff] [blame] | 331 | xmlOutputBufferCreatePythonFile(PyObject *file, |
| 332 | xmlCharEncodingHandlerPtr encoder) { |
| 333 | xmlOutputBufferPtr ret; |
| 334 | |
| 335 | if (file == NULL) return(NULL); |
| 336 | |
| 337 | ret = xmlAllocOutputBuffer(encoder); |
| 338 | if (ret != NULL) { |
| 339 | ret->context = file; |
| 340 | /* Py_INCREF(file); */ |
| 341 | ret->writecallback = xmlPythonFileWrite; |
| 342 | ret->closecallback = xmlPythonFileClose; |
| 343 | } |
| 344 | |
| 345 | return(ret); |
| 346 | } |
| 347 | |
| 348 | PyObject * |
| 349 | libxml_xmlCreateOutputBuffer(ATTRIBUTE_UNUSED PyObject *self, PyObject *args) { |
| 350 | PyObject *py_retval; |
| 351 | PyObject *file; |
| 352 | xmlChar *encoding; |
| 353 | xmlCharEncodingHandlerPtr handler = NULL; |
| 354 | xmlOutputBufferPtr buffer; |
| 355 | |
| 356 | |
| 357 | if (!PyArg_ParseTuple(args, (char *)"Oz:xmlOutputBufferCreate", |
| 358 | &file, &encoding)) |
| 359 | return(NULL); |
| 360 | if ((encoding != NULL) && (encoding[0] != 0)) { |
Daniel Veillard | 118aed7 | 2002-09-24 14:13:13 +0000 | [diff] [blame] | 361 | handler = xmlFindCharEncodingHandler((const char *) encoding); |
Daniel Veillard | c6d4a93 | 2002-09-12 15:00:57 +0000 | [diff] [blame] | 362 | } |
| 363 | buffer = xmlOutputBufferCreatePythonFile(file, handler); |
| 364 | if (buffer == NULL) |
| 365 | printf("libxml_xmlCreateOutputBuffer: buffer == NULL\n"); |
| 366 | py_retval = libxml_xmlOutputBufferPtrWrap(buffer); |
| 367 | return(py_retval); |
| 368 | } |
| 369 | |
| 370 | |
| 371 | /** |
| 372 | * xmlParserInputBufferCreatePythonFile: |
| 373 | * @file: a PyFile_Type |
| 374 | * @encoder: the encoding converter or NULL |
| 375 | * |
| 376 | * Create a buffered output for the progressive saving to a PyFile_Type |
| 377 | * buffered C I/O |
| 378 | * |
| 379 | * Returns the new parser output or NULL |
| 380 | */ |
Daniel Veillard | 118aed7 | 2002-09-24 14:13:13 +0000 | [diff] [blame] | 381 | static xmlParserInputBufferPtr |
Daniel Veillard | c6d4a93 | 2002-09-12 15:00:57 +0000 | [diff] [blame] | 382 | xmlParserInputBufferCreatePythonFile(PyObject *file, |
| 383 | xmlCharEncoding encoding) { |
| 384 | xmlParserInputBufferPtr ret; |
| 385 | |
| 386 | if (file == NULL) return(NULL); |
| 387 | |
| 388 | ret = xmlAllocParserInputBuffer(encoding); |
| 389 | if (ret != NULL) { |
| 390 | ret->context = file; |
| 391 | /* Py_INCREF(file); */ |
| 392 | ret->readcallback = xmlPythonFileRead; |
| 393 | ret->closecallback = xmlPythonFileClose; |
| 394 | } |
| 395 | |
| 396 | return(ret); |
| 397 | } |
| 398 | |
| 399 | PyObject * |
| 400 | libxml_xmlCreateInputBuffer(ATTRIBUTE_UNUSED PyObject *self, PyObject *args) { |
| 401 | PyObject *py_retval; |
| 402 | PyObject *file; |
| 403 | xmlChar *encoding; |
| 404 | xmlCharEncoding enc = XML_CHAR_ENCODING_NONE; |
| 405 | xmlParserInputBufferPtr buffer; |
| 406 | |
| 407 | |
| 408 | if (!PyArg_ParseTuple(args, (char *)"Oz:xmlParserInputBufferCreate", |
| 409 | &file, &encoding)) |
| 410 | return(NULL); |
| 411 | if ((encoding != NULL) && (encoding[0] != 0)) { |
Daniel Veillard | 118aed7 | 2002-09-24 14:13:13 +0000 | [diff] [blame] | 412 | enc = xmlParseCharEncoding((const char *) encoding); |
Daniel Veillard | c6d4a93 | 2002-09-12 15:00:57 +0000 | [diff] [blame] | 413 | } |
| 414 | buffer = xmlParserInputBufferCreatePythonFile(file, enc); |
| 415 | if (buffer == NULL) |
| 416 | printf("libxml_xmlParserInputBufferCreate: buffer == NULL\n"); |
| 417 | py_retval = libxml_xmlParserInputBufferPtrWrap(buffer); |
| 418 | return(py_retval); |
| 419 | } |
| 420 | |
| 421 | /************************************************************************ |
| 422 | * * |
| 423 | * Providing the resolver at the Python level * |
| 424 | * * |
| 425 | ************************************************************************/ |
| 426 | |
| 427 | static xmlExternalEntityLoader defaultExternalEntityLoader = NULL; |
| 428 | static PyObject *pythonExternalEntityLoaderObjext; |
| 429 | |
| 430 | static xmlParserInputPtr |
| 431 | pythonExternalEntityLoader(const char *URL, const char *ID, |
| 432 | xmlParserCtxtPtr ctxt) { |
| 433 | xmlParserInputPtr result = NULL; |
| 434 | if (pythonExternalEntityLoaderObjext != NULL) { |
| 435 | PyObject *ret; |
| 436 | PyObject *ctxtobj; |
| 437 | |
| 438 | ctxtobj = libxml_xmlParserCtxtPtrWrap(ctxt); |
| 439 | #ifdef DEBUG_LOADER |
| 440 | printf("pythonExternalEntityLoader: ready to call\n"); |
| 441 | #endif |
| 442 | |
| 443 | ret = PyObject_CallFunction(pythonExternalEntityLoaderObjext, |
Daniel Veillard | 118aed7 | 2002-09-24 14:13:13 +0000 | [diff] [blame] | 444 | (char *) "(ssO)", URL, ID, ctxtobj); |
Daniel Veillard | e4a07e7 | 2003-01-14 14:40:25 +0000 | [diff] [blame] | 445 | Py_XDECREF(ctxtobj); |
Daniel Veillard | c6d4a93 | 2002-09-12 15:00:57 +0000 | [diff] [blame] | 446 | #ifdef DEBUG_LOADER |
| 447 | printf("pythonExternalEntityLoader: result "); |
| 448 | PyObject_Print(ret, stdout, 0); |
| 449 | printf("\n"); |
| 450 | #endif |
| 451 | |
| 452 | if (ret != NULL) { |
Daniel Veillard | 118aed7 | 2002-09-24 14:13:13 +0000 | [diff] [blame] | 453 | if (PyObject_HasAttrString(ret, (char *) "read")) { |
Daniel Veillard | c6d4a93 | 2002-09-12 15:00:57 +0000 | [diff] [blame] | 454 | xmlParserInputBufferPtr buf; |
| 455 | |
| 456 | buf = xmlAllocParserInputBuffer(XML_CHAR_ENCODING_NONE); |
| 457 | if (buf != NULL) { |
| 458 | buf->context = ret; |
| 459 | buf->readcallback = xmlPythonFileReadRaw; |
| 460 | buf->closecallback = xmlPythonFileCloseRaw; |
| 461 | result = xmlNewIOInputStream(ctxt, buf, |
| 462 | XML_CHAR_ENCODING_NONE); |
| 463 | } |
| 464 | } else { |
| 465 | printf("pythonExternalEntityLoader: can't read\n"); |
| 466 | } |
| 467 | if (result == NULL) { |
| 468 | Py_DECREF(ret); |
Daniel Veillard | c64b8e9 | 2003-02-24 11:47:13 +0000 | [diff] [blame] | 469 | } else if (URL != NULL) { |
| 470 | result->filename = xmlStrdup(URL); |
| 471 | result->directory = xmlParserGetDirectory((const char *) URL); |
Daniel Veillard | c6d4a93 | 2002-09-12 15:00:57 +0000 | [diff] [blame] | 472 | } |
| 473 | } |
| 474 | } |
| 475 | if ((result == NULL) && (defaultExternalEntityLoader != NULL)) { |
| 476 | result = defaultExternalEntityLoader(URL, ID, ctxt); |
| 477 | } |
| 478 | return(result); |
| 479 | } |
| 480 | |
| 481 | PyObject * |
| 482 | libxml_xmlSetEntityLoader(ATTRIBUTE_UNUSED PyObject *self, PyObject *args) { |
| 483 | PyObject *py_retval; |
| 484 | PyObject *loader; |
| 485 | |
| 486 | if (!PyArg_ParseTuple(args, (char *)"O:libxml_xmlSetEntityLoader", |
| 487 | &loader)) |
| 488 | return(NULL); |
| 489 | |
| 490 | #ifdef DEBUG_LOADER |
| 491 | printf("libxml_xmlSetEntityLoader\n"); |
| 492 | #endif |
| 493 | if (defaultExternalEntityLoader == NULL) |
| 494 | defaultExternalEntityLoader = xmlGetExternalEntityLoader(); |
| 495 | |
| 496 | pythonExternalEntityLoaderObjext = loader; |
| 497 | xmlSetExternalEntityLoader(pythonExternalEntityLoader); |
| 498 | |
| 499 | py_retval = PyInt_FromLong(0); |
| 500 | return(py_retval); |
| 501 | } |
| 502 | |
| 503 | |
| 504 | /************************************************************************ |
| 505 | * * |
Daniel Veillard | 3ce5257 | 2002-02-03 15:08:05 +0000 | [diff] [blame] | 506 | * Handling SAX/xmllib/sgmlop callback interfaces * |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 507 | * * |
| 508 | ************************************************************************/ |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 509 | |
Daniel Veillard | 33caa0b | 2002-02-04 14:07:26 +0000 | [diff] [blame] | 510 | static void |
| 511 | pythonStartElement(void *user_data, const xmlChar * name, |
| 512 | const xmlChar ** attrs) |
| 513 | { |
| 514 | int i; |
| 515 | PyObject *handler; |
| 516 | PyObject *dict; |
| 517 | PyObject *attrname; |
| 518 | PyObject *attrvalue; |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 519 | PyObject *result = NULL; |
Daniel Veillard | 33caa0b | 2002-02-04 14:07:26 +0000 | [diff] [blame] | 520 | int type = 0; |
| 521 | |
Daniel Veillard | 797a565 | 2002-02-12 13:46:21 +0000 | [diff] [blame] | 522 | #ifdef DEBUG_SAX |
| 523 | printf("pythonStartElement(%s) called\n", name); |
| 524 | #endif |
Daniel Veillard | 33caa0b | 2002-02-04 14:07:26 +0000 | [diff] [blame] | 525 | handler = (PyObject *) user_data; |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 526 | if (PyObject_HasAttrString(handler, (char *) "startElement")) |
Daniel Veillard | 33caa0b | 2002-02-04 14:07:26 +0000 | [diff] [blame] | 527 | type = 1; |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 528 | else if (PyObject_HasAttrString(handler, (char *) "start")) |
Daniel Veillard | 33caa0b | 2002-02-04 14:07:26 +0000 | [diff] [blame] | 529 | type = 2; |
| 530 | if (type != 0) { |
| 531 | /* |
| 532 | * the xmllib interface always generate a dictionnary, |
| 533 | * possibly empty |
| 534 | */ |
| 535 | if ((attrs == NULL) && (type == 1)) { |
| 536 | Py_XINCREF(Py_None); |
| 537 | dict = Py_None; |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 538 | } else if (attrs == NULL) { |
| 539 | dict = PyDict_New(); |
Daniel Veillard | 33caa0b | 2002-02-04 14:07:26 +0000 | [diff] [blame] | 540 | } else { |
| 541 | dict = PyDict_New(); |
| 542 | for (i = 0; attrs[i] != NULL; i++) { |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 543 | attrname = PyString_FromString((char *) attrs[i]); |
Daniel Veillard | 33caa0b | 2002-02-04 14:07:26 +0000 | [diff] [blame] | 544 | i++; |
| 545 | if (attrs[i] != NULL) { |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 546 | attrvalue = PyString_FromString((char *) attrs[i]); |
Daniel Veillard | 33caa0b | 2002-02-04 14:07:26 +0000 | [diff] [blame] | 547 | } else { |
| 548 | Py_XINCREF(Py_None); |
| 549 | attrvalue = Py_None; |
| 550 | } |
| 551 | PyDict_SetItem(dict, attrname, attrvalue); |
| 552 | } |
| 553 | } |
| 554 | |
| 555 | if (type == 1) |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 556 | result = PyObject_CallMethod(handler, (char *) "startElement", |
| 557 | (char *) "sO", name, dict); |
Daniel Veillard | 33caa0b | 2002-02-04 14:07:26 +0000 | [diff] [blame] | 558 | else if (type == 2) |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 559 | result = PyObject_CallMethod(handler, (char *) "start", |
| 560 | (char *) "sO", name, dict); |
| 561 | if (PyErr_Occurred()) |
| 562 | PyErr_Print(); |
Daniel Veillard | 33caa0b | 2002-02-04 14:07:26 +0000 | [diff] [blame] | 563 | Py_XDECREF(dict); |
| 564 | Py_XDECREF(result); |
| 565 | } |
| 566 | } |
| 567 | |
| 568 | static void |
| 569 | pythonStartDocument(void *user_data) |
| 570 | { |
| 571 | PyObject *handler; |
| 572 | PyObject *result; |
| 573 | |
Daniel Veillard | 797a565 | 2002-02-12 13:46:21 +0000 | [diff] [blame] | 574 | #ifdef DEBUG_SAX |
| 575 | printf("pythonStartDocument() called\n"); |
| 576 | #endif |
Daniel Veillard | 33caa0b | 2002-02-04 14:07:26 +0000 | [diff] [blame] | 577 | handler = (PyObject *) user_data; |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 578 | if (PyObject_HasAttrString(handler, (char *) "startDocument")) { |
| 579 | result = |
| 580 | PyObject_CallMethod(handler, (char *) "startDocument", NULL); |
| 581 | if (PyErr_Occurred()) |
| 582 | PyErr_Print(); |
Daniel Veillard | 33caa0b | 2002-02-04 14:07:26 +0000 | [diff] [blame] | 583 | Py_XDECREF(result); |
| 584 | } |
| 585 | } |
| 586 | |
| 587 | static void |
| 588 | pythonEndDocument(void *user_data) |
| 589 | { |
| 590 | PyObject *handler; |
| 591 | PyObject *result; |
| 592 | |
Daniel Veillard | 797a565 | 2002-02-12 13:46:21 +0000 | [diff] [blame] | 593 | #ifdef DEBUG_SAX |
| 594 | printf("pythonEndDocument() called\n"); |
| 595 | #endif |
Daniel Veillard | 33caa0b | 2002-02-04 14:07:26 +0000 | [diff] [blame] | 596 | handler = (PyObject *) user_data; |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 597 | if (PyObject_HasAttrString(handler, (char *) "endDocument")) { |
| 598 | result = |
| 599 | PyObject_CallMethod(handler, (char *) "endDocument", NULL); |
| 600 | if (PyErr_Occurred()) |
| 601 | PyErr_Print(); |
Daniel Veillard | 33caa0b | 2002-02-04 14:07:26 +0000 | [diff] [blame] | 602 | Py_XDECREF(result); |
| 603 | } |
| 604 | /* |
| 605 | * The reference to the handler is released there |
| 606 | */ |
| 607 | Py_XDECREF(handler); |
| 608 | } |
| 609 | |
| 610 | static void |
| 611 | pythonEndElement(void *user_data, const xmlChar * name) |
| 612 | { |
| 613 | PyObject *handler; |
| 614 | PyObject *result; |
| 615 | |
Daniel Veillard | 797a565 | 2002-02-12 13:46:21 +0000 | [diff] [blame] | 616 | #ifdef DEBUG_SAX |
| 617 | printf("pythonEndElement(%s) called\n", name); |
| 618 | #endif |
Daniel Veillard | 33caa0b | 2002-02-04 14:07:26 +0000 | [diff] [blame] | 619 | handler = (PyObject *) user_data; |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 620 | if (PyObject_HasAttrString(handler, (char *) "endElement")) { |
| 621 | result = PyObject_CallMethod(handler, (char *) "endElement", |
| 622 | (char *) "s", name); |
| 623 | if (PyErr_Occurred()) |
| 624 | PyErr_Print(); |
Daniel Veillard | 33caa0b | 2002-02-04 14:07:26 +0000 | [diff] [blame] | 625 | Py_XDECREF(result); |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 626 | } else if (PyObject_HasAttrString(handler, (char *) "end")) { |
| 627 | result = PyObject_CallMethod(handler, (char *) "end", |
| 628 | (char *) "s", name); |
| 629 | if (PyErr_Occurred()) |
| 630 | PyErr_Print(); |
Daniel Veillard | 797a565 | 2002-02-12 13:46:21 +0000 | [diff] [blame] | 631 | Py_XDECREF(result); |
Daniel Veillard | 33caa0b | 2002-02-04 14:07:26 +0000 | [diff] [blame] | 632 | } |
| 633 | } |
| 634 | |
| 635 | static void |
| 636 | pythonReference(void *user_data, const xmlChar * name) |
| 637 | { |
| 638 | PyObject *handler; |
| 639 | PyObject *result; |
| 640 | |
Daniel Veillard | 797a565 | 2002-02-12 13:46:21 +0000 | [diff] [blame] | 641 | #ifdef DEBUG_SAX |
| 642 | printf("pythonReference(%s) called\n", name); |
| 643 | #endif |
Daniel Veillard | 33caa0b | 2002-02-04 14:07:26 +0000 | [diff] [blame] | 644 | handler = (PyObject *) user_data; |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 645 | if (PyObject_HasAttrString(handler, (char *) "reference")) { |
| 646 | result = PyObject_CallMethod(handler, (char *) "reference", |
| 647 | (char *) "s", name); |
| 648 | if (PyErr_Occurred()) |
| 649 | PyErr_Print(); |
Daniel Veillard | 33caa0b | 2002-02-04 14:07:26 +0000 | [diff] [blame] | 650 | Py_XDECREF(result); |
| 651 | } |
| 652 | } |
| 653 | |
| 654 | static void |
| 655 | pythonCharacters(void *user_data, const xmlChar * ch, int len) |
| 656 | { |
| 657 | PyObject *handler; |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 658 | PyObject *result = NULL; |
Daniel Veillard | 33caa0b | 2002-02-04 14:07:26 +0000 | [diff] [blame] | 659 | int type = 0; |
| 660 | |
Daniel Veillard | 797a565 | 2002-02-12 13:46:21 +0000 | [diff] [blame] | 661 | #ifdef DEBUG_SAX |
| 662 | printf("pythonCharacters(%s, %d) called\n", ch, len); |
| 663 | #endif |
Daniel Veillard | 33caa0b | 2002-02-04 14:07:26 +0000 | [diff] [blame] | 664 | handler = (PyObject *) user_data; |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 665 | if (PyObject_HasAttrString(handler, (char *) "characters")) |
| 666 | type = 1; |
| 667 | else if (PyObject_HasAttrString(handler, (char *) "data")) |
| 668 | type = 2; |
Daniel Veillard | 33caa0b | 2002-02-04 14:07:26 +0000 | [diff] [blame] | 669 | if (type != 0) { |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 670 | if (type == 1) |
| 671 | result = PyObject_CallMethod(handler, (char *) "characters", |
| 672 | (char *) "s#", ch, len); |
| 673 | else if (type == 2) |
| 674 | result = PyObject_CallMethod(handler, (char *) "data", |
| 675 | (char *) "s#", ch, len); |
| 676 | if (PyErr_Occurred()) |
| 677 | PyErr_Print(); |
Daniel Veillard | 33caa0b | 2002-02-04 14:07:26 +0000 | [diff] [blame] | 678 | Py_XDECREF(result); |
| 679 | } |
| 680 | } |
| 681 | |
| 682 | static void |
| 683 | pythonIgnorableWhitespace(void *user_data, const xmlChar * ch, int len) |
| 684 | { |
| 685 | PyObject *handler; |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 686 | PyObject *result = NULL; |
Daniel Veillard | 33caa0b | 2002-02-04 14:07:26 +0000 | [diff] [blame] | 687 | int type = 0; |
| 688 | |
Daniel Veillard | 797a565 | 2002-02-12 13:46:21 +0000 | [diff] [blame] | 689 | #ifdef DEBUG_SAX |
| 690 | printf("pythonIgnorableWhitespace(%s, %d) called\n", ch, len); |
| 691 | #endif |
Daniel Veillard | 33caa0b | 2002-02-04 14:07:26 +0000 | [diff] [blame] | 692 | handler = (PyObject *) user_data; |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 693 | if (PyObject_HasAttrString(handler, (char *) "ignorableWhitespace")) |
Daniel Veillard | 33caa0b | 2002-02-04 14:07:26 +0000 | [diff] [blame] | 694 | type = 1; |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 695 | else if (PyObject_HasAttrString(handler, (char *) "data")) |
Daniel Veillard | 33caa0b | 2002-02-04 14:07:26 +0000 | [diff] [blame] | 696 | type = 2; |
| 697 | if (type != 0) { |
| 698 | if (type == 1) |
| 699 | result = |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 700 | PyObject_CallMethod(handler, |
| 701 | (char *) "ignorableWhitespace", |
| 702 | (char *) "s#", ch, len); |
Daniel Veillard | 33caa0b | 2002-02-04 14:07:26 +0000 | [diff] [blame] | 703 | else if (type == 2) |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 704 | result = |
| 705 | PyObject_CallMethod(handler, (char *) "data", |
| 706 | (char *) "s#", ch, len); |
Daniel Veillard | 33caa0b | 2002-02-04 14:07:26 +0000 | [diff] [blame] | 707 | Py_XDECREF(result); |
| 708 | } |
| 709 | } |
| 710 | |
| 711 | static void |
| 712 | pythonProcessingInstruction(void *user_data, |
| 713 | const xmlChar * target, const xmlChar * data) |
| 714 | { |
| 715 | PyObject *handler; |
| 716 | PyObject *result; |
| 717 | |
Daniel Veillard | 797a565 | 2002-02-12 13:46:21 +0000 | [diff] [blame] | 718 | #ifdef DEBUG_SAX |
| 719 | printf("pythonProcessingInstruction(%s, %s) called\n", target, data); |
| 720 | #endif |
Daniel Veillard | 33caa0b | 2002-02-04 14:07:26 +0000 | [diff] [blame] | 721 | handler = (PyObject *) user_data; |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 722 | if (PyObject_HasAttrString(handler, (char *) "processingInstruction")) { |
| 723 | result = PyObject_CallMethod(handler, (char *) |
| 724 | "processingInstruction", |
| 725 | (char *) "ss", target, data); |
Daniel Veillard | 33caa0b | 2002-02-04 14:07:26 +0000 | [diff] [blame] | 726 | Py_XDECREF(result); |
| 727 | } |
| 728 | } |
| 729 | |
| 730 | static void |
| 731 | pythonComment(void *user_data, const xmlChar * value) |
| 732 | { |
| 733 | PyObject *handler; |
| 734 | PyObject *result; |
| 735 | |
Daniel Veillard | 797a565 | 2002-02-12 13:46:21 +0000 | [diff] [blame] | 736 | #ifdef DEBUG_SAX |
| 737 | printf("pythonComment(%s) called\n", value); |
| 738 | #endif |
Daniel Veillard | 33caa0b | 2002-02-04 14:07:26 +0000 | [diff] [blame] | 739 | handler = (PyObject *) user_data; |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 740 | if (PyObject_HasAttrString(handler, (char *) "comment")) { |
| 741 | result = |
| 742 | PyObject_CallMethod(handler, (char *) "comment", (char *) "s", |
| 743 | value); |
| 744 | if (PyErr_Occurred()) |
| 745 | PyErr_Print(); |
Daniel Veillard | 33caa0b | 2002-02-04 14:07:26 +0000 | [diff] [blame] | 746 | Py_XDECREF(result); |
| 747 | } |
| 748 | } |
| 749 | |
| 750 | static void |
| 751 | pythonWarning(void *user_data, const char *msg, ...) |
| 752 | { |
| 753 | PyObject *handler; |
| 754 | PyObject *result; |
| 755 | va_list args; |
| 756 | char buf[1024]; |
| 757 | |
Daniel Veillard | 797a565 | 2002-02-12 13:46:21 +0000 | [diff] [blame] | 758 | #ifdef DEBUG_SAX |
| 759 | printf("pythonWarning(%s) called\n", msg); |
| 760 | #endif |
Daniel Veillard | 33caa0b | 2002-02-04 14:07:26 +0000 | [diff] [blame] | 761 | handler = (PyObject *) user_data; |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 762 | if (PyObject_HasAttrString(handler, (char *) "warning")) { |
Daniel Veillard | 33caa0b | 2002-02-04 14:07:26 +0000 | [diff] [blame] | 763 | va_start(args, msg); |
| 764 | vsnprintf(buf, 1023, msg, args); |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 765 | va_end(args); |
| 766 | buf[1023] = 0; |
| 767 | result = |
| 768 | PyObject_CallMethod(handler, (char *) "warning", (char *) "s", |
| 769 | buf); |
| 770 | if (PyErr_Occurred()) |
| 771 | PyErr_Print(); |
Daniel Veillard | 33caa0b | 2002-02-04 14:07:26 +0000 | [diff] [blame] | 772 | Py_XDECREF(result); |
| 773 | } |
| 774 | } |
| 775 | |
| 776 | static void |
| 777 | pythonError(void *user_data, const char *msg, ...) |
| 778 | { |
| 779 | PyObject *handler; |
| 780 | PyObject *result; |
| 781 | va_list args; |
| 782 | char buf[1024]; |
| 783 | |
Daniel Veillard | 797a565 | 2002-02-12 13:46:21 +0000 | [diff] [blame] | 784 | #ifdef DEBUG_SAX |
| 785 | printf("pythonError(%s) called\n", msg); |
| 786 | #endif |
Daniel Veillard | 33caa0b | 2002-02-04 14:07:26 +0000 | [diff] [blame] | 787 | handler = (PyObject *) user_data; |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 788 | if (PyObject_HasAttrString(handler, (char *) "error")) { |
Daniel Veillard | 33caa0b | 2002-02-04 14:07:26 +0000 | [diff] [blame] | 789 | va_start(args, msg); |
| 790 | vsnprintf(buf, 1023, msg, args); |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 791 | va_end(args); |
| 792 | buf[1023] = 0; |
| 793 | result = |
| 794 | PyObject_CallMethod(handler, (char *) "error", (char *) "s", |
| 795 | buf); |
| 796 | if (PyErr_Occurred()) |
| 797 | PyErr_Print(); |
Daniel Veillard | 33caa0b | 2002-02-04 14:07:26 +0000 | [diff] [blame] | 798 | Py_XDECREF(result); |
| 799 | } |
| 800 | } |
| 801 | |
| 802 | static void |
| 803 | pythonFatalError(void *user_data, const char *msg, ...) |
| 804 | { |
| 805 | PyObject *handler; |
| 806 | PyObject *result; |
| 807 | va_list args; |
| 808 | char buf[1024]; |
| 809 | |
Daniel Veillard | 797a565 | 2002-02-12 13:46:21 +0000 | [diff] [blame] | 810 | #ifdef DEBUG_SAX |
| 811 | printf("pythonFatalError(%s) called\n", msg); |
| 812 | #endif |
Daniel Veillard | 33caa0b | 2002-02-04 14:07:26 +0000 | [diff] [blame] | 813 | handler = (PyObject *) user_data; |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 814 | if (PyObject_HasAttrString(handler, (char *) "fatalError")) { |
Daniel Veillard | 33caa0b | 2002-02-04 14:07:26 +0000 | [diff] [blame] | 815 | va_start(args, msg); |
| 816 | vsnprintf(buf, 1023, msg, args); |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 817 | va_end(args); |
| 818 | buf[1023] = 0; |
| 819 | result = |
| 820 | PyObject_CallMethod(handler, (char *) "fatalError", |
| 821 | (char *) "s", buf); |
| 822 | if (PyErr_Occurred()) |
| 823 | PyErr_Print(); |
Daniel Veillard | 33caa0b | 2002-02-04 14:07:26 +0000 | [diff] [blame] | 824 | Py_XDECREF(result); |
| 825 | } |
| 826 | } |
| 827 | |
| 828 | static void |
| 829 | pythonCdataBlock(void *user_data, const xmlChar * ch, int len) |
| 830 | { |
| 831 | PyObject *handler; |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 832 | PyObject *result = NULL; |
Daniel Veillard | 33caa0b | 2002-02-04 14:07:26 +0000 | [diff] [blame] | 833 | int type = 0; |
| 834 | |
Daniel Veillard | 797a565 | 2002-02-12 13:46:21 +0000 | [diff] [blame] | 835 | #ifdef DEBUG_SAX |
| 836 | printf("pythonCdataBlock(%s, %d) called\n", ch, len); |
| 837 | #endif |
Daniel Veillard | 33caa0b | 2002-02-04 14:07:26 +0000 | [diff] [blame] | 838 | handler = (PyObject *) user_data; |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 839 | if (PyObject_HasAttrString(handler, (char *) "cdataBlock")) |
| 840 | type = 1; |
| 841 | else if (PyObject_HasAttrString(handler, (char *) "cdata")) |
| 842 | type = 2; |
Daniel Veillard | 33caa0b | 2002-02-04 14:07:26 +0000 | [diff] [blame] | 843 | if (type != 0) { |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 844 | if (type == 1) |
| 845 | result = |
| 846 | PyObject_CallMethod(handler, (char *) "cdataBlock", |
| 847 | (char *) "s#", ch, len); |
| 848 | else if (type == 2) |
| 849 | result = |
| 850 | PyObject_CallMethod(handler, (char *) "cdata", |
| 851 | (char *) "s#", ch, len); |
| 852 | if (PyErr_Occurred()) |
| 853 | PyErr_Print(); |
Daniel Veillard | 33caa0b | 2002-02-04 14:07:26 +0000 | [diff] [blame] | 854 | Py_XDECREF(result); |
| 855 | } |
| 856 | } |
| 857 | |
| 858 | static void |
| 859 | pythonExternalSubset(void *user_data, |
| 860 | const xmlChar * name, |
| 861 | const xmlChar * externalID, const xmlChar * systemID) |
| 862 | { |
| 863 | PyObject *handler; |
| 864 | PyObject *result; |
| 865 | |
Daniel Veillard | 797a565 | 2002-02-12 13:46:21 +0000 | [diff] [blame] | 866 | #ifdef DEBUG_SAX |
| 867 | printf("pythonExternalSubset(%s, %s, %s) called\n", |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 868 | name, externalID, systemID); |
Daniel Veillard | 797a565 | 2002-02-12 13:46:21 +0000 | [diff] [blame] | 869 | #endif |
Daniel Veillard | 33caa0b | 2002-02-04 14:07:26 +0000 | [diff] [blame] | 870 | handler = (PyObject *) user_data; |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 871 | if (PyObject_HasAttrString(handler, (char *) "externalSubset")) { |
Daniel Veillard | 33caa0b | 2002-02-04 14:07:26 +0000 | [diff] [blame] | 872 | result = |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 873 | PyObject_CallMethod(handler, (char *) "externalSubset", |
| 874 | (char *) "sss", name, externalID, |
| 875 | systemID); |
Daniel Veillard | 33caa0b | 2002-02-04 14:07:26 +0000 | [diff] [blame] | 876 | Py_XDECREF(result); |
| 877 | } |
| 878 | } |
| 879 | |
| 880 | static void |
| 881 | pythonEntityDecl(void *user_data, |
| 882 | const xmlChar * name, |
| 883 | int type, |
| 884 | const xmlChar * publicId, |
| 885 | const xmlChar * systemId, xmlChar * content) |
| 886 | { |
| 887 | PyObject *handler; |
| 888 | PyObject *result; |
| 889 | |
| 890 | handler = (PyObject *) user_data; |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 891 | if (PyObject_HasAttrString(handler, (char *) "entityDecl")) { |
| 892 | result = PyObject_CallMethod(handler, (char *) "entityDecl", |
| 893 | (char *) "sisss", name, type, |
| 894 | publicId, systemId, content); |
| 895 | if (PyErr_Occurred()) |
| 896 | PyErr_Print(); |
Daniel Veillard | 33caa0b | 2002-02-04 14:07:26 +0000 | [diff] [blame] | 897 | Py_XDECREF(result); |
| 898 | } |
| 899 | } |
| 900 | |
| 901 | |
| 902 | |
| 903 | static void |
| 904 | |
| 905 | pythonNotationDecl(void *user_data, |
| 906 | const xmlChar * name, |
| 907 | const xmlChar * publicId, const xmlChar * systemId) |
| 908 | { |
| 909 | PyObject *handler; |
| 910 | PyObject *result; |
| 911 | |
| 912 | handler = (PyObject *) user_data; |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 913 | if (PyObject_HasAttrString(handler, (char *) "notationDecl")) { |
| 914 | result = PyObject_CallMethod(handler, (char *) "notationDecl", |
| 915 | (char *) "sss", name, publicId, |
| 916 | systemId); |
| 917 | if (PyErr_Occurred()) |
| 918 | PyErr_Print(); |
Daniel Veillard | 33caa0b | 2002-02-04 14:07:26 +0000 | [diff] [blame] | 919 | Py_XDECREF(result); |
| 920 | } |
| 921 | } |
| 922 | |
| 923 | static void |
| 924 | pythonAttributeDecl(void *user_data, |
| 925 | const xmlChar * elem, |
| 926 | const xmlChar * name, |
| 927 | int type, |
| 928 | int def, |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 929 | const xmlChar * defaultValue, xmlEnumerationPtr tree) |
Daniel Veillard | 33caa0b | 2002-02-04 14:07:26 +0000 | [diff] [blame] | 930 | { |
| 931 | PyObject *handler; |
| 932 | PyObject *nameList; |
| 933 | PyObject *newName; |
| 934 | xmlEnumerationPtr node; |
| 935 | PyObject *result; |
| 936 | int count; |
| 937 | |
| 938 | handler = (PyObject *) user_data; |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 939 | if (PyObject_HasAttrString(handler, (char *) "attributeDecl")) { |
Daniel Veillard | 33caa0b | 2002-02-04 14:07:26 +0000 | [diff] [blame] | 940 | count = 0; |
| 941 | for (node = tree; node != NULL; node = node->next) { |
| 942 | count++; |
| 943 | } |
| 944 | nameList = PyList_New(count); |
| 945 | count = 0; |
| 946 | for (node = tree; node != NULL; node = node->next) { |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 947 | newName = PyString_FromString((char *) node->name); |
Daniel Veillard | 33caa0b | 2002-02-04 14:07:26 +0000 | [diff] [blame] | 948 | PyList_SetItem(nameList, count, newName); |
| 949 | count++; |
| 950 | } |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 951 | result = PyObject_CallMethod(handler, (char *) "attributeDecl", |
| 952 | (char *) "ssiisO", elem, name, type, |
| 953 | def, defaultValue, nameList); |
| 954 | if (PyErr_Occurred()) |
| 955 | PyErr_Print(); |
Daniel Veillard | 33caa0b | 2002-02-04 14:07:26 +0000 | [diff] [blame] | 956 | Py_XDECREF(nameList); |
| 957 | Py_XDECREF(result); |
| 958 | } |
| 959 | } |
| 960 | |
| 961 | static void |
| 962 | pythonElementDecl(void *user_data, |
| 963 | const xmlChar * name, |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 964 | int type, ATTRIBUTE_UNUSED xmlElementContentPtr content) |
Daniel Veillard | 33caa0b | 2002-02-04 14:07:26 +0000 | [diff] [blame] | 965 | { |
| 966 | PyObject *handler; |
| 967 | PyObject *obj; |
| 968 | PyObject *result; |
| 969 | |
| 970 | handler = (PyObject *) user_data; |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 971 | if (PyObject_HasAttrString(handler, (char *) "elementDecl")) { |
| 972 | /* TODO: wrap in an elementContent object */ |
| 973 | printf |
| 974 | ("pythonElementDecl: xmlElementContentPtr wrapper missing !\n"); |
| 975 | obj = Py_None; |
| 976 | /* Py_XINCREF(Py_None); isn't the reference just borrowed ??? */ |
| 977 | result = PyObject_CallMethod(handler, (char *) "elementDecl", |
| 978 | (char *) "siO", name, type, obj); |
| 979 | if (PyErr_Occurred()) |
| 980 | PyErr_Print(); |
Daniel Veillard | 33caa0b | 2002-02-04 14:07:26 +0000 | [diff] [blame] | 981 | Py_XDECREF(result); |
| 982 | } |
| 983 | } |
| 984 | |
| 985 | static void |
| 986 | pythonUnparsedEntityDecl(void *user_data, |
| 987 | const xmlChar * name, |
| 988 | const xmlChar * publicId, |
| 989 | const xmlChar * systemId, |
| 990 | const xmlChar * notationName) |
| 991 | { |
| 992 | PyObject *handler; |
| 993 | PyObject *result; |
| 994 | |
| 995 | handler = (PyObject *) user_data; |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 996 | if (PyObject_HasAttrString(handler, (char *) "unparsedEntityDecl")) { |
| 997 | result = |
| 998 | PyObject_CallMethod(handler, (char *) "unparsedEntityDecl", |
| 999 | (char *) "ssss", name, publicId, systemId, |
| 1000 | notationName); |
| 1001 | if (PyErr_Occurred()) |
| 1002 | PyErr_Print(); |
Daniel Veillard | 33caa0b | 2002-02-04 14:07:26 +0000 | [diff] [blame] | 1003 | Py_XDECREF(result); |
| 1004 | } |
| 1005 | } |
| 1006 | |
| 1007 | static void |
| 1008 | pythonInternalSubset(void *user_data, const xmlChar * name, |
| 1009 | const xmlChar * ExternalID, const xmlChar * SystemID) |
| 1010 | { |
| 1011 | PyObject *handler; |
| 1012 | PyObject *result; |
| 1013 | |
Daniel Veillard | 797a565 | 2002-02-12 13:46:21 +0000 | [diff] [blame] | 1014 | #ifdef DEBUG_SAX |
| 1015 | printf("pythonInternalSubset(%s, %s, %s) called\n", |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1016 | name, ExternalID, SystemID); |
Daniel Veillard | 797a565 | 2002-02-12 13:46:21 +0000 | [diff] [blame] | 1017 | #endif |
Daniel Veillard | 33caa0b | 2002-02-04 14:07:26 +0000 | [diff] [blame] | 1018 | handler = (PyObject *) user_data; |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1019 | if (PyObject_HasAttrString(handler, (char *) "internalSubset")) { |
| 1020 | result = PyObject_CallMethod(handler, (char *) "internalSubset", |
| 1021 | (char *) "sss", name, ExternalID, |
| 1022 | SystemID); |
| 1023 | if (PyErr_Occurred()) |
| 1024 | PyErr_Print(); |
Daniel Veillard | 33caa0b | 2002-02-04 14:07:26 +0000 | [diff] [blame] | 1025 | Py_XDECREF(result); |
| 1026 | } |
| 1027 | } |
| 1028 | |
| 1029 | static xmlSAXHandler pythonSaxHandler = { |
| 1030 | pythonInternalSubset, |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1031 | NULL, /* TODO pythonIsStandalone, */ |
| 1032 | NULL, /* TODO pythonHasInternalSubset, */ |
| 1033 | NULL, /* TODO pythonHasExternalSubset, */ |
| 1034 | NULL, /* TODO pythonResolveEntity, */ |
| 1035 | NULL, /* TODO pythonGetEntity, */ |
Daniel Veillard | 33caa0b | 2002-02-04 14:07:26 +0000 | [diff] [blame] | 1036 | pythonEntityDecl, |
| 1037 | pythonNotationDecl, |
| 1038 | pythonAttributeDecl, |
| 1039 | pythonElementDecl, |
| 1040 | pythonUnparsedEntityDecl, |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1041 | NULL, /* OBSOLETED pythonSetDocumentLocator, */ |
Daniel Veillard | 33caa0b | 2002-02-04 14:07:26 +0000 | [diff] [blame] | 1042 | pythonStartDocument, |
| 1043 | pythonEndDocument, |
| 1044 | pythonStartElement, |
| 1045 | pythonEndElement, |
| 1046 | pythonReference, |
| 1047 | pythonCharacters, |
| 1048 | pythonIgnorableWhitespace, |
| 1049 | pythonProcessingInstruction, |
| 1050 | pythonComment, |
| 1051 | pythonWarning, |
| 1052 | pythonError, |
| 1053 | pythonFatalError, |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1054 | NULL, /* TODO pythonGetParameterEntity, */ |
Daniel Veillard | 33caa0b | 2002-02-04 14:07:26 +0000 | [diff] [blame] | 1055 | pythonCdataBlock, |
| 1056 | pythonExternalSubset, |
| 1057 | 1 |
| 1058 | }; |
Daniel Veillard | 3ce5257 | 2002-02-03 15:08:05 +0000 | [diff] [blame] | 1059 | |
| 1060 | /************************************************************************ |
| 1061 | * * |
| 1062 | * Handling of specific parser context * |
| 1063 | * * |
| 1064 | ************************************************************************/ |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 1065 | |
| 1066 | PyObject * |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1067 | libxml_xmlCreatePushParser(ATTRIBUTE_UNUSED PyObject * self, |
| 1068 | PyObject * args) |
| 1069 | { |
| 1070 | const char *chunk; |
Daniel Veillard | 3ce5257 | 2002-02-03 15:08:05 +0000 | [diff] [blame] | 1071 | int size; |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1072 | const char *URI; |
Daniel Veillard | 33caa0b | 2002-02-04 14:07:26 +0000 | [diff] [blame] | 1073 | PyObject *pyobj_SAX = NULL; |
Daniel Veillard | 3ce5257 | 2002-02-03 15:08:05 +0000 | [diff] [blame] | 1074 | xmlSAXHandlerPtr SAX = NULL; |
Daniel Veillard | 3ce5257 | 2002-02-03 15:08:05 +0000 | [diff] [blame] | 1075 | xmlParserCtxtPtr ret; |
| 1076 | PyObject *pyret; |
Daniel Veillard | 96fe095 | 2002-01-30 20:52:23 +0000 | [diff] [blame] | 1077 | |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1078 | if (!PyArg_ParseTuple |
| 1079 | (args, (char *) "Oziz:xmlCreatePushParser", &pyobj_SAX, &chunk, |
| 1080 | &size, &URI)) |
| 1081 | return (NULL); |
Daniel Veillard | 3ce5257 | 2002-02-03 15:08:05 +0000 | [diff] [blame] | 1082 | |
Daniel Veillard | 8d24cc1 | 2002-03-05 15:41:29 +0000 | [diff] [blame] | 1083 | #ifdef DEBUG |
Daniel Veillard | 3ce5257 | 2002-02-03 15:08:05 +0000 | [diff] [blame] | 1084 | printf("libxml_xmlCreatePushParser(%p, %s, %d, %s) called\n", |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1085 | pyobj_SAX, chunk, size, URI); |
Daniel Veillard | 96fe095 | 2002-01-30 20:52:23 +0000 | [diff] [blame] | 1086 | #endif |
Daniel Veillard | 3ce5257 | 2002-02-03 15:08:05 +0000 | [diff] [blame] | 1087 | if (pyobj_SAX != Py_None) { |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1088 | SAX = &pythonSaxHandler; |
| 1089 | Py_INCREF(pyobj_SAX); |
| 1090 | /* The reference is released in pythonEndDocument() */ |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 1091 | } |
Daniel Veillard | 33caa0b | 2002-02-04 14:07:26 +0000 | [diff] [blame] | 1092 | ret = xmlCreatePushParserCtxt(SAX, pyobj_SAX, chunk, size, URI); |
Daniel Veillard | 3ce5257 | 2002-02-03 15:08:05 +0000 | [diff] [blame] | 1093 | pyret = libxml_xmlParserCtxtPtrWrap(ret); |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1094 | return (pyret); |
Daniel Veillard | a7340c8 | 2002-02-01 17:56:45 +0000 | [diff] [blame] | 1095 | } |
Daniel Veillard | 5d81903 | 2002-02-02 21:49:17 +0000 | [diff] [blame] | 1096 | |
Daniel Veillard | 4e1b26c | 2002-02-03 20:13:06 +0000 | [diff] [blame] | 1097 | PyObject * |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1098 | libxml_htmlCreatePushParser(ATTRIBUTE_UNUSED PyObject * self, |
| 1099 | PyObject * args) |
| 1100 | { |
| 1101 | const char *chunk; |
Daniel Veillard | 4e1b26c | 2002-02-03 20:13:06 +0000 | [diff] [blame] | 1102 | int size; |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1103 | const char *URI; |
Daniel Veillard | 33caa0b | 2002-02-04 14:07:26 +0000 | [diff] [blame] | 1104 | PyObject *pyobj_SAX = NULL; |
Daniel Veillard | 4e1b26c | 2002-02-03 20:13:06 +0000 | [diff] [blame] | 1105 | xmlSAXHandlerPtr SAX = NULL; |
Daniel Veillard | 4e1b26c | 2002-02-03 20:13:06 +0000 | [diff] [blame] | 1106 | xmlParserCtxtPtr ret; |
| 1107 | PyObject *pyret; |
| 1108 | |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1109 | if (!PyArg_ParseTuple |
| 1110 | (args, (char *) "Oziz:htmlCreatePushParser", &pyobj_SAX, &chunk, |
| 1111 | &size, &URI)) |
| 1112 | return (NULL); |
Daniel Veillard | 4e1b26c | 2002-02-03 20:13:06 +0000 | [diff] [blame] | 1113 | |
Daniel Veillard | 8d24cc1 | 2002-03-05 15:41:29 +0000 | [diff] [blame] | 1114 | #ifdef DEBUG |
Daniel Veillard | 4e1b26c | 2002-02-03 20:13:06 +0000 | [diff] [blame] | 1115 | printf("libxml_htmlCreatePushParser(%p, %s, %d, %s) called\n", |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1116 | pyobj_SAX, chunk, size, URI); |
Daniel Veillard | 4e1b26c | 2002-02-03 20:13:06 +0000 | [diff] [blame] | 1117 | #endif |
| 1118 | if (pyobj_SAX != Py_None) { |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1119 | SAX = &pythonSaxHandler; |
| 1120 | Py_INCREF(pyobj_SAX); |
| 1121 | /* The reference is released in pythonEndDocument() */ |
Daniel Veillard | 4e1b26c | 2002-02-03 20:13:06 +0000 | [diff] [blame] | 1122 | } |
Daniel Veillard | 33caa0b | 2002-02-04 14:07:26 +0000 | [diff] [blame] | 1123 | ret = htmlCreatePushParserCtxt(SAX, pyobj_SAX, chunk, size, URI, |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1124 | XML_CHAR_ENCODING_NONE); |
Daniel Veillard | 4e1b26c | 2002-02-03 20:13:06 +0000 | [diff] [blame] | 1125 | pyret = libxml_xmlParserCtxtPtrWrap(ret); |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1126 | return (pyret); |
Daniel Veillard | 4e1b26c | 2002-02-03 20:13:06 +0000 | [diff] [blame] | 1127 | } |
| 1128 | |
Daniel Veillard | 8d24cc1 | 2002-03-05 15:41:29 +0000 | [diff] [blame] | 1129 | PyObject * |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1130 | libxml_xmlSAXParseFile(ATTRIBUTE_UNUSED PyObject * self, PyObject * args) |
| 1131 | { |
Daniel Veillard | 8d24cc1 | 2002-03-05 15:41:29 +0000 | [diff] [blame] | 1132 | int recover; |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1133 | const char *URI; |
Daniel Veillard | 8d24cc1 | 2002-03-05 15:41:29 +0000 | [diff] [blame] | 1134 | PyObject *pyobj_SAX = NULL; |
| 1135 | xmlSAXHandlerPtr SAX = NULL; |
| 1136 | |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1137 | if (!PyArg_ParseTuple(args, (char *) "Osi:xmlSAXParseFile", &pyobj_SAX, |
| 1138 | &URI, &recover)) |
| 1139 | return (NULL); |
Daniel Veillard | 8d24cc1 | 2002-03-05 15:41:29 +0000 | [diff] [blame] | 1140 | |
| 1141 | #ifdef DEBUG |
| 1142 | printf("libxml_xmlSAXParseFile(%p, %s, %d) called\n", |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1143 | pyobj_SAX, URI, recover); |
Daniel Veillard | 8d24cc1 | 2002-03-05 15:41:29 +0000 | [diff] [blame] | 1144 | #endif |
| 1145 | if (pyobj_SAX == Py_None) { |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1146 | Py_INCREF(Py_None); |
| 1147 | return (Py_None); |
Daniel Veillard | 8d24cc1 | 2002-03-05 15:41:29 +0000 | [diff] [blame] | 1148 | } |
| 1149 | SAX = &pythonSaxHandler; |
| 1150 | Py_INCREF(pyobj_SAX); |
| 1151 | /* The reference is released in pythonEndDocument() */ |
| 1152 | xmlSAXParseFileWithData(SAX, URI, recover, pyobj_SAX); |
| 1153 | Py_INCREF(Py_None); |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1154 | return (Py_None); |
Daniel Veillard | 8d24cc1 | 2002-03-05 15:41:29 +0000 | [diff] [blame] | 1155 | } |
| 1156 | |
| 1157 | PyObject * |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1158 | libxml_htmlSAXParseFile(ATTRIBUTE_UNUSED PyObject * self, PyObject * args) |
| 1159 | { |
| 1160 | const char *URI; |
| 1161 | const char *encoding; |
Daniel Veillard | 8d24cc1 | 2002-03-05 15:41:29 +0000 | [diff] [blame] | 1162 | PyObject *pyobj_SAX = NULL; |
| 1163 | xmlSAXHandlerPtr SAX = NULL; |
| 1164 | |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1165 | if (!PyArg_ParseTuple |
| 1166 | (args, (char *) "Osz:htmlSAXParseFile", &pyobj_SAX, &URI, |
| 1167 | &encoding)) |
| 1168 | return (NULL); |
Daniel Veillard | 8d24cc1 | 2002-03-05 15:41:29 +0000 | [diff] [blame] | 1169 | |
| 1170 | #ifdef DEBUG |
| 1171 | printf("libxml_htmlSAXParseFile(%p, %s, %s) called\n", |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1172 | pyobj_SAX, URI, encoding); |
Daniel Veillard | 8d24cc1 | 2002-03-05 15:41:29 +0000 | [diff] [blame] | 1173 | #endif |
| 1174 | if (pyobj_SAX == Py_None) { |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1175 | Py_INCREF(Py_None); |
| 1176 | return (Py_None); |
Daniel Veillard | 8d24cc1 | 2002-03-05 15:41:29 +0000 | [diff] [blame] | 1177 | } |
| 1178 | SAX = &pythonSaxHandler; |
| 1179 | Py_INCREF(pyobj_SAX); |
| 1180 | /* The reference is released in pythonEndDocument() */ |
| 1181 | htmlSAXParseFile(URI, encoding, SAX, pyobj_SAX); |
| 1182 | Py_INCREF(Py_None); |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1183 | return (Py_None); |
Daniel Veillard | 8d24cc1 | 2002-03-05 15:41:29 +0000 | [diff] [blame] | 1184 | } |
| 1185 | |
Daniel Veillard | 5d81903 | 2002-02-02 21:49:17 +0000 | [diff] [blame] | 1186 | /************************************************************************ |
| 1187 | * * |
| 1188 | * Error message callback * |
| 1189 | * * |
| 1190 | ************************************************************************/ |
| 1191 | |
| 1192 | static PyObject *libxml_xmlPythonErrorFuncHandler = NULL; |
| 1193 | static PyObject *libxml_xmlPythonErrorFuncCtxt = NULL; |
| 1194 | |
Daniel Veillard | e6227e0 | 2003-01-14 11:42:39 +0000 | [diff] [blame] | 1195 | /* helper to build a xmlMalloc'ed string from a format and va_list */ |
| 1196 | static char * |
| 1197 | libxml_buildMessage(const char *msg, va_list ap) |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1198 | { |
| 1199 | int size; |
| 1200 | int chars; |
| 1201 | char *larger; |
Daniel Veillard | e6227e0 | 2003-01-14 11:42:39 +0000 | [diff] [blame] | 1202 | char *str; |
| 1203 | |
| 1204 | str = (char *) xmlMalloc(150); |
| 1205 | if (str == NULL) |
| 1206 | return NULL; |
| 1207 | |
| 1208 | size = 150; |
| 1209 | |
| 1210 | while (1) { |
| 1211 | chars = vsnprintf(str, size, msg, ap); |
| 1212 | if ((chars > -1) && (chars < size)) |
| 1213 | break; |
| 1214 | if (chars > -1) |
| 1215 | size += chars + 1; |
| 1216 | else |
| 1217 | size += 100; |
| 1218 | if ((larger = (char *) xmlRealloc(str, size)) == NULL) { |
| 1219 | xmlFree(str); |
| 1220 | return NULL; |
| 1221 | } |
| 1222 | str = larger; |
| 1223 | } |
| 1224 | |
| 1225 | return str; |
| 1226 | } |
| 1227 | |
| 1228 | static void |
| 1229 | libxml_xmlErrorFuncHandler(ATTRIBUTE_UNUSED void *ctx, const char *msg, |
| 1230 | ...) |
| 1231 | { |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1232 | va_list ap; |
| 1233 | char *str; |
Daniel Veillard | 5d81903 | 2002-02-02 21:49:17 +0000 | [diff] [blame] | 1234 | PyObject *list; |
| 1235 | PyObject *message; |
| 1236 | PyObject *result; |
| 1237 | |
| 1238 | #ifdef DEBUG_ERROR |
| 1239 | printf("libxml_xmlErrorFuncHandler(%p, %s, ...) called\n", ctx, msg); |
| 1240 | #endif |
| 1241 | |
| 1242 | |
| 1243 | if (libxml_xmlPythonErrorFuncHandler == NULL) { |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1244 | va_start(ap, msg); |
| 1245 | vfprintf(stdout, msg, ap); |
| 1246 | va_end(ap); |
Daniel Veillard | 5d81903 | 2002-02-02 21:49:17 +0000 | [diff] [blame] | 1247 | } else { |
Daniel Veillard | e6227e0 | 2003-01-14 11:42:39 +0000 | [diff] [blame] | 1248 | va_start(ap, msg); |
| 1249 | str = libxml_buildMessage(msg,ap); |
| 1250 | va_end(ap); |
Daniel Veillard | 5d81903 | 2002-02-02 21:49:17 +0000 | [diff] [blame] | 1251 | |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1252 | list = PyTuple_New(2); |
| 1253 | PyTuple_SetItem(list, 0, libxml_xmlPythonErrorFuncCtxt); |
| 1254 | Py_XINCREF(libxml_xmlPythonErrorFuncCtxt); |
| 1255 | message = libxml_charPtrWrap(str); |
| 1256 | PyTuple_SetItem(list, 1, message); |
| 1257 | result = PyEval_CallObject(libxml_xmlPythonErrorFuncHandler, list); |
| 1258 | Py_XDECREF(list); |
| 1259 | Py_XDECREF(result); |
Daniel Veillard | 5d81903 | 2002-02-02 21:49:17 +0000 | [diff] [blame] | 1260 | } |
| 1261 | } |
| 1262 | |
| 1263 | static void |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1264 | libxml_xmlErrorInitialize(void) |
| 1265 | { |
Daniel Veillard | 5d81903 | 2002-02-02 21:49:17 +0000 | [diff] [blame] | 1266 | #ifdef DEBUG_ERROR |
| 1267 | printf("libxml_xmlErrorInitialize() called\n"); |
| 1268 | #endif |
| 1269 | xmlSetGenericErrorFunc(NULL, libxml_xmlErrorFuncHandler); |
Daniel Veillard | 781ac8b | 2003-05-15 22:11:36 +0000 | [diff] [blame^] | 1270 | xmlThrDefSetGenericErrorFunc(NULL, libxml_xmlErrorFuncHandler); |
Daniel Veillard | 5d81903 | 2002-02-02 21:49:17 +0000 | [diff] [blame] | 1271 | } |
| 1272 | |
| 1273 | PyObject * |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1274 | libxml_xmlRegisterErrorHandler(ATTRIBUTE_UNUSED PyObject * self, |
| 1275 | PyObject * args) |
| 1276 | { |
Daniel Veillard | 5d81903 | 2002-02-02 21:49:17 +0000 | [diff] [blame] | 1277 | PyObject *py_retval; |
| 1278 | PyObject *pyobj_f; |
| 1279 | PyObject *pyobj_ctx; |
| 1280 | |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1281 | if (!PyArg_ParseTuple |
| 1282 | (args, (char *) "OO:xmlRegisterErrorHandler", &pyobj_f, |
| 1283 | &pyobj_ctx)) |
| 1284 | return (NULL); |
Daniel Veillard | 5d81903 | 2002-02-02 21:49:17 +0000 | [diff] [blame] | 1285 | |
| 1286 | #ifdef DEBUG_ERROR |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1287 | printf("libxml_registerXPathFunction(%p, %p) called\n", pyobj_ctx, |
| 1288 | pyobj_f); |
Daniel Veillard | 5d81903 | 2002-02-02 21:49:17 +0000 | [diff] [blame] | 1289 | #endif |
| 1290 | |
| 1291 | if (libxml_xmlPythonErrorFuncHandler != NULL) { |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1292 | Py_XDECREF(libxml_xmlPythonErrorFuncHandler); |
Daniel Veillard | 5d81903 | 2002-02-02 21:49:17 +0000 | [diff] [blame] | 1293 | } |
| 1294 | if (libxml_xmlPythonErrorFuncCtxt != NULL) { |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1295 | Py_XDECREF(libxml_xmlPythonErrorFuncCtxt); |
Daniel Veillard | 5d81903 | 2002-02-02 21:49:17 +0000 | [diff] [blame] | 1296 | } |
| 1297 | |
| 1298 | Py_XINCREF(pyobj_ctx); |
| 1299 | Py_XINCREF(pyobj_f); |
| 1300 | |
| 1301 | /* TODO: check f is a function ! */ |
| 1302 | libxml_xmlPythonErrorFuncHandler = pyobj_f; |
| 1303 | libxml_xmlPythonErrorFuncCtxt = pyobj_ctx; |
| 1304 | |
| 1305 | py_retval = libxml_intWrap(1); |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1306 | return (py_retval); |
Daniel Veillard | 5d81903 | 2002-02-02 21:49:17 +0000 | [diff] [blame] | 1307 | } |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1308 | |
Daniel Veillard | e6227e0 | 2003-01-14 11:42:39 +0000 | [diff] [blame] | 1309 | |
| 1310 | /************************************************************************ |
| 1311 | * * |
| 1312 | * Per parserCtxt error handler * |
| 1313 | * * |
| 1314 | ************************************************************************/ |
| 1315 | |
Daniel Veillard | 417be3a | 2003-01-20 21:26:34 +0000 | [diff] [blame] | 1316 | typedef struct |
Daniel Veillard | e6227e0 | 2003-01-14 11:42:39 +0000 | [diff] [blame] | 1317 | { |
Daniel Veillard | 417be3a | 2003-01-20 21:26:34 +0000 | [diff] [blame] | 1318 | PyObject *f; |
| 1319 | PyObject *arg; |
| 1320 | } xmlParserCtxtPyCtxt; |
| 1321 | typedef xmlParserCtxtPyCtxt *xmlParserCtxtPyCtxtPtr; |
| 1322 | |
| 1323 | static void |
| 1324 | libxml_xmlParserCtxtGenericErrorFuncHandler(void *ctx, int severity, char *str) |
| 1325 | { |
Daniel Veillard | e6227e0 | 2003-01-14 11:42:39 +0000 | [diff] [blame] | 1326 | PyObject *list; |
Daniel Veillard | e6227e0 | 2003-01-14 11:42:39 +0000 | [diff] [blame] | 1327 | PyObject *result; |
Daniel Veillard | 417be3a | 2003-01-20 21:26:34 +0000 | [diff] [blame] | 1328 | xmlParserCtxtPtr ctxt; |
Daniel Veillard | e6227e0 | 2003-01-14 11:42:39 +0000 | [diff] [blame] | 1329 | xmlParserCtxtPyCtxtPtr pyCtxt; |
| 1330 | |
| 1331 | #ifdef DEBUG_ERROR |
Daniel Veillard | 417be3a | 2003-01-20 21:26:34 +0000 | [diff] [blame] | 1332 | printf("libxml_xmlParserCtxtGenericErrorFuncHandler(%p, %s, ...) called\n", ctx, msg); |
Daniel Veillard | e6227e0 | 2003-01-14 11:42:39 +0000 | [diff] [blame] | 1333 | #endif |
| 1334 | |
Daniel Veillard | 417be3a | 2003-01-20 21:26:34 +0000 | [diff] [blame] | 1335 | ctxt = (xmlParserCtxtPtr)ctx; |
| 1336 | pyCtxt = (xmlParserCtxtPyCtxtPtr)ctxt->_private; |
Daniel Veillard | e6227e0 | 2003-01-14 11:42:39 +0000 | [diff] [blame] | 1337 | |
Daniel Veillard | 417be3a | 2003-01-20 21:26:34 +0000 | [diff] [blame] | 1338 | list = PyTuple_New(4); |
| 1339 | PyTuple_SetItem(list, 0, pyCtxt->arg); |
| 1340 | Py_XINCREF(pyCtxt->arg); |
| 1341 | PyTuple_SetItem(list, 1, libxml_charPtrWrap(str)); |
| 1342 | PyTuple_SetItem(list, 2, libxml_intWrap(severity)); |
| 1343 | PyTuple_SetItem(list, 3, Py_None); |
| 1344 | Py_INCREF(Py_None); |
| 1345 | result = PyEval_CallObject(pyCtxt->f, list); |
| 1346 | if (result == NULL) |
| 1347 | { |
| 1348 | /* TODO: manage for the exception to be propagated... */ |
| 1349 | PyErr_Print(); |
Daniel Veillard | e6227e0 | 2003-01-14 11:42:39 +0000 | [diff] [blame] | 1350 | } |
Daniel Veillard | 417be3a | 2003-01-20 21:26:34 +0000 | [diff] [blame] | 1351 | Py_XDECREF(list); |
| 1352 | Py_XDECREF(result); |
| 1353 | } |
| 1354 | |
| 1355 | static void |
| 1356 | libxml_xmlParserCtxtErrorFuncHandler(void *ctx, const char *msg, ...) |
| 1357 | { |
| 1358 | va_list ap; |
| 1359 | |
| 1360 | va_start(ap, msg); |
| 1361 | libxml_xmlParserCtxtGenericErrorFuncHandler(ctx,XML_PARSER_SEVERITY_ERROR,libxml_buildMessage(msg,ap)); |
| 1362 | va_end(ap); |
| 1363 | } |
| 1364 | |
| 1365 | static void |
| 1366 | libxml_xmlParserCtxtWarningFuncHandler(void *ctx, const char *msg, ...) |
| 1367 | { |
| 1368 | va_list ap; |
| 1369 | |
| 1370 | va_start(ap, msg); |
| 1371 | libxml_xmlParserCtxtGenericErrorFuncHandler(ctx,XML_PARSER_SEVERITY_WARNING,libxml_buildMessage(msg,ap)); |
| 1372 | va_end(ap); |
| 1373 | } |
| 1374 | |
| 1375 | static void |
| 1376 | libxml_xmlParserCtxtValidityErrorFuncHandler(void *ctx, const char *msg, ...) |
| 1377 | { |
| 1378 | va_list ap; |
| 1379 | |
| 1380 | va_start(ap, msg); |
| 1381 | libxml_xmlParserCtxtGenericErrorFuncHandler(ctx,XML_PARSER_SEVERITY_VALIDITY_ERROR,libxml_buildMessage(msg,ap)); |
| 1382 | va_end(ap); |
| 1383 | } |
| 1384 | |
| 1385 | static void |
| 1386 | libxml_xmlParserCtxtValidityWarningFuncHandler(void *ctx, const char *msg, ...) |
| 1387 | { |
| 1388 | va_list ap; |
| 1389 | |
| 1390 | va_start(ap, msg); |
| 1391 | libxml_xmlParserCtxtGenericErrorFuncHandler(ctx,XML_PARSER_SEVERITY_VALIDITY_WARNING,libxml_buildMessage(msg,ap)); |
| 1392 | va_end(ap); |
Daniel Veillard | e6227e0 | 2003-01-14 11:42:39 +0000 | [diff] [blame] | 1393 | } |
| 1394 | |
| 1395 | PyObject * |
Daniel Veillard | 417be3a | 2003-01-20 21:26:34 +0000 | [diff] [blame] | 1396 | libxml_xmlParserCtxtSetErrorHandler(ATTRIBUTE_UNUSED PyObject *self, PyObject *args) |
Daniel Veillard | e6227e0 | 2003-01-14 11:42:39 +0000 | [diff] [blame] | 1397 | { |
| 1398 | PyObject *py_retval; |
| 1399 | xmlParserCtxtPtr ctxt; |
Daniel Veillard | e4a07e7 | 2003-01-14 14:40:25 +0000 | [diff] [blame] | 1400 | xmlParserCtxtPyCtxtPtr pyCtxt; |
Daniel Veillard | e6227e0 | 2003-01-14 11:42:39 +0000 | [diff] [blame] | 1401 | PyObject *pyobj_ctxt; |
| 1402 | PyObject *pyobj_f; |
| 1403 | PyObject *pyobj_arg; |
| 1404 | |
Daniel Veillard | 417be3a | 2003-01-20 21:26:34 +0000 | [diff] [blame] | 1405 | if (!PyArg_ParseTuple(args, (char *)"OOO:xmlParserCtxtSetErrorHandler", |
Daniel Veillard | e6227e0 | 2003-01-14 11:42:39 +0000 | [diff] [blame] | 1406 | &pyobj_ctxt, &pyobj_f, &pyobj_arg)) |
| 1407 | return(NULL); |
| 1408 | ctxt = (xmlParserCtxtPtr) PyparserCtxt_Get(pyobj_ctxt); |
| 1409 | if (ctxt->_private == NULL) { |
Daniel Veillard | e6227e0 | 2003-01-14 11:42:39 +0000 | [diff] [blame] | 1410 | pyCtxt = xmlMalloc(sizeof(xmlParserCtxtPyCtxt)); |
| 1411 | if (pyCtxt == NULL) { |
| 1412 | py_retval = libxml_intWrap(-1); |
| 1413 | return(py_retval); |
| 1414 | } |
| 1415 | memset(pyCtxt,0,sizeof(xmlParserCtxtPyCtxt)); |
| 1416 | ctxt->_private = pyCtxt; |
| 1417 | } |
Daniel Veillard | e4a07e7 | 2003-01-14 14:40:25 +0000 | [diff] [blame] | 1418 | else { |
Daniel Veillard | 417be3a | 2003-01-20 21:26:34 +0000 | [diff] [blame] | 1419 | pyCtxt = (xmlParserCtxtPyCtxtPtr)ctxt->_private; |
Daniel Veillard | e4a07e7 | 2003-01-14 14:40:25 +0000 | [diff] [blame] | 1420 | } |
Daniel Veillard | e6227e0 | 2003-01-14 11:42:39 +0000 | [diff] [blame] | 1421 | /* TODO: check f is a function ! */ |
Daniel Veillard | 417be3a | 2003-01-20 21:26:34 +0000 | [diff] [blame] | 1422 | Py_XDECREF(pyCtxt->f); |
Daniel Veillard | e6227e0 | 2003-01-14 11:42:39 +0000 | [diff] [blame] | 1423 | Py_XINCREF(pyobj_f); |
Daniel Veillard | 417be3a | 2003-01-20 21:26:34 +0000 | [diff] [blame] | 1424 | pyCtxt->f = pyobj_f; |
| 1425 | Py_XDECREF(pyCtxt->arg); |
Daniel Veillard | e6227e0 | 2003-01-14 11:42:39 +0000 | [diff] [blame] | 1426 | Py_XINCREF(pyobj_arg); |
Daniel Veillard | 417be3a | 2003-01-20 21:26:34 +0000 | [diff] [blame] | 1427 | pyCtxt->arg = pyobj_arg; |
Daniel Veillard | e6227e0 | 2003-01-14 11:42:39 +0000 | [diff] [blame] | 1428 | |
Daniel Veillard | 417be3a | 2003-01-20 21:26:34 +0000 | [diff] [blame] | 1429 | if (pyobj_f != Py_None) { |
| 1430 | ctxt->sax->error = libxml_xmlParserCtxtErrorFuncHandler; |
| 1431 | ctxt->sax->warning = libxml_xmlParserCtxtWarningFuncHandler; |
| 1432 | ctxt->vctxt.error = libxml_xmlParserCtxtValidityErrorFuncHandler; |
| 1433 | ctxt->vctxt.warning = libxml_xmlParserCtxtValidityWarningFuncHandler; |
| 1434 | } |
| 1435 | else { |
| 1436 | ctxt->sax->error = xmlParserError; |
| 1437 | ctxt->vctxt.error = xmlParserValidityError; |
| 1438 | ctxt->sax->warning = xmlParserWarning; |
| 1439 | ctxt->vctxt.warning = xmlParserValidityWarning; |
| 1440 | } |
Daniel Veillard | e6227e0 | 2003-01-14 11:42:39 +0000 | [diff] [blame] | 1441 | |
| 1442 | py_retval = libxml_intWrap(1); |
| 1443 | return(py_retval); |
| 1444 | } |
| 1445 | |
Daniel Veillard | e6227e0 | 2003-01-14 11:42:39 +0000 | [diff] [blame] | 1446 | PyObject * |
Daniel Veillard | 417be3a | 2003-01-20 21:26:34 +0000 | [diff] [blame] | 1447 | libxml_xmlParserCtxtGetErrorHandler(ATTRIBUTE_UNUSED PyObject *self, PyObject *args) |
Daniel Veillard | e6227e0 | 2003-01-14 11:42:39 +0000 | [diff] [blame] | 1448 | { |
| 1449 | PyObject *py_retval; |
| 1450 | xmlParserCtxtPtr ctxt; |
Daniel Veillard | e4a07e7 | 2003-01-14 14:40:25 +0000 | [diff] [blame] | 1451 | xmlParserCtxtPyCtxtPtr pyCtxt; |
Daniel Veillard | e6227e0 | 2003-01-14 11:42:39 +0000 | [diff] [blame] | 1452 | PyObject *pyobj_ctxt; |
Daniel Veillard | e6227e0 | 2003-01-14 11:42:39 +0000 | [diff] [blame] | 1453 | |
Daniel Veillard | 417be3a | 2003-01-20 21:26:34 +0000 | [diff] [blame] | 1454 | if (!PyArg_ParseTuple(args, (char *)"O:xmlParserCtxtGetErrorHandler", |
| 1455 | &pyobj_ctxt)) |
Daniel Veillard | e6227e0 | 2003-01-14 11:42:39 +0000 | [diff] [blame] | 1456 | return(NULL); |
| 1457 | ctxt = (xmlParserCtxtPtr) PyparserCtxt_Get(pyobj_ctxt); |
Daniel Veillard | 417be3a | 2003-01-20 21:26:34 +0000 | [diff] [blame] | 1458 | py_retval = PyTuple_New(2); |
| 1459 | if (ctxt->_private != NULL) { |
| 1460 | pyCtxt = (xmlParserCtxtPyCtxtPtr)ctxt->_private; |
| 1461 | |
| 1462 | PyTuple_SetItem(py_retval, 0, pyCtxt->f); |
| 1463 | Py_XINCREF(pyCtxt->f); |
| 1464 | PyTuple_SetItem(py_retval, 1, pyCtxt->arg); |
| 1465 | Py_XINCREF(pyCtxt->arg); |
Daniel Veillard | e4a07e7 | 2003-01-14 14:40:25 +0000 | [diff] [blame] | 1466 | } |
| 1467 | else { |
Daniel Veillard | 417be3a | 2003-01-20 21:26:34 +0000 | [diff] [blame] | 1468 | /* no python error handler registered */ |
| 1469 | PyTuple_SetItem(py_retval, 0, Py_None); |
| 1470 | Py_XINCREF(Py_None); |
| 1471 | PyTuple_SetItem(py_retval, 1, Py_None); |
| 1472 | Py_XINCREF(Py_None); |
Daniel Veillard | e4a07e7 | 2003-01-14 14:40:25 +0000 | [diff] [blame] | 1473 | } |
Daniel Veillard | e6227e0 | 2003-01-14 11:42:39 +0000 | [diff] [blame] | 1474 | return(py_retval); |
| 1475 | } |
| 1476 | |
Daniel Veillard | 417be3a | 2003-01-20 21:26:34 +0000 | [diff] [blame] | 1477 | PyObject * |
| 1478 | libxml_xmlFreeParserCtxt(ATTRIBUTE_UNUSED PyObject *self, PyObject *args) { |
| 1479 | xmlParserCtxtPtr ctxt; |
| 1480 | PyObject *pyobj_ctxt; |
| 1481 | xmlParserCtxtPyCtxtPtr pyCtxt; |
| 1482 | |
| 1483 | if (!PyArg_ParseTuple(args, (char *)"O:xmlFreeParserCtxt", &pyobj_ctxt)) |
| 1484 | return(NULL); |
| 1485 | ctxt = (xmlParserCtxtPtr) PyparserCtxt_Get(pyobj_ctxt); |
| 1486 | |
| 1487 | if (ctxt != NULL) { |
| 1488 | pyCtxt = (xmlParserCtxtPyCtxtPtr)((xmlParserCtxtPtr)ctxt)->_private; |
| 1489 | if (pyCtxt) { |
| 1490 | Py_XDECREF(pyCtxt->f); |
| 1491 | Py_XDECREF(pyCtxt->arg); |
| 1492 | xmlFree(pyCtxt); |
| 1493 | } |
| 1494 | xmlFreeParserCtxt(ctxt); |
| 1495 | } |
| 1496 | |
| 1497 | Py_INCREF(Py_None); |
| 1498 | return(Py_None); |
| 1499 | } |
| 1500 | |
Daniel Veillard | a7340c8 | 2002-02-01 17:56:45 +0000 | [diff] [blame] | 1501 | /************************************************************************ |
| 1502 | * * |
Daniel Veillard | 26f7026 | 2003-01-16 22:45:08 +0000 | [diff] [blame] | 1503 | * Per xmlTextReader error handler * |
| 1504 | * * |
| 1505 | ************************************************************************/ |
| 1506 | |
| 1507 | typedef struct |
| 1508 | { |
| 1509 | PyObject *f; |
| 1510 | PyObject *arg; |
| 1511 | } xmlTextReaderPyCtxt; |
| 1512 | typedef xmlTextReaderPyCtxt *xmlTextReaderPyCtxtPtr; |
| 1513 | |
| 1514 | static void |
| 1515 | libxml_xmlTextReaderErrorCallback(void *arg, |
| 1516 | const char *msg, |
Daniel Veillard | 417be3a | 2003-01-20 21:26:34 +0000 | [diff] [blame] | 1517 | int severity, |
| 1518 | xmlTextReaderLocatorPtr locator) |
Daniel Veillard | 26f7026 | 2003-01-16 22:45:08 +0000 | [diff] [blame] | 1519 | { |
| 1520 | xmlTextReaderPyCtxt *pyCtxt = (xmlTextReaderPyCtxt *)arg; |
| 1521 | PyObject *list; |
| 1522 | PyObject *result; |
| 1523 | |
Daniel Veillard | 417be3a | 2003-01-20 21:26:34 +0000 | [diff] [blame] | 1524 | list = PyTuple_New(4); |
Daniel Veillard | 26f7026 | 2003-01-16 22:45:08 +0000 | [diff] [blame] | 1525 | PyTuple_SetItem(list, 0, pyCtxt->arg); |
| 1526 | Py_XINCREF(pyCtxt->arg); |
| 1527 | PyTuple_SetItem(list, 1, libxml_charPtrConstWrap(msg)); |
Daniel Veillard | 417be3a | 2003-01-20 21:26:34 +0000 | [diff] [blame] | 1528 | PyTuple_SetItem(list, 2, libxml_intWrap(severity)); |
| 1529 | PyTuple_SetItem(list, 3, libxml_xmlTextReaderLocatorPtrWrap(locator)); |
Daniel Veillard | 26f7026 | 2003-01-16 22:45:08 +0000 | [diff] [blame] | 1530 | result = PyEval_CallObject(pyCtxt->f, list); |
| 1531 | if (result == NULL) |
| 1532 | { |
Daniel Veillard | 417be3a | 2003-01-20 21:26:34 +0000 | [diff] [blame] | 1533 | /* TODO: manage for the exception to be propagated... */ |
Daniel Veillard | 26f7026 | 2003-01-16 22:45:08 +0000 | [diff] [blame] | 1534 | PyErr_Print(); |
| 1535 | } |
| 1536 | Py_XDECREF(list); |
| 1537 | Py_XDECREF(result); |
| 1538 | } |
| 1539 | |
| 1540 | PyObject * |
| 1541 | libxml_xmlTextReaderSetErrorHandler(ATTRIBUTE_UNUSED PyObject *self, PyObject *args) |
| 1542 | { |
| 1543 | xmlTextReaderPtr reader; |
| 1544 | xmlTextReaderPyCtxtPtr pyCtxt; |
| 1545 | xmlTextReaderErrorFunc f; |
| 1546 | void *arg; |
| 1547 | PyObject *pyobj_reader; |
| 1548 | PyObject *pyobj_f; |
| 1549 | PyObject *pyobj_arg; |
| 1550 | PyObject *py_retval; |
| 1551 | |
| 1552 | if (!PyArg_ParseTuple(args, (char *)"OOO:xmlTextReaderSetErrorHandler", &pyobj_reader, &pyobj_f, &pyobj_arg)) |
| 1553 | return(NULL); |
| 1554 | reader = (xmlTextReaderPtr) PyxmlTextReader_Get(pyobj_reader); |
| 1555 | /* clear previous error handler */ |
| 1556 | xmlTextReaderGetErrorHandler(reader,&f,&arg); |
| 1557 | if (arg != NULL) { |
| 1558 | if (f == libxml_xmlTextReaderErrorCallback) { |
| 1559 | /* ok, it's our error handler! */ |
| 1560 | pyCtxt = (xmlTextReaderPyCtxtPtr)arg; |
| 1561 | Py_XDECREF(pyCtxt->f); |
| 1562 | Py_XDECREF(pyCtxt->arg); |
| 1563 | xmlFree(pyCtxt); |
| 1564 | } |
| 1565 | else { |
| 1566 | /* |
| 1567 | * there already an arg, and it's not ours, |
| 1568 | * there is definitely something wrong going on here... |
| 1569 | * we don't know how to free it, so we bail out... |
| 1570 | */ |
| 1571 | py_retval = libxml_intWrap(-1); |
| 1572 | return(py_retval); |
| 1573 | } |
| 1574 | } |
| 1575 | xmlTextReaderSetErrorHandler(reader,NULL,NULL); |
| 1576 | /* set new error handler */ |
| 1577 | if (pyobj_f != Py_None) |
| 1578 | { |
| 1579 | pyCtxt = (xmlTextReaderPyCtxtPtr)xmlMalloc(sizeof(xmlTextReaderPyCtxt)); |
| 1580 | if (pyCtxt == NULL) { |
| 1581 | py_retval = libxml_intWrap(-1); |
| 1582 | return(py_retval); |
| 1583 | } |
| 1584 | Py_XINCREF(pyobj_f); |
| 1585 | pyCtxt->f = pyobj_f; |
| 1586 | Py_XINCREF(pyobj_arg); |
| 1587 | pyCtxt->arg = pyobj_arg; |
| 1588 | xmlTextReaderSetErrorHandler(reader,libxml_xmlTextReaderErrorCallback,pyCtxt); |
| 1589 | } |
| 1590 | |
| 1591 | py_retval = libxml_intWrap(1); |
| 1592 | return(py_retval); |
| 1593 | } |
| 1594 | |
| 1595 | PyObject * |
| 1596 | libxml_xmlTextReaderGetErrorHandler(ATTRIBUTE_UNUSED PyObject *self, PyObject *args) |
| 1597 | { |
| 1598 | xmlTextReaderPtr reader; |
| 1599 | xmlTextReaderPyCtxtPtr pyCtxt; |
| 1600 | xmlTextReaderErrorFunc f; |
| 1601 | void *arg; |
| 1602 | PyObject *pyobj_reader; |
| 1603 | PyObject *py_retval; |
| 1604 | |
| 1605 | if (!PyArg_ParseTuple(args, (char *)"O:xmlTextReaderSetErrorHandler", &pyobj_reader)) |
| 1606 | return(NULL); |
| 1607 | reader = (xmlTextReaderPtr) PyxmlTextReader_Get(pyobj_reader); |
| 1608 | xmlTextReaderGetErrorHandler(reader,&f,&arg); |
| 1609 | py_retval = PyTuple_New(2); |
| 1610 | if (f == libxml_xmlTextReaderErrorCallback) { |
| 1611 | /* ok, it's our error handler! */ |
| 1612 | pyCtxt = (xmlTextReaderPyCtxtPtr)arg; |
| 1613 | PyTuple_SetItem(py_retval, 0, pyCtxt->f); |
| 1614 | Py_XINCREF(pyCtxt->f); |
| 1615 | PyTuple_SetItem(py_retval, 1, pyCtxt->arg); |
| 1616 | Py_XINCREF(pyCtxt->arg); |
| 1617 | } |
| 1618 | else |
| 1619 | { |
| 1620 | /* f is null or it's not our error handler */ |
| 1621 | PyTuple_SetItem(py_retval, 0, Py_None); |
| 1622 | Py_XINCREF(Py_None); |
| 1623 | PyTuple_SetItem(py_retval, 1, Py_None); |
| 1624 | Py_XINCREF(Py_None); |
| 1625 | } |
| 1626 | return(py_retval); |
| 1627 | } |
| 1628 | |
| 1629 | PyObject * |
| 1630 | libxml_xmlFreeTextReader(ATTRIBUTE_UNUSED PyObject *self, PyObject *args) { |
| 1631 | xmlTextReaderPtr reader; |
| 1632 | PyObject *pyobj_reader; |
| 1633 | xmlTextReaderPyCtxtPtr pyCtxt; |
| 1634 | xmlTextReaderErrorFunc f; |
| 1635 | void *arg; |
| 1636 | |
| 1637 | if (!PyArg_ParseTuple(args, (char *)"O:xmlFreeTextReader", &pyobj_reader)) |
| 1638 | return(NULL); |
| 1639 | reader = (xmlTextReaderPtr) PyxmlTextReader_Get(pyobj_reader); |
| 1640 | |
| 1641 | xmlTextReaderGetErrorHandler(reader,&f,&arg); |
| 1642 | if (arg != NULL) { |
| 1643 | if (f == libxml_xmlTextReaderErrorCallback) { |
| 1644 | /* ok, it's our error handler! */ |
| 1645 | pyCtxt = (xmlTextReaderPyCtxtPtr)arg; |
| 1646 | Py_XDECREF(pyCtxt->f); |
| 1647 | Py_XDECREF(pyCtxt->arg); |
| 1648 | xmlFree(pyCtxt); |
| 1649 | } |
| 1650 | /* |
| 1651 | * else, something wrong happened, because the error handler is |
| 1652 | * not owned by the python bindings... |
| 1653 | */ |
| 1654 | } |
| 1655 | |
| 1656 | xmlFreeTextReader(reader); |
| 1657 | Py_INCREF(Py_None); |
| 1658 | return(Py_None); |
| 1659 | } |
| 1660 | |
| 1661 | /************************************************************************ |
| 1662 | * * |
Daniel Veillard | a7340c8 | 2002-02-01 17:56:45 +0000 | [diff] [blame] | 1663 | * XPath extensions * |
| 1664 | * * |
| 1665 | ************************************************************************/ |
| 1666 | |
| 1667 | static int libxml_xpathCallbacksInitialized = 0; |
| 1668 | |
| 1669 | typedef struct libxml_xpathCallback { |
| 1670 | xmlXPathContextPtr ctx; |
| 1671 | xmlChar *name; |
| 1672 | xmlChar *ns_uri; |
| 1673 | PyObject *function; |
| 1674 | } libxml_xpathCallback, *libxml_xpathCallbackPtr; |
| 1675 | static libxml_xpathCallback libxml_xpathCallbacks[10]; |
| 1676 | static int libxml_xpathCallbacksNb = 0; |
| 1677 | static int libxml_xpathCallbacksMax = 10; |
| 1678 | |
Daniel Veillard | a7340c8 | 2002-02-01 17:56:45 +0000 | [diff] [blame] | 1679 | static void |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1680 | libxml_xmlXPathFuncCallback(xmlXPathParserContextPtr ctxt, int nargs) |
| 1681 | { |
Daniel Veillard | a7340c8 | 2002-02-01 17:56:45 +0000 | [diff] [blame] | 1682 | PyObject *list, *cur, *result; |
| 1683 | xmlXPathObjectPtr obj; |
Daniel Veillard | 70cab35 | 2002-02-06 16:06:58 +0000 | [diff] [blame] | 1684 | xmlXPathContextPtr rctxt; |
| 1685 | PyObject *current_function = NULL; |
| 1686 | const xmlChar *name; |
| 1687 | const xmlChar *ns_uri; |
Daniel Veillard | a7340c8 | 2002-02-01 17:56:45 +0000 | [diff] [blame] | 1688 | int i; |
| 1689 | |
Daniel Veillard | 70cab35 | 2002-02-06 16:06:58 +0000 | [diff] [blame] | 1690 | if (ctxt == NULL) |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1691 | return; |
Daniel Veillard | 70cab35 | 2002-02-06 16:06:58 +0000 | [diff] [blame] | 1692 | rctxt = ctxt->context; |
| 1693 | if (rctxt == NULL) |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1694 | return; |
Daniel Veillard | 70cab35 | 2002-02-06 16:06:58 +0000 | [diff] [blame] | 1695 | name = rctxt->function; |
| 1696 | ns_uri = rctxt->functionURI; |
Daniel Veillard | a7340c8 | 2002-02-01 17:56:45 +0000 | [diff] [blame] | 1697 | #ifdef DEBUG_XPATH |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1698 | printf("libxml_xmlXPathFuncCallback called name %s URI %s\n", name, |
| 1699 | ns_uri); |
Daniel Veillard | a7340c8 | 2002-02-01 17:56:45 +0000 | [diff] [blame] | 1700 | #endif |
| 1701 | |
Daniel Veillard | 70cab35 | 2002-02-06 16:06:58 +0000 | [diff] [blame] | 1702 | /* |
| 1703 | * Find the function, it should be there it was there at lookup |
| 1704 | */ |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1705 | for (i = 0; i < libxml_xpathCallbacksNb; i++) { |
| 1706 | if ( /* TODO (ctxt == libxml_xpathCallbacks[i].ctx) && */ |
| 1707 | (xmlStrEqual(name, libxml_xpathCallbacks[i].name)) && |
| 1708 | (xmlStrEqual(ns_uri, libxml_xpathCallbacks[i].ns_uri))) { |
| 1709 | current_function = libxml_xpathCallbacks[i].function; |
| 1710 | } |
Daniel Veillard | 70cab35 | 2002-02-06 16:06:58 +0000 | [diff] [blame] | 1711 | } |
| 1712 | if (current_function == NULL) { |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1713 | printf |
| 1714 | ("libxml_xmlXPathFuncCallback: internal error %s not found !\n", |
| 1715 | name); |
| 1716 | return; |
Daniel Veillard | 70cab35 | 2002-02-06 16:06:58 +0000 | [diff] [blame] | 1717 | } |
| 1718 | |
Daniel Veillard | c575b99 | 2002-02-08 13:28:40 +0000 | [diff] [blame] | 1719 | list = PyTuple_New(nargs + 1); |
| 1720 | PyTuple_SetItem(list, 0, libxml_xmlXPathParserContextPtrWrap(ctxt)); |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1721 | for (i = 0; i < nargs; i++) { |
| 1722 | obj = valuePop(ctxt); |
| 1723 | cur = libxml_xmlXPathObjectPtrWrap(obj); |
| 1724 | PyTuple_SetItem(list, i + 1, cur); |
Daniel Veillard | a7340c8 | 2002-02-01 17:56:45 +0000 | [diff] [blame] | 1725 | } |
| 1726 | result = PyEval_CallObject(current_function, list); |
| 1727 | Py_DECREF(list); |
| 1728 | |
| 1729 | obj = libxml_xmlXPathObjectPtrConvert(result); |
| 1730 | valuePush(ctxt, obj); |
| 1731 | } |
| 1732 | |
| 1733 | static xmlXPathFunction |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1734 | libxml_xmlXPathFuncLookupFunc(void *ctxt, const xmlChar * name, |
| 1735 | const xmlChar * ns_uri) |
| 1736 | { |
Daniel Veillard | a7340c8 | 2002-02-01 17:56:45 +0000 | [diff] [blame] | 1737 | int i; |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1738 | |
Daniel Veillard | a7340c8 | 2002-02-01 17:56:45 +0000 | [diff] [blame] | 1739 | #ifdef DEBUG_XPATH |
| 1740 | printf("libxml_xmlXPathFuncLookupFunc(%p, %s, %s) called\n", |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1741 | ctxt, name, ns_uri); |
Daniel Veillard | a7340c8 | 2002-02-01 17:56:45 +0000 | [diff] [blame] | 1742 | #endif |
Daniel Veillard | 70cab35 | 2002-02-06 16:06:58 +0000 | [diff] [blame] | 1743 | /* |
| 1744 | * This is called once only. The address is then stored in the |
| 1745 | * XPath expression evaluation, the proper object to call can |
| 1746 | * then still be found using the execution context function |
| 1747 | * and functionURI fields. |
| 1748 | */ |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1749 | for (i = 0; i < libxml_xpathCallbacksNb; i++) { |
| 1750 | if ((ctxt == libxml_xpathCallbacks[i].ctx) && |
| 1751 | (xmlStrEqual(name, libxml_xpathCallbacks[i].name)) && |
| 1752 | (xmlStrEqual(ns_uri, libxml_xpathCallbacks[i].ns_uri))) { |
| 1753 | return (libxml_xmlXPathFuncCallback); |
| 1754 | } |
Daniel Veillard | a7340c8 | 2002-02-01 17:56:45 +0000 | [diff] [blame] | 1755 | } |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1756 | return (NULL); |
Daniel Veillard | a7340c8 | 2002-02-01 17:56:45 +0000 | [diff] [blame] | 1757 | } |
| 1758 | |
| 1759 | static void |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1760 | libxml_xpathCallbacksInitialize(void) |
| 1761 | { |
Daniel Veillard | a7340c8 | 2002-02-01 17:56:45 +0000 | [diff] [blame] | 1762 | int i; |
| 1763 | |
| 1764 | if (libxml_xpathCallbacksInitialized != 0) |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1765 | return; |
Daniel Veillard | a7340c8 | 2002-02-01 17:56:45 +0000 | [diff] [blame] | 1766 | |
| 1767 | #ifdef DEBUG_XPATH |
| 1768 | printf("libxml_xpathCallbacksInitialized called\n"); |
| 1769 | #endif |
| 1770 | |
Daniel Veillard | 781ac8b | 2003-05-15 22:11:36 +0000 | [diff] [blame^] | 1771 | for (i = 0; i < libxml_xpathCallbacksMax; i++) { |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1772 | libxml_xpathCallbacks[i].ctx = NULL; |
| 1773 | libxml_xpathCallbacks[i].name = NULL; |
| 1774 | libxml_xpathCallbacks[i].ns_uri = NULL; |
| 1775 | libxml_xpathCallbacks[i].function = NULL; |
Daniel Veillard | a7340c8 | 2002-02-01 17:56:45 +0000 | [diff] [blame] | 1776 | } |
Daniel Veillard | a7340c8 | 2002-02-01 17:56:45 +0000 | [diff] [blame] | 1777 | libxml_xpathCallbacksInitialized = 1; |
| 1778 | } |
| 1779 | |
| 1780 | PyObject * |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1781 | libxml_xmlRegisterXPathFunction(ATTRIBUTE_UNUSED PyObject * self, |
| 1782 | PyObject * args) |
| 1783 | { |
Daniel Veillard | a7340c8 | 2002-02-01 17:56:45 +0000 | [diff] [blame] | 1784 | PyObject *py_retval; |
| 1785 | int c_retval = 0; |
| 1786 | xmlChar *name; |
| 1787 | xmlChar *ns_uri; |
| 1788 | xmlXPathContextPtr ctx; |
| 1789 | PyObject *pyobj_ctx; |
| 1790 | PyObject *pyobj_f; |
| 1791 | int i; |
| 1792 | |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1793 | if (!PyArg_ParseTuple |
| 1794 | (args, (char *) "OszO:registerXPathFunction", &pyobj_ctx, &name, |
| 1795 | &ns_uri, &pyobj_f)) |
| 1796 | return (NULL); |
Daniel Veillard | a7340c8 | 2002-02-01 17:56:45 +0000 | [diff] [blame] | 1797 | |
| 1798 | ctx = (xmlXPathContextPtr) PyxmlXPathContext_Get(pyobj_ctx); |
| 1799 | if (libxml_xpathCallbacksInitialized == 0) |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1800 | libxml_xpathCallbacksInitialize(); |
Daniel Veillard | a7340c8 | 2002-02-01 17:56:45 +0000 | [diff] [blame] | 1801 | xmlXPathRegisterFuncLookup(ctx, libxml_xmlXPathFuncLookupFunc, ctx); |
| 1802 | |
| 1803 | if ((pyobj_ctx == NULL) || (name == NULL) || (pyobj_f == NULL)) { |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1804 | py_retval = libxml_intWrap(-1); |
| 1805 | return (py_retval); |
Daniel Veillard | a7340c8 | 2002-02-01 17:56:45 +0000 | [diff] [blame] | 1806 | } |
Daniel Veillard | a7340c8 | 2002-02-01 17:56:45 +0000 | [diff] [blame] | 1807 | #ifdef DEBUG_XPATH |
| 1808 | printf("libxml_registerXPathFunction(%p, %s, %s) called\n", |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1809 | ctx, name, ns_uri); |
Daniel Veillard | a7340c8 | 2002-02-01 17:56:45 +0000 | [diff] [blame] | 1810 | #endif |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1811 | for (i = 0; i < libxml_xpathCallbacksNb; i++) { |
| 1812 | if ((ctx == libxml_xpathCallbacks[i].ctx) && |
| 1813 | (xmlStrEqual(name, libxml_xpathCallbacks[i].name)) && |
| 1814 | (xmlStrEqual(ns_uri, libxml_xpathCallbacks[i].ns_uri))) { |
| 1815 | Py_XINCREF(pyobj_f); |
| 1816 | Py_XDECREF(libxml_xpathCallbacks[i].function); |
| 1817 | libxml_xpathCallbacks[i].function = pyobj_f; |
| 1818 | c_retval = 1; |
| 1819 | goto done; |
| 1820 | } |
Daniel Veillard | a7340c8 | 2002-02-01 17:56:45 +0000 | [diff] [blame] | 1821 | } |
| 1822 | if (libxml_xpathCallbacksNb >= libxml_xpathCallbacksMax) { |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1823 | printf("libxml_registerXPathFunction() table full\n"); |
Daniel Veillard | a7340c8 | 2002-02-01 17:56:45 +0000 | [diff] [blame] | 1824 | } else { |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1825 | i = libxml_xpathCallbacksNb++; |
| 1826 | Py_XINCREF(pyobj_f); |
Daniel Veillard | a7340c8 | 2002-02-01 17:56:45 +0000 | [diff] [blame] | 1827 | libxml_xpathCallbacks[i].ctx = ctx; |
| 1828 | libxml_xpathCallbacks[i].name = xmlStrdup(name); |
| 1829 | libxml_xpathCallbacks[i].ns_uri = xmlStrdup(ns_uri); |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1830 | libxml_xpathCallbacks[i].function = pyobj_f; |
| 1831 | c_retval = 1; |
Daniel Veillard | a7340c8 | 2002-02-01 17:56:45 +0000 | [diff] [blame] | 1832 | } |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1833 | done: |
Daniel Veillard | a7340c8 | 2002-02-01 17:56:45 +0000 | [diff] [blame] | 1834 | py_retval = libxml_intWrap((int) c_retval); |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1835 | return (py_retval); |
Daniel Veillard | a7340c8 | 2002-02-01 17:56:45 +0000 | [diff] [blame] | 1836 | } |
| 1837 | |
Daniel Veillard | 1971ee2 | 2002-01-31 20:29:19 +0000 | [diff] [blame] | 1838 | /************************************************************************ |
| 1839 | * * |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 1840 | * Global properties access * |
| 1841 | * * |
| 1842 | ************************************************************************/ |
| 1843 | static PyObject * |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1844 | libxml_name(ATTRIBUTE_UNUSED PyObject * self, PyObject * args) |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 1845 | { |
| 1846 | PyObject *resultobj, *obj; |
| 1847 | xmlNodePtr cur; |
| 1848 | const xmlChar *res; |
| 1849 | |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1850 | if (!PyArg_ParseTuple(args, (char *) "O:name", &obj)) |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 1851 | return NULL; |
| 1852 | cur = PyxmlNode_Get(obj); |
| 1853 | |
| 1854 | #ifdef DEBUG |
| 1855 | printf("libxml_name: cur = %p type %d\n", cur, cur->type); |
| 1856 | #endif |
| 1857 | |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1858 | switch (cur->type) { |
| 1859 | case XML_DOCUMENT_NODE: |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 1860 | #ifdef LIBXML_DOCB_ENABLED |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1861 | case XML_DOCB_DOCUMENT_NODE: |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 1862 | #endif |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1863 | case XML_HTML_DOCUMENT_NODE:{ |
| 1864 | xmlDocPtr doc = (xmlDocPtr) cur; |
| 1865 | |
| 1866 | res = doc->URL; |
| 1867 | break; |
| 1868 | } |
| 1869 | case XML_ATTRIBUTE_NODE:{ |
| 1870 | xmlAttrPtr attr = (xmlAttrPtr) cur; |
| 1871 | |
| 1872 | res = attr->name; |
| 1873 | break; |
| 1874 | } |
| 1875 | case XML_NAMESPACE_DECL:{ |
| 1876 | xmlNsPtr ns = (xmlNsPtr) cur; |
| 1877 | |
| 1878 | res = ns->prefix; |
| 1879 | break; |
| 1880 | } |
| 1881 | default: |
| 1882 | res = cur->name; |
| 1883 | break; |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 1884 | } |
Daniel Veillard | 1971ee2 | 2002-01-31 20:29:19 +0000 | [diff] [blame] | 1885 | resultobj = libxml_constxmlCharPtrWrap(res); |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 1886 | |
| 1887 | return resultobj; |
| 1888 | } |
| 1889 | |
| 1890 | static PyObject * |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1891 | libxml_doc(ATTRIBUTE_UNUSED PyObject * self, PyObject * args) |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 1892 | { |
| 1893 | PyObject *resultobj, *obj; |
| 1894 | xmlNodePtr cur; |
| 1895 | xmlDocPtr res; |
| 1896 | |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1897 | if (!PyArg_ParseTuple(args, (char *) "O:doc", &obj)) |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 1898 | return NULL; |
| 1899 | cur = PyxmlNode_Get(obj); |
| 1900 | |
| 1901 | #ifdef DEBUG |
| 1902 | printf("libxml_doc: cur = %p\n", cur); |
| 1903 | #endif |
| 1904 | |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1905 | switch (cur->type) { |
| 1906 | case XML_DOCUMENT_NODE: |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 1907 | #ifdef LIBXML_DOCB_ENABLED |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1908 | case XML_DOCB_DOCUMENT_NODE: |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 1909 | #endif |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1910 | case XML_HTML_DOCUMENT_NODE: |
| 1911 | res = NULL; |
| 1912 | break; |
| 1913 | case XML_ATTRIBUTE_NODE:{ |
| 1914 | xmlAttrPtr attr = (xmlAttrPtr) cur; |
| 1915 | |
| 1916 | res = attr->doc; |
| 1917 | break; |
| 1918 | } |
| 1919 | case XML_NAMESPACE_DECL: |
| 1920 | res = NULL; |
| 1921 | break; |
| 1922 | default: |
| 1923 | res = cur->doc; |
| 1924 | break; |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 1925 | } |
| 1926 | resultobj = libxml_xmlDocPtrWrap(res); |
| 1927 | return resultobj; |
| 1928 | } |
| 1929 | |
| 1930 | static PyObject * |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1931 | libxml_properties(ATTRIBUTE_UNUSED PyObject * self, PyObject * args) |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 1932 | { |
| 1933 | PyObject *resultobj, *obj; |
| 1934 | xmlNodePtr cur = NULL; |
| 1935 | xmlAttrPtr res; |
| 1936 | |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1937 | if (!PyArg_ParseTuple(args, (char *) "O:properties", &obj)) |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 1938 | return NULL; |
| 1939 | cur = PyxmlNode_Get(obj); |
| 1940 | if (cur->type == XML_ELEMENT_NODE) |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1941 | res = cur->properties; |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 1942 | else |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1943 | res = NULL; |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 1944 | resultobj = libxml_xmlAttrPtrWrap(res); |
| 1945 | return resultobj; |
| 1946 | } |
| 1947 | |
| 1948 | static PyObject * |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1949 | libxml_next(ATTRIBUTE_UNUSED PyObject * self, PyObject * args) |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 1950 | { |
| 1951 | PyObject *resultobj, *obj; |
| 1952 | xmlNodePtr cur; |
| 1953 | xmlNodePtr res; |
| 1954 | |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1955 | if (!PyArg_ParseTuple(args, (char *) "O:next", &obj)) |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 1956 | return NULL; |
| 1957 | cur = PyxmlNode_Get(obj); |
| 1958 | |
| 1959 | #ifdef DEBUG |
| 1960 | printf("libxml_next: cur = %p\n", cur); |
| 1961 | #endif |
| 1962 | |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1963 | switch (cur->type) { |
| 1964 | case XML_DOCUMENT_NODE: |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 1965 | #ifdef LIBXML_DOCB_ENABLED |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1966 | case XML_DOCB_DOCUMENT_NODE: |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 1967 | #endif |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1968 | case XML_HTML_DOCUMENT_NODE: |
| 1969 | res = NULL; |
| 1970 | break; |
| 1971 | case XML_ATTRIBUTE_NODE:{ |
| 1972 | xmlAttrPtr attr = (xmlAttrPtr) cur; |
| 1973 | |
| 1974 | res = (xmlNodePtr) attr->next; |
| 1975 | break; |
| 1976 | } |
| 1977 | case XML_NAMESPACE_DECL:{ |
| 1978 | xmlNsPtr ns = (xmlNsPtr) cur; |
| 1979 | |
| 1980 | res = (xmlNodePtr) ns->next; |
| 1981 | break; |
| 1982 | } |
| 1983 | default: |
| 1984 | res = cur->next; |
| 1985 | break; |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 1986 | |
| 1987 | } |
| 1988 | resultobj = libxml_xmlNodePtrWrap(res); |
| 1989 | return resultobj; |
| 1990 | } |
| 1991 | |
| 1992 | static PyObject * |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1993 | libxml_prev(ATTRIBUTE_UNUSED PyObject * self, PyObject * args) |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 1994 | { |
| 1995 | PyObject *resultobj, *obj; |
| 1996 | xmlNodePtr cur; |
| 1997 | xmlNodePtr res; |
| 1998 | |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1999 | if (!PyArg_ParseTuple(args, (char *) "O:prev", &obj)) |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 2000 | return NULL; |
| 2001 | cur = PyxmlNode_Get(obj); |
| 2002 | |
| 2003 | #ifdef DEBUG |
| 2004 | printf("libxml_prev: cur = %p\n", cur); |
| 2005 | #endif |
| 2006 | |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2007 | switch (cur->type) { |
| 2008 | case XML_DOCUMENT_NODE: |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 2009 | #ifdef LIBXML_DOCB_ENABLED |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2010 | case XML_DOCB_DOCUMENT_NODE: |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 2011 | #endif |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2012 | case XML_HTML_DOCUMENT_NODE: |
| 2013 | res = NULL; |
| 2014 | break; |
| 2015 | case XML_ATTRIBUTE_NODE:{ |
| 2016 | xmlAttrPtr attr = (xmlAttrPtr) cur; |
| 2017 | |
Daniel Veillard | faa35ff | 2002-11-24 13:53:43 +0000 | [diff] [blame] | 2018 | res = (xmlNodePtr) attr->prev; |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2019 | } |
| 2020 | case XML_NAMESPACE_DECL: |
| 2021 | res = NULL; |
| 2022 | break; |
| 2023 | default: |
Daniel Veillard | faa35ff | 2002-11-24 13:53:43 +0000 | [diff] [blame] | 2024 | res = cur->prev; |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2025 | break; |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 2026 | } |
| 2027 | resultobj = libxml_xmlNodePtrWrap(res); |
| 2028 | return resultobj; |
| 2029 | } |
| 2030 | |
| 2031 | static PyObject * |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2032 | libxml_children(ATTRIBUTE_UNUSED PyObject * self, PyObject * args) |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 2033 | { |
| 2034 | PyObject *resultobj, *obj; |
| 2035 | xmlNodePtr cur; |
| 2036 | xmlNodePtr res; |
| 2037 | |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2038 | if (!PyArg_ParseTuple(args, (char *) "O:children", &obj)) |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 2039 | return NULL; |
| 2040 | cur = PyxmlNode_Get(obj); |
| 2041 | |
| 2042 | #ifdef DEBUG |
| 2043 | printf("libxml_children: cur = %p\n", cur); |
| 2044 | #endif |
| 2045 | |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2046 | switch (cur->type) { |
| 2047 | case XML_ELEMENT_NODE: |
| 2048 | case XML_ENTITY_REF_NODE: |
| 2049 | case XML_ENTITY_NODE: |
| 2050 | case XML_PI_NODE: |
| 2051 | case XML_COMMENT_NODE: |
| 2052 | case XML_DOCUMENT_NODE: |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 2053 | #ifdef LIBXML_DOCB_ENABLED |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2054 | case XML_DOCB_DOCUMENT_NODE: |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 2055 | #endif |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2056 | case XML_HTML_DOCUMENT_NODE: |
| 2057 | case XML_DTD_NODE: |
| 2058 | res = cur->children; |
| 2059 | break; |
| 2060 | case XML_ATTRIBUTE_NODE:{ |
| 2061 | xmlAttrPtr attr = (xmlAttrPtr) cur; |
| 2062 | |
| 2063 | res = attr->children; |
| 2064 | break; |
| 2065 | } |
| 2066 | default: |
| 2067 | res = NULL; |
| 2068 | break; |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 2069 | } |
| 2070 | resultobj = libxml_xmlNodePtrWrap(res); |
| 2071 | return resultobj; |
| 2072 | } |
| 2073 | |
| 2074 | static PyObject * |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2075 | libxml_last(ATTRIBUTE_UNUSED PyObject * self, PyObject * args) |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 2076 | { |
| 2077 | PyObject *resultobj, *obj; |
| 2078 | xmlNodePtr cur; |
| 2079 | xmlNodePtr res; |
| 2080 | |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2081 | if (!PyArg_ParseTuple(args, (char *) "O:last", &obj)) |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 2082 | return NULL; |
| 2083 | cur = PyxmlNode_Get(obj); |
| 2084 | |
| 2085 | #ifdef DEBUG |
| 2086 | printf("libxml_last: cur = %p\n", cur); |
| 2087 | #endif |
| 2088 | |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2089 | switch (cur->type) { |
| 2090 | case XML_ELEMENT_NODE: |
| 2091 | case XML_ENTITY_REF_NODE: |
| 2092 | case XML_ENTITY_NODE: |
| 2093 | case XML_PI_NODE: |
| 2094 | case XML_COMMENT_NODE: |
| 2095 | case XML_DOCUMENT_NODE: |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 2096 | #ifdef LIBXML_DOCB_ENABLED |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2097 | case XML_DOCB_DOCUMENT_NODE: |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 2098 | #endif |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2099 | case XML_HTML_DOCUMENT_NODE: |
| 2100 | case XML_DTD_NODE: |
| 2101 | res = cur->last; |
| 2102 | break; |
| 2103 | case XML_ATTRIBUTE_NODE:{ |
| 2104 | xmlAttrPtr attr = (xmlAttrPtr) cur; |
| 2105 | |
| 2106 | res = attr->last; |
| 2107 | } |
| 2108 | default: |
| 2109 | res = NULL; |
| 2110 | break; |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 2111 | } |
| 2112 | resultobj = libxml_xmlNodePtrWrap(res); |
| 2113 | return resultobj; |
| 2114 | } |
| 2115 | |
| 2116 | static PyObject * |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2117 | libxml_parent(ATTRIBUTE_UNUSED PyObject * self, PyObject * args) |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 2118 | { |
| 2119 | PyObject *resultobj, *obj; |
| 2120 | xmlNodePtr cur; |
| 2121 | xmlNodePtr res; |
| 2122 | |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2123 | if (!PyArg_ParseTuple(args, (char *) "O:parent", &obj)) |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 2124 | return NULL; |
| 2125 | cur = PyxmlNode_Get(obj); |
| 2126 | |
| 2127 | #ifdef DEBUG |
| 2128 | printf("libxml_parent: cur = %p\n", cur); |
| 2129 | #endif |
| 2130 | |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2131 | switch (cur->type) { |
| 2132 | case XML_DOCUMENT_NODE: |
| 2133 | case XML_HTML_DOCUMENT_NODE: |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 2134 | #ifdef LIBXML_DOCB_ENABLED |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2135 | case XML_DOCB_DOCUMENT_NODE: |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 2136 | #endif |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2137 | res = NULL; |
| 2138 | break; |
| 2139 | case XML_ATTRIBUTE_NODE:{ |
| 2140 | xmlAttrPtr attr = (xmlAttrPtr) cur; |
| 2141 | |
| 2142 | res = attr->parent; |
| 2143 | } |
| 2144 | case XML_ENTITY_DECL: |
| 2145 | case XML_NAMESPACE_DECL: |
| 2146 | case XML_XINCLUDE_START: |
| 2147 | case XML_XINCLUDE_END: |
| 2148 | res = NULL; |
| 2149 | break; |
| 2150 | default: |
| 2151 | res = cur->parent; |
| 2152 | break; |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 2153 | } |
| 2154 | resultobj = libxml_xmlNodePtrWrap(res); |
| 2155 | return resultobj; |
| 2156 | } |
| 2157 | |
| 2158 | static PyObject * |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2159 | libxml_type(ATTRIBUTE_UNUSED PyObject * self, PyObject * args) |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 2160 | { |
| 2161 | PyObject *resultobj, *obj; |
| 2162 | xmlNodePtr cur; |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2163 | const xmlChar *res = NULL; |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 2164 | |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2165 | if (!PyArg_ParseTuple(args, (char *) "O:last", &obj)) |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 2166 | return NULL; |
| 2167 | cur = PyxmlNode_Get(obj); |
| 2168 | |
| 2169 | #ifdef DEBUG |
| 2170 | printf("libxml_type: cur = %p\n", cur); |
| 2171 | #endif |
| 2172 | |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2173 | switch (cur->type) { |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 2174 | case XML_ELEMENT_NODE: |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2175 | res = (const xmlChar *) "element"; |
| 2176 | break; |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 2177 | case XML_ATTRIBUTE_NODE: |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2178 | res = (const xmlChar *) "attribute"; |
| 2179 | break; |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 2180 | case XML_TEXT_NODE: |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2181 | res = (const xmlChar *) "text"; |
| 2182 | break; |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 2183 | case XML_CDATA_SECTION_NODE: |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2184 | res = (const xmlChar *) "cdata"; |
| 2185 | break; |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 2186 | case XML_ENTITY_REF_NODE: |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2187 | res = (const xmlChar *) "entity_ref"; |
| 2188 | break; |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 2189 | case XML_ENTITY_NODE: |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2190 | res = (const xmlChar *) "entity"; |
| 2191 | break; |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 2192 | case XML_PI_NODE: |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2193 | res = (const xmlChar *) "pi"; |
| 2194 | break; |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 2195 | case XML_COMMENT_NODE: |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2196 | res = (const xmlChar *) "comment"; |
| 2197 | break; |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 2198 | case XML_DOCUMENT_NODE: |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2199 | res = (const xmlChar *) "document_xml"; |
| 2200 | break; |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 2201 | case XML_DOCUMENT_TYPE_NODE: |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2202 | res = (const xmlChar *) "doctype"; |
| 2203 | break; |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 2204 | case XML_DOCUMENT_FRAG_NODE: |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2205 | res = (const xmlChar *) "fragment"; |
| 2206 | break; |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 2207 | case XML_NOTATION_NODE: |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2208 | res = (const xmlChar *) "notation"; |
| 2209 | break; |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 2210 | case XML_HTML_DOCUMENT_NODE: |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2211 | res = (const xmlChar *) "document_html"; |
| 2212 | break; |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 2213 | case XML_DTD_NODE: |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2214 | res = (const xmlChar *) "dtd"; |
| 2215 | break; |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 2216 | case XML_ELEMENT_DECL: |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2217 | res = (const xmlChar *) "elem_decl"; |
| 2218 | break; |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 2219 | case XML_ATTRIBUTE_DECL: |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2220 | res = (const xmlChar *) "attribute_decl"; |
| 2221 | break; |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 2222 | case XML_ENTITY_DECL: |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2223 | res = (const xmlChar *) "entity_decl"; |
| 2224 | break; |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 2225 | case XML_NAMESPACE_DECL: |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2226 | res = (const xmlChar *) "namespace"; |
| 2227 | break; |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 2228 | case XML_XINCLUDE_START: |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2229 | res = (const xmlChar *) "xinclude_start"; |
| 2230 | break; |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 2231 | case XML_XINCLUDE_END: |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2232 | res = (const xmlChar *) "xinclude_end"; |
| 2233 | break; |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 2234 | #ifdef LIBXML_DOCB_ENABLED |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2235 | case XML_DOCB_DOCUMENT_NODE: |
| 2236 | res = (const xmlChar *) "document_docbook"; |
| 2237 | break; |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 2238 | #endif |
| 2239 | } |
| 2240 | #ifdef DEBUG |
| 2241 | printf("libxml_type: cur = %p: %s\n", cur, res); |
| 2242 | #endif |
| 2243 | |
Daniel Veillard | 1971ee2 | 2002-01-31 20:29:19 +0000 | [diff] [blame] | 2244 | resultobj = libxml_constxmlCharPtrWrap(res); |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 2245 | return resultobj; |
| 2246 | } |
| 2247 | |
| 2248 | /************************************************************************ |
| 2249 | * * |
Daniel Veillard | 36eea2d | 2002-02-04 00:17:01 +0000 | [diff] [blame] | 2250 | * Specific accessor functions * |
| 2251 | * * |
| 2252 | ************************************************************************/ |
| 2253 | PyObject * |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2254 | libxml_xmlNodeGetNsDefs(ATTRIBUTE_UNUSED PyObject * self, PyObject * args) |
| 2255 | { |
Daniel Veillard | 36eea2d | 2002-02-04 00:17:01 +0000 | [diff] [blame] | 2256 | PyObject *py_retval; |
| 2257 | xmlNsPtr c_retval; |
| 2258 | xmlNodePtr node; |
| 2259 | PyObject *pyobj_node; |
| 2260 | |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2261 | if (!PyArg_ParseTuple |
| 2262 | (args, (char *) "O:xmlNodeGetNsDefs", &pyobj_node)) |
| 2263 | return (NULL); |
Daniel Veillard | 36eea2d | 2002-02-04 00:17:01 +0000 | [diff] [blame] | 2264 | node = (xmlNodePtr) PyxmlNode_Get(pyobj_node); |
| 2265 | |
| 2266 | if ((node == NULL) || (node->type != XML_ELEMENT_NODE)) { |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2267 | Py_INCREF(Py_None); |
| 2268 | return (Py_None); |
Daniel Veillard | 36eea2d | 2002-02-04 00:17:01 +0000 | [diff] [blame] | 2269 | } |
| 2270 | c_retval = node->nsDef; |
| 2271 | py_retval = libxml_xmlNsPtrWrap((xmlNsPtr) c_retval); |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2272 | return (py_retval); |
Daniel Veillard | 36eea2d | 2002-02-04 00:17:01 +0000 | [diff] [blame] | 2273 | } |
| 2274 | |
| 2275 | PyObject * |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2276 | libxml_xmlNodeGetNs(ATTRIBUTE_UNUSED PyObject * self, PyObject * args) |
| 2277 | { |
Daniel Veillard | 36eea2d | 2002-02-04 00:17:01 +0000 | [diff] [blame] | 2278 | PyObject *py_retval; |
| 2279 | xmlNsPtr c_retval; |
| 2280 | xmlNodePtr node; |
| 2281 | PyObject *pyobj_node; |
| 2282 | |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2283 | if (!PyArg_ParseTuple(args, (char *) "O:xmlNodeGetNs", &pyobj_node)) |
| 2284 | return (NULL); |
Daniel Veillard | 36eea2d | 2002-02-04 00:17:01 +0000 | [diff] [blame] | 2285 | node = (xmlNodePtr) PyxmlNode_Get(pyobj_node); |
| 2286 | |
| 2287 | if ((node == NULL) || (node->type != XML_ELEMENT_NODE)) { |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2288 | Py_INCREF(Py_None); |
| 2289 | return (Py_None); |
Daniel Veillard | 36eea2d | 2002-02-04 00:17:01 +0000 | [diff] [blame] | 2290 | } |
| 2291 | c_retval = node->ns; |
| 2292 | py_retval = libxml_xmlNsPtrWrap((xmlNsPtr) c_retval); |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2293 | return (py_retval); |
Daniel Veillard | 36eea2d | 2002-02-04 00:17:01 +0000 | [diff] [blame] | 2294 | } |
| 2295 | |
| 2296 | /************************************************************************ |
| 2297 | * * |
Daniel Veillard | 1e77438 | 2002-03-06 17:35:40 +0000 | [diff] [blame] | 2298 | * Serialization front-end * |
| 2299 | * * |
| 2300 | ************************************************************************/ |
| 2301 | |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2302 | static PyObject * |
| 2303 | libxml_serializeNode(ATTRIBUTE_UNUSED PyObject * self, PyObject * args) |
| 2304 | { |
Daniel Veillard | 1e77438 | 2002-03-06 17:35:40 +0000 | [diff] [blame] | 2305 | PyObject *py_retval = NULL; |
| 2306 | xmlChar *c_retval; |
| 2307 | PyObject *pyobj_node; |
| 2308 | xmlNodePtr node; |
| 2309 | xmlDocPtr doc; |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2310 | const char *encoding; |
Daniel Veillard | 1e77438 | 2002-03-06 17:35:40 +0000 | [diff] [blame] | 2311 | int format; |
| 2312 | int len; |
| 2313 | |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2314 | if (!PyArg_ParseTuple(args, (char *) "Ozi:serializeNode", &pyobj_node, |
| 2315 | &encoding, &format)) |
| 2316 | return (NULL); |
Daniel Veillard | 1e77438 | 2002-03-06 17:35:40 +0000 | [diff] [blame] | 2317 | node = (xmlNodePtr) PyxmlNode_Get(pyobj_node); |
| 2318 | |
| 2319 | if (node == NULL) { |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2320 | Py_INCREF(Py_None); |
| 2321 | return (Py_None); |
Daniel Veillard | 1e77438 | 2002-03-06 17:35:40 +0000 | [diff] [blame] | 2322 | } |
| 2323 | if (node->type == XML_DOCUMENT_NODE) { |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2324 | doc = (xmlDocPtr) node; |
| 2325 | xmlDocDumpFormatMemoryEnc(doc, &c_retval, &len, |
| 2326 | (const char *) encoding, format); |
| 2327 | py_retval = libxml_charPtrWrap((char *) c_retval); |
Daniel Veillard | 1e77438 | 2002-03-06 17:35:40 +0000 | [diff] [blame] | 2328 | } else if (node->type == XML_HTML_DOCUMENT_NODE) { |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2329 | xmlOutputBufferPtr buf; |
| 2330 | xmlCharEncodingHandlerPtr handler = NULL; |
Daniel Veillard | 1e77438 | 2002-03-06 17:35:40 +0000 | [diff] [blame] | 2331 | |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2332 | doc = (xmlDocPtr) node; |
| 2333 | if (encoding != NULL) |
| 2334 | htmlSetMetaEncoding(doc, (const xmlChar *) encoding); |
| 2335 | encoding = (const char *) htmlGetMetaEncoding(doc); |
Daniel Veillard | 1e77438 | 2002-03-06 17:35:40 +0000 | [diff] [blame] | 2336 | |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2337 | if (encoding != NULL) { |
| 2338 | handler = xmlFindCharEncodingHandler(encoding); |
| 2339 | if (handler == NULL) { |
| 2340 | Py_INCREF(Py_None); |
| 2341 | return (Py_None); |
| 2342 | } |
| 2343 | } |
Daniel Veillard | 1e77438 | 2002-03-06 17:35:40 +0000 | [diff] [blame] | 2344 | |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2345 | /* |
| 2346 | * Fallback to HTML or ASCII when the encoding is unspecified |
| 2347 | */ |
| 2348 | if (handler == NULL) |
| 2349 | handler = xmlFindCharEncodingHandler("HTML"); |
| 2350 | if (handler == NULL) |
| 2351 | handler = xmlFindCharEncodingHandler("ascii"); |
Daniel Veillard | 1e77438 | 2002-03-06 17:35:40 +0000 | [diff] [blame] | 2352 | |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2353 | buf = xmlAllocOutputBuffer(handler); |
| 2354 | if (buf == NULL) { |
| 2355 | Py_INCREF(Py_None); |
| 2356 | return (Py_None); |
| 2357 | } |
| 2358 | htmlDocContentDumpFormatOutput(buf, doc, encoding, format); |
| 2359 | xmlOutputBufferFlush(buf); |
| 2360 | if (buf->conv != NULL) { |
| 2361 | len = buf->conv->use; |
| 2362 | c_retval = buf->conv->content; |
| 2363 | buf->conv->content = NULL; |
| 2364 | } else { |
| 2365 | len = buf->buffer->use; |
| 2366 | c_retval = buf->buffer->content; |
| 2367 | buf->buffer->content = NULL; |
| 2368 | } |
| 2369 | (void) xmlOutputBufferClose(buf); |
| 2370 | py_retval = libxml_charPtrWrap((char *) c_retval); |
Daniel Veillard | 1e77438 | 2002-03-06 17:35:40 +0000 | [diff] [blame] | 2371 | } else { |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2372 | doc = node->doc; |
Daniel Veillard | a8c0adb | 2002-11-17 22:37:35 +0000 | [diff] [blame] | 2373 | if ((doc == NULL) || (doc->type == XML_DOCUMENT_NODE)) { |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2374 | xmlOutputBufferPtr buf; |
| 2375 | xmlCharEncodingHandlerPtr handler = NULL; |
Daniel Veillard | 1e77438 | 2002-03-06 17:35:40 +0000 | [diff] [blame] | 2376 | |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2377 | if (encoding != NULL) { |
| 2378 | handler = xmlFindCharEncodingHandler(encoding); |
| 2379 | if (handler == NULL) { |
| 2380 | Py_INCREF(Py_None); |
| 2381 | return (Py_None); |
| 2382 | } |
| 2383 | } |
Daniel Veillard | 1e77438 | 2002-03-06 17:35:40 +0000 | [diff] [blame] | 2384 | |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2385 | buf = xmlAllocOutputBuffer(handler); |
| 2386 | if (buf == NULL) { |
| 2387 | Py_INCREF(Py_None); |
| 2388 | return (Py_None); |
| 2389 | } |
| 2390 | xmlNodeDumpOutput(buf, doc, node, 0, format, encoding); |
| 2391 | xmlOutputBufferFlush(buf); |
| 2392 | if (buf->conv != NULL) { |
| 2393 | len = buf->conv->use; |
| 2394 | c_retval = buf->conv->content; |
| 2395 | buf->conv->content = NULL; |
| 2396 | } else { |
| 2397 | len = buf->buffer->use; |
| 2398 | c_retval = buf->buffer->content; |
| 2399 | buf->buffer->content = NULL; |
| 2400 | } |
| 2401 | (void) xmlOutputBufferClose(buf); |
| 2402 | py_retval = libxml_charPtrWrap((char *) c_retval); |
| 2403 | } else if (doc->type == XML_HTML_DOCUMENT_NODE) { |
| 2404 | xmlOutputBufferPtr buf; |
| 2405 | xmlCharEncodingHandlerPtr handler = NULL; |
Daniel Veillard | 1e77438 | 2002-03-06 17:35:40 +0000 | [diff] [blame] | 2406 | |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2407 | if (encoding != NULL) |
| 2408 | htmlSetMetaEncoding(doc, (const xmlChar *) encoding); |
| 2409 | encoding = (const char *) htmlGetMetaEncoding(doc); |
| 2410 | if (encoding != NULL) { |
| 2411 | handler = xmlFindCharEncodingHandler(encoding); |
| 2412 | if (handler == NULL) { |
| 2413 | Py_INCREF(Py_None); |
| 2414 | return (Py_None); |
| 2415 | } |
| 2416 | } |
Daniel Veillard | 1e77438 | 2002-03-06 17:35:40 +0000 | [diff] [blame] | 2417 | |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2418 | /* |
| 2419 | * Fallback to HTML or ASCII when the encoding is unspecified |
| 2420 | */ |
| 2421 | if (handler == NULL) |
| 2422 | handler = xmlFindCharEncodingHandler("HTML"); |
| 2423 | if (handler == NULL) |
| 2424 | handler = xmlFindCharEncodingHandler("ascii"); |
Daniel Veillard | 1e77438 | 2002-03-06 17:35:40 +0000 | [diff] [blame] | 2425 | |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2426 | buf = xmlAllocOutputBuffer(handler); |
| 2427 | if (buf == NULL) { |
| 2428 | Py_INCREF(Py_None); |
| 2429 | return (Py_None); |
| 2430 | } |
| 2431 | htmlNodeDumpFormatOutput(buf, doc, node, encoding, format); |
| 2432 | xmlOutputBufferFlush(buf); |
| 2433 | if (buf->conv != NULL) { |
| 2434 | len = buf->conv->use; |
| 2435 | c_retval = buf->conv->content; |
| 2436 | buf->conv->content = NULL; |
| 2437 | } else { |
| 2438 | len = buf->buffer->use; |
| 2439 | c_retval = buf->buffer->content; |
| 2440 | buf->buffer->content = NULL; |
| 2441 | } |
| 2442 | (void) xmlOutputBufferClose(buf); |
| 2443 | py_retval = libxml_charPtrWrap((char *) c_retval); |
| 2444 | } else { |
| 2445 | Py_INCREF(Py_None); |
| 2446 | return (Py_None); |
| 2447 | } |
Daniel Veillard | 1e77438 | 2002-03-06 17:35:40 +0000 | [diff] [blame] | 2448 | } |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2449 | return (py_retval); |
Daniel Veillard | 1e77438 | 2002-03-06 17:35:40 +0000 | [diff] [blame] | 2450 | } |
| 2451 | |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2452 | static PyObject * |
| 2453 | libxml_saveNodeTo(ATTRIBUTE_UNUSED PyObject * self, PyObject * args) |
| 2454 | { |
Daniel Veillard | 1e77438 | 2002-03-06 17:35:40 +0000 | [diff] [blame] | 2455 | PyObject *py_file = NULL; |
| 2456 | FILE *output; |
| 2457 | PyObject *pyobj_node; |
| 2458 | xmlNodePtr node; |
| 2459 | xmlDocPtr doc; |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2460 | const char *encoding; |
Daniel Veillard | 1e77438 | 2002-03-06 17:35:40 +0000 | [diff] [blame] | 2461 | int format; |
| 2462 | int len; |
| 2463 | xmlOutputBufferPtr buf; |
| 2464 | xmlCharEncodingHandlerPtr handler = NULL; |
| 2465 | |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2466 | if (!PyArg_ParseTuple(args, (char *) "OOzi:serializeNode", &pyobj_node, |
| 2467 | &py_file, &encoding, &format)) |
| 2468 | return (NULL); |
Daniel Veillard | 1e77438 | 2002-03-06 17:35:40 +0000 | [diff] [blame] | 2469 | node = (xmlNodePtr) PyxmlNode_Get(pyobj_node); |
| 2470 | |
| 2471 | if (node == NULL) { |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2472 | return (PyInt_FromLong((long) -1)); |
Daniel Veillard | 1e77438 | 2002-03-06 17:35:40 +0000 | [diff] [blame] | 2473 | } |
| 2474 | if ((py_file == NULL) || (!(PyFile_Check(py_file)))) { |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2475 | return (PyInt_FromLong((long) -1)); |
Daniel Veillard | 1e77438 | 2002-03-06 17:35:40 +0000 | [diff] [blame] | 2476 | } |
| 2477 | output = PyFile_AsFile(py_file); |
| 2478 | if (output == NULL) { |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2479 | return (PyInt_FromLong((long) -1)); |
Daniel Veillard | 1e77438 | 2002-03-06 17:35:40 +0000 | [diff] [blame] | 2480 | } |
| 2481 | |
| 2482 | if (node->type == XML_DOCUMENT_NODE) { |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2483 | doc = (xmlDocPtr) node; |
Daniel Veillard | 1e77438 | 2002-03-06 17:35:40 +0000 | [diff] [blame] | 2484 | } else if (node->type == XML_HTML_DOCUMENT_NODE) { |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2485 | doc = (xmlDocPtr) node; |
Daniel Veillard | 1e77438 | 2002-03-06 17:35:40 +0000 | [diff] [blame] | 2486 | } else { |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2487 | doc = node->doc; |
Daniel Veillard | 1e77438 | 2002-03-06 17:35:40 +0000 | [diff] [blame] | 2488 | } |
| 2489 | if (doc->type == XML_HTML_DOCUMENT_NODE) { |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2490 | if (encoding == NULL) |
| 2491 | encoding = (const char *) htmlGetMetaEncoding(doc); |
Daniel Veillard | 1e77438 | 2002-03-06 17:35:40 +0000 | [diff] [blame] | 2492 | } |
| 2493 | if (encoding != NULL) { |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2494 | handler = xmlFindCharEncodingHandler(encoding); |
| 2495 | if (handler == NULL) { |
| 2496 | return (PyInt_FromLong((long) -1)); |
| 2497 | } |
Daniel Veillard | 1e77438 | 2002-03-06 17:35:40 +0000 | [diff] [blame] | 2498 | } |
| 2499 | if (doc->type == XML_HTML_DOCUMENT_NODE) { |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2500 | if (handler == NULL) |
| 2501 | handler = xmlFindCharEncodingHandler("HTML"); |
| 2502 | if (handler == NULL) |
| 2503 | handler = xmlFindCharEncodingHandler("ascii"); |
Daniel Veillard | 1e77438 | 2002-03-06 17:35:40 +0000 | [diff] [blame] | 2504 | } |
| 2505 | |
| 2506 | buf = xmlOutputBufferCreateFile(output, handler); |
| 2507 | if (node->type == XML_DOCUMENT_NODE) { |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2508 | len = xmlSaveFormatFileTo(buf, doc, encoding, format); |
Daniel Veillard | 1e77438 | 2002-03-06 17:35:40 +0000 | [diff] [blame] | 2509 | } else if (node->type == XML_HTML_DOCUMENT_NODE) { |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2510 | htmlDocContentDumpFormatOutput(buf, doc, encoding, format); |
| 2511 | len = xmlOutputBufferClose(buf); |
Daniel Veillard | 1e77438 | 2002-03-06 17:35:40 +0000 | [diff] [blame] | 2512 | } else if (doc->type == XML_HTML_DOCUMENT_NODE) { |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2513 | htmlNodeDumpFormatOutput(buf, doc, node, encoding, format); |
| 2514 | len = xmlOutputBufferClose(buf); |
Daniel Veillard | 1e77438 | 2002-03-06 17:35:40 +0000 | [diff] [blame] | 2515 | } else { |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2516 | xmlNodeDumpOutput(buf, doc, node, 0, format, encoding); |
| 2517 | len = xmlOutputBufferClose(buf); |
Daniel Veillard | 1e77438 | 2002-03-06 17:35:40 +0000 | [diff] [blame] | 2518 | } |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2519 | return (PyInt_FromLong((long) len)); |
Daniel Veillard | 1e77438 | 2002-03-06 17:35:40 +0000 | [diff] [blame] | 2520 | } |
| 2521 | |
| 2522 | /************************************************************************ |
| 2523 | * * |
Daniel Veillard | a94ec6f | 2002-03-01 13:00:53 +0000 | [diff] [blame] | 2524 | * Extra stuff * |
| 2525 | * * |
| 2526 | ************************************************************************/ |
| 2527 | PyObject * |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2528 | libxml_xmlNewNode(ATTRIBUTE_UNUSED PyObject * self, PyObject * args) |
| 2529 | { |
Daniel Veillard | a94ec6f | 2002-03-01 13:00:53 +0000 | [diff] [blame] | 2530 | PyObject *py_retval; |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2531 | xmlChar *name; |
Daniel Veillard | a94ec6f | 2002-03-01 13:00:53 +0000 | [diff] [blame] | 2532 | xmlNodePtr node; |
| 2533 | |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2534 | if (!PyArg_ParseTuple(args, (char *) "s:xmlNewNode", &name)) |
| 2535 | return (NULL); |
Daniel Veillard | a94ec6f | 2002-03-01 13:00:53 +0000 | [diff] [blame] | 2536 | node = (xmlNodePtr) xmlNewNode(NULL, name); |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 2537 | #ifdef DEBUG |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2538 | printf("NewNode: %s : %p\n", name, (void *) node); |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 2539 | #endif |
Daniel Veillard | a94ec6f | 2002-03-01 13:00:53 +0000 | [diff] [blame] | 2540 | |
| 2541 | if (node == NULL) { |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2542 | Py_INCREF(Py_None); |
| 2543 | return (Py_None); |
Daniel Veillard | a94ec6f | 2002-03-01 13:00:53 +0000 | [diff] [blame] | 2544 | } |
| 2545 | py_retval = libxml_xmlNodePtrWrap(node); |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2546 | return (py_retval); |
Daniel Veillard | a94ec6f | 2002-03-01 13:00:53 +0000 | [diff] [blame] | 2547 | } |
| 2548 | |
Daniel Veillard | 5439624 | 2003-04-23 07:36:50 +0000 | [diff] [blame] | 2549 | |
| 2550 | /************************************************************************ |
| 2551 | * * |
| 2552 | * Local Catalog stuff * |
| 2553 | * * |
| 2554 | ************************************************************************/ |
| 2555 | static PyObject * |
| 2556 | libxml_addLocalCatalog(ATTRIBUTE_UNUSED PyObject * self, PyObject * args) |
| 2557 | { |
| 2558 | xmlChar *URL; |
| 2559 | xmlParserCtxtPtr ctxt; |
| 2560 | PyObject *pyobj_ctxt; |
| 2561 | |
| 2562 | if (!PyArg_ParseTuple(args, (char *)"Os:addLocalCatalog", &pyobj_ctxt, &URL)) |
| 2563 | return(NULL); |
| 2564 | |
| 2565 | ctxt = (xmlParserCtxtPtr) PyparserCtxt_Get(pyobj_ctxt); |
| 2566 | |
| 2567 | if (URL != NULL) { |
| 2568 | ctxt->catalogs = xmlCatalogAddLocal(ctxt->catalogs, URL); |
| 2569 | } |
| 2570 | |
| 2571 | #ifdef DEBUG |
| 2572 | printf("LocalCatalog: %s\n", URL); |
| 2573 | #endif |
| 2574 | |
| 2575 | Py_INCREF(Py_None); |
| 2576 | return (Py_None); |
| 2577 | } |
| 2578 | |
Daniel Veillard | a94ec6f | 2002-03-01 13:00:53 +0000 | [diff] [blame] | 2579 | /************************************************************************ |
| 2580 | * * |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 2581 | * The registration stuff * |
| 2582 | * * |
| 2583 | ************************************************************************/ |
| 2584 | static PyMethodDef libxmlMethods[] = { |
Daniel Veillard | 96fe095 | 2002-01-30 20:52:23 +0000 | [diff] [blame] | 2585 | #include "libxml2-export.c" |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2586 | {(char *) "name", libxml_name, METH_VARARGS, NULL}, |
| 2587 | {(char *) "children", libxml_children, METH_VARARGS, NULL}, |
| 2588 | {(char *) "properties", libxml_properties, METH_VARARGS, NULL}, |
| 2589 | {(char *) "last", libxml_last, METH_VARARGS, NULL}, |
| 2590 | {(char *) "prev", libxml_prev, METH_VARARGS, NULL}, |
| 2591 | {(char *) "next", libxml_next, METH_VARARGS, NULL}, |
| 2592 | {(char *) "parent", libxml_parent, METH_VARARGS, NULL}, |
| 2593 | {(char *) "type", libxml_type, METH_VARARGS, NULL}, |
| 2594 | {(char *) "doc", libxml_doc, METH_VARARGS, NULL}, |
| 2595 | {(char *) "xmlNewNode", libxml_xmlNewNode, METH_VARARGS, NULL}, |
| 2596 | {(char *) "serializeNode", libxml_serializeNode, METH_VARARGS, NULL}, |
| 2597 | {(char *) "saveNodeTo", libxml_saveNodeTo, METH_VARARGS, NULL}, |
Daniel Veillard | c6d4a93 | 2002-09-12 15:00:57 +0000 | [diff] [blame] | 2598 | {(char *) "outputBufferCreate", libxml_xmlCreateOutputBuffer, METH_VARARGS, NULL}, |
| 2599 | {(char *) "inputBufferCreate", libxml_xmlCreateInputBuffer, METH_VARARGS, NULL}, |
| 2600 | {(char *) "setEntityLoader", libxml_xmlSetEntityLoader, METH_VARARGS, NULL}, |
Daniel Veillard | 3e20a29 | 2003-01-10 13:14:40 +0000 | [diff] [blame] | 2601 | {(char *)"xmlRegisterErrorHandler", libxml_xmlRegisterErrorHandler, METH_VARARGS, NULL }, |
Daniel Veillard | 417be3a | 2003-01-20 21:26:34 +0000 | [diff] [blame] | 2602 | {(char *)"xmlParserCtxtSetErrorHandler", libxml_xmlParserCtxtSetErrorHandler, METH_VARARGS, NULL }, |
| 2603 | {(char *)"xmlParserCtxtGetErrorHandler", libxml_xmlParserCtxtGetErrorHandler, METH_VARARGS, NULL }, |
Daniel Veillard | e6227e0 | 2003-01-14 11:42:39 +0000 | [diff] [blame] | 2604 | {(char *)"xmlFreeParserCtxt", libxml_xmlFreeParserCtxt, METH_VARARGS, NULL }, |
Daniel Veillard | 26f7026 | 2003-01-16 22:45:08 +0000 | [diff] [blame] | 2605 | {(char *)"xmlTextReaderSetErrorHandler", libxml_xmlTextReaderSetErrorHandler, METH_VARARGS, NULL }, |
| 2606 | {(char *)"xmlTextReaderGetErrorHandler", libxml_xmlTextReaderGetErrorHandler, METH_VARARGS, NULL }, |
| 2607 | {(char *)"xmlFreeTextReader", libxml_xmlFreeTextReader, METH_VARARGS, NULL }, |
Daniel Veillard | 5439624 | 2003-04-23 07:36:50 +0000 | [diff] [blame] | 2608 | {(char *)"addLocalCatalog", libxml_addLocalCatalog, METH_VARARGS, NULL }, |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2609 | {NULL, NULL, 0, NULL} |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 2610 | }; |
| 2611 | |
Daniel Veillard | 0fea6f4 | 2002-02-22 22:51:13 +0000 | [diff] [blame] | 2612 | #ifdef MERGED_MODULES |
Daniel Veillard | 6361da0 | 2002-02-23 10:10:33 +0000 | [diff] [blame] | 2613 | extern void initlibxsltmod(void); |
Daniel Veillard | 0fea6f4 | 2002-02-22 22:51:13 +0000 | [diff] [blame] | 2614 | #endif |
| 2615 | |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2616 | void |
| 2617 | initlibxml2mod(void) |
| 2618 | { |
Daniel Veillard | af43f63 | 2002-03-08 15:05:20 +0000 | [diff] [blame] | 2619 | static int initialized = 0; |
Daniel Veillard | 3ce5257 | 2002-02-03 15:08:05 +0000 | [diff] [blame] | 2620 | PyObject *m; |
Daniel Veillard | af43f63 | 2002-03-08 15:05:20 +0000 | [diff] [blame] | 2621 | |
| 2622 | if (initialized != 0) |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2623 | return; |
Daniel Veillard | 781ac8b | 2003-05-15 22:11:36 +0000 | [diff] [blame^] | 2624 | /* XXX xmlInitParser does much more than this */ |
| 2625 | xmlInitGlobals(); |
Daniel Veillard | c6d4a93 | 2002-09-12 15:00:57 +0000 | [diff] [blame] | 2626 | xmlRegisterDefaultOutputCallbacks(); |
| 2627 | xmlRegisterDefaultInputCallbacks(); |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2628 | m = Py_InitModule((char *) "libxml2mod", libxmlMethods); |
Daniel Veillard | af43f63 | 2002-03-08 15:05:20 +0000 | [diff] [blame] | 2629 | initialized = 1; |
Daniel Veillard | 5d81903 | 2002-02-02 21:49:17 +0000 | [diff] [blame] | 2630 | libxml_xmlErrorInitialize(); |
Daniel Veillard | 6361da0 | 2002-02-23 10:10:33 +0000 | [diff] [blame] | 2631 | |
Daniel Veillard | 0fea6f4 | 2002-02-22 22:51:13 +0000 | [diff] [blame] | 2632 | #ifdef MERGED_MODULES |
| 2633 | initlibxsltmod(); |
| 2634 | #endif |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 2635 | } |