File:  [LON-CAPA] / loncom / xml / lonxml.pm
Revision 1.65: download - view: text, annotated - select for diffs
Tue Mar 27 18:19:29 2001 UTC (23 years, 2 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- stupid screwups

    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 $metamode<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 $metamode<br />\n");
  259:       if ($metamode <1) {
  260: 	if (defined($token->[4]) && ($metamode < 1)) {
  261: 	  $currentstring .= $token->[4];
  262: 	} else {
  263: 	  $currentstring .= $token->[2];
  264: 	}
  265:       }
  266:     }
  267:     if ($target eq 'edit' && $token->[0] eq 'E') {
  268:       $currentstring .= &Apache::edit::tag_end($target,$token,$parstack,$parser,
  269: 						$safeeval,$style);
  270:     }
  271:     use strict 'refs';
  272:   }
  273:   return $currentstring;
  274: }
  275: 
  276: sub startredirection {
  277:   $Apache::lonxml::redirection++;
  278:   push (@Apache::lonxml::outputstack, '');
  279: }
  280: 
  281: sub endredirection {
  282:   if (!$Apache::lonxml::redirection) {
  283:     &Apache::lonxml::error("Endredirection was called, before a startredirection, perhaps you have unbalanced tags. Some debuggin information:".join ":",caller);
  284:     return '';
  285:   }
  286:   $Apache::lonxml::redirection--;
  287:   pop @Apache::lonxml::outputstack;
  288: }
  289: 
  290: sub initdepth {
  291:   @Apache::lonxml::depthcounter=();
  292:   $Apache::lonxml::depth=-1;
  293:   $Apache::lonxml::olddepth=-1;
  294: }
  295: 
  296: sub increasedepth {
  297:   my ($token) = @_;
  298:   $Apache::lonxml::depth++;
  299:   $Apache::lonxml::depthcounter[$Apache::lonxml::depth]++;
  300:   if ($Apache::lonxml::depthcounter[$Apache::lonxml::depth]==1) {
  301:     $Apache::lonxml::olddepth=$Apache::lonxml::depth;
  302:   }
  303:   my $curdepth=join('_',@Apache::lonxml::depthcounter);
  304:   &Apache::lonxml::debug("s $Apache::lonxml::depth : $Apache::lonxml::olddepth : $curdepth : $token->[1]\n");
  305: #print "<br />s $Apache::lonxml::depth : $Apache::lonxml::olddepth : $curdepth : $token->[1]\n";
  306: }
  307: 
  308: sub decreasedepth {
  309:   my ($token) = @_;
  310:   $Apache::lonxml::depth--;
  311:   if ($Apache::lonxml::depth<$Apache::lonxml::olddepth-1) {
  312:     $#Apache::lonxml::depthcounter--;
  313:     $Apache::lonxml::olddepth=$Apache::lonxml::depth+1;
  314:   }
  315:   if (  $Apache::lonxml::depth < -1) {
  316:     &Apache::lonxml::warning("Unbalanced tags in resource");   
  317:     $Apache::lonxml::depth='-1';
  318:   }
  319:   my $curdepth=join('_',@Apache::lonxml::depthcounter);
  320:   &Apache::lonxml::debug("e $Apache::lonxml::depth : $Apache::lonxml::olddepth : $token->[1] : $curdepth\n");
  321: #print "<br />e $Apache::lonxml::depth : $Apache::lonxml::olddepth : $token->[1] : $curdepth\n";
  322: }
  323: 
  324: sub get_all_text {
  325: 
  326:  my($tag,$pars)= @_;
  327:  my $depth=0;
  328:  my $token;
  329:  my $result='';
  330:  if ( $tag =~ m:^/: ) { 
  331:    my $tag=substr($tag,1); 
  332: #   &Apache::lonxml::debug("have:$tag:");
  333:    while (($depth >=0) && ($token = $pars->get_token)) {
  334: #     &Apache::lonxml::debug("e token:$token->[0]:$depth:$token->[1]");
  335:      if (($token->[0] eq 'T')||($token->[0] eq 'C')||($token->[0] eq 'D')) {
  336:        $result.=$token->[1];
  337:      } elsif ($token->[0] eq 'PI') {
  338:        $result.=$token->[2];
  339:      } elsif ($token->[0] eq 'S') {
  340:        if ($token->[1] eq $tag) { $depth++; }
  341:        $result.=$token->[4];
  342:      } elsif ($token->[0] eq 'E')  {
  343:        if ( $token->[1] eq $tag) { $depth--; }
  344:        #skip sending back the last end tag
  345:        if ($depth > -1) { $result.=$token->[2]; } else {
  346: 	 $pars->unget_token($token);
  347:        }
  348:      }
  349:    }
  350:  } else {
  351:    while ($token = $pars->get_token) {
  352: #     &Apache::lonxml::debug("s token:$token->[0]:$depth:$token->[1]");
  353:      if (($token->[0] eq 'T')||($token->[0] eq 'C')||($token->[0] eq 'D')) {
  354:        $result.=$token->[1];
  355:      } elsif ($token->[0] eq 'PI') {
  356:        $result.=$token->[2];
  357:      } elsif ($token->[0] eq 'S') {
  358:        if ( $token->[1] eq $tag) { 
  359: 	 $pars->unget_token($token); last;
  360:        } else {
  361: 	 $result.=$token->[4];
  362:        }
  363:      } elsif ($token->[0] eq 'E')  {
  364:        $result.=$token->[2];
  365:      }
  366:    }
  367:  }
  368: # &Apache::lonxml::debug("Exit:$result:");
  369:  return $result
  370: }
  371: 
  372: sub newparser {
  373:   my ($parser,$contentref,$dir) = @_;
  374:   push (@$parser,HTML::TokeParser->new($contentref));
  375:   $$parser['-1']->xml_mode('1');
  376:   if ( $dir eq '' ) {
  377:     push (@Apache::lonxml::pwd, $Apache::lonxml::pwd[$#Apache::lonxml::pwd]);
  378:   } else {
  379:     push (@Apache::lonxml::pwd, $dir);
  380:   } 
  381: #  &Apache::lonxml::debug("pwd:$#Apache::lonxml::pwd");
  382: #  &Apache::lonxml::debug("pwd:$Apache::lonxml::pwd[$#Apache::lonxml::pwd]");
  383: }
  384: 
  385: sub parstring {
  386:   my ($token) = @_;
  387:   my $temp='';
  388:   map {
  389:     unless ($_=~/\W/) {
  390:       my $val=$token->[2]->{$_};
  391:       $val =~ s/([\%\@\\])/\\$1/g;
  392:       #if ($val =~ m/^[\%\@]/) { $val="\\".$val; }
  393:       $temp .= "my \$$_=\"$val\";"
  394:     }
  395:   } @{$token->[3]};
  396:   return $temp;
  397: }
  398: 
  399: sub writeallows {
  400:     my $thisurl='/res/'.&Apache::lonnet::declutter(shift);
  401:     my $thisdir=$thisurl;
  402:     $thisdir=~s/\/[^\/]+$//;
  403:     my %httpref=();
  404:     map {
  405:        $httpref{'httpref.'.
  406:  	        &Apache::lonnet::hreflocation($thisdir,$_)}=$thisurl;              } @extlinks;
  407:     &Apache::lonnet::appenv(%httpref);
  408: }
  409: 
  410: sub handler {
  411:   my $request=shift;
  412:   
  413:   my $target='web';
  414:   $Apache::lonxml::debug=0;
  415:   if ($ENV{'browser.mathml'}) {
  416:     $request->content_type('text/xml');
  417:   } else {
  418:     $request->content_type('text/html');
  419:   }
  420:   
  421: #  $request->print(<<ENDHEADER);
  422: #<html>
  423: #<head>
  424: #<title>Just test</title>
  425: #</head>
  426: #<body bgcolor="#FFFFFF">
  427: #ENDHEADER
  428: #  &Apache::lonhomework::send_header($request);
  429:   $request->send_http_header;
  430:   
  431:   return OK if $request->header_only;
  432: 
  433:   if ($target eq 'web') {
  434:     $request->print(&Apache::lontexconvert::header());
  435:     $request->print('<body bgcolor="#FFFFFF">'."\n");
  436:   }
  437: 
  438:   my $file=&Apache::lonnet::filelocation("",$request->uri);
  439:   my %mystyle;
  440:   my $result = ''; 
  441:   my $filecontents=&Apache::lonnet::getfile($file);
  442:   if ($filecontents == -1) {
  443:     &Apache::lonxml::error("<b> Unable to find <i>$file</i></b>");
  444:     $filecontents='';
  445:   } else {
  446:     $result = &Apache::lonxml::xmlparse($target,$filecontents,'',%mystyle);
  447:   }
  448:   $request->print($result);
  449: 
  450: 
  451:   if ($target eq 'tex') {
  452: #    $request->print('\end{document}'."\n");
  453:   } elsif ($target eq 'web') {
  454:     $request->print('</body>');
  455:     $request->print(&Apache::lontexconvert::footer());
  456:   }
  457: 
  458:   writeallows($request->uri);
  459:   return OK;
  460: }
  461:  
  462: $Apache::lonxml::debug=0;
  463: sub debug {
  464:   if ($Apache::lonxml::debug eq 1) {
  465:     print "DEBUG:".$_[0]."<br />\n";
  466:   }
  467: }
  468: 
  469: sub error {
  470:   if ($Apache::lonxml::debug eq 1) {
  471:     print "<b>ERROR:</b>".$_[0]."<br />\n";
  472:   } else {
  473:     print "<b>An Error occured while processing this resource. The instructor has been notified.</b> <br />";
  474:     #notify author
  475:     &Apache::lonmsg::author_res_msg($ENV{'request.filename'},$_[0]);
  476:     #notify course
  477:     if ( $ENV{'request.course.id'} ) {
  478:       my $users=$ENV{'course.'.$ENV{'request.course.id'}.'.comment.email'};
  479:       foreach my $user (split /\,/, $users) {
  480: 	($user,my $domain) = split /:/, $user;
  481: 	&Apache::lonmsg::user_normal_msg($user,$domain,"Error in $ENV{'request.filename'}",$_[0]);
  482:       }
  483:     }
  484:     
  485:     #FIXME probably shouldn't have me get everything forever.
  486:     &Apache::lonmsg::user_normal_msg('albertel','msu',"Error in $ENV{'request.filename'}",$_[0]);
  487:     #&Apache::lonmsg::user_normal_msg('albertel','103',"Error in $ENV{'request.filename'}",$_[0]);   
  488:   }
  489: }
  490: 
  491: sub warning {
  492:   if ($Apache::lonxml::debug eq 1) {
  493:     print "<b>W</b>ARNING<b>:</b>".$_[0]."<br />\n";
  494:   }
  495: }
  496: 
  497: 1;
  498: __END__

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>