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