From cd6ff282117f1fe46560384e048beb96990e4620 Mon Sep 17 00:00:00 2001 From: Daniel Veillard Date: Wed, 8 Oct 2003 22:38:13 +0000 Subject: [PATCH] final error handling cleanup converted XInclude to the new error handling * xmlIO.c: final error handling cleanup * xinclude.c error.c: converted XInclude to the new error handling * include/libxml/xmlerror.h: added XInclude errors Daniel --- ChangeLog | 6 + doc/libxml2-api.xml | 838 ++++++++++++++++++++++++++++++++++++-- error.c | 9 +- include/libxml/xmlerror.h | 20 +- python/libxml2class.txt | 4 +- xinclude.c | 369 +++++++---------- xmlIO.c | 50 ++- 7 files changed, 1013 insertions(+), 283 deletions(-) diff --git a/ChangeLog b/ChangeLog index 671670ec..6ec9d9e5 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +Thu Oct 9 00:36:03 CEST 2003 Daniel Veillard + + * xmlIO.c: final error handling cleanup + * xinclude.c error.c: converted XInclude to the new error handling + * include/libxml/xmlerror.h: added XInclude errors + Wed Oct 8 23:31:23 CEST 2003 Daniel Veillard * parser.c: bug in compression saving was crashing galeon diff --git a/doc/libxml2-api.xml b/doc/libxml2-api.xml index f92587fc..c5b43015 100644 --- a/doc/libxml2-api.xml +++ b/doc/libxml2-api.xml @@ -137,6 +137,7 @@ + @@ -154,7 +155,6 @@ - @@ -308,6 +308,7 @@ + @@ -670,6 +671,44 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -894,6 +933,7 @@ + @@ -1160,6 +1200,7 @@ + @@ -1209,6 +1250,7 @@ + @@ -1681,6 +1723,44 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -1725,7 +1805,6 @@ - @@ -1734,13 +1813,11 @@ - - @@ -1754,7 +1831,6 @@ - @@ -1782,19 +1858,241 @@ - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -1813,8 +2111,10 @@ + + @@ -2505,6 +2805,9 @@ the version number string, 1.2.3 value is "1002003" + + Whether the xmlDocWalker interface is configured in + Whether XInclude is configured in @@ -2835,7 +3138,49 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -2867,7 +3212,7 @@ - + @@ -2898,7 +3243,6 @@ - @@ -2907,13 +3251,11 @@ - - @@ -2927,7 +3269,6 @@ - @@ -2953,28 +3294,94 @@ - + - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - + + + + @@ -3035,6 +3442,7 @@ + @@ -3075,6 +3483,132 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -3142,10 +3676,39 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -3331,6 +3894,9 @@ actually an xmlCharEncoding'/> + + + @@ -3422,8 +3988,11 @@ actually an xmlCharEncoding'/> + + + @@ -3531,6 +4100,7 @@ actually an xmlCharEncoding'/> + @@ -3616,7 +4186,9 @@ actually an xmlCharEncoding'/> - + + @@ -3648,6 +4220,7 @@ actually an xmlCharEncoding'/> + @@ -3981,7 +4554,11 @@ actually an xmlCharEncoding'/> - + + + + + @@ -4516,7 +5093,7 @@ actually an xmlCharEncoding'/> Reset a parser context - + Applies the options to the parser context @@ -4688,7 +5265,7 @@ actually an xmlCharEncoding'/> Parse a Chunk of memory - + @@ -5854,10 +6431,10 @@ actually an xmlCharEncoding'/> - - - - + Save the original error to the new place. + + + Do a copy of the namespace. @@ -5961,9 +6538,9 @@ actually an xmlCharEncoding'/> - - - + Get the last parsing error registered. + + parse an XML in-memory document and build a tree. This reuses the existing @ctxt parser context @@ -6018,9 +6595,9 @@ actually an xmlCharEncoding'/> - + Cleanup the last global error registered. For parsing error this does not change the well-formedness result. - + Applies the options to the parser context @@ -6236,6 +6813,150 @@ actually an xmlCharEncoding'/> + + Provides the number of attributes of the current node + + + + + + + + + + Hacking interface allowing to get the xmlDocPtr correponding to the current document being accessed by the xmlDocWalker. + + + + + Hacking interface allowing to get the xmlNodePtr correponding to the current node being accessed by the xmlDocWalker. + + + + + The depth of the node in the tree. + + + + + Provides the value of the attribute with the specified qualified name. + + + + + + Provides the value of the attribute with the specified index relative to the containing element. + + + + + + Provides the value of the specified attribute + + + + + + + Whether the node has attributes. + + + + + Whether the node can have a text value. + + + + + Check if the current node is empty + + + + + The local name of the node. + + + + + Resolves a namespace prefix in the scope of the current element. + + + + + + Moves the position of the current instance to the attribute with the specified qualified name. + + + + + + Moves the position of the current instance to the attribute with the specified index relative to the containing element. + + + + + + Moves the position of the current instance to the attribute with the specified local name and namespace URI. + + + + + + + Moves the position of the current instance to the node that contains the current Attribute node. + + + + + Moves the position of the current instance to the first attribute associated with the current node. + + + + + Moves the position of the current instance to the next attribute associated with the current node. + + + + + The qualified name of the node, equal to Prefix :LocalName. + + + + + The URI defining the namespace associated with the node. + + + + + Step to the next sibling of the current node in document order + + + + + Get the node type of the current node Reference: http://dotgnu.org/pnetlib-doc/System/Xml/XmlNodeType.html + + + + + A shorthand reference to the namespace associated with the node. + + + + + Initializes the xmlDocWalker + + + + + Steps through the xml tree + + + + + Provides the text value of the node if present + + + This will dump the content of the attribute declaration as an XML DTD definition @@ -6316,6 +7037,12 @@ actually an xmlCharEncoding'/> + + Handle a redefinition of attribute error + + + + External entity loaders types. @@ -6370,6 +7097,11 @@ actually an xmlCharEncoding'/> + + Deallocate the xmlDocWalker + + + Free a DTD structure. @@ -6622,8 +7354,8 @@ actually an xmlCharEncoding'/> - - + Get the last global error registered. This is per thread if compiled with thread support. + Get line number of node. this requires activation of this option before invoking the parser by calling xmlLineNumbersDefault(1) @@ -7747,6 +8479,11 @@ actually an xmlCharEncoding'/> + + Creates a new instance of the xmlDocWalker + + + Creation of a new DTD for the external subset. To create an internal subset, use xmlCreateIntSubset(). @@ -7837,7 +8574,7 @@ actually an xmlCharEncoding'/> - + Allocate and initialize a new parser context. @@ -8978,8 +9715,13 @@ actually an xmlCharEncoding'/> + + Cleanup the error. + + + - + Cleanup the last global error registered. For parsing error this does not change the well-formedness result. @@ -9783,6 +10525,14 @@ actually an xmlCharEncoding'/> + + Formats @msg and places result into @buf. + + + + + + Check if a QName is Equal to a given string @@ -9916,6 +10666,12 @@ actually an xmlCharEncoding'/> + + Signature of the function to use when there is an error and the module handles the new error reporting mechanism. + + + + Set and return the previous value for default entity support. Initially the parser always keep entity references instead of substituting entity values in the output. This function has to be used to change the default parser behavior SAX::substituteEntities() has to be used for changing that on a file by file basis. @@ -11512,6 +12268,12 @@ actually an xmlCharEncoding'/> + + Handle a Relax NG Parsing error + + + + Evaluate the XPath Location Path in the given context. diff --git a/error.c b/error.c index cda871a8..a8455e17 100644 --- a/error.c +++ b/error.c @@ -417,13 +417,18 @@ __xmlRaiseError(xmlGenericErrorFunc channel, void *data, void *ctx, const char *str2, const char *str3, int int1, int int2, const char *msg, ...) { - xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; + xmlParserCtxtPtr ctxt = NULL; xmlNodePtr node = (xmlNodePtr) nod; char *str = NULL; xmlParserInputPtr input = NULL; xmlErrorPtr to = &xmlLastError; xmlChar *base = NULL; + if ((domain == XML_FROM_PARSER) || (domain == XML_FROM_HTML) || + (domain == XML_FROM_DTD) || (domain == XML_FROM_NAMESPACE) || + (domain == XML_FROM_IO)) { + ctxt = (xmlParserCtxtPtr) ctx; + } if (code == XML_ERR_OK) return; /* @@ -492,7 +497,7 @@ __xmlRaiseError(xmlGenericErrorFunc channel, void *data, void *ctx, to->int1 = int1; to->int2 = int2; to->node = node; - to->ctxt = ctxt; + to->ctxt = ctx; /* * Find the callback channel. diff --git a/include/libxml/xmlerror.h b/include/libxml/xmlerror.h index cec93f8f..b3ba2857 100644 --- a/include/libxml/xmlerror.h +++ b/include/libxml/xmlerror.h @@ -454,7 +454,25 @@ typedef enum { XML_IO_FLUSH, XML_IO_WRITE, XML_IO_NO_INPUT, - XML_IO_BUFFER_FULL + XML_IO_BUFFER_FULL, + XML_IO_LOAD_ERROR, + XML_XINCLUDE_RECURSION=1600, + XML_XINCLUDE_PARSE_VALUE, + XML_XINCLUDE_ENTITY_DEF_MISMATCH, + XML_XINCLUDE_NO_HREF, + XML_XINCLUDE_NO_FALLBACK, + XML_XINCLUDE_HREF_URI, + XML_XINCLUDE_TEXT_FRAGMENT, + XML_XINCLUDE_TEXT_DOCUMENT, + XML_XINCLUDE_INVALID_CHAR, + XML_XINCLUDE_BUILD_FAILED, + XML_XINCLUDE_UNKNOWN_ENCODING, + XML_XINCLUDE_MULTIPLE_ROOT, + XML_XINCLUDE_XPTR_FAILED, + XML_XINCLUDE_XPTR_RESULT, + XML_XINCLUDE_INCLUDE_IN_INCLUDE, + XML_XINCLUDE_FALLBACKS_IN_INCLUDE, + XML_XINCLUDE_FALLBACK_NOT_IN_INCLUDE } xmlParserErrors; /** diff --git a/python/libxml2class.txt b/python/libxml2class.txt index 1c6e3985..2f37fb31 100644 --- a/python/libxml2class.txt +++ b/python/libxml2class.txt @@ -108,6 +108,7 @@ createDocParserCtxt() initParser() keepBlanksDefault() lineNumbersDefault() +newParserCtxt() parseDTD() parseDoc() parseEntity() @@ -143,7 +144,6 @@ isLetter() isPubidChar() namePop() namePush() -newParserCtxt() nodePop() nodePush() @@ -754,6 +754,7 @@ Class xpathParserContext() xpathCountFunction() xpathDivValues() xpathEqualValues() + xpathErr() xpathEvalExpr() xpathFalseFunction() xpathFloorFunction() @@ -849,6 +850,7 @@ Class parserCtxt(parserCtxtCore) # functions from module parserInternals decodeEntities() + errMemory() handleEntity() namespaceParseNCName() namespaceParseNSDef() diff --git a/xinclude.c b/xinclude.c index ef535420..8a336dc2 100644 --- a/xinclude.c +++ b/xinclude.c @@ -94,71 +94,49 @@ static int xmlXIncludeDoProcess(xmlXIncludeCtxtPtr ctxt, xmlDocPtr doc, xmlNodePtr tree); +/************************************************************************ + * * + * Tree memory error handler * + * * + ************************************************************************/ + /** - * xmlXIncludeErrorContext: - * @ctxt: the XInclude context - * @node: the node + * xmlXIncludeErrMemory: + * @extra: extra informations * - * Dump informations about the kocation of the error in the instance + * Handle an out of memory condition */ static void -xmlXIncludeErrorContext(xmlXIncludeCtxtPtr ctxt ATTRIBUTE_UNUSED, - xmlNodePtr node) +xmlXIncludeErrMemory(xmlXIncludeCtxtPtr ctxt, xmlNodePtr node, + const char *extra) { - int line = 0; - const xmlChar *file = NULL; - const xmlChar *name = NULL; - const char *type = "error"; + if (ctxt != NULL) + ctxt->nbErrors++; + __xmlRaiseError(NULL, NULL, ctxt, node, XML_FROM_XINCLUDE, + XML_ERR_NO_MEMORY, XML_ERR_ERROR, NULL, 0, + extra, NULL, NULL, 0, 0, + "Memory allocation failed : %s\n", extra); +} - if (node == NULL) { - return; - } - if (node != NULL) { - if ((node->type == XML_DOCUMENT_NODE) || - (node->type == XML_HTML_DOCUMENT_NODE)) { - xmlDocPtr doc = (xmlDocPtr) node; - - file = doc->URL; - } else { - /* - * Try to find contextual informations to report - */ - if (node->type == XML_ELEMENT_NODE) { - line = (long) node->content; - } else if ((node->prev != NULL) && - (node->prev->type == XML_ELEMENT_NODE)) { - line = (long) node->prev->content; - } else if ((node->parent != NULL) && - (node->parent->type == XML_ELEMENT_NODE)) { - line = (long) node->parent->content; - } - if ((node->doc != NULL) && (node->doc->URL != NULL)) - file = node->doc->URL; - if (node->name != NULL) - name = node->name; - } - } - - type = "XInclude :"; - - if ((file != NULL) && (line != 0) && (name != NULL)) - xmlGenericError(xmlGenericErrorContext, - "%s: file %s line %d element %s\n", type, file, - line, name); - else if ((file != NULL) && (name != NULL)) - xmlGenericError(xmlGenericErrorContext, "%s: file %s element %s\n", - type, file, name); - else if ((file != NULL) && (line != 0)) - xmlGenericError(xmlGenericErrorContext, "%s: file %s line %d\n", - type, file, line); - else if (file != NULL) - xmlGenericError(xmlGenericErrorContext, "%s: file %s\n", type, - file); - else if (name != NULL) - xmlGenericError(xmlGenericErrorContext, "%s: element %s\n", type, - name); - else - xmlGenericError(xmlGenericErrorContext, "%s\n", type); +/** + * xmlXIncludeErr: + * @ctxt: the XInclude context + * @node: the context node + * @msg: the error message + * @extra: extra informations + * + * Handle a resource access error + */ +static void +xmlXIncludeErr(xmlXIncludeCtxtPtr ctxt, xmlNodePtr node, int error, + const char *msg, const xmlChar *extra) +{ + if (ctxt != NULL) + ctxt->nbErrors++; + __xmlRaiseError(NULL, NULL, ctxt, node, XML_FROM_XINCLUDE, + error, XML_ERR_ERROR, NULL, 0, + (const char *) extra, NULL, NULL, 0, 0, + msg, (const char *) extra); } /** @@ -207,8 +185,10 @@ xmlXIncludeNewRef(xmlXIncludeCtxtPtr ctxt, const xmlChar *URI, xmlGenericError(xmlGenericErrorContext, "New ref %s\n", URI); #endif ret = (xmlXIncludeRefPtr) xmlMalloc(sizeof(xmlXIncludeRef)); - if (ret == NULL) + if (ret == NULL) { + xmlXIncludeErrMemory(ctxt, ref, "growing XInclude context"); return(NULL); + } memset(ret, 0, sizeof(xmlXIncludeRef)); if (URI == NULL) ret->URI = NULL; @@ -225,10 +205,7 @@ xmlXIncludeNewRef(xmlXIncludeCtxtPtr ctxt, const xmlChar *URI, ctxt->incTab = (xmlXIncludeRefPtr *) xmlMalloc(ctxt->incMax * sizeof(ctxt->incTab[0])); if (ctxt->incTab == NULL) { - xmlXIncludeErrorContext(ctxt, NULL); - xmlGenericError(xmlGenericErrorContext, - "malloc failed !\n"); - ctxt->nbErrors++; + xmlXIncludeErrMemory(ctxt, ref, "growing XInclude context"); xmlXIncludeFreeRef(ret); return(NULL); } @@ -238,9 +215,7 @@ xmlXIncludeNewRef(xmlXIncludeCtxtPtr ctxt, const xmlChar *URI, ctxt->incTab = (xmlXIncludeRefPtr *) xmlRealloc(ctxt->incTab, ctxt->incMax * sizeof(ctxt->incTab[0])); if (ctxt->incTab == NULL) { - xmlXIncludeErrorContext(ctxt, NULL); - xmlGenericError(xmlGenericErrorContext, - "realloc failed !\n"); + xmlXIncludeErrMemory(ctxt, ref, "growing XInclude context"); xmlXIncludeFreeRef(ret); return(NULL); } @@ -267,8 +242,11 @@ xmlXIncludeNewContext(xmlDocPtr doc) { if (doc == NULL) return(NULL); ret = (xmlXIncludeCtxtPtr) xmlMalloc(sizeof(xmlXIncludeCtxt)); - if (ret == NULL) + if (ret == NULL) { + xmlXIncludeErrMemory(NULL, (xmlNodePtr) doc, + "creating XInclude context"); return(NULL); + } memset(ret, 0, sizeof(xmlXIncludeCtxt)); ret->doc = doc; ret->incNr = 0; @@ -293,11 +271,8 @@ xmlXIncludeURLPush(xmlXIncludeCtxtPtr ctxt, const xmlChar *value) { if (ctxt->urlNr > XINCLUDE_MAX_DEPTH) { - xmlXIncludeErrorContext(ctxt, NULL); - xmlGenericError(xmlGenericErrorContext, - "XInclude: detected a recursion in %s\n", - value); - ctxt->nbErrors++; + xmlXIncludeErr(ctxt, NULL, XML_XINCLUDE_RECURSION, + "detected a recursion in %s\n", value); return(-1); } if (ctxt->urlTab == NULL) { @@ -306,8 +281,7 @@ xmlXIncludeURLPush(xmlXIncludeCtxtPtr ctxt, ctxt->urlTab = (xmlChar * *) xmlMalloc( ctxt->urlMax * sizeof(ctxt->urlTab[0])); if (ctxt->urlTab == NULL) { - xmlXIncludeErrorContext(ctxt, NULL); - xmlGenericError(xmlGenericErrorContext, "malloc failed !\n"); + xmlXIncludeErrMemory(ctxt, NULL, "adding URL"); return (-1); } } @@ -318,8 +292,7 @@ xmlXIncludeURLPush(xmlXIncludeCtxtPtr ctxt, ctxt->urlMax * sizeof(ctxt->urlTab[0])); if (ctxt->urlTab == NULL) { - xmlXIncludeErrorContext(ctxt, NULL); - xmlGenericError(xmlGenericErrorContext, "realloc failed !\n"); + xmlXIncludeErrMemory(ctxt, NULL, "adding URL"); return (-1); } } @@ -464,9 +437,8 @@ xmlXIncludeAddNode(xmlXIncludeCtxtPtr ctxt, xmlNodePtr cur) { if (href == NULL) { href = xmlGetProp(cur, XINCLUDE_HREF); if (href == NULL) { - xmlXIncludeErrorContext(ctxt, cur); - xmlGenericError(xmlGenericErrorContext, "XInclude: no href\n"); - ctxt->nbErrors++; + xmlXIncludeErr(ctxt, cur, XML_XINCLUDE_NO_HREF, + "no href\n", NULL); return(-1); } } @@ -482,11 +454,8 @@ xmlXIncludeAddNode(xmlXIncludeCtxtPtr ctxt, xmlNodePtr cur) { else if (xmlStrEqual(parse, XINCLUDE_PARSE_TEXT)) xml = 0; else { - xmlXIncludeErrorContext(ctxt, cur); - xmlGenericError(xmlGenericErrorContext, - "XInclude: invalid value %s for %s\n", - parse, XINCLUDE_PARSE); - ctxt->nbErrors++; + xmlXIncludeErr(ctxt, cur, XML_XINCLUDE_PARSE_VALUE, + "invalid value %s for 'parse'\n", parse); if (href != NULL) xmlFree(href); if (parse != NULL) @@ -525,9 +494,8 @@ xmlXIncludeAddNode(xmlXIncludeCtxtPtr ctxt, xmlNodePtr cur) { if (base != NULL) xmlFree(base); if (URI == NULL) { - xmlXIncludeErrorContext(ctxt, cur); - xmlGenericError(xmlGenericErrorContext, "XInclude: failed build URL\n"); - ctxt->nbErrors++; + xmlXIncludeErr(ctxt, cur, XML_XINCLUDE_HREF_URI, + "failed build URL\n", NULL); return(-1); } @@ -536,10 +504,8 @@ xmlXIncludeAddNode(xmlXIncludeCtxtPtr ctxt, xmlNodePtr cur) { */ uri = xmlParseURI((const char *)URI); if (uri == NULL) { - xmlXIncludeErrorContext(ctxt, cur); - xmlGenericError(xmlGenericErrorContext, - "XInclude: invalid value URI %s\n", URI); - ctxt->nbErrors++; + xmlXIncludeErr(ctxt, cur, XML_XINCLUDE_HREF_URI, + "invalid value URI %s\n", URI); return(-1); } if (uri->fragment != NULL) { @@ -550,10 +516,8 @@ xmlXIncludeAddNode(xmlXIncludeCtxtPtr ctxt, xmlNodePtr cur) { xmlFreeURI(uri); xmlFree(URI); if (URL == NULL) { - xmlXIncludeErrorContext(ctxt, cur); - xmlGenericError(xmlGenericErrorContext, - "XInclude: invalid value URI %s\n", URI); - ctxt->nbErrors++; + xmlXIncludeErr(ctxt, cur, XML_XINCLUDE_HREF_URI, + "invalid value URI %s\n", URI); if (fragment != NULL) xmlFree(fragment); return(-1); @@ -565,11 +529,8 @@ xmlXIncludeAddNode(xmlXIncludeCtxtPtr ctxt, xmlNodePtr cur) { if (!local) { for (i = 0;i < ctxt->urlNr;i++) { if (xmlStrEqual(URL, ctxt->urlTab[i])) { - xmlXIncludeErrorContext(ctxt, cur); - xmlGenericError(xmlGenericErrorContext, - "XInclude: detected a recursion in %s\n", - URL); - ctxt->nbErrors++; + xmlXIncludeErr(ctxt, cur, XML_XINCLUDE_RECURSION, + "detected a recursion in %s\n", URL); return(-1); } } @@ -626,10 +587,7 @@ xmlXIncludeRecurseDoc(xmlXIncludeCtxtPtr ctxt, xmlDocPtr doc, newctxt->incTab = (xmlXIncludeRefPtr *) xmlMalloc(newctxt->incMax * sizeof(newctxt->incTab[0])); if (newctxt->incTab == NULL) { - xmlXIncludeErrorContext(ctxt, NULL); - xmlGenericError(xmlGenericErrorContext, - "malloc failed !\n"); - ctxt->nbErrors++; + xmlXIncludeErrMemory(ctxt, (xmlNodePtr) doc, "processing doc"); xmlFree(newctxt); return; } @@ -688,19 +646,13 @@ xmlXIncludeAddTxt(xmlXIncludeCtxtPtr ctxt, xmlNodePtr txt, const xmlURL url) { ctxt->txtTab = (xmlNodePtr *) xmlMalloc(ctxt->txtMax * sizeof(ctxt->txtTab[0])); if (ctxt->txtTab == NULL) { - xmlXIncludeErrorContext(ctxt, NULL); - xmlGenericError(xmlGenericErrorContext, - "malloc failed !\n"); - ctxt->nbErrors++; + xmlXIncludeErrMemory(ctxt, NULL, "processing text"); return; } ctxt->txturlTab = (xmlURL *) xmlMalloc(ctxt->txtMax * sizeof(ctxt->txturlTab[0])); if (ctxt->txturlTab == NULL) { - xmlXIncludeErrorContext(ctxt, NULL); - xmlGenericError(xmlGenericErrorContext, - "malloc failed !\n"); - ctxt->nbErrors++; + xmlXIncludeErrMemory(ctxt, NULL, "processing text"); return; } } @@ -709,19 +661,13 @@ xmlXIncludeAddTxt(xmlXIncludeCtxtPtr ctxt, xmlNodePtr txt, const xmlURL url) { ctxt->txtTab = (xmlNodePtr *) xmlRealloc(ctxt->txtTab, ctxt->txtMax * sizeof(ctxt->txtTab[0])); if (ctxt->txtTab == NULL) { - xmlXIncludeErrorContext(ctxt, NULL); - xmlGenericError(xmlGenericErrorContext, - "realloc failed !\n"); - ctxt->nbErrors++; + xmlXIncludeErrMemory(ctxt, NULL, "processing text"); return; } ctxt->txturlTab = (xmlURL *) xmlRealloc(ctxt->txturlTab, ctxt->txtMax * sizeof(ctxt->txturlTab[0])); if (ctxt->txturlTab == NULL) { - xmlXIncludeErrorContext(ctxt, NULL); - xmlGenericError(xmlGenericErrorContext, - "realloc failed !\n"); - ctxt->nbErrors++; + xmlXIncludeErrMemory(ctxt, NULL, "processing text"); return; } } @@ -1211,10 +1157,9 @@ error: case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY: break; } - xmlXIncludeErrorContext(ctxt, (xmlNodePtr) ent); - xmlGenericError(xmlGenericErrorContext, - "XInclude: mismatch in redefinition of entity %s\n", ent->name); - ctxt->nbErrors++; + xmlXIncludeErr(ctxt, (xmlNodePtr) ent, XML_XINCLUDE_ENTITY_DEF_MISMATCH, + "mismatch in redefinition of entity %s\n", + ent->name); } /** @@ -1304,10 +1249,9 @@ xmlXIncludeLoadDoc(xmlXIncludeCtxtPtr ctxt, const xmlChar *url, int nr) { */ uri = xmlParseURI((const char *)url); if (uri == NULL) { - xmlXIncludeErrorContext(ctxt, ctxt->incTab[nr]->ref); - xmlGenericError(xmlGenericErrorContext, - "XInclude: invalid value URI %s\n", url); - ctxt->nbErrors++; + xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref, + XML_XINCLUDE_HREF_URI, + "invalid value URI %s\n", url); return(-1); } if (uri->fragment != NULL) { @@ -1317,10 +1261,9 @@ xmlXIncludeLoadDoc(xmlXIncludeCtxtPtr ctxt, const xmlChar *url, int nr) { URL = xmlSaveUri(uri); xmlFreeURI(uri); if (URL == NULL) { - xmlXIncludeErrorContext(ctxt, ctxt->incTab[nr]->ref); - xmlGenericError(xmlGenericErrorContext, - "XInclude: invalid value URI %s\n", url); - ctxt->nbErrors++; + xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref, + XML_XINCLUDE_HREF_URI, + "invalid value URI %s\n", url); if (fragment != NULL) xmlFree(fragment); return(-1); @@ -1426,21 +1369,19 @@ loaded: xptrctxt = xmlXPtrNewContext(doc, NULL, NULL); } if (xptrctxt == NULL) { - xmlXIncludeErrorContext(ctxt, ctxt->incTab[nr]->ref); - xmlGenericError(xmlGenericErrorContext, - "XInclude: could create XPointer context\n"); - ctxt->nbErrors++; + xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref, + XML_XINCLUDE_XPTR_FAILED, + "could create XPointer context\n", NULL); xmlFree(URL); xmlFree(fragment); return(-1); } xptr = xmlXPtrEval(fragment, xptrctxt); if (xptr == NULL) { - xmlXIncludeErrorContext(ctxt, ctxt->incTab[nr]->ref); - xmlGenericError(xmlGenericErrorContext, - "XInclude: XPointer evaluation failed: #%s\n", - fragment); - ctxt->nbErrors++; + xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref, + XML_XINCLUDE_XPTR_FAILED, + "XPointer evaluation failed: #%s\n", + fragment); xmlXPathFreeContext(xptrctxt); xmlFree(URL); xmlFree(fragment); @@ -1454,11 +1395,10 @@ loaded: case XPATH_POINT: case XPATH_USERS: case XPATH_XSLT_TREE: - xmlXIncludeErrorContext(ctxt, ctxt->incTab[nr]->ref); - xmlGenericError(xmlGenericErrorContext, - "XInclude: XPointer is not a range: #%s\n", - fragment); - ctxt->nbErrors++; + xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref, + XML_XINCLUDE_XPTR_RESULT, + "XPointer is not a range: #%s\n", + fragment); xmlXPathFreeContext(xptrctxt); xmlFree(URL); xmlFree(fragment); @@ -1488,19 +1428,17 @@ loaded: #endif continue; case XML_ATTRIBUTE_NODE: - xmlXIncludeErrorContext(ctxt, ctxt->incTab[nr]->ref); - xmlGenericError(xmlGenericErrorContext, - "XInclude: XPointer selects an attribute: #%s\n", - fragment); - ctxt->nbErrors++; + xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref, + XML_XINCLUDE_XPTR_RESULT, + "XPointer selects an attribute: #%s\n", + fragment); set->nodeTab[i] = NULL; continue; case XML_NAMESPACE_DECL: - xmlXIncludeErrorContext(ctxt, ctxt->incTab[nr]->ref); - xmlGenericError(xmlGenericErrorContext, - "XInclude: XPointer selects a namespace: #%s\n", - fragment); - ctxt->nbErrors++; + xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref, + XML_XINCLUDE_XPTR_RESULT, + "XPointer selects a namespace: #%s\n", + fragment); set->nodeTab[i] = NULL; continue; case XML_DOCUMENT_TYPE_NODE: @@ -1512,11 +1450,10 @@ loaded: case XML_ENTITY_DECL: case XML_XINCLUDE_START: case XML_XINCLUDE_END: - xmlXIncludeErrorContext(ctxt, ctxt->incTab[nr]->ref); - xmlGenericError(xmlGenericErrorContext, - "XInclude: XPointer selects unexpected nodes: #%s\n", - fragment); - ctxt->nbErrors++; + xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref, + XML_XINCLUDE_XPTR_RESULT, + "XPointer selects unexpected nodes: #%s\n", + fragment); set->nodeTab[i] = NULL; set->nodeTab[i] = NULL; continue; /* for */ @@ -1586,28 +1523,22 @@ xmlXIncludeLoadTxt(xmlXIncludeCtxtPtr ctxt, const xmlChar *url, int nr) { */ uri = xmlParseURI((const char *)url); if (uri == NULL) { - xmlXIncludeErrorContext(ctxt, ctxt->incTab[nr]->ref); - xmlGenericError(xmlGenericErrorContext, - "XInclude: invalid value URI %s\n", url); - ctxt->nbErrors++; + xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref, XML_XINCLUDE_HREF_URI, + "invalid value URI %s\n", url); return(-1); } if (uri->fragment != NULL) { - xmlXIncludeErrorContext(ctxt, ctxt->incTab[nr]->ref); - xmlGenericError(xmlGenericErrorContext, - "XInclude: fragment identifier forbidden for text: %s\n", - uri->fragment); - ctxt->nbErrors++; + xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref, XML_XINCLUDE_TEXT_FRAGMENT, + "fragment identifier forbidden for text: %s\n", + (const xmlChar *) uri->fragment); xmlFreeURI(uri); return(-1); } URL = xmlSaveUri(uri); xmlFreeURI(uri); if (URL == NULL) { - xmlXIncludeErrorContext(ctxt, ctxt->incTab[nr]->ref); - xmlGenericError(xmlGenericErrorContext, - "XInclude: invalid value URI %s\n", url); - ctxt->nbErrors++; + xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref, XML_XINCLUDE_HREF_URI, + "invalid value URI %s\n", url); return(-1); } @@ -1616,10 +1547,9 @@ xmlXIncludeLoadTxt(xmlXIncludeCtxtPtr ctxt, const xmlChar *url, int nr) { * directly through ctxt->doc. */ if (URL[0] == 0) { - xmlXIncludeErrorContext(ctxt, ctxt->incTab[nr]->ref); - xmlGenericError(xmlGenericErrorContext, - "XInclude: text serialization of document not available\n"); - ctxt->nbErrors++; + xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref, + XML_XINCLUDE_TEXT_DOCUMENT, + "text serialization of document not available\n", NULL); xmlFree(URL); return(-1); } @@ -1648,10 +1578,9 @@ xmlXIncludeLoadTxt(xmlXIncludeCtxtPtr ctxt, const xmlChar *url, int nr) { */ enc = xmlParseCharEncoding((const char *) encoding); if (enc == XML_CHAR_ENCODING_ERROR) { - xmlXIncludeErrorContext(ctxt, ctxt->incTab[nr]->ref); - xmlGenericError(xmlGenericErrorContext, - "XInclude: encoding %s not supported\n", encoding); - ctxt->nbErrors++; + xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref, + XML_XINCLUDE_UNKNOWN_ENCODING, + "encoding %s not supported\n", encoding); xmlFree(encoding); xmlFree(URL); return(-1); @@ -1684,10 +1613,9 @@ xmlXIncludeLoadTxt(xmlXIncludeCtxtPtr ctxt, const xmlChar *url, int nr) { cur = xmlStringCurrentChar(NULL, &content[i], &l); if (!IS_CHAR(cur)) { - xmlXIncludeErrorContext(ctxt, ctxt->incTab[nr]->ref); - xmlGenericError(xmlGenericErrorContext, - "XInclude: %s contains invalid char %d\n", URL, cur); - ctxt->nbErrors++; + xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref, + XML_XINCLUDE_INVALID_CHAR, + "%s contains invalid char\n", URL); } else { xmlNodeAddContentLen(node, &content[i], l); } @@ -1783,9 +1711,8 @@ xmlXIncludeLoadNode(xmlXIncludeCtxtPtr ctxt, int nr) { if (href == NULL) { href = xmlGetProp(cur, XINCLUDE_HREF); if (href == NULL) { - xmlXIncludeErrorContext(ctxt, ctxt->incTab[nr]->ref); - xmlGenericError(xmlGenericErrorContext, "XInclude: no href\n"); - ctxt->nbErrors++; + xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref, + XML_XINCLUDE_NO_HREF, "no href\n", NULL); return(-1); } } @@ -1799,11 +1726,9 @@ xmlXIncludeLoadNode(xmlXIncludeCtxtPtr ctxt, int nr) { else if (xmlStrEqual(parse, XINCLUDE_PARSE_TEXT)) xml = 0; else { - xmlXIncludeErrorContext(ctxt, ctxt->incTab[nr]->ref); - xmlGenericError(xmlGenericErrorContext, - "XInclude: invalid value %s for %s\n", - parse, XINCLUDE_PARSE); - ctxt->nbErrors++; + xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref, + XML_XINCLUDE_PARSE_VALUE, + "invalid value %s for 'parse'\n", parse); if (href != NULL) xmlFree(href); if (parse != NULL) @@ -1836,9 +1761,8 @@ xmlXIncludeLoadNode(xmlXIncludeCtxtPtr ctxt, int nr) { xmlFree(eschref); } if (URI == NULL) { - xmlXIncludeErrorContext(ctxt, ctxt->incTab[nr]->ref); - xmlGenericError(xmlGenericErrorContext, "XInclude: failed build URL\n"); - ctxt->nbErrors++; + xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref, + XML_XINCLUDE_HREF_URI, "failed build URL\n", NULL); if (parse != NULL) xmlFree(parse); if (href != NULL) @@ -1885,11 +1809,10 @@ xmlXIncludeLoadNode(xmlXIncludeCtxtPtr ctxt, int nr) { } } if (ret < 0) { - xmlXIncludeErrorContext(ctxt, ctxt->incTab[nr]->ref); - xmlGenericError(xmlGenericErrorContext, - "XInclude: could not load %s, and no fallback was found\n", - URI); - ctxt->nbErrors++; + xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref, + XML_XINCLUDE_NO_FALLBACK, + "could not load %s, and no fallback was found\n", + URI); } /* @@ -1955,10 +1878,10 @@ xmlXIncludeIncludeNode(xmlXIncludeCtxtPtr ctxt, int nr) { tmp = tmp->next; } if (nb_elem > 1) { - xmlXIncludeErrorContext(ctxt, ctxt->incTab[nr]->ref); - xmlGenericError(xmlGenericErrorContext, - "XInclude error: would result in multiple root nodes\n"); - ctxt->nbErrors++; + xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref, + XML_XINCLUDE_MULTIPLE_ROOT, + "XInclude error: would result in multiple root nodes\n", + NULL); return(-1); } } @@ -1970,10 +1893,8 @@ xmlXIncludeIncludeNode(xmlXIncludeCtxtPtr ctxt, int nr) { cur->type = XML_XINCLUDE_START; end = xmlNewNode(cur->ns, cur->name); if (end == NULL) { - xmlXIncludeErrorContext(ctxt, ctxt->incTab[nr]->ref); - xmlGenericError(xmlGenericErrorContext, - "XInclude: failed to build node\n"); - ctxt->nbErrors++; + xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref, XML_XINCLUDE_BUILD_FAILED, + "failed to build node\n", NULL); return(-1); } end->type = XML_XINCLUDE_END; @@ -2020,11 +1941,10 @@ xmlXIncludeTestNode(xmlXIncludeCtxtPtr ctxt, xmlNodePtr node) { (child->ns != NULL) && (xmlStrEqual(child->ns->href, XINCLUDE_NS))) { if (xmlStrEqual(child->name, XINCLUDE_NODE)) { - xmlXIncludeErrorContext(ctxt, node); - xmlGenericError(xmlGenericErrorContext, - "XInclude: %s has an %s child\n", - XINCLUDE_NODE, XINCLUDE_NODE); - ctxt->nbErrors++; + xmlXIncludeErr(ctxt, node, + XML_XINCLUDE_INCLUDE_IN_INCLUDE, + "%s has an 'include' child\n", + XINCLUDE_NODE); return(0); } if (xmlStrEqual(child->name, XINCLUDE_FALLBACK)) { @@ -2034,11 +1954,9 @@ xmlXIncludeTestNode(xmlXIncludeCtxtPtr ctxt, xmlNodePtr node) { child = child->next; } if (nb_fallback > 1) { - xmlXIncludeErrorContext(ctxt, node); - xmlGenericError(xmlGenericErrorContext, - "XInclude: %s has %d %s children\n", - XINCLUDE_NODE, nb_fallback, XINCLUDE_FALLBACK); - ctxt->nbErrors++; + xmlXIncludeErr(ctxt, node, XML_XINCLUDE_FALLBACKS_IN_INCLUDE, + "%s has multiple fallback children\n", + XINCLUDE_NODE); return(0); } return(1); @@ -2049,11 +1967,10 @@ xmlXIncludeTestNode(xmlXIncludeCtxtPtr ctxt, xmlNodePtr node) { (node->parent->ns == NULL) || (!xmlStrEqual(node->parent->ns->href, XINCLUDE_NS)) || (!xmlStrEqual(node->parent->name, XINCLUDE_NODE))) { - xmlXIncludeErrorContext(ctxt, node); - xmlGenericError(xmlGenericErrorContext, - "XInclude: %s is not the child of an %s\n", - XINCLUDE_FALLBACK, XINCLUDE_NODE); - ctxt->nbErrors++; + xmlXIncludeErr(ctxt, node, + XML_XINCLUDE_FALLBACK_NOT_IN_INCLUDE, + "%s is not the child of an 'include'\n", + XINCLUDE_FALLBACK); } } } diff --git a/xmlIO.c b/xmlIO.c index 0698d049..6642a599 100644 --- a/xmlIO.c +++ b/xmlIO.c @@ -194,7 +194,7 @@ xmlIOErrMemory(const char *extra) * @code: the error number * @extra: extra informations * - * Handle an out of memory condition + * Handle an I/O error */ static void xmlIOErr(int code, const char *extra) @@ -340,6 +340,37 @@ xmlIOErr(int code, const char *extra) __xmlSimpleError(XML_FROM_IO, code, NULL, IOerr[idx], extra); } +/** + * xmlLoaderErr: + * @ctxt: the parser context + * @extra: extra informations + * + * Handle a resource access error + */ +static void +xmlLoaderErr(xmlParserCtxtPtr ctxt, const char *msg, const char *filename) +{ + xmlGenericErrorFunc channel = NULL; + void *data = NULL; + xmlErrorLevel level = XML_ERR_ERROR; + + if ((ctxt != NULL) && (ctxt->sax != NULL)) { + if (ctxt->validate) { + channel = ctxt->sax->error; + level = XML_ERR_ERROR; + } else { + channel = ctxt->sax->warning; + level = XML_ERR_WARNING; + } + data = ctxt->userData; + } + __xmlRaiseError(channel, data, ctxt, NULL, XML_FROM_IO, + XML_IO_LOAD_ERROR, level, NULL, 0, + filename, NULL, NULL, 0, 0, + msg, filename); + +} + /************************************************************************ * * * Tree memory error handler * @@ -2986,24 +3017,13 @@ xmlDefaultExternalEntityLoader(const char *URL, const char *ID, if (resource == NULL) { if (ID == NULL) ID = "NULL"; - if ((ctxt->validate) && (ctxt->sax != NULL) && - (ctxt->sax->error != NULL)) - ctxt->sax->error(ctxt->userData, - "failed to load external entity \"%s\"\n", ID); - else if ((ctxt->sax != NULL) && (ctxt->sax->warning != NULL)) - ctxt->sax->warning(ctxt->userData, - "failed to load external entity \"%s\"\n", ID); + xmlLoaderErr(ctxt, "failed to load external entity \"%s\"\n", ID); return(NULL); } ret = xmlNewInputFromFile(ctxt, (const char *)resource); if (ret == NULL) { - if ((ctxt->validate) && (ctxt->sax != NULL) && - (ctxt->sax->error != NULL)) - ctxt->sax->error(ctxt->userData, - "failed to load external entity \"%s\"\n", resource); - else if ((ctxt->sax != NULL) && (ctxt->sax->warning != NULL)) - ctxt->sax->warning(ctxt->userData, - "failed to load external entity \"%s\"\n", resource); + xmlLoaderErr(ctxt, "failed to load external entity \"%s\"\n", + (const char *) resource); } if ((resource != NULL) && (resource != (xmlChar *) URL)) xmlFree(resource);