Annotation of loncom/xml/lonxml.pm, revision 1.43
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.2 sakharuk 6:
1.4 albertel 7: package Apache::lonxml;
1.33 www 8: use vars
9: qw(@pwd $outputstack $redirection $textredirection $on_offimport @extlinks);
1.1 sakharuk 10: use strict;
11: use HTML::TokeParser;
1.3 sakharuk 12: use Safe;
1.40 albertel 13: use Safe::Hole;
1.13 albertel 14: use Opcode;
1.7 albertel 15:
16: sub register {
17: my $space;
18: my @taglist;
19: my $temptag;
20: ($space,@taglist) = @_;
21: foreach $temptag (@taglist) {
22: $Apache::lonxml::alltags{$temptag}=$space;
23: }
24: }
1.11 sakharuk 25:
1.4 albertel 26: use Apache::style;
1.3 sakharuk 27: use Apache::lontexconvert;
1.7 albertel 28: use Apache::run;
1.4 albertel 29: use Apache::londefdef;
1.7 albertel 30: use Apache::scripttag;
1.3 sakharuk 31: #================================================== Main subroutine: xmlparse
1.33 www 32: @pwd=();
33: $outputstack = '';
34: $redirection = 1;
35: $textredirection = 1;
36: $on_offimport = 0;
37: @extlinks=();
1.31 sakharuk 38:
1.3 sakharuk 39: sub xmlparse {
40:
1.18 albertel 41: my ($target,$content_file_string,$safeinit,%style_for_target) = @_;
1.41 albertel 42: if ($target eq 'meta') {
43: $Apache::lonxml::textredirection = 0;
44: $Apache::lonxml::on_offimport = 1;
1.32 sakharuk 45: }
1.16 albertel 46: my @pars = ();
1.23 albertel 47: @Apache::lonxml::pwd=();
48: my $pwd=$ENV{'request.filename'};
49: $pwd =~ s:/[^/]*$::;
50: &newparser(\@pars,\$content_file_string,$pwd);
1.3 sakharuk 51: my $currentstring = '';
52: my $finaloutput = '';
53: my $newarg = '';
1.16 albertel 54: my $result;
1.24 sakharuk 55:
1.3 sakharuk 56: my $safeeval = new Safe;
1.40 albertel 57: my $safehole = new Safe::Hole;
1.6 albertel 58: $safeeval->permit("entereval");
1.13 albertel 59: $safeeval->permit(":base_math");
1.19 albertel 60: $safeeval->deny(":base_io");
1.40 albertel 61: $safehole->wrap(\&Apache::lonnet::EXT,$safeeval,'&EXT');
1.19 albertel 62: #need to inspect this class of ops
63: # $safeeval->deny(":base_orig");
1.21 albertel 64: $safeinit .= ';$external::target='.$target.';';
1.26 albertel 65: $safeinit .= ';$external::randomseed='.&Apache::lonnet::rndseed().';';
1.21 albertel 66: &Apache::run::run($safeinit,$safeeval);
1.3 sakharuk 67: #-------------------- Redefinition of the target in the case of compound target
68:
69: ($target, my @tenta) = split('&&',$target);
70:
71: my @stack = ();
72: my @parstack = ();
1.17 albertel 73: &initdepth;
1.3 sakharuk 74: my $token;
1.16 albertel 75: while ( $#pars > -1 ) {
76: while ($token = $pars[$#pars]->get_token) {
77: if ($token->[0] eq 'T') {
1.41 albertel 78: if ($Apache::lonxml::textredirection == 1) {$result=$token->[1];}
79: # $finaloutput .= &Apache::run::evaluate($token->[1],$safeeval,'');
1.16 albertel 80: } elsif ($token->[0] eq 'S') {
1.41 albertel 81: # if ($target eq 'meta' and $token->[2]->{metaout} eq 'ON') {$Apache::lonxml::textredirection = 1;}
1.16 albertel 82: # add tag to stack
83: push (@stack,$token->[1]);
84: # add parameters list to another stack
85: push (@parstack,&parstring($token));
1.19 albertel 86: &increasedepth($token);
1.16 albertel 87: if (exists $style_for_target{$token->[1]}) {
1.41 albertel 88:
89: if ($Apache::lonxml::redirection == 1) {
90: $finaloutput .= &recurse($style_for_target{$token->[1]},
91: $target,$safeeval,\%style_for_target,
92: @parstack);
93: } else {
94: $Apache::lonxml::outputstack .= &recurse($style_for_target{$token->[1]},
95: $target,$safeeval,\%style_for_target,
96: @parstack);
97: }
98:
1.16 albertel 99: } else {
1.17 albertel 100: $result = &callsub("start_$token->[1]", $target, $token,\@parstack,
1.41 albertel 101: \@pars, $safeeval, \%style_for_target);
1.16 albertel 102: }
103: } elsif ($token->[0] eq 'E') {
1.41 albertel 104: #if ($target eq 'meta') {$Apache::lonxml::textredirection = 0;}
1.16 albertel 105: #clear out any tags that didn't end
106: while ($token->[1] ne $stack[$#stack]
1.43 ! albertel 107: && ($#stack > -1)) {
! 108: &Apache::lonxml::error("Unbalanced tags in resource $stack['-1']");
! 109: pop @stack;pop @parstack;&decreasedepth($token);
! 110: }
1.16 albertel 111:
112: if (exists $style_for_target{'/'."$token->[1]"}) {
1.24 sakharuk 113:
1.25 sakharuk 114: if ($Apache::lonxml::redirection == 1) {
1.16 albertel 115: $finaloutput .= &recurse($style_for_target{'/'."$token->[1]"},
116: $target,$safeeval,\%style_for_target,
117: @parstack);
1.24 sakharuk 118: } else {
1.25 sakharuk 119: $Apache::lonxml::outputstack .= &recurse($style_for_target{'/'."$token->[1]"},
1.24 sakharuk 120: $target,$safeeval,\%style_for_target,
121: @parstack);
122: }
123:
1.16 albertel 124: } else {
1.17 albertel 125: $result = &callsub("end_$token->[1]", $target, $token, \@parstack,
1.16 albertel 126: \@pars,$safeeval, \%style_for_target);
1.13 albertel 127: }
1.16 albertel 128: }
1.25 sakharuk 129: if ($result ne "") {
1.24 sakharuk 130: if ( $#parstack > -1 ) {
131:
1.25 sakharuk 132: if ($Apache::lonxml::redirection == 1) {
1.13 albertel 133: $finaloutput .= &Apache::run::evaluate($result,$safeeval,
134: $parstack[$#parstack]);
1.24 sakharuk 135: } else {
1.25 sakharuk 136: $Apache::lonxml::outputstack .= &Apache::run::evaluate($result,$safeeval,
1.24 sakharuk 137: $parstack[$#parstack]);
138: }
139:
1.16 albertel 140: } else {
141: $finaloutput .= &Apache::run::evaluate($result,$safeeval,'');
1.13 albertel 142: }
1.16 albertel 143: $result = '';
1.25 sakharuk 144: } else {
145: $finaloutput .= $result;
1.2 sakharuk 146: }
1.19 albertel 147: if ($token->[0] eq 'E') { pop @stack;pop @parstack;&decreasedepth($token);}
1.5 albertel 148: }
1.16 albertel 149: pop @pars;
1.23 albertel 150: pop @Apache::lonxml::pwd;
1.3 sakharuk 151: }
1.24 sakharuk 152:
1.3 sakharuk 153: return $finaloutput;
1.15 albertel 154: }
155:
156: sub recurse {
157:
158: my @innerstack = ();
159: my @innerparstack = ();
160: my ($newarg,$target,$safeeval,$style_for_target,@parstack) = @_;
1.16 albertel 161: my @pat = ();
1.23 albertel 162: &newparser(\@pat,\$newarg);
1.15 albertel 163: my $tokenpat;
164: my $partstring = '';
165: my $output='';
1.16 albertel 166: my $decls='';
167: while ( $#pat > -1 ) {
168: while ($tokenpat = $pat[$#pat]->get_token) {
169: if ($tokenpat->[0] eq 'T') {
1.30 sakharuk 170: if ($Apache::lonxml::textredirection == 1) {$partstring = $tokenpat->[1];}
1.16 albertel 171: } elsif ($tokenpat->[0] eq 'S') {
172: push (@innerstack,$tokenpat->[1]);
173: push (@innerparstack,&parstring($tokenpat));
1.19 albertel 174: &increasedepth($tokenpat);
1.16 albertel 175: $partstring = &callsub("start_$tokenpat->[1]",
176: $target, $tokenpat, \@innerparstack,
177: \@pat, $safeeval, $style_for_target);
178: } elsif ($tokenpat->[0] eq 'E') {
179: #clear out any tags that didn't end
180: while ($tokenpat->[1] ne $innerstack[$#innerstack]
1.43 ! albertel 181: && ($#innerstack > -1)) {
! 182: &Apache::lonxml::error("Unbalanced tags in resource $innerstack['-1']");
! 183: pop @innerstack;pop @innerparstack;&decreasedepth($tokenpat);
! 184: }
1.16 albertel 185: $partstring = &callsub("end_$tokenpat->[1]",
186: $target, $tokenpat, \@innerparstack,
187: \@pat, $safeeval, $style_for_target);
188: }
189: #pass both the variable to the style tag, and the tag we
190: #are processing inside the <definedtag>
191: if ( $partstring ne "" ) {
192: if ( $#parstack > -1 ) {
193: if ( $#innerparstack > -1 ) {
194: $decls= $parstack[$#parstack].$innerparstack[$#innerparstack];
195: } else {
196: $decls= $parstack[$#parstack];
197: }
198: } else {
199: if ( $#innerparstack > -1 ) {
200: $decls=$innerparstack[$#innerparstack];
201: } else {
202: $decls='';
203: }
204: }
205: $output .= &Apache::run::evaluate($partstring,$safeeval,$decls);
206: $partstring = '';
207: }
1.17 albertel 208: if ($tokenpat->[0] eq 'E') { pop @innerstack;pop @innerparstack;
1.19 albertel 209: &decreasedepth($tokenpat);}
1.15 albertel 210: }
1.16 albertel 211: pop @pat;
1.23 albertel 212: pop @Apache::lonxml::pwd;
1.15 albertel 213: }
214: return $output;
1.7 albertel 215: }
216:
217: sub callsub {
1.14 albertel 218: my ($sub,$target,$token,$parstack,$parser,$safeeval,$style)=@_;
1.7 albertel 219: my $currentstring='';
220: {
1.24 sakharuk 221: my $sub1;
1.7 albertel 222: no strict 'refs';
223: if (my $space=$Apache::lonxml::alltags{$token->[1]}) {
1.26 albertel 224: #&Apache::lonxml::debug("Calling sub $sub in $space<br>\n");
1.24 sakharuk 225: $sub1="$space\:\:$sub";
1.17 albertel 226: $Apache::lonxml::curdepth=join('_',@Apache::lonxml::depthcounter);
1.24 sakharuk 227: $currentstring = &$sub1($target,$token,$parstack,$parser,
1.16 albertel 228: $safeeval,$style);
1.7 albertel 229: } else {
1.23 albertel 230: #&Apache::lonxml::debug("NOT Calling sub $sub in $space<br>\n");
1.7 albertel 231: if (defined($token->[4])) {
232: $currentstring = $token->[4];
233: } else {
234: $currentstring = $token->[2];
235: }
236: }
237: use strict 'refs';
238: }
239: return $currentstring;
1.17 albertel 240: }
241:
242: sub initdepth {
243: @Apache::lonxml::depthcounter=();
244: $Apache::lonxml::depth=-1;
245: $Apache::lonxml::olddepth=-1;
246: }
247:
248: sub increasedepth {
1.19 albertel 249: my ($token) = @_;
1.17 albertel 250: $Apache::lonxml::depth++;
251: $Apache::lonxml::depthcounter[$Apache::lonxml::depth]++;
252: if ($Apache::lonxml::depthcounter[$Apache::lonxml::depth]==1) {
253: $Apache::lonxml::olddepth=$Apache::lonxml::depth;
254: }
1.42 albertel 255: my $curdepth=join('_',@Apache::lonxml::depthcounter);
256: &Apache::lonxml::debug("s $Apache::lonxml::depth : $Apache::lonxml::olddepth : $curdepth : $token->[1]\n");
1.43 ! albertel 257: #print "<br>s $Apache::lonxml::depth : $Apache::lonxml::olddepth : $curdepth : $token->[1]\n";
1.17 albertel 258: }
259:
260: sub decreasedepth {
1.19 albertel 261: my ($token) = @_;
1.17 albertel 262: $Apache::lonxml::depth--;
1.36 albertel 263: if ($Apache::lonxml::depth<$Apache::lonxml::olddepth-1) {
264: $#Apache::lonxml::depthcounter--;
265: $Apache::lonxml::olddepth=$Apache::lonxml::depth+1;
266: }
1.43 ! albertel 267: if ( $Apache::lonxml::depth < -1) {
! 268: &Apache::lonxml::error("Unbalanced tags in resource");
! 269: $Apache::lonxml::depth='-1';
! 270: }
1.42 albertel 271: my $curdepth=join('_',@Apache::lonxml::depthcounter);
272: &Apache::lonxml::debug("e $Apache::lonxml::depth : $Apache::lonxml::olddepth : $token->[1] : $curdepth\n");
1.43 ! albertel 273: #print "<br>e $Apache::lonxml::depth : $Apache::lonxml::olddepth : $token->[1] : $curdepth\n";
1.1 sakharuk 274: }
1.19 albertel 275:
276: sub get_all_text {
277:
278: my($tag,$pars)= @_;
279: my $depth=0;
280: my $token;
281: my $result='';
1.24 sakharuk 282: my $tag=substr($tag,1); #strip the / off the tag
283: # &Apache::lonxml::debug("have:$tag:");
1.19 albertel 284: while (($depth >=0) && ($token = $pars->get_token)) {
285: if ($token->[0] eq 'T') {
286: $result.=$token->[1];
287: } elsif ($token->[0] eq 'S') {
288: if ($token->[1] eq $tag) { $depth++; }
289: $result.=$token->[4];
290: } elsif ($token->[0] eq 'E') {
1.24 sakharuk 291: if ( $token->[1] eq $tag) { $depth--; }
1.19 albertel 292: #skip sending back the last end tag
1.36 albertel 293: if ($depth > -1) { $result.=$token->[2]; } else {
294: $pars->unget_token($token);
295: }
1.19 albertel 296: }
297: }
298: return $result
299: }
300:
1.23 albertel 301: sub newparser {
302: my ($parser,$contentref,$dir) = @_;
303: push (@$parser,HTML::TokeParser->new($contentref));
304: if ( $dir eq '' ) {
305: push (@Apache::lonxml::pwd, $Apache::lonxml::pwd[$#Apache::lonxml::pwd]);
306: } else {
307: push (@Apache::lonxml::pwd, $dir);
308: }
309: # &Apache::lonxml::debug("pwd:$#Apache::lonxml::pwd");
310: # &Apache::lonxml::debug("pwd:$Apache::lonxml::pwd[$#Apache::lonxml::pwd]");
311: }
1.1 sakharuk 312:
1.8 albertel 313: sub parstring {
314: my ($token) = @_;
315: my $temp='';
1.20 albertel 316: map {
1.35 www 317: unless ($_=~/\W/) {
1.42 albertel 318: my $val=$token->[2]->{$_};
319: if ($val =~ m/^[\%\@]/) { $val="\\".$val; }
320: $temp .= "my \$$_=\"$val\";"
1.20 albertel 321: }
322: } @{$token->[3]};
1.8 albertel 323: return $temp;
324: }
1.22 albertel 325:
1.34 www 326: sub writeallows {
327: my $thisurl='/res/'.&Apache::lonnet::declutter(shift);
328: my $thisdir=$thisurl;
329: $thisdir=~s/\/[^\/]+$//;
330: my %httpref=();
331: map {
332: $httpref{'httpref.'.
333: &Apache::lonnet::hreflocation($thisdir,$_)}=$thisurl; } @extlinks;
334: &Apache::lonnet::appenv(%httpref);
335: }
336:
1.24 sakharuk 337: sub handler {
338: my $request=shift;
339:
1.31 sakharuk 340: my $target='web';
1.43 ! albertel 341: # $Apache::lonxml::debug=1;
1.25 sakharuk 342: if ($ENV{'browser.mathml'}) {
1.27 albertel 343: $request->content_type('text/xml');
344: } else {
345: $request->content_type('text/html');
1.25 sakharuk 346: }
1.29 sakharuk 347:
348: # $request->print(<<ENDHEADER);
349: #<html>
350: #<head>
351: #<title>Just test</title>
352: #</head>
353: #<body bgcolor="#FFFFFF">
354: #ENDHEADER
355: # &Apache::lonhomework::send_header($request);
1.27 albertel 356: $request->send_http_header;
357:
1.28 albertel 358: return 'OK' if $request->header_only;
1.27 albertel 359:
360: $request->print(&Apache::lontexconvert::header());
361:
1.28 albertel 362: $request->print('<body bgcolor="#FFFFFF">'."\n");
1.27 albertel 363:
1.24 sakharuk 364: my $file = "/home/httpd/html".$request->uri;
365: my %mystyle;
366: my $result = '';
367: $result = Apache::lonxml::xmlparse($target, &Apache::lonnet::getfile($file),'',%mystyle);
368: $request->print($result);
1.29 sakharuk 369:
1.28 albertel 370: $request->print('</body>');
371: $request->print(&Apache::lontexconvert::footer());
1.34 www 372: writeallows($request->uri);
1.28 albertel 373: return 'OK';
1.24 sakharuk 374: }
375:
1.22 albertel 376: $Apache::lonxml::debug=0;
377: sub debug {
378: if ($Apache::lonxml::debug eq 1) {
379: print "DEBUG:".$_[0]."<br>\n";
380: }
381: }
382: sub error {
1.23 albertel 383: print "ERROR:".$_[0]."<br>\n";
1.22 albertel 384: }
385: sub warning {
386: if ($Apache::lonxml::debug eq 1) {
387: print "WARNING:".$_[0]."<br>\n";
388: }
389: }
390:
1.1 sakharuk 391: 1;
392: __END__
1.25 sakharuk 393:
394:
395:
396:
397:
398:
399:
400:
401:
402:
403:
404:
405:
1.11 sakharuk 406:
407:
408:
409:
410:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>