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