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