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