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<1) { $result=$token->[1]; }
101: } elsif ($token->[0] eq 'PI') {
102: if ($metamode<1) { $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 ($Apache::lonxml::redirection) {
111: $Apache::lonxml::outputstack['-1'] .=
112: &recurse($style_for_target{$token->[1]},$target,$safeeval,
113: \%style_for_target,@parstack);
114: } else {
115: $finaloutput .= &recurse($style_for_target{$token->[1]},$target,
116: $safeeval,\%style_for_target,@parstack);
117: }
118: } else {
119: $result = &callsub("start_$token->[1]", $target, $token,\@parstack,
120: \@pars, $safeeval, \%style_for_target);
121: }
122: } elsif ($token->[0] eq 'E') {
123: #clear out any tags that didn't end
124: while ($token->[1] ne $stack[$#stack] && ($#stack > -1)) {
125: &Apache::lonxml::warning("Unbalanced tags in resource $stack['-1']");
126: pop @stack;pop @parstack;&decreasedepth($token);
127: }
128:
129: if (exists $style_for_target{'/'."$token->[1]"}) {
130: if ($Apache::lonxml::redirection) {
131: $Apache::lonxml::outputstack['-1'] .=
132: &recurse($style_for_target{'/'."$token->[1]"},
133: $target,$safeeval,\%style_for_target,@parstack);
134: } else {
135: $finaloutput .= &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: } else {
145: &Apache::lonxml::error("Unknown token event :$token->[0]:$token->[1]:");
146: }
147: #evaluate variable refs in result
148: if ($result ne "") {
149: if ( $#parstack > -1 ) {
150: if ($Apache::lonxml::redirection) {
151: $Apache::lonxml::outputstack['-1'] .=
152: &Apache::run::evaluate($result,$safeeval,$parstack[$#parstack]);
153: } else {
154: $finaloutput .= &Apache::run::evaluate($result,$safeeval,
155: $parstack[$#parstack]);
156: }
157: } else {
158: $finaloutput .= &Apache::run::evaluate($result,$safeeval,'');
159: }
160: $result = '';
161: }
162: if ($token->[0] eq 'E') {
163: pop @stack;pop @parstack;&decreasedepth($token);
164: }
165: }
166: pop @pars;
167: pop @Apache::lonxml::pwd;
168: }
169:
170: # if ($target eq 'meta') {
171: # $finaloutput.=&endredirection;
172: # }
173: return $finaloutput;
174: }
175:
176: sub recurse {
177:
178: my @innerstack = ();
179: my @innerparstack = ();
180: my ($newarg,$target,$safeeval,$style_for_target,@parstack) = @_;
181: my @pat = ();
182: &newparser(\@pat,\$newarg);
183: my $tokenpat;
184: my $partstring = '';
185: my $output='';
186: my $decls='';
187: while ( $#pat > -1 ) {
188: while ($tokenpat = $pat[$#pat]->get_token) {
189: if (($tokenpat->[0] eq 'T') || ($tokenpat->[0] eq 'C') || ($tokenpat->[0] eq 'D') ) {
190: if ($metamode<1) { $partstring=$tokenpat->[1]; }
191: } elsif ($tokenpat->[0] eq 'PI') {
192: if ($metamode<1) { $partstring=$tokenpat->[2]; }
193: } elsif ($tokenpat->[0] eq 'S') {
194: push (@innerstack,$tokenpat->[1]);
195: push (@innerparstack,&parstring($tokenpat));
196: &increasedepth($tokenpat);
197: $partstring = &callsub("start_$tokenpat->[1]",
198: $target, $tokenpat, \@innerparstack,
199: \@pat, $safeeval, $style_for_target);
200: } elsif ($tokenpat->[0] eq 'E') {
201: #clear out any tags that didn't end
202: while ($tokenpat->[1] ne $innerstack[$#innerstack]
203: && ($#innerstack > -1)) {
204: &Apache::lonxml::warning("Unbalanced tags in resource $innerstack['-1']");
205: pop @innerstack;pop @innerparstack;&decreasedepth($tokenpat);
206: }
207: $partstring = &callsub("end_$tokenpat->[1]",
208: $target, $tokenpat, \@innerparstack,
209: \@pat, $safeeval, $style_for_target);
210: } else {
211: &Apache::lonxml::error("Unknown token event :$tokenpat->[0]:$tokenpat->[1]:");
212: }
213: #pass both the variable to the style tag, and the tag we
214: #are processing inside the <definedtag>
215: if ( $partstring ne "" ) {
216: if ( $#parstack > -1 ) {
217: if ( $#innerparstack > -1 ) {
218: $decls= $parstack[$#parstack].$innerparstack[$#innerparstack];
219: } else {
220: $decls= $parstack[$#parstack];
221: }
222: } else {
223: if ( $#innerparstack > -1 ) {
224: $decls=$innerparstack[$#innerparstack];
225: } else {
226: $decls='';
227: }
228: }
229: $output .= &Apache::run::evaluate($partstring,$safeeval,$decls);
230: $partstring = '';
231: }
232: if ($tokenpat->[0] eq 'E') { pop @innerstack;pop @innerparstack;
233: &decreasedepth($tokenpat);}
234: }
235: pop @pat;
236: pop @Apache::lonxml::pwd;
237: }
238: return $output;
239: }
240:
241: sub callsub {
242: my ($sub,$target,$token,$parstack,$parser,$safeeval,$style)=@_;
243: my $currentstring='';
244: {
245: my $sub1;
246: no strict 'refs';
247: if ($target eq 'edit' && $token->[0] eq 'S') {
248: $currentstring = &Apache::edit::tag_start($target,$token,$parstack,$parser,
249: $safeeval,$style);
250: }
251: if (my $space=$Apache::lonxml::alltags{$token->[1]}) {
252: #&Apache::lonxml::debug("Calling sub $sub in $space<br />\n");
253: $sub1="$space\:\:$sub";
254: $Apache::lonxml::curdepth=join('_',@Apache::lonxml::depthcounter);
255: $currentstring .= &$sub1($target,$token,$parstack,$parser,
256: $safeeval,$style);
257: } else {
258: #&Apache::lonxml::debug("NOT Calling sub $sub in $space<br />\n");
259: if (defined($token->[4]) && ($metamode < '1')) {
260: $currentstring .= $token->[4];
261: } else {
262: $currentstring .= $token->[2];
263: }
264: }
265: if ($target eq 'edit' && $token->[0] eq 'E') {
266: $currentstring .= &Apache::edit::tag_end($target,$token,$parstack,$parser,
267: $safeeval,$style);
268: }
269: use strict 'refs';
270: }
271: return $currentstring;
272: }
273:
274: sub startredirection {
275: $Apache::lonxml::redirection++;
276: push (@Apache::lonxml::outputstack, '');
277: }
278:
279: sub endredirection {
280: if (!$Apache::lonxml::redirection) {
281: &Apache::lonxml::error("Endredirection was called, before a startredirection, perhaps you have unbalanced tags. Some debuggin information:".join ":",caller);
282: return '';
283: }
284: $Apache::lonxml::redirection--;
285: pop @Apache::lonxml::outputstack;
286: }
287:
288: sub initdepth {
289: @Apache::lonxml::depthcounter=();
290: $Apache::lonxml::depth=-1;
291: $Apache::lonxml::olddepth=-1;
292: }
293:
294: sub increasedepth {
295: my ($token) = @_;
296: $Apache::lonxml::depth++;
297: $Apache::lonxml::depthcounter[$Apache::lonxml::depth]++;
298: if ($Apache::lonxml::depthcounter[$Apache::lonxml::depth]==1) {
299: $Apache::lonxml::olddepth=$Apache::lonxml::depth;
300: }
301: my $curdepth=join('_',@Apache::lonxml::depthcounter);
302: &Apache::lonxml::debug("s $Apache::lonxml::depth : $Apache::lonxml::olddepth : $curdepth : $token->[1]\n");
303: #print "<br />s $Apache::lonxml::depth : $Apache::lonxml::olddepth : $curdepth : $token->[1]\n";
304: }
305:
306: sub decreasedepth {
307: my ($token) = @_;
308: $Apache::lonxml::depth--;
309: if ($Apache::lonxml::depth<$Apache::lonxml::olddepth-1) {
310: $#Apache::lonxml::depthcounter--;
311: $Apache::lonxml::olddepth=$Apache::lonxml::depth+1;
312: }
313: if ( $Apache::lonxml::depth < -1) {
314: &Apache::lonxml::warning("Unbalanced tags in resource");
315: $Apache::lonxml::depth='-1';
316: }
317: my $curdepth=join('_',@Apache::lonxml::depthcounter);
318: &Apache::lonxml::debug("e $Apache::lonxml::depth : $Apache::lonxml::olddepth : $token->[1] : $curdepth\n");
319: #print "<br />e $Apache::lonxml::depth : $Apache::lonxml::olddepth : $token->[1] : $curdepth\n";
320: }
321:
322: sub get_all_text {
323:
324: my($tag,$pars)= @_;
325: my $depth=0;
326: my $token;
327: my $result='';
328: if ( $tag =~ m:^/: ) {
329: my $tag=substr($tag,1);
330: # &Apache::lonxml::debug("have:$tag:");
331: while (($depth >=0) && ($token = $pars->get_token)) {
332: # &Apache::lonxml::debug("e token:$token->[0]:$depth:$token->[1]");
333: if (($token->[0] eq 'T')||($token->[0] eq 'C')||($token->[0] eq 'D')) {
334: $result.=$token->[1];
335: } elsif ($token->[0] eq 'PI') {
336: $result.=$token->[2];
337: } elsif ($token->[0] eq 'S') {
338: if ($token->[1] eq $tag) { $depth++; }
339: $result.=$token->[4];
340: } elsif ($token->[0] eq 'E') {
341: if ( $token->[1] eq $tag) { $depth--; }
342: #skip sending back the last end tag
343: if ($depth > -1) { $result.=$token->[2]; } else {
344: $pars->unget_token($token);
345: }
346: }
347: }
348: } else {
349: while ($token = $pars->get_token) {
350: # &Apache::lonxml::debug("s token:$token->[0]:$depth:$token->[1]");
351: if (($token->[0] eq 'T')||($token->[0] eq 'C')||($token->[0] eq 'D')) {
352: $result.=$token->[1];
353: } elsif ($token->[0] eq 'PI') {
354: $result.=$token->[2];
355: } elsif ($token->[0] eq 'S') {
356: if ( $token->[1] eq $tag) {
357: $pars->unget_token($token); last;
358: } else {
359: $result.=$token->[4];
360: }
361: } elsif ($token->[0] eq 'E') {
362: $result.=$token->[2];
363: }
364: }
365: }
366: # &Apache::lonxml::debug("Exit:$result:");
367: return $result
368: }
369:
370: sub newparser {
371: my ($parser,$contentref,$dir) = @_;
372: push (@$parser,HTML::TokeParser->new($contentref));
373: $$parser['-1']->xml_mode('1');
374: if ( $dir eq '' ) {
375: push (@Apache::lonxml::pwd, $Apache::lonxml::pwd[$#Apache::lonxml::pwd]);
376: } else {
377: push (@Apache::lonxml::pwd, $dir);
378: }
379: # &Apache::lonxml::debug("pwd:$#Apache::lonxml::pwd");
380: # &Apache::lonxml::debug("pwd:$Apache::lonxml::pwd[$#Apache::lonxml::pwd]");
381: }
382:
383: sub parstring {
384: my ($token) = @_;
385: my $temp='';
386: map {
387: unless ($_=~/\W/) {
388: my $val=$token->[2]->{$_};
389: $val =~ s/([\%\@\\])/\\$1/g;
390: #if ($val =~ m/^[\%\@]/) { $val="\\".$val; }
391: $temp .= "my \$$_=\"$val\";"
392: }
393: } @{$token->[3]};
394: return $temp;
395: }
396:
397: sub writeallows {
398: my $thisurl='/res/'.&Apache::lonnet::declutter(shift);
399: my $thisdir=$thisurl;
400: $thisdir=~s/\/[^\/]+$//;
401: my %httpref=();
402: map {
403: $httpref{'httpref.'.
404: &Apache::lonnet::hreflocation($thisdir,$_)}=$thisurl; } @extlinks;
405: &Apache::lonnet::appenv(%httpref);
406: }
407:
408: sub handler {
409: my $request=shift;
410:
411: my $target='web';
412: $Apache::lonxml::debug=0;
413: if ($ENV{'browser.mathml'}) {
414: $request->content_type('text/xml');
415: } else {
416: $request->content_type('text/html');
417: }
418:
419: # $request->print(<<ENDHEADER);
420: #<html>
421: #<head>
422: #<title>Just test</title>
423: #</head>
424: #<body bgcolor="#FFFFFF">
425: #ENDHEADER
426: # &Apache::lonhomework::send_header($request);
427: $request->send_http_header;
428:
429: return OK if $request->header_only;
430:
431: $request->print(&Apache::lontexconvert::header());
432:
433: $request->print('<body bgcolor="#FFFFFF">'."\n");
434:
435: my $file=&Apache::lonnet::filelocation("",$request->uri);
436: my %mystyle;
437: my $result = '';
438: my $filecontents=&Apache::lonnet::getfile($file);
439: if ($filecontents == -1) {
440: &Apache::lonxml::error("<b> Unable to find <i>$file</i></b>");
441: $filecontents='';
442: } else {
443: $result = &Apache::lonxml::xmlparse($target,$filecontents,'',%mystyle);
444: }
445: $request->print($result);
446:
447:
448: $request->print('</body>');
449: $request->print(&Apache::lontexconvert::footer());
450: writeallows($request->uri);
451: return OK;
452: }
453:
454: $Apache::lonxml::debug=0;
455: sub debug {
456: if ($Apache::lonxml::debug eq 1) {
457: print "DEBUG:".$_[0]."<br />\n";
458: }
459: }
460:
461: sub error {
462: if ($Apache::lonxml::debug eq 1) {
463: print "<b>ERROR:</b>".$_[0]."<br />\n";
464: } else {
465: print "<b>An Error occured while processing this resource. The instructor has been notified.</b> <br />";
466: #notify author
467: &Apache::lonmsg::author_res_msg($ENV{'request.filename'},$_[0]);
468: #notify course
469: if ( $ENV{'request.course.id'} ) {
470: my $users=$ENV{'course.'.$ENV{'request.course.id'}.'.comment.email'};
471: foreach my $user (split /\,/, $users) {
472: ($user,my $domain) = split /:/, $user;
473: &Apache::lonmsg::user_normal_msg($user,$domain,"Error in $ENV{'request.filename'}",$_[0]);
474: }
475: }
476:
477: #FIXME probably shouldn't have me get everything forever.
478: &Apache::lonmsg::user_normal_msg('albertel','msu',"Error in $ENV{'request.filename'}",$_[0]);
479: #&Apache::lonmsg::user_normal_msg('albertel','103',"Error in $ENV{'request.filename'}",$_[0]);
480: }
481: }
482:
483: sub warning {
484: if ($Apache::lonxml::debug eq 1) {
485: print "<b>W</b>ARNING<b>:</b>".$_[0]."<br />\n";
486: }
487: }
488:
489: 1;
490: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>