File:  [LON-CAPA] / loncom / xml / lonxml.pm
Revision 1.36: download - view: text, annotated - select for diffs
Tue Nov 7 23:39:07 2000 UTC (23 years, 7 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- increase depth and decrease depth work as advertised now
- get_all_text puts the end tag back into the parser

    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);&Apache::lonxml::debug("Clearing out stack");}
  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:   $Apache::lonxml::depth++;
  243:   $Apache::lonxml::depthcounter[$Apache::lonxml::depth]++;
  244:   if ($Apache::lonxml::depthcounter[$Apache::lonxml::depth]==1) {
  245:     $Apache::lonxml::olddepth=$Apache::lonxml::depth;
  246:   }
  247: #  my $curdepth=join('_',@Apache::lonxml::depthcounter);
  248: #  print "<br>s $Apache::lonxml::depth : $Apache::lonxml::olddepth : $curdepth : $token->[1]<br>\n";
  249: }
  250: 
  251: sub decreasedepth {
  252:   my ($token) = @_;
  253:   $Apache::lonxml::depth--;
  254:   if ($Apache::lonxml::depth<$Apache::lonxml::olddepth-1) {
  255:     $#Apache::lonxml::depthcounter--;
  256:     $Apache::lonxml::olddepth=$Apache::lonxml::depth+1;
  257:   }
  258: #  my $curdepth=join('_',@Apache::lonxml::depthcounter);
  259: #  print "<br>e $Apache::lonxml::depth : $Apache::lonxml::olddepth : $token->[1] : $curdepth <br>\n";
  260: }
  261: 
  262: sub get_all_text {
  263: 
  264:  my($tag,$pars)= @_;
  265:  my $depth=0;
  266:  my $token;
  267:  my $result='';
  268:  my $tag=substr($tag,1); #strip the / off the tag
  269: # &Apache::lonxml::debug("have:$tag:");
  270:  while (($depth >=0) && ($token = $pars->get_token)) {
  271:    if ($token->[0] eq 'T') {
  272:      $result.=$token->[1];
  273:    } elsif ($token->[0] eq 'S') {
  274:      if ($token->[1] eq $tag) { $depth++; }
  275:      $result.=$token->[4];
  276:    } elsif ($token->[0] eq 'E')  {
  277:      if ( $token->[1] eq $tag) { $depth--; }
  278:      #skip sending back the last end tag
  279:      if ($depth > -1) { $result.=$token->[2]; } else {
  280:        $pars->unget_token($token);
  281:      }
  282:    }
  283:  }
  284:  return $result
  285: }
  286: 
  287: sub newparser {
  288:   my ($parser,$contentref,$dir) = @_;
  289:   push (@$parser,HTML::TokeParser->new($contentref));
  290:   if ( $dir eq '' ) {
  291:     push (@Apache::lonxml::pwd, $Apache::lonxml::pwd[$#Apache::lonxml::pwd]);
  292:   } else {
  293:     push (@Apache::lonxml::pwd, $dir);
  294:   } 
  295: #  &Apache::lonxml::debug("pwd:$#Apache::lonxml::pwd");
  296: #  &Apache::lonxml::debug("pwd:$Apache::lonxml::pwd[$#Apache::lonxml::pwd]");
  297: }
  298: 
  299: sub parstring {
  300:   my ($token) = @_;
  301:   my $temp='';
  302:   map {
  303:     unless ($_=~/\W/) {
  304:       $temp .= "my \$$_=\"$token->[2]->{$_}\";"
  305:     }
  306:   } @{$token->[3]};
  307:   return $temp;
  308: }
  309: 
  310: sub writeallows {
  311:     my $thisurl='/res/'.&Apache::lonnet::declutter(shift);
  312:     my $thisdir=$thisurl;
  313:     $thisdir=~s/\/[^\/]+$//;
  314:     my %httpref=();
  315:     map {
  316:        $httpref{'httpref.'.
  317:  	        &Apache::lonnet::hreflocation($thisdir,$_)}=$thisurl;              } @extlinks;
  318:     &Apache::lonnet::appenv(%httpref);
  319: }
  320: 
  321: sub handler {
  322:   my $request=shift;
  323: 
  324:   my $target='web';
  325:   $Apache::lonxml::debug=1;
  326:   if ($ENV{'browser.mathml'}) {
  327:     $request->content_type('text/xml');
  328:   } else {
  329:     $request->content_type('text/html');
  330:   }
  331: 
  332: #  $request->print(<<ENDHEADER);
  333: #<html>
  334: #<head>
  335: #<title>Just test</title>
  336: #</head>
  337: #<body bgcolor="#FFFFFF">
  338: #ENDHEADER
  339: #  &Apache::lonhomework::send_header($request);
  340:   $request->send_http_header;
  341: 
  342:   return 'OK' if $request->header_only;
  343: 
  344:   $request->print(&Apache::lontexconvert::header());
  345: 
  346:   $request->print('<body bgcolor="#FFFFFF">'."\n");
  347: 
  348:   my $file = "/home/httpd/html".$request->uri;
  349:   my %mystyle;
  350:   my $result = '';
  351:   $result = Apache::lonxml::xmlparse($target, &Apache::lonnet::getfile($file),'',%mystyle);
  352:   $request->print($result);
  353: 
  354:   $request->print('</body>');
  355:   $request->print(&Apache::lontexconvert::footer());
  356:   writeallows($request->uri);
  357:   return 'OK';
  358: }
  359:  
  360: $Apache::lonxml::debug=0;
  361: sub debug {
  362:   if ($Apache::lonxml::debug eq 1) {
  363:     print "DEBUG:".$_[0]."<br>\n";
  364:   }
  365: }
  366: sub error {
  367:   print "ERROR:".$_[0]."<br>\n";
  368: }
  369: sub warning {
  370:   if ($Apache::lonxml::debug eq 1) {
  371:     print "WARNING:".$_[0]."<br>\n";
  372:   }
  373: }
  374: 
  375: 1;
  376: __END__
  377: 
  378: 
  379: 
  380: 
  381: 
  382: 
  383: 
  384: 
  385: 
  386: 
  387: 
  388: 
  389: 
  390: 
  391: 
  392: 
  393: 
  394: 

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