File:  [LON-CAPA] / loncom / xml / lonxml.pm
Revision 1.24: download - view: text, annotated - select for diffs
Mon Oct 9 14:07:02 2000 UTC (23 years, 8 months ago) by sakharuk
Branches: MAIN
CVS tags: HEAD
added handler which makes lonxml working with cbi stuff and modified get_all_text to more like a get_text

    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: sub xmlparse {
   31: 
   32:  my ($target,$content_file_string,$safeinit,%style_for_target) = @_;
   33:  my @pars = ();
   34:  @Apache::lonxml::pwd=();
   35:  my $pwd=$ENV{'request.filename'};
   36:  $pwd =~ s:/[^/]*$::;
   37:  &newparser(\@pars,\$content_file_string,$pwd);
   38:  my $currentstring = '';
   39:  my $finaloutput = ''; 
   40:  my $newarg = '';
   41:  my $result;
   42: 
   43:  my $indicator = 1;
   44:  my $pocketbox = '';
   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:  &Apache::run::run($safeinit,$safeeval);
   54: #-------------------- Redefinition of the target in the case of compound target
   55: 
   56:  ($target, my @tenta) = split('&&',$target);
   57: 
   58:  my @stack = (); 
   59:  my @parstack = ();
   60:  &initdepth;
   61:  my $token;
   62:  while ( $#pars > -1 ) {
   63:    while ($token = $pars[$#pars]->get_token) {
   64:      if ($token->[0] eq 'T') {
   65:        $result=$token->[1];
   66: #       $finaloutput .= &Apache::run::evaluate($token->[1],$safeeval,'');
   67:      } elsif ($token->[0] eq 'S') {
   68:        # add tag to stack 	    
   69:        push (@stack,$token->[1]);
   70:        # add parameters list to another stack
   71:        push (@parstack,&parstring($token));
   72:        &increasedepth($token);       
   73:        if (exists $style_for_target{$token->[1]}) {
   74: 
   75: 	if ($indicator == 1) {
   76: 	  $finaloutput .= &recurse($style_for_target{$token->[1]},
   77: 		  		  $target,$safeeval,\%style_for_target,
   78: 	 		 	  @parstack);
   79:         } else {
   80:           $pocketbox .=  &recurse($style_for_target{$token->[1]},
   81: 		  		  $target,$safeeval,\%style_for_target,
   82: 	 		 	  @parstack);
   83:         }
   84: 
   85:        } else {
   86: 	 $result = &callsub("start_$token->[1]", $target, $token,\@parstack,
   87: 			       \@pars, $safeeval, \%style_for_target);
   88: 
   89:          if ($result eq "footnote") { &Apache::lonxml::debug($result);}
   90: 
   91:        }              
   92:      } elsif ($token->[0] eq 'E')  {
   93:        #clear out any tags that didn't end
   94:        while ($token->[1] ne $stack[$#stack] 
   95: 	      && ($#stack > -1)) {pop @stack;pop @parstack;&decreasedepth($token);}
   96:        
   97:        if (exists $style_for_target{'/'."$token->[1]"}) {
   98: 
   99: 	if ($indicator == 1) {
  100: 	 $finaloutput .= &recurse($style_for_target{'/'."$token->[1]"},
  101: 				  $target,$safeeval,\%style_for_target,
  102: 				  @parstack);
  103:         } else {
  104:          $pocketbox .=  &recurse($style_for_target{'/'."$token->[1]"},
  105: 				  $target,$safeeval,\%style_for_target,
  106: 				  @parstack);
  107:         }
  108: 
  109:        } else {
  110: 	 $result = &callsub("end_$token->[1]", $target, $token, \@parstack,
  111: 			       \@pars,$safeeval, \%style_for_target);
  112:        }
  113:      }
  114:      if ($result ne "" ) {
  115:        if ( $#parstack > -1 ) {
  116:  
  117: 	if ($indicator == 1) {
  118: 	 $finaloutput .= &Apache::run::evaluate($result,$safeeval,
  119: 						$parstack[$#parstack]);
  120:         } else {
  121:          $pocketbox .= &Apache::run::evaluate($result,$safeeval,
  122: 						$parstack[$#parstack]);
  123:         }
  124: 
  125:        } else {
  126: 	 $finaloutput .= &Apache::run::evaluate($result,$safeeval,'');
  127:        }
  128:        $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: #      if ( $sub eq "start_parserlib" ) {
  207: #	  print "me:".%$style.":\n";
  208: #      }
  209:       $sub1="$space\:\:$sub";
  210:       $Apache::lonxml::curdepth=join('_',@Apache::lonxml::depthcounter);
  211:       $currentstring = &$sub1($target,$token,$parstack,$parser,
  212: 			     $safeeval,$style);
  213: #      if ( $sub eq "start_parserlib" ) {
  214: #	  print "me2:".%$style.":";
  215: #      }
  216:     } else {
  217:       #&Apache::lonxml::debug("NOT Calling sub $sub in $space<br>\n");
  218:       if (defined($token->[4])) {
  219: 	$currentstring = $token->[4];
  220:       } else {
  221: 	$currentstring = $token->[2];
  222:       }
  223:     }
  224:     use strict 'refs';
  225:   }
  226:   return $currentstring;
  227: }
  228: 
  229: sub initdepth {
  230:   @Apache::lonxml::depthcounter=();
  231:   $Apache::lonxml::depth=-1;
  232:   $Apache::lonxml::olddepth=-1;
  233: }
  234: 
  235: sub increasedepth {
  236:   my ($token) = @_;
  237:   if ($Apache::lonxml::depth<$Apache::lonxml::olddepth-1) {
  238:     $#Apache::lonxml::depthcounter--;
  239:     $Apache::lonxml::olddepth=$Apache::lonxml::depth;
  240:   }
  241:   $Apache::lonxml::depth++;
  242: #  print "<br>s $Apache::lonxml::depth : $Apache::lonxml::olddepth : $token->[1]<br>\n";
  243:   $Apache::lonxml::depthcounter[$Apache::lonxml::depth]++;
  244:   if ($Apache::lonxml::depthcounter[$Apache::lonxml::depth]==1) {
  245:     $Apache::lonxml::olddepth=$Apache::lonxml::depth;
  246:   }
  247: }
  248: 
  249: sub decreasedepth {
  250:   my ($token) = @_;
  251:   $Apache::lonxml::depth--;
  252: #  print "<br>e $Apache::lonxml::depth : $Apache::lonxml::olddepth : $token->[1]<br>\n";
  253: }
  254: 
  255: sub get_all_text {
  256: 
  257:  my($tag,$pars)= @_;
  258:  my $depth=0;
  259:  my $token;
  260:  my $result='';
  261:  my $tag=substr($tag,1); #strip the / off the tag
  262: # &Apache::lonxml::debug("have:$tag:");
  263:  while (($depth >=0) && ($token = $pars->get_token)) {
  264:    if ($token->[0] eq 'T') {
  265:      $result.=$token->[1];
  266:    } elsif ($token->[0] eq 'S') {
  267:      if ($token->[1] eq $tag) { $depth++; }
  268:      $result.=$token->[4];
  269:    } elsif ($token->[0] eq 'E')  {
  270:      if ( $token->[1] eq $tag) { $depth--; }
  271:      #skip sending back the last end tag
  272:      if ($depth > -1) { $result.=$token->[2]; }
  273:    }
  274:  }
  275:  return $result
  276: }
  277: 
  278: sub newparser {
  279:   my ($parser,$contentref,$dir) = @_;
  280:   push (@$parser,HTML::TokeParser->new($contentref));
  281:   if ( $dir eq '' ) {
  282:     push (@Apache::lonxml::pwd, $Apache::lonxml::pwd[$#Apache::lonxml::pwd]);
  283:   } else {
  284:     push (@Apache::lonxml::pwd, $dir);
  285:   } 
  286: #  &Apache::lonxml::debug("pwd:$#Apache::lonxml::pwd");
  287: #  &Apache::lonxml::debug("pwd:$Apache::lonxml::pwd[$#Apache::lonxml::pwd]");
  288: }
  289: 
  290: sub parstring {
  291:   my ($token) = @_;
  292:   my $temp='';
  293:   map {
  294:     if ($_=~/\w+/) {
  295:       $temp .= "my \$$_=\"$token->[2]->{$_}\";"
  296:     }
  297:   } @{$token->[3]};
  298:   return $temp;
  299: }
  300: #<<<<<<< lonxml.pm
  301: 
  302: sub handler {
  303:   my $request=shift;
  304: 
  305:   my $target='web';
  306:   $Apache::lonxml::debug=1;
  307:   $request->content_type('text/html');
  308:   $request->send_http_header;  
  309:   $request->print(<<ENDHEADER);
  310: <html>
  311: <head>
  312: <title>Just test</title>
  313: </head>
  314: <body bgcolor="#FFFFFF">
  315: ENDHEADER
  316: #  &Apache::lonhomework::send_header($request);
  317:   my $file = "/home/httpd/html".$request->uri;
  318:   my %mystyle;
  319:   my $result = '';
  320:   $result = Apache::lonxml::xmlparse($target, &Apache::lonnet::getfile($file),'',%mystyle);
  321: #  $request->print("Result follows:");  
  322:   $request->print($result);
  323: #  $request->print(":Result ends");
  324: }
  325:  
  326: $Apache::lonxml::debug=0;
  327: sub debug {
  328:   if ($Apache::lonxml::debug eq 1) {
  329:     print "DEBUG:".$_[0]."<br>\n";
  330:   }
  331: }
  332: sub error {
  333:   print "ERROR:".$_[0]."<br>\n";
  334: }
  335: sub warning {
  336:   if ($Apache::lonxml::debug eq 1) {
  337:     print "WARNING:".$_[0]."<br>\n";
  338:   }
  339: }
  340: 
  341: 1;
  342: __END__
  343: 
  344: 
  345: 
  346: 
  347: 

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