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