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