File:  [LON-CAPA] / loncom / xml / lonxml.pm
Revision 1.58: download - view: text, annotated - select for diffs
Tue Mar 20 16:47:21 2001 UTC (23 years, 2 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- typo

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

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