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 | d5e198a | 2004-03-09 09:03:28 +0000 | [diff] [blame] | 25 | #include <libxml/c14n.h> |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 26 | #include "libxml_wrap.h" |
Daniel Veillard | 96fe095 | 2002-01-30 20:52:23 +0000 | [diff] [blame] | 27 | #include "libxml2-py.h" |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 28 | |
Daniel Veillard | 0d132cf | 2002-12-23 14:43:32 +0000 | [diff] [blame] | 29 | #if (defined(_MSC_VER) || defined(__MINGW32__)) && !defined(vsnprintf) |
| 30 | #define vsnprintf(b,c,f,a) _vsnprintf(b,c,f,a) |
Daniel Veillard | e4fa293 | 2003-03-26 00:38:10 +0000 | [diff] [blame] | 31 | #elif defined(WITH_TRIO) |
| 32 | #include "trio.h" |
| 33 | #define vsnprintf trio_vsnprintf |
Daniel Veillard | 0d132cf | 2002-12-23 14:43:32 +0000 | [diff] [blame] | 34 | #endif |
| 35 | |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 36 | /* #define DEBUG */ |
Daniel Veillard | 797a565 | 2002-02-12 13:46:21 +0000 | [diff] [blame] | 37 | /* #define DEBUG_SAX */ |
Daniel Veillard | a7340c8 | 2002-02-01 17:56:45 +0000 | [diff] [blame] | 38 | /* #define DEBUG_XPATH */ |
Daniel Veillard | 5d81903 | 2002-02-02 21:49:17 +0000 | [diff] [blame] | 39 | /* #define DEBUG_ERROR */ |
Daniel Veillard | 4e1b26c | 2002-02-03 20:13:06 +0000 | [diff] [blame] | 40 | /* #define DEBUG_MEMORY */ |
Daniel Veillard | c6d4a93 | 2002-09-12 15:00:57 +0000 | [diff] [blame] | 41 | /* #define DEBUG_FILES */ |
| 42 | /* #define DEBUG_LOADER */ |
Daniel Veillard | 4e1b26c | 2002-02-03 20:13:06 +0000 | [diff] [blame] | 43 | |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 44 | void initlibxml2mod(void); |
| 45 | |
Daniel Veillard | c6d4a93 | 2002-09-12 15:00:57 +0000 | [diff] [blame] | 46 | /** |
| 47 | * TODO: |
| 48 | * |
| 49 | * macro to flag unimplemented blocks |
| 50 | */ |
| 51 | #define TODO \ |
| 52 | xmlGenericError(xmlGenericErrorContext, \ |
| 53 | "Unimplemented block at %s:%d\n", \ |
| 54 | __FILE__, __LINE__); |
William M. Brack | 8e2cc6f | 2004-07-03 23:28:52 +0000 | [diff] [blame] | 55 | /* |
| 56 | * the following vars are used for XPath extensions, but |
| 57 | * are also referenced within the parser cleanup routine. |
| 58 | */ |
| 59 | static int libxml_xpathCallbacksInitialized = 0; |
| 60 | |
| 61 | typedef struct libxml_xpathCallback { |
| 62 | xmlXPathContextPtr ctx; |
| 63 | xmlChar *name; |
| 64 | xmlChar *ns_uri; |
| 65 | PyObject *function; |
| 66 | } libxml_xpathCallback, *libxml_xpathCallbackPtr; |
| 67 | typedef libxml_xpathCallback libxml_xpathCallbackArray[]; |
| 68 | static int libxml_xpathCallbacksAllocd = 10; |
| 69 | static libxml_xpathCallbackArray *libxml_xpathCallbacks = NULL; |
| 70 | static int libxml_xpathCallbacksNb = 0; |
Daniel Veillard | c6d4a93 | 2002-09-12 15:00:57 +0000 | [diff] [blame] | 71 | |
Daniel Veillard | 4e1b26c | 2002-02-03 20:13:06 +0000 | [diff] [blame] | 72 | /************************************************************************ |
| 73 | * * |
| 74 | * Memory debug interface * |
| 75 | * * |
| 76 | ************************************************************************/ |
| 77 | |
Daniel Veillard | c266464 | 2003-07-29 20:44:53 +0000 | [diff] [blame] | 78 | #if 0 |
Daniel Veillard | 4e1b26c | 2002-02-03 20:13:06 +0000 | [diff] [blame] | 79 | extern void xmlMemFree(void *ptr); |
| 80 | extern void *xmlMemMalloc(size_t size); |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 81 | extern void *xmlMemRealloc(void *ptr, size_t size); |
Daniel Veillard | 4e1b26c | 2002-02-03 20:13:06 +0000 | [diff] [blame] | 82 | extern char *xmlMemoryStrdup(const char *str); |
Daniel Veillard | c266464 | 2003-07-29 20:44:53 +0000 | [diff] [blame] | 83 | #endif |
Daniel Veillard | 4e1b26c | 2002-02-03 20:13:06 +0000 | [diff] [blame] | 84 | |
| 85 | static int libxmlMemoryDebugActivated = 0; |
| 86 | static long libxmlMemoryAllocatedBase = 0; |
| 87 | |
| 88 | static int libxmlMemoryDebug = 0; |
| 89 | static xmlFreeFunc freeFunc = NULL; |
| 90 | static xmlMallocFunc mallocFunc = NULL; |
| 91 | static xmlReallocFunc reallocFunc = NULL; |
| 92 | static xmlStrdupFunc strdupFunc = NULL; |
| 93 | |
Daniel Veillard | f93a866 | 2004-07-01 12:56:30 +0000 | [diff] [blame] | 94 | static void |
| 95 | libxml_xmlErrorInitialize(void); /* forward declare */ |
| 96 | |
Daniel Veillard | 4e1b26c | 2002-02-03 20:13:06 +0000 | [diff] [blame] | 97 | PyObject * |
William M. Brack | c68d78d | 2004-07-16 10:39:30 +0000 | [diff] [blame] | 98 | libxml_xmlMemoryUsed(PyObject * self ATTRIBUTE_UNUSED, |
| 99 | PyObject * args ATTRIBUTE_UNUSED) |
Daniel Veillard | 529233c | 2004-07-02 12:23:44 +0000 | [diff] [blame] | 100 | { |
| 101 | long ret; |
| 102 | PyObject *py_retval; |
| 103 | |
| 104 | ret = xmlMemUsed(); |
| 105 | |
| 106 | py_retval = libxml_longWrap(ret); |
| 107 | return (py_retval); |
| 108 | } |
| 109 | |
| 110 | PyObject * |
William M. Brack | c68d78d | 2004-07-16 10:39:30 +0000 | [diff] [blame] | 111 | libxml_xmlDebugMemory(PyObject * self ATTRIBUTE_UNUSED, PyObject * args) |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 112 | { |
Daniel Veillard | 4e1b26c | 2002-02-03 20:13:06 +0000 | [diff] [blame] | 113 | int activate; |
| 114 | PyObject *py_retval; |
| 115 | long ret; |
| 116 | |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 117 | if (!PyArg_ParseTuple(args, (char *) "i:xmlDebugMemory", &activate)) |
| 118 | return (NULL); |
Daniel Veillard | 4e1b26c | 2002-02-03 20:13:06 +0000 | [diff] [blame] | 119 | |
| 120 | #ifdef DEBUG_MEMORY |
| 121 | printf("libxml_xmlDebugMemory(%d) called\n", activate); |
| 122 | #endif |
| 123 | |
| 124 | if (activate != 0) { |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 125 | if (libxmlMemoryDebug == 0) { |
| 126 | /* |
| 127 | * First initialize the library and grab the old memory handlers |
| 128 | * and switch the library to memory debugging |
| 129 | */ |
| 130 | xmlMemGet((xmlFreeFunc *) & freeFunc, |
| 131 | (xmlMallocFunc *) & mallocFunc, |
| 132 | (xmlReallocFunc *) & reallocFunc, |
| 133 | (xmlStrdupFunc *) & strdupFunc); |
| 134 | if ((freeFunc == xmlMemFree) && (mallocFunc == xmlMemMalloc) && |
| 135 | (reallocFunc == xmlMemRealloc) && |
| 136 | (strdupFunc == xmlMemoryStrdup)) { |
| 137 | libxmlMemoryAllocatedBase = xmlMemUsed(); |
| 138 | } else { |
Daniel Veillard | f93a866 | 2004-07-01 12:56:30 +0000 | [diff] [blame] | 139 | /* |
| 140 | * cleanup first, because some memory has been |
| 141 | * allocated with the non-debug malloc in xmlInitParser |
| 142 | * when the python module was imported |
| 143 | */ |
| 144 | xmlCleanupParser(); |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 145 | ret = (long) xmlMemSetup(xmlMemFree, xmlMemMalloc, |
| 146 | xmlMemRealloc, xmlMemoryStrdup); |
| 147 | if (ret < 0) |
| 148 | goto error; |
| 149 | libxmlMemoryAllocatedBase = xmlMemUsed(); |
Daniel Veillard | f93a866 | 2004-07-01 12:56:30 +0000 | [diff] [blame] | 150 | /* reinitialize */ |
| 151 | xmlInitParser(); |
| 152 | libxml_xmlErrorInitialize(); |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 153 | } |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 154 | ret = 0; |
| 155 | } else if (libxmlMemoryDebugActivated == 0) { |
| 156 | libxmlMemoryAllocatedBase = xmlMemUsed(); |
| 157 | ret = 0; |
| 158 | } else { |
| 159 | ret = xmlMemUsed() - libxmlMemoryAllocatedBase; |
| 160 | } |
| 161 | libxmlMemoryDebug = 1; |
| 162 | libxmlMemoryDebugActivated = 1; |
Daniel Veillard | 4e1b26c | 2002-02-03 20:13:06 +0000 | [diff] [blame] | 163 | } else { |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 164 | if (libxmlMemoryDebugActivated == 1) |
| 165 | ret = xmlMemUsed() - libxmlMemoryAllocatedBase; |
| 166 | else |
| 167 | ret = 0; |
| 168 | libxmlMemoryDebugActivated = 0; |
Daniel Veillard | 4e1b26c | 2002-02-03 20:13:06 +0000 | [diff] [blame] | 169 | } |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 170 | error: |
Daniel Veillard | 4e1b26c | 2002-02-03 20:13:06 +0000 | [diff] [blame] | 171 | py_retval = libxml_longWrap(ret); |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 172 | return (py_retval); |
Daniel Veillard | 4e1b26c | 2002-02-03 20:13:06 +0000 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | PyObject * |
Daniel Veillard | f93a866 | 2004-07-01 12:56:30 +0000 | [diff] [blame] | 176 | libxml_xmlPythonCleanupParser(PyObject *self ATTRIBUTE_UNUSED, |
| 177 | PyObject *args ATTRIBUTE_UNUSED) { |
| 178 | |
William M. Brack | 8e2cc6f | 2004-07-03 23:28:52 +0000 | [diff] [blame] | 179 | int ix; |
| 180 | long freed = -1; |
Daniel Veillard | f93a866 | 2004-07-01 12:56:30 +0000 | [diff] [blame] | 181 | |
| 182 | if (libxmlMemoryDebug) { |
| 183 | freed = xmlMemUsed(); |
| 184 | } |
| 185 | |
| 186 | xmlCleanupParser(); |
William M. Brack | 8e2cc6f | 2004-07-03 23:28:52 +0000 | [diff] [blame] | 187 | /* |
| 188 | * Need to confirm whether we really want to do this (required for |
| 189 | * memcheck) in all cases... |
| 190 | */ |
| 191 | |
| 192 | if (libxml_xpathCallbacks != NULL) { /* if ext funcs declared */ |
| 193 | for (ix=0; ix<libxml_xpathCallbacksNb; ix++) { |
| 194 | if ((*libxml_xpathCallbacks)[ix].name != NULL) |
| 195 | xmlFree((*libxml_xpathCallbacks)[ix].name); |
| 196 | if ((*libxml_xpathCallbacks)[ix].ns_uri != NULL) |
| 197 | xmlFree((*libxml_xpathCallbacks)[ix].ns_uri); |
| 198 | } |
| 199 | libxml_xpathCallbacksNb = 0; |
| 200 | xmlFree(libxml_xpathCallbacks); |
| 201 | libxml_xpathCallbacks = NULL; |
| 202 | } |
Daniel Veillard | f93a866 | 2004-07-01 12:56:30 +0000 | [diff] [blame] | 203 | |
| 204 | if (libxmlMemoryDebug) { |
| 205 | freed -= xmlMemUsed(); |
| 206 | libxmlMemoryAllocatedBase -= freed; |
| 207 | if (libxmlMemoryAllocatedBase < 0) |
| 208 | libxmlMemoryAllocatedBase = 0; |
| 209 | } |
| 210 | |
| 211 | Py_INCREF(Py_None); |
| 212 | return(Py_None); |
| 213 | } |
| 214 | |
| 215 | PyObject * |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 216 | libxml_xmlDumpMemory(ATTRIBUTE_UNUSED PyObject * self, |
| 217 | ATTRIBUTE_UNUSED PyObject * args) |
| 218 | { |
Daniel Veillard | 4e1b26c | 2002-02-03 20:13:06 +0000 | [diff] [blame] | 219 | |
| 220 | if (libxmlMemoryDebug != 0) |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 221 | xmlMemoryDump(); |
Daniel Veillard | 4e1b26c | 2002-02-03 20:13:06 +0000 | [diff] [blame] | 222 | Py_INCREF(Py_None); |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 223 | return (Py_None); |
Daniel Veillard | 4e1b26c | 2002-02-03 20:13:06 +0000 | [diff] [blame] | 224 | } |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 225 | |
| 226 | /************************************************************************ |
| 227 | * * |
Daniel Veillard | c6d4a93 | 2002-09-12 15:00:57 +0000 | [diff] [blame] | 228 | * Handling Python FILE I/O at the C level * |
| 229 | * The raw I/O attack diectly the File objects, while the * |
| 230 | * other routines address the ioWrapper instance instead * |
| 231 | * * |
| 232 | ************************************************************************/ |
| 233 | |
| 234 | /** |
| 235 | * xmlPythonFileCloseUnref: |
| 236 | * @context: the I/O context |
| 237 | * |
| 238 | * Close an I/O channel |
| 239 | */ |
| 240 | static int |
| 241 | xmlPythonFileCloseRaw (void * context) { |
| 242 | PyObject *file, *ret; |
| 243 | |
| 244 | #ifdef DEBUG_FILES |
| 245 | printf("xmlPythonFileCloseUnref\n"); |
| 246 | #endif |
| 247 | file = (PyObject *) context; |
| 248 | if (file == NULL) return(-1); |
Daniel Veillard | 118aed7 | 2002-09-24 14:13:13 +0000 | [diff] [blame] | 249 | ret = PyEval_CallMethod(file, (char *) "close", (char *) "()"); |
Daniel Veillard | c6d4a93 | 2002-09-12 15:00:57 +0000 | [diff] [blame] | 250 | if (ret != NULL) { |
| 251 | Py_DECREF(ret); |
| 252 | } |
| 253 | Py_DECREF(file); |
| 254 | return(0); |
| 255 | } |
| 256 | |
| 257 | /** |
| 258 | * xmlPythonFileReadRaw: |
| 259 | * @context: the I/O context |
| 260 | * @buffer: where to drop data |
| 261 | * @len: number of bytes to write |
| 262 | * |
| 263 | * Read @len bytes to @buffer from the Python file in the I/O channel |
| 264 | * |
| 265 | * Returns the number of bytes read |
| 266 | */ |
| 267 | static int |
| 268 | xmlPythonFileReadRaw (void * context, char * buffer, int len) { |
| 269 | PyObject *file; |
| 270 | PyObject *ret; |
| 271 | int lenread = -1; |
| 272 | char *data; |
| 273 | |
| 274 | #ifdef DEBUG_FILES |
| 275 | printf("xmlPythonFileReadRaw: %d\n", len); |
| 276 | #endif |
| 277 | file = (PyObject *) context; |
| 278 | if (file == NULL) return(-1); |
Daniel Veillard | 118aed7 | 2002-09-24 14:13:13 +0000 | [diff] [blame] | 279 | ret = PyEval_CallMethod(file, (char *) "read", (char *) "(i)", len); |
Daniel Veillard | c6d4a93 | 2002-09-12 15:00:57 +0000 | [diff] [blame] | 280 | if (ret == NULL) { |
| 281 | printf("xmlPythonFileReadRaw: result is NULL\n"); |
| 282 | return(-1); |
| 283 | } else if (PyString_Check(ret)) { |
| 284 | lenread = PyString_Size(ret); |
| 285 | data = PyString_AsString(ret); |
| 286 | if (lenread > len) |
| 287 | memcpy(buffer, data, len); |
| 288 | else |
| 289 | memcpy(buffer, data, lenread); |
| 290 | Py_DECREF(ret); |
| 291 | } else { |
| 292 | printf("xmlPythonFileReadRaw: result is not a String\n"); |
| 293 | Py_DECREF(ret); |
| 294 | } |
| 295 | return(lenread); |
| 296 | } |
| 297 | |
| 298 | /** |
| 299 | * xmlPythonFileRead: |
| 300 | * @context: the I/O context |
| 301 | * @buffer: where to drop data |
| 302 | * @len: number of bytes to write |
| 303 | * |
| 304 | * Read @len bytes to @buffer from the I/O channel. |
| 305 | * |
| 306 | * Returns the number of bytes read |
| 307 | */ |
| 308 | static int |
| 309 | xmlPythonFileRead (void * context, char * buffer, int len) { |
| 310 | PyObject *file; |
| 311 | PyObject *ret; |
| 312 | int lenread = -1; |
| 313 | char *data; |
| 314 | |
| 315 | #ifdef DEBUG_FILES |
| 316 | printf("xmlPythonFileRead: %d\n", len); |
| 317 | #endif |
| 318 | file = (PyObject *) context; |
| 319 | if (file == NULL) return(-1); |
Daniel Veillard | 118aed7 | 2002-09-24 14:13:13 +0000 | [diff] [blame] | 320 | ret = PyEval_CallMethod(file, (char *) "io_read", (char *) "(i)", len); |
Daniel Veillard | c6d4a93 | 2002-09-12 15:00:57 +0000 | [diff] [blame] | 321 | if (ret == NULL) { |
| 322 | printf("xmlPythonFileRead: result is NULL\n"); |
| 323 | return(-1); |
| 324 | } else if (PyString_Check(ret)) { |
| 325 | lenread = PyString_Size(ret); |
| 326 | data = PyString_AsString(ret); |
| 327 | if (lenread > len) |
| 328 | memcpy(buffer, data, len); |
| 329 | else |
| 330 | memcpy(buffer, data, lenread); |
| 331 | Py_DECREF(ret); |
| 332 | } else { |
| 333 | printf("xmlPythonFileRead: result is not a String\n"); |
| 334 | Py_DECREF(ret); |
| 335 | } |
| 336 | return(lenread); |
| 337 | } |
| 338 | |
| 339 | /** |
| 340 | * xmlFileWrite: |
| 341 | * @context: the I/O context |
| 342 | * @buffer: where to drop data |
| 343 | * @len: number of bytes to write |
| 344 | * |
| 345 | * Write @len bytes from @buffer to the I/O channel. |
| 346 | * |
| 347 | * Returns the number of bytes written |
| 348 | */ |
| 349 | static int |
| 350 | xmlPythonFileWrite (void * context, const char * buffer, int len) { |
| 351 | PyObject *file; |
| 352 | PyObject *string; |
Daniel Veillard | 6cbd6c0 | 2003-12-04 12:31:49 +0000 | [diff] [blame] | 353 | PyObject *ret = NULL; |
Daniel Veillard | c6d4a93 | 2002-09-12 15:00:57 +0000 | [diff] [blame] | 354 | int written = -1; |
| 355 | |
| 356 | #ifdef DEBUG_FILES |
| 357 | printf("xmlPythonFileWrite: %d\n", len); |
| 358 | #endif |
| 359 | file = (PyObject *) context; |
| 360 | if (file == NULL) return(-1); |
| 361 | string = PyString_FromStringAndSize(buffer, len); |
| 362 | if (string == NULL) return(-1); |
Daniel Veillard | 6cbd6c0 | 2003-12-04 12:31:49 +0000 | [diff] [blame] | 363 | if (PyObject_HasAttrString(file, (char *) "io_write")) { |
| 364 | ret = PyEval_CallMethod(file, (char *) "io_write", (char *) "(O)", |
| 365 | string); |
| 366 | } else if (PyObject_HasAttrString(file, (char *) "write")) { |
| 367 | ret = PyEval_CallMethod(file, (char *) "write", (char *) "(O)", |
| 368 | string); |
| 369 | } |
Daniel Veillard | c6d4a93 | 2002-09-12 15:00:57 +0000 | [diff] [blame] | 370 | Py_DECREF(string); |
| 371 | if (ret == NULL) { |
| 372 | printf("xmlPythonFileWrite: result is NULL\n"); |
| 373 | return(-1); |
| 374 | } else if (PyInt_Check(ret)) { |
| 375 | written = (int) PyInt_AsLong(ret); |
| 376 | Py_DECREF(ret); |
| 377 | } else if (ret == Py_None) { |
| 378 | written = len; |
| 379 | Py_DECREF(ret); |
| 380 | } else { |
| 381 | printf("xmlPythonFileWrite: result is not an Int nor None\n"); |
| 382 | Py_DECREF(ret); |
| 383 | } |
| 384 | return(written); |
| 385 | } |
| 386 | |
| 387 | /** |
| 388 | * xmlPythonFileClose: |
| 389 | * @context: the I/O context |
| 390 | * |
| 391 | * Close an I/O channel |
| 392 | */ |
| 393 | static int |
| 394 | xmlPythonFileClose (void * context) { |
Daniel Veillard | 6cbd6c0 | 2003-12-04 12:31:49 +0000 | [diff] [blame] | 395 | PyObject *file, *ret = NULL; |
Daniel Veillard | c6d4a93 | 2002-09-12 15:00:57 +0000 | [diff] [blame] | 396 | |
| 397 | #ifdef DEBUG_FILES |
| 398 | printf("xmlPythonFileClose\n"); |
| 399 | #endif |
| 400 | file = (PyObject *) context; |
| 401 | if (file == NULL) return(-1); |
Daniel Veillard | 6cbd6c0 | 2003-12-04 12:31:49 +0000 | [diff] [blame] | 402 | if (PyObject_HasAttrString(file, (char *) "io_close")) { |
| 403 | ret = PyEval_CallMethod(file, (char *) "io_close", (char *) "()"); |
| 404 | } else if (PyObject_HasAttrString(file, (char *) "flush")) { |
| 405 | ret = PyEval_CallMethod(file, (char *) "flush", (char *) "()"); |
| 406 | } |
Daniel Veillard | c6d4a93 | 2002-09-12 15:00:57 +0000 | [diff] [blame] | 407 | if (ret != NULL) { |
| 408 | Py_DECREF(ret); |
| 409 | } |
| 410 | return(0); |
| 411 | } |
| 412 | |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 413 | #ifdef LIBXML_OUTPUT_ENABLED |
Daniel Veillard | c6d4a93 | 2002-09-12 15:00:57 +0000 | [diff] [blame] | 414 | /** |
| 415 | * xmlOutputBufferCreatePythonFile: |
| 416 | * @file: a PyFile_Type |
| 417 | * @encoder: the encoding converter or NULL |
| 418 | * |
| 419 | * Create a buffered output for the progressive saving to a PyFile_Type |
| 420 | * buffered C I/O |
| 421 | * |
| 422 | * Returns the new parser output or NULL |
| 423 | */ |
Daniel Veillard | 118aed7 | 2002-09-24 14:13:13 +0000 | [diff] [blame] | 424 | static xmlOutputBufferPtr |
Daniel Veillard | c6d4a93 | 2002-09-12 15:00:57 +0000 | [diff] [blame] | 425 | xmlOutputBufferCreatePythonFile(PyObject *file, |
| 426 | xmlCharEncodingHandlerPtr encoder) { |
| 427 | xmlOutputBufferPtr ret; |
| 428 | |
| 429 | if (file == NULL) return(NULL); |
| 430 | |
| 431 | ret = xmlAllocOutputBuffer(encoder); |
| 432 | if (ret != NULL) { |
| 433 | ret->context = file; |
| 434 | /* Py_INCREF(file); */ |
| 435 | ret->writecallback = xmlPythonFileWrite; |
| 436 | ret->closecallback = xmlPythonFileClose; |
| 437 | } |
| 438 | |
| 439 | return(ret); |
| 440 | } |
| 441 | |
| 442 | PyObject * |
| 443 | libxml_xmlCreateOutputBuffer(ATTRIBUTE_UNUSED PyObject *self, PyObject *args) { |
| 444 | PyObject *py_retval; |
| 445 | PyObject *file; |
| 446 | xmlChar *encoding; |
| 447 | xmlCharEncodingHandlerPtr handler = NULL; |
| 448 | xmlOutputBufferPtr buffer; |
| 449 | |
| 450 | |
| 451 | if (!PyArg_ParseTuple(args, (char *)"Oz:xmlOutputBufferCreate", |
| 452 | &file, &encoding)) |
| 453 | return(NULL); |
| 454 | if ((encoding != NULL) && (encoding[0] != 0)) { |
Daniel Veillard | 118aed7 | 2002-09-24 14:13:13 +0000 | [diff] [blame] | 455 | handler = xmlFindCharEncodingHandler((const char *) encoding); |
Daniel Veillard | c6d4a93 | 2002-09-12 15:00:57 +0000 | [diff] [blame] | 456 | } |
| 457 | buffer = xmlOutputBufferCreatePythonFile(file, handler); |
| 458 | if (buffer == NULL) |
| 459 | printf("libxml_xmlCreateOutputBuffer: buffer == NULL\n"); |
| 460 | py_retval = libxml_xmlOutputBufferPtrWrap(buffer); |
| 461 | return(py_retval); |
| 462 | } |
Daniel Veillard | 6cbd6c0 | 2003-12-04 12:31:49 +0000 | [diff] [blame] | 463 | |
| 464 | /** |
| 465 | * libxml_outputBufferGetPythonFile: |
| 466 | * @buffer: the I/O buffer |
| 467 | * |
| 468 | * read the Python I/O from the CObject |
| 469 | * |
| 470 | * Returns the new parser output or NULL |
| 471 | */ |
| 472 | static PyObject * |
| 473 | libxml_outputBufferGetPythonFile(ATTRIBUTE_UNUSED PyObject *self, |
| 474 | PyObject *args) { |
| 475 | PyObject *buffer; |
| 476 | PyObject *file; |
| 477 | xmlOutputBufferPtr obj; |
| 478 | |
| 479 | if (!PyArg_ParseTuple(args, (char *)"O:outputBufferGetPythonFile", |
| 480 | &buffer)) |
| 481 | return(NULL); |
| 482 | |
| 483 | obj = PyoutputBuffer_Get(buffer); |
| 484 | if (obj == NULL) { |
| 485 | fprintf(stderr, |
| 486 | "outputBufferGetPythonFile: obj == NULL\n"); |
| 487 | Py_INCREF(Py_None); |
| 488 | return(Py_None); |
| 489 | } |
| 490 | if (obj->closecallback != xmlPythonFileClose) { |
| 491 | fprintf(stderr, |
| 492 | "outputBufferGetPythonFile: not a python file wrapper\n"); |
| 493 | Py_INCREF(Py_None); |
| 494 | return(Py_None); |
| 495 | } |
| 496 | file = (PyObject *) obj->context; |
| 497 | if (file == NULL) { |
| 498 | Py_INCREF(Py_None); |
| 499 | return(Py_None); |
| 500 | } |
| 501 | Py_INCREF(file); |
| 502 | return(file); |
| 503 | } |
| 504 | |
Daniel Veillard | d5e198a | 2004-03-09 09:03:28 +0000 | [diff] [blame] | 505 | static PyObject * |
Daniel Veillard | 6cbd6c0 | 2003-12-04 12:31:49 +0000 | [diff] [blame] | 506 | libxml_xmlOutputBufferClose(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) { |
| 507 | PyObject *py_retval; |
| 508 | int c_retval; |
| 509 | xmlOutputBufferPtr out; |
| 510 | PyObject *pyobj_out; |
| 511 | |
| 512 | if (!PyArg_ParseTuple(args, (char *)"O:xmlOutputBufferClose", &pyobj_out)) |
| 513 | return(NULL); |
| 514 | out = (xmlOutputBufferPtr) PyoutputBuffer_Get(pyobj_out); |
Daniel Veillard | 263ec86 | 2004-10-04 10:26:54 +0000 | [diff] [blame] | 515 | /* Buffer may already have been destroyed elsewhere. This is harmless. */ |
| 516 | if (out == NULL) { |
| 517 | Py_INCREF(Py_None); |
| 518 | return(Py_None); |
| 519 | } |
Daniel Veillard | 6cbd6c0 | 2003-12-04 12:31:49 +0000 | [diff] [blame] | 520 | |
| 521 | c_retval = xmlOutputBufferClose(out); |
| 522 | py_retval = libxml_intWrap((int) c_retval); |
| 523 | return(py_retval); |
| 524 | } |
| 525 | |
Daniel Veillard | d5e198a | 2004-03-09 09:03:28 +0000 | [diff] [blame] | 526 | static PyObject * |
Daniel Veillard | 6cbd6c0 | 2003-12-04 12:31:49 +0000 | [diff] [blame] | 527 | libxml_xmlOutputBufferFlush(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) { |
| 528 | PyObject *py_retval; |
| 529 | int c_retval; |
| 530 | xmlOutputBufferPtr out; |
| 531 | PyObject *pyobj_out; |
| 532 | |
| 533 | if (!PyArg_ParseTuple(args, (char *)"O:xmlOutputBufferFlush", &pyobj_out)) |
| 534 | return(NULL); |
| 535 | out = (xmlOutputBufferPtr) PyoutputBuffer_Get(pyobj_out); |
| 536 | |
| 537 | c_retval = xmlOutputBufferFlush(out); |
| 538 | py_retval = libxml_intWrap((int) c_retval); |
| 539 | return(py_retval); |
| 540 | } |
Daniel Veillard | 263ec86 | 2004-10-04 10:26:54 +0000 | [diff] [blame] | 541 | |
| 542 | static PyObject * |
| 543 | libxml_xmlSaveFileTo(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) { |
| 544 | PyObject *py_retval; |
| 545 | int c_retval; |
| 546 | xmlOutputBufferPtr buf; |
| 547 | PyObject *pyobj_buf; |
| 548 | xmlDocPtr cur; |
| 549 | PyObject *pyobj_cur; |
| 550 | char * encoding; |
| 551 | |
| 552 | if (!PyArg_ParseTuple(args, (char *)"OOz:xmlSaveFileTo", &pyobj_buf, &pyobj_cur, &encoding)) |
| 553 | return(NULL); |
| 554 | buf = (xmlOutputBufferPtr) PyoutputBuffer_Get(pyobj_buf); |
| 555 | cur = (xmlDocPtr) PyxmlNode_Get(pyobj_cur); |
| 556 | |
| 557 | c_retval = xmlSaveFileTo(buf, cur, encoding); |
| 558 | /* xmlSaveTo() freed the memory pointed to by buf, so record that in the |
| 559 | * Python object. */ |
| 560 | ((PyoutputBuffer_Object *)(pyobj_buf))->obj = NULL; |
| 561 | py_retval = libxml_intWrap((int) c_retval); |
| 562 | return(py_retval); |
| 563 | } |
| 564 | |
| 565 | static PyObject * |
| 566 | libxml_xmlSaveFormatFileTo(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) { |
| 567 | PyObject *py_retval; |
| 568 | int c_retval; |
| 569 | xmlOutputBufferPtr buf; |
| 570 | PyObject *pyobj_buf; |
| 571 | xmlDocPtr cur; |
| 572 | PyObject *pyobj_cur; |
| 573 | char * encoding; |
| 574 | int format; |
| 575 | |
| 576 | if (!PyArg_ParseTuple(args, (char *)"OOzi:xmlSaveFormatFileTo", &pyobj_buf, &pyobj_cur, &encoding, &format)) |
| 577 | return(NULL); |
| 578 | buf = (xmlOutputBufferPtr) PyoutputBuffer_Get(pyobj_buf); |
| 579 | cur = (xmlDocPtr) PyxmlNode_Get(pyobj_cur); |
| 580 | |
| 581 | c_retval = xmlSaveFormatFileTo(buf, cur, encoding, format); |
| 582 | /* xmlSaveFormatFileTo() freed the memory pointed to by buf, so record that |
| 583 | * in the Python object */ |
| 584 | ((PyoutputBuffer_Object *)(pyobj_buf))->obj = NULL; |
| 585 | py_retval = libxml_intWrap((int) c_retval); |
| 586 | return(py_retval); |
| 587 | } |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 588 | #endif /* LIBXML_OUTPUT_ENABLED */ |
Daniel Veillard | c6d4a93 | 2002-09-12 15:00:57 +0000 | [diff] [blame] | 589 | |
| 590 | |
| 591 | /** |
| 592 | * xmlParserInputBufferCreatePythonFile: |
| 593 | * @file: a PyFile_Type |
| 594 | * @encoder: the encoding converter or NULL |
| 595 | * |
| 596 | * Create a buffered output for the progressive saving to a PyFile_Type |
| 597 | * buffered C I/O |
| 598 | * |
| 599 | * Returns the new parser output or NULL |
| 600 | */ |
Daniel Veillard | 118aed7 | 2002-09-24 14:13:13 +0000 | [diff] [blame] | 601 | static xmlParserInputBufferPtr |
Daniel Veillard | c6d4a93 | 2002-09-12 15:00:57 +0000 | [diff] [blame] | 602 | xmlParserInputBufferCreatePythonFile(PyObject *file, |
| 603 | xmlCharEncoding encoding) { |
| 604 | xmlParserInputBufferPtr ret; |
| 605 | |
| 606 | if (file == NULL) return(NULL); |
| 607 | |
| 608 | ret = xmlAllocParserInputBuffer(encoding); |
| 609 | if (ret != NULL) { |
| 610 | ret->context = file; |
| 611 | /* Py_INCREF(file); */ |
| 612 | ret->readcallback = xmlPythonFileRead; |
| 613 | ret->closecallback = xmlPythonFileClose; |
| 614 | } |
| 615 | |
| 616 | return(ret); |
| 617 | } |
| 618 | |
| 619 | PyObject * |
| 620 | libxml_xmlCreateInputBuffer(ATTRIBUTE_UNUSED PyObject *self, PyObject *args) { |
| 621 | PyObject *py_retval; |
| 622 | PyObject *file; |
| 623 | xmlChar *encoding; |
| 624 | xmlCharEncoding enc = XML_CHAR_ENCODING_NONE; |
| 625 | xmlParserInputBufferPtr buffer; |
| 626 | |
| 627 | |
| 628 | if (!PyArg_ParseTuple(args, (char *)"Oz:xmlParserInputBufferCreate", |
| 629 | &file, &encoding)) |
| 630 | return(NULL); |
| 631 | if ((encoding != NULL) && (encoding[0] != 0)) { |
Daniel Veillard | 118aed7 | 2002-09-24 14:13:13 +0000 | [diff] [blame] | 632 | enc = xmlParseCharEncoding((const char *) encoding); |
Daniel Veillard | c6d4a93 | 2002-09-12 15:00:57 +0000 | [diff] [blame] | 633 | } |
| 634 | buffer = xmlParserInputBufferCreatePythonFile(file, enc); |
| 635 | if (buffer == NULL) |
| 636 | printf("libxml_xmlParserInputBufferCreate: buffer == NULL\n"); |
| 637 | py_retval = libxml_xmlParserInputBufferPtrWrap(buffer); |
| 638 | return(py_retval); |
| 639 | } |
| 640 | |
| 641 | /************************************************************************ |
| 642 | * * |
| 643 | * Providing the resolver at the Python level * |
| 644 | * * |
| 645 | ************************************************************************/ |
| 646 | |
| 647 | static xmlExternalEntityLoader defaultExternalEntityLoader = NULL; |
| 648 | static PyObject *pythonExternalEntityLoaderObjext; |
| 649 | |
| 650 | static xmlParserInputPtr |
| 651 | pythonExternalEntityLoader(const char *URL, const char *ID, |
| 652 | xmlParserCtxtPtr ctxt) { |
| 653 | xmlParserInputPtr result = NULL; |
| 654 | if (pythonExternalEntityLoaderObjext != NULL) { |
| 655 | PyObject *ret; |
| 656 | PyObject *ctxtobj; |
| 657 | |
| 658 | ctxtobj = libxml_xmlParserCtxtPtrWrap(ctxt); |
| 659 | #ifdef DEBUG_LOADER |
| 660 | printf("pythonExternalEntityLoader: ready to call\n"); |
| 661 | #endif |
| 662 | |
| 663 | ret = PyObject_CallFunction(pythonExternalEntityLoaderObjext, |
Daniel Veillard | 118aed7 | 2002-09-24 14:13:13 +0000 | [diff] [blame] | 664 | (char *) "(ssO)", URL, ID, ctxtobj); |
Daniel Veillard | e4a07e7 | 2003-01-14 14:40:25 +0000 | [diff] [blame] | 665 | Py_XDECREF(ctxtobj); |
Daniel Veillard | c6d4a93 | 2002-09-12 15:00:57 +0000 | [diff] [blame] | 666 | #ifdef DEBUG_LOADER |
| 667 | printf("pythonExternalEntityLoader: result "); |
Daniel Veillard | 007d51e | 2003-09-17 20:07:28 +0000 | [diff] [blame] | 668 | PyObject_Print(ret, stderr, 0); |
Daniel Veillard | c6d4a93 | 2002-09-12 15:00:57 +0000 | [diff] [blame] | 669 | printf("\n"); |
| 670 | #endif |
| 671 | |
| 672 | if (ret != NULL) { |
Daniel Veillard | 118aed7 | 2002-09-24 14:13:13 +0000 | [diff] [blame] | 673 | if (PyObject_HasAttrString(ret, (char *) "read")) { |
Daniel Veillard | c6d4a93 | 2002-09-12 15:00:57 +0000 | [diff] [blame] | 674 | xmlParserInputBufferPtr buf; |
| 675 | |
| 676 | buf = xmlAllocParserInputBuffer(XML_CHAR_ENCODING_NONE); |
| 677 | if (buf != NULL) { |
| 678 | buf->context = ret; |
| 679 | buf->readcallback = xmlPythonFileReadRaw; |
| 680 | buf->closecallback = xmlPythonFileCloseRaw; |
| 681 | result = xmlNewIOInputStream(ctxt, buf, |
| 682 | XML_CHAR_ENCODING_NONE); |
| 683 | } |
Daniel Veillard | 4ea89f0 | 2005-07-29 10:12:45 +0000 | [diff] [blame^] | 684 | #if 0 |
Daniel Veillard | c6d4a93 | 2002-09-12 15:00:57 +0000 | [diff] [blame] | 685 | } else { |
Daniel Veillard | 4ea89f0 | 2005-07-29 10:12:45 +0000 | [diff] [blame^] | 686 | if (URL != NULL) |
| 687 | printf("pythonExternalEntityLoader: can't read %s\n", |
| 688 | URL); |
| 689 | #endif |
Daniel Veillard | c6d4a93 | 2002-09-12 15:00:57 +0000 | [diff] [blame] | 690 | } |
| 691 | if (result == NULL) { |
| 692 | Py_DECREF(ret); |
Daniel Veillard | c64b8e9 | 2003-02-24 11:47:13 +0000 | [diff] [blame] | 693 | } else if (URL != NULL) { |
William M. Brack | c193956 | 2003-08-05 15:52:22 +0000 | [diff] [blame] | 694 | result->filename = (char *) xmlStrdup((const xmlChar *)URL); |
Daniel Veillard | c64b8e9 | 2003-02-24 11:47:13 +0000 | [diff] [blame] | 695 | result->directory = xmlParserGetDirectory((const char *) URL); |
Daniel Veillard | c6d4a93 | 2002-09-12 15:00:57 +0000 | [diff] [blame] | 696 | } |
| 697 | } |
| 698 | } |
| 699 | if ((result == NULL) && (defaultExternalEntityLoader != NULL)) { |
| 700 | result = defaultExternalEntityLoader(URL, ID, ctxt); |
| 701 | } |
| 702 | return(result); |
| 703 | } |
| 704 | |
| 705 | PyObject * |
| 706 | libxml_xmlSetEntityLoader(ATTRIBUTE_UNUSED PyObject *self, PyObject *args) { |
| 707 | PyObject *py_retval; |
| 708 | PyObject *loader; |
| 709 | |
| 710 | if (!PyArg_ParseTuple(args, (char *)"O:libxml_xmlSetEntityLoader", |
| 711 | &loader)) |
| 712 | return(NULL); |
| 713 | |
| 714 | #ifdef DEBUG_LOADER |
| 715 | printf("libxml_xmlSetEntityLoader\n"); |
| 716 | #endif |
| 717 | if (defaultExternalEntityLoader == NULL) |
| 718 | defaultExternalEntityLoader = xmlGetExternalEntityLoader(); |
| 719 | |
| 720 | pythonExternalEntityLoaderObjext = loader; |
| 721 | xmlSetExternalEntityLoader(pythonExternalEntityLoader); |
| 722 | |
| 723 | py_retval = PyInt_FromLong(0); |
| 724 | return(py_retval); |
| 725 | } |
| 726 | |
| 727 | |
| 728 | /************************************************************************ |
| 729 | * * |
Daniel Veillard | 3ce5257 | 2002-02-03 15:08:05 +0000 | [diff] [blame] | 730 | * Handling SAX/xmllib/sgmlop callback interfaces * |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 731 | * * |
| 732 | ************************************************************************/ |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 733 | |
Daniel Veillard | 33caa0b | 2002-02-04 14:07:26 +0000 | [diff] [blame] | 734 | static void |
| 735 | pythonStartElement(void *user_data, const xmlChar * name, |
| 736 | const xmlChar ** attrs) |
| 737 | { |
| 738 | int i; |
| 739 | PyObject *handler; |
| 740 | PyObject *dict; |
| 741 | PyObject *attrname; |
| 742 | PyObject *attrvalue; |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 743 | PyObject *result = NULL; |
Daniel Veillard | 33caa0b | 2002-02-04 14:07:26 +0000 | [diff] [blame] | 744 | int type = 0; |
| 745 | |
Daniel Veillard | 797a565 | 2002-02-12 13:46:21 +0000 | [diff] [blame] | 746 | #ifdef DEBUG_SAX |
| 747 | printf("pythonStartElement(%s) called\n", name); |
| 748 | #endif |
Daniel Veillard | 33caa0b | 2002-02-04 14:07:26 +0000 | [diff] [blame] | 749 | handler = (PyObject *) user_data; |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 750 | if (PyObject_HasAttrString(handler, (char *) "startElement")) |
Daniel Veillard | 33caa0b | 2002-02-04 14:07:26 +0000 | [diff] [blame] | 751 | type = 1; |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 752 | else if (PyObject_HasAttrString(handler, (char *) "start")) |
Daniel Veillard | 33caa0b | 2002-02-04 14:07:26 +0000 | [diff] [blame] | 753 | type = 2; |
| 754 | if (type != 0) { |
| 755 | /* |
| 756 | * the xmllib interface always generate a dictionnary, |
| 757 | * possibly empty |
| 758 | */ |
| 759 | if ((attrs == NULL) && (type == 1)) { |
| 760 | Py_XINCREF(Py_None); |
| 761 | dict = Py_None; |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 762 | } else if (attrs == NULL) { |
| 763 | dict = PyDict_New(); |
Daniel Veillard | 33caa0b | 2002-02-04 14:07:26 +0000 | [diff] [blame] | 764 | } else { |
| 765 | dict = PyDict_New(); |
| 766 | for (i = 0; attrs[i] != NULL; i++) { |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 767 | attrname = PyString_FromString((char *) attrs[i]); |
Daniel Veillard | 33caa0b | 2002-02-04 14:07:26 +0000 | [diff] [blame] | 768 | i++; |
| 769 | if (attrs[i] != NULL) { |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 770 | attrvalue = PyString_FromString((char *) attrs[i]); |
Daniel Veillard | 33caa0b | 2002-02-04 14:07:26 +0000 | [diff] [blame] | 771 | } else { |
| 772 | Py_XINCREF(Py_None); |
| 773 | attrvalue = Py_None; |
| 774 | } |
| 775 | PyDict_SetItem(dict, attrname, attrvalue); |
| 776 | } |
| 777 | } |
| 778 | |
| 779 | if (type == 1) |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 780 | result = PyObject_CallMethod(handler, (char *) "startElement", |
| 781 | (char *) "sO", name, dict); |
Daniel Veillard | 33caa0b | 2002-02-04 14:07:26 +0000 | [diff] [blame] | 782 | else if (type == 2) |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 783 | result = PyObject_CallMethod(handler, (char *) "start", |
| 784 | (char *) "sO", name, dict); |
| 785 | if (PyErr_Occurred()) |
| 786 | PyErr_Print(); |
Daniel Veillard | 33caa0b | 2002-02-04 14:07:26 +0000 | [diff] [blame] | 787 | Py_XDECREF(dict); |
| 788 | Py_XDECREF(result); |
| 789 | } |
| 790 | } |
| 791 | |
| 792 | static void |
| 793 | pythonStartDocument(void *user_data) |
| 794 | { |
| 795 | PyObject *handler; |
| 796 | PyObject *result; |
| 797 | |
Daniel Veillard | 797a565 | 2002-02-12 13:46:21 +0000 | [diff] [blame] | 798 | #ifdef DEBUG_SAX |
| 799 | printf("pythonStartDocument() called\n"); |
| 800 | #endif |
Daniel Veillard | 33caa0b | 2002-02-04 14:07:26 +0000 | [diff] [blame] | 801 | handler = (PyObject *) user_data; |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 802 | if (PyObject_HasAttrString(handler, (char *) "startDocument")) { |
| 803 | result = |
| 804 | PyObject_CallMethod(handler, (char *) "startDocument", NULL); |
| 805 | if (PyErr_Occurred()) |
| 806 | PyErr_Print(); |
Daniel Veillard | 33caa0b | 2002-02-04 14:07:26 +0000 | [diff] [blame] | 807 | Py_XDECREF(result); |
| 808 | } |
| 809 | } |
| 810 | |
| 811 | static void |
| 812 | pythonEndDocument(void *user_data) |
| 813 | { |
| 814 | PyObject *handler; |
| 815 | PyObject *result; |
| 816 | |
Daniel Veillard | 797a565 | 2002-02-12 13:46:21 +0000 | [diff] [blame] | 817 | #ifdef DEBUG_SAX |
| 818 | printf("pythonEndDocument() called\n"); |
| 819 | #endif |
Daniel Veillard | 33caa0b | 2002-02-04 14:07:26 +0000 | [diff] [blame] | 820 | handler = (PyObject *) user_data; |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 821 | if (PyObject_HasAttrString(handler, (char *) "endDocument")) { |
| 822 | result = |
| 823 | PyObject_CallMethod(handler, (char *) "endDocument", NULL); |
| 824 | if (PyErr_Occurred()) |
| 825 | PyErr_Print(); |
Daniel Veillard | 33caa0b | 2002-02-04 14:07:26 +0000 | [diff] [blame] | 826 | Py_XDECREF(result); |
| 827 | } |
| 828 | /* |
| 829 | * The reference to the handler is released there |
| 830 | */ |
| 831 | Py_XDECREF(handler); |
| 832 | } |
| 833 | |
| 834 | static void |
| 835 | pythonEndElement(void *user_data, const xmlChar * name) |
| 836 | { |
| 837 | PyObject *handler; |
| 838 | PyObject *result; |
| 839 | |
Daniel Veillard | 797a565 | 2002-02-12 13:46:21 +0000 | [diff] [blame] | 840 | #ifdef DEBUG_SAX |
| 841 | printf("pythonEndElement(%s) called\n", name); |
| 842 | #endif |
Daniel Veillard | 33caa0b | 2002-02-04 14:07:26 +0000 | [diff] [blame] | 843 | handler = (PyObject *) user_data; |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 844 | if (PyObject_HasAttrString(handler, (char *) "endElement")) { |
| 845 | result = PyObject_CallMethod(handler, (char *) "endElement", |
| 846 | (char *) "s", name); |
| 847 | if (PyErr_Occurred()) |
| 848 | PyErr_Print(); |
Daniel Veillard | 33caa0b | 2002-02-04 14:07:26 +0000 | [diff] [blame] | 849 | Py_XDECREF(result); |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 850 | } else if (PyObject_HasAttrString(handler, (char *) "end")) { |
| 851 | result = PyObject_CallMethod(handler, (char *) "end", |
| 852 | (char *) "s", name); |
| 853 | if (PyErr_Occurred()) |
| 854 | PyErr_Print(); |
Daniel Veillard | 797a565 | 2002-02-12 13:46:21 +0000 | [diff] [blame] | 855 | Py_XDECREF(result); |
Daniel Veillard | 33caa0b | 2002-02-04 14:07:26 +0000 | [diff] [blame] | 856 | } |
| 857 | } |
| 858 | |
| 859 | static void |
| 860 | pythonReference(void *user_data, const xmlChar * name) |
| 861 | { |
| 862 | PyObject *handler; |
| 863 | PyObject *result; |
| 864 | |
Daniel Veillard | 797a565 | 2002-02-12 13:46:21 +0000 | [diff] [blame] | 865 | #ifdef DEBUG_SAX |
| 866 | printf("pythonReference(%s) called\n", name); |
| 867 | #endif |
Daniel Veillard | 33caa0b | 2002-02-04 14:07:26 +0000 | [diff] [blame] | 868 | handler = (PyObject *) user_data; |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 869 | if (PyObject_HasAttrString(handler, (char *) "reference")) { |
| 870 | result = PyObject_CallMethod(handler, (char *) "reference", |
| 871 | (char *) "s", name); |
| 872 | if (PyErr_Occurred()) |
| 873 | PyErr_Print(); |
Daniel Veillard | 33caa0b | 2002-02-04 14:07:26 +0000 | [diff] [blame] | 874 | Py_XDECREF(result); |
| 875 | } |
| 876 | } |
| 877 | |
| 878 | static void |
| 879 | pythonCharacters(void *user_data, const xmlChar * ch, int len) |
| 880 | { |
| 881 | PyObject *handler; |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 882 | PyObject *result = NULL; |
Daniel Veillard | 33caa0b | 2002-02-04 14:07:26 +0000 | [diff] [blame] | 883 | int type = 0; |
| 884 | |
Daniel Veillard | 797a565 | 2002-02-12 13:46:21 +0000 | [diff] [blame] | 885 | #ifdef DEBUG_SAX |
| 886 | printf("pythonCharacters(%s, %d) called\n", ch, len); |
| 887 | #endif |
Daniel Veillard | 33caa0b | 2002-02-04 14:07:26 +0000 | [diff] [blame] | 888 | handler = (PyObject *) user_data; |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 889 | if (PyObject_HasAttrString(handler, (char *) "characters")) |
| 890 | type = 1; |
| 891 | else if (PyObject_HasAttrString(handler, (char *) "data")) |
| 892 | type = 2; |
Daniel Veillard | 33caa0b | 2002-02-04 14:07:26 +0000 | [diff] [blame] | 893 | if (type != 0) { |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 894 | if (type == 1) |
| 895 | result = PyObject_CallMethod(handler, (char *) "characters", |
| 896 | (char *) "s#", ch, len); |
| 897 | else if (type == 2) |
| 898 | result = PyObject_CallMethod(handler, (char *) "data", |
| 899 | (char *) "s#", ch, len); |
| 900 | if (PyErr_Occurred()) |
| 901 | PyErr_Print(); |
Daniel Veillard | 33caa0b | 2002-02-04 14:07:26 +0000 | [diff] [blame] | 902 | Py_XDECREF(result); |
| 903 | } |
| 904 | } |
| 905 | |
| 906 | static void |
| 907 | pythonIgnorableWhitespace(void *user_data, const xmlChar * ch, int len) |
| 908 | { |
| 909 | PyObject *handler; |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 910 | PyObject *result = NULL; |
Daniel Veillard | 33caa0b | 2002-02-04 14:07:26 +0000 | [diff] [blame] | 911 | int type = 0; |
| 912 | |
Daniel Veillard | 797a565 | 2002-02-12 13:46:21 +0000 | [diff] [blame] | 913 | #ifdef DEBUG_SAX |
| 914 | printf("pythonIgnorableWhitespace(%s, %d) called\n", ch, len); |
| 915 | #endif |
Daniel Veillard | 33caa0b | 2002-02-04 14:07:26 +0000 | [diff] [blame] | 916 | handler = (PyObject *) user_data; |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 917 | if (PyObject_HasAttrString(handler, (char *) "ignorableWhitespace")) |
Daniel Veillard | 33caa0b | 2002-02-04 14:07:26 +0000 | [diff] [blame] | 918 | type = 1; |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 919 | else if (PyObject_HasAttrString(handler, (char *) "data")) |
Daniel Veillard | 33caa0b | 2002-02-04 14:07:26 +0000 | [diff] [blame] | 920 | type = 2; |
| 921 | if (type != 0) { |
| 922 | if (type == 1) |
| 923 | result = |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 924 | PyObject_CallMethod(handler, |
| 925 | (char *) "ignorableWhitespace", |
| 926 | (char *) "s#", ch, len); |
Daniel Veillard | 33caa0b | 2002-02-04 14:07:26 +0000 | [diff] [blame] | 927 | else if (type == 2) |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 928 | result = |
| 929 | PyObject_CallMethod(handler, (char *) "data", |
| 930 | (char *) "s#", ch, len); |
Daniel Veillard | 33caa0b | 2002-02-04 14:07:26 +0000 | [diff] [blame] | 931 | Py_XDECREF(result); |
| 932 | } |
| 933 | } |
| 934 | |
| 935 | static void |
| 936 | pythonProcessingInstruction(void *user_data, |
| 937 | const xmlChar * target, const xmlChar * data) |
| 938 | { |
| 939 | PyObject *handler; |
| 940 | PyObject *result; |
| 941 | |
Daniel Veillard | 797a565 | 2002-02-12 13:46:21 +0000 | [diff] [blame] | 942 | #ifdef DEBUG_SAX |
| 943 | printf("pythonProcessingInstruction(%s, %s) called\n", target, data); |
| 944 | #endif |
Daniel Veillard | 33caa0b | 2002-02-04 14:07:26 +0000 | [diff] [blame] | 945 | handler = (PyObject *) user_data; |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 946 | if (PyObject_HasAttrString(handler, (char *) "processingInstruction")) { |
| 947 | result = PyObject_CallMethod(handler, (char *) |
| 948 | "processingInstruction", |
| 949 | (char *) "ss", target, data); |
Daniel Veillard | 33caa0b | 2002-02-04 14:07:26 +0000 | [diff] [blame] | 950 | Py_XDECREF(result); |
| 951 | } |
| 952 | } |
| 953 | |
| 954 | static void |
| 955 | pythonComment(void *user_data, const xmlChar * value) |
| 956 | { |
| 957 | PyObject *handler; |
| 958 | PyObject *result; |
| 959 | |
Daniel Veillard | 797a565 | 2002-02-12 13:46:21 +0000 | [diff] [blame] | 960 | #ifdef DEBUG_SAX |
| 961 | printf("pythonComment(%s) called\n", value); |
| 962 | #endif |
Daniel Veillard | 33caa0b | 2002-02-04 14:07:26 +0000 | [diff] [blame] | 963 | handler = (PyObject *) user_data; |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 964 | if (PyObject_HasAttrString(handler, (char *) "comment")) { |
| 965 | result = |
| 966 | PyObject_CallMethod(handler, (char *) "comment", (char *) "s", |
| 967 | value); |
| 968 | if (PyErr_Occurred()) |
| 969 | PyErr_Print(); |
Daniel Veillard | 33caa0b | 2002-02-04 14:07:26 +0000 | [diff] [blame] | 970 | Py_XDECREF(result); |
| 971 | } |
| 972 | } |
| 973 | |
| 974 | static void |
| 975 | pythonWarning(void *user_data, const char *msg, ...) |
| 976 | { |
| 977 | PyObject *handler; |
| 978 | PyObject *result; |
| 979 | va_list args; |
| 980 | char buf[1024]; |
| 981 | |
Daniel Veillard | 797a565 | 2002-02-12 13:46:21 +0000 | [diff] [blame] | 982 | #ifdef DEBUG_SAX |
| 983 | printf("pythonWarning(%s) called\n", msg); |
| 984 | #endif |
Daniel Veillard | 33caa0b | 2002-02-04 14:07:26 +0000 | [diff] [blame] | 985 | handler = (PyObject *) user_data; |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 986 | if (PyObject_HasAttrString(handler, (char *) "warning")) { |
Daniel Veillard | 33caa0b | 2002-02-04 14:07:26 +0000 | [diff] [blame] | 987 | va_start(args, msg); |
| 988 | vsnprintf(buf, 1023, msg, args); |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 989 | va_end(args); |
| 990 | buf[1023] = 0; |
| 991 | result = |
| 992 | PyObject_CallMethod(handler, (char *) "warning", (char *) "s", |
| 993 | buf); |
| 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 | pythonError(void *user_data, const char *msg, ...) |
| 1002 | { |
| 1003 | PyObject *handler; |
| 1004 | PyObject *result; |
| 1005 | va_list args; |
| 1006 | char buf[1024]; |
| 1007 | |
Daniel Veillard | 797a565 | 2002-02-12 13:46:21 +0000 | [diff] [blame] | 1008 | #ifdef DEBUG_SAX |
| 1009 | printf("pythonError(%s) called\n", msg); |
| 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 *) "error")) { |
Daniel Veillard | 33caa0b | 2002-02-04 14:07:26 +0000 | [diff] [blame] | 1013 | va_start(args, msg); |
| 1014 | vsnprintf(buf, 1023, msg, args); |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1015 | va_end(args); |
| 1016 | buf[1023] = 0; |
| 1017 | result = |
| 1018 | PyObject_CallMethod(handler, (char *) "error", (char *) "s", |
| 1019 | buf); |
| 1020 | if (PyErr_Occurred()) |
| 1021 | PyErr_Print(); |
Daniel Veillard | 33caa0b | 2002-02-04 14:07:26 +0000 | [diff] [blame] | 1022 | Py_XDECREF(result); |
| 1023 | } |
| 1024 | } |
| 1025 | |
| 1026 | static void |
| 1027 | pythonFatalError(void *user_data, const char *msg, ...) |
| 1028 | { |
| 1029 | PyObject *handler; |
| 1030 | PyObject *result; |
| 1031 | va_list args; |
| 1032 | char buf[1024]; |
| 1033 | |
Daniel Veillard | 797a565 | 2002-02-12 13:46:21 +0000 | [diff] [blame] | 1034 | #ifdef DEBUG_SAX |
| 1035 | printf("pythonFatalError(%s) called\n", msg); |
| 1036 | #endif |
Daniel Veillard | 33caa0b | 2002-02-04 14:07:26 +0000 | [diff] [blame] | 1037 | handler = (PyObject *) user_data; |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1038 | if (PyObject_HasAttrString(handler, (char *) "fatalError")) { |
Daniel Veillard | 33caa0b | 2002-02-04 14:07:26 +0000 | [diff] [blame] | 1039 | va_start(args, msg); |
| 1040 | vsnprintf(buf, 1023, msg, args); |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1041 | va_end(args); |
| 1042 | buf[1023] = 0; |
| 1043 | result = |
| 1044 | PyObject_CallMethod(handler, (char *) "fatalError", |
| 1045 | (char *) "s", buf); |
| 1046 | if (PyErr_Occurred()) |
| 1047 | PyErr_Print(); |
Daniel Veillard | 33caa0b | 2002-02-04 14:07:26 +0000 | [diff] [blame] | 1048 | Py_XDECREF(result); |
| 1049 | } |
| 1050 | } |
| 1051 | |
| 1052 | static void |
| 1053 | pythonCdataBlock(void *user_data, const xmlChar * ch, int len) |
| 1054 | { |
| 1055 | PyObject *handler; |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1056 | PyObject *result = NULL; |
Daniel Veillard | 33caa0b | 2002-02-04 14:07:26 +0000 | [diff] [blame] | 1057 | int type = 0; |
| 1058 | |
Daniel Veillard | 797a565 | 2002-02-12 13:46:21 +0000 | [diff] [blame] | 1059 | #ifdef DEBUG_SAX |
| 1060 | printf("pythonCdataBlock(%s, %d) called\n", ch, len); |
| 1061 | #endif |
Daniel Veillard | 33caa0b | 2002-02-04 14:07:26 +0000 | [diff] [blame] | 1062 | handler = (PyObject *) user_data; |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1063 | if (PyObject_HasAttrString(handler, (char *) "cdataBlock")) |
| 1064 | type = 1; |
| 1065 | else if (PyObject_HasAttrString(handler, (char *) "cdata")) |
| 1066 | type = 2; |
Daniel Veillard | 33caa0b | 2002-02-04 14:07:26 +0000 | [diff] [blame] | 1067 | if (type != 0) { |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1068 | if (type == 1) |
| 1069 | result = |
| 1070 | PyObject_CallMethod(handler, (char *) "cdataBlock", |
| 1071 | (char *) "s#", ch, len); |
| 1072 | else if (type == 2) |
| 1073 | result = |
| 1074 | PyObject_CallMethod(handler, (char *) "cdata", |
| 1075 | (char *) "s#", ch, len); |
| 1076 | if (PyErr_Occurred()) |
| 1077 | PyErr_Print(); |
Daniel Veillard | 33caa0b | 2002-02-04 14:07:26 +0000 | [diff] [blame] | 1078 | Py_XDECREF(result); |
| 1079 | } |
| 1080 | } |
| 1081 | |
| 1082 | static void |
| 1083 | pythonExternalSubset(void *user_data, |
| 1084 | const xmlChar * name, |
| 1085 | const xmlChar * externalID, const xmlChar * systemID) |
| 1086 | { |
| 1087 | PyObject *handler; |
| 1088 | PyObject *result; |
| 1089 | |
Daniel Veillard | 797a565 | 2002-02-12 13:46:21 +0000 | [diff] [blame] | 1090 | #ifdef DEBUG_SAX |
| 1091 | printf("pythonExternalSubset(%s, %s, %s) called\n", |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1092 | name, externalID, systemID); |
Daniel Veillard | 797a565 | 2002-02-12 13:46:21 +0000 | [diff] [blame] | 1093 | #endif |
Daniel Veillard | 33caa0b | 2002-02-04 14:07:26 +0000 | [diff] [blame] | 1094 | handler = (PyObject *) user_data; |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1095 | if (PyObject_HasAttrString(handler, (char *) "externalSubset")) { |
Daniel Veillard | 33caa0b | 2002-02-04 14:07:26 +0000 | [diff] [blame] | 1096 | result = |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1097 | PyObject_CallMethod(handler, (char *) "externalSubset", |
| 1098 | (char *) "sss", name, externalID, |
| 1099 | systemID); |
Daniel Veillard | 33caa0b | 2002-02-04 14:07:26 +0000 | [diff] [blame] | 1100 | Py_XDECREF(result); |
| 1101 | } |
| 1102 | } |
| 1103 | |
| 1104 | static void |
| 1105 | pythonEntityDecl(void *user_data, |
| 1106 | const xmlChar * name, |
| 1107 | int type, |
| 1108 | const xmlChar * publicId, |
| 1109 | const xmlChar * systemId, xmlChar * content) |
| 1110 | { |
| 1111 | PyObject *handler; |
| 1112 | PyObject *result; |
| 1113 | |
| 1114 | handler = (PyObject *) user_data; |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1115 | if (PyObject_HasAttrString(handler, (char *) "entityDecl")) { |
| 1116 | result = PyObject_CallMethod(handler, (char *) "entityDecl", |
| 1117 | (char *) "sisss", name, type, |
| 1118 | publicId, systemId, content); |
| 1119 | if (PyErr_Occurred()) |
| 1120 | PyErr_Print(); |
Daniel Veillard | 33caa0b | 2002-02-04 14:07:26 +0000 | [diff] [blame] | 1121 | Py_XDECREF(result); |
| 1122 | } |
| 1123 | } |
| 1124 | |
| 1125 | |
| 1126 | |
| 1127 | static void |
| 1128 | |
| 1129 | pythonNotationDecl(void *user_data, |
| 1130 | const xmlChar * name, |
| 1131 | const xmlChar * publicId, const xmlChar * systemId) |
| 1132 | { |
| 1133 | PyObject *handler; |
| 1134 | PyObject *result; |
| 1135 | |
| 1136 | handler = (PyObject *) user_data; |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1137 | if (PyObject_HasAttrString(handler, (char *) "notationDecl")) { |
| 1138 | result = PyObject_CallMethod(handler, (char *) "notationDecl", |
| 1139 | (char *) "sss", name, publicId, |
| 1140 | systemId); |
| 1141 | if (PyErr_Occurred()) |
| 1142 | PyErr_Print(); |
Daniel Veillard | 33caa0b | 2002-02-04 14:07:26 +0000 | [diff] [blame] | 1143 | Py_XDECREF(result); |
| 1144 | } |
| 1145 | } |
| 1146 | |
| 1147 | static void |
| 1148 | pythonAttributeDecl(void *user_data, |
| 1149 | const xmlChar * elem, |
| 1150 | const xmlChar * name, |
| 1151 | int type, |
| 1152 | int def, |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1153 | const xmlChar * defaultValue, xmlEnumerationPtr tree) |
Daniel Veillard | 33caa0b | 2002-02-04 14:07:26 +0000 | [diff] [blame] | 1154 | { |
| 1155 | PyObject *handler; |
| 1156 | PyObject *nameList; |
| 1157 | PyObject *newName; |
| 1158 | xmlEnumerationPtr node; |
| 1159 | PyObject *result; |
| 1160 | int count; |
| 1161 | |
| 1162 | handler = (PyObject *) user_data; |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1163 | if (PyObject_HasAttrString(handler, (char *) "attributeDecl")) { |
Daniel Veillard | 33caa0b | 2002-02-04 14:07:26 +0000 | [diff] [blame] | 1164 | count = 0; |
| 1165 | for (node = tree; node != NULL; node = node->next) { |
| 1166 | count++; |
| 1167 | } |
| 1168 | nameList = PyList_New(count); |
| 1169 | count = 0; |
| 1170 | for (node = tree; node != NULL; node = node->next) { |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1171 | newName = PyString_FromString((char *) node->name); |
Daniel Veillard | 33caa0b | 2002-02-04 14:07:26 +0000 | [diff] [blame] | 1172 | PyList_SetItem(nameList, count, newName); |
| 1173 | count++; |
| 1174 | } |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1175 | result = PyObject_CallMethod(handler, (char *) "attributeDecl", |
| 1176 | (char *) "ssiisO", elem, name, type, |
| 1177 | def, defaultValue, nameList); |
| 1178 | if (PyErr_Occurred()) |
| 1179 | PyErr_Print(); |
Daniel Veillard | 33caa0b | 2002-02-04 14:07:26 +0000 | [diff] [blame] | 1180 | Py_XDECREF(nameList); |
| 1181 | Py_XDECREF(result); |
| 1182 | } |
| 1183 | } |
| 1184 | |
| 1185 | static void |
| 1186 | pythonElementDecl(void *user_data, |
| 1187 | const xmlChar * name, |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1188 | int type, ATTRIBUTE_UNUSED xmlElementContentPtr content) |
Daniel Veillard | 33caa0b | 2002-02-04 14:07:26 +0000 | [diff] [blame] | 1189 | { |
| 1190 | PyObject *handler; |
| 1191 | PyObject *obj; |
| 1192 | PyObject *result; |
| 1193 | |
| 1194 | handler = (PyObject *) user_data; |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1195 | if (PyObject_HasAttrString(handler, (char *) "elementDecl")) { |
| 1196 | /* TODO: wrap in an elementContent object */ |
| 1197 | printf |
| 1198 | ("pythonElementDecl: xmlElementContentPtr wrapper missing !\n"); |
| 1199 | obj = Py_None; |
| 1200 | /* Py_XINCREF(Py_None); isn't the reference just borrowed ??? */ |
| 1201 | result = PyObject_CallMethod(handler, (char *) "elementDecl", |
| 1202 | (char *) "siO", name, type, obj); |
| 1203 | if (PyErr_Occurred()) |
| 1204 | PyErr_Print(); |
Daniel Veillard | 33caa0b | 2002-02-04 14:07:26 +0000 | [diff] [blame] | 1205 | Py_XDECREF(result); |
| 1206 | } |
| 1207 | } |
| 1208 | |
| 1209 | static void |
| 1210 | pythonUnparsedEntityDecl(void *user_data, |
| 1211 | const xmlChar * name, |
| 1212 | const xmlChar * publicId, |
| 1213 | const xmlChar * systemId, |
| 1214 | const xmlChar * notationName) |
| 1215 | { |
| 1216 | PyObject *handler; |
| 1217 | PyObject *result; |
| 1218 | |
| 1219 | handler = (PyObject *) user_data; |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1220 | if (PyObject_HasAttrString(handler, (char *) "unparsedEntityDecl")) { |
| 1221 | result = |
| 1222 | PyObject_CallMethod(handler, (char *) "unparsedEntityDecl", |
| 1223 | (char *) "ssss", name, publicId, systemId, |
| 1224 | notationName); |
| 1225 | if (PyErr_Occurred()) |
| 1226 | PyErr_Print(); |
Daniel Veillard | 33caa0b | 2002-02-04 14:07:26 +0000 | [diff] [blame] | 1227 | Py_XDECREF(result); |
| 1228 | } |
| 1229 | } |
| 1230 | |
| 1231 | static void |
| 1232 | pythonInternalSubset(void *user_data, const xmlChar * name, |
| 1233 | const xmlChar * ExternalID, const xmlChar * SystemID) |
| 1234 | { |
| 1235 | PyObject *handler; |
| 1236 | PyObject *result; |
| 1237 | |
Daniel Veillard | 797a565 | 2002-02-12 13:46:21 +0000 | [diff] [blame] | 1238 | #ifdef DEBUG_SAX |
| 1239 | printf("pythonInternalSubset(%s, %s, %s) called\n", |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1240 | name, ExternalID, SystemID); |
Daniel Veillard | 797a565 | 2002-02-12 13:46:21 +0000 | [diff] [blame] | 1241 | #endif |
Daniel Veillard | 33caa0b | 2002-02-04 14:07:26 +0000 | [diff] [blame] | 1242 | handler = (PyObject *) user_data; |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1243 | if (PyObject_HasAttrString(handler, (char *) "internalSubset")) { |
| 1244 | result = PyObject_CallMethod(handler, (char *) "internalSubset", |
| 1245 | (char *) "sss", name, ExternalID, |
| 1246 | SystemID); |
| 1247 | if (PyErr_Occurred()) |
| 1248 | PyErr_Print(); |
Daniel Veillard | 33caa0b | 2002-02-04 14:07:26 +0000 | [diff] [blame] | 1249 | Py_XDECREF(result); |
| 1250 | } |
| 1251 | } |
| 1252 | |
| 1253 | static xmlSAXHandler pythonSaxHandler = { |
| 1254 | pythonInternalSubset, |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1255 | NULL, /* TODO pythonIsStandalone, */ |
| 1256 | NULL, /* TODO pythonHasInternalSubset, */ |
| 1257 | NULL, /* TODO pythonHasExternalSubset, */ |
| 1258 | NULL, /* TODO pythonResolveEntity, */ |
| 1259 | NULL, /* TODO pythonGetEntity, */ |
Daniel Veillard | 33caa0b | 2002-02-04 14:07:26 +0000 | [diff] [blame] | 1260 | pythonEntityDecl, |
| 1261 | pythonNotationDecl, |
| 1262 | pythonAttributeDecl, |
| 1263 | pythonElementDecl, |
| 1264 | pythonUnparsedEntityDecl, |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1265 | NULL, /* OBSOLETED pythonSetDocumentLocator, */ |
Daniel Veillard | 33caa0b | 2002-02-04 14:07:26 +0000 | [diff] [blame] | 1266 | pythonStartDocument, |
| 1267 | pythonEndDocument, |
| 1268 | pythonStartElement, |
| 1269 | pythonEndElement, |
| 1270 | pythonReference, |
| 1271 | pythonCharacters, |
| 1272 | pythonIgnorableWhitespace, |
| 1273 | pythonProcessingInstruction, |
| 1274 | pythonComment, |
| 1275 | pythonWarning, |
| 1276 | pythonError, |
| 1277 | pythonFatalError, |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1278 | NULL, /* TODO pythonGetParameterEntity, */ |
Daniel Veillard | 33caa0b | 2002-02-04 14:07:26 +0000 | [diff] [blame] | 1279 | pythonCdataBlock, |
| 1280 | pythonExternalSubset, |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 1281 | 1, |
| 1282 | NULL, /* TODO mograte to SAX2 */ |
| 1283 | NULL, |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 1284 | NULL, |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 1285 | NULL |
Daniel Veillard | 33caa0b | 2002-02-04 14:07:26 +0000 | [diff] [blame] | 1286 | }; |
Daniel Veillard | 3ce5257 | 2002-02-03 15:08:05 +0000 | [diff] [blame] | 1287 | |
| 1288 | /************************************************************************ |
| 1289 | * * |
| 1290 | * Handling of specific parser context * |
| 1291 | * * |
| 1292 | ************************************************************************/ |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 1293 | |
| 1294 | PyObject * |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1295 | libxml_xmlCreatePushParser(ATTRIBUTE_UNUSED PyObject * self, |
| 1296 | PyObject * args) |
| 1297 | { |
| 1298 | const char *chunk; |
Daniel Veillard | 3ce5257 | 2002-02-03 15:08:05 +0000 | [diff] [blame] | 1299 | int size; |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1300 | const char *URI; |
Daniel Veillard | 33caa0b | 2002-02-04 14:07:26 +0000 | [diff] [blame] | 1301 | PyObject *pyobj_SAX = NULL; |
Daniel Veillard | 3ce5257 | 2002-02-03 15:08:05 +0000 | [diff] [blame] | 1302 | xmlSAXHandlerPtr SAX = NULL; |
Daniel Veillard | 3ce5257 | 2002-02-03 15:08:05 +0000 | [diff] [blame] | 1303 | xmlParserCtxtPtr ret; |
| 1304 | PyObject *pyret; |
Daniel Veillard | 96fe095 | 2002-01-30 20:52:23 +0000 | [diff] [blame] | 1305 | |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1306 | if (!PyArg_ParseTuple |
| 1307 | (args, (char *) "Oziz:xmlCreatePushParser", &pyobj_SAX, &chunk, |
| 1308 | &size, &URI)) |
| 1309 | return (NULL); |
Daniel Veillard | 3ce5257 | 2002-02-03 15:08:05 +0000 | [diff] [blame] | 1310 | |
Daniel Veillard | 8d24cc1 | 2002-03-05 15:41:29 +0000 | [diff] [blame] | 1311 | #ifdef DEBUG |
Daniel Veillard | 3ce5257 | 2002-02-03 15:08:05 +0000 | [diff] [blame] | 1312 | printf("libxml_xmlCreatePushParser(%p, %s, %d, %s) called\n", |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1313 | pyobj_SAX, chunk, size, URI); |
Daniel Veillard | 96fe095 | 2002-01-30 20:52:23 +0000 | [diff] [blame] | 1314 | #endif |
Daniel Veillard | 3ce5257 | 2002-02-03 15:08:05 +0000 | [diff] [blame] | 1315 | if (pyobj_SAX != Py_None) { |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1316 | SAX = &pythonSaxHandler; |
| 1317 | Py_INCREF(pyobj_SAX); |
| 1318 | /* The reference is released in pythonEndDocument() */ |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 1319 | } |
Daniel Veillard | 33caa0b | 2002-02-04 14:07:26 +0000 | [diff] [blame] | 1320 | ret = xmlCreatePushParserCtxt(SAX, pyobj_SAX, chunk, size, URI); |
Daniel Veillard | 3ce5257 | 2002-02-03 15:08:05 +0000 | [diff] [blame] | 1321 | pyret = libxml_xmlParserCtxtPtrWrap(ret); |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1322 | return (pyret); |
Daniel Veillard | a7340c8 | 2002-02-01 17:56:45 +0000 | [diff] [blame] | 1323 | } |
Daniel Veillard | 5d81903 | 2002-02-02 21:49:17 +0000 | [diff] [blame] | 1324 | |
Daniel Veillard | 4e1b26c | 2002-02-03 20:13:06 +0000 | [diff] [blame] | 1325 | PyObject * |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1326 | libxml_htmlCreatePushParser(ATTRIBUTE_UNUSED PyObject * self, |
| 1327 | PyObject * args) |
| 1328 | { |
Daniel Veillard | 656ce94 | 2004-04-30 23:11:45 +0000 | [diff] [blame] | 1329 | #ifdef LIBXML_HTML_ENABLED |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1330 | const char *chunk; |
Daniel Veillard | 4e1b26c | 2002-02-03 20:13:06 +0000 | [diff] [blame] | 1331 | int size; |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1332 | const char *URI; |
Daniel Veillard | 33caa0b | 2002-02-04 14:07:26 +0000 | [diff] [blame] | 1333 | PyObject *pyobj_SAX = NULL; |
Daniel Veillard | 4e1b26c | 2002-02-03 20:13:06 +0000 | [diff] [blame] | 1334 | xmlSAXHandlerPtr SAX = NULL; |
Daniel Veillard | 4e1b26c | 2002-02-03 20:13:06 +0000 | [diff] [blame] | 1335 | xmlParserCtxtPtr ret; |
| 1336 | PyObject *pyret; |
| 1337 | |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1338 | if (!PyArg_ParseTuple |
| 1339 | (args, (char *) "Oziz:htmlCreatePushParser", &pyobj_SAX, &chunk, |
| 1340 | &size, &URI)) |
| 1341 | return (NULL); |
Daniel Veillard | 4e1b26c | 2002-02-03 20:13:06 +0000 | [diff] [blame] | 1342 | |
Daniel Veillard | 8d24cc1 | 2002-03-05 15:41:29 +0000 | [diff] [blame] | 1343 | #ifdef DEBUG |
Daniel Veillard | 4e1b26c | 2002-02-03 20:13:06 +0000 | [diff] [blame] | 1344 | printf("libxml_htmlCreatePushParser(%p, %s, %d, %s) called\n", |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1345 | pyobj_SAX, chunk, size, URI); |
Daniel Veillard | 4e1b26c | 2002-02-03 20:13:06 +0000 | [diff] [blame] | 1346 | #endif |
| 1347 | if (pyobj_SAX != Py_None) { |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1348 | SAX = &pythonSaxHandler; |
| 1349 | Py_INCREF(pyobj_SAX); |
| 1350 | /* The reference is released in pythonEndDocument() */ |
Daniel Veillard | 4e1b26c | 2002-02-03 20:13:06 +0000 | [diff] [blame] | 1351 | } |
Daniel Veillard | 33caa0b | 2002-02-04 14:07:26 +0000 | [diff] [blame] | 1352 | ret = htmlCreatePushParserCtxt(SAX, pyobj_SAX, chunk, size, URI, |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1353 | XML_CHAR_ENCODING_NONE); |
Daniel Veillard | 4e1b26c | 2002-02-03 20:13:06 +0000 | [diff] [blame] | 1354 | pyret = libxml_xmlParserCtxtPtrWrap(ret); |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1355 | return (pyret); |
Daniel Veillard | 656ce94 | 2004-04-30 23:11:45 +0000 | [diff] [blame] | 1356 | #else |
| 1357 | Py_INCREF(Py_None); |
| 1358 | return (Py_None); |
| 1359 | #endif /* LIBXML_HTML_ENABLED */ |
Daniel Veillard | 4e1b26c | 2002-02-03 20:13:06 +0000 | [diff] [blame] | 1360 | } |
| 1361 | |
Daniel Veillard | 8d24cc1 | 2002-03-05 15:41:29 +0000 | [diff] [blame] | 1362 | PyObject * |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1363 | libxml_xmlSAXParseFile(ATTRIBUTE_UNUSED PyObject * self, PyObject * args) |
| 1364 | { |
Daniel Veillard | 8d24cc1 | 2002-03-05 15:41:29 +0000 | [diff] [blame] | 1365 | int recover; |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1366 | const char *URI; |
Daniel Veillard | 8d24cc1 | 2002-03-05 15:41:29 +0000 | [diff] [blame] | 1367 | PyObject *pyobj_SAX = NULL; |
| 1368 | xmlSAXHandlerPtr SAX = NULL; |
| 1369 | |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1370 | if (!PyArg_ParseTuple(args, (char *) "Osi:xmlSAXParseFile", &pyobj_SAX, |
| 1371 | &URI, &recover)) |
| 1372 | return (NULL); |
Daniel Veillard | 8d24cc1 | 2002-03-05 15:41:29 +0000 | [diff] [blame] | 1373 | |
| 1374 | #ifdef DEBUG |
| 1375 | printf("libxml_xmlSAXParseFile(%p, %s, %d) called\n", |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1376 | pyobj_SAX, URI, recover); |
Daniel Veillard | 8d24cc1 | 2002-03-05 15:41:29 +0000 | [diff] [blame] | 1377 | #endif |
| 1378 | if (pyobj_SAX == Py_None) { |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1379 | Py_INCREF(Py_None); |
| 1380 | return (Py_None); |
Daniel Veillard | 8d24cc1 | 2002-03-05 15:41:29 +0000 | [diff] [blame] | 1381 | } |
| 1382 | SAX = &pythonSaxHandler; |
| 1383 | Py_INCREF(pyobj_SAX); |
| 1384 | /* The reference is released in pythonEndDocument() */ |
Daniel Veillard | f2531af | 2005-03-31 11:06:29 +0000 | [diff] [blame] | 1385 | xmlSAXUserParseFile(SAX, pyobj_SAX, URI); |
Daniel Veillard | 8d24cc1 | 2002-03-05 15:41:29 +0000 | [diff] [blame] | 1386 | Py_INCREF(Py_None); |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1387 | return (Py_None); |
Daniel Veillard | 8d24cc1 | 2002-03-05 15:41:29 +0000 | [diff] [blame] | 1388 | } |
| 1389 | |
| 1390 | PyObject * |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1391 | libxml_htmlSAXParseFile(ATTRIBUTE_UNUSED PyObject * self, PyObject * args) |
| 1392 | { |
Daniel Veillard | 656ce94 | 2004-04-30 23:11:45 +0000 | [diff] [blame] | 1393 | #ifdef LIBXML_HTML_ENABLED |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1394 | const char *URI; |
| 1395 | const char *encoding; |
Daniel Veillard | 8d24cc1 | 2002-03-05 15:41:29 +0000 | [diff] [blame] | 1396 | PyObject *pyobj_SAX = NULL; |
| 1397 | xmlSAXHandlerPtr SAX = NULL; |
| 1398 | |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1399 | if (!PyArg_ParseTuple |
| 1400 | (args, (char *) "Osz:htmlSAXParseFile", &pyobj_SAX, &URI, |
| 1401 | &encoding)) |
| 1402 | return (NULL); |
Daniel Veillard | 8d24cc1 | 2002-03-05 15:41:29 +0000 | [diff] [blame] | 1403 | |
| 1404 | #ifdef DEBUG |
| 1405 | printf("libxml_htmlSAXParseFile(%p, %s, %s) called\n", |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1406 | pyobj_SAX, URI, encoding); |
Daniel Veillard | 8d24cc1 | 2002-03-05 15:41:29 +0000 | [diff] [blame] | 1407 | #endif |
| 1408 | if (pyobj_SAX == Py_None) { |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1409 | Py_INCREF(Py_None); |
| 1410 | return (Py_None); |
Daniel Veillard | 8d24cc1 | 2002-03-05 15:41:29 +0000 | [diff] [blame] | 1411 | } |
| 1412 | SAX = &pythonSaxHandler; |
| 1413 | Py_INCREF(pyobj_SAX); |
| 1414 | /* The reference is released in pythonEndDocument() */ |
| 1415 | htmlSAXParseFile(URI, encoding, SAX, pyobj_SAX); |
| 1416 | Py_INCREF(Py_None); |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1417 | return (Py_None); |
Daniel Veillard | 656ce94 | 2004-04-30 23:11:45 +0000 | [diff] [blame] | 1418 | #else |
| 1419 | Py_INCREF(Py_None); |
| 1420 | return (Py_None); |
| 1421 | #endif /* LIBXML_HTML_ENABLED */ |
Daniel Veillard | 8d24cc1 | 2002-03-05 15:41:29 +0000 | [diff] [blame] | 1422 | } |
| 1423 | |
Daniel Veillard | 5d81903 | 2002-02-02 21:49:17 +0000 | [diff] [blame] | 1424 | /************************************************************************ |
| 1425 | * * |
| 1426 | * Error message callback * |
| 1427 | * * |
| 1428 | ************************************************************************/ |
| 1429 | |
| 1430 | static PyObject *libxml_xmlPythonErrorFuncHandler = NULL; |
| 1431 | static PyObject *libxml_xmlPythonErrorFuncCtxt = NULL; |
| 1432 | |
Daniel Veillard | e6227e0 | 2003-01-14 11:42:39 +0000 | [diff] [blame] | 1433 | /* helper to build a xmlMalloc'ed string from a format and va_list */ |
Daniel Veillard | ad9fb7c | 2004-10-22 11:05:37 +0000 | [diff] [blame] | 1434 | /* |
| 1435 | * disabled the loop, the repeated call to vsnprintf without reset of ap |
| 1436 | * in case the initial buffer was too small segfaulted on x86_64 |
| 1437 | * we now directly vsnprintf on a large buffer. |
| 1438 | */ |
Daniel Veillard | e6227e0 | 2003-01-14 11:42:39 +0000 | [diff] [blame] | 1439 | static char * |
| 1440 | libxml_buildMessage(const char *msg, va_list ap) |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1441 | { |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1442 | int chars; |
Daniel Veillard | e6227e0 | 2003-01-14 11:42:39 +0000 | [diff] [blame] | 1443 | char *str; |
| 1444 | |
Daniel Veillard | ad9fb7c | 2004-10-22 11:05:37 +0000 | [diff] [blame] | 1445 | str = (char *) xmlMalloc(1000); |
Daniel Veillard | e6227e0 | 2003-01-14 11:42:39 +0000 | [diff] [blame] | 1446 | if (str == NULL) |
| 1447 | return NULL; |
| 1448 | |
Daniel Veillard | ad9fb7c | 2004-10-22 11:05:37 +0000 | [diff] [blame] | 1449 | chars = vsnprintf(str, 999, msg, ap); |
| 1450 | if (chars >= 998) |
| 1451 | str[999] = 0; |
Daniel Veillard | e6227e0 | 2003-01-14 11:42:39 +0000 | [diff] [blame] | 1452 | |
| 1453 | return str; |
| 1454 | } |
| 1455 | |
| 1456 | static void |
| 1457 | libxml_xmlErrorFuncHandler(ATTRIBUTE_UNUSED void *ctx, const char *msg, |
| 1458 | ...) |
| 1459 | { |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1460 | va_list ap; |
Daniel Veillard | 5d81903 | 2002-02-02 21:49:17 +0000 | [diff] [blame] | 1461 | PyObject *list; |
| 1462 | PyObject *message; |
| 1463 | PyObject *result; |
Daniel Veillard | ad9fb7c | 2004-10-22 11:05:37 +0000 | [diff] [blame] | 1464 | char str[1000]; |
Daniel Veillard | 5d81903 | 2002-02-02 21:49:17 +0000 | [diff] [blame] | 1465 | |
| 1466 | #ifdef DEBUG_ERROR |
| 1467 | printf("libxml_xmlErrorFuncHandler(%p, %s, ...) called\n", ctx, msg); |
| 1468 | #endif |
| 1469 | |
| 1470 | |
| 1471 | if (libxml_xmlPythonErrorFuncHandler == NULL) { |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1472 | va_start(ap, msg); |
Daniel Veillard | 007d51e | 2003-09-17 20:07:28 +0000 | [diff] [blame] | 1473 | vfprintf(stderr, msg, ap); |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1474 | va_end(ap); |
Daniel Veillard | 5d81903 | 2002-02-02 21:49:17 +0000 | [diff] [blame] | 1475 | } else { |
Daniel Veillard | e6227e0 | 2003-01-14 11:42:39 +0000 | [diff] [blame] | 1476 | va_start(ap, msg); |
Daniel Veillard | ad9fb7c | 2004-10-22 11:05:37 +0000 | [diff] [blame] | 1477 | if (vsnprintf(str, 999, msg, ap) >= 998) |
| 1478 | str[999] = 0; |
Daniel Veillard | e6227e0 | 2003-01-14 11:42:39 +0000 | [diff] [blame] | 1479 | va_end(ap); |
Daniel Veillard | 5d81903 | 2002-02-02 21:49:17 +0000 | [diff] [blame] | 1480 | |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1481 | list = PyTuple_New(2); |
| 1482 | PyTuple_SetItem(list, 0, libxml_xmlPythonErrorFuncCtxt); |
| 1483 | Py_XINCREF(libxml_xmlPythonErrorFuncCtxt); |
Daniel Veillard | ad9fb7c | 2004-10-22 11:05:37 +0000 | [diff] [blame] | 1484 | message = libxml_charPtrConstWrap(str); |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1485 | PyTuple_SetItem(list, 1, message); |
| 1486 | result = PyEval_CallObject(libxml_xmlPythonErrorFuncHandler, list); |
| 1487 | Py_XDECREF(list); |
| 1488 | Py_XDECREF(result); |
Daniel Veillard | 5d81903 | 2002-02-02 21:49:17 +0000 | [diff] [blame] | 1489 | } |
| 1490 | } |
| 1491 | |
| 1492 | static void |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1493 | libxml_xmlErrorInitialize(void) |
| 1494 | { |
Daniel Veillard | 5d81903 | 2002-02-02 21:49:17 +0000 | [diff] [blame] | 1495 | #ifdef DEBUG_ERROR |
| 1496 | printf("libxml_xmlErrorInitialize() called\n"); |
| 1497 | #endif |
| 1498 | xmlSetGenericErrorFunc(NULL, libxml_xmlErrorFuncHandler); |
Daniel Veillard | 781ac8b | 2003-05-15 22:11:36 +0000 | [diff] [blame] | 1499 | xmlThrDefSetGenericErrorFunc(NULL, libxml_xmlErrorFuncHandler); |
Daniel Veillard | 5d81903 | 2002-02-02 21:49:17 +0000 | [diff] [blame] | 1500 | } |
| 1501 | |
Daniel Veillard | c266464 | 2003-07-29 20:44:53 +0000 | [diff] [blame] | 1502 | static PyObject * |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1503 | libxml_xmlRegisterErrorHandler(ATTRIBUTE_UNUSED PyObject * self, |
| 1504 | PyObject * args) |
| 1505 | { |
Daniel Veillard | 5d81903 | 2002-02-02 21:49:17 +0000 | [diff] [blame] | 1506 | PyObject *py_retval; |
| 1507 | PyObject *pyobj_f; |
| 1508 | PyObject *pyobj_ctx; |
| 1509 | |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1510 | if (!PyArg_ParseTuple |
| 1511 | (args, (char *) "OO:xmlRegisterErrorHandler", &pyobj_f, |
| 1512 | &pyobj_ctx)) |
| 1513 | return (NULL); |
Daniel Veillard | 5d81903 | 2002-02-02 21:49:17 +0000 | [diff] [blame] | 1514 | |
| 1515 | #ifdef DEBUG_ERROR |
William M. Brack | 8e2cc6f | 2004-07-03 23:28:52 +0000 | [diff] [blame] | 1516 | printf("libxml_xmlRegisterErrorHandler(%p, %p) called\n", pyobj_ctx, |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1517 | pyobj_f); |
Daniel Veillard | 5d81903 | 2002-02-02 21:49:17 +0000 | [diff] [blame] | 1518 | #endif |
| 1519 | |
| 1520 | if (libxml_xmlPythonErrorFuncHandler != NULL) { |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1521 | Py_XDECREF(libxml_xmlPythonErrorFuncHandler); |
Daniel Veillard | 5d81903 | 2002-02-02 21:49:17 +0000 | [diff] [blame] | 1522 | } |
| 1523 | if (libxml_xmlPythonErrorFuncCtxt != NULL) { |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1524 | Py_XDECREF(libxml_xmlPythonErrorFuncCtxt); |
Daniel Veillard | 5d81903 | 2002-02-02 21:49:17 +0000 | [diff] [blame] | 1525 | } |
| 1526 | |
| 1527 | Py_XINCREF(pyobj_ctx); |
| 1528 | Py_XINCREF(pyobj_f); |
| 1529 | |
| 1530 | /* TODO: check f is a function ! */ |
| 1531 | libxml_xmlPythonErrorFuncHandler = pyobj_f; |
| 1532 | libxml_xmlPythonErrorFuncCtxt = pyobj_ctx; |
| 1533 | |
| 1534 | py_retval = libxml_intWrap(1); |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1535 | return (py_retval); |
Daniel Veillard | 5d81903 | 2002-02-02 21:49:17 +0000 | [diff] [blame] | 1536 | } |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 1537 | |
Daniel Veillard | e6227e0 | 2003-01-14 11:42:39 +0000 | [diff] [blame] | 1538 | |
| 1539 | /************************************************************************ |
| 1540 | * * |
| 1541 | * Per parserCtxt error handler * |
| 1542 | * * |
| 1543 | ************************************************************************/ |
| 1544 | |
Daniel Veillard | 417be3a | 2003-01-20 21:26:34 +0000 | [diff] [blame] | 1545 | typedef struct |
Daniel Veillard | e6227e0 | 2003-01-14 11:42:39 +0000 | [diff] [blame] | 1546 | { |
Daniel Veillard | 417be3a | 2003-01-20 21:26:34 +0000 | [diff] [blame] | 1547 | PyObject *f; |
| 1548 | PyObject *arg; |
| 1549 | } xmlParserCtxtPyCtxt; |
| 1550 | typedef xmlParserCtxtPyCtxt *xmlParserCtxtPyCtxtPtr; |
| 1551 | |
| 1552 | static void |
| 1553 | libxml_xmlParserCtxtGenericErrorFuncHandler(void *ctx, int severity, char *str) |
| 1554 | { |
Daniel Veillard | e6227e0 | 2003-01-14 11:42:39 +0000 | [diff] [blame] | 1555 | PyObject *list; |
Daniel Veillard | e6227e0 | 2003-01-14 11:42:39 +0000 | [diff] [blame] | 1556 | PyObject *result; |
Daniel Veillard | 417be3a | 2003-01-20 21:26:34 +0000 | [diff] [blame] | 1557 | xmlParserCtxtPtr ctxt; |
Daniel Veillard | e6227e0 | 2003-01-14 11:42:39 +0000 | [diff] [blame] | 1558 | xmlParserCtxtPyCtxtPtr pyCtxt; |
| 1559 | |
| 1560 | #ifdef DEBUG_ERROR |
Daniel Veillard | 850ce9b | 2004-11-10 11:55:47 +0000 | [diff] [blame] | 1561 | printf("libxml_xmlParserCtxtGenericErrorFuncHandler(%p, %s, ...) called\n", ctx, str); |
Daniel Veillard | e6227e0 | 2003-01-14 11:42:39 +0000 | [diff] [blame] | 1562 | #endif |
| 1563 | |
Daniel Veillard | 417be3a | 2003-01-20 21:26:34 +0000 | [diff] [blame] | 1564 | ctxt = (xmlParserCtxtPtr)ctx; |
| 1565 | pyCtxt = (xmlParserCtxtPyCtxtPtr)ctxt->_private; |
Daniel Veillard | e6227e0 | 2003-01-14 11:42:39 +0000 | [diff] [blame] | 1566 | |
Daniel Veillard | 417be3a | 2003-01-20 21:26:34 +0000 | [diff] [blame] | 1567 | list = PyTuple_New(4); |
| 1568 | PyTuple_SetItem(list, 0, pyCtxt->arg); |
| 1569 | Py_XINCREF(pyCtxt->arg); |
| 1570 | PyTuple_SetItem(list, 1, libxml_charPtrWrap(str)); |
| 1571 | PyTuple_SetItem(list, 2, libxml_intWrap(severity)); |
| 1572 | PyTuple_SetItem(list, 3, Py_None); |
| 1573 | Py_INCREF(Py_None); |
| 1574 | result = PyEval_CallObject(pyCtxt->f, list); |
| 1575 | if (result == NULL) |
| 1576 | { |
| 1577 | /* TODO: manage for the exception to be propagated... */ |
| 1578 | PyErr_Print(); |
Daniel Veillard | e6227e0 | 2003-01-14 11:42:39 +0000 | [diff] [blame] | 1579 | } |
Daniel Veillard | 417be3a | 2003-01-20 21:26:34 +0000 | [diff] [blame] | 1580 | Py_XDECREF(list); |
| 1581 | Py_XDECREF(result); |
| 1582 | } |
| 1583 | |
| 1584 | static void |
| 1585 | libxml_xmlParserCtxtErrorFuncHandler(void *ctx, const char *msg, ...) |
| 1586 | { |
| 1587 | va_list ap; |
| 1588 | |
| 1589 | va_start(ap, msg); |
| 1590 | libxml_xmlParserCtxtGenericErrorFuncHandler(ctx,XML_PARSER_SEVERITY_ERROR,libxml_buildMessage(msg,ap)); |
| 1591 | va_end(ap); |
| 1592 | } |
| 1593 | |
| 1594 | static void |
| 1595 | libxml_xmlParserCtxtWarningFuncHandler(void *ctx, const char *msg, ...) |
| 1596 | { |
| 1597 | va_list ap; |
| 1598 | |
| 1599 | va_start(ap, msg); |
| 1600 | libxml_xmlParserCtxtGenericErrorFuncHandler(ctx,XML_PARSER_SEVERITY_WARNING,libxml_buildMessage(msg,ap)); |
| 1601 | va_end(ap); |
| 1602 | } |
| 1603 | |
| 1604 | static void |
| 1605 | libxml_xmlParserCtxtValidityErrorFuncHandler(void *ctx, const char *msg, ...) |
| 1606 | { |
| 1607 | va_list ap; |
| 1608 | |
| 1609 | va_start(ap, msg); |
| 1610 | libxml_xmlParserCtxtGenericErrorFuncHandler(ctx,XML_PARSER_SEVERITY_VALIDITY_ERROR,libxml_buildMessage(msg,ap)); |
| 1611 | va_end(ap); |
| 1612 | } |
| 1613 | |
| 1614 | static void |
| 1615 | libxml_xmlParserCtxtValidityWarningFuncHandler(void *ctx, const char *msg, ...) |
| 1616 | { |
| 1617 | va_list ap; |
| 1618 | |
| 1619 | va_start(ap, msg); |
| 1620 | libxml_xmlParserCtxtGenericErrorFuncHandler(ctx,XML_PARSER_SEVERITY_VALIDITY_WARNING,libxml_buildMessage(msg,ap)); |
| 1621 | va_end(ap); |
Daniel Veillard | e6227e0 | 2003-01-14 11:42:39 +0000 | [diff] [blame] | 1622 | } |
| 1623 | |
Daniel Veillard | c266464 | 2003-07-29 20:44:53 +0000 | [diff] [blame] | 1624 | static PyObject * |
Daniel Veillard | 417be3a | 2003-01-20 21:26:34 +0000 | [diff] [blame] | 1625 | libxml_xmlParserCtxtSetErrorHandler(ATTRIBUTE_UNUSED PyObject *self, PyObject *args) |
Daniel Veillard | e6227e0 | 2003-01-14 11:42:39 +0000 | [diff] [blame] | 1626 | { |
| 1627 | PyObject *py_retval; |
| 1628 | xmlParserCtxtPtr ctxt; |
Daniel Veillard | e4a07e7 | 2003-01-14 14:40:25 +0000 | [diff] [blame] | 1629 | xmlParserCtxtPyCtxtPtr pyCtxt; |
Daniel Veillard | e6227e0 | 2003-01-14 11:42:39 +0000 | [diff] [blame] | 1630 | PyObject *pyobj_ctxt; |
| 1631 | PyObject *pyobj_f; |
| 1632 | PyObject *pyobj_arg; |
| 1633 | |
Daniel Veillard | 417be3a | 2003-01-20 21:26:34 +0000 | [diff] [blame] | 1634 | if (!PyArg_ParseTuple(args, (char *)"OOO:xmlParserCtxtSetErrorHandler", |
Daniel Veillard | e6227e0 | 2003-01-14 11:42:39 +0000 | [diff] [blame] | 1635 | &pyobj_ctxt, &pyobj_f, &pyobj_arg)) |
| 1636 | return(NULL); |
| 1637 | ctxt = (xmlParserCtxtPtr) PyparserCtxt_Get(pyobj_ctxt); |
| 1638 | if (ctxt->_private == NULL) { |
Daniel Veillard | e6227e0 | 2003-01-14 11:42:39 +0000 | [diff] [blame] | 1639 | pyCtxt = xmlMalloc(sizeof(xmlParserCtxtPyCtxt)); |
| 1640 | if (pyCtxt == NULL) { |
| 1641 | py_retval = libxml_intWrap(-1); |
| 1642 | return(py_retval); |
| 1643 | } |
| 1644 | memset(pyCtxt,0,sizeof(xmlParserCtxtPyCtxt)); |
| 1645 | ctxt->_private = pyCtxt; |
| 1646 | } |
Daniel Veillard | e4a07e7 | 2003-01-14 14:40:25 +0000 | [diff] [blame] | 1647 | else { |
Daniel Veillard | 417be3a | 2003-01-20 21:26:34 +0000 | [diff] [blame] | 1648 | pyCtxt = (xmlParserCtxtPyCtxtPtr)ctxt->_private; |
Daniel Veillard | e4a07e7 | 2003-01-14 14:40:25 +0000 | [diff] [blame] | 1649 | } |
Daniel Veillard | e6227e0 | 2003-01-14 11:42:39 +0000 | [diff] [blame] | 1650 | /* TODO: check f is a function ! */ |
Daniel Veillard | 417be3a | 2003-01-20 21:26:34 +0000 | [diff] [blame] | 1651 | Py_XDECREF(pyCtxt->f); |
Daniel Veillard | e6227e0 | 2003-01-14 11:42:39 +0000 | [diff] [blame] | 1652 | Py_XINCREF(pyobj_f); |
Daniel Veillard | 417be3a | 2003-01-20 21:26:34 +0000 | [diff] [blame] | 1653 | pyCtxt->f = pyobj_f; |
| 1654 | Py_XDECREF(pyCtxt->arg); |
Daniel Veillard | e6227e0 | 2003-01-14 11:42:39 +0000 | [diff] [blame] | 1655 | Py_XINCREF(pyobj_arg); |
Daniel Veillard | 417be3a | 2003-01-20 21:26:34 +0000 | [diff] [blame] | 1656 | pyCtxt->arg = pyobj_arg; |
Daniel Veillard | e6227e0 | 2003-01-14 11:42:39 +0000 | [diff] [blame] | 1657 | |
Daniel Veillard | 417be3a | 2003-01-20 21:26:34 +0000 | [diff] [blame] | 1658 | if (pyobj_f != Py_None) { |
| 1659 | ctxt->sax->error = libxml_xmlParserCtxtErrorFuncHandler; |
| 1660 | ctxt->sax->warning = libxml_xmlParserCtxtWarningFuncHandler; |
| 1661 | ctxt->vctxt.error = libxml_xmlParserCtxtValidityErrorFuncHandler; |
| 1662 | ctxt->vctxt.warning = libxml_xmlParserCtxtValidityWarningFuncHandler; |
| 1663 | } |
| 1664 | else { |
| 1665 | ctxt->sax->error = xmlParserError; |
| 1666 | ctxt->vctxt.error = xmlParserValidityError; |
| 1667 | ctxt->sax->warning = xmlParserWarning; |
| 1668 | ctxt->vctxt.warning = xmlParserValidityWarning; |
| 1669 | } |
Daniel Veillard | e6227e0 | 2003-01-14 11:42:39 +0000 | [diff] [blame] | 1670 | |
| 1671 | py_retval = libxml_intWrap(1); |
| 1672 | return(py_retval); |
| 1673 | } |
| 1674 | |
Daniel Veillard | c266464 | 2003-07-29 20:44:53 +0000 | [diff] [blame] | 1675 | static PyObject * |
Daniel Veillard | 417be3a | 2003-01-20 21:26:34 +0000 | [diff] [blame] | 1676 | libxml_xmlParserCtxtGetErrorHandler(ATTRIBUTE_UNUSED PyObject *self, PyObject *args) |
Daniel Veillard | e6227e0 | 2003-01-14 11:42:39 +0000 | [diff] [blame] | 1677 | { |
| 1678 | PyObject *py_retval; |
| 1679 | xmlParserCtxtPtr ctxt; |
Daniel Veillard | e4a07e7 | 2003-01-14 14:40:25 +0000 | [diff] [blame] | 1680 | xmlParserCtxtPyCtxtPtr pyCtxt; |
Daniel Veillard | e6227e0 | 2003-01-14 11:42:39 +0000 | [diff] [blame] | 1681 | PyObject *pyobj_ctxt; |
Daniel Veillard | e6227e0 | 2003-01-14 11:42:39 +0000 | [diff] [blame] | 1682 | |
Daniel Veillard | 417be3a | 2003-01-20 21:26:34 +0000 | [diff] [blame] | 1683 | if (!PyArg_ParseTuple(args, (char *)"O:xmlParserCtxtGetErrorHandler", |
| 1684 | &pyobj_ctxt)) |
Daniel Veillard | e6227e0 | 2003-01-14 11:42:39 +0000 | [diff] [blame] | 1685 | return(NULL); |
| 1686 | ctxt = (xmlParserCtxtPtr) PyparserCtxt_Get(pyobj_ctxt); |
Daniel Veillard | 417be3a | 2003-01-20 21:26:34 +0000 | [diff] [blame] | 1687 | py_retval = PyTuple_New(2); |
| 1688 | if (ctxt->_private != NULL) { |
| 1689 | pyCtxt = (xmlParserCtxtPyCtxtPtr)ctxt->_private; |
| 1690 | |
| 1691 | PyTuple_SetItem(py_retval, 0, pyCtxt->f); |
| 1692 | Py_XINCREF(pyCtxt->f); |
| 1693 | PyTuple_SetItem(py_retval, 1, pyCtxt->arg); |
| 1694 | Py_XINCREF(pyCtxt->arg); |
Daniel Veillard | e4a07e7 | 2003-01-14 14:40:25 +0000 | [diff] [blame] | 1695 | } |
| 1696 | else { |
Daniel Veillard | 417be3a | 2003-01-20 21:26:34 +0000 | [diff] [blame] | 1697 | /* no python error handler registered */ |
| 1698 | PyTuple_SetItem(py_retval, 0, Py_None); |
| 1699 | Py_XINCREF(Py_None); |
| 1700 | PyTuple_SetItem(py_retval, 1, Py_None); |
| 1701 | Py_XINCREF(Py_None); |
Daniel Veillard | e4a07e7 | 2003-01-14 14:40:25 +0000 | [diff] [blame] | 1702 | } |
Daniel Veillard | e6227e0 | 2003-01-14 11:42:39 +0000 | [diff] [blame] | 1703 | return(py_retval); |
| 1704 | } |
| 1705 | |
Daniel Veillard | c266464 | 2003-07-29 20:44:53 +0000 | [diff] [blame] | 1706 | static PyObject * |
Daniel Veillard | 417be3a | 2003-01-20 21:26:34 +0000 | [diff] [blame] | 1707 | libxml_xmlFreeParserCtxt(ATTRIBUTE_UNUSED PyObject *self, PyObject *args) { |
| 1708 | xmlParserCtxtPtr ctxt; |
| 1709 | PyObject *pyobj_ctxt; |
| 1710 | xmlParserCtxtPyCtxtPtr pyCtxt; |
| 1711 | |
| 1712 | if (!PyArg_ParseTuple(args, (char *)"O:xmlFreeParserCtxt", &pyobj_ctxt)) |
| 1713 | return(NULL); |
| 1714 | ctxt = (xmlParserCtxtPtr) PyparserCtxt_Get(pyobj_ctxt); |
| 1715 | |
| 1716 | if (ctxt != NULL) { |
| 1717 | pyCtxt = (xmlParserCtxtPyCtxtPtr)((xmlParserCtxtPtr)ctxt)->_private; |
| 1718 | if (pyCtxt) { |
| 1719 | Py_XDECREF(pyCtxt->f); |
| 1720 | Py_XDECREF(pyCtxt->arg); |
| 1721 | xmlFree(pyCtxt); |
| 1722 | } |
| 1723 | xmlFreeParserCtxt(ctxt); |
| 1724 | } |
| 1725 | |
| 1726 | Py_INCREF(Py_None); |
| 1727 | return(Py_None); |
| 1728 | } |
| 1729 | |
Daniel Veillard | 850ce9b | 2004-11-10 11:55:47 +0000 | [diff] [blame] | 1730 | /*** |
| 1731 | * xmlValidCtxt stuff |
| 1732 | */ |
| 1733 | |
| 1734 | typedef struct |
| 1735 | { |
| 1736 | PyObject *warn; |
| 1737 | PyObject *error; |
| 1738 | PyObject *arg; |
| 1739 | } xmlValidCtxtPyCtxt; |
| 1740 | typedef xmlValidCtxtPyCtxt *xmlValidCtxtPyCtxtPtr; |
| 1741 | |
| 1742 | static void |
| 1743 | libxml_xmlValidCtxtGenericErrorFuncHandler(void *ctx, int severity, char *str) |
| 1744 | { |
| 1745 | PyObject *list; |
| 1746 | PyObject *result; |
| 1747 | xmlValidCtxtPyCtxtPtr pyCtxt; |
| 1748 | |
| 1749 | #ifdef DEBUG_ERROR |
| 1750 | printf("libxml_xmlValidCtxtGenericErrorFuncHandler(%p, %d, %s, ...) called\n", ctx, severity, str); |
| 1751 | #endif |
| 1752 | |
| 1753 | pyCtxt = (xmlValidCtxtPyCtxtPtr)ctx; |
| 1754 | |
| 1755 | list = PyTuple_New(2); |
| 1756 | PyTuple_SetItem(list, 0, libxml_charPtrWrap(str)); |
| 1757 | PyTuple_SetItem(list, 1, pyCtxt->arg); |
| 1758 | Py_XINCREF(pyCtxt->arg); |
| 1759 | result = PyEval_CallObject(pyCtxt->error, list); |
| 1760 | if (result == NULL) |
| 1761 | { |
| 1762 | /* TODO: manage for the exception to be propagated... */ |
| 1763 | PyErr_Print(); |
| 1764 | } |
| 1765 | Py_XDECREF(list); |
| 1766 | Py_XDECREF(result); |
| 1767 | } |
| 1768 | |
| 1769 | static void |
| 1770 | libxml_xmlValidCtxtGenericWarningFuncHandler(void *ctx, int severity, char *str) |
| 1771 | { |
| 1772 | PyObject *list; |
| 1773 | PyObject *result; |
| 1774 | xmlValidCtxtPyCtxtPtr pyCtxt; |
| 1775 | |
| 1776 | #ifdef DEBUG_ERROR |
| 1777 | printf("libxml_xmlValidCtxtGenericWarningFuncHandler(%p, %d, %s, ...) called\n", ctx, severity, str); |
| 1778 | #endif |
| 1779 | |
| 1780 | pyCtxt = (xmlValidCtxtPyCtxtPtr)ctx; |
| 1781 | |
| 1782 | list = PyTuple_New(2); |
| 1783 | PyTuple_SetItem(list, 0, libxml_charPtrWrap(str)); |
| 1784 | PyTuple_SetItem(list, 1, pyCtxt->arg); |
| 1785 | Py_XINCREF(pyCtxt->arg); |
| 1786 | result = PyEval_CallObject(pyCtxt->warn, list); |
| 1787 | if (result == NULL) |
| 1788 | { |
| 1789 | /* TODO: manage for the exception to be propagated... */ |
| 1790 | PyErr_Print(); |
| 1791 | } |
| 1792 | Py_XDECREF(list); |
| 1793 | Py_XDECREF(result); |
| 1794 | } |
| 1795 | |
| 1796 | static void |
| 1797 | libxml_xmlValidCtxtErrorFuncHandler(void *ctx, const char *msg, ...) |
| 1798 | { |
| 1799 | va_list ap; |
| 1800 | |
| 1801 | va_start(ap, msg); |
| 1802 | libxml_xmlValidCtxtGenericErrorFuncHandler(ctx,XML_PARSER_SEVERITY_VALIDITY_ERROR,libxml_buildMessage(msg,ap)); |
| 1803 | va_end(ap); |
| 1804 | } |
| 1805 | |
| 1806 | static void |
| 1807 | libxml_xmlValidCtxtWarningFuncHandler(void *ctx, const char *msg, ...) |
| 1808 | { |
| 1809 | va_list ap; |
| 1810 | |
| 1811 | va_start(ap, msg); |
| 1812 | libxml_xmlValidCtxtGenericWarningFuncHandler(ctx,XML_PARSER_SEVERITY_VALIDITY_WARNING,libxml_buildMessage(msg,ap)); |
| 1813 | va_end(ap); |
| 1814 | } |
| 1815 | |
| 1816 | static PyObject * |
| 1817 | libxml_xmlSetValidErrors(ATTRIBUTE_UNUSED PyObject * self, PyObject * args) |
| 1818 | { |
| 1819 | PyObject *py_retval; |
| 1820 | PyObject *pyobj_error; |
| 1821 | PyObject *pyobj_warn; |
| 1822 | PyObject *pyobj_ctx; |
| 1823 | PyObject *pyobj_arg = Py_None; |
| 1824 | xmlValidCtxtPtr ctxt; |
| 1825 | xmlValidCtxtPyCtxtPtr pyCtxt; |
| 1826 | |
| 1827 | if (!PyArg_ParseTuple |
| 1828 | (args, (char *) "OOO|O:xmlSetValidErrors", &pyobj_ctx, &pyobj_error, &pyobj_warn, &pyobj_arg)) |
| 1829 | return (NULL); |
| 1830 | |
| 1831 | #ifdef DEBUG_ERROR |
| 1832 | printf("libxml_xmlSetValidErrors(%p, %p, %p) called\n", pyobj_ctx, pyobj_error, pyobj_warn); |
| 1833 | #endif |
| 1834 | |
| 1835 | ctxt = PyValidCtxt_Get(pyobj_ctx); |
| 1836 | pyCtxt = xmlMalloc(sizeof(xmlValidCtxtPyCtxt)); |
| 1837 | if (pyCtxt == NULL) { |
| 1838 | py_retval = libxml_intWrap(-1); |
| 1839 | return(py_retval); |
| 1840 | } |
| 1841 | memset(pyCtxt, 0, sizeof(xmlValidCtxtPyCtxt)); |
| 1842 | |
| 1843 | |
| 1844 | /* TODO: check warn and error is a function ! */ |
| 1845 | Py_XDECREF(pyCtxt->error); |
| 1846 | Py_XINCREF(pyobj_error); |
| 1847 | pyCtxt->error = pyobj_error; |
| 1848 | |
| 1849 | Py_XDECREF(pyCtxt->warn); |
| 1850 | Py_XINCREF(pyobj_warn); |
| 1851 | pyCtxt->warn = pyobj_warn; |
| 1852 | |
| 1853 | Py_XDECREF(pyCtxt->arg); |
| 1854 | Py_XINCREF(pyobj_arg); |
| 1855 | pyCtxt->arg = pyobj_arg; |
| 1856 | |
| 1857 | ctxt->error = libxml_xmlValidCtxtErrorFuncHandler; |
| 1858 | ctxt->warning = libxml_xmlValidCtxtWarningFuncHandler; |
| 1859 | ctxt->userData = pyCtxt; |
| 1860 | |
| 1861 | py_retval = libxml_intWrap(1); |
| 1862 | return (py_retval); |
| 1863 | } |
| 1864 | |
Daniel Veillard | 25c90c5 | 2005-03-02 10:47:41 +0000 | [diff] [blame] | 1865 | |
Daniel Veillard | bb8502c | 2005-03-30 07:40:35 +0000 | [diff] [blame] | 1866 | static PyObject * |
Daniel Veillard | 25c90c5 | 2005-03-02 10:47:41 +0000 | [diff] [blame] | 1867 | libxml_xmlFreeValidCtxt(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) { |
| 1868 | xmlValidCtxtPtr cur; |
| 1869 | xmlValidCtxtPyCtxtPtr pyCtxt; |
| 1870 | PyObject *pyobj_cur; |
| 1871 | |
| 1872 | if (!PyArg_ParseTuple(args, (char *)"O:xmlFreeValidCtxt", &pyobj_cur)) |
| 1873 | return(NULL); |
| 1874 | cur = (xmlValidCtxtPtr) PyValidCtxt_Get(pyobj_cur); |
| 1875 | |
| 1876 | pyCtxt = (xmlValidCtxtPyCtxtPtr)(cur->userData); |
| 1877 | if (pyCtxt != NULL) |
| 1878 | { |
| 1879 | Py_XDECREF(pyCtxt->error); |
| 1880 | Py_XDECREF(pyCtxt->warn); |
| 1881 | Py_XDECREF(pyCtxt->arg); |
| 1882 | xmlFree(pyCtxt); |
| 1883 | } |
| 1884 | |
| 1885 | xmlFreeValidCtxt(cur); |
| 1886 | Py_INCREF(Py_None); |
| 1887 | return(Py_None); |
| 1888 | } |
| 1889 | |
Daniel Veillard | a7340c8 | 2002-02-01 17:56:45 +0000 | [diff] [blame] | 1890 | /************************************************************************ |
| 1891 | * * |
Daniel Veillard | 26f7026 | 2003-01-16 22:45:08 +0000 | [diff] [blame] | 1892 | * Per xmlTextReader error handler * |
| 1893 | * * |
| 1894 | ************************************************************************/ |
| 1895 | |
| 1896 | typedef struct |
| 1897 | { |
| 1898 | PyObject *f; |
| 1899 | PyObject *arg; |
| 1900 | } xmlTextReaderPyCtxt; |
| 1901 | typedef xmlTextReaderPyCtxt *xmlTextReaderPyCtxtPtr; |
| 1902 | |
| 1903 | static void |
| 1904 | libxml_xmlTextReaderErrorCallback(void *arg, |
| 1905 | const char *msg, |
Daniel Veillard | 417be3a | 2003-01-20 21:26:34 +0000 | [diff] [blame] | 1906 | int severity, |
| 1907 | xmlTextReaderLocatorPtr locator) |
Daniel Veillard | 26f7026 | 2003-01-16 22:45:08 +0000 | [diff] [blame] | 1908 | { |
| 1909 | xmlTextReaderPyCtxt *pyCtxt = (xmlTextReaderPyCtxt *)arg; |
| 1910 | PyObject *list; |
| 1911 | PyObject *result; |
| 1912 | |
Daniel Veillard | 417be3a | 2003-01-20 21:26:34 +0000 | [diff] [blame] | 1913 | list = PyTuple_New(4); |
Daniel Veillard | 26f7026 | 2003-01-16 22:45:08 +0000 | [diff] [blame] | 1914 | PyTuple_SetItem(list, 0, pyCtxt->arg); |
| 1915 | Py_XINCREF(pyCtxt->arg); |
| 1916 | PyTuple_SetItem(list, 1, libxml_charPtrConstWrap(msg)); |
Daniel Veillard | 417be3a | 2003-01-20 21:26:34 +0000 | [diff] [blame] | 1917 | PyTuple_SetItem(list, 2, libxml_intWrap(severity)); |
| 1918 | PyTuple_SetItem(list, 3, libxml_xmlTextReaderLocatorPtrWrap(locator)); |
Daniel Veillard | 26f7026 | 2003-01-16 22:45:08 +0000 | [diff] [blame] | 1919 | result = PyEval_CallObject(pyCtxt->f, list); |
| 1920 | if (result == NULL) |
| 1921 | { |
Daniel Veillard | 417be3a | 2003-01-20 21:26:34 +0000 | [diff] [blame] | 1922 | /* TODO: manage for the exception to be propagated... */ |
Daniel Veillard | 26f7026 | 2003-01-16 22:45:08 +0000 | [diff] [blame] | 1923 | PyErr_Print(); |
| 1924 | } |
| 1925 | Py_XDECREF(list); |
| 1926 | Py_XDECREF(result); |
| 1927 | } |
| 1928 | |
Daniel Veillard | c266464 | 2003-07-29 20:44:53 +0000 | [diff] [blame] | 1929 | static PyObject * |
Daniel Veillard | 26f7026 | 2003-01-16 22:45:08 +0000 | [diff] [blame] | 1930 | libxml_xmlTextReaderSetErrorHandler(ATTRIBUTE_UNUSED PyObject *self, PyObject *args) |
| 1931 | { |
| 1932 | xmlTextReaderPtr reader; |
| 1933 | xmlTextReaderPyCtxtPtr pyCtxt; |
| 1934 | xmlTextReaderErrorFunc f; |
| 1935 | void *arg; |
| 1936 | PyObject *pyobj_reader; |
| 1937 | PyObject *pyobj_f; |
| 1938 | PyObject *pyobj_arg; |
| 1939 | PyObject *py_retval; |
| 1940 | |
| 1941 | if (!PyArg_ParseTuple(args, (char *)"OOO:xmlTextReaderSetErrorHandler", &pyobj_reader, &pyobj_f, &pyobj_arg)) |
| 1942 | return(NULL); |
| 1943 | reader = (xmlTextReaderPtr) PyxmlTextReader_Get(pyobj_reader); |
| 1944 | /* clear previous error handler */ |
| 1945 | xmlTextReaderGetErrorHandler(reader,&f,&arg); |
| 1946 | if (arg != NULL) { |
Daniel Veillard | c266464 | 2003-07-29 20:44:53 +0000 | [diff] [blame] | 1947 | if (f == (xmlTextReaderErrorFunc) libxml_xmlTextReaderErrorCallback) { |
Daniel Veillard | 26f7026 | 2003-01-16 22:45:08 +0000 | [diff] [blame] | 1948 | /* ok, it's our error handler! */ |
| 1949 | pyCtxt = (xmlTextReaderPyCtxtPtr)arg; |
| 1950 | Py_XDECREF(pyCtxt->f); |
| 1951 | Py_XDECREF(pyCtxt->arg); |
| 1952 | xmlFree(pyCtxt); |
| 1953 | } |
| 1954 | else { |
| 1955 | /* |
| 1956 | * there already an arg, and it's not ours, |
| 1957 | * there is definitely something wrong going on here... |
| 1958 | * we don't know how to free it, so we bail out... |
| 1959 | */ |
| 1960 | py_retval = libxml_intWrap(-1); |
| 1961 | return(py_retval); |
| 1962 | } |
| 1963 | } |
| 1964 | xmlTextReaderSetErrorHandler(reader,NULL,NULL); |
| 1965 | /* set new error handler */ |
| 1966 | if (pyobj_f != Py_None) |
| 1967 | { |
| 1968 | pyCtxt = (xmlTextReaderPyCtxtPtr)xmlMalloc(sizeof(xmlTextReaderPyCtxt)); |
| 1969 | if (pyCtxt == NULL) { |
| 1970 | py_retval = libxml_intWrap(-1); |
| 1971 | return(py_retval); |
| 1972 | } |
| 1973 | Py_XINCREF(pyobj_f); |
| 1974 | pyCtxt->f = pyobj_f; |
| 1975 | Py_XINCREF(pyobj_arg); |
| 1976 | pyCtxt->arg = pyobj_arg; |
Daniel Veillard | c266464 | 2003-07-29 20:44:53 +0000 | [diff] [blame] | 1977 | xmlTextReaderSetErrorHandler(reader, |
| 1978 | (xmlTextReaderErrorFunc) libxml_xmlTextReaderErrorCallback, |
| 1979 | pyCtxt); |
Daniel Veillard | 26f7026 | 2003-01-16 22:45:08 +0000 | [diff] [blame] | 1980 | } |
| 1981 | |
| 1982 | py_retval = libxml_intWrap(1); |
| 1983 | return(py_retval); |
| 1984 | } |
| 1985 | |
Daniel Veillard | c266464 | 2003-07-29 20:44:53 +0000 | [diff] [blame] | 1986 | static PyObject * |
Daniel Veillard | 26f7026 | 2003-01-16 22:45:08 +0000 | [diff] [blame] | 1987 | libxml_xmlTextReaderGetErrorHandler(ATTRIBUTE_UNUSED PyObject *self, PyObject *args) |
| 1988 | { |
| 1989 | xmlTextReaderPtr reader; |
| 1990 | xmlTextReaderPyCtxtPtr pyCtxt; |
| 1991 | xmlTextReaderErrorFunc f; |
| 1992 | void *arg; |
| 1993 | PyObject *pyobj_reader; |
| 1994 | PyObject *py_retval; |
| 1995 | |
| 1996 | if (!PyArg_ParseTuple(args, (char *)"O:xmlTextReaderSetErrorHandler", &pyobj_reader)) |
| 1997 | return(NULL); |
| 1998 | reader = (xmlTextReaderPtr) PyxmlTextReader_Get(pyobj_reader); |
| 1999 | xmlTextReaderGetErrorHandler(reader,&f,&arg); |
| 2000 | py_retval = PyTuple_New(2); |
Daniel Veillard | c266464 | 2003-07-29 20:44:53 +0000 | [diff] [blame] | 2001 | if (f == (xmlTextReaderErrorFunc)libxml_xmlTextReaderErrorCallback) { |
Daniel Veillard | 26f7026 | 2003-01-16 22:45:08 +0000 | [diff] [blame] | 2002 | /* ok, it's our error handler! */ |
| 2003 | pyCtxt = (xmlTextReaderPyCtxtPtr)arg; |
| 2004 | PyTuple_SetItem(py_retval, 0, pyCtxt->f); |
| 2005 | Py_XINCREF(pyCtxt->f); |
| 2006 | PyTuple_SetItem(py_retval, 1, pyCtxt->arg); |
| 2007 | Py_XINCREF(pyCtxt->arg); |
| 2008 | } |
| 2009 | else |
| 2010 | { |
| 2011 | /* f is null or it's not our error handler */ |
| 2012 | PyTuple_SetItem(py_retval, 0, Py_None); |
| 2013 | Py_XINCREF(Py_None); |
| 2014 | PyTuple_SetItem(py_retval, 1, Py_None); |
| 2015 | Py_XINCREF(Py_None); |
| 2016 | } |
| 2017 | return(py_retval); |
| 2018 | } |
| 2019 | |
Daniel Veillard | c266464 | 2003-07-29 20:44:53 +0000 | [diff] [blame] | 2020 | static PyObject * |
Daniel Veillard | 26f7026 | 2003-01-16 22:45:08 +0000 | [diff] [blame] | 2021 | libxml_xmlFreeTextReader(ATTRIBUTE_UNUSED PyObject *self, PyObject *args) { |
| 2022 | xmlTextReaderPtr reader; |
| 2023 | PyObject *pyobj_reader; |
| 2024 | xmlTextReaderPyCtxtPtr pyCtxt; |
| 2025 | xmlTextReaderErrorFunc f; |
| 2026 | void *arg; |
| 2027 | |
Daniel Veillard | 157fee0 | 2003-10-31 10:36:03 +0000 | [diff] [blame] | 2028 | if (!PyArg_ParseTuple(args, (char *)"O:xmlFreeTextReader", &pyobj_reader)) |
| 2029 | return(NULL); |
| 2030 | if (!PyCObject_Check(pyobj_reader)) { |
Daniel Veillard | bb3ba32 | 2003-10-30 13:12:43 +0000 | [diff] [blame] | 2031 | Py_INCREF(Py_None); |
| 2032 | return(Py_None); |
| 2033 | } |
Daniel Veillard | 26f7026 | 2003-01-16 22:45:08 +0000 | [diff] [blame] | 2034 | reader = (xmlTextReaderPtr) PyxmlTextReader_Get(pyobj_reader); |
Daniel Veillard | bb3ba32 | 2003-10-30 13:12:43 +0000 | [diff] [blame] | 2035 | if (reader == NULL) { |
| 2036 | Py_INCREF(Py_None); |
| 2037 | return(Py_None); |
| 2038 | } |
Daniel Veillard | 26f7026 | 2003-01-16 22:45:08 +0000 | [diff] [blame] | 2039 | |
| 2040 | xmlTextReaderGetErrorHandler(reader,&f,&arg); |
| 2041 | if (arg != NULL) { |
Daniel Veillard | c266464 | 2003-07-29 20:44:53 +0000 | [diff] [blame] | 2042 | if (f == (xmlTextReaderErrorFunc) libxml_xmlTextReaderErrorCallback) { |
Daniel Veillard | 26f7026 | 2003-01-16 22:45:08 +0000 | [diff] [blame] | 2043 | /* ok, it's our error handler! */ |
| 2044 | pyCtxt = (xmlTextReaderPyCtxtPtr)arg; |
| 2045 | Py_XDECREF(pyCtxt->f); |
| 2046 | Py_XDECREF(pyCtxt->arg); |
| 2047 | xmlFree(pyCtxt); |
| 2048 | } |
| 2049 | /* |
| 2050 | * else, something wrong happened, because the error handler is |
| 2051 | * not owned by the python bindings... |
| 2052 | */ |
| 2053 | } |
| 2054 | |
| 2055 | xmlFreeTextReader(reader); |
| 2056 | Py_INCREF(Py_None); |
| 2057 | return(Py_None); |
| 2058 | } |
| 2059 | |
| 2060 | /************************************************************************ |
| 2061 | * * |
Daniel Veillard | a7340c8 | 2002-02-01 17:56:45 +0000 | [diff] [blame] | 2062 | * XPath extensions * |
| 2063 | * * |
| 2064 | ************************************************************************/ |
| 2065 | |
Daniel Veillard | a7340c8 | 2002-02-01 17:56:45 +0000 | [diff] [blame] | 2066 | static void |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2067 | libxml_xmlXPathFuncCallback(xmlXPathParserContextPtr ctxt, int nargs) |
| 2068 | { |
Daniel Veillard | a7340c8 | 2002-02-01 17:56:45 +0000 | [diff] [blame] | 2069 | PyObject *list, *cur, *result; |
| 2070 | xmlXPathObjectPtr obj; |
Daniel Veillard | 70cab35 | 2002-02-06 16:06:58 +0000 | [diff] [blame] | 2071 | xmlXPathContextPtr rctxt; |
| 2072 | PyObject *current_function = NULL; |
| 2073 | const xmlChar *name; |
| 2074 | const xmlChar *ns_uri; |
Daniel Veillard | a7340c8 | 2002-02-01 17:56:45 +0000 | [diff] [blame] | 2075 | int i; |
| 2076 | |
Daniel Veillard | 70cab35 | 2002-02-06 16:06:58 +0000 | [diff] [blame] | 2077 | if (ctxt == NULL) |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2078 | return; |
Daniel Veillard | 70cab35 | 2002-02-06 16:06:58 +0000 | [diff] [blame] | 2079 | rctxt = ctxt->context; |
| 2080 | if (rctxt == NULL) |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2081 | return; |
Daniel Veillard | 70cab35 | 2002-02-06 16:06:58 +0000 | [diff] [blame] | 2082 | name = rctxt->function; |
| 2083 | ns_uri = rctxt->functionURI; |
Daniel Veillard | a7340c8 | 2002-02-01 17:56:45 +0000 | [diff] [blame] | 2084 | #ifdef DEBUG_XPATH |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2085 | printf("libxml_xmlXPathFuncCallback called name %s URI %s\n", name, |
| 2086 | ns_uri); |
Daniel Veillard | a7340c8 | 2002-02-01 17:56:45 +0000 | [diff] [blame] | 2087 | #endif |
| 2088 | |
Daniel Veillard | 70cab35 | 2002-02-06 16:06:58 +0000 | [diff] [blame] | 2089 | /* |
| 2090 | * Find the function, it should be there it was there at lookup |
| 2091 | */ |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2092 | for (i = 0; i < libxml_xpathCallbacksNb; i++) { |
| 2093 | if ( /* TODO (ctxt == libxml_xpathCallbacks[i].ctx) && */ |
William M. Brack | 8e2cc6f | 2004-07-03 23:28:52 +0000 | [diff] [blame] | 2094 | (xmlStrEqual(name, (*libxml_xpathCallbacks)[i].name)) && |
| 2095 | (xmlStrEqual(ns_uri, (*libxml_xpathCallbacks)[i].ns_uri))) { |
| 2096 | current_function = (*libxml_xpathCallbacks)[i].function; |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2097 | } |
Daniel Veillard | 70cab35 | 2002-02-06 16:06:58 +0000 | [diff] [blame] | 2098 | } |
| 2099 | if (current_function == NULL) { |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2100 | printf |
| 2101 | ("libxml_xmlXPathFuncCallback: internal error %s not found !\n", |
| 2102 | name); |
| 2103 | return; |
Daniel Veillard | 70cab35 | 2002-02-06 16:06:58 +0000 | [diff] [blame] | 2104 | } |
| 2105 | |
Daniel Veillard | c575b99 | 2002-02-08 13:28:40 +0000 | [diff] [blame] | 2106 | list = PyTuple_New(nargs + 1); |
| 2107 | PyTuple_SetItem(list, 0, libxml_xmlXPathParserContextPtrWrap(ctxt)); |
Daniel Veillard | 05349ab | 2004-01-25 20:01:35 +0000 | [diff] [blame] | 2108 | for (i = nargs - 1; i >= 0; i--) { |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2109 | obj = valuePop(ctxt); |
| 2110 | cur = libxml_xmlXPathObjectPtrWrap(obj); |
| 2111 | PyTuple_SetItem(list, i + 1, cur); |
Daniel Veillard | a7340c8 | 2002-02-01 17:56:45 +0000 | [diff] [blame] | 2112 | } |
| 2113 | result = PyEval_CallObject(current_function, list); |
| 2114 | Py_DECREF(list); |
| 2115 | |
| 2116 | obj = libxml_xmlXPathObjectPtrConvert(result); |
| 2117 | valuePush(ctxt, obj); |
| 2118 | } |
| 2119 | |
| 2120 | static xmlXPathFunction |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2121 | libxml_xmlXPathFuncLookupFunc(void *ctxt, const xmlChar * name, |
| 2122 | const xmlChar * ns_uri) |
| 2123 | { |
Daniel Veillard | a7340c8 | 2002-02-01 17:56:45 +0000 | [diff] [blame] | 2124 | int i; |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2125 | |
Daniel Veillard | a7340c8 | 2002-02-01 17:56:45 +0000 | [diff] [blame] | 2126 | #ifdef DEBUG_XPATH |
| 2127 | printf("libxml_xmlXPathFuncLookupFunc(%p, %s, %s) called\n", |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2128 | ctxt, name, ns_uri); |
Daniel Veillard | a7340c8 | 2002-02-01 17:56:45 +0000 | [diff] [blame] | 2129 | #endif |
Daniel Veillard | 70cab35 | 2002-02-06 16:06:58 +0000 | [diff] [blame] | 2130 | /* |
| 2131 | * This is called once only. The address is then stored in the |
| 2132 | * XPath expression evaluation, the proper object to call can |
| 2133 | * then still be found using the execution context function |
| 2134 | * and functionURI fields. |
| 2135 | */ |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2136 | for (i = 0; i < libxml_xpathCallbacksNb; i++) { |
William M. Brack | 8e2cc6f | 2004-07-03 23:28:52 +0000 | [diff] [blame] | 2137 | if ((ctxt == (*libxml_xpathCallbacks)[i].ctx) && |
| 2138 | (xmlStrEqual(name, (*libxml_xpathCallbacks)[i].name)) && |
| 2139 | (xmlStrEqual(ns_uri, (*libxml_xpathCallbacks)[i].ns_uri))) { |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2140 | return (libxml_xmlXPathFuncCallback); |
| 2141 | } |
Daniel Veillard | a7340c8 | 2002-02-01 17:56:45 +0000 | [diff] [blame] | 2142 | } |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2143 | return (NULL); |
Daniel Veillard | a7340c8 | 2002-02-01 17:56:45 +0000 | [diff] [blame] | 2144 | } |
| 2145 | |
| 2146 | static void |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2147 | libxml_xpathCallbacksInitialize(void) |
| 2148 | { |
Daniel Veillard | a7340c8 | 2002-02-01 17:56:45 +0000 | [diff] [blame] | 2149 | int i; |
| 2150 | |
| 2151 | if (libxml_xpathCallbacksInitialized != 0) |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2152 | return; |
Daniel Veillard | a7340c8 | 2002-02-01 17:56:45 +0000 | [diff] [blame] | 2153 | |
| 2154 | #ifdef DEBUG_XPATH |
| 2155 | printf("libxml_xpathCallbacksInitialized called\n"); |
| 2156 | #endif |
William M. Brack | 8e2cc6f | 2004-07-03 23:28:52 +0000 | [diff] [blame] | 2157 | libxml_xpathCallbacks = (libxml_xpathCallbackArray*)xmlMalloc( |
| 2158 | libxml_xpathCallbacksAllocd*sizeof(libxml_xpathCallback)); |
Daniel Veillard | a7340c8 | 2002-02-01 17:56:45 +0000 | [diff] [blame] | 2159 | |
William M. Brack | 8e2cc6f | 2004-07-03 23:28:52 +0000 | [diff] [blame] | 2160 | for (i = 0; i < libxml_xpathCallbacksAllocd; i++) { |
| 2161 | (*libxml_xpathCallbacks)[i].ctx = NULL; |
| 2162 | (*libxml_xpathCallbacks)[i].name = NULL; |
| 2163 | (*libxml_xpathCallbacks)[i].ns_uri = NULL; |
| 2164 | (*libxml_xpathCallbacks)[i].function = NULL; |
Daniel Veillard | a7340c8 | 2002-02-01 17:56:45 +0000 | [diff] [blame] | 2165 | } |
Daniel Veillard | a7340c8 | 2002-02-01 17:56:45 +0000 | [diff] [blame] | 2166 | libxml_xpathCallbacksInitialized = 1; |
| 2167 | } |
| 2168 | |
| 2169 | PyObject * |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2170 | libxml_xmlRegisterXPathFunction(ATTRIBUTE_UNUSED PyObject * self, |
| 2171 | PyObject * args) |
| 2172 | { |
Daniel Veillard | a7340c8 | 2002-02-01 17:56:45 +0000 | [diff] [blame] | 2173 | PyObject *py_retval; |
| 2174 | int c_retval = 0; |
| 2175 | xmlChar *name; |
| 2176 | xmlChar *ns_uri; |
| 2177 | xmlXPathContextPtr ctx; |
| 2178 | PyObject *pyobj_ctx; |
| 2179 | PyObject *pyobj_f; |
| 2180 | int i; |
| 2181 | |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2182 | if (!PyArg_ParseTuple |
| 2183 | (args, (char *) "OszO:registerXPathFunction", &pyobj_ctx, &name, |
| 2184 | &ns_uri, &pyobj_f)) |
| 2185 | return (NULL); |
Daniel Veillard | a7340c8 | 2002-02-01 17:56:45 +0000 | [diff] [blame] | 2186 | |
| 2187 | ctx = (xmlXPathContextPtr) PyxmlXPathContext_Get(pyobj_ctx); |
| 2188 | if (libxml_xpathCallbacksInitialized == 0) |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2189 | libxml_xpathCallbacksInitialize(); |
Daniel Veillard | a7340c8 | 2002-02-01 17:56:45 +0000 | [diff] [blame] | 2190 | xmlXPathRegisterFuncLookup(ctx, libxml_xmlXPathFuncLookupFunc, ctx); |
| 2191 | |
| 2192 | if ((pyobj_ctx == NULL) || (name == NULL) || (pyobj_f == NULL)) { |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2193 | py_retval = libxml_intWrap(-1); |
| 2194 | return (py_retval); |
Daniel Veillard | a7340c8 | 2002-02-01 17:56:45 +0000 | [diff] [blame] | 2195 | } |
Daniel Veillard | a7340c8 | 2002-02-01 17:56:45 +0000 | [diff] [blame] | 2196 | #ifdef DEBUG_XPATH |
| 2197 | printf("libxml_registerXPathFunction(%p, %s, %s) called\n", |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2198 | ctx, name, ns_uri); |
Daniel Veillard | a7340c8 | 2002-02-01 17:56:45 +0000 | [diff] [blame] | 2199 | #endif |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2200 | for (i = 0; i < libxml_xpathCallbacksNb; i++) { |
William M. Brack | 8e2cc6f | 2004-07-03 23:28:52 +0000 | [diff] [blame] | 2201 | if ((ctx == (*libxml_xpathCallbacks)[i].ctx) && |
| 2202 | (xmlStrEqual(name, (*libxml_xpathCallbacks)[i].name)) && |
| 2203 | (xmlStrEqual(ns_uri, (*libxml_xpathCallbacks)[i].ns_uri))) { |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2204 | Py_XINCREF(pyobj_f); |
William M. Brack | 8e2cc6f | 2004-07-03 23:28:52 +0000 | [diff] [blame] | 2205 | Py_XDECREF((*libxml_xpathCallbacks)[i].function); |
| 2206 | (*libxml_xpathCallbacks)[i].function = pyobj_f; |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2207 | c_retval = 1; |
| 2208 | goto done; |
| 2209 | } |
Daniel Veillard | a7340c8 | 2002-02-01 17:56:45 +0000 | [diff] [blame] | 2210 | } |
William M. Brack | 8e2cc6f | 2004-07-03 23:28:52 +0000 | [diff] [blame] | 2211 | if (libxml_xpathCallbacksNb >= libxml_xpathCallbacksAllocd) { |
| 2212 | libxml_xpathCallbacksAllocd+=10; |
| 2213 | libxml_xpathCallbacks = (libxml_xpathCallbackArray*)xmlRealloc( |
| 2214 | libxml_xpathCallbacks, |
| 2215 | libxml_xpathCallbacksAllocd*sizeof(libxml_xpathCallback)); |
| 2216 | } |
| 2217 | i = libxml_xpathCallbacksNb++; |
| 2218 | Py_XINCREF(pyobj_f); |
| 2219 | (*libxml_xpathCallbacks)[i].ctx = ctx; |
| 2220 | (*libxml_xpathCallbacks)[i].name = xmlStrdup(name); |
| 2221 | (*libxml_xpathCallbacks)[i].ns_uri = xmlStrdup(ns_uri); |
| 2222 | (*libxml_xpathCallbacks)[i].function = pyobj_f; |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2223 | c_retval = 1; |
William M. Brack | 8e2cc6f | 2004-07-03 23:28:52 +0000 | [diff] [blame] | 2224 | |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2225 | done: |
Daniel Veillard | a7340c8 | 2002-02-01 17:56:45 +0000 | [diff] [blame] | 2226 | py_retval = libxml_intWrap((int) c_retval); |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2227 | return (py_retval); |
Daniel Veillard | a7340c8 | 2002-02-01 17:56:45 +0000 | [diff] [blame] | 2228 | } |
| 2229 | |
Daniel Veillard | 1971ee2 | 2002-01-31 20:29:19 +0000 | [diff] [blame] | 2230 | /************************************************************************ |
| 2231 | * * |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 2232 | * Global properties access * |
| 2233 | * * |
| 2234 | ************************************************************************/ |
| 2235 | static PyObject * |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2236 | libxml_name(ATTRIBUTE_UNUSED PyObject * self, PyObject * args) |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 2237 | { |
| 2238 | PyObject *resultobj, *obj; |
| 2239 | xmlNodePtr cur; |
| 2240 | const xmlChar *res; |
| 2241 | |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2242 | if (!PyArg_ParseTuple(args, (char *) "O:name", &obj)) |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 2243 | return NULL; |
| 2244 | cur = PyxmlNode_Get(obj); |
| 2245 | |
| 2246 | #ifdef DEBUG |
| 2247 | printf("libxml_name: cur = %p type %d\n", cur, cur->type); |
| 2248 | #endif |
| 2249 | |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2250 | switch (cur->type) { |
| 2251 | case XML_DOCUMENT_NODE: |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 2252 | #ifdef LIBXML_DOCB_ENABLED |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2253 | case XML_DOCB_DOCUMENT_NODE: |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 2254 | #endif |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2255 | case XML_HTML_DOCUMENT_NODE:{ |
| 2256 | xmlDocPtr doc = (xmlDocPtr) cur; |
| 2257 | |
| 2258 | res = doc->URL; |
| 2259 | break; |
| 2260 | } |
| 2261 | case XML_ATTRIBUTE_NODE:{ |
| 2262 | xmlAttrPtr attr = (xmlAttrPtr) cur; |
| 2263 | |
| 2264 | res = attr->name; |
| 2265 | break; |
| 2266 | } |
| 2267 | case XML_NAMESPACE_DECL:{ |
| 2268 | xmlNsPtr ns = (xmlNsPtr) cur; |
| 2269 | |
| 2270 | res = ns->prefix; |
| 2271 | break; |
| 2272 | } |
| 2273 | default: |
| 2274 | res = cur->name; |
| 2275 | break; |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 2276 | } |
Daniel Veillard | 1971ee2 | 2002-01-31 20:29:19 +0000 | [diff] [blame] | 2277 | resultobj = libxml_constxmlCharPtrWrap(res); |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 2278 | |
| 2279 | return resultobj; |
| 2280 | } |
| 2281 | |
| 2282 | static PyObject * |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2283 | libxml_doc(ATTRIBUTE_UNUSED PyObject * self, PyObject * args) |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 2284 | { |
| 2285 | PyObject *resultobj, *obj; |
| 2286 | xmlNodePtr cur; |
| 2287 | xmlDocPtr res; |
| 2288 | |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2289 | if (!PyArg_ParseTuple(args, (char *) "O:doc", &obj)) |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 2290 | return NULL; |
| 2291 | cur = PyxmlNode_Get(obj); |
| 2292 | |
| 2293 | #ifdef DEBUG |
| 2294 | printf("libxml_doc: cur = %p\n", cur); |
| 2295 | #endif |
| 2296 | |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2297 | switch (cur->type) { |
| 2298 | case XML_DOCUMENT_NODE: |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 2299 | #ifdef LIBXML_DOCB_ENABLED |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2300 | case XML_DOCB_DOCUMENT_NODE: |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 2301 | #endif |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2302 | case XML_HTML_DOCUMENT_NODE: |
| 2303 | res = NULL; |
| 2304 | break; |
| 2305 | case XML_ATTRIBUTE_NODE:{ |
| 2306 | xmlAttrPtr attr = (xmlAttrPtr) cur; |
| 2307 | |
| 2308 | res = attr->doc; |
| 2309 | break; |
| 2310 | } |
| 2311 | case XML_NAMESPACE_DECL: |
| 2312 | res = NULL; |
| 2313 | break; |
| 2314 | default: |
| 2315 | res = cur->doc; |
| 2316 | break; |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 2317 | } |
| 2318 | resultobj = libxml_xmlDocPtrWrap(res); |
| 2319 | return resultobj; |
| 2320 | } |
| 2321 | |
| 2322 | static PyObject * |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2323 | libxml_properties(ATTRIBUTE_UNUSED PyObject * self, PyObject * args) |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 2324 | { |
| 2325 | PyObject *resultobj, *obj; |
| 2326 | xmlNodePtr cur = NULL; |
| 2327 | xmlAttrPtr res; |
| 2328 | |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2329 | if (!PyArg_ParseTuple(args, (char *) "O:properties", &obj)) |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 2330 | return NULL; |
| 2331 | cur = PyxmlNode_Get(obj); |
| 2332 | if (cur->type == XML_ELEMENT_NODE) |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2333 | res = cur->properties; |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 2334 | else |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2335 | res = NULL; |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 2336 | resultobj = libxml_xmlAttrPtrWrap(res); |
| 2337 | return resultobj; |
| 2338 | } |
| 2339 | |
| 2340 | static PyObject * |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2341 | libxml_next(ATTRIBUTE_UNUSED PyObject * self, PyObject * args) |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 2342 | { |
| 2343 | PyObject *resultobj, *obj; |
| 2344 | xmlNodePtr cur; |
| 2345 | xmlNodePtr res; |
| 2346 | |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2347 | if (!PyArg_ParseTuple(args, (char *) "O:next", &obj)) |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 2348 | return NULL; |
| 2349 | cur = PyxmlNode_Get(obj); |
| 2350 | |
| 2351 | #ifdef DEBUG |
| 2352 | printf("libxml_next: cur = %p\n", cur); |
| 2353 | #endif |
| 2354 | |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2355 | switch (cur->type) { |
| 2356 | case XML_DOCUMENT_NODE: |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 2357 | #ifdef LIBXML_DOCB_ENABLED |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2358 | case XML_DOCB_DOCUMENT_NODE: |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 2359 | #endif |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2360 | case XML_HTML_DOCUMENT_NODE: |
| 2361 | res = NULL; |
| 2362 | break; |
| 2363 | case XML_ATTRIBUTE_NODE:{ |
| 2364 | xmlAttrPtr attr = (xmlAttrPtr) cur; |
| 2365 | |
| 2366 | res = (xmlNodePtr) attr->next; |
| 2367 | break; |
| 2368 | } |
| 2369 | case XML_NAMESPACE_DECL:{ |
| 2370 | xmlNsPtr ns = (xmlNsPtr) cur; |
| 2371 | |
| 2372 | res = (xmlNodePtr) ns->next; |
| 2373 | break; |
| 2374 | } |
| 2375 | default: |
| 2376 | res = cur->next; |
| 2377 | break; |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 2378 | |
| 2379 | } |
| 2380 | resultobj = libxml_xmlNodePtrWrap(res); |
| 2381 | return resultobj; |
| 2382 | } |
| 2383 | |
| 2384 | static PyObject * |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2385 | libxml_prev(ATTRIBUTE_UNUSED PyObject * self, PyObject * args) |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 2386 | { |
| 2387 | PyObject *resultobj, *obj; |
| 2388 | xmlNodePtr cur; |
| 2389 | xmlNodePtr res; |
| 2390 | |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2391 | if (!PyArg_ParseTuple(args, (char *) "O:prev", &obj)) |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 2392 | return NULL; |
| 2393 | cur = PyxmlNode_Get(obj); |
| 2394 | |
| 2395 | #ifdef DEBUG |
| 2396 | printf("libxml_prev: cur = %p\n", cur); |
| 2397 | #endif |
| 2398 | |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2399 | switch (cur->type) { |
| 2400 | case XML_DOCUMENT_NODE: |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 2401 | #ifdef LIBXML_DOCB_ENABLED |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2402 | case XML_DOCB_DOCUMENT_NODE: |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 2403 | #endif |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2404 | case XML_HTML_DOCUMENT_NODE: |
| 2405 | res = NULL; |
| 2406 | break; |
| 2407 | case XML_ATTRIBUTE_NODE:{ |
| 2408 | xmlAttrPtr attr = (xmlAttrPtr) cur; |
| 2409 | |
Daniel Veillard | faa35ff | 2002-11-24 13:53:43 +0000 | [diff] [blame] | 2410 | res = (xmlNodePtr) attr->prev; |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2411 | } |
| 2412 | case XML_NAMESPACE_DECL: |
| 2413 | res = NULL; |
| 2414 | break; |
| 2415 | default: |
Daniel Veillard | faa35ff | 2002-11-24 13:53:43 +0000 | [diff] [blame] | 2416 | res = cur->prev; |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2417 | break; |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 2418 | } |
| 2419 | resultobj = libxml_xmlNodePtrWrap(res); |
| 2420 | return resultobj; |
| 2421 | } |
| 2422 | |
| 2423 | static PyObject * |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2424 | libxml_children(ATTRIBUTE_UNUSED PyObject * self, PyObject * args) |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 2425 | { |
| 2426 | PyObject *resultobj, *obj; |
| 2427 | xmlNodePtr cur; |
| 2428 | xmlNodePtr res; |
| 2429 | |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2430 | if (!PyArg_ParseTuple(args, (char *) "O:children", &obj)) |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 2431 | return NULL; |
| 2432 | cur = PyxmlNode_Get(obj); |
| 2433 | |
| 2434 | #ifdef DEBUG |
| 2435 | printf("libxml_children: cur = %p\n", cur); |
| 2436 | #endif |
| 2437 | |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2438 | switch (cur->type) { |
| 2439 | case XML_ELEMENT_NODE: |
| 2440 | case XML_ENTITY_REF_NODE: |
| 2441 | case XML_ENTITY_NODE: |
| 2442 | case XML_PI_NODE: |
| 2443 | case XML_COMMENT_NODE: |
| 2444 | case XML_DOCUMENT_NODE: |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 2445 | #ifdef LIBXML_DOCB_ENABLED |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2446 | case XML_DOCB_DOCUMENT_NODE: |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 2447 | #endif |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2448 | case XML_HTML_DOCUMENT_NODE: |
| 2449 | case XML_DTD_NODE: |
| 2450 | res = cur->children; |
| 2451 | break; |
| 2452 | case XML_ATTRIBUTE_NODE:{ |
| 2453 | xmlAttrPtr attr = (xmlAttrPtr) cur; |
| 2454 | |
| 2455 | res = attr->children; |
| 2456 | break; |
| 2457 | } |
| 2458 | default: |
| 2459 | res = NULL; |
| 2460 | break; |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 2461 | } |
| 2462 | resultobj = libxml_xmlNodePtrWrap(res); |
| 2463 | return resultobj; |
| 2464 | } |
| 2465 | |
| 2466 | static PyObject * |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2467 | libxml_last(ATTRIBUTE_UNUSED PyObject * self, PyObject * args) |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 2468 | { |
| 2469 | PyObject *resultobj, *obj; |
| 2470 | xmlNodePtr cur; |
| 2471 | xmlNodePtr res; |
| 2472 | |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2473 | if (!PyArg_ParseTuple(args, (char *) "O:last", &obj)) |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 2474 | return NULL; |
| 2475 | cur = PyxmlNode_Get(obj); |
| 2476 | |
| 2477 | #ifdef DEBUG |
| 2478 | printf("libxml_last: cur = %p\n", cur); |
| 2479 | #endif |
| 2480 | |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2481 | switch (cur->type) { |
| 2482 | case XML_ELEMENT_NODE: |
| 2483 | case XML_ENTITY_REF_NODE: |
| 2484 | case XML_ENTITY_NODE: |
| 2485 | case XML_PI_NODE: |
| 2486 | case XML_COMMENT_NODE: |
| 2487 | case XML_DOCUMENT_NODE: |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 2488 | #ifdef LIBXML_DOCB_ENABLED |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2489 | case XML_DOCB_DOCUMENT_NODE: |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 2490 | #endif |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2491 | case XML_HTML_DOCUMENT_NODE: |
| 2492 | case XML_DTD_NODE: |
| 2493 | res = cur->last; |
| 2494 | break; |
| 2495 | case XML_ATTRIBUTE_NODE:{ |
| 2496 | xmlAttrPtr attr = (xmlAttrPtr) cur; |
| 2497 | |
| 2498 | res = attr->last; |
| 2499 | } |
| 2500 | default: |
| 2501 | res = NULL; |
| 2502 | break; |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 2503 | } |
| 2504 | resultobj = libxml_xmlNodePtrWrap(res); |
| 2505 | return resultobj; |
| 2506 | } |
| 2507 | |
| 2508 | static PyObject * |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2509 | libxml_parent(ATTRIBUTE_UNUSED PyObject * self, PyObject * args) |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 2510 | { |
| 2511 | PyObject *resultobj, *obj; |
| 2512 | xmlNodePtr cur; |
| 2513 | xmlNodePtr res; |
| 2514 | |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2515 | if (!PyArg_ParseTuple(args, (char *) "O:parent", &obj)) |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 2516 | return NULL; |
| 2517 | cur = PyxmlNode_Get(obj); |
| 2518 | |
| 2519 | #ifdef DEBUG |
| 2520 | printf("libxml_parent: cur = %p\n", cur); |
| 2521 | #endif |
| 2522 | |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2523 | switch (cur->type) { |
| 2524 | case XML_DOCUMENT_NODE: |
| 2525 | case XML_HTML_DOCUMENT_NODE: |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 2526 | #ifdef LIBXML_DOCB_ENABLED |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2527 | case XML_DOCB_DOCUMENT_NODE: |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 2528 | #endif |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2529 | res = NULL; |
| 2530 | break; |
| 2531 | case XML_ATTRIBUTE_NODE:{ |
| 2532 | xmlAttrPtr attr = (xmlAttrPtr) cur; |
| 2533 | |
| 2534 | res = attr->parent; |
| 2535 | } |
| 2536 | case XML_ENTITY_DECL: |
| 2537 | case XML_NAMESPACE_DECL: |
| 2538 | case XML_XINCLUDE_START: |
| 2539 | case XML_XINCLUDE_END: |
| 2540 | res = NULL; |
| 2541 | break; |
| 2542 | default: |
| 2543 | res = cur->parent; |
| 2544 | break; |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 2545 | } |
| 2546 | resultobj = libxml_xmlNodePtrWrap(res); |
| 2547 | return resultobj; |
| 2548 | } |
| 2549 | |
| 2550 | static PyObject * |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2551 | libxml_type(ATTRIBUTE_UNUSED PyObject * self, PyObject * args) |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 2552 | { |
| 2553 | PyObject *resultobj, *obj; |
| 2554 | xmlNodePtr cur; |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2555 | const xmlChar *res = NULL; |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 2556 | |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2557 | if (!PyArg_ParseTuple(args, (char *) "O:last", &obj)) |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 2558 | return NULL; |
| 2559 | cur = PyxmlNode_Get(obj); |
| 2560 | |
| 2561 | #ifdef DEBUG |
| 2562 | printf("libxml_type: cur = %p\n", cur); |
| 2563 | #endif |
| 2564 | |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2565 | switch (cur->type) { |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 2566 | case XML_ELEMENT_NODE: |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2567 | res = (const xmlChar *) "element"; |
| 2568 | break; |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 2569 | case XML_ATTRIBUTE_NODE: |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2570 | res = (const xmlChar *) "attribute"; |
| 2571 | break; |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 2572 | case XML_TEXT_NODE: |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2573 | res = (const xmlChar *) "text"; |
| 2574 | break; |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 2575 | case XML_CDATA_SECTION_NODE: |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2576 | res = (const xmlChar *) "cdata"; |
| 2577 | break; |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 2578 | case XML_ENTITY_REF_NODE: |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2579 | res = (const xmlChar *) "entity_ref"; |
| 2580 | break; |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 2581 | case XML_ENTITY_NODE: |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2582 | res = (const xmlChar *) "entity"; |
| 2583 | break; |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 2584 | case XML_PI_NODE: |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2585 | res = (const xmlChar *) "pi"; |
| 2586 | break; |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 2587 | case XML_COMMENT_NODE: |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2588 | res = (const xmlChar *) "comment"; |
| 2589 | break; |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 2590 | case XML_DOCUMENT_NODE: |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2591 | res = (const xmlChar *) "document_xml"; |
| 2592 | break; |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 2593 | case XML_DOCUMENT_TYPE_NODE: |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2594 | res = (const xmlChar *) "doctype"; |
| 2595 | break; |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 2596 | case XML_DOCUMENT_FRAG_NODE: |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2597 | res = (const xmlChar *) "fragment"; |
| 2598 | break; |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 2599 | case XML_NOTATION_NODE: |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2600 | res = (const xmlChar *) "notation"; |
| 2601 | break; |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 2602 | case XML_HTML_DOCUMENT_NODE: |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2603 | res = (const xmlChar *) "document_html"; |
| 2604 | break; |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 2605 | case XML_DTD_NODE: |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2606 | res = (const xmlChar *) "dtd"; |
| 2607 | break; |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 2608 | case XML_ELEMENT_DECL: |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2609 | res = (const xmlChar *) "elem_decl"; |
| 2610 | break; |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 2611 | case XML_ATTRIBUTE_DECL: |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2612 | res = (const xmlChar *) "attribute_decl"; |
| 2613 | break; |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 2614 | case XML_ENTITY_DECL: |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2615 | res = (const xmlChar *) "entity_decl"; |
| 2616 | break; |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 2617 | case XML_NAMESPACE_DECL: |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2618 | res = (const xmlChar *) "namespace"; |
| 2619 | break; |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 2620 | case XML_XINCLUDE_START: |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2621 | res = (const xmlChar *) "xinclude_start"; |
| 2622 | break; |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 2623 | case XML_XINCLUDE_END: |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2624 | res = (const xmlChar *) "xinclude_end"; |
| 2625 | break; |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 2626 | #ifdef LIBXML_DOCB_ENABLED |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2627 | case XML_DOCB_DOCUMENT_NODE: |
| 2628 | res = (const xmlChar *) "document_docbook"; |
| 2629 | break; |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 2630 | #endif |
| 2631 | } |
| 2632 | #ifdef DEBUG |
| 2633 | printf("libxml_type: cur = %p: %s\n", cur, res); |
| 2634 | #endif |
| 2635 | |
Daniel Veillard | 1971ee2 | 2002-01-31 20:29:19 +0000 | [diff] [blame] | 2636 | resultobj = libxml_constxmlCharPtrWrap(res); |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 2637 | return resultobj; |
| 2638 | } |
| 2639 | |
| 2640 | /************************************************************************ |
| 2641 | * * |
Daniel Veillard | 36eea2d | 2002-02-04 00:17:01 +0000 | [diff] [blame] | 2642 | * Specific accessor functions * |
| 2643 | * * |
| 2644 | ************************************************************************/ |
| 2645 | PyObject * |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2646 | libxml_xmlNodeGetNsDefs(ATTRIBUTE_UNUSED PyObject * self, PyObject * args) |
| 2647 | { |
Daniel Veillard | 36eea2d | 2002-02-04 00:17:01 +0000 | [diff] [blame] | 2648 | PyObject *py_retval; |
| 2649 | xmlNsPtr c_retval; |
| 2650 | xmlNodePtr node; |
| 2651 | PyObject *pyobj_node; |
| 2652 | |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2653 | if (!PyArg_ParseTuple |
| 2654 | (args, (char *) "O:xmlNodeGetNsDefs", &pyobj_node)) |
| 2655 | return (NULL); |
Daniel Veillard | 36eea2d | 2002-02-04 00:17:01 +0000 | [diff] [blame] | 2656 | node = (xmlNodePtr) PyxmlNode_Get(pyobj_node); |
| 2657 | |
| 2658 | if ((node == NULL) || (node->type != XML_ELEMENT_NODE)) { |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2659 | Py_INCREF(Py_None); |
| 2660 | return (Py_None); |
Daniel Veillard | 36eea2d | 2002-02-04 00:17:01 +0000 | [diff] [blame] | 2661 | } |
| 2662 | c_retval = node->nsDef; |
| 2663 | py_retval = libxml_xmlNsPtrWrap((xmlNsPtr) c_retval); |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2664 | return (py_retval); |
Daniel Veillard | 36eea2d | 2002-02-04 00:17:01 +0000 | [diff] [blame] | 2665 | } |
| 2666 | |
| 2667 | PyObject * |
Daniel Veillard | f9cf6f5 | 2005-04-12 01:02:29 +0000 | [diff] [blame] | 2668 | libxml_xmlNodeRemoveNsDef(ATTRIBUTE_UNUSED PyObject * self, PyObject * args) |
| 2669 | { |
| 2670 | PyObject *py_retval; |
| 2671 | xmlNsPtr ns, prev; |
| 2672 | xmlNodePtr node; |
| 2673 | PyObject *pyobj_node; |
| 2674 | xmlChar *href; |
| 2675 | xmlNsPtr c_retval; |
| 2676 | |
| 2677 | if (!PyArg_ParseTuple |
| 2678 | (args, (char *) "Oz:xmlNodeRemoveNsDef", &pyobj_node, &href)) |
| 2679 | return (NULL); |
| 2680 | node = (xmlNodePtr) PyxmlNode_Get(pyobj_node); |
| 2681 | ns = NULL; |
| 2682 | |
| 2683 | if ((node == NULL) || (node->type != XML_ELEMENT_NODE)) { |
| 2684 | Py_INCREF(Py_None); |
| 2685 | return (Py_None); |
| 2686 | } |
| 2687 | |
| 2688 | if (href == NULL) { |
| 2689 | ns = node->nsDef; |
| 2690 | node->nsDef = NULL; |
| 2691 | c_retval = 0; |
| 2692 | } |
| 2693 | else { |
| 2694 | prev = NULL; |
| 2695 | ns = node->nsDef; |
| 2696 | while (ns != NULL) { |
| 2697 | if (xmlStrEqual(ns->href, href)) { |
| 2698 | if (prev != NULL) |
| 2699 | prev->next = ns->next; |
| 2700 | else |
| 2701 | node->nsDef = ns->next; |
| 2702 | ns->next = NULL; |
| 2703 | c_retval = 0; |
| 2704 | break; |
| 2705 | } |
| 2706 | prev = ns; |
| 2707 | ns = ns->next; |
| 2708 | } |
| 2709 | } |
| 2710 | |
| 2711 | c_retval = ns; |
| 2712 | py_retval = libxml_xmlNsPtrWrap((xmlNsPtr) c_retval); |
| 2713 | return (py_retval); |
| 2714 | } |
| 2715 | |
| 2716 | PyObject * |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2717 | libxml_xmlNodeGetNs(ATTRIBUTE_UNUSED PyObject * self, PyObject * args) |
| 2718 | { |
Daniel Veillard | 36eea2d | 2002-02-04 00:17:01 +0000 | [diff] [blame] | 2719 | PyObject *py_retval; |
| 2720 | xmlNsPtr c_retval; |
| 2721 | xmlNodePtr node; |
| 2722 | PyObject *pyobj_node; |
| 2723 | |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2724 | if (!PyArg_ParseTuple(args, (char *) "O:xmlNodeGetNs", &pyobj_node)) |
| 2725 | return (NULL); |
Daniel Veillard | 36eea2d | 2002-02-04 00:17:01 +0000 | [diff] [blame] | 2726 | node = (xmlNodePtr) PyxmlNode_Get(pyobj_node); |
| 2727 | |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 2728 | if ((node == NULL) || |
| 2729 | ((node->type != XML_ELEMENT_NODE) && |
| 2730 | (node->type != XML_ATTRIBUTE_NODE))) { |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2731 | Py_INCREF(Py_None); |
| 2732 | return (Py_None); |
Daniel Veillard | 36eea2d | 2002-02-04 00:17:01 +0000 | [diff] [blame] | 2733 | } |
| 2734 | c_retval = node->ns; |
| 2735 | py_retval = libxml_xmlNsPtrWrap((xmlNsPtr) c_retval); |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2736 | return (py_retval); |
Daniel Veillard | 36eea2d | 2002-02-04 00:17:01 +0000 | [diff] [blame] | 2737 | } |
| 2738 | |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 2739 | #ifdef LIBXML_OUTPUT_ENABLED |
Daniel Veillard | 36eea2d | 2002-02-04 00:17:01 +0000 | [diff] [blame] | 2740 | /************************************************************************ |
| 2741 | * * |
Daniel Veillard | 1e77438 | 2002-03-06 17:35:40 +0000 | [diff] [blame] | 2742 | * Serialization front-end * |
| 2743 | * * |
| 2744 | ************************************************************************/ |
| 2745 | |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2746 | static PyObject * |
| 2747 | libxml_serializeNode(ATTRIBUTE_UNUSED PyObject * self, PyObject * args) |
| 2748 | { |
Daniel Veillard | 1e77438 | 2002-03-06 17:35:40 +0000 | [diff] [blame] | 2749 | PyObject *py_retval = NULL; |
| 2750 | xmlChar *c_retval; |
| 2751 | PyObject *pyobj_node; |
| 2752 | xmlNodePtr node; |
| 2753 | xmlDocPtr doc; |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2754 | const char *encoding; |
Daniel Veillard | 1e77438 | 2002-03-06 17:35:40 +0000 | [diff] [blame] | 2755 | int format; |
| 2756 | int len; |
| 2757 | |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2758 | if (!PyArg_ParseTuple(args, (char *) "Ozi:serializeNode", &pyobj_node, |
| 2759 | &encoding, &format)) |
| 2760 | return (NULL); |
Daniel Veillard | 1e77438 | 2002-03-06 17:35:40 +0000 | [diff] [blame] | 2761 | node = (xmlNodePtr) PyxmlNode_Get(pyobj_node); |
| 2762 | |
| 2763 | if (node == NULL) { |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2764 | Py_INCREF(Py_None); |
| 2765 | return (Py_None); |
Daniel Veillard | 1e77438 | 2002-03-06 17:35:40 +0000 | [diff] [blame] | 2766 | } |
| 2767 | if (node->type == XML_DOCUMENT_NODE) { |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2768 | doc = (xmlDocPtr) node; |
| 2769 | xmlDocDumpFormatMemoryEnc(doc, &c_retval, &len, |
| 2770 | (const char *) encoding, format); |
| 2771 | py_retval = libxml_charPtrWrap((char *) c_retval); |
Daniel Veillard | 656ce94 | 2004-04-30 23:11:45 +0000 | [diff] [blame] | 2772 | #ifdef LIBXML_HTML_ENABLED |
Daniel Veillard | 1e77438 | 2002-03-06 17:35:40 +0000 | [diff] [blame] | 2773 | } else if (node->type == XML_HTML_DOCUMENT_NODE) { |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2774 | xmlOutputBufferPtr buf; |
| 2775 | xmlCharEncodingHandlerPtr handler = NULL; |
Daniel Veillard | 1e77438 | 2002-03-06 17:35:40 +0000 | [diff] [blame] | 2776 | |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2777 | doc = (xmlDocPtr) node; |
| 2778 | if (encoding != NULL) |
| 2779 | htmlSetMetaEncoding(doc, (const xmlChar *) encoding); |
| 2780 | encoding = (const char *) htmlGetMetaEncoding(doc); |
Daniel Veillard | 1e77438 | 2002-03-06 17:35:40 +0000 | [diff] [blame] | 2781 | |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2782 | if (encoding != NULL) { |
| 2783 | handler = xmlFindCharEncodingHandler(encoding); |
| 2784 | if (handler == NULL) { |
| 2785 | Py_INCREF(Py_None); |
| 2786 | return (Py_None); |
| 2787 | } |
| 2788 | } |
Daniel Veillard | 1e77438 | 2002-03-06 17:35:40 +0000 | [diff] [blame] | 2789 | |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2790 | /* |
| 2791 | * Fallback to HTML or ASCII when the encoding is unspecified |
| 2792 | */ |
| 2793 | if (handler == NULL) |
| 2794 | handler = xmlFindCharEncodingHandler("HTML"); |
| 2795 | if (handler == NULL) |
| 2796 | handler = xmlFindCharEncodingHandler("ascii"); |
Daniel Veillard | 1e77438 | 2002-03-06 17:35:40 +0000 | [diff] [blame] | 2797 | |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2798 | buf = xmlAllocOutputBuffer(handler); |
| 2799 | if (buf == NULL) { |
| 2800 | Py_INCREF(Py_None); |
| 2801 | return (Py_None); |
| 2802 | } |
| 2803 | htmlDocContentDumpFormatOutput(buf, doc, encoding, format); |
| 2804 | xmlOutputBufferFlush(buf); |
| 2805 | if (buf->conv != NULL) { |
| 2806 | len = buf->conv->use; |
| 2807 | c_retval = buf->conv->content; |
| 2808 | buf->conv->content = NULL; |
| 2809 | } else { |
| 2810 | len = buf->buffer->use; |
| 2811 | c_retval = buf->buffer->content; |
| 2812 | buf->buffer->content = NULL; |
| 2813 | } |
| 2814 | (void) xmlOutputBufferClose(buf); |
| 2815 | py_retval = libxml_charPtrWrap((char *) c_retval); |
Daniel Veillard | 656ce94 | 2004-04-30 23:11:45 +0000 | [diff] [blame] | 2816 | #endif /* LIBXML_HTML_ENABLED */ |
Daniel Veillard | 1e77438 | 2002-03-06 17:35:40 +0000 | [diff] [blame] | 2817 | } else { |
William M. Brack | 95af594 | 2004-02-08 04:12:49 +0000 | [diff] [blame] | 2818 | if (node->type == XML_NAMESPACE_DECL) |
| 2819 | doc = NULL; |
| 2820 | else |
| 2821 | doc = node->doc; |
Daniel Veillard | a8c0adb | 2002-11-17 22:37:35 +0000 | [diff] [blame] | 2822 | if ((doc == NULL) || (doc->type == XML_DOCUMENT_NODE)) { |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2823 | xmlOutputBufferPtr buf; |
| 2824 | xmlCharEncodingHandlerPtr handler = NULL; |
Daniel Veillard | 1e77438 | 2002-03-06 17:35:40 +0000 | [diff] [blame] | 2825 | |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2826 | if (encoding != NULL) { |
| 2827 | handler = xmlFindCharEncodingHandler(encoding); |
| 2828 | if (handler == NULL) { |
| 2829 | Py_INCREF(Py_None); |
| 2830 | return (Py_None); |
| 2831 | } |
| 2832 | } |
Daniel Veillard | 1e77438 | 2002-03-06 17:35:40 +0000 | [diff] [blame] | 2833 | |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2834 | buf = xmlAllocOutputBuffer(handler); |
| 2835 | if (buf == NULL) { |
| 2836 | Py_INCREF(Py_None); |
| 2837 | return (Py_None); |
| 2838 | } |
| 2839 | xmlNodeDumpOutput(buf, doc, node, 0, format, encoding); |
| 2840 | xmlOutputBufferFlush(buf); |
| 2841 | if (buf->conv != NULL) { |
| 2842 | len = buf->conv->use; |
| 2843 | c_retval = buf->conv->content; |
| 2844 | buf->conv->content = NULL; |
| 2845 | } else { |
| 2846 | len = buf->buffer->use; |
| 2847 | c_retval = buf->buffer->content; |
| 2848 | buf->buffer->content = NULL; |
| 2849 | } |
| 2850 | (void) xmlOutputBufferClose(buf); |
| 2851 | py_retval = libxml_charPtrWrap((char *) c_retval); |
Daniel Veillard | 656ce94 | 2004-04-30 23:11:45 +0000 | [diff] [blame] | 2852 | #ifdef LIBXML_HTML_ENABLED |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2853 | } else if (doc->type == XML_HTML_DOCUMENT_NODE) { |
| 2854 | xmlOutputBufferPtr buf; |
| 2855 | xmlCharEncodingHandlerPtr handler = NULL; |
Daniel Veillard | 1e77438 | 2002-03-06 17:35:40 +0000 | [diff] [blame] | 2856 | |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2857 | if (encoding != NULL) |
| 2858 | htmlSetMetaEncoding(doc, (const xmlChar *) encoding); |
| 2859 | encoding = (const char *) htmlGetMetaEncoding(doc); |
| 2860 | if (encoding != NULL) { |
| 2861 | handler = xmlFindCharEncodingHandler(encoding); |
| 2862 | if (handler == NULL) { |
| 2863 | Py_INCREF(Py_None); |
| 2864 | return (Py_None); |
| 2865 | } |
| 2866 | } |
Daniel Veillard | 1e77438 | 2002-03-06 17:35:40 +0000 | [diff] [blame] | 2867 | |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2868 | /* |
| 2869 | * Fallback to HTML or ASCII when the encoding is unspecified |
| 2870 | */ |
| 2871 | if (handler == NULL) |
| 2872 | handler = xmlFindCharEncodingHandler("HTML"); |
| 2873 | if (handler == NULL) |
| 2874 | handler = xmlFindCharEncodingHandler("ascii"); |
Daniel Veillard | 1e77438 | 2002-03-06 17:35:40 +0000 | [diff] [blame] | 2875 | |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2876 | buf = xmlAllocOutputBuffer(handler); |
| 2877 | if (buf == NULL) { |
| 2878 | Py_INCREF(Py_None); |
| 2879 | return (Py_None); |
| 2880 | } |
| 2881 | htmlNodeDumpFormatOutput(buf, doc, node, encoding, format); |
| 2882 | xmlOutputBufferFlush(buf); |
| 2883 | if (buf->conv != NULL) { |
| 2884 | len = buf->conv->use; |
| 2885 | c_retval = buf->conv->content; |
| 2886 | buf->conv->content = NULL; |
| 2887 | } else { |
| 2888 | len = buf->buffer->use; |
| 2889 | c_retval = buf->buffer->content; |
| 2890 | buf->buffer->content = NULL; |
| 2891 | } |
| 2892 | (void) xmlOutputBufferClose(buf); |
| 2893 | py_retval = libxml_charPtrWrap((char *) c_retval); |
Daniel Veillard | 656ce94 | 2004-04-30 23:11:45 +0000 | [diff] [blame] | 2894 | #endif /* LIBXML_HTML_ENABLED */ |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2895 | } else { |
| 2896 | Py_INCREF(Py_None); |
| 2897 | return (Py_None); |
| 2898 | } |
Daniel Veillard | 1e77438 | 2002-03-06 17:35:40 +0000 | [diff] [blame] | 2899 | } |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2900 | return (py_retval); |
Daniel Veillard | 1e77438 | 2002-03-06 17:35:40 +0000 | [diff] [blame] | 2901 | } |
| 2902 | |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2903 | static PyObject * |
| 2904 | libxml_saveNodeTo(ATTRIBUTE_UNUSED PyObject * self, PyObject * args) |
| 2905 | { |
Daniel Veillard | 1e77438 | 2002-03-06 17:35:40 +0000 | [diff] [blame] | 2906 | PyObject *py_file = NULL; |
| 2907 | FILE *output; |
| 2908 | PyObject *pyobj_node; |
| 2909 | xmlNodePtr node; |
| 2910 | xmlDocPtr doc; |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2911 | const char *encoding; |
Daniel Veillard | 1e77438 | 2002-03-06 17:35:40 +0000 | [diff] [blame] | 2912 | int format; |
| 2913 | int len; |
| 2914 | xmlOutputBufferPtr buf; |
| 2915 | xmlCharEncodingHandlerPtr handler = NULL; |
| 2916 | |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2917 | if (!PyArg_ParseTuple(args, (char *) "OOzi:serializeNode", &pyobj_node, |
| 2918 | &py_file, &encoding, &format)) |
| 2919 | return (NULL); |
Daniel Veillard | 1e77438 | 2002-03-06 17:35:40 +0000 | [diff] [blame] | 2920 | node = (xmlNodePtr) PyxmlNode_Get(pyobj_node); |
| 2921 | |
| 2922 | if (node == NULL) { |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2923 | return (PyInt_FromLong((long) -1)); |
Daniel Veillard | 1e77438 | 2002-03-06 17:35:40 +0000 | [diff] [blame] | 2924 | } |
| 2925 | if ((py_file == NULL) || (!(PyFile_Check(py_file)))) { |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2926 | return (PyInt_FromLong((long) -1)); |
Daniel Veillard | 1e77438 | 2002-03-06 17:35:40 +0000 | [diff] [blame] | 2927 | } |
| 2928 | output = PyFile_AsFile(py_file); |
| 2929 | if (output == NULL) { |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2930 | return (PyInt_FromLong((long) -1)); |
Daniel Veillard | 1e77438 | 2002-03-06 17:35:40 +0000 | [diff] [blame] | 2931 | } |
| 2932 | |
| 2933 | if (node->type == XML_DOCUMENT_NODE) { |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2934 | doc = (xmlDocPtr) node; |
Daniel Veillard | 1e77438 | 2002-03-06 17:35:40 +0000 | [diff] [blame] | 2935 | } else if (node->type == XML_HTML_DOCUMENT_NODE) { |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2936 | doc = (xmlDocPtr) node; |
Daniel Veillard | 1e77438 | 2002-03-06 17:35:40 +0000 | [diff] [blame] | 2937 | } else { |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2938 | doc = node->doc; |
Daniel Veillard | 1e77438 | 2002-03-06 17:35:40 +0000 | [diff] [blame] | 2939 | } |
Daniel Veillard | 656ce94 | 2004-04-30 23:11:45 +0000 | [diff] [blame] | 2940 | #ifdef LIBXML_HTML_ENABLED |
Daniel Veillard | 1e77438 | 2002-03-06 17:35:40 +0000 | [diff] [blame] | 2941 | if (doc->type == XML_HTML_DOCUMENT_NODE) { |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2942 | if (encoding == NULL) |
| 2943 | encoding = (const char *) htmlGetMetaEncoding(doc); |
Daniel Veillard | 1e77438 | 2002-03-06 17:35:40 +0000 | [diff] [blame] | 2944 | } |
Daniel Veillard | 656ce94 | 2004-04-30 23:11:45 +0000 | [diff] [blame] | 2945 | #endif /* LIBXML_HTML_ENABLED */ |
Daniel Veillard | 1e77438 | 2002-03-06 17:35:40 +0000 | [diff] [blame] | 2946 | if (encoding != NULL) { |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2947 | handler = xmlFindCharEncodingHandler(encoding); |
| 2948 | if (handler == NULL) { |
| 2949 | return (PyInt_FromLong((long) -1)); |
| 2950 | } |
Daniel Veillard | 1e77438 | 2002-03-06 17:35:40 +0000 | [diff] [blame] | 2951 | } |
| 2952 | if (doc->type == XML_HTML_DOCUMENT_NODE) { |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2953 | if (handler == NULL) |
| 2954 | handler = xmlFindCharEncodingHandler("HTML"); |
| 2955 | if (handler == NULL) |
| 2956 | handler = xmlFindCharEncodingHandler("ascii"); |
Daniel Veillard | 1e77438 | 2002-03-06 17:35:40 +0000 | [diff] [blame] | 2957 | } |
| 2958 | |
| 2959 | buf = xmlOutputBufferCreateFile(output, handler); |
| 2960 | if (node->type == XML_DOCUMENT_NODE) { |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2961 | len = xmlSaveFormatFileTo(buf, doc, encoding, format); |
Daniel Veillard | 656ce94 | 2004-04-30 23:11:45 +0000 | [diff] [blame] | 2962 | #ifdef LIBXML_HTML_ENABLED |
Daniel Veillard | 1e77438 | 2002-03-06 17:35:40 +0000 | [diff] [blame] | 2963 | } else if (node->type == XML_HTML_DOCUMENT_NODE) { |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2964 | htmlDocContentDumpFormatOutput(buf, doc, encoding, format); |
| 2965 | len = xmlOutputBufferClose(buf); |
Daniel Veillard | 1e77438 | 2002-03-06 17:35:40 +0000 | [diff] [blame] | 2966 | } else if (doc->type == XML_HTML_DOCUMENT_NODE) { |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2967 | htmlNodeDumpFormatOutput(buf, doc, node, encoding, format); |
| 2968 | len = xmlOutputBufferClose(buf); |
Daniel Veillard | 656ce94 | 2004-04-30 23:11:45 +0000 | [diff] [blame] | 2969 | #endif /* LIBXML_HTML_ENABLED */ |
Daniel Veillard | 1e77438 | 2002-03-06 17:35:40 +0000 | [diff] [blame] | 2970 | } else { |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2971 | xmlNodeDumpOutput(buf, doc, node, 0, format, encoding); |
| 2972 | len = xmlOutputBufferClose(buf); |
Daniel Veillard | 1e77438 | 2002-03-06 17:35:40 +0000 | [diff] [blame] | 2973 | } |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2974 | return (PyInt_FromLong((long) len)); |
Daniel Veillard | 1e77438 | 2002-03-06 17:35:40 +0000 | [diff] [blame] | 2975 | } |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 2976 | #endif /* LIBXML_OUTPUT_ENABLED */ |
Daniel Veillard | 1e77438 | 2002-03-06 17:35:40 +0000 | [diff] [blame] | 2977 | |
| 2978 | /************************************************************************ |
| 2979 | * * |
Daniel Veillard | a94ec6f | 2002-03-01 13:00:53 +0000 | [diff] [blame] | 2980 | * Extra stuff * |
| 2981 | * * |
| 2982 | ************************************************************************/ |
| 2983 | PyObject * |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2984 | libxml_xmlNewNode(ATTRIBUTE_UNUSED PyObject * self, PyObject * args) |
| 2985 | { |
Daniel Veillard | a94ec6f | 2002-03-01 13:00:53 +0000 | [diff] [blame] | 2986 | PyObject *py_retval; |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2987 | xmlChar *name; |
Daniel Veillard | a94ec6f | 2002-03-01 13:00:53 +0000 | [diff] [blame] | 2988 | xmlNodePtr node; |
| 2989 | |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2990 | if (!PyArg_ParseTuple(args, (char *) "s:xmlNewNode", &name)) |
| 2991 | return (NULL); |
Daniel Veillard | a94ec6f | 2002-03-01 13:00:53 +0000 | [diff] [blame] | 2992 | node = (xmlNodePtr) xmlNewNode(NULL, name); |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 2993 | #ifdef DEBUG |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2994 | printf("NewNode: %s : %p\n", name, (void *) node); |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 2995 | #endif |
Daniel Veillard | a94ec6f | 2002-03-01 13:00:53 +0000 | [diff] [blame] | 2996 | |
| 2997 | if (node == NULL) { |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 2998 | Py_INCREF(Py_None); |
| 2999 | return (Py_None); |
Daniel Veillard | a94ec6f | 2002-03-01 13:00:53 +0000 | [diff] [blame] | 3000 | } |
| 3001 | py_retval = libxml_xmlNodePtrWrap(node); |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 3002 | return (py_retval); |
Daniel Veillard | a94ec6f | 2002-03-01 13:00:53 +0000 | [diff] [blame] | 3003 | } |
| 3004 | |
Daniel Veillard | 5439624 | 2003-04-23 07:36:50 +0000 | [diff] [blame] | 3005 | |
| 3006 | /************************************************************************ |
| 3007 | * * |
| 3008 | * Local Catalog stuff * |
| 3009 | * * |
| 3010 | ************************************************************************/ |
| 3011 | static PyObject * |
| 3012 | libxml_addLocalCatalog(ATTRIBUTE_UNUSED PyObject * self, PyObject * args) |
| 3013 | { |
| 3014 | xmlChar *URL; |
| 3015 | xmlParserCtxtPtr ctxt; |
| 3016 | PyObject *pyobj_ctxt; |
| 3017 | |
| 3018 | if (!PyArg_ParseTuple(args, (char *)"Os:addLocalCatalog", &pyobj_ctxt, &URL)) |
| 3019 | return(NULL); |
| 3020 | |
| 3021 | ctxt = (xmlParserCtxtPtr) PyparserCtxt_Get(pyobj_ctxt); |
| 3022 | |
| 3023 | if (URL != NULL) { |
| 3024 | ctxt->catalogs = xmlCatalogAddLocal(ctxt->catalogs, URL); |
| 3025 | } |
| 3026 | |
| 3027 | #ifdef DEBUG |
| 3028 | printf("LocalCatalog: %s\n", URL); |
| 3029 | #endif |
| 3030 | |
| 3031 | Py_INCREF(Py_None); |
| 3032 | return (Py_None); |
| 3033 | } |
| 3034 | |
Daniel Veillard | c266464 | 2003-07-29 20:44:53 +0000 | [diff] [blame] | 3035 | #ifdef LIBXML_SCHEMAS_ENABLED |
| 3036 | |
| 3037 | /************************************************************************ |
| 3038 | * * |
| 3039 | * RelaxNG error handler registration * |
| 3040 | * * |
| 3041 | ************************************************************************/ |
| 3042 | |
| 3043 | typedef struct |
| 3044 | { |
| 3045 | PyObject *warn; |
| 3046 | PyObject *error; |
| 3047 | PyObject *arg; |
| 3048 | } xmlRelaxNGValidCtxtPyCtxt; |
| 3049 | typedef xmlRelaxNGValidCtxtPyCtxt *xmlRelaxNGValidCtxtPyCtxtPtr; |
| 3050 | |
| 3051 | static void |
| 3052 | libxml_xmlRelaxNGValidityGenericErrorFuncHandler(void *ctx, char *str) |
| 3053 | { |
| 3054 | PyObject *list; |
| 3055 | PyObject *result; |
| 3056 | xmlRelaxNGValidCtxtPyCtxtPtr pyCtxt; |
| 3057 | |
| 3058 | #ifdef DEBUG_ERROR |
| 3059 | printf("libxml_xmlRelaxNGValidityGenericErrorFuncHandler(%p, %s, ...) called\n", ctx, str); |
| 3060 | #endif |
| 3061 | |
| 3062 | pyCtxt = (xmlRelaxNGValidCtxtPyCtxtPtr)ctx; |
| 3063 | |
| 3064 | list = PyTuple_New(2); |
| 3065 | PyTuple_SetItem(list, 0, libxml_charPtrWrap(str)); |
| 3066 | PyTuple_SetItem(list, 1, pyCtxt->arg); |
| 3067 | Py_XINCREF(pyCtxt->arg); |
| 3068 | result = PyEval_CallObject(pyCtxt->error, list); |
| 3069 | if (result == NULL) |
| 3070 | { |
| 3071 | /* TODO: manage for the exception to be propagated... */ |
| 3072 | PyErr_Print(); |
| 3073 | } |
| 3074 | Py_XDECREF(list); |
| 3075 | Py_XDECREF(result); |
| 3076 | } |
| 3077 | |
| 3078 | static void |
| 3079 | libxml_xmlRelaxNGValidityGenericWarningFuncHandler(void *ctx, char *str) |
| 3080 | { |
| 3081 | PyObject *list; |
| 3082 | PyObject *result; |
| 3083 | xmlRelaxNGValidCtxtPyCtxtPtr pyCtxt; |
| 3084 | |
| 3085 | #ifdef DEBUG_ERROR |
| 3086 | printf("libxml_xmlRelaxNGValidityGenericWarningFuncHandler(%p, %s, ...) called\n", ctx, str); |
| 3087 | #endif |
| 3088 | |
| 3089 | pyCtxt = (xmlRelaxNGValidCtxtPyCtxtPtr)ctx; |
| 3090 | |
| 3091 | list = PyTuple_New(2); |
| 3092 | PyTuple_SetItem(list, 0, libxml_charPtrWrap(str)); |
| 3093 | PyTuple_SetItem(list, 1, pyCtxt->arg); |
| 3094 | Py_XINCREF(pyCtxt->arg); |
| 3095 | result = PyEval_CallObject(pyCtxt->warn, list); |
| 3096 | if (result == NULL) |
| 3097 | { |
| 3098 | /* TODO: manage for the exception to be propagated... */ |
| 3099 | PyErr_Print(); |
| 3100 | } |
| 3101 | Py_XDECREF(list); |
| 3102 | Py_XDECREF(result); |
| 3103 | } |
| 3104 | |
| 3105 | static void |
| 3106 | libxml_xmlRelaxNGValidityErrorFunc(void *ctx, const char *msg, ...) |
| 3107 | { |
| 3108 | va_list ap; |
| 3109 | |
| 3110 | va_start(ap, msg); |
| 3111 | libxml_xmlRelaxNGValidityGenericErrorFuncHandler(ctx, libxml_buildMessage(msg, ap)); |
| 3112 | va_end(ap); |
| 3113 | } |
| 3114 | |
| 3115 | static void |
| 3116 | libxml_xmlRelaxNGValidityWarningFunc(void *ctx, const char *msg, ...) |
| 3117 | { |
| 3118 | va_list ap; |
| 3119 | |
| 3120 | va_start(ap, msg); |
| 3121 | libxml_xmlRelaxNGValidityGenericWarningFuncHandler(ctx, libxml_buildMessage(msg, ap)); |
| 3122 | va_end(ap); |
| 3123 | } |
| 3124 | |
| 3125 | static PyObject * |
| 3126 | libxml_xmlRelaxNGSetValidErrors(ATTRIBUTE_UNUSED PyObject * self, PyObject * args) |
| 3127 | { |
| 3128 | PyObject *py_retval; |
| 3129 | PyObject *pyobj_error; |
| 3130 | PyObject *pyobj_warn; |
| 3131 | PyObject *pyobj_ctx; |
| 3132 | PyObject *pyobj_arg = Py_None; |
| 3133 | xmlRelaxNGValidCtxtPtr ctxt; |
| 3134 | xmlRelaxNGValidCtxtPyCtxtPtr pyCtxt; |
| 3135 | |
| 3136 | if (!PyArg_ParseTuple |
| 3137 | (args, (char *) "OOO|O:xmlRelaxNGSetValidErrors", &pyobj_ctx, &pyobj_error, &pyobj_warn, &pyobj_arg)) |
| 3138 | return (NULL); |
| 3139 | |
| 3140 | #ifdef DEBUG_ERROR |
| 3141 | printf("libxml_xmlRelaxNGSetValidErrors(%p, %p, %p) called\n", pyobj_ctx, pyobj_error, pyobj_warn); |
| 3142 | #endif |
| 3143 | |
| 3144 | ctxt = PyrelaxNgValidCtxt_Get(pyobj_ctx); |
William M. Brack | c193956 | 2003-08-05 15:52:22 +0000 | [diff] [blame] | 3145 | if (xmlRelaxNGGetValidErrors(ctxt, NULL, NULL, (void **) &pyCtxt) == -1) |
Daniel Veillard | c266464 | 2003-07-29 20:44:53 +0000 | [diff] [blame] | 3146 | { |
| 3147 | py_retval = libxml_intWrap(-1); |
| 3148 | return(py_retval); |
| 3149 | } |
| 3150 | |
| 3151 | if (pyCtxt == NULL) |
| 3152 | { |
| 3153 | /* first time to set the error handlers */ |
| 3154 | pyCtxt = xmlMalloc(sizeof(xmlRelaxNGValidCtxtPyCtxt)); |
| 3155 | if (pyCtxt == NULL) { |
| 3156 | py_retval = libxml_intWrap(-1); |
| 3157 | return(py_retval); |
| 3158 | } |
| 3159 | memset(pyCtxt, 0, sizeof(xmlRelaxNGValidCtxtPyCtxt)); |
| 3160 | } |
| 3161 | |
| 3162 | /* TODO: check warn and error is a function ! */ |
| 3163 | Py_XDECREF(pyCtxt->error); |
| 3164 | Py_XINCREF(pyobj_error); |
| 3165 | pyCtxt->error = pyobj_error; |
| 3166 | |
| 3167 | Py_XDECREF(pyCtxt->warn); |
| 3168 | Py_XINCREF(pyobj_warn); |
| 3169 | pyCtxt->warn = pyobj_warn; |
| 3170 | |
| 3171 | Py_XDECREF(pyCtxt->arg); |
| 3172 | Py_XINCREF(pyobj_arg); |
| 3173 | pyCtxt->arg = pyobj_arg; |
| 3174 | |
| 3175 | xmlRelaxNGSetValidErrors(ctxt, &libxml_xmlRelaxNGValidityErrorFunc, &libxml_xmlRelaxNGValidityWarningFunc, pyCtxt); |
| 3176 | |
| 3177 | py_retval = libxml_intWrap(1); |
| 3178 | return (py_retval); |
| 3179 | } |
| 3180 | |
| 3181 | static PyObject * |
| 3182 | libxml_xmlRelaxNGFreeValidCtxt(ATTRIBUTE_UNUSED PyObject *self, PyObject *args) { |
| 3183 | xmlRelaxNGValidCtxtPtr ctxt; |
| 3184 | xmlRelaxNGValidCtxtPyCtxtPtr pyCtxt; |
| 3185 | PyObject *pyobj_ctxt; |
| 3186 | |
| 3187 | if (!PyArg_ParseTuple(args, (char *)"O:xmlRelaxNGFreeValidCtxt", &pyobj_ctxt)) |
| 3188 | return(NULL); |
| 3189 | ctxt = (xmlRelaxNGValidCtxtPtr) PyrelaxNgValidCtxt_Get(pyobj_ctxt); |
| 3190 | |
William M. Brack | c193956 | 2003-08-05 15:52:22 +0000 | [diff] [blame] | 3191 | if (xmlRelaxNGGetValidErrors(ctxt, NULL, NULL, (void **) &pyCtxt) == 0) |
Daniel Veillard | c266464 | 2003-07-29 20:44:53 +0000 | [diff] [blame] | 3192 | { |
| 3193 | if (pyCtxt != NULL) |
| 3194 | { |
| 3195 | Py_XDECREF(pyCtxt->error); |
| 3196 | Py_XDECREF(pyCtxt->warn); |
| 3197 | Py_XDECREF(pyCtxt->arg); |
| 3198 | xmlFree(pyCtxt); |
| 3199 | } |
| 3200 | } |
| 3201 | |
| 3202 | xmlRelaxNGFreeValidCtxt(ctxt); |
| 3203 | Py_INCREF(Py_None); |
| 3204 | return(Py_None); |
| 3205 | } |
| 3206 | |
Daniel Veillard | 259f0df | 2004-08-18 09:13:18 +0000 | [diff] [blame] | 3207 | typedef struct |
| 3208 | { |
| 3209 | PyObject *warn; |
| 3210 | PyObject *error; |
| 3211 | PyObject *arg; |
| 3212 | } xmlSchemaValidCtxtPyCtxt; |
| 3213 | typedef xmlSchemaValidCtxtPyCtxt *xmlSchemaValidCtxtPyCtxtPtr; |
| 3214 | |
| 3215 | static void |
| 3216 | libxml_xmlSchemaValidityGenericErrorFuncHandler(void *ctx, char *str) |
| 3217 | { |
| 3218 | PyObject *list; |
| 3219 | PyObject *result; |
| 3220 | xmlSchemaValidCtxtPyCtxtPtr pyCtxt; |
| 3221 | |
| 3222 | #ifdef DEBUG_ERROR |
| 3223 | printf("libxml_xmlSchemaValiditiyGenericErrorFuncHandler(%p, %s, ...) called\n", ctx, str); |
| 3224 | #endif |
| 3225 | |
| 3226 | pyCtxt = (xmlSchemaValidCtxtPyCtxtPtr) ctx; |
| 3227 | |
| 3228 | list = PyTuple_New(2); |
| 3229 | PyTuple_SetItem(list, 0, libxml_charPtrWrap(str)); |
| 3230 | PyTuple_SetItem(list, 1, pyCtxt->arg); |
| 3231 | Py_XINCREF(pyCtxt->arg); |
| 3232 | result = PyEval_CallObject(pyCtxt->error, list); |
| 3233 | if (result == NULL) |
| 3234 | { |
| 3235 | /* TODO: manage for the exception to be propagated... */ |
| 3236 | PyErr_Print(); |
| 3237 | } |
| 3238 | Py_XDECREF(list); |
| 3239 | Py_XDECREF(result); |
| 3240 | } |
| 3241 | |
| 3242 | static void |
| 3243 | libxml_xmlSchemaValidityGenericWarningFuncHandler(void *ctx, char *str) |
| 3244 | { |
| 3245 | PyObject *list; |
| 3246 | PyObject *result; |
| 3247 | xmlSchemaValidCtxtPyCtxtPtr pyCtxt; |
| 3248 | |
| 3249 | #ifdef DEBUG_ERROR |
| 3250 | printf("libxml_xmlSchemaValidityGenericWarningFuncHandler(%p, %s, ...) called\n", ctx, str); |
| 3251 | #endif |
| 3252 | |
| 3253 | pyCtxt = (xmlSchemaValidCtxtPyCtxtPtr) ctx; |
| 3254 | |
| 3255 | list = PyTuple_New(2); |
| 3256 | PyTuple_SetItem(list, 0, libxml_charPtrWrap(str)); |
| 3257 | PyTuple_SetItem(list, 1, pyCtxt->arg); |
| 3258 | Py_XINCREF(pyCtxt->arg); |
| 3259 | result = PyEval_CallObject(pyCtxt->warn, list); |
| 3260 | if (result == NULL) |
| 3261 | { |
| 3262 | /* TODO: manage for the exception to be propagated... */ |
| 3263 | PyErr_Print(); |
| 3264 | } |
| 3265 | Py_XDECREF(list); |
| 3266 | Py_XDECREF(result); |
| 3267 | } |
| 3268 | |
| 3269 | static void |
| 3270 | libxml_xmlSchemaValidityErrorFunc(void *ctx, const char *msg, ...) |
| 3271 | { |
| 3272 | va_list ap; |
| 3273 | |
| 3274 | va_start(ap, msg); |
| 3275 | libxml_xmlSchemaValidityGenericErrorFuncHandler(ctx, libxml_buildMessage(msg, ap)); |
| 3276 | va_end(ap); |
| 3277 | } |
| 3278 | |
| 3279 | static void |
| 3280 | libxml_xmlSchemaValidityWarningFunc(void *ctx, const char *msg, ...) |
| 3281 | { |
| 3282 | va_list ap; |
| 3283 | |
| 3284 | va_start(ap, msg); |
| 3285 | libxml_xmlSchemaValidityGenericWarningFuncHandler(ctx, libxml_buildMessage(msg, ap)); |
| 3286 | va_end(ap); |
| 3287 | } |
| 3288 | |
Daniel Veillard | 6ebf3c4 | 2004-08-22 13:11:39 +0000 | [diff] [blame] | 3289 | PyObject * |
Daniel Veillard | 259f0df | 2004-08-18 09:13:18 +0000 | [diff] [blame] | 3290 | libxml_xmlSchemaSetValidErrors(ATTRIBUTE_UNUSED PyObject * self, PyObject * args) |
| 3291 | { |
| 3292 | PyObject *py_retval; |
| 3293 | PyObject *pyobj_error; |
| 3294 | PyObject *pyobj_warn; |
| 3295 | PyObject *pyobj_ctx; |
| 3296 | PyObject *pyobj_arg = Py_None; |
| 3297 | xmlSchemaValidCtxtPtr ctxt; |
| 3298 | xmlSchemaValidCtxtPyCtxtPtr pyCtxt; |
| 3299 | |
| 3300 | if (!PyArg_ParseTuple |
| 3301 | (args, (char *) "OOO|O:xmlSchemaSetValidErrors", &pyobj_ctx, &pyobj_error, &pyobj_warn, &pyobj_arg)) |
| 3302 | return (NULL); |
| 3303 | |
| 3304 | #ifdef DEBUG_ERROR |
| 3305 | printf("libxml_xmlSchemaSetValidErrors(%p, %p, %p) called\n", pyobj_ctx, pyobj_error, pyobj_warn); |
| 3306 | #endif |
| 3307 | |
| 3308 | ctxt = PySchemaValidCtxt_Get(pyobj_ctx); |
| 3309 | if (xmlSchemaGetValidErrors(ctxt, NULL, NULL, (void **) &pyCtxt) == -1) |
| 3310 | { |
| 3311 | py_retval = libxml_intWrap(-1); |
| 3312 | return(py_retval); |
| 3313 | } |
| 3314 | |
| 3315 | if (pyCtxt == NULL) |
| 3316 | { |
| 3317 | /* first time to set the error handlers */ |
| 3318 | pyCtxt = xmlMalloc(sizeof(xmlSchemaValidCtxtPyCtxt)); |
| 3319 | if (pyCtxt == NULL) { |
| 3320 | py_retval = libxml_intWrap(-1); |
| 3321 | return(py_retval); |
| 3322 | } |
| 3323 | memset(pyCtxt, 0, sizeof(xmlSchemaValidCtxtPyCtxt)); |
| 3324 | } |
| 3325 | |
| 3326 | /* TODO: check warn and error is a function ! */ |
| 3327 | Py_XDECREF(pyCtxt->error); |
| 3328 | Py_XINCREF(pyobj_error); |
| 3329 | pyCtxt->error = pyobj_error; |
| 3330 | |
| 3331 | Py_XDECREF(pyCtxt->warn); |
| 3332 | Py_XINCREF(pyobj_warn); |
| 3333 | pyCtxt->warn = pyobj_warn; |
| 3334 | |
| 3335 | Py_XDECREF(pyCtxt->arg); |
| 3336 | Py_XINCREF(pyobj_arg); |
| 3337 | pyCtxt->arg = pyobj_arg; |
| 3338 | |
| 3339 | xmlSchemaSetValidErrors(ctxt, &libxml_xmlSchemaValidityErrorFunc, &libxml_xmlSchemaValidityWarningFunc, pyCtxt); |
| 3340 | |
| 3341 | py_retval = libxml_intWrap(1); |
| 3342 | return(py_retval); |
| 3343 | } |
| 3344 | |
Daniel Veillard | bb8502c | 2005-03-30 07:40:35 +0000 | [diff] [blame] | 3345 | static PyObject * |
Daniel Veillard | 259f0df | 2004-08-18 09:13:18 +0000 | [diff] [blame] | 3346 | libxml_xmlSchemaFreeValidCtxt(ATTRIBUTE_UNUSED PyObject * self, PyObject * args) |
| 3347 | { |
| 3348 | xmlSchemaValidCtxtPtr ctxt; |
| 3349 | xmlSchemaValidCtxtPyCtxtPtr pyCtxt; |
| 3350 | PyObject *pyobj_ctxt; |
| 3351 | |
| 3352 | if (!PyArg_ParseTuple(args, (char *)"O:xmlSchemaFreeValidCtxt", &pyobj_ctxt)) |
| 3353 | return(NULL); |
| 3354 | ctxt = (xmlSchemaValidCtxtPtr) PySchemaValidCtxt_Get(pyobj_ctxt); |
| 3355 | |
| 3356 | if (xmlSchemaGetValidErrors(ctxt, NULL, NULL, (void **) &pyCtxt) == 0) |
| 3357 | { |
| 3358 | if (pyCtxt != NULL) |
| 3359 | { |
| 3360 | Py_XDECREF(pyCtxt->error); |
| 3361 | Py_XDECREF(pyCtxt->warn); |
| 3362 | Py_XDECREF(pyCtxt->arg); |
| 3363 | xmlFree(pyCtxt); |
| 3364 | } |
| 3365 | } |
| 3366 | |
| 3367 | xmlSchemaFreeValidCtxt(ctxt); |
| 3368 | Py_INCREF(Py_None); |
| 3369 | return(Py_None); |
| 3370 | } |
| 3371 | |
Daniel Veillard | c266464 | 2003-07-29 20:44:53 +0000 | [diff] [blame] | 3372 | #endif |
| 3373 | |
Daniel Veillard | d5e198a | 2004-03-09 09:03:28 +0000 | [diff] [blame] | 3374 | #ifdef LIBXML_C14N_ENABLED |
| 3375 | #ifdef LIBXML_OUTPUT_ENABLED |
| 3376 | |
| 3377 | /************************************************************************ |
| 3378 | * * |
| 3379 | * XML Canonicalization c14n * |
| 3380 | * * |
| 3381 | ************************************************************************/ |
| 3382 | |
| 3383 | static int |
| 3384 | PyxmlNodeSet_Convert(PyObject *py_nodeset, xmlNodeSetPtr *result) |
| 3385 | { |
| 3386 | xmlNodeSetPtr nodeSet; |
| 3387 | int is_tuple = 0; |
| 3388 | |
| 3389 | if (PyTuple_Check(py_nodeset)) |
| 3390 | is_tuple = 1; |
| 3391 | else if (PyList_Check(py_nodeset)) |
| 3392 | is_tuple = 0; |
| 3393 | else if (py_nodeset == Py_None) { |
| 3394 | *result = NULL; |
| 3395 | return 0; |
| 3396 | } |
| 3397 | else { |
| 3398 | PyErr_SetString(PyExc_TypeError, |
| 3399 | "must be a tuple or list of nodes."); |
| 3400 | return -1; |
| 3401 | } |
| 3402 | |
| 3403 | nodeSet = (xmlNodeSetPtr) xmlMalloc(sizeof(xmlNodeSet)); |
| 3404 | if (nodeSet == NULL) { |
| 3405 | PyErr_SetString(PyExc_MemoryError, ""); |
| 3406 | return -1; |
| 3407 | } |
| 3408 | |
| 3409 | nodeSet->nodeNr = 0; |
| 3410 | nodeSet->nodeMax = (is_tuple |
| 3411 | ? PyTuple_GET_SIZE(py_nodeset) |
| 3412 | : PyList_GET_SIZE(py_nodeset)); |
| 3413 | nodeSet->nodeTab |
| 3414 | = (xmlNodePtr *) xmlMalloc (nodeSet->nodeMax |
| 3415 | * sizeof(xmlNodePtr)); |
| 3416 | if (nodeSet->nodeTab == NULL) { |
| 3417 | xmlFree(nodeSet); |
| 3418 | PyErr_SetString(PyExc_MemoryError, ""); |
| 3419 | return -1; |
| 3420 | } |
| 3421 | memset(nodeSet->nodeTab, 0 , |
| 3422 | nodeSet->nodeMax * sizeof(xmlNodePtr)); |
| 3423 | |
| 3424 | { |
| 3425 | int idx; |
| 3426 | for (idx=0; idx < nodeSet->nodeMax; ++idx) { |
| 3427 | xmlNodePtr pynode = |
| 3428 | PyxmlNode_Get (is_tuple |
| 3429 | ? PyTuple_GET_ITEM(py_nodeset, idx) |
| 3430 | : PyList_GET_ITEM(py_nodeset, idx)); |
| 3431 | if (pynode) |
| 3432 | nodeSet->nodeTab[nodeSet->nodeNr++] = pynode; |
| 3433 | } |
| 3434 | } |
| 3435 | *result = nodeSet; |
| 3436 | return 0; |
| 3437 | } |
| 3438 | |
| 3439 | static int |
| 3440 | PystringSet_Convert(PyObject *py_strings, xmlChar *** result) |
| 3441 | { |
| 3442 | /* NOTE: the array should be freed, but the strings are shared |
| 3443 | with the python strings and so must not be freed. */ |
| 3444 | |
| 3445 | xmlChar ** strings; |
| 3446 | int is_tuple = 0; |
| 3447 | int count; |
| 3448 | int init_index = 0; |
| 3449 | |
| 3450 | if (PyTuple_Check(py_strings)) |
| 3451 | is_tuple = 1; |
| 3452 | else if (PyList_Check(py_strings)) |
| 3453 | is_tuple = 0; |
| 3454 | else if (py_strings == Py_None) { |
| 3455 | *result = NULL; |
| 3456 | return 0; |
| 3457 | } |
| 3458 | else { |
| 3459 | PyErr_SetString(PyExc_TypeError, |
| 3460 | "must be a tuple or list of strings."); |
| 3461 | return -1; |
| 3462 | } |
| 3463 | |
| 3464 | count = (is_tuple |
| 3465 | ? PyTuple_GET_SIZE(py_strings) |
| 3466 | : PyList_GET_SIZE(py_strings)); |
| 3467 | |
| 3468 | strings = (xmlChar **) xmlMalloc(sizeof(xmlChar *) * count); |
| 3469 | |
| 3470 | if (strings == NULL) { |
| 3471 | PyErr_SetString(PyExc_MemoryError, ""); |
| 3472 | return -1; |
| 3473 | } |
| 3474 | |
| 3475 | memset(strings, 0 , sizeof(xmlChar *) * count); |
| 3476 | |
| 3477 | { |
| 3478 | int idx; |
| 3479 | for (idx=0; idx < count; ++idx) { |
| 3480 | char* s = PyString_AsString |
| 3481 | (is_tuple |
| 3482 | ? PyTuple_GET_ITEM(py_strings, idx) |
| 3483 | : PyList_GET_ITEM(py_strings, idx)); |
| 3484 | if (s) |
| 3485 | strings[init_index++] = (xmlChar *)s; |
| 3486 | else { |
| 3487 | xmlFree(strings); |
| 3488 | PyErr_SetString(PyExc_TypeError, |
| 3489 | "must be a tuple or list of strings."); |
| 3490 | return -1; |
| 3491 | } |
| 3492 | } |
| 3493 | } |
| 3494 | |
| 3495 | *result = strings; |
| 3496 | return 0; |
| 3497 | } |
| 3498 | |
| 3499 | static PyObject * |
| 3500 | libxml_C14NDocDumpMemory(ATTRIBUTE_UNUSED PyObject * self, |
| 3501 | PyObject * args) |
| 3502 | { |
| 3503 | PyObject *py_retval = NULL; |
| 3504 | |
| 3505 | PyObject *pyobj_doc; |
| 3506 | PyObject *pyobj_nodes; |
| 3507 | int exclusive; |
| 3508 | PyObject *pyobj_prefixes; |
| 3509 | int with_comments; |
| 3510 | |
| 3511 | xmlDocPtr doc; |
| 3512 | xmlNodeSetPtr nodes; |
| 3513 | xmlChar **prefixes = NULL; |
| 3514 | xmlChar *doc_txt; |
| 3515 | |
| 3516 | int result; |
| 3517 | |
| 3518 | if (!PyArg_ParseTuple(args, (char *) "OOiOi:C14NDocDumpMemory", |
| 3519 | &pyobj_doc, |
| 3520 | &pyobj_nodes, |
| 3521 | &exclusive, |
| 3522 | &pyobj_prefixes, |
| 3523 | &with_comments)) |
| 3524 | return (NULL); |
| 3525 | |
| 3526 | doc = (xmlDocPtr) PyxmlNode_Get(pyobj_doc); |
| 3527 | if (!doc) { |
| 3528 | PyErr_SetString(PyExc_TypeError, "bad document."); |
| 3529 | return NULL; |
| 3530 | } |
| 3531 | |
| 3532 | result = PyxmlNodeSet_Convert(pyobj_nodes, &nodes); |
| 3533 | if (result < 0) return NULL; |
| 3534 | |
| 3535 | if (exclusive) { |
| 3536 | result = PystringSet_Convert(pyobj_prefixes, &prefixes); |
| 3537 | if (result < 0) { |
| 3538 | if (nodes) { |
| 3539 | xmlFree(nodes->nodeTab); |
| 3540 | xmlFree(nodes); |
| 3541 | } |
| 3542 | return NULL; |
| 3543 | } |
| 3544 | } |
| 3545 | |
| 3546 | result = xmlC14NDocDumpMemory(doc, |
| 3547 | nodes, |
| 3548 | exclusive, |
| 3549 | prefixes, |
| 3550 | with_comments, |
| 3551 | &doc_txt); |
| 3552 | |
| 3553 | if (nodes) { |
| 3554 | xmlFree(nodes->nodeTab); |
| 3555 | xmlFree(nodes); |
| 3556 | } |
| 3557 | if (prefixes) { |
| 3558 | xmlChar ** idx = prefixes; |
| 3559 | while (*idx) xmlFree(*(idx++)); |
| 3560 | xmlFree(prefixes); |
| 3561 | } |
| 3562 | |
| 3563 | if (result < 0) { |
| 3564 | PyErr_SetString(PyExc_Exception, |
| 3565 | "libxml2 xmlC14NDocDumpMemory failure."); |
| 3566 | return NULL; |
| 3567 | } |
| 3568 | else { |
| 3569 | py_retval = PyString_FromStringAndSize((const char *) doc_txt, |
| 3570 | result); |
| 3571 | xmlFree(doc_txt); |
| 3572 | return py_retval; |
| 3573 | } |
| 3574 | } |
| 3575 | |
| 3576 | static PyObject * |
| 3577 | libxml_C14NDocSaveTo(ATTRIBUTE_UNUSED PyObject * self, |
| 3578 | PyObject * args) |
| 3579 | { |
| 3580 | PyObject *pyobj_doc; |
| 3581 | PyObject *py_file; |
| 3582 | PyObject *pyobj_nodes; |
| 3583 | int exclusive; |
| 3584 | PyObject *pyobj_prefixes; |
| 3585 | int with_comments; |
| 3586 | |
| 3587 | xmlDocPtr doc; |
| 3588 | xmlNodeSetPtr nodes; |
| 3589 | xmlChar **prefixes = NULL; |
| 3590 | FILE * output; |
| 3591 | xmlOutputBufferPtr buf; |
| 3592 | |
| 3593 | int result; |
| 3594 | int len; |
| 3595 | |
| 3596 | if (!PyArg_ParseTuple(args, (char *) "OOiOiO:C14NDocSaveTo", |
| 3597 | &pyobj_doc, |
| 3598 | &pyobj_nodes, |
| 3599 | &exclusive, |
| 3600 | &pyobj_prefixes, |
| 3601 | &with_comments, |
| 3602 | &py_file)) |
| 3603 | return (NULL); |
| 3604 | |
| 3605 | doc = (xmlDocPtr) PyxmlNode_Get(pyobj_doc); |
| 3606 | if (!doc) { |
| 3607 | PyErr_SetString(PyExc_TypeError, "bad document."); |
| 3608 | return NULL; |
| 3609 | } |
| 3610 | |
| 3611 | if ((py_file == NULL) || (!(PyFile_Check(py_file)))) { |
| 3612 | PyErr_SetString(PyExc_TypeError, "bad file."); |
| 3613 | return NULL; |
| 3614 | } |
| 3615 | output = PyFile_AsFile(py_file); |
| 3616 | if (output == NULL) { |
| 3617 | PyErr_SetString(PyExc_TypeError, "bad file."); |
| 3618 | return NULL; |
| 3619 | } |
| 3620 | buf = xmlOutputBufferCreateFile(output, NULL); |
| 3621 | |
| 3622 | result = PyxmlNodeSet_Convert(pyobj_nodes, &nodes); |
| 3623 | if (result < 0) return NULL; |
| 3624 | |
| 3625 | if (exclusive) { |
| 3626 | result = PystringSet_Convert(pyobj_prefixes, &prefixes); |
| 3627 | if (result < 0) { |
| 3628 | if (nodes) { |
| 3629 | xmlFree(nodes->nodeTab); |
| 3630 | xmlFree(nodes); |
| 3631 | } |
| 3632 | return NULL; |
| 3633 | } |
| 3634 | } |
| 3635 | |
| 3636 | result = xmlC14NDocSaveTo(doc, |
| 3637 | nodes, |
| 3638 | exclusive, |
| 3639 | prefixes, |
| 3640 | with_comments, |
| 3641 | buf); |
| 3642 | |
| 3643 | if (nodes) { |
| 3644 | xmlFree(nodes->nodeTab); |
| 3645 | xmlFree(nodes); |
| 3646 | } |
| 3647 | if (prefixes) { |
| 3648 | xmlChar ** idx = prefixes; |
| 3649 | while (*idx) xmlFree(*(idx++)); |
| 3650 | xmlFree(prefixes); |
| 3651 | } |
| 3652 | |
| 3653 | len = xmlOutputBufferClose(buf); |
| 3654 | |
| 3655 | if (result < 0) { |
| 3656 | PyErr_SetString(PyExc_Exception, |
| 3657 | "libxml2 xmlC14NDocSaveTo failure."); |
| 3658 | return NULL; |
| 3659 | } |
| 3660 | else |
| 3661 | return PyInt_FromLong((long) len); |
| 3662 | } |
| 3663 | |
| 3664 | #endif |
| 3665 | #endif |
| 3666 | |
William M. Brack | c68d78d | 2004-07-16 10:39:30 +0000 | [diff] [blame] | 3667 | static PyObject * |
| 3668 | libxml_getObjDesc(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) { |
Daniel Veillard | d5e198a | 2004-03-09 09:03:28 +0000 | [diff] [blame] | 3669 | |
William M. Brack | c68d78d | 2004-07-16 10:39:30 +0000 | [diff] [blame] | 3670 | PyObject *obj; |
| 3671 | char *str; |
| 3672 | |
| 3673 | if (!PyArg_ParseTuple(args, (char *)"O:getObjDesc", &obj)) |
| 3674 | return NULL; |
| 3675 | str = PyCObject_GetDesc(obj); |
| 3676 | return Py_BuildValue((char *)"s", str); |
| 3677 | } |
Daniel Veillard | d5e198a | 2004-03-09 09:03:28 +0000 | [diff] [blame] | 3678 | |
Daniel Veillard | a94ec6f | 2002-03-01 13:00:53 +0000 | [diff] [blame] | 3679 | /************************************************************************ |
| 3680 | * * |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 3681 | * The registration stuff * |
| 3682 | * * |
| 3683 | ************************************************************************/ |
| 3684 | static PyMethodDef libxmlMethods[] = { |
Daniel Veillard | 96fe095 | 2002-01-30 20:52:23 +0000 | [diff] [blame] | 3685 | #include "libxml2-export.c" |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 3686 | {(char *) "name", libxml_name, METH_VARARGS, NULL}, |
| 3687 | {(char *) "children", libxml_children, METH_VARARGS, NULL}, |
| 3688 | {(char *) "properties", libxml_properties, METH_VARARGS, NULL}, |
| 3689 | {(char *) "last", libxml_last, METH_VARARGS, NULL}, |
| 3690 | {(char *) "prev", libxml_prev, METH_VARARGS, NULL}, |
| 3691 | {(char *) "next", libxml_next, METH_VARARGS, NULL}, |
| 3692 | {(char *) "parent", libxml_parent, METH_VARARGS, NULL}, |
| 3693 | {(char *) "type", libxml_type, METH_VARARGS, NULL}, |
| 3694 | {(char *) "doc", libxml_doc, METH_VARARGS, NULL}, |
| 3695 | {(char *) "xmlNewNode", libxml_xmlNewNode, METH_VARARGS, NULL}, |
Daniel Veillard | f9cf6f5 | 2005-04-12 01:02:29 +0000 | [diff] [blame] | 3696 | {(char *) "xmlNodeRemoveNsDef", libxml_xmlNodeRemoveNsDef, METH_VARARGS, NULL}, |
Daniel Veillard | 850ce9b | 2004-11-10 11:55:47 +0000 | [diff] [blame] | 3697 | {(char *)"xmlSetValidErrors", libxml_xmlSetValidErrors, METH_VARARGS, NULL}, |
Daniel Veillard | 25c90c5 | 2005-03-02 10:47:41 +0000 | [diff] [blame] | 3698 | {(char *)"xmlFreeValidCtxt", libxml_xmlFreeValidCtxt, METH_VARARGS, NULL}, |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 3699 | #ifdef LIBXML_OUTPUT_ENABLED |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 3700 | {(char *) "serializeNode", libxml_serializeNode, METH_VARARGS, NULL}, |
| 3701 | {(char *) "saveNodeTo", libxml_saveNodeTo, METH_VARARGS, NULL}, |
Daniel Veillard | c6d4a93 | 2002-09-12 15:00:57 +0000 | [diff] [blame] | 3702 | {(char *) "outputBufferCreate", libxml_xmlCreateOutputBuffer, METH_VARARGS, NULL}, |
Daniel Veillard | 6cbd6c0 | 2003-12-04 12:31:49 +0000 | [diff] [blame] | 3703 | {(char *) "outputBufferGetPythonFile", libxml_outputBufferGetPythonFile, METH_VARARGS, NULL}, |
| 3704 | {(char *) "xmlOutputBufferClose", libxml_xmlOutputBufferClose, METH_VARARGS, NULL}, |
| 3705 | { (char *)"xmlOutputBufferFlush", libxml_xmlOutputBufferFlush, METH_VARARGS, NULL }, |
Daniel Veillard | 263ec86 | 2004-10-04 10:26:54 +0000 | [diff] [blame] | 3706 | { (char *)"xmlSaveFileTo", libxml_xmlSaveFileTo, METH_VARARGS, NULL }, |
| 3707 | { (char *)"xmlSaveFormatFileTo", libxml_xmlSaveFormatFileTo, METH_VARARGS, NULL }, |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 3708 | #endif /* LIBXML_OUTPUT_ENABLED */ |
Daniel Veillard | c6d4a93 | 2002-09-12 15:00:57 +0000 | [diff] [blame] | 3709 | {(char *) "inputBufferCreate", libxml_xmlCreateInputBuffer, METH_VARARGS, NULL}, |
| 3710 | {(char *) "setEntityLoader", libxml_xmlSetEntityLoader, METH_VARARGS, NULL}, |
Daniel Veillard | 3e20a29 | 2003-01-10 13:14:40 +0000 | [diff] [blame] | 3711 | {(char *)"xmlRegisterErrorHandler", libxml_xmlRegisterErrorHandler, METH_VARARGS, NULL }, |
Daniel Veillard | 417be3a | 2003-01-20 21:26:34 +0000 | [diff] [blame] | 3712 | {(char *)"xmlParserCtxtSetErrorHandler", libxml_xmlParserCtxtSetErrorHandler, METH_VARARGS, NULL }, |
| 3713 | {(char *)"xmlParserCtxtGetErrorHandler", libxml_xmlParserCtxtGetErrorHandler, METH_VARARGS, NULL }, |
Daniel Veillard | e6227e0 | 2003-01-14 11:42:39 +0000 | [diff] [blame] | 3714 | {(char *)"xmlFreeParserCtxt", libxml_xmlFreeParserCtxt, METH_VARARGS, NULL }, |
Daniel Veillard | 26f7026 | 2003-01-16 22:45:08 +0000 | [diff] [blame] | 3715 | {(char *)"xmlTextReaderSetErrorHandler", libxml_xmlTextReaderSetErrorHandler, METH_VARARGS, NULL }, |
| 3716 | {(char *)"xmlTextReaderGetErrorHandler", libxml_xmlTextReaderGetErrorHandler, METH_VARARGS, NULL }, |
| 3717 | {(char *)"xmlFreeTextReader", libxml_xmlFreeTextReader, METH_VARARGS, NULL }, |
Daniel Veillard | 5439624 | 2003-04-23 07:36:50 +0000 | [diff] [blame] | 3718 | {(char *)"addLocalCatalog", libxml_addLocalCatalog, METH_VARARGS, NULL }, |
Daniel Veillard | c266464 | 2003-07-29 20:44:53 +0000 | [diff] [blame] | 3719 | #ifdef LIBXML_SCHEMAS_ENABLED |
| 3720 | {(char *)"xmlRelaxNGSetValidErrors", libxml_xmlRelaxNGSetValidErrors, METH_VARARGS, NULL}, |
| 3721 | {(char *)"xmlRelaxNGFreeValidCtxt", libxml_xmlRelaxNGFreeValidCtxt, METH_VARARGS, NULL}, |
Daniel Veillard | eff45a9 | 2004-10-29 12:10:55 +0000 | [diff] [blame] | 3722 | {(char *)"xmlSchemaSetValidErrors", libxml_xmlSchemaSetValidErrors, METH_VARARGS, NULL}, |
Daniel Veillard | bb8502c | 2005-03-30 07:40:35 +0000 | [diff] [blame] | 3723 | {(char *)"xmlSchemaFreeValidCtxt", libxml_xmlSchemaFreeValidCtxt, METH_VARARGS, NULL}, |
Daniel Veillard | c266464 | 2003-07-29 20:44:53 +0000 | [diff] [blame] | 3724 | #endif |
Daniel Veillard | d5e198a | 2004-03-09 09:03:28 +0000 | [diff] [blame] | 3725 | #ifdef LIBXML_C14N_ENABLED |
| 3726 | #ifdef LIBXML_OUTPUT_ENABLED |
| 3727 | {(char *)"xmlC14NDocDumpMemory", libxml_C14NDocDumpMemory, METH_VARARGS, NULL}, |
| 3728 | {(char *)"xmlC14NDocSaveTo", libxml_C14NDocSaveTo, METH_VARARGS, NULL}, |
| 3729 | #endif |
| 3730 | #endif |
William M. Brack | c68d78d | 2004-07-16 10:39:30 +0000 | [diff] [blame] | 3731 | {(char *) "getObjDesc", libxml_getObjDesc, METH_VARARGS, NULL}, |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 3732 | {NULL, NULL, 0, NULL} |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 3733 | }; |
| 3734 | |
Daniel Veillard | 0fea6f4 | 2002-02-22 22:51:13 +0000 | [diff] [blame] | 3735 | #ifdef MERGED_MODULES |
Daniel Veillard | 6361da0 | 2002-02-23 10:10:33 +0000 | [diff] [blame] | 3736 | extern void initlibxsltmod(void); |
Daniel Veillard | 0fea6f4 | 2002-02-22 22:51:13 +0000 | [diff] [blame] | 3737 | #endif |
| 3738 | |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 3739 | void |
| 3740 | initlibxml2mod(void) |
| 3741 | { |
Daniel Veillard | af43f63 | 2002-03-08 15:05:20 +0000 | [diff] [blame] | 3742 | static int initialized = 0; |
Daniel Veillard | af43f63 | 2002-03-08 15:05:20 +0000 | [diff] [blame] | 3743 | |
| 3744 | if (initialized != 0) |
Daniel Veillard | d237901 | 2002-03-15 22:24:56 +0000 | [diff] [blame] | 3745 | return; |
William M. Brack | 8e2cc6f | 2004-07-03 23:28:52 +0000 | [diff] [blame] | 3746 | |
Daniel Veillard | f93a866 | 2004-07-01 12:56:30 +0000 | [diff] [blame] | 3747 | /* intialize the python extension module */ |
| 3748 | Py_InitModule((char *) "libxml2mod", libxmlMethods); |
| 3749 | |
| 3750 | /* initialize libxml2 */ |
| 3751 | xmlInitParser(); |
Daniel Veillard | 5d81903 | 2002-02-02 21:49:17 +0000 | [diff] [blame] | 3752 | libxml_xmlErrorInitialize(); |
Daniel Veillard | 6361da0 | 2002-02-23 10:10:33 +0000 | [diff] [blame] | 3753 | |
Daniel Veillard | f93a866 | 2004-07-01 12:56:30 +0000 | [diff] [blame] | 3754 | initialized = 1; |
| 3755 | |
Daniel Veillard | 0fea6f4 | 2002-02-22 22:51:13 +0000 | [diff] [blame] | 3756 | #ifdef MERGED_MODULES |
| 3757 | initlibxsltmod(); |
| 3758 | #endif |
Daniel Veillard | d2897fd | 2002-01-30 16:37:32 +0000 | [diff] [blame] | 3759 | } |