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: sub printalltags {
29: my $temp;
30: foreach $temp (sort keys %Apache::lonxml::alltags) {
31: &Apache::lonxml::debug("$temp -- $Apache::lonxml::alltags{$temp}");
32: }
33: }
34:
35: use Apache::style;
36: use Apache::lontexconvert;
37: use Apache::run;
38: use Apache::londefdef;
39: use Apache::scripttag;
40: #================================================== Main subroutine: xmlparse
41: @pwd=();
42: $outputstack = '';
43: $redirection = 1;
44: $textredirection = 1;
45: $on_offimport = 0;
46: @extlinks=();
47:
48: sub xmlparse {
49:
50: my ($target,$content_file_string,$safeinit,%style_for_target) = @_;
51: if ($target eq 'meta') {
52: $Apache::lonxml::textredirection = 0;
53: $Apache::lonxml::on_offimport = 1;
54: } elsif ($target eq 'grade') {
55: $Apache::lonxml::textredirection = 0;
56: $Apache::lonxml::on_offimport = 0;
57: } else {
58: $Apache::lonxml::textredirection = 1;
59: $Apache::lonxml::on_offimport = 0;
60: }
61: #&printalltags();
62: my @pars = ();
63: @Apache::lonxml::pwd=();
64: my $pwd=$ENV{'request.filename'};
65: $pwd =~ s:/[^/]*$::;
66: &newparser(\@pars,\$content_file_string,$pwd);
67: my $currentstring = '';
68: my $finaloutput = '';
69: my $newarg = '';
70: my $result;
71:
72: my $safeeval = new Safe;
73: my $safehole = new Safe::Hole;
74: $safeeval->permit("entereval");
75: $safeeval->permit(":base_math");
76: $safeeval->deny(":base_io");
77: $safehole->wrap(\&Apache::lonnet::EXT,$safeeval,'&EXT');
78: #need to inspect this class of ops
79: # $safeeval->deny(":base_orig");
80: $safeinit .= ';$external::target='.$target.';';
81: $safeinit .= ';$external::randomseed='.&Apache::lonnet::rndseed().';';
82: &Apache::run::run($safeinit,$safeeval);
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 = ();
89: &initdepth;
90: my $token;
91: while ( $#pars > -1 ) {
92: while ($token = $pars[$#pars]->get_token) {
93: if ($token->[0] eq 'T') {
94: if ($Apache::lonxml::textredirection == 1) {$result=$token->[1];}
95: # $finaloutput .= &Apache::run::evaluate($token->[1],$safeeval,'');
96: } elsif ($token->[0] eq 'S') {
97: # if ($target eq 'meta' and $token->[2]->{metaout} eq 'ON') {$Apache::lonxml::textredirection = 1;}
98: # add tag to stack
99: push (@stack,$token->[1]);
100: # add parameters list to another stack
101: push (@parstack,&parstring($token));
102: &increasedepth($token);
103: if (exists $style_for_target{$token->[1]}) {
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:
115: } else {
116: $result = &callsub("start_$token->[1]", $target, $token,\@parstack,
117: \@pars, $safeeval, \%style_for_target);
118: }
119: } elsif ($token->[0] eq 'E') {
120: #if ($target eq 'meta') {$Apache::lonxml::textredirection = 0;}
121: #clear out any tags that didn't end
122: while ($token->[1] ne $stack[$#stack]
123: && ($#stack > -1)) {
124: &Apache::lonxml::error("Unbalanced tags in resource $stack['-1']");
125: pop @stack;pop @parstack;&decreasedepth($token);
126: }
127:
128: if (exists $style_for_target{'/'."$token->[1]"}) {
129:
130: if ($Apache::lonxml::redirection == 1) {
131: $finaloutput .= &recurse($style_for_target{'/'."$token->[1]"},
132: $target,$safeeval,\%style_for_target,
133: @parstack);
134: } else {
135: $Apache::lonxml::outputstack .= &recurse($style_for_target{'/'."$token->[1]"},
136: $target,$safeeval,\%style_for_target,
137: @parstack);
138: }
139:
140: } else {
141: $result = &callsub("end_$token->[1]", $target, $token, \@parstack,
142: \@pars,$safeeval, \%style_for_target);
143: }
144: }
145: if ($result ne "") {
146: if ( $#parstack > -1 ) {
147:
148: if ($Apache::lonxml::redirection == 1) {
149: $finaloutput .= &Apache::run::evaluate($result,$safeeval,
150: $parstack[$#parstack]);
151: } else {
152: $Apache::lonxml::outputstack .= &Apache::run::evaluate($result,$safeeval,
153: $parstack[$#parstack]);
154: }
155:
156: } else {
157: $finaloutput .= &Apache::run::evaluate($result,$safeeval,'');
158: }
159: $result = '';
160: } else {
161: $finaloutput .= $result;
162: }
163: if ($token->[0] eq 'E') { pop @stack;pop @parstack;&decreasedepth($token);}
164: }
165: pop @pars;
166: pop @Apache::lonxml::pwd;
167: }
168:
169: return $finaloutput;
170: }
171:
172: sub recurse {
173:
174: my @innerstack = ();
175: my @innerparstack = ();
176: my ($newarg,$target,$safeeval,$style_for_target,@parstack) = @_;
177: my @pat = ();
178: &newparser(\@pat,\$newarg);
179: my $tokenpat;
180: my $partstring = '';
181: my $output='';
182: my $decls='';
183: while ( $#pat > -1 ) {
184: while ($tokenpat = $pat[$#pat]->get_token) {
185: if ($tokenpat->[0] eq 'T') {
186: if ($Apache::lonxml::textredirection == 1) {$partstring = $tokenpat->[1];}
187: } elsif ($tokenpat->[0] eq 'S') {
188: push (@innerstack,$tokenpat->[1]);
189: push (@innerparstack,&parstring($tokenpat));
190: &increasedepth($tokenpat);
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]
197: && ($#innerstack > -1)) {
198: &Apache::lonxml::error("Unbalanced tags in resource $innerstack['-1']");
199: pop @innerstack;pop @innerparstack;&decreasedepth($tokenpat);
200: }
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: }
224: if ($tokenpat->[0] eq 'E') { pop @innerstack;pop @innerparstack;
225: &decreasedepth($tokenpat);}
226: }
227: pop @pat;
228: pop @Apache::lonxml::pwd;
229: }
230: return $output;
231: }
232:
233: sub callsub {
234: my ($sub,$target,$token,$parstack,$parser,$safeeval,$style)=@_;
235: my $currentstring='';
236: {
237: my $sub1;
238: no strict 'refs';
239: if (my $space=$Apache::lonxml::alltags{$token->[1]}) {
240: #&Apache::lonxml::debug("Calling sub $sub in $space<br>\n");
241: $sub1="$space\:\:$sub";
242: $Apache::lonxml::curdepth=join('_',@Apache::lonxml::depthcounter);
243: $currentstring = &$sub1($target,$token,$parstack,$parser,
244: $safeeval,$style);
245: } else {
246: #&Apache::lonxml::debug("NOT Calling sub $sub in $space<br>\n");
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;
256: }
257:
258: sub initdepth {
259: @Apache::lonxml::depthcounter=();
260: $Apache::lonxml::depth=-1;
261: $Apache::lonxml::olddepth=-1;
262: }
263:
264: sub increasedepth {
265: my ($token) = @_;
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: }
271: my $curdepth=join('_',@Apache::lonxml::depthcounter);
272: &Apache::lonxml::debug("s $Apache::lonxml::depth : $Apache::lonxml::olddepth : $curdepth : $token->[1]\n");
273: #print "<br>s $Apache::lonxml::depth : $Apache::lonxml::olddepth : $curdepth : $token->[1]\n";
274: }
275:
276: sub decreasedepth {
277: my ($token) = @_;
278: $Apache::lonxml::depth--;
279: if ($Apache::lonxml::depth<$Apache::lonxml::olddepth-1) {
280: $#Apache::lonxml::depthcounter--;
281: $Apache::lonxml::olddepth=$Apache::lonxml::depth+1;
282: }
283: if ( $Apache::lonxml::depth < -1) {
284: &Apache::lonxml::error("Unbalanced tags in resource");
285: $Apache::lonxml::depth='-1';
286: }
287: my $curdepth=join('_',@Apache::lonxml::depthcounter);
288: &Apache::lonxml::debug("e $Apache::lonxml::depth : $Apache::lonxml::olddepth : $token->[1] : $curdepth\n");
289: #print "<br>e $Apache::lonxml::depth : $Apache::lonxml::olddepth : $token->[1] : $curdepth\n";
290: }
291:
292: sub get_all_text {
293:
294: my($tag,$pars)= @_;
295: my $depth=0;
296: my $token;
297: my $result='';
298: my $tag=substr($tag,1); #strip the / off the tag
299: # &Apache::lonxml::debug("have:$tag:");
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') {
307: if ( $token->[1] eq $tag) { $depth--; }
308: #skip sending back the last end tag
309: if ($depth > -1) { $result.=$token->[2]; } else {
310: $pars->unget_token($token);
311: }
312: }
313: }
314: return $result
315: }
316:
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: }
328:
329: sub parstring {
330: my ($token) = @_;
331: my $temp='';
332: map {
333: unless ($_=~/\W/) {
334: my $val=$token->[2]->{$_};
335: if ($val =~ m/^[\%\@]/) { $val="\\".$val; }
336: $temp .= "my \$$_=\"$val\";"
337: }
338: } @{$token->[3]};
339: return $temp;
340: }
341:
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:
353: sub handler {
354: my $request=shift;
355:
356: my $target='web';
357: $Apache::lonxml::debug=0;
358: if ($ENV{'browser.mathml'}) {
359: $request->content_type('text/xml');
360: } else {
361: $request->content_type('text/html');
362: }
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);
372: $request->send_http_header;
373:
374: return OK if $request->header_only;
375:
376: $request->print(&Apache::lontexconvert::header());
377:
378: $request->print('<body bgcolor="#FFFFFF">'."\n");
379:
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);
385:
386: $request->print('</body>');
387: $request->print(&Apache::lontexconvert::footer());
388: writeallows($request->uri);
389: return OK;
390: }
391:
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 {
399: # print "ERROR:".$_[0]."<br>\n";
400: }
401: sub warning {
402: if ($Apache::lonxml::debug eq 1) {
403: print "WARNING:".$_[0]."<br>\n";
404: }
405: }
406:
407: 1;
408: __END__
409:
410:
411:
412:
413:
414:
415:
416:
417:
418:
419:
420:
421:
422:
423:
424:
425:
426:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>