From 8606bbbc0a04293afd7541033d6a83c4943a6f02 Mon Sep 17 00:00:00 2001 From: Daniel Veillard Date: Tue, 12 Nov 2002 12:36:52 +0000 Subject: [PATCH] fixed the initialization of the SAX structure which was breaking xsltproc * parserInternals.c: fixed the initialization of the SAX structure which was breaking xsltproc * xpath.c: patch from Petr Pajas for CDATA nodes * tree.c: patch from Petr Pajas improving xmlGetNodePath() * parser.c include/libxml/parser.h: patch from Peter Jones removing a leak in xmlSAXParseMemory() and adding the function xmlSAXParseMemoryWithData() Daniel --- ChangeLog | 10 +++++ include/libxml/parser.h | 5 +++ parser.c | 74 +++++++++++++++++++++++---------- parserInternals.c | 2 +- tree.c | 90 ++++++++++++++++++++++++++++++++++++++++- xpath.c | 4 +- 6 files changed, 159 insertions(+), 26 deletions(-) diff --git a/ChangeLog b/ChangeLog index c014f32b..8b7bb30b 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,13 @@ +Tue Nov 12 13:32:50 CET 2002 Daniel Veillard + + * parserInternals.c: fixed the initialization of the SAX structure + which was breaking xsltproc + * xpath.c: patch from Petr Pajas for CDATA nodes + * tree.c: patch from Petr Pajas improving xmlGetNodePath() + * parser.c include/libxml/parser.h: patch from Peter Jones + removing a leak in xmlSAXParseMemory() and adding the + function xmlSAXParseMemoryWithData() + Mon Nov 11 20:47:03 MST 2002 John Fleck adding pdf of tutorial, changing web page to link to it diff --git a/include/libxml/parser.h b/include/libxml/parser.h index a1c8b377..a54b6d98 100644 --- a/include/libxml/parser.h +++ b/include/libxml/parser.h @@ -734,6 +734,11 @@ xmlDocPtr xmlSAXParseMemory (xmlSAXHandlerPtr sax, const char *buffer, int size, int recovery); +xmlDocPtr xmlSAXParseMemoryWithData (xmlSAXHandlerPtr sax, + const char *buffer, + int size, + int recovery, + void *data); xmlDocPtr xmlSAXParseFile (xmlSAXHandlerPtr sax, const char *filename, int recovery); diff --git a/parser.c b/parser.c index 304dc67f..f6363ae4 100644 --- a/parser.c +++ b/parser.c @@ -10338,6 +10338,57 @@ xmlCreateMemoryParserCtxt(const char *buffer, int size) { return(ctxt); } +/** + * xmlSAXParseMemoryWithData: + * @sax: the SAX handler block + * @buffer: an pointer to a char array + * @size: the size of the array + * @recovery: work in recovery mode, i.e. tries to read no Well Formed + * documents + * @data: the userdata + * + * parse an XML in-memory block and use the given SAX function block + * to handle the parsing callback. If sax is NULL, fallback to the default + * DOM tree building routines. + * + * User data (void *) is stored within the parser context in the + * context's _private member, so it is available nearly everywhere in libxml + * + * Returns the resulting document tree + */ + +xmlDocPtr +xmlSAXParseMemoryWithData(xmlSAXHandlerPtr sax, const char *buffer, + int size, int recovery, void *data) { + xmlDocPtr ret; + xmlParserCtxtPtr ctxt; + + ctxt = xmlCreateMemoryParserCtxt(buffer, size); + if (ctxt == NULL) return(NULL); + if (sax != NULL) { + if (ctxt->sax != NULL) + xmlFree(ctxt->sax); + ctxt->sax = sax; + } + if (data!=NULL) { + ctxt->_private=data; + } + + xmlParseDocument(ctxt); + + if ((ctxt->wellFormed) || recovery) ret = ctxt->myDoc; + else { + ret = NULL; + xmlFreeDoc(ctxt->myDoc); + ctxt->myDoc = NULL; + } + if (sax != NULL) + ctxt->sax = NULL; + xmlFreeParserCtxt(ctxt); + + return(ret); +} + /** * xmlSAXParseMemory: * @sax: the SAX handler block @@ -10355,28 +10406,7 @@ xmlCreateMemoryParserCtxt(const char *buffer, int size) { xmlDocPtr xmlSAXParseMemory(xmlSAXHandlerPtr sax, const char *buffer, int size, int recovery) { - xmlDocPtr ret; - xmlParserCtxtPtr ctxt; - - ctxt = xmlCreateMemoryParserCtxt(buffer, size); - if (ctxt == NULL) return(NULL); - if (sax != NULL) { - ctxt->sax = sax; - } - - xmlParseDocument(ctxt); - - if ((ctxt->wellFormed) || recovery) ret = ctxt->myDoc; - else { - ret = NULL; - xmlFreeDoc(ctxt->myDoc); - ctxt->myDoc = NULL; - } - if (sax != NULL) - ctxt->sax = NULL; - xmlFreeParserCtxt(ctxt); - - return(ret); + return xmlSAXParseMemoryWithData(sax, buffer, size, recovery, NULL); } /** diff --git a/parserInternals.c b/parserInternals.c index 41725d56..c09fc95d 100644 --- a/parserInternals.c +++ b/parserInternals.c @@ -2250,7 +2250,7 @@ xmlInitParserCtxt(xmlParserCtxtPtr ctxt) ctxt->space = &ctxt->spaceTab[0]; ctxt->sax = sax; - initxmlDefaultSAXHandler(sax, xmlGetWarningsDefaultValue); + memcpy(sax, &xmlDefaultSAXHandler, sizeof(xmlSAXHandler)); ctxt->userData = ctxt; ctxt->myDoc = NULL; diff --git a/tree.c b/tree.c index 44ea7a55..26276ca1 100644 --- a/tree.c +++ b/tree.c @@ -3493,8 +3493,9 @@ xmlGetNodePath(xmlNodePtr node) } if (occur == 0) { tmp = cur->next; - while (tmp != NULL) { - if (xmlStrEqual(cur->name, tmp->name)) + while (tmp != NULL && occur == 0) { + if ((tmp->type == XML_ELEMENT_NODE) && + (xmlStrEqual(cur->name, tmp->name))) occur++; tmp = tmp->next; } @@ -3502,6 +3503,91 @@ xmlGetNodePath(xmlNodePtr node) occur = 1; } else occur++; + } else if (cur->type == XML_COMMENT_NODE) { + sep = "/"; + name = "comment()"; + next = cur->parent; + + /* + * Thumbler index computation + */ + tmp = cur->prev; + while (tmp != NULL) { + if (tmp->type == XML_COMMENT_NODE) + occur++; + tmp = tmp->prev; + } + if (occur == 0) { + tmp = cur->next; + while (tmp != NULL && occur == 0) { + if (tmp->type == XML_COMMENT_NODE) + occur++; + tmp = tmp->next; + } + if (occur != 0) + occur = 1; + } else + occur++; + } else if ((cur->type == XML_TEXT_NODE) || + (cur->type == XML_CDATA_SECTION_NODE)) { + sep = "/"; + name = "text()"; + next = cur->parent; + + /* + * Thumbler index computation + */ + tmp = cur->prev; + while (tmp != NULL) { + if ((cur->type == XML_TEXT_NODE) || + (cur->type == XML_CDATA_SECTION_NODE)) + occur++; + tmp = tmp->prev; + } + if (occur == 0) { + tmp = cur->next; + while (tmp != NULL && occur == 0) { + if ((cur->type == XML_TEXT_NODE) || + (cur->type == XML_CDATA_SECTION_NODE)) + occur++; + tmp = tmp->next; + } + if (occur != 0) + occur = 1; + } else + occur++; + } else if (cur->type == XML_PI_NODE) { + sep = "/"; + snprintf(nametemp, sizeof(nametemp) - 1, + "processing-instruction('%s')", cur->name); + nametemp[sizeof(nametemp) - 1] = 0; + name = nametemp; + + next = cur->parent; + + /* + * Thumbler index computation + */ + tmp = cur->prev; + while (tmp != NULL) { + if ((tmp->type == XML_PI_NODE) && + (xmlStrEqual(cur->name, tmp->name))) + occur++; + tmp = tmp->prev; + } + if (occur == 0) { + tmp = cur->next; + while (tmp != NULL && occur == 0) { + if ((tmp->type == XML_PI_NODE) && + (xmlStrEqual(cur->name, tmp->name))) + occur++; + tmp = tmp->next; + } + if (occur != 0) + occur = 1; + } else + occur++; + } else if (cur->type == XML_ATTRIBUTE_NODE) { sep = "/@"; name = (const char *) (((xmlAttrPtr) cur)->name); diff --git a/xpath.c b/xpath.c index fc884aa9..5da280f7 100644 --- a/xpath.c +++ b/xpath.c @@ -9336,7 +9336,9 @@ xmlXPathNodeCollectAndTestNth(xmlXPathParserContextPtr ctxt, (cur->type == XML_PI_NODE) || (cur->type == XML_COMMENT_NODE) || (cur->type == XML_CDATA_SECTION_NODE) || - (cur->type == XML_TEXT_NODE)))) { + (cur->type == XML_TEXT_NODE))) || + ((type == NODE_TYPE_TEXT) && + (cur->type == XML_CDATA_SECTION_NODE))) { n++; if (n == indx) addNode(list, cur);