File:  [LON-CAPA] / loncom / xml / lonxml.pm
Revision 1.46: download - view: text, annotated - select for diffs
Sat Jan 6 16:22:55 2001 UTC (23 years, 4 months ago) by www
Branches: MAIN
CVS tags: HEAD
Sorry, that should be 'qw'

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

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