Error Buddy

Do you have an error message from your application? Then find the answer with Error Buddy. You can search over 40000 source code files and troubleshooting documents using our beta lucene/nutch search interface or if you prefer, search as normal using google. With LXR technology you can drill right down into the line of source code where it came from with full cross-referencing.

If after searching you didn't get your ideal answer, or you are still unclear what the error means, you can choose to post that question to the community forums following the link included in the search results.

corestack/ libxml2-2.6.19/ SAX2.c [1.6]
001 /*
002  * SAX2.c : Default SAX2 handler to build a tree.
003  *
004  * See Copyright for the status of this software.
005  *
006  * Daniel Veillard <daniel@veillard.com>
007  */
008 
009 
010 #define IN_LIBXML
011 #include "libxml.h"
012 #include <stdlib.h>
013 #include <string.h>
014 #include <libxml/xmlmemory.h>
015 #include <libxml/tree.h>
016 #include <libxml/parser.h>
017 #include <libxml/parserInternals.h>
018 #include <libxml/valid.h>
019 #include <libxml/entities.h>
020 #include <libxml/xmlerror.h>
021 #include <libxml/debugXML.h>
022 #include <libxml/xmlIO.h>
023 #include <libxml/SAX.h>
024 #include <libxml/uri.h>
025 #include <libxml/valid.h>
026 #include <libxml/HTMLtree.h>
027 #include <libxml/globals.h>
028 
029 /* #define DEBUG_SAX2 */
030 /* #define DEBUG_SAX2_TREE */
031 
032 /**
033  * TODO:
034  *
035  * macro to flag unimplemented blocks
036  * XML_CATALOG_PREFER user env to select between system/public prefered
037  * option. C.f. Richard Tobin <richard@cogsci.ed.ac.uk>
038  *> Just FYI, I am using an environment variable XML_CATALOG_PREFER with
039  *> values "system" and "public".  I have made the default be "system" to
040  *> match yours.
041  */
042 #define TODO                                                            \
043     xmlGenericError(xmlGenericErrorContext,                             \
044             "Unimplemented block at %s:%d\n",                           \
045             __FILE__, __LINE__);
046 
047 /*
048  * xmlSAX2ErrMemory:
049  * @ctxt:  an XML validation parser context
050  * @msg:   a string to accompany the error message
051  */
052 static void
053 xmlSAX2ErrMemory(xmlParserCtxtPtr ctxt, const char *msg) {
054     if (ctxt != NULL) {
055         if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
056             ctxt->sax->error(ctxt->userData, "%s: out of memory\n", msg);
057         ctxt->errNo = XML_ERR_NO_MEMORY;
058         ctxt->instate = XML_PARSER_EOF;
059         ctxt->disableSAX = 1;
060     }
061 }
062 
063 /**
064  * xmlValidError:
065  * @ctxt:  an XML validation parser context
066  * @error:  the error number
067  * @msg:  the error message
068  * @str1:  extra data
069  * @str2:  extra data
070  *
071  * Handle a validation error
072  */
073 static void
074 xmlErrValid(xmlParserCtxtPtr ctxt, xmlParserErrors error,
075             const char *msg, const char *str1, const char *str2)
076 {
077     xmlStructuredErrorFunc schannel = NULL;
078 
079     if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
080         (ctxt->instate == XML_PARSER_EOF))
081         return;
082     if (ctxt != NULL) {
083         ctxt->errNo = error;
084         if ((ctxt->sax != NULL) && (ctxt->sax->initialized == XML_SAX2_MAGIC))
085             schannel = ctxt->sax->serror;
086     }
087     __xmlRaiseError(schannel,
088                     ctxt->vctxt.error, ctxt->vctxt.userData,
089                     ctxt, NULL, XML_FROM_DTD, error,
090                     XML_ERR_ERROR, NULL, 0, (const char *) str1,
091                     (const char *) str2, NULL, 0, 0,
092                     msg, (const char *) str1, (const char *) str2);
093     if (ctxt != NULL)
094         ctxt->valid = 0;
095 }
096 
097 /**
098  * xmlSAX2GetPublicId:
099  * @ctx: the user data (XML parser context)
100  *
101  * Provides the public ID e.g. "-//SGMLSOURCE//DTD DEMO//EN"
102  *
103  * Returns a xmlChar *
104  */
105 const xmlChar *
106 xmlSAX2GetPublicId(void *ctx ATTRIBUTE_UNUSED)
107 {
108     /* xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; */
109     return(NULL);
110 }
111 
112 /**
113  * xmlSAX2GetSystemId:
114  * @ctx: the user data (XML parser context)
115  *
116  * Provides the system ID, basically URL or filename e.g.
117  * http://www.sgmlsource.com/dtds/memo.dtd
118  *
119  * Returns a xmlChar *
120  */
121 const xmlChar *
122 xmlSAX2GetSystemId(void *ctx)
123 {
124     xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
125     if ((ctx == NULL) || (ctxt->input == NULL)) return(0);
126     return((const xmlChar *) ctxt->input->filename); 
127 }
128 
129 /**
130  * xmlSAX2GetLineNumber:
131  * @ctx: the user data (XML parser context)
132  *
133  * Provide the line number of the current parsing point.
134  *
135  * Returns an int
136  */
137 int
138 xmlSAX2GetLineNumber(void *ctx)
139 {
140     xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
141     if ((ctx == NULL) || (ctxt->input == NULL)) return(0);
142     return(ctxt->input->line);
143 }
144 
145 /**
146  * xmlSAX2GetColumnNumber:
147  * @ctx: the user data (XML parser context)
148  *
149  * Provide the column number of the current parsing point.
150  *
151  * Returns an int
152  */
153 int
154 xmlSAX2GetColumnNumber(void *ctx)
155 {
156     xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
157     if ((ctx == NULL) || (ctxt->input == NULL)) return(0);
158     return(ctxt->input->col);
159 }
160 
161 /**
162  * xmlSAX2IsStandalone:
163  * @ctx: the user data (XML parser context)
164  *
165  * Is this document tagged standalone ?
166  *
167  * Returns 1 if true
168  */
169 int
170 xmlSAX2IsStandalone(void *ctx)
171 {
172     xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
173     if ((ctx == NULL) || (ctxt->myDoc == NULL)) return(0);
174     return(ctxt->myDoc->standalone == 1);
175 }
176 
177 /**
178  * xmlSAX2HasInternalSubset:
179  * @ctx: the user data (XML parser context)
180  *
181  * Does this document has an internal subset
182  *
183  * Returns 1 if true
184  */
185 int
186 xmlSAX2HasInternalSubset(void *ctx)
187 {
188     xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
189     if ((ctxt == NULL) || (ctxt->myDoc == NULL)) return(0);
190     return(ctxt->myDoc->intSubset != NULL);
191 }
192 
193 /**
194  * xmlSAX2HasExternalSubset:
195  * @ctx: the user data (XML parser context)
196  *
197  * Does this document has an external subset
198  *
199  * Returns 1 if true
200  */
201 int
202 xmlSAX2HasExternalSubset(void *ctx)
203 {
204     xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
205     if ((ctxt == NULL) || (ctxt->myDoc == NULL)) return(0);
206     return(ctxt->myDoc->extSubset != NULL);
207 }
208 
209 /**
210  * xmlSAX2InternalSubset:
211  * @ctx:  the user data (XML parser context)
212  * @name:  the root element name
213  * @ExternalID:  the external ID
214  * @SystemID:  the SYSTEM ID (e.g. filename or URL)
215  *
216  * Callback on internal subset declaration.
217  */
218 void
219 xmlSAX2InternalSubset(void *ctx, const xmlChar *name,
220                const xmlChar *ExternalID, const xmlChar *SystemID)
221 {
222     xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
223     xmlDtdPtr dtd;
224     if (ctx == NULL) return;
225 #ifdef DEBUG_SAX
226     xmlGenericError(xmlGenericErrorContext,
227             "SAX.xmlSAX2InternalSubset(%s, %s, %s)\n",
228             name, ExternalID, SystemID);
229 #endif
230 
231     if (ctxt->myDoc == NULL)
232         return;
233     dtd = xmlGetIntSubset(ctxt->myDoc);
234     if (dtd != NULL) {
235         if (ctxt->html)
236             return;
237         xmlUnlinkNode((xmlNodePtr) dtd);
238         xmlFreeDtd(dtd);
239         ctxt->myDoc->intSubset = NULL;
240     }
241     ctxt->myDoc->intSubset = 
242         xmlCreateIntSubset(ctxt->myDoc, name, ExternalID, SystemID);
243     if (ctxt->myDoc->intSubset == NULL)
244         xmlSAX2ErrMemory(ctxt, "xmlSAX2InternalSubset");
245 }
246 
247 /**
248  * xmlSAX2ExternalSubset:
249  * @ctx: the user data (XML parser context)
250  * @name:  the root element name
251  * @ExternalID:  the external ID
252  * @SystemID:  the SYSTEM ID (e.g. filename or URL)
253  *
254  * Callback on external subset declaration.
255  */
256 void
257 xmlSAX2ExternalSubset(void *ctx, const xmlChar *name,
258                const xmlChar *ExternalID, const xmlChar *SystemID)
259 {
260     xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
261     if (ctx == NULL) return;
262 #ifdef DEBUG_SAX
263     xmlGenericError(xmlGenericErrorContext,
264             "SAX.xmlSAX2ExternalSubset(%s, %s, %s)\n",
265             name, ExternalID, SystemID);
266 #endif
267     if (((ExternalID != NULL) || (SystemID != NULL)) &&
268         (((ctxt->validate) || (ctxt->loadsubset != 0)) &&
269          (ctxt->wellFormed && ctxt->myDoc))) {
270         /*
271          * Try to fetch and parse the external subset.
272          */
273         xmlParserInputPtr oldinput;
274         int oldinputNr;
275         int oldinputMax;
276         xmlParserInputPtr *oldinputTab;
277         xmlParserInputPtr input = NULL;
278         xmlCharEncoding enc;
279         int oldcharset;
280 
281         /*
282          * Ask the Entity resolver to load the damn thing
283          */
284         if ((ctxt->sax != NULL) && (ctxt->sax->resolveEntity != NULL))
285             input = ctxt->sax->resolveEntity(ctxt->userData, ExternalID,
286                                                 SystemID);
287         if (input == NULL) {
288             return;
289         }
290 
291         xmlNewDtd(ctxt->myDoc, name, ExternalID, SystemID);
292 
293         /*
294          * make sure we won't destroy the main document context
295          */
296         oldinput = ctxt->input;
297         oldinputNr = ctxt->inputNr;
298         oldinputMax = ctxt->inputMax;
299         oldinputTab = ctxt->inputTab;
300         oldcharset = ctxt->charset;
301 
302         ctxt->inputTab = (xmlParserInputPtr *)
303                          xmlMalloc(5 * sizeof(xmlParserInputPtr));
304         if (ctxt->inputTab == NULL) {
305             xmlSAX2ErrMemory(ctxt, "xmlSAX2ExternalSubset");
306             ctxt->input = oldinput;
307             ctxt->inputNr = oldinputNr;
308             ctxt->inputMax = oldinputMax;
309             ctxt->inputTab = oldinputTab;
310             ctxt->charset = oldcharset;
311             return;
312         }
313         ctxt->inputNr = 0;
314         ctxt->inputMax = 5;
315         ctxt->input = NULL;
316         xmlPushInput(ctxt, input);
317 
318         /*
319          * On the fly encoding conversion if needed
320          */
321         if (ctxt->input->length >= 4) {
322             enc = xmlDetectCharEncoding(ctxt->input->cur, 4);
323             xmlSwitchEncoding(ctxt, enc);
324         }
325 
326         if (input->filename == NULL)
327             input->filename = (char *) xmlCanonicPath(SystemID);
328         input->line = 1;
329         input->col = 1;
330         input->base = ctxt->input->cur;
331         input->cur = ctxt->input->cur;
332         input->free = NULL;
333 
334         /*
335          * let's parse that entity knowing it's an external subset.
336          */
337         xmlParseExternalSubset(ctxt, ExternalID, SystemID);
338 
339         /*
340          * Free up the external entities
341          */
342 
343         while (ctxt->inputNr > 1)
344             xmlPopInput(ctxt);
345         xmlFreeInputStream(ctxt->input);
346         xmlFree(ctxt->inputTab);
347 
348         /*
349          * Restore the parsing context of the main entity
350          */
351         ctxt->input = oldinput;
352         ctxt->inputNr = oldinputNr;
353         ctxt->inputMax = oldinputMax;
354         ctxt->inputTab = oldinputTab;
355         ctxt->charset = oldcharset;
356         /* ctxt->wellFormed = oldwellFormed; */
357     }
358 }
359 
360 /**
361  * xmlSAX2ResolveEntity:
362  * @ctx: the user data (XML parser context)
363  * @publicId: The public ID of the entity
364  * @systemId: The system ID of the entity
365  *
366  * The entity loader, to control the loading of external entities,
367  * the application can either:
368  *    - override this xmlSAX2ResolveEntity() callback in the SAX block
369  *    - or better use the xmlSetExternalEntityLoader() function to
370  *      set up it's own entity resolution routine
371  *
372  * Returns the xmlParserInputPtr if inlined or NULL for DOM behaviour.
373  */
374 xmlParserInputPtr
375 xmlSAX2ResolveEntity(void *ctx, const xmlChar *publicId, const xmlChar *systemId)
376 {
377     xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
378     xmlParserInputPtr ret;
379     xmlChar *URI;
380     const char *base = NULL;
381 
382     if (ctx == NULL) return(NULL);
383     if (ctxt->input != NULL)
384         base = ctxt->input->filename;
385     if (base == NULL)
386         base = ctxt->directory;
387 
388     URI = xmlBuildURI(systemId, (const xmlChar *) base);
389 
390 #ifdef DEBUG_SAX
391     xmlGenericError(xmlGenericErrorContext,
392             "SAX.xmlSAX2ResolveEntity(%s, %s)\n", publicId, systemId);
393 #endif
394 
395     ret = xmlLoadExternalEntity((const char *) URI,
396                                 (const char *) publicId, ctxt);
397     if (URI != NULL)
398         xmlFree(URI);
399     return(ret);
400 }
401 
402 /**
403  * xmlSAX2GetEntity:
404  * @ctx: the user data (XML parser context)
405  * @name: The entity name
406  *
407  * Get an entity by name
408  *
409  * Returns the xmlEntityPtr if found.
410  */
411 xmlEntityPtr
412 xmlSAX2GetEntity(void *ctx, const xmlChar *name)
413 {
414     xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
415     xmlEntityPtr ret = NULL;
416 
417     if (ctx == NULL) return(NULL);
418 #ifdef DEBUG_SAX
419     xmlGenericError(xmlGenericErrorContext,
420             "SAX.xmlSAX2GetEntity(%s)\n", name);
421 #endif
422 
423     if (ctxt->inSubset == 0) {
424         ret = xmlGetPredefinedEntity(name);
425         if (ret != NULL)
426             return(ret);
427     }
428     if ((ctxt->myDoc != NULL) && (ctxt->myDoc->standalone == 1)) {
429         if (ctxt->inSubset == 2) {
430             ctxt->myDoc->standalone = 0;
431             ret = xmlGetDocEntity(ctxt->myDoc, name);
432             ctxt->myDoc->standalone = 1;
433         } else {
434             ret = xmlGetDocEntity(ctxt->myDoc, name);
435             if (ret == NULL) {
436                 ctxt->myDoc->standalone = 0;
437                 ret = xmlGetDocEntity(ctxt->myDoc, name);
438                 if (ret != NULL) {
439                     if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
440                         ctxt->sax->error(ctxt->userData, 
441                  "Entity(%s) document marked standalone but requires external subset\n",
442                                          name);
443                     ctxt->valid = 0;
444                     ctxt->wellFormed = 0;
445                 }
446                 ctxt->myDoc->standalone = 1;
447             }
448         }
449     } else {
450         ret = xmlGetDocEntity(ctxt->myDoc, name);
451     }
452     if ((ret != NULL) &&
453         ((ctxt->validate) || (ctxt->replaceEntities)) &&
454         (ret->children == NULL) &&
455         (ret->etype == XML_EXTERNAL_GENERAL_PARSED_ENTITY)) {
456         int val;
457 
458         /*
459          * for validation purposes we really need to fetch and
460          * parse the external entity
461          */
462         xmlNodePtr children;
463 
464         val = xmlParseCtxtExternalEntity(ctxt, ret->URI,
465                                          ret->ExternalID, &children);
466         if (val == 0) {
467             xmlAddChildList((xmlNodePtr) ret, children);
468         } else {
469             if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
470                 ctxt->sax->error(ctxt->userData, 
471                  "Failure to process entity %s\n", name);
472             ctxt->wellFormed = 0;
473             ctxt->valid = 0;
474             ctxt->validate = 0;
475             return(NULL);
476         }
477         ret->owner = 1;
478     }
479     return(ret);
480 }
481 
482 /**
483  * xmlSAX2GetParameterEntity:
484  * @ctx: the user data (XML parser context)
485  * @name: The entity name
486  *
487  * Get a parameter entity by name
488  *
489  * Returns the xmlEntityPtr if found.
490  */
491 xmlEntityPtr
492 xmlSAX2GetParameterEntity(void *ctx, const xmlChar *name)
493 {
494     xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
495     xmlEntityPtr ret;
496 
497     if (ctx == NULL) return(NULL);
498 #ifdef DEBUG_SAX
499     xmlGenericError(xmlGenericErrorContext,
500             "SAX.xmlSAX2GetParameterEntity(%s)\n", name);
501 #endif
502 
503     ret = xmlGetParameterEntity(ctxt->myDoc, name);
504     return(ret);
505 }
506 
507 
508 /**
509  * xmlSAX2EntityDecl:
510  * @ctx: the user data (XML parser context)
511  * @name:  the entity name 
512  * @type:  the entity type 
513  * @publicId: The public ID of the entity
514  * @systemId: The system ID of the entity
515  * @content: the entity value (without processing).
516  *
517  * An entity definition has been parsed
518  */
519 void
520 xmlSAX2EntityDecl(void *ctx, const xmlChar *name, int type,
521           const xmlChar *publicId, const xmlChar *systemId, xmlChar *content)
522 {
523     xmlEntityPtr ent;
524     xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
525 
526     if (ctx == NULL) return;
527 #ifdef DEBUG_SAX
528     xmlGenericError(xmlGenericErrorContext,
529             "SAX.xmlSAX2EntityDecl(%s, %d, %s, %s, %s)\n",
530             name, type, publicId, systemId, content);
531 #endif
532     if (ctxt->inSubset == 1) {
533         ent = xmlAddDocEntity(ctxt->myDoc, name, type, publicId,
534                               systemId, content);
535         if ((ent == NULL) && (ctxt->pedantic) &&
536             (ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
537             ctxt->sax->warning(ctxt->userData, 
538              "Entity(%s) already defined in the internal subset\n", name);
539         if ((ent != NULL) && (ent->URI == NULL) && (systemId != NULL)) {
540             xmlChar *URI;
541             const char *base = NULL;
542 
543             if (ctxt->input != NULL)
544                 base = ctxt->input->filename;
545             if (base == NULL)
546                 base = ctxt->directory;
547         
548             URI = xmlBuildURI(systemId, (const xmlChar *) base);
549             ent->URI = URI;
550         }
551     } else if (ctxt->inSubset == 2) {
552         ent = xmlAddDtdEntity(ctxt->myDoc, name, type, publicId,
553                               systemId, content);
554         if ((ent == NULL) && (ctxt->pedantic) &&
555             (ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
556             ctxt->sax->warning(ctxt->userData, 
557              "Entity(%s) already defined in the external subset\n", name);
558         if ((ent != NULL) && (ent->URI == NULL) && (systemId != NULL)) {
559             xmlChar *URI;
560             const char *base = NULL;
561 
562             if (ctxt->input != NULL)
563                 base = ctxt->input->filename;
564             if (base == NULL)
565                 base = ctxt->directory;
566         
567             URI = xmlBuildURI(systemId, (const xmlChar *) base);
568             ent->URI = URI;
569         }
570     } else {
571         if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
572             ctxt->sax->error(ctxt->userData, 
573              "SAX.xmlSAX2EntityDecl(%s) called while not in subset\n", name);
574     }
575 }
576 
577 /**
578  * xmlSAX2AttributeDecl:
579  * @ctx: the user data (XML parser context)
580  * @elem:  the name of the element
581  * @fullname:  the attribute name 
582  * @type:  the attribute type 
583  * @def:  the type of default value
584  * @defaultValue: the attribute default value
585  * @tree:  the tree of enumerated value set
586  *
587  * An attribute definition has been parsed
588  */
589 void
590 xmlSAX2AttributeDecl(void *ctx, const xmlChar *elem, const xmlChar *fullname,
591               int type, int def, const xmlChar *defaultValue,
592               xmlEnumerationPtr tree)
593 {
594     xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
595     xmlAttributePtr attr;
596     xmlChar *name = NULL, *prefix = NULL;
597 
598     if (ctx == NULL) return;
599 #ifdef DEBUG_SAX
600     xmlGenericError(xmlGenericErrorContext,
601             "SAX.xmlSAX2AttributeDecl(%s, %s, %d, %d, %s, ...)\n",
602             elem, fullname, type, def, defaultValue);
603 #endif
604     if ((xmlStrEqual(fullname, BAD_CAST "xml:id")) &&
605         (type != XML_ATTRIBUTE_ID)) {
606         /*
607          * Raise the error but keep the validity flag
608          */
609         int tmp = ctxt->valid;
610         xmlErrValid(