File:  [LON-CAPA] / loncom / xml / lonxml.pm
Revision 1.32: download - view: text, annotated - select for diffs
Mon Nov 6 14:32:35 2000 UTC (23 years, 7 months ago) by sakharuk
Branches: MAIN
CVS tags: HEAD
added a global variable for control of import (web/tex/meta)

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

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