--- loncom/xml/lonxml.pm	2001/11/29 19:03:58	1.139
+++ loncom/xml/lonxml.pm	2002/01/02 13:18:26	1.144
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # XML Parser Module 
 #
-# $Id: lonxml.pm,v 1.139 2001/11/29 19:03:58 www Exp $
+# $Id: lonxml.pm,v 1.144 2002/01/02 13:18:26 matthew Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -51,7 +51,10 @@
 # 8/7,8/9,8/10,8/11,8/15,8/16,8/17,8/18,8/20,8/23,8/24 Gerd Kortemeyer
 # Guy Albertelli
 # 9/26 Gerd Kortemeyer
-
+# Dec Guy Albertelli
+# YEAR=2002
+# 1/1 Gerd Kortemeyer
+#
 
 package Apache::lonxml; 
 use vars 
@@ -66,13 +69,21 @@ use Math::Random qw(:all);
 use Opcode;
 
 sub register {
-  my $space;
-  my @taglist;
-  my $temptag;
-  ($space,@taglist) = @_;
-  foreach $temptag (@taglist) {
-    $Apache::lonxml::alltags{$temptag}=$space;
+  my ($space,@taglist) = @_;
+  foreach my $temptag (@taglist) {
+    push(@{ $Apache::lonxml::alltags{$temptag} },$space);
+  }
+}
+
+sub deregister {
+  my ($space,@taglist) = @_;
+  foreach my $temptag (@taglist) {
+    my $tempspace = $Apache::lonxml::alltags{$temptag}[-1];
+    if ($tempspace eq $space) {
+      pop(@{ $Apache::lonxml::alltags{$temptag} });
+    }
   }
+  #&printalltags();
 }
 
 use Apache::Constants qw(:common);
@@ -425,7 +436,8 @@ sub unloadevents() {
 sub printalltags {
   my $temp;
   foreach $temp (sort keys %Apache::lonxml::alltags) {
-    &Apache::lonxml::debug("$temp -- $Apache::lonxml::alltags{$temp}");
+    &Apache::lonxml::debug("$temp -- ".
+		  join(',',@{ $Apache::lonxml::alltags{$temp} }));
   }
 }
 
@@ -463,11 +475,11 @@ sub htmlclean {
 
     my $tree = HTML::TreeBuilder->new;
     $tree->ignore_unknown(0);
-    
+
     $tree->parse($raw);
 
     my $output= $tree->as_HTML(undef,' ');
-     
+
     $output=~s/\<(br|hr|img|meta|allow)([^\>\/]*)\>/\<$1$2 \/\>/gis;
     $output=~s/\<\/(br|hr|img|meta|allow)\>//gis;
     unless ($full) {
@@ -495,14 +507,14 @@ sub inner_xmlparse {
 	  $result=$token->[2];
 	}
       } elsif ($token->[0] eq 'S') {
-	# add tag to stack 	    
+	# add tag to stack
 	push (@$stack,$token->[1]);
 	# add parameters list to another stack
 	push (@$parstack,&parstring($token));
-	&increasedepth($token);       
+	&increasedepth($token);
 	if (exists $$style_for_target{$token->[1]}) {
 	  if ($Apache::lonxml::redirection) {
-	    $Apache::lonxml::outputstack['-1'] .=  
+	    $Apache::lonxml::outputstack['-1'] .=
 	      &recurse($$style_for_target{$token->[1]},$target,$safeeval,
 		       $style_for_target,@$parstack);
 	  } else {
@@ -512,15 +524,15 @@ sub inner_xmlparse {
 	} else {
 	  $result = &callsub("start_$token->[1]", $target, $token, $stack,
 			     $parstack, $pars, $safeeval, $style_for_target);
-	}              
+	}
       } elsif ($token->[0] eq 'E') {
 	#clear out any tags that didn't end
 	while ($token->[1] ne $$stack['-1'] && ($#$stack > -1)) {
-	  &Apache::lonxml::warning("Unbalanced tags in resource $$stack['-1']");
+	  &Apache::lonxml::warning('Missing tag &lt;/'.$$stack['-1'].'&gt; in file');
 	  &end_tag($stack,$parstack,$token);
 	}
-		
-	if (exists $$style_for_target{'/'."$token->[1]"}) {
+
+	if (exists($$style_for_target{'/'."$token->[1]"})) {
 	  if ($Apache::lonxml::redirection) {
 	    $Apache::lonxml::outputstack['-1'] .=  
 	      &recurse($$style_for_target{'/'."$token->[1]"},
@@ -530,7 +542,6 @@ sub inner_xmlparse {
 				     $target,$safeeval,$style_for_target,
 				     @$parstack);
 	  }
-		    
 	} else {
 	  $result = &callsub("end_$token->[1]", $target, $token, $stack,
 			     $parstack, $pars,$safeeval, $style_for_target);
@@ -581,6 +592,7 @@ sub recurse {
   my $partstring = '';
   my $output='';
   my $decls='';
+  &Apache::lonxml::debug("Recursing");
   while ( $#pat > -1 ) {
     while  ($tokenpat = $pat[$#pat]->get_token) {
       if (($tokenpat->[0] eq 'T') || ($tokenpat->[0] eq 'C') || ($tokenpat->[0] eq 'D') ) {
@@ -598,7 +610,7 @@ sub recurse {
 	#clear out any tags that didn't end
 	while ($tokenpat->[1] ne $innerstack[$#innerstack] 
 	       && ($#innerstack > -1)) {
-	  &Apache::lonxml::warning("Unbalanced tags in resource $innerstack['-1']");
+	  &Apache::lonxml::warning('Missing tag &lt;/'.$innerstack['-1'].'&gt; in style');
 	  &end_tag(\@innerstack,\@innerparstack,$tokenpat);
 	}
 	$partstring = &callsub("end_$tokenpat->[1]", $target, $tokenpat,
@@ -632,6 +644,7 @@ sub recurse {
     pop @pat;
     pop @Apache::lonxml::pwd;
   }
+  &Apache::lonxml::debug("Exiting Recursing");
   return $output;
 }
 
@@ -643,11 +656,11 @@ sub callsub {
     my $sub1;
     no strict 'refs';
     my $tag=$token->[1];
-    my $space=$Apache::lonxml::alltags{$tag};
+    my $space=$Apache::lonxml::alltags{$tag}[-1];
     if (!$space) {
-	$tag=~tr/A-Z/a-z/;
+     	$tag=~tr/A-Z/a-z/;
 	$sub=~tr/A-Z/a-z/;
-	$space=$Apache::lonxml::alltags{$tag}
+	$space=$Apache::lonxml::alltags{$tag}[-1]
     }
 
     my $deleted=0;
@@ -843,7 +856,7 @@ sub decreasedepth {
     $Apache::lonxml::olddepth=$Apache::lonxml::depth+1;
   }
   if (  $Apache::lonxml::depth < -1) {
-    &Apache::lonxml::warning("Unbalanced tags in resource");   
+    &Apache::lonxml::warning("Missing tags, unable to properly run file.");
     $Apache::lonxml::depth='-1';
   }
   my $curdepth=join('_',@Apache::lonxml::depthcounter);
@@ -915,14 +928,14 @@ sub newparser {
 sub parstring {
   my ($token) = @_;
   my $temp='';
-  map {
+  foreach (@{$token->[3]}) {
     unless ($_=~/\W/) {
       my $val=$token->[2]->{$_};
       $val =~ s/([\%\@\\])/\\$1/g;
       #if ($val =~ m/^[\%\@]/) { $val="\\".$val; }
       $temp .= "my \$$_=\"$val\";"
     }
-  } @{$token->[3]};
+  }
   return $temp;
 }
 
@@ -935,10 +948,10 @@ sub writeallows {
     my $thisdir=$thisurl;
     $thisdir=~s/\/[^\/]+$//;
     my %httpref=();
-    map {
+    foreach (@extlinks) {
        $httpref{'httpref.'.
  	        &Apache::lonnet::hreflocation($thisdir,$_)}=$thisurl;
-    } @extlinks;
+    }
     @extlinks=();
     &Apache::lonnet::appenv(%httpref);
 }
@@ -948,7 +961,7 @@ sub writeallows {
 #
 sub afterburn {
     my $result=shift;
-    map {
+    foreach (split(/&/,$ENV{'QUERY_STRING'})) {
        my ($name, $value) = split(/=/,$_);
        $value =~ tr/+/ /;
        $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
@@ -957,22 +970,22 @@ sub afterburn {
               $ENV{'form.'.$name}=$value;
 	   }
        }
-    } (split(/&/,$ENV{'QUERY_STRING'}));
+    }
     if ($ENV{'form.highlight'}) {
-        map {
+       foreach (split(/\,/,$ENV{'form.highlight'})) {
            my $anchorname=$_;
 	   my $matchthis=$anchorname;
            $matchthis=~s/\_+/\\s\+/g;
            $result=~s/($matchthis)/\<font color=\"red\"\>$1\<\/font\>/gs;
-       } split(/\,/,$ENV{'form.highlight'});
+       }
     }
     if ($ENV{'form.link'}) {
-        map {
+       foreach (split(/\,/,$ENV{'form.link'})) {
            my ($anchorname,$linkurl)=split(/\>/,$_);
 	   my $matchthis=$anchorname;
            $matchthis=~s/\_+/\\s\+/g;
            $result=~s/($matchthis)/\<a href=\"$linkurl\"\>$1\<\/a\>/gs;
-       } split(/\,/,$ENV{'form.link'});
+       }
     }
     if ($ENV{'form.anchor'}) {
         my $anchorname=$ENV{'form.anchor'};
@@ -1043,9 +1056,9 @@ sub handler {
   } else {
     $request->content_type('text/html');
   }
-  &Apache::loncommon::no_cache($request); 
+  &Apache::loncommon::no_cache($request);
   $request->send_http_header;
-  
+
   return OK if $request->header_only;
 
 
@@ -1112,9 +1125,11 @@ sub error {
     #notify course
     if ( $ENV{'request.course.id'} ) {
       my $users=$ENV{'course.'.$ENV{'request.course.id'}.'.comment.email'};
+      my $declutter=&Apache::lonnet::declutter($ENV{'request.filename'});
       foreach my $user (split /\,/, $users) {
 	($user,my $domain) = split /:/, $user;
-	&Apache::lonmsg::user_normal_msg($user,$domain,"Error in $ENV{'request.filename'}",$_[0]);
+	&Apache::lonmsg::user_normal_msg($user,$domain,
+        "Error [$declutter]",$_[0]);
       }
     }
 
@@ -1183,7 +1198,7 @@ sub register_insert {
     my $line = $data[$i];
     my ($mnemonic,@which) = split(/ +/,$line);
     my $tag = $insertlist{"$tagnum.tag"};
-    for (my $j=0;$j <$#which;$j++) {
+    for (my $j=0;$j <=$#which;$j++) {
       if ( $which[$j] eq 'Y' ) {
 	if ($insertlist{"$j.show"} ne 'no') {
 	  push(@{ $insertlist{"$tag.which"} },$j);