Annotation of loncom/xml/lonxml.pm, revision 1.59
1.2 sakharuk 1: # The LearningOnline Network with CAPA
1.3 sakharuk 2: # XML Parser Module
1.2 sakharuk 3: #
1.3 sakharuk 4: # last modified 06/26/00 by Alexander Sakharuk
1.33 www 5: # 11/6 Gerd Kortemeyer
1.45 www 6: # 6/1/1 Gerd Kortemeyer
1.56 albertel 7: # 2/21,3/13 Guy
1.2 sakharuk 8:
1.4 albertel 9: package Apache::lonxml;
1.33 www 10: use vars
1.59 ! albertel 11: qw(@pwd @outputstack $redirection $textredirection $import @extlinks $metamode);
1.1 sakharuk 12: use strict;
13: use HTML::TokeParser;
1.3 sakharuk 14: use Safe;
1.40 albertel 15: use Safe::Hole;
1.13 albertel 16: use Opcode;
1.46 www 17: use Apache::Constants qw(:common);
1.7 albertel 18:
19: sub register {
20: my $space;
21: my @taglist;
22: my $temptag;
23: ($space,@taglist) = @_;
24: foreach $temptag (@taglist) {
25: $Apache::lonxml::alltags{$temptag}=$space;
26: }
27: }
1.48 albertel 28:
29: sub printalltags {
30: my $temp;
31: foreach $temp (sort keys %Apache::lonxml::alltags) {
32: &Apache::lonxml::debug("$temp -- $Apache::lonxml::alltags{$temp}");
33: }
34: }
1.4 albertel 35: use Apache::style;
1.3 sakharuk 36: use Apache::lontexconvert;
1.7 albertel 37: use Apache::run;
1.4 albertel 38: use Apache::londefdef;
1.7 albertel 39: use Apache::scripttag;
1.59 ! albertel 40: use Apache::edit;
1.3 sakharuk 41: #================================================== Main subroutine: xmlparse
1.33 www 42: @pwd=();
1.55 albertel 43: @outputstack = ();
44: $redirection = 0;
45: $import = 1;
1.33 www 46: @extlinks=();
1.59 ! albertel 47: $metamode = 0;
1.31 sakharuk 48:
1.3 sakharuk 49: sub xmlparse {
50:
1.18 albertel 51: my ($target,$content_file_string,$safeinit,%style_for_target) = @_;
1.41 albertel 52: if ($target eq 'meta') {
1.59 ! albertel 53: # meta mode is a bit weird only some output is to be turned off
! 54: $Apache::lonxml::redirection = 0;
! 55: $Apache::lonxml::metamode = 1;
1.55 albertel 56: $Apache::lonxml::import = 0;
1.47 albertel 57: } elsif ($target eq 'grade') {
1.55 albertel 58: &startredirection;
1.59 ! albertel 59: $Apache::lonxml::metamode = 0;
1.55 albertel 60: $Apache::lonxml::import = 1;
1.44 albertel 61: } else {
1.59 ! albertel 62: $Apache::lonxml::metamode = 0;
1.55 albertel 63: $Apache::lonxml::redirection = 0;
64: $Apache::lonxml::import = 1;
1.32 sakharuk 65: }
1.48 albertel 66: #&printalltags();
1.16 albertel 67: my @pars = ();
1.23 albertel 68: @Apache::lonxml::pwd=();
69: my $pwd=$ENV{'request.filename'};
70: $pwd =~ s:/[^/]*$::;
71: &newparser(\@pars,\$content_file_string,$pwd);
1.3 sakharuk 72: my $currentstring = '';
73: my $finaloutput = '';
74: my $newarg = '';
1.16 albertel 75: my $result;
1.24 sakharuk 76:
1.3 sakharuk 77: my $safeeval = new Safe;
1.40 albertel 78: my $safehole = new Safe::Hole;
1.6 albertel 79: $safeeval->permit("entereval");
1.13 albertel 80: $safeeval->permit(":base_math");
1.19 albertel 81: $safeeval->deny(":base_io");
1.40 albertel 82: $safehole->wrap(\&Apache::lonnet::EXT,$safeeval,'&EXT');
1.19 albertel 83: #need to inspect this class of ops
84: # $safeeval->deny(":base_orig");
1.21 albertel 85: $safeinit .= ';$external::target='.$target.';';
1.26 albertel 86: $safeinit .= ';$external::randomseed='.&Apache::lonnet::rndseed().';';
1.21 albertel 87: &Apache::run::run($safeinit,$safeeval);
1.3 sakharuk 88: #-------------------- Redefinition of the target in the case of compound target
89:
90: ($target, my @tenta) = split('&&',$target);
91:
92: my @stack = ();
93: my @parstack = ();
1.17 albertel 94: &initdepth;
1.3 sakharuk 95: my $token;
1.16 albertel 96: while ( $#pars > -1 ) {
97: while ($token = $pars[$#pars]->get_token) {
1.57 albertel 98: if (($token->[0] eq 'T') || ($token->[0] eq 'C') || ($token->[0] eq 'D') ) {
1.59 ! albertel 99: if (!$metamode) { $result=$token->[1]; }
1.57 albertel 100: } elsif ($token->[0] eq 'PI') {
1.59 ! albertel 101: if (!$metamode) { $result=$token->[2]; }
1.16 albertel 102: } elsif ($token->[0] eq 'S') {
103: # add tag to stack
104: push (@stack,$token->[1]);
105: # add parameters list to another stack
106: push (@parstack,&parstring($token));
1.19 albertel 107: &increasedepth($token);
1.16 albertel 108: if (exists $style_for_target{$token->[1]}) {
1.59 ! albertel 109: if ($metamode) {
! 110: $result = $style_for_target{$token->[1]};
! 111: } elsif ($Apache::lonxml::redirection) {
1.55 albertel 112: $Apache::lonxml::outputstack['-1'] .=
113: &recurse($style_for_target{$token->[1]},$target,$safeeval,
114: \%style_for_target,@parstack);
1.41 albertel 115: } else {
1.55 albertel 116: $finaloutput .= &recurse($style_for_target{$token->[1]},$target,
117: $safeeval,\%style_for_target,@parstack);
1.41 albertel 118: }
1.16 albertel 119: } else {
1.17 albertel 120: $result = &callsub("start_$token->[1]", $target, $token,\@parstack,
1.41 albertel 121: \@pars, $safeeval, \%style_for_target);
1.16 albertel 122: }
123: } elsif ($token->[0] eq 'E') {
124: #clear out any tags that didn't end
1.55 albertel 125: while ($token->[1] ne $stack[$#stack] && ($#stack > -1)) {
126: &Apache::lonxml::warning("Unbalanced tags in resource $stack['-1']");
1.43 albertel 127: pop @stack;pop @parstack;&decreasedepth($token);
128: }
1.16 albertel 129:
130: if (exists $style_for_target{'/'."$token->[1]"}) {
1.59 ! albertel 131: if ($metamode) {
! 132: $result = $style_for_target{$token->[1]};
! 133: } elsif ($Apache::lonxml::redirection) {
1.55 albertel 134: $Apache::lonxml::outputstack['-1'] .=
135: &recurse($style_for_target{'/'."$token->[1]"},
136: $target,$safeeval,\%style_for_target,@parstack);
137: } else {
138: $finaloutput .= &recurse($style_for_target{'/'."$token->[1]"},
139: $target,$safeeval,\%style_for_target,
140: @parstack);
141: }
1.59 ! albertel 142:
1.16 albertel 143: } else {
1.17 albertel 144: $result = &callsub("end_$token->[1]", $target, $token, \@parstack,
1.55 albertel 145: \@pars,$safeeval, \%style_for_target);
1.13 albertel 146: }
1.57 albertel 147: } else {
148: &Apache::lonxml::error("Unknown token event :$token->[0]:$token->[1]:");
1.16 albertel 149: }
1.55 albertel 150: #evaluate variable refs in result
1.25 sakharuk 151: if ($result ne "") {
1.24 sakharuk 152: if ( $#parstack > -1 ) {
1.55 albertel 153: if ($Apache::lonxml::redirection) {
154: $Apache::lonxml::outputstack['-1'] .=
155: &Apache::run::evaluate($result,$safeeval,$parstack[$#parstack]);
156: } else {
157: $finaloutput .= &Apache::run::evaluate($result,$safeeval,
158: $parstack[$#parstack]);
159: }
1.16 albertel 160: } else {
161: $finaloutput .= &Apache::run::evaluate($result,$safeeval,'');
1.13 albertel 162: }
1.16 albertel 163: $result = '';
1.55 albertel 164: }
165: if ($token->[0] eq 'E') {
166: pop @stack;pop @parstack;&decreasedepth($token);
1.2 sakharuk 167: }
1.5 albertel 168: }
1.16 albertel 169: pop @pars;
1.23 albertel 170: pop @Apache::lonxml::pwd;
1.3 sakharuk 171: }
1.24 sakharuk 172:
1.59 ! albertel 173: # if ($target eq 'meta') {
! 174: # $finaloutput.=&endredirection;
! 175: # }
1.3 sakharuk 176: return $finaloutput;
1.15 albertel 177: }
178:
179: sub recurse {
180:
181: my @innerstack = ();
182: my @innerparstack = ();
183: my ($newarg,$target,$safeeval,$style_for_target,@parstack) = @_;
1.16 albertel 184: my @pat = ();
1.23 albertel 185: &newparser(\@pat,\$newarg);
1.15 albertel 186: my $tokenpat;
187: my $partstring = '';
188: my $output='';
1.16 albertel 189: my $decls='';
190: while ( $#pat > -1 ) {
191: while ($tokenpat = $pat[$#pat]->get_token) {
1.57 albertel 192: if (($tokenpat->[0] eq 'T') || ($tokenpat->[0] eq 'C') || ($tokenpat->[0] eq 'D') ) {
1.55 albertel 193: $partstring = $tokenpat->[1];
1.57 albertel 194: } elsif ($tokenpat->[0] eq 'PI') {
1.58 albertel 195: $partstring = $tokenpat->[2];
1.16 albertel 196: } elsif ($tokenpat->[0] eq 'S') {
197: push (@innerstack,$tokenpat->[1]);
198: push (@innerparstack,&parstring($tokenpat));
1.19 albertel 199: &increasedepth($tokenpat);
1.16 albertel 200: $partstring = &callsub("start_$tokenpat->[1]",
201: $target, $tokenpat, \@innerparstack,
202: \@pat, $safeeval, $style_for_target);
203: } elsif ($tokenpat->[0] eq 'E') {
204: #clear out any tags that didn't end
205: while ($tokenpat->[1] ne $innerstack[$#innerstack]
1.43 albertel 206: && ($#innerstack > -1)) {
1.49 albertel 207: &Apache::lonxml::warning("Unbalanced tags in resource $innerstack['-1']");
1.43 albertel 208: pop @innerstack;pop @innerparstack;&decreasedepth($tokenpat);
209: }
1.16 albertel 210: $partstring = &callsub("end_$tokenpat->[1]",
211: $target, $tokenpat, \@innerparstack,
212: \@pat, $safeeval, $style_for_target);
1.57 albertel 213: } else {
214: &Apache::lonxml::error("Unknown token event :$tokenpat->[0]:$tokenpat->[1]:");
1.16 albertel 215: }
216: #pass both the variable to the style tag, and the tag we
217: #are processing inside the <definedtag>
218: if ( $partstring ne "" ) {
219: if ( $#parstack > -1 ) {
220: if ( $#innerparstack > -1 ) {
221: $decls= $parstack[$#parstack].$innerparstack[$#innerparstack];
222: } else {
223: $decls= $parstack[$#parstack];
224: }
225: } else {
226: if ( $#innerparstack > -1 ) {
227: $decls=$innerparstack[$#innerparstack];
228: } else {
229: $decls='';
230: }
231: }
232: $output .= &Apache::run::evaluate($partstring,$safeeval,$decls);
233: $partstring = '';
234: }
1.17 albertel 235: if ($tokenpat->[0] eq 'E') { pop @innerstack;pop @innerparstack;
1.19 albertel 236: &decreasedepth($tokenpat);}
1.15 albertel 237: }
1.16 albertel 238: pop @pat;
1.23 albertel 239: pop @Apache::lonxml::pwd;
1.15 albertel 240: }
241: return $output;
1.7 albertel 242: }
243:
244: sub callsub {
1.14 albertel 245: my ($sub,$target,$token,$parstack,$parser,$safeeval,$style)=@_;
1.7 albertel 246: my $currentstring='';
247: {
1.59 ! albertel 248: my $sub1;
1.7 albertel 249: no strict 'refs';
1.59 ! albertel 250: if ($target eq 'edit' && $token->[0] eq 'S') {
! 251: $currentstring = &Apache::edit::tag_start($target,$token,$parstack,$parser,
! 252: $safeeval,$style);
! 253: }
1.7 albertel 254: if (my $space=$Apache::lonxml::alltags{$token->[1]}) {
1.54 albertel 255: #&Apache::lonxml::debug("Calling sub $sub in $space<br />\n");
1.24 sakharuk 256: $sub1="$space\:\:$sub";
1.17 albertel 257: $Apache::lonxml::curdepth=join('_',@Apache::lonxml::depthcounter);
1.59 ! albertel 258: $currentstring .= &$sub1($target,$token,$parstack,$parser,
1.16 albertel 259: $safeeval,$style);
1.7 albertel 260: } else {
1.54 albertel 261: #&Apache::lonxml::debug("NOT Calling sub $sub in $space<br />\n");
1.59 ! albertel 262: if (defined($token->[4]) && !$metamode) {
! 263: $currentstring .= $token->[4];
1.7 albertel 264: } else {
1.59 ! albertel 265: $currentstring .= $token->[2];
1.7 albertel 266: }
1.59 ! albertel 267: }
! 268: if ($target eq 'edit' && $token->[0] eq 'E') {
! 269: $currentstring = &Apache::edit::tag_end($target,$token,$parstack,$parser,
! 270: $safeeval,$style);
1.7 albertel 271: }
272: use strict 'refs';
273: }
274: return $currentstring;
1.17 albertel 275: }
276:
1.55 albertel 277: sub startredirection {
278: $Apache::lonxml::redirection++;
279: push (@Apache::lonxml::outputstack, '');
280: }
281:
282: sub endredirection {
283: if (!$Apache::lonxml::redirection) {
284: &Apache::lonxml::error("Endredirection was called, before a startredirection, perhaps you have unbalanced tags. Some debuggin information:".join ":",caller);
285: return '';
286: }
287: $Apache::lonxml::redirection--;
288: pop @Apache::lonxml::outputstack;
289: }
290:
1.17 albertel 291: sub initdepth {
292: @Apache::lonxml::depthcounter=();
293: $Apache::lonxml::depth=-1;
294: $Apache::lonxml::olddepth=-1;
295: }
296:
297: sub increasedepth {
1.19 albertel 298: my ($token) = @_;
1.17 albertel 299: $Apache::lonxml::depth++;
300: $Apache::lonxml::depthcounter[$Apache::lonxml::depth]++;
301: if ($Apache::lonxml::depthcounter[$Apache::lonxml::depth]==1) {
302: $Apache::lonxml::olddepth=$Apache::lonxml::depth;
303: }
1.42 albertel 304: my $curdepth=join('_',@Apache::lonxml::depthcounter);
305: &Apache::lonxml::debug("s $Apache::lonxml::depth : $Apache::lonxml::olddepth : $curdepth : $token->[1]\n");
1.54 albertel 306: #print "<br />s $Apache::lonxml::depth : $Apache::lonxml::olddepth : $curdepth : $token->[1]\n";
1.17 albertel 307: }
308:
309: sub decreasedepth {
1.19 albertel 310: my ($token) = @_;
1.17 albertel 311: $Apache::lonxml::depth--;
1.36 albertel 312: if ($Apache::lonxml::depth<$Apache::lonxml::olddepth-1) {
313: $#Apache::lonxml::depthcounter--;
314: $Apache::lonxml::olddepth=$Apache::lonxml::depth+1;
315: }
1.43 albertel 316: if ( $Apache::lonxml::depth < -1) {
1.49 albertel 317: &Apache::lonxml::warning("Unbalanced tags in resource");
1.43 albertel 318: $Apache::lonxml::depth='-1';
319: }
1.42 albertel 320: my $curdepth=join('_',@Apache::lonxml::depthcounter);
321: &Apache::lonxml::debug("e $Apache::lonxml::depth : $Apache::lonxml::olddepth : $token->[1] : $curdepth\n");
1.54 albertel 322: #print "<br />e $Apache::lonxml::depth : $Apache::lonxml::olddepth : $token->[1] : $curdepth\n";
1.1 sakharuk 323: }
1.19 albertel 324:
325: sub get_all_text {
326:
327: my($tag,$pars)= @_;
328: my $depth=0;
329: my $token;
330: my $result='';
1.57 albertel 331: if ( $tag =~ m:^/: ) {
332: my $tag=substr($tag,1);
333: # &Apache::lonxml::debug("have:$tag:");
334: while (($depth >=0) && ($token = $pars->get_token)) {
335: # &Apache::lonxml::debug("e token:$token->[0]:$depth:$token->[1]");
336: if (($token->[0] eq 'T')||($token->[0] eq 'C')||($token->[0] eq 'D')) {
337: $result.=$token->[1];
338: } elsif ($token->[0] eq 'PI') {
339: $result.=$token->[2];
340: } elsif ($token->[0] eq 'S') {
341: if ($token->[1] eq $tag) { $depth++; }
342: $result.=$token->[4];
343: } elsif ($token->[0] eq 'E') {
344: if ( $token->[1] eq $tag) { $depth--; }
345: #skip sending back the last end tag
346: if ($depth > -1) { $result.=$token->[2]; } else {
347: $pars->unget_token($token);
348: }
349: }
350: }
351: } else {
352: while ($token = $pars->get_token) {
353: # &Apache::lonxml::debug("s token:$token->[0]:$depth:$token->[1]");
354: if (($token->[0] eq 'T')||($token->[0] eq 'C')||($token->[0] eq 'D')) {
355: $result.=$token->[1];
356: } elsif ($token->[0] eq 'PI') {
357: $result.=$token->[2];
358: } elsif ($token->[0] eq 'S') {
359: if ( $token->[1] eq $tag) {
360: $pars->unget_token($token); last;
361: } else {
362: $result.=$token->[4];
363: }
364: } elsif ($token->[0] eq 'E') {
365: $result.=$token->[2];
1.36 albertel 366: }
1.19 albertel 367: }
368: }
1.49 albertel 369: # &Apache::lonxml::debug("Exit:$result:");
1.19 albertel 370: return $result
371: }
372:
1.23 albertel 373: sub newparser {
374: my ($parser,$contentref,$dir) = @_;
375: push (@$parser,HTML::TokeParser->new($contentref));
1.56 albertel 376: $$parser['-1']->xml_mode('1');
1.23 albertel 377: if ( $dir eq '' ) {
378: push (@Apache::lonxml::pwd, $Apache::lonxml::pwd[$#Apache::lonxml::pwd]);
379: } else {
380: push (@Apache::lonxml::pwd, $dir);
381: }
382: # &Apache::lonxml::debug("pwd:$#Apache::lonxml::pwd");
383: # &Apache::lonxml::debug("pwd:$Apache::lonxml::pwd[$#Apache::lonxml::pwd]");
384: }
1.1 sakharuk 385:
1.8 albertel 386: sub parstring {
387: my ($token) = @_;
388: my $temp='';
1.20 albertel 389: map {
1.35 www 390: unless ($_=~/\W/) {
1.42 albertel 391: my $val=$token->[2]->{$_};
1.53 albertel 392: $val =~ s/([\%\@\\])/\\$1/g;
1.51 albertel 393: #if ($val =~ m/^[\%\@]/) { $val="\\".$val; }
1.42 albertel 394: $temp .= "my \$$_=\"$val\";"
1.20 albertel 395: }
396: } @{$token->[3]};
1.8 albertel 397: return $temp;
398: }
1.22 albertel 399:
1.34 www 400: sub writeallows {
401: my $thisurl='/res/'.&Apache::lonnet::declutter(shift);
402: my $thisdir=$thisurl;
403: $thisdir=~s/\/[^\/]+$//;
404: my %httpref=();
405: map {
406: $httpref{'httpref.'.
407: &Apache::lonnet::hreflocation($thisdir,$_)}=$thisurl; } @extlinks;
408: &Apache::lonnet::appenv(%httpref);
409: }
410:
1.24 sakharuk 411: sub handler {
412: my $request=shift;
1.44 albertel 413:
1.31 sakharuk 414: my $target='web';
1.48 albertel 415: $Apache::lonxml::debug=0;
1.25 sakharuk 416: if ($ENV{'browser.mathml'}) {
1.27 albertel 417: $request->content_type('text/xml');
418: } else {
419: $request->content_type('text/html');
1.25 sakharuk 420: }
1.29 sakharuk 421:
422: # $request->print(<<ENDHEADER);
423: #<html>
424: #<head>
425: #<title>Just test</title>
426: #</head>
427: #<body bgcolor="#FFFFFF">
428: #ENDHEADER
429: # &Apache::lonhomework::send_header($request);
1.27 albertel 430: $request->send_http_header;
431:
1.45 www 432: return OK if $request->header_only;
1.27 albertel 433:
434: $request->print(&Apache::lontexconvert::header());
435:
1.28 albertel 436: $request->print('<body bgcolor="#FFFFFF">'."\n");
1.27 albertel 437:
1.50 albertel 438: my $file=&Apache::lonnet::filelocation("",$request->uri);
1.24 sakharuk 439: my %mystyle;
1.50 albertel 440: my $result = '';
441: my $filecontents=&Apache::lonnet::getfile($file);
442: if ($filecontents == -1) {
443: &Apache::lonxml::error("<b> Unable to find <i>$file</i></b>");
444: $filecontents='';
445: } else {
446: $result = &Apache::lonxml::xmlparse($target,$filecontents,'',%mystyle);
447: }
1.24 sakharuk 448: $request->print($result);
1.29 sakharuk 449:
1.50 albertel 450:
1.28 albertel 451: $request->print('</body>');
452: $request->print(&Apache::lontexconvert::footer());
1.34 www 453: writeallows($request->uri);
1.45 www 454: return OK;
1.24 sakharuk 455: }
456:
1.22 albertel 457: $Apache::lonxml::debug=0;
458: sub debug {
459: if ($Apache::lonxml::debug eq 1) {
1.54 albertel 460: print "DEBUG:".$_[0]."<br />\n";
1.22 albertel 461: }
462: }
1.49 albertel 463:
1.22 albertel 464: sub error {
1.52 albertel 465: if ($Apache::lonxml::debug eq 1) {
1.55 albertel 466: print "<b>ERROR:</b>".$_[0]."<br />\n";
1.52 albertel 467: } else {
468: print "<b>An Error occured while processing this resource. The instructor has been notified.</b> <br />";
469: #notify author
470: &Apache::lonmsg::author_res_msg($ENV{'request.filename'},$_[0]);
471: #notify course
472: if ( $ENV{'request.course.id'} ) {
473: my $users=$ENV{'course.'.$ENV{'request.course.id'}.'.comment.email'};
474: foreach my $user (split /\,/, $users) {
475: ($user,my $domain) = split /:/, $user;
1.54 albertel 476: &Apache::lonmsg::user_normal_msg($user,$domain,"Error in $ENV{'request.filename'}",$_[0]);
1.52 albertel 477: }
478: }
479:
480: #FIXME probably shouldn't have me get everything forever.
1.54 albertel 481: &Apache::lonmsg::user_normal_msg('albertel','msu',"Error in $ENV{'request.filename'}",$_[0]);
482: #&Apache::lonmsg::user_normal_msg('albertel','103',"Error in $ENV{'request.filename'}",$_[0]);
1.52 albertel 483: }
1.22 albertel 484: }
1.49 albertel 485:
1.22 albertel 486: sub warning {
487: if ($Apache::lonxml::debug eq 1) {
1.55 albertel 488: print "<b>W</b>ARNING<b>:</b>".$_[0]."<br />\n";
1.22 albertel 489: }
490: }
491:
1.1 sakharuk 492: 1;
493: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>