File:  [LON-CAPA] / loncom / xml / lonxml.pm
Revision 1.16: download - view: text, annotated - select for diffs
Thu Aug 3 19:34:11 2000 UTC (23 years, 10 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- modified the xml parser to actually have a stack of parsers and pass
  this stack along

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

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