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