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