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