File:  [LON-CAPA] / loncom / xml / lonxml.pm
Revision 1.33: download - view: text, annotated - select for diffs
Mon Nov 6 16:22:59 2000 UTC (23 years, 6 months ago) by www
Branches: MAIN
CVS tags: HEAD
Register external links for access control

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

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