Submitted By: Nico R. <n-roeser@gmx.net>
Date: 2004-10-15
Initial Package Version: 2.6.14
Upstream Status: From Upstream
Origin: Upstream CVS
Description: This patch fixes GNOME bug 152623
             <URL:http://bugzilla.gnome.org/show_bug.cgi?id=152623>. We hit this
             bug on 2004-09-14 when validating the LFS book failed: xmllint
             displayed the filename of the file which used xinclude to include
             the buggy XML file, not the filename of the file which actually
             contained the source of the validation problem. See
             <URL:http://linuxfromscratch.org/pipermail/lfs-dev/2004-September/048839.html>
             for the discussion thread on lfs-dev.

diff -Naur libxml2-2.6.14.orig/ChangeLog libxml2-2.6.14/ChangeLog
--- libxml2-2.6.14.orig/ChangeLog	2004-09-29 13:43:47.000000000 +0000
+++ libxml2-2.6.14/ChangeLog	2004-10-15 06:08:08.000000000 +0000
@@ -1,3 +1,8 @@
+Fri Oct  1 20:37:25 PDT 2004 William Brack <wbrack@mmm.com.hk>
+
+	* error.c: added some coding to attempt to display which file
+	  contains an error when using XInclude (bug 152623)
+
 Wed Sep 29 15:00:11 CEST 2004 Kasimier Buchcik <libxml2-cvs@cazic.net>
 
 	* xmlschemas.c include/libxml/xmlerror.h include/libxml/xmlschemas.h
diff -Naur libxml2-2.6.14.orig/error.c libxml2-2.6.14/error.c
--- libxml2-2.6.14.orig/error.c	2004-08-14 10:50:30.000000000 +0000
+++ libxml2-2.6.14/error.c	2004-10-15 06:08:08.000000000 +0000
@@ -447,7 +447,7 @@
     char *str = NULL;
     xmlParserInputPtr input = NULL;
     xmlErrorPtr to = &xmlLastError;
-    xmlChar *base = NULL;
+    xmlNodePtr baseptr = NULL;
 
     if ((xmlGetWarningsDefaultValue == 0) && (level == XML_ERR_WARNING))
         return;
@@ -509,14 +509,14 @@
 	int i;
 
 	if ((node->doc != NULL) && (node->doc->URL != NULL))
-	    base = xmlStrdup(node->doc->URL);
+	    baseptr = node;
 	for (i = 0;
 	     ((i < 10) && (node != NULL) && (node->type != XML_ELEMENT_NODE));
 	     i++)
 	     node = node->parent;
-        if ((base == NULL) && (node != NULL) &&
+        if ((baseptr == NULL) && (node != NULL) &&
 	    (node->doc != NULL) && (node->doc->URL != NULL))
-	    base = xmlStrdup(node->doc->URL);
+	    baseptr = node;
 
 	if ((node != NULL) && (node->type == XML_ELEMENT_NODE))
 	    line = node->line;
@@ -532,9 +532,33 @@
     to->level = level;
     if (file != NULL)
         to->file = (char *) xmlStrdup((const xmlChar *) file);
-    else if (base != NULL) {
-        to->file = (char *) base;
-	file = (char *) base;
+    else if (baseptr != NULL) {
+#ifdef LIBXML_XINCLUDE_ENABLED
+	/*
+	 * We check if the error is within an XInclude section and,
+	 * if so, attempt to print out the href of the XInclude instead
+	 * of the usual "base" (doc->URL) for the node (bug 152623).
+	 */
+        xmlNodePtr prev = baseptr;
+	int inclcount = 0;
+	while (prev != NULL) {
+	    if (prev->prev == NULL)
+	        prev = prev->parent;
+	    else {
+	        prev = prev->prev;
+		if (prev->type == XML_XINCLUDE_START) {
+		    if (--inclcount < 0)
+		        break;
+		} else if (prev->type == XML_XINCLUDE_END)
+		    inclcount++;
+	    }
+	}
+	if (prev != NULL) {
+	    to->file = (char *) xmlGetProp(prev, BAD_CAST "href");
+	} else
+#endif
+	    to->file = (char *) xmlStrdup(baseptr->doc->URL);
+	file = to->file;
     }
     to->line = line;
     if (str1 != NULL)
