fixing #61290 "namespace nodes have no parent" long standing divergence

* xpath.c: fixing #61290 "namespace nodes have no parent"
  long standing divergence from the XPath REC. NodeSets
  simply hold a copy of namespace nodes and those node ->next
  points to the parent (which may not be the node carrying the
  definition).
* include/libxml/xpath.h: flagged but didn't added a possible
  speedup
* DOCBparser.c HTMLparser.c: removed some warnings from push
  parser due to new state being added.
* tree.c: new fix from Boris Erdmann
* configure.in c14n.c include/libxml/c14n.h testC14N.c: added
  the XML Canonalization support from Aleksey Sanin
Daniel
This commit is contained in:
Daniel Veillard 2002-03-04 17:09:44 +00:00
parent d4f41aacfc
commit 044fc6b747
14 changed files with 2290 additions and 45 deletions

View File

@ -1,3 +1,18 @@
Mon Mar 4 17:59:29 CET 2002 Daniel Veillard <daniel@veillard.com>
* xpath.c: fixing #61290 "namespace nodes have no parent"
long standing divergence from the XPath REC. NodeSets
simply hold a copy of namespace nodes and those node ->next
points to the parent (which may not be the node carrying the
definition).
* include/libxml/xpath.h: flagged but didn't added a possible
speedup
* DOCBparser.c HTMLparser.c: removed some warnings from push
parser due to new state being added.
* tree.c: new fix from Boris Erdmann
* configure.in c14n.c include/libxml/c14n.h testC14N.c: added
the XML Canonalization support from Aleksey Sanin
Sun Mar 3 15:12:42 CET 2002 Daniel Veillard <daniel@veillard.com>
* tree.c: patch from Boris Erdmann fixing some namespace odities

View File

@ -5750,6 +5750,16 @@ docbParseTryOrFinish(docbParserCtxtPtr ctxt, int terminate) {
"HPP: entering CONTENT\n");
#endif
break;
case XML_PARSER_PUBLIC_LITERAL:
xmlGenericError(xmlGenericErrorContext,
"HPP: internal error, state == XML_PARSER_LITERAL\n");
ctxt->instate = XML_PARSER_CONTENT;
ctxt->checkIndex = 0;
#ifdef DEBUG_PUSH
xmlGenericError(xmlGenericErrorContext,
"HPP: entering CONTENT\n");
#endif
break;
}
}
done:

View File

@ -4605,6 +4605,17 @@ htmlParseTryOrFinish(htmlParserCtxtPtr ctxt, int terminate) {
"HPP: entering CONTENT\n");
#endif
break;
case XML_PARSER_PUBLIC_LITERAL:
xmlGenericError(xmlGenericErrorContext,
"HPP: internal error, state == XML_PARSER_LITERAL\n");
ctxt->instate = XML_PARSER_CONTENT;
ctxt->checkIndex = 0;
#ifdef DEBUG_PUSH
xmlGenericError(xmlGenericErrorContext,
"HPP: entering CONTENT\n");
#endif
break;
}
}
done:

View File

@ -5,7 +5,8 @@ SUBDIRS = include . doc example python
INCLUDES = -I@srcdir@/include -I$(top_builddir)/include @THREAD_CFLAGS@ @Z_CFLAGS@
noinst_PROGRAMS=testSAX testHTML testXPath testURI testDocbook testThreads
noinst_PROGRAMS=testSAX testHTML testXPath testURI testDocbook testThreads \
testC14N
bin_PROGRAMS = xmllint xmlcatalog
@ -21,14 +22,14 @@ libxml2_la_SOURCES = SAX.c entities.c encoding.c error.c parserInternals.c \
parser.c tree.c hash.c list.c xmlIO.c xmlmemory.c uri.c \
valid.c xlink.c HTMLparser.c HTMLtree.c debugXML.c xpath.c \
xpointer.c xinclude.c nanohttp.c nanoftp.c DOCBparser.c \
catalog.c globals.c threads.c triostr.c trio.c
catalog.c globals.c threads.c c14n.c triostr.c trio.c
else
libxml2_la_SOURCES = SAX.c entities.c encoding.c error.c parserInternals.c \
parser.c tree.c hash.c list.c xmlIO.c xmlmemory.c uri.c \
valid.c xlink.c HTMLparser.c HTMLtree.c debugXML.c xpath.c \
xpointer.c xinclude.c nanohttp.c nanoftp.c DOCBparser.c \
catalog.c globals.c threads.c
catalog.c globals.c threads.c c14n.c
endif
@ -70,6 +71,11 @@ testXPath_LDFLAGS =
testXPath_DEPENDENCIES = $(DEPS)
testXPath_LDADD= $(LDADDS)
testC14N_SOURCES=testC14N.c
testC14N_LDFLAGS =
testC14N_DEPENDENCIES = $(DEPS)
testC14N_LDADD= $(LDADDS)
testThreads_SOURCES=testThreads.c
testThreads_LDFLAGS =
testThreads_DEPENDENCIES = $(DEPS)

1556
c14n.c Normal file

File diff suppressed because it is too large Load Diff

View File

@ -420,6 +420,7 @@ AC_ARG_WITH(xpath, [ --with-xpath Add the XPATH support (on)])
if test "$with_xpath" = "no" ; then
echo Disabling XPATH support
with_xptr="no"
with-c14n="no"
WITH_XPATH=0
XPATH_OBJ=
else
@ -441,6 +442,18 @@ fi
AC_SUBST(WITH_XPTR)
AC_SUBST(XPTR_OBJ)
AC_ARG_WITH(c14n, [ --with-c14n Add the Canonicalization support (on)])
if test "$with_c14n" = "no" ; then
echo Disabling C14N support
WITH_C14N=0
C14N_OBJ=
else
WITH_C14N=1
C14N_OBJ="c14n.c"
fi
AC_SUBST(WITH_C14N)
AC_SUBST(C14N_OBJ)
AC_ARG_WITH(xinclude, [ --with-xinclude Add the XInclude support (on)])
if test "$with_xinclude" = "no" ; then
echo Disabling XInclude support

74
include/libxml/c14n.h Normal file
View File

@ -0,0 +1,74 @@
/*
* "Canonical XML" implementation
* http://www.w3.org/TR/xml-c14n
*
* "Exclusive XML Canonicalization" implementation
* http://www.w3.org/TR/xml-exc-c14n
* See Copyright for the status of this software.
*
* Author: Aleksey Sanin <aleksey@aleksey.com>
*/
#ifndef __XML_C14N_H__
#define __XML_C14N_H__
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
#include <libxml/tree.h>
#include <libxml/xpath.h>
/*
* XML Canonicazation
* http://www.w3.org/TR/xml-c14n
*
* Exclusive XML Canonicazation
* http://www.w3.org/TR/xml-exc-c14n
*
* Canonical form of an XML document could be created if and only if
* a) default attributes (if any) are added to all nodes
* b) all character and parsed entity references are resolved
* In order to achive this in libxml2 the document MUST be loaded with
* following global setings:
*
* xmlLoadExtDtdDefaultValue = XML_DETECT_IDS | XML_COMPLETE_ATTRS;
* xmlSubstituteEntitiesDefault(1);
*
* or corresponding parser context setting:
* xmlParserCtxtPtr ctxt;
*
* ...
* ctxt->loadsubset = XML_DETECT_IDS | XML_COMPLETE_ATTRS;
* ctxt->replaceEntities = 1;
* ...
*/
int xmlC14NDocSaveTo (xmlDocPtr doc,
xmlNodeSetPtr nodes,
int exclusive,
xmlChar **inclusive_ns_prefixes,
int with_comments,
xmlOutputBufferPtr buf);
int xmlC14NDocDumpMemory (xmlDocPtr doc,
xmlNodeSetPtr nodes,
int exclusive,
xmlChar **inclusive_ns_prefixes,
int with_comments,
xmlChar **doc_txt_ptr);
int xmlC14NDocSave (xmlDocPtr doc,
xmlNodeSetPtr nodes,
int exclusive,
xmlChar **inclusive_ns_prefixes,
int with_comments,
const char* filename,
int compression);
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif /* __XML_C14N_H__ */

View File

@ -108,6 +108,15 @@ extern void xmlCheckVersion(int version);
#define LIBXML_HTML_ENABLED
#endif
/**
* LIBXML_C14N_ENABLED:
*
* Whether the Canonicalization support is configured in
*/
#if @WITH_C14N@
#define LIBXML_C14N_ENABLED
#endif
/**
* LIBXML_CATALOG_ENABLED:
*

View File

@ -139,6 +139,15 @@ extern void xmlCheckVersion(int version);
#define LIBXML_XPTR_ENABLED
#endif
/**
* LIBXML_C14N_ENABLED:
*
* Whether the Canonicalization support is configured in
*/
#if 0
#define LIBXML_C14N_ENABLED
#endif
/**
* LIBXML_XINCLUDE_ENABLED:
*

View File

@ -139,6 +139,15 @@ extern void xmlCheckVersion(int version);
#define LIBXML_XPTR_ENABLED
#endif
/**
* LIBXML_C14N_ENABLED:
*
* Whether the Canonicalization support is configured in
*/
#if 0
#define LIBXML_C14N_ENABLED
#endif
/**
* LIBXML_XINCLUDE_ENABLED:
*

View File

@ -62,6 +62,7 @@ struct _xmlNodeSet {
int nodeNr; /* number of nodes in the set */
int nodeMax; /* size of the array as allocated */
xmlNodePtr *nodeTab; /* array of nodes in no particular order */
/* @@ with_ns to check wether namespace nodes should be looked at @@ */
};
/*

333
testC14N.c Normal file
View File

@ -0,0 +1,333 @@
/*
* Canonical XML implementation test program
* (http://www.w3.org/TR/2001/REC-xml-c14n-20010315)
*
* See Copyright for the status of this software.
*
* Author: Aleksey Sanin <aleksey@aleksey.com>
*/
#include "libxml.h"
#if defined(LIBXML_C14N_ENABLED)
#include <stdio.h>
#include <string.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#include <libxml/xmlmemory.h>
#include <libxml/parser.h>
#include <libxml/xpath.h>
#include <libxml/xpathInternals.h>
#include <libxml/c14n.h>
static void usage(const char *name) {
fprintf(stderr,
"Usage: %s <mode> <xml-file> [<xpath-expr>] [<inclusive-ns-list>]\n",
name);
fprintf(stderr, "where <mode> is one of following:\n");
fprintf(stderr,
"--with-comments \t XML file canonization w comments\n");
fprintf(stderr,
"--without-comments \t XML file canonization w/o comments\n");
fprintf(stderr,
"--exc-with-comments \t Exclusive XML file canonization w comments\n");
fprintf(stderr,
"--exc-without-comments\t Exclusive XML file canonization w/o comments\n");
}
xmlXPathObjectPtr
load_xpath_expr (xmlDocPtr parent_doc, const char* filename);
xmlChar **parse_list(xmlChar *str);
void
print_xpath_nodes(xmlXPathObjectPtr ptr);
static int
test_c14n(const char* xml_filename, int with_comments, int exclusive,
const char* xpath_filename, xmlChar **inclusive_namespaces) {
xmlDocPtr doc;
xmlXPathObjectPtr xpath = NULL;
xmlChar *result = NULL;
int ret;
/*
* build an XML tree from a the file; we need to add default
* attributes and resolve all character and entities references
*/
xmlLoadExtDtdDefaultValue = XML_DETECT_IDS | XML_COMPLETE_ATTRS;
xmlSubstituteEntitiesDefault(1);
doc = xmlParseFile(xml_filename);
if (doc == NULL) {
fprintf(stderr, "Error: unable to parse file \"%s\"\n", xml_filename);
return(-1);
}
/*
* Check the document is of the right kind
*/
if(xmlDocGetRootElement(doc) == NULL) {
fprintf(stderr,"Error: empty document for file \"%s\"\n", xml_filename);
xmlFreeDoc(doc);
return(-1);
}
/*
* load xpath file if specified
*/
if(xpath_filename) {
xpath = load_xpath_expr(doc, xpath_filename);
if(xpath == NULL) {
fprintf(stderr,"Error: unable to evaluate xpath expression\n");
xmlFreeDoc(doc);
return(-1);
}
}
/*
* Canonical form
*/
/* fprintf(stderr,"File \"%s\" loaded: start canonization\n", xml_filename); */
ret = xmlC14NDocDumpMemory(doc,
(xpath) ? xpath->nodesetval : NULL,
exclusive, inclusive_namespaces,
with_comments, &result);
if(ret >= 0) {
if(result != NULL) {
write(1, result, ret);
xmlFree(result);
}
} else {
fprintf(stderr,"Error: failed to canonicalize XML file \"%s\" (ret=%d)\n", xml_filename, ret);
if(result != NULL) xmlFree(result);
xmlFreeDoc(doc);
return(-1);
}
/*
* Cleanup
*/
if(xpath != NULL) xmlXPathFreeObject(xpath);
xmlFreeDoc(doc);
return(ret);
}
int main(int argc, char **argv) {
int ret = -1;
/*
* Init libxml
*/
xmlInitParser();
LIBXML_TEST_VERSION
/*
* Parse command line and process file
*/
if( argc < 3 ) {
fprintf(stderr, "Error: wrong number of arguments.\n");
usage(argv[0]);
} else if(strcmp(argv[1], "--with-comments") == 0) {
ret = test_c14n(argv[2], 1, 0, (argc > 3) ? argv[3] : NULL, NULL);
} else if(strcmp(argv[1], "--without-comments") == 0) {
ret = test_c14n(argv[2], 0, 0, (argc > 3) ? argv[3] : NULL, NULL);
} else if(strcmp(argv[1], "--exc-with-comments") == 0) {
xmlChar **list;
/* load exclusive namespace from command line */
list = (argc > 4) ? parse_list((xmlChar *)argv[4]) : NULL;
ret = test_c14n(argv[2], 1, 1, (argc > 3) ? argv[3] : NULL, list);
if(list != NULL) xmlFree(list);
} else if(strcmp(argv[1], "--exc-without-comments") == 0) {
xmlChar **list;
/* load exclusive namespace from command line */
list = (argc > 4) ? parse_list((xmlChar *)argv[4]) : NULL;
ret = test_c14n(argv[2], 0, 1, (argc > 3) ? argv[3] : NULL, list);
if(list != NULL) xmlFree(list);
} else {
fprintf(stderr, "Error: bad option.\n");
usage(argv[0]);
}
/*
* Shutdown libxml
*/
xmlCleanupParser();
xmlMemoryDump();
return((ret >= 0) ? 0 : 1);
}
/*
* Macro used to grow the current buffer.
*/
#define growBufferReentrant() { \
buffer_size *= 2; \
buffer = (xmlChar **) \
xmlRealloc(buffer, buffer_size * sizeof(xmlChar*)); \
if (buffer == NULL) { \
perror("realloc failed"); \
return(NULL); \
} \
}
xmlChar **parse_list(xmlChar *str) {
xmlChar **buffer;
xmlChar **out = NULL;
int buffer_size = 0;
if(str == NULL) {
return(NULL);
}
/*
* allocate an translation buffer.
*/
buffer_size = 1000;
buffer = (xmlChar **) xmlMalloc(buffer_size * sizeof(xmlChar*));
if (buffer == NULL) {
perror("malloc failed");
return(NULL);
}
out = buffer;
while(*str != '\0') {
if (out - buffer > buffer_size - 10) {
int indx = out - buffer;
growBufferReentrant();
out = &buffer[indx];
}
(*out++) = str;
while(*str != ',' && *str != '\0') ++str;
if(*str == ',') *(str++) = '\0';
}
(*out) = NULL;
return buffer;
}
xmlXPathObjectPtr
load_xpath_expr (xmlDocPtr parent_doc, const char* filename) {
xmlXPathObjectPtr xpath;
xmlDocPtr doc;
xmlChar *expr;
xmlXPathContextPtr ctx;
xmlNodePtr node;
xmlNsPtr ns;
/*
* load XPath expr as a file
*/
xmlLoadExtDtdDefaultValue = XML_DETECT_IDS | XML_COMPLETE_ATTRS;
xmlSubstituteEntitiesDefault(1);
doc = xmlParseFile(filename);
if (doc == NULL) {
fprintf(stderr, "Error: unable to parse file \"%s\"\n", filename);
return(NULL);
}
/*
* Check the document is of the right kind
*/
if(xmlDocGetRootElement(doc) == NULL) {
fprintf(stderr,"Error: empty document for file \"%s\"\n", filename);
xmlFreeDoc(doc);
return(NULL);
}
node = doc->children;
while(node != NULL && !xmlStrEqual(node->name, (const xmlChar *)"XPath")) {
node = node->next;
}
if(node == NULL) {
fprintf(stderr,"Error: XPath element expected in the file \"%s\"\n", filename);
xmlFreeDoc(doc);
return(NULL);
}
expr = xmlNodeGetContent(node);
if(expr == NULL) {
fprintf(stderr,"Error: XPath content element is NULL \"%s\"\n", filename);
xmlFreeDoc(doc);
return(NULL);
}
ctx = xmlXPathNewContext(parent_doc);
if(ctx == NULL) {
fprintf(stderr,"Error: unable to create new context\n");
xmlFree(expr);
xmlFreeDoc(doc);
return(NULL);
}
/*
* Register namespaces
*/
ns = node->nsDef;
while(ns != NULL) {
if(xmlXPathRegisterNs(ctx, ns->prefix, ns->href) != 0) {
fprintf(stderr,"Error: unable to register NS with prefix=\"%s\" and href=\"%s\"\n", ns->prefix, ns->href);
xmlFree(expr);
xmlXPathFreeContext(ctx);
xmlFreeDoc(doc);
return(NULL);
}
ns = ns->next;
}
/*
* Evaluate xpath
*/
xpath = xmlXPathEvalExpression(expr, ctx);
if(xpath == NULL) {
fprintf(stderr,"Error: unable to evaluate xpath expression\n");
xmlFree(expr);
xmlXPathFreeContext(ctx);
xmlFreeDoc(doc);
return(NULL);
}
/* print_xpath_nodes(xpath); */
xmlFree(expr);
xmlXPathFreeContext(ctx);
xmlFreeDoc(doc);
return(xpath);
}
void
print_xpath_nodes(xmlXPathObjectPtr ptr) {
xmlNodePtr cur;
int i;
if(ptr == NULL || ptr->nodesetval == NULL ){
fprintf(stderr, "Error: no nodes set defined\n");
return;
}
for(i = 0; i < ptr->nodesetval->nodeNr; ++i) {
cur = ptr->nodesetval->nodeTab[i];
fprintf(stderr, "node %s. type %d\n", cur->name, cur->type);
}
}
#else
#include <stdio.h>
int main(int argc, char **argv) {
printf("%s : XPath/Canonicalization support not compiled in\n", argv[0]);
return(0);
}
#endif /* LIBXML_C14N_ENABLED */

7
tree.c
View File

@ -2859,7 +2859,7 @@ xmlCopyProp(xmlNodePtr target, xmlAttrPtr cur) {
* we cant be sure, that the namespce we found is identified
* by the prefix
*/
if (xmlStrEqual(ns->href, ret->ns->href)) {
if (xmlStrEqual(ns->href, cur->ns->href)) {
/* this is the nice case */
ret->ns = ns;
} else {
@ -6907,8 +6907,9 @@ xmlSetDocCompressMode (xmlDocPtr doc, int mode) {
* Returns 0 (uncompressed) to 9 (max compression)
*/
int
xmlGetCompressMode(void) {
return(xmlCompressMode);
xmlGetCompressMode(void)
{
return (xmlCompressMode);
}
/**

276
xpath.c
View File

@ -1386,6 +1386,67 @@ xmlXPathNodeSetSort(xmlNodeSetPtr set) {
}
#define XML_NODESET_DEFAULT 10
/**
* xmlXPathNodeSetDupNs:
* @node: the parent node of the namespace XPath node
* @ns: the libxml namespace declaration node.
*
* Namespace node in libxml don't match the XPath semantic. In a node set
* the namespace nodes are duplicated and the next pointer is set to the
* parent node in the XPath semantic.
*
* Returns the newly created object.
*/
static xmlNodePtr
xmlXPathNodeSetDupNs(xmlNodePtr node, xmlNsPtr ns) {
xmlNsPtr cur;
if ((ns == NULL) || (ns->type != XML_NAMESPACE_DECL))
return(NULL);
if ((node == NULL) || (node->type == XML_NAMESPACE_DECL))
return((xmlNodePtr) ns);
/*
* Allocate a new Namespace and fill the fields.
*/
cur = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
if (cur == NULL) {
xmlGenericError(xmlGenericErrorContext,
"xmlXPathNodeSetDupNs : malloc failed\n");
return(NULL);
}
memset(cur, 0, sizeof(xmlNs));
cur->type = XML_NAMESPACE_DECL;
if (ns->href != NULL)
cur->href = xmlStrdup(ns->href);
if (ns->prefix != NULL)
cur->prefix = xmlStrdup(ns->prefix);
cur->next = (xmlNsPtr) node;
return((xmlNodePtr) cur);
}
/**
* xmlXPathNodeSetFreeNs:
* @ns: the XPath namespace node found in a nodeset.
*
* Namespace node in libxml don't match the XPath semantic. In a node set
* the namespace nodes are duplicated and the next pointer is set to the
* parent node in the XPath semantic. Check if such a node need to be freed
*/
static void
xmlXPathNodeSetFreeNs(xmlNsPtr ns) {
if ((ns == NULL) || (ns->type != XML_NAMESPACE_DECL))
return;
if ((ns->next != NULL) && (ns->next->type != XML_NAMESPACE_DECL)) {
if (ns->href != NULL)
xmlFree((xmlChar *)ns->href);
if (ns->prefix != NULL)
xmlFree((xmlChar *)ns->prefix);
xmlFree(ns);
}
}
/**
* xmlXPathNodeSetCreate:
* @val: an initial xmlNodePtr, or NULL
@ -1416,7 +1477,13 @@ xmlXPathNodeSetCreate(xmlNodePtr val) {
memset(ret->nodeTab, 0 ,
XML_NODESET_DEFAULT * (size_t) sizeof(xmlNodePtr));
ret->nodeMax = XML_NODESET_DEFAULT;
ret->nodeTab[ret->nodeNr++] = val;
if (val->type == XML_NAMESPACE_DECL) {
xmlNsPtr ns = (xmlNsPtr) val;
ret->nodeTab[ret->nodeNr++] =
xmlXPathNodeSetDupNs((xmlNodePtr) ns->next, ns);
} else
ret->nodeTab[ret->nodeNr++] = val;
}
return(ret);
}
@ -1434,13 +1501,87 @@ int
xmlXPathNodeSetContains (xmlNodeSetPtr cur, xmlNodePtr val) {
int i;
for (i = 0; i < cur->nodeNr; i++) {
if (cur->nodeTab[i] == val)
return(1);
if (val->type == XML_NAMESPACE_DECL) {
for (i = 0; i < cur->nodeNr; i++) {
if (cur->nodeTab[i]->type == XML_NAMESPACE_DECL) {
xmlNsPtr ns1, ns2;
ns1 = (xmlNsPtr) val;
ns2 = (xmlNsPtr) cur->nodeTab[i];
if (ns1 == ns2)
return(1);
if ((ns1->next != NULL) && (ns2->next == ns1->next) &&
(xmlStrEqual(ns1->prefix, ns2->prefix)))
return(1);
}
}
} else {
for (i = 0; i < cur->nodeNr; i++) {
if (cur->nodeTab[i] == val)
return(1);
}
}
return(0);
}
/**
* xmlXPathNodeSetAddNs:
* @cur: the initial node set
* @node: the hosting node
* @ns: a the namespace node
*
* add a new namespace node to an existing NodeSet
*/
static void
xmlXPathNodeSetAddNs(xmlNodeSetPtr cur, xmlNodePtr node, xmlNsPtr ns) {
int i;
if ((ns == NULL) || (node == NULL) || (ns->type != XML_NAMESPACE_DECL) ||
(node->type != XML_ELEMENT_NODE))
return;
/* @@ with_ns to check wether namespace nodes should be looked at @@ */
/*
* check against doublons
*/
for (i = 0;i < cur->nodeNr;i++) {
if ((cur->nodeTab[i] != NULL) &&
(cur->nodeTab[i]->type == XML_NAMESPACE_DECL) &&
(cur->nodeTab[i]->next == node) &&
(xmlStrEqual(ns->prefix, ((xmlNsPtr)cur->nodeTab[i])->prefix)))
return;
}
/*
* grow the nodeTab if needed
*/
if (cur->nodeMax == 0) {
cur->nodeTab = (xmlNodePtr *) xmlMalloc(XML_NODESET_DEFAULT *
sizeof(xmlNodePtr));
if (cur->nodeTab == NULL) {
xmlGenericError(xmlGenericErrorContext,
"xmlXPathNodeSetAdd: out of memory\n");
return;
}
memset(cur->nodeTab, 0 ,
XML_NODESET_DEFAULT * (size_t) sizeof(xmlNodePtr));
cur->nodeMax = XML_NODESET_DEFAULT;
} else if (cur->nodeNr == cur->nodeMax) {
xmlNodePtr *temp;
cur->nodeMax *= 2;
temp = (xmlNodePtr *) xmlRealloc(cur->nodeTab, cur->nodeMax *
sizeof(xmlNodePtr));
if (temp == NULL) {
xmlGenericError(xmlGenericErrorContext,
"xmlXPathNodeSetAdd: out of memory\n");
return;
}
cur->nodeTab = temp;
}
cur->nodeTab[cur->nodeNr++] = xmlXPathNodeSetDupNs(node, ns);
}
/**
* xmlXPathNodeSetAdd:
* @cur: the initial node set
@ -1454,6 +1595,7 @@ xmlXPathNodeSetAdd(xmlNodeSetPtr cur, xmlNodePtr val) {
if (val == NULL) return;
/* @@ with_ns to check wether namespace nodes should be looked at @@ */
/*
* check against doublons
*/
@ -1487,7 +1629,13 @@ xmlXPathNodeSetAdd(xmlNodeSetPtr cur, xmlNodePtr val) {
}
cur->nodeTab = temp;
}
cur->nodeTab[cur->nodeNr++] = val;
if (val->type == XML_NAMESPACE_DECL) {
xmlNsPtr ns = (xmlNsPtr) val;
cur->nodeTab[cur->nodeNr++] =
xmlXPathNodeSetDupNs((xmlNodePtr) ns->next, ns);
} else
cur->nodeTab[cur->nodeNr++] = val;
}
/**
@ -1502,6 +1650,7 @@ void
xmlXPathNodeSetAddUnique(xmlNodeSetPtr cur, xmlNodePtr val) {
if (val == NULL) return;
/* @@ with_ns to check wether namespace nodes should be looked at @@ */
/*
* grow the nodeTab if needed
*/
@ -1529,7 +1678,13 @@ xmlXPathNodeSetAddUnique(xmlNodeSetPtr cur, xmlNodePtr val) {
}
cur->nodeTab = temp;
}
cur->nodeTab[cur->nodeNr++] = val;
if (val->type == XML_NAMESPACE_DECL) {
xmlNsPtr ns = (xmlNsPtr) val;
cur->nodeTab[cur->nodeNr++] =
xmlXPathNodeSetDupNs((xmlNodePtr) ns->next, ns);
} else
cur->nodeTab[cur->nodeNr++] = val;
}
/**
@ -1551,6 +1706,7 @@ xmlXPathNodeSetMerge(xmlNodeSetPtr val1, xmlNodeSetPtr val2) {
val1 = xmlXPathNodeSetCreate(NULL);
}
/* @@ with_ns to check wether namespace nodes should be looked at @@ */
initNr = val1->nodeNr;
for (i = 0;i < val2->nodeNr;i++) {
@ -1562,6 +1718,16 @@ xmlXPathNodeSetMerge(xmlNodeSetPtr val1, xmlNodeSetPtr val2) {
if (val1->nodeTab[j] == val2->nodeTab[i]) {
skip = 1;
break;
} else if ((val1->nodeTab[j]->type == XML_NAMESPACE_DECL) &&
(val2->nodeTab[i]->type == XML_NAMESPACE_DECL)) {
xmlNsPtr ns1, ns2;
ns1 = (xmlNsPtr) val1->nodeTab[j];
ns2 = (xmlNsPtr) val2->nodeTab[i];
if ((ns1->next == ns2->next) &&
(xmlStrEqual(ns1->prefix, ns2->prefix))) {
skip = 1;
break;
}
}
}
if (skip)
@ -1594,7 +1760,13 @@ xmlXPathNodeSetMerge(xmlNodeSetPtr val1, xmlNodeSetPtr val2) {
}
val1->nodeTab = temp;
}
val1->nodeTab[val1->nodeNr++] = val2->nodeTab[i];
if (val2->nodeTab[i]->type == XML_NAMESPACE_DECL) {
xmlNsPtr ns = (xmlNsPtr) val2->nodeTab[i];
val1->nodeTab[val1->nodeNr++] =
xmlXPathNodeSetDupNs((xmlNodePtr) ns->next, ns);
} else
val1->nodeTab[val1->nodeNr++] = val2->nodeTab[i];
}
return(val1);
@ -1628,6 +1800,9 @@ xmlXPathNodeSetDel(xmlNodeSetPtr cur, xmlNodePtr val) {
#endif
return;
}
if ((cur->nodeTab[i] != NULL) &&
(cur->nodeTab[i]->type == XML_NAMESPACE_DECL))
xmlXPathNodeSetFreeNs((xmlNsPtr) cur->nodeTab[i]);
cur->nodeNr--;
for (;i < cur->nodeNr;i++)
cur->nodeTab[i] = cur->nodeTab[i + 1];
@ -1645,6 +1820,9 @@ void
xmlXPathNodeSetRemove(xmlNodeSetPtr cur, int val) {
if (cur == NULL) return;
if (val >= cur->nodeNr) return;
if ((cur->nodeTab[val] != NULL) &&
(cur->nodeTab[val]->type == XML_NAMESPACE_DECL))
xmlXPathNodeSetFreeNs((xmlNsPtr) cur->nodeTab[val]);
cur->nodeNr--;
for (;val < cur->nodeNr;val++)
cur->nodeTab[val] = cur->nodeTab[val + 1];
@ -1661,6 +1839,13 @@ void
xmlXPathFreeNodeSet(xmlNodeSetPtr obj) {
if (obj == NULL) return;
if (obj->nodeTab != NULL) {
int i;
/* @@ with_ns to check wether namespace nodes should be looked at @@ */
for (i = 0;i < obj->nodeNr;i++)
if ((obj->nodeTab[i] != NULL) &&
(obj->nodeTab[i]->type == XML_NAMESPACE_DECL))
xmlXPathNodeSetFreeNs((xmlNsPtr) obj->nodeTab[i]);
xmlFree(obj->nodeTab);
}
xmlFree(obj);
@ -1678,11 +1863,17 @@ xmlXPathFreeValueTree(xmlNodeSetPtr obj) {
int i;
if (obj == NULL) return;
for (i = 0;i < obj->nodeNr;i++)
if (obj->nodeTab[i] != NULL)
xmlFreeNodeList(obj->nodeTab[i]);
if (obj->nodeTab != NULL) {
for (i = 0;i < obj->nodeNr;i++) {
if (obj->nodeTab[i] != NULL) {
if (obj->nodeTab[i]->type == XML_NAMESPACE_DECL) {
xmlXPathNodeSetFreeNs((xmlNsPtr) obj->nodeTab[i]);
} else {
xmlFreeNodeList(obj->nodeTab[i]);
}
}
}
xmlFree(obj->nodeTab);
}
xmlFree(obj);
@ -1752,6 +1943,7 @@ xmlXPathNewNodeSet(xmlNodePtr val) {
ret->type = XPATH_NODESET;
ret->boolval = 0;
ret->nodesetval = xmlXPathNodeSetCreate(val);
/* @@ with_ns to check wether namespace nodes should be looked at @@ */
return(ret);
}
@ -1792,22 +1984,22 @@ xmlXPathNewValueTree(xmlNodePtr val) {
* Returns the newly created object.
*/
xmlXPathObjectPtr
xmlXPathNewNodeSetList(xmlNodeSetPtr val) {
xmlXPathNewNodeSetList(xmlNodeSetPtr val)
{
xmlXPathObjectPtr ret;
int i;
if (val == NULL)
ret = NULL;
ret = NULL;
else if (val->nodeTab == NULL)
ret = xmlXPathNewNodeSet(NULL);
else
{
ret = xmlXPathNewNodeSet(val->nodeTab[0]);
for (i = 1; i < val->nodeNr; ++i)
xmlXPathNodeSetAddUnique(ret->nodesetval, val->nodeTab[i]);
}
ret = xmlXPathNewNodeSet(NULL);
else {
ret = xmlXPathNewNodeSet(val->nodeTab[0]);
for (i = 1; i < val->nodeNr; ++i)
xmlXPathNodeSetAddUnique(ret->nodesetval, val->nodeTab[i]);
}
return(ret);
return (ret);
}
/**
@ -4668,13 +4860,14 @@ xmlXPathNextParent(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) {
case XML_DOCB_DOCUMENT_NODE:
#endif
return(NULL);
case XML_NAMESPACE_DECL:
/*
* TODO !!! may require extending struct _xmlNs with
* parent field
* C.f. Infoset case...
*/
case XML_NAMESPACE_DECL: {
xmlNsPtr ns = (xmlNsPtr) ctxt->context->node;
if ((ns->next != NULL) &&
(ns->next->type != XML_NAMESPACE_DECL))
return((xmlNodePtr) ns->next);
return(NULL);
}
}
}
return(NULL);
@ -4734,13 +4927,15 @@ xmlXPathNextAncestor(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) {
case XML_DOCB_DOCUMENT_NODE:
#endif
return(NULL);
case XML_NAMESPACE_DECL:
/*
* TODO !!! may require extending struct _xmlNs with
* parent field
* C.f. Infoset case...
*/
case XML_NAMESPACE_DECL: {
xmlNsPtr ns = (xmlNsPtr) ctxt->context->node;
if ((ns->next != NULL) &&
(ns->next->type != XML_NAMESPACE_DECL))
return((xmlNodePtr) ns->next);
/* Bad, how did that namespace ended-up there ? */
return(NULL);
}
}
return(NULL);
}
@ -4779,9 +4974,8 @@ xmlXPathNextAncestor(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) {
return(NULL);
case XML_NAMESPACE_DECL:
/*
* TODO !!! may require extending struct _xmlNs with
* parent field
* C.f. Infoset case...
* this should not hapen a namespace can't be
* the ancestor of another node
*/
return(NULL);
}
@ -8260,7 +8454,8 @@ xmlXPathNodeCollectAndTest(xmlXPathParserContextPtr ctxt,
#ifdef DEBUG_STEP
n++;
#endif
addNode(list, cur);
xmlXPathNodeSetAddNs(list, ctxt->context->node,
(xmlNsPtr) cur);
}
} else {
if (cur->type == XML_ELEMENT_NODE) {
@ -8343,7 +8538,8 @@ xmlXPathNodeCollectAndTest(xmlXPathParserContextPtr ctxt,
#ifdef DEBUG_STEP
n++;
#endif
addNode(list, cur);
xmlXPathNodeSetAddNs(list,
ctxt->context->node, (xmlNsPtr) cur);
}
}
break;
@ -8674,7 +8870,8 @@ xmlXPathNodeCollectAndTestNth(xmlXPathParserContextPtr ctxt,
if (cur->type == XML_NAMESPACE_DECL) {
n++;
if (n == indx)
addNode(list, cur);
xmlXPathNodeSetAddNs(list, ctxt->context->node,
(xmlNsPtr) cur);
}
} else {
if (cur->type == XML_ELEMENT_NODE) {
@ -8748,7 +8945,8 @@ xmlXPathNodeCollectAndTestNth(xmlXPathParserContextPtr ctxt,
&& (xmlStrEqual(ns->prefix, name))) {
n++;
if (n == indx)
addNode(list, cur);
xmlXPathNodeSetAddNs(list,
ctxt->context->node, (xmlNsPtr) cur);
}
}
break;