File:  [LON-CAPA] / loncom / xml / lonxml.pm
Revision 1.43: download - view: text, annotated - select for diffs
Thu Jan 4 19:16:16 2001 UTC (23 years, 6 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- debug is by default off
- throws errors at missing end tags

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

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