--- loncom/xml/lonxml.pm 2001/11/29 19:03:58 1.139
+++ loncom/xml/lonxml.pm 2002/02/21 04:11:25 1.152
@@ -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.152 2002/02/21 04:11:25 albertel Exp $
#
# Copyright Michigan State University Board of Trustees
#
@@ -51,7 +51,12 @@
# 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
+# 1/2 Matthew Hall
+# 1/3 Gerd Kortemeyer
+#
package Apache::lonxml;
use vars
@@ -66,13 +71,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);
@@ -277,11 +290,14 @@ sub printtokenheader {
$reply{'generation'};
if ($target eq 'web') {
+ my %idhash=&Apache::lonnet::idrget($tudom,($tuname));
return
''.
'Checked out for '.$plainname.
'
User: '.$tuname.' at '.$tudom.
+ '
ID: '.$idhash{$tuname}.
'
CourseID: '.$tcrsid.
+ '
Course: '.$ENV{'course.'.$tcrsid.'.description'}.
'
DocID: '.$token.
'
Time: '.localtime().'
';
} else {
@@ -425,7 +441,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} }));
}
}
@@ -446,7 +463,7 @@ sub xmlparse {
($target, my @tenta) = split('&&',$target);
- my @stack = ();
+ my @stack = ();
my @parstack = ();
&initdepth;
@@ -463,11 +480,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 +512,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 +529,21 @@ 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']");
- &end_tag($stack,$parstack,$token);
+ my $lasttag=$$stack[-1];
+ if ($token->[1] =~ /^$lasttag$/i) {
+ &Apache::lonxml::warning('Using tag </'.$token->[1].'> as end tag to <'.$$stack[-1].'>');
+ last;
+ } else {
+ &Apache::lonxml::warning('Found tag </'.$token->[1].'> when looking for </'.$$stack[-1].'> 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 +553,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 +603,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') ) {
@@ -596,10 +619,16 @@ sub recurse {
$safeeval, $style_for_target);
} elsif ($tokenpat->[0] eq 'E') {
#clear out any tags that didn't end
- while ($tokenpat->[1] ne $innerstack[$#innerstack]
+ while ($tokenpat->[1] ne $innerstack[$#innerstack]
&& ($#innerstack > -1)) {
- &Apache::lonxml::warning("Unbalanced tags in resource $innerstack['-1']");
- &end_tag(\@innerstack,\@innerparstack,$tokenpat);
+ my $lasttag=$innerstack[-1];
+ if ($tokenpat->[1] =~ /^$lasttag$/i) {
+ &Apache::lonxml::warning('Using tag </'.$tokenpat->[1].'> as end tag to <'.$innerstack[-1].'>');
+ last;
+ } else {
+ &Apache::lonxml::warning('Found tag </'.$tokenpat->[1].'> when looking for </'.$innerstack[-1].'> in file');
+ &end_tag(\@innerstack,\@innerparstack,$tokenpat);
+ }
}
$partstring = &callsub("end_$tokenpat->[1]", $target, $tokenpat,
\@innerstack, \@innerparstack, \@pat,
@@ -632,6 +661,7 @@ sub recurse {
pop @pat;
pop @Apache::lonxml::pwd;
}
+ &Apache::lonxml::debug("Exiting Recursing");
return $output;
}
@@ -643,11 +673,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;
@@ -659,13 +689,13 @@ sub callsub {
}
if (!$deleted) {
if ($space) {
- #&Apache::lonxml::debug("Calling sub $sub in $space $metamode
\n");
+ #&Apache::lonxml::debug("Calling sub $sub in $space $metamode");
$sub1="$space\:\:$sub";
($currentstring,$nodefault) = &$sub1($target,$token,$tagstack,
$parstack,$parser,$safeeval,
$style);
} else {
- #&Apache::lonxml::debug("NOT Calling sub $sub in $space $metamode
\n");
+ #&Apache::lonxml::debug("NOT Calling sub $sub in $space $metamode");
if ($metamode <1) {
if (defined($token->[4]) && ($metamode < 1)) {
$currentstring = $token->[4];
@@ -843,7 +873,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);
@@ -867,10 +897,10 @@ sub get_all_text {
} elsif ($token->[0] eq 'PI') {
$result.=$token->[2];
} elsif ($token->[0] eq 'S') {
- if ($token->[1] eq $tag) { $depth++; }
+ if ($token->[1] =~ /^$tag$/i) { $depth++; }
$result.=$token->[4];
} elsif ($token->[0] eq 'E') {
- if ( $token->[1] eq $tag) { $depth--; }
+ if ( $token->[1] =~ /^$tag$/i) { $depth--; }
#skip sending back the last end tag
if ($depth > -1) { $result.=$token->[2]; } else {
$pars->unget_token($token);
@@ -885,7 +915,7 @@ sub get_all_text {
} elsif ($token->[0] eq 'PI') {
$result.=$token->[2];
} elsif ($token->[0] eq 'S') {
- if ( $token->[1] eq $tag) {
+ if ( $token->[1] =~ /^$tag$/i) {
$pars->unget_token($token); last;
} else {
$result.=$token->[4];
@@ -915,14 +945,14 @@ sub newparser {
sub parstring {
my ($token) = @_;
my $temp='';
- map {
+ foreach (@{$token->[3]}) {
unless ($_=~/\W/) {
my $val=$token->[2]->{$_};
- $val =~ s/([\%\@\\])/\\$1/g;
+ $val =~ s/([\%\@\\\"])/\\$1/g;
#if ($val =~ m/^[\%\@]/) { $val="\\".$val; }
$temp .= "my \$$_=\"$val\";"
}
- } @{$token->[3]};
+ }
return $temp;
}
@@ -935,10 +965,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 +978,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 +987,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)/\$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)/\$1\<\/a\>/gs;
- } split(/\,/,$ENV{'form.link'});
+ }
}
if ($ENV{'form.anchor'}) {
my $anchorname=$ENV{'form.anchor'};
@@ -993,13 +1023,13 @@ sub storefile {
if (my $fh=Apache::File->new('>'.$file)) {
print $fh $contents;
$fh->close();
+ } else {
+ &warning("Unable to save file $file");
}
}
-sub inserteditinfo {
- my ($result,$filecontents)=@_;
- unless ($filecontents) {
- $filecontents=(<
@@ -1013,28 +1043,56 @@ sub inserteditinfo {