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