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