File:  [LON-CAPA] / loncom / xml / lonxml.pm
Revision 1.11: download - view: text, annotated - select for diffs
Tue Jul 18 21:27:10 2000 UTC (23 years, 10 months ago) by sakharuk
Branches: MAIN
CVS tags: HEAD
*** empty log message ***

    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: 
   12: sub register {
   13:   my $space;
   14:   my @taglist;
   15:   my $temptag;
   16:   ($space,@taglist) = @_;
   17:   foreach $temptag (@taglist) {
   18:     $Apache::lonxml::alltags{$temptag}=$space;
   19:   }
   20: }
   21:                                      
   22: use Apache::style;
   23: use Apache::lontexconvert;
   24: use Apache::run;
   25: use Apache::londefdef;
   26: use Apache::scripttag;
   27: #==================================================   Main subroutine: xmlparse  
   28: 
   29: sub xmlparse {
   30: 
   31:  my ($target,$content_file_string,%style_for_target) = @_;
   32:  my $pars = HTML::TokeParser->new(\$content_file_string);
   33:  my $currentstring = '';
   34:  my $finaloutput = ''; 
   35:  my $newarg = '';
   36:  my $safeeval = new Safe;
   37:  $safeeval->permit("entereval");
   38: #-------------------- Redefinition of the target in the case of compound target
   39: 
   40:  ($target, my @tenta) = split('&&',$target);
   41: 
   42: #------------------------- Stack definition (in stack we have all current tags)
   43: 
   44:  my @stack = (); 
   45:  my @parstack = ();
   46: 
   47: #------------------------------------- Parse input string (content_file_string)
   48:  
   49:  my $token;
   50:  
   51:  while ($token = $pars->get_token) {
   52:    if ($token->[0] eq 'T') {
   53:      $finaloutput .= &Apache::run::evaluate($token->[1],$safeeval,'');
   54:    } elsif ($token->[0] eq 'S') {
   55:      # add tag to stack 	    
   56:      push (@stack,$token->[1]);
   57:      # add parameters list to another stack
   58:      push (@parstack,&parstring($token));
   59:      
   60:      if (exists $style_for_target{$token->[1]}) {
   61:        #basically recurse, but we never got more than one level down so just 
   62:        #create the new context here
   63:        my @innerstack = (); 
   64:        my @innerparstack = ();
   65:        # use style file definition
   66:        $newarg = $style_for_target{$token->[1]};       
   67:        my $pat = HTML::TokeParser->new(\$newarg);
   68:        my $tokenpat = '';
   69:        my $partstring = '';
   70: 
   71:        while  ($tokenpat = $pat->get_token) {
   72: 	 if ($tokenpat->[0] eq 'T') {
   73: 	   $partstring = $tokenpat->[1];
   74: 	 } elsif ($tokenpat->[0] eq 'S') {
   75: 	   push (@innerstack,$tokenpat->[1]);
   76: 	   push (@innerparstack,&parstring($tokenpat));
   77: 	   $partstring = &callsub("start_$tokenpat->[1]", 
   78: 				  $target, $tokenpat, \@innerparstack,
   79: 				  $pat, $safeeval);
   80: 	 } elsif ($tokenpat->[0] eq 'E') {
   81: 	   #clear out any tags that didn't end
   82: 	   while ($tokenpat->[1] ne $innerstack[$#innerstack] 
   83: 		  && ($#innerstack > 0)) {pop @innerstack;pop @innerparstack;}
   84: 	   $partstring = &callsub("end_$tokenpat->[1]",
   85: 				  $target, $tokenpat, \@innerparstack,
   86: 				  $pat, $safeeval);
   87: 	 }
   88: 	 #pass both the variable to the style tag, and the tag we 
   89: 	 #are processing inside the <definedtag>
   90: 	 $finaloutput .= &Apache::run::evaluate($partstring,$safeeval,
   91: 		$parstack[$#parstack].$innerparstack[$#innerparstack]);
   92: 	 if ($tokenpat->[0] eq 'E') { pop @innerstack;pop @innerparstack; }
   93:        }
   94:      } else {
   95:        my $result = &callsub("start_$token->[1]", $target, $token,\@parstack,
   96: 			     $pars, $safeeval);
   97:        $finaloutput .= &Apache::run::evaluate($result,$safeeval,
   98: 					      $parstack[$#parstack]);
   99:      }              
  100:    } elsif ($token->[0] eq 'E')  {
  101:      #clear out any tags that didn't end
  102:      while ($token->[1] ne $stack[$#stack] 
  103: 	    && ($#stack > 0)) {pop @stack;pop @parstack;}
  104: 
  105:      if (exists $style_for_target{'/'."$token->[1]"}) {
  106:        my @innerstack = (); 
  107:        my @innerparstack = ();
  108:        $newarg = $style_for_target{'/'."$token->[1]"};
  109:        my $pat = HTML::TokeParser->new(\$newarg);
  110:        my $tokenpat;
  111:        my $partstring = '';
  112:        
  113:        while  ($tokenpat = $pat->get_token) {
  114: 	 if ($tokenpat->[0] eq 'T') {
  115: 	   $partstring .= $tokenpat->[1];
  116: 	 } elsif ($tokenpat->[0] eq 'S') {
  117: 	   push (@innerstack,$tokenpat->[1]);
  118: 	   push (@innerparstack,&parstring($tokenpat));
  119: 	   $partstring = &callsub("start_$tokenpat->[1]", 
  120: 				  $target, $tokenpat, \@innerparstack,
  121: 				  $pat, $safeeval);
  122: 	 } elsif ($tokenpat->[0] eq 'E') {
  123: 	   #clear out any tags that didn't end
  124: 	   while ($tokenpat->[1] ne $innerstack[$#innerstack] 
  125: 		  && ($#innerstack > 0)) {pop @innerstack;pop @innerparstack;}
  126: 	   $partstring = &callsub("end_$tokenpat->[1]",
  127: 				  $target, $tokenpat, \@innerparstack,
  128: 				  $pat, $safeeval);
  129: 	 }
  130: 	 #pass both the variable to the style tag, and the tag we 
  131: 	 #are processing inside the <definedtag>
  132: 	 $finaloutput .= &Apache::run::evaluate($partstring,$safeeval,
  133: 		$parstack[$#parstack].$innerparstack[$#innerparstack]);
  134: 	 if ($tokenpat->[0] eq 'E') { pop @innerstack;pop @innerparstack; }
  135:        }
  136:      } else {
  137:        my $result = &callsub("end_$token->[1]", $target, $token, \@parstack,
  138: 			     $pars,$safeeval);
  139:        $finaloutput .= &Apache::run::evaluate($result,$safeeval,
  140: 					      $parstack[$#parstack]);
  141:      }
  142:      pop @stack; 
  143:      pop @parstack;
  144:    }
  145:  }
  146:  return $finaloutput;
  147: }
  148: 
  149: sub callsub {
  150:   my ($sub,$target,$token,$parstack,$parser,$safeeval)=@_;
  151:   my $currentstring='';
  152:   {
  153:     no strict 'refs';
  154:     if (my $space=$Apache::lonxml::alltags{$token->[1]}) {
  155:       #print "Calling sub $sub in $space \n";
  156:       $sub="$space\:\:$sub";
  157:       $currentstring = &$sub($target,$token,\@$parstack,$parser,$safeeval);
  158:     } else {
  159:       print "NOT Calling sub $sub\n";
  160:       if (defined($token->[4])) {
  161: 	$currentstring = $token->[4];
  162:       } else {
  163: 	$currentstring = $token->[2];
  164:       }
  165:     }
  166:     use strict 'refs';
  167:   }
  168:   return $currentstring;
  169: }
  170: 
  171: sub parstring {
  172:   my ($token) = @_;
  173:   my $temp='';
  174:   map {$temp .= "my \$$_=\"$token->[2]->{$_}\";"} @{$token->[3]};
  175:   return $temp;
  176: }
  177: 1;
  178: __END__
  179: 
  180: 
  181: 
  182: 
  183: 

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