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