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