File:  [LON-CAPA] / loncom / xml / lonxml.pm
Revision 1.44: download - view: text, annotated - select for diffs
Fri Jan 5 19:45:03 2001 UTC (23 years, 6 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- forgot to switch text redirection off.

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

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