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