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