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