File:  [LON-CAPA] / loncom / xml / lonxml.pm
Revision 1.29: download - view: text, annotated - select for diffs
Thu Oct 26 14:01:40 2000 UTC (23 years, 7 months ago) by sakharuk
Branches: MAIN
CVS tags: HEAD
new but not full and last version of cbi.sty

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

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