fix some recursion problems introduced in the last release. more debugging

* SAX.c parser.c: fix some recursion problems introduced in the
  last release.
* relaxng.c: more debugging of the RNG validation engine, still
  problems though.
Daniel
diff --git a/ChangeLog b/ChangeLog
index 718a65b..23a87b4 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+Tue Mar 11 12:08:23 CET 2003 Daniel Veillard <daniel@veillard.com>
+
+	* SAX.c parser.c: fix some recursion problems introduced in the
+	  last release.
+	* relaxng.c: more debugging of the RNG validation engine, still
+	  problems though.
+
 Mon Mar 10 14:10:47 CET 2003 Daniel Veillard <daniel@veillard.com>
 
 	* Makefile.am: stop generating wrong result file with * in name
diff --git a/SAX.c b/SAX.c
index 24e679d..0f2a029 100644
--- a/SAX.c
+++ b/SAX.c
@@ -378,14 +378,26 @@
 	((ctxt->validate) || (ctxt->replaceEntities)) &&
 	(ret->children == NULL) &&
 	(ret->etype == XML_EXTERNAL_GENERAL_PARSED_ENTITY)) {
+	int val;
+
 	/*
 	 * for validation purposes we really need to fetch and
 	 * parse the external entity
 	 */
 	xmlNodePtr children;
 
-        xmlParseCtxtExternalEntity(ctxt, ret->URI, ret->ExternalID, &children);
-	xmlAddChildList((xmlNodePtr) ret, children);
+        val = xmlParseCtxtExternalEntity(ctxt, ret->URI,
+		                         ret->ExternalID, &children);
+	if (val == 0) {
+	    xmlAddChildList((xmlNodePtr) ret, children);
+	} else {
+	    ctxt->sax->error(ctxt, 
+	     "Failure to process entity %s\n", name);
+	    ctxt->wellFormed = 0;
+	    ctxt->valid = 0;
+	    ctxt->validate = 0;
+	    return(NULL);
+	}
 	ret->owner = 1;
     }
     return(ret);
diff --git a/parser.c b/parser.c
index ba9327c..83db22b 100644
--- a/parser.c
+++ b/parser.c
@@ -5753,9 +5753,10 @@
 		if (ctxt->sax != NULL) {
 		    if (ctxt->sax->getEntity != NULL)
 			ent = ctxt->sax->getEntity(ctxt->userData, name);
-		    if (ent == NULL)
+		    if ((ctxt->wellFormed == 1 ) && (ent == NULL))
 		        ent = xmlGetPredefinedEntity(name);
-		    if ((ent == NULL) && (ctxt->userData==ctxt)) {
+		    if ((ctxt->wellFormed == 1 ) && (ent == NULL) &&
+			(ctxt->userData==ctxt)) {
 			ent = getEntity(ctxt, name);
 		    }
 		}
diff --git a/relaxng.c b/relaxng.c
index b1d4d47..81df430 100644
--- a/relaxng.c
+++ b/relaxng.c
@@ -46,7 +46,7 @@
     (xmlStrEqual(node->ns->href, xmlRelaxNGNs)))
 
 
-/* #define DEBUG 1 */               /* very verbose output */
+/* #define DEBUG 1 */
 /* #define DEBUG_GRAMMAR 1 */
 /* #define DEBUG_CONTENT 1 */
 /* #define DEBUG_TYPE 1 */
@@ -2191,7 +2191,9 @@
 
 
 #define IS_BLANK_NODE(n)						\
-    (((n)->type == XML_TEXT_NODE) && (xmlRelaxNGIsBlank((n)->content)))
+    ((((n)->type == XML_TEXT_NODE) ||					\
+      ((n)->type == XML_CDATA_SECTION_NODE)) &&				\
+     (xmlRelaxNGIsBlank((n)->content)))
 
 /**
  * xmlRelaxNGIsBlank:
@@ -2331,7 +2333,8 @@
     }
     if (node->children == NULL) {
 	def->value = xmlStrdup(BAD_CAST "");
-    } else if ((node->children->type != XML_TEXT_NODE) ||
+    } else if (((node->children->type != XML_TEXT_NODE) &&
+	        (node->children->type != XML_CDATA_SECTION_NODE)) ||
 	       (node->children->next != NULL)) {
 	if (ctxt->error != NULL)
 	    ctxt->error(ctxt->userData,
@@ -5738,7 +5741,8 @@
 	/*
 	 * Simplification 4.2 whitespaces
 	 */
-	else if (cur->type == XML_TEXT_NODE) {
+	else if ((cur->type == XML_TEXT_NODE) ||
+		 (cur->type == XML_CDATA_SECTION_NODE)) {
 	    if (IS_BLANK_NODE(cur)) {
 	        if (cur->parent->type == XML_ELEMENT_NODE) {
 		    if ((!xmlStrEqual(cur->parent->name, BAD_CAST "value")) &&
@@ -5749,7 +5753,7 @@
 		    goto skip_children;
 		}
 	    }
-	} else if (cur->type != XML_CDATA_SECTION_NODE) {
+	} else {
 	    delete = cur;
 	    goto skip_children;
 	}
@@ -6220,7 +6224,8 @@
     while ((node != NULL) &&
 	   ((node->type == XML_COMMENT_NODE) ||
 	    (node->type == XML_PI_NODE) ||
-	    ((node->type == XML_TEXT_NODE) &&
+	    (((node->type == XML_TEXT_NODE) || 
+	      (node->type == XML_CDATA_SECTION_NODE)) &&
 	     (IS_BLANK_NODE(node))))) {
 	node = node->next;
     }
@@ -6874,7 +6879,8 @@
 			return(1);
 		}
 	    }
-	} else if ((node->type == XML_TEXT_NODE) &&
+	} else if (((node->type == XML_TEXT_NODE) ||
+		    (node->type == XML_CDATA_SECTION_NODE)) &&
 		   (cur->type == XML_RELAXNG_TEXT)) {
 	    return(1);
 	}
@@ -7188,12 +7194,14 @@
     switch (define->type) {
         case XML_RELAXNG_EMPTY:
 	    node = xmlRelaxNGSkipIgnored(ctxt, node);
+#if 0
 	    if (node != NULL) {
 		VALID_ERR2(XML_RELAXNG_ERR_ELEMNOTEMPTY,
 			     ctxt->state->node->name);
 		ret = -1;
 		break;
 	    }
+#endif
 	    ret = 0;
 	    break;
         case XML_RELAXNG_NOT_ALLOWED:
@@ -7378,9 +7386,11 @@
         case XML_RELAXNG_CHOICE: {
 	    xmlRelaxNGDefinePtr list = define->content;
 	    int success = 0;
+	    xmlRelaxNGValidStatePtr sstate = NULL;
 
 	    oldflags = ctxt->flags;
 	    ctxt->flags |= FLAGS_IGNORABLE;
+	    node = xmlRelaxNGSkipIgnored(ctxt, node);
 
 	    while (list != NULL) {
 		oldstate = xmlRelaxNGCopyValidState(ctxt, ctxt->state);
@@ -7392,6 +7402,10 @@
 			 * to make more progresses
 			 */
 			success = 1;
+			if (sstate != NULL) {
+			    xmlRelaxNGFreeValidState(sstate);
+			}
+			sstate = xmlRelaxNGCopyValidState(ctxt, ctxt->state);
 		    } else {
 			xmlRelaxNGFreeValidState(oldstate);
 			break;
@@ -7402,8 +7416,15 @@
 		list = list->next;
 	    }
 	    ctxt->flags = oldflags;
-	    if (success == 1)
+	    if (success == 1) {
+		if (ret != 0) {
+		    xmlRelaxNGFreeValidState(ctxt->state);
+		    ctxt->state = sstate;
+		} else {
+		    xmlRelaxNGFreeValidState(sstate);
+		}
 		ret = 0;
+	    }
 	    if (ret != 0) {
 		if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
 		    xmlRelaxNGDumpValidError(ctxt);