File:  [LON-CAPA] / loncom / xml / lonxml.pm
Revision 1.31: download - view: text, annotated - select for diffs
Wed Nov 1 17:25:36 2000 UTC (23 years, 7 months ago) by sakharuk
Branches: MAIN
CVS tags: HEAD
new version for switch on and off of metadata output

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

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