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