File:  [LON-CAPA] / loncom / xml / lonxml.pm
Revision 1.40: download - view: text, annotated - select for diffs
Wed Nov 22 17:35:13 2000 UTC (23 years, 6 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- poke a hole in the Safe compartment for &EXT()

    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: 
    7: package Apache::lonxml; 
    8: use vars 
    9: qw(@pwd $outputstack $redirection $textredirection $on_offimport @extlinks);
   10: use strict;
   11: use HTML::TokeParser;
   12: use Safe;
   13: use Safe::Hole;
   14: use Opcode;
   15: 
   16: sub register {
   17:   my $space;
   18:   my @taglist;
   19:   my $temptag;
   20:   ($space,@taglist) = @_;
   21:   foreach $temptag (@taglist) {
   22:     $Apache::lonxml::alltags{$temptag}=$space;
   23:   }
   24: }
   25:                                      
   26: use Apache::style;
   27: use Apache::lontexconvert;
   28: use Apache::run;
   29: use Apache::londefdef;
   30: use Apache::scripttag;
   31: #==================================================   Main subroutine: xmlparse  
   32: @pwd=();
   33: $outputstack = '';
   34: $redirection = 1;
   35: $textredirection = 1;
   36: $on_offimport = 0;
   37: @extlinks=();
   38: 
   39: sub xmlparse {
   40: 
   41:  my ($target,$content_file_string,$safeinit,%style_for_target) = @_;
   42:  if ($target eq 'meta') 
   43:    {$Apache::lonxml::textredirection = 0;
   44:     $Apache::lonxml::on_offimport = 1;
   45:  }
   46:  my @pars = ();
   47:  @Apache::lonxml::pwd=();
   48:  my $pwd=$ENV{'request.filename'};
   49:  $pwd =~ s:/[^/]*$::;
   50:  &newparser(\@pars,\$content_file_string,$pwd);
   51:  my $currentstring = '';
   52:  my $finaloutput = ''; 
   53:  my $newarg = '';
   54:  my $result;
   55: 
   56:  my $safeeval = new Safe;
   57:  my $safehole = new Safe::Hole;
   58:  $safeeval->permit("entereval");
   59:  $safeeval->permit(":base_math");
   60:  $safeeval->deny(":base_io");
   61:  $safehole->wrap(\&Apache::lonnet::EXT,$safeeval,'&EXT');
   62: #need to inspect this class of ops
   63: # $safeeval->deny(":base_orig");
   64:  $safeinit .= ';$external::target='.$target.';';
   65:  $safeinit .= ';$external::randomseed='.&Apache::lonnet::rndseed().';';
   66:  &Apache::run::run($safeinit,$safeeval);
   67: #-------------------- Redefinition of the target in the case of compound target
   68: 
   69:  ($target, my @tenta) = split('&&',$target);
   70: 
   71:  my @stack = (); 
   72:  my @parstack = ();
   73:  &initdepth;
   74:  my $token;
   75:  while ( $#pars > -1 ) {
   76:    while ($token = $pars[$#pars]->get_token) {
   77:      if ($token->[0] eq 'T') {
   78: 	 if ($Apache::lonxml::textredirection == 1) {$result=$token->[1];}
   79: #       $finaloutput .= &Apache::run::evaluate($token->[1],$safeeval,'');
   80:      } elsif ($token->[0] eq 'S') {
   81: #            if ($target eq 'meta' and $token->[2]->{metaout} eq 'ON') {$Apache::lonxml::textredirection = 1;}
   82:        # add tag to stack 	    
   83:        push (@stack,$token->[1]);
   84:        # add parameters list to another stack
   85:        push (@parstack,&parstring($token));
   86:        &increasedepth($token);       
   87:        if (exists $style_for_target{$token->[1]}) {
   88: 
   89: 	if ($Apache::lonxml::redirection == 1) {
   90: 	  $finaloutput .= &recurse($style_for_target{$token->[1]},
   91: 		  		  $target,$safeeval,\%style_for_target,
   92: 	 		 	  @parstack);
   93:         } else {
   94:           $Apache::lonxml::outputstack .=  &recurse($style_for_target{$token->[1]},
   95: 		  		  $target,$safeeval,\%style_for_target,
   96: 	 		 	  @parstack);
   97:         }
   98: 
   99:        } else {
  100: 	 $result = &callsub("start_$token->[1]", $target, $token,\@parstack,
  101: 			       \@pars, $safeeval, \%style_for_target);
  102:        }              
  103:      } elsif ($token->[0] eq 'E')  {
  104: #	 if ($target eq 'meta') {$Apache::lonxml::textredirection = 0;}
  105:        #clear out any tags that didn't end
  106:        while ($token->[1] ne $stack[$#stack] 
  107: 	      && ($#stack > -1)) {pop @stack;pop @parstack;&decreasedepth($token);}
  108:        
  109:        if (exists $style_for_target{'/'."$token->[1]"}) {
  110: 
  111: 	if ($Apache::lonxml::redirection == 1) {
  112: 	 $finaloutput .= &recurse($style_for_target{'/'."$token->[1]"},
  113: 				  $target,$safeeval,\%style_for_target,
  114: 				  @parstack);
  115:         } else {
  116:          $Apache::lonxml::outputstack .=  &recurse($style_for_target{'/'."$token->[1]"},
  117: 				  $target,$safeeval,\%style_for_target,
  118: 				  @parstack);
  119:         }
  120: 
  121:        } else {
  122: 	 $result = &callsub("end_$token->[1]", $target, $token, \@parstack,
  123: 			       \@pars,$safeeval, \%style_for_target);
  124:        }
  125:      }
  126:      if ($result ne "") {
  127:        if ( $#parstack > -1 ) {
  128:  
  129: 	if ($Apache::lonxml::redirection == 1) {
  130: 	 $finaloutput .= &Apache::run::evaluate($result,$safeeval,
  131: 						$parstack[$#parstack]);
  132:         } else {
  133:          $Apache::lonxml::outputstack .= &Apache::run::evaluate($result,$safeeval,
  134: 						$parstack[$#parstack]);
  135:         }
  136: 
  137:        } else {
  138: 	 $finaloutput .= &Apache::run::evaluate($result,$safeeval,'');
  139:        }
  140:        $result = '';
  141:      } else {
  142:          $finaloutput .= $result;
  143:      }
  144:      if ($token->[0] eq 'E') { pop @stack;pop @parstack;&decreasedepth($token);}
  145:    }
  146:    pop @pars;
  147:    pop @Apache::lonxml::pwd;
  148:  }
  149: 
  150:  return $finaloutput;
  151: }
  152: 
  153: sub recurse {
  154:   
  155:   my @innerstack = (); 
  156:   my @innerparstack = ();
  157:   my ($newarg,$target,$safeeval,$style_for_target,@parstack) = @_;
  158:   my @pat = ();
  159:   &newparser(\@pat,\$newarg);
  160:   my $tokenpat;
  161:   my $partstring = '';
  162:   my $output='';
  163:   my $decls='';
  164:   while ( $#pat > -1 ) {
  165:     while  ($tokenpat = $pat[$#pat]->get_token) {
  166:       if ($tokenpat->[0] eq 'T') {
  167: 	  if ($Apache::lonxml::textredirection == 1) {$partstring = $tokenpat->[1];}
  168:       } elsif ($tokenpat->[0] eq 'S') {
  169: 	push (@innerstack,$tokenpat->[1]);
  170: 	push (@innerparstack,&parstring($tokenpat));
  171: 	&increasedepth($tokenpat);
  172: 	$partstring = &callsub("start_$tokenpat->[1]", 
  173: 			       $target, $tokenpat, \@innerparstack,
  174: 			       \@pat, $safeeval, $style_for_target);
  175:       } elsif ($tokenpat->[0] eq 'E') {
  176: 	#clear out any tags that didn't end
  177: 	while ($tokenpat->[1] ne $innerstack[$#innerstack] 
  178: 	       && ($#innerstack > -1)) {pop @innerstack;pop @innerparstack;
  179: 					&decreasedepth($tokenpat);}
  180: 	$partstring = &callsub("end_$tokenpat->[1]",
  181: 			       $target, $tokenpat, \@innerparstack,
  182: 			       \@pat, $safeeval, $style_for_target);
  183:       }
  184:       #pass both the variable to the style tag, and the tag we 
  185:       #are processing inside the <definedtag>
  186:       if ( $partstring ne "" ) {
  187: 	if ( $#parstack > -1 ) { 
  188: 	  if ( $#innerparstack > -1 ) { 
  189: 	    $decls= $parstack[$#parstack].$innerparstack[$#innerparstack];
  190: 	  } else {
  191: 	    $decls= $parstack[$#parstack];
  192: 	  }
  193: 	} else {
  194: 	  if ( $#innerparstack > -1 ) { 
  195: 	    $decls=$innerparstack[$#innerparstack];
  196: 	  } else {
  197: 	    $decls='';
  198: 	  }
  199: 	}
  200: 	$output .= &Apache::run::evaluate($partstring,$safeeval,$decls);
  201: 	$partstring = '';
  202:       }
  203:       if ($tokenpat->[0] eq 'E') { pop @innerstack;pop @innerparstack;
  204: 				 &decreasedepth($tokenpat);}
  205:     }
  206:     pop @pat;
  207:     pop @Apache::lonxml::pwd;
  208:   }
  209:   return $output;
  210: }
  211: 
  212: sub callsub {
  213:   my ($sub,$target,$token,$parstack,$parser,$safeeval,$style)=@_;
  214:   my $currentstring='';
  215:   {
  216:       my $sub1;
  217:     no strict 'refs';
  218:     if (my $space=$Apache::lonxml::alltags{$token->[1]}) {
  219:       #&Apache::lonxml::debug("Calling sub $sub in $space<br>\n");
  220:       $sub1="$space\:\:$sub";
  221:       $Apache::lonxml::curdepth=join('_',@Apache::lonxml::depthcounter);
  222:       $currentstring = &$sub1($target,$token,$parstack,$parser,
  223: 			     $safeeval,$style);
  224:     } else {
  225:       #&Apache::lonxml::debug("NOT Calling sub $sub in $space<br>\n");
  226:       if (defined($token->[4])) {
  227: 	$currentstring = $token->[4];
  228:       } else {
  229: 	$currentstring = $token->[2];
  230:       }
  231:     }
  232:     use strict 'refs';
  233:   }
  234:   return $currentstring;
  235: }
  236: 
  237: sub initdepth {
  238:   @Apache::lonxml::depthcounter=();
  239:   $Apache::lonxml::depth=-1;
  240:   $Apache::lonxml::olddepth=-1;
  241: }
  242: 
  243: sub increasedepth {
  244:   my ($token) = @_;
  245:   $Apache::lonxml::depth++;
  246:   $Apache::lonxml::depthcounter[$Apache::lonxml::depth]++;
  247:   if ($Apache::lonxml::depthcounter[$Apache::lonxml::depth]==1) {
  248:     $Apache::lonxml::olddepth=$Apache::lonxml::depth;
  249:   }
  250: #  my $curdepth=join('_',@Apache::lonxml::depthcounter);
  251: #  print "<br>s $Apache::lonxml::depth : $Apache::lonxml::olddepth : $curdepth : $token->[1]<br>\n";
  252: }
  253: 
  254: sub decreasedepth {
  255:   my ($token) = @_;
  256:   $Apache::lonxml::depth--;
  257:   if ($Apache::lonxml::depth<$Apache::lonxml::olddepth-1) {
  258:     $#Apache::lonxml::depthcounter--;
  259:     $Apache::lonxml::olddepth=$Apache::lonxml::depth+1;
  260:   }
  261: #  my $curdepth=join('_',@Apache::lonxml::depthcounter);
  262: #  print "<br>e $Apache::lonxml::depth : $Apache::lonxml::olddepth : $token->[1] : $curdepth <br>\n";
  263: }
  264: 
  265: sub get_all_text {
  266: 
  267:  my($tag,$pars)= @_;
  268:  my $depth=0;
  269:  my $token;
  270:  my $result='';
  271:  my $tag=substr($tag,1); #strip the / off the tag
  272: # &Apache::lonxml::debug("have:$tag:");
  273:  while (($depth >=0) && ($token = $pars->get_token)) {
  274:    if ($token->[0] eq 'T') {
  275:      $result.=$token->[1];
  276:    } elsif ($token->[0] eq 'S') {
  277:      if ($token->[1] eq $tag) { $depth++; }
  278:      $result.=$token->[4];
  279:    } elsif ($token->[0] eq 'E')  {
  280:      if ( $token->[1] eq $tag) { $depth--; }
  281:      #skip sending back the last end tag
  282:      if ($depth > -1) { $result.=$token->[2]; } else {
  283:        $pars->unget_token($token);
  284:      }
  285:    }
  286:  }
  287:  return $result
  288: }
  289: 
  290: sub newparser {
  291:   my ($parser,$contentref,$dir) = @_;
  292:   push (@$parser,HTML::TokeParser->new($contentref));
  293:   if ( $dir eq '' ) {
  294:     push (@Apache::lonxml::pwd, $Apache::lonxml::pwd[$#Apache::lonxml::pwd]);
  295:   } else {
  296:     push (@Apache::lonxml::pwd, $dir);
  297:   } 
  298: #  &Apache::lonxml::debug("pwd:$#Apache::lonxml::pwd");
  299: #  &Apache::lonxml::debug("pwd:$Apache::lonxml::pwd[$#Apache::lonxml::pwd]");
  300: }
  301: 
  302: sub parstring {
  303:   my ($token) = @_;
  304:   my $temp='';
  305:   map {
  306:     unless ($_=~/\W/) {
  307:       $temp .= "my \$$_=\"$token->[2]->{$_}\";"
  308:     }
  309:   } @{$token->[3]};
  310:   return $temp;
  311: }
  312: 
  313: sub writeallows {
  314:     my $thisurl='/res/'.&Apache::lonnet::declutter(shift);
  315:     my $thisdir=$thisurl;
  316:     $thisdir=~s/\/[^\/]+$//;
  317:     my %httpref=();
  318:     map {
  319:        $httpref{'httpref.'.
  320:  	        &Apache::lonnet::hreflocation($thisdir,$_)}=$thisurl;              } @extlinks;
  321:     &Apache::lonnet::appenv(%httpref);
  322: }
  323: 
  324: sub handler {
  325:   my $request=shift;
  326: 
  327:   my $target='web';
  328:   $Apache::lonxml::debug=1;
  329:   if ($ENV{'browser.mathml'}) {
  330:     $request->content_type('text/xml');
  331:   } else {
  332:     $request->content_type('text/html');
  333:   }
  334: 
  335: #  $request->print(<<ENDHEADER);
  336: #<html>
  337: #<head>
  338: #<title>Just test</title>
  339: #</head>
  340: #<body bgcolor="#FFFFFF">
  341: #ENDHEADER
  342: #  &Apache::lonhomework::send_header($request);
  343:   $request->send_http_header;
  344: 
  345:   return 'OK' if $request->header_only;
  346: 
  347:   $request->print(&Apache::lontexconvert::header());
  348: 
  349:   $request->print('<body bgcolor="#FFFFFF">'."\n");
  350: 
  351:   my $file = "/home/httpd/html".$request->uri;
  352:   my %mystyle;
  353:   my $result = '';
  354:   $result = Apache::lonxml::xmlparse($target, &Apache::lonnet::getfile($file),'',%mystyle);
  355:   $request->print($result);
  356: 
  357:   $request->print('</body>');
  358:   $request->print(&Apache::lontexconvert::footer());
  359:   writeallows($request->uri);
  360:   return 'OK';
  361: }
  362:  
  363: $Apache::lonxml::debug=0;
  364: sub debug {
  365:   if ($Apache::lonxml::debug eq 1) {
  366:     print "DEBUG:".$_[0]."<br>\n";
  367:   }
  368: }
  369: sub error {
  370:   print "ERROR:".$_[0]."<br>\n";
  371: }
  372: sub warning {
  373:   if ($Apache::lonxml::debug eq 1) {
  374:     print "WARNING:".$_[0]."<br>\n";
  375:   }
  376: }
  377: 
  378: 1;
  379: __END__
  380: 
  381: 
  382: 
  383: 
  384: 
  385: 
  386: 
  387: 
  388: 
  389: 
  390: 
  391: 
  392: 
  393: 
  394: 
  395: 
  396: 
  397: 

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