Annotation of loncom/xml/lonxml.pm, revision 1.23

1.2       sakharuk    1: # The LearningOnline Network with CAPA
1.3       sakharuk    2: # XML Parser Module 
1.2       sakharuk    3: #
1.3       sakharuk    4: # last modified 06/26/00 by Alexander Sakharuk
1.2       sakharuk    5: 
1.4       albertel    6: package Apache::lonxml; 
1.1       sakharuk    7: 
                      8: use strict;
                      9: use HTML::TokeParser;
1.3       sakharuk   10: use Safe;
1.13      albertel   11: use Opcode;
1.7       albertel   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: }
1.11      sakharuk   22:                                      
1.4       albertel   23: use Apache::style;
1.3       sakharuk   24: use Apache::lontexconvert;
1.7       albertel   25: use Apache::run;
1.4       albertel   26: use Apache::londefdef;
1.7       albertel   27: use Apache::scripttag;
1.3       sakharuk   28: #==================================================   Main subroutine: xmlparse  
1.23    ! albertel   29: @Apache::lonxml::pwd=();
1.3       sakharuk   30: sub xmlparse {
                     31: 
1.18      albertel   32:  my ($target,$content_file_string,$safeinit,%style_for_target) = @_;
1.16      albertel   33:  my @pars = ();
1.23    ! albertel   34:  @Apache::lonxml::pwd=();
        !            35:  my $pwd=$ENV{'request.filename'};
        !            36:  $pwd =~ s:/[^/]*$::;
        !            37:  &newparser(\@pars,\$content_file_string,$pwd);
1.3       sakharuk   38:  my $currentstring = '';
                     39:  my $finaloutput = ''; 
                     40:  my $newarg = '';
1.16      albertel   41:  my $result;
1.3       sakharuk   42:  my $safeeval = new Safe;
1.6       albertel   43:  $safeeval->permit("entereval");
1.13      albertel   44:  $safeeval->permit(":base_math");
1.19      albertel   45:  $safeeval->deny(":base_io");
                     46: #need to inspect this class of ops
                     47: # $safeeval->deny(":base_orig");
1.21      albertel   48:  $safeinit .= ';$external::target='.$target.';';
                     49:  &Apache::run::run($safeinit,$safeeval);
1.3       sakharuk   50: #-------------------- Redefinition of the target in the case of compound target
                     51: 
                     52:  ($target, my @tenta) = split('&&',$target);
                     53: 
                     54:  my @stack = (); 
                     55:  my @parstack = ();
1.17      albertel   56:  &initdepth;
1.3       sakharuk   57:  my $token;
1.16      albertel   58:  while ( $#pars > -1 ) {
                     59:    while ($token = $pars[$#pars]->get_token) {
                     60:      if ($token->[0] eq 'T') {
                     61:        $result=$token->[1];
                     62: #       $finaloutput .= &Apache::run::evaluate($token->[1],$safeeval,'');
                     63:      } elsif ($token->[0] eq 'S') {
                     64:        # add tag to stack 	    
                     65:        push (@stack,$token->[1]);
                     66:        # add parameters list to another stack
                     67:        push (@parstack,&parstring($token));
1.19      albertel   68:        &increasedepth($token);       
1.16      albertel   69:        if (exists $style_for_target{$token->[1]}) {
                     70: 	 $finaloutput .= &recurse($style_for_target{$token->[1]},
                     71: 				  $target,$safeeval,\%style_for_target,
                     72: 				  @parstack);
                     73:        } else {
1.17      albertel   74: 	 $result = &callsub("start_$token->[1]", $target, $token,\@parstack,
1.16      albertel   75: 			       \@pars, $safeeval, \%style_for_target);
                     76:        }              
                     77:      } elsif ($token->[0] eq 'E')  {
                     78:        #clear out any tags that didn't end
                     79:        while ($token->[1] ne $stack[$#stack] 
1.19      albertel   80: 	      && ($#stack > -1)) {pop @stack;pop @parstack;&decreasedepth($token);}
1.16      albertel   81:        
                     82:        if (exists $style_for_target{'/'."$token->[1]"}) {
                     83: 	 $finaloutput .= &recurse($style_for_target{'/'."$token->[1]"},
                     84: 				  $target,$safeeval,\%style_for_target,
                     85: 				  @parstack);
                     86:        } else {
1.17      albertel   87: 	 $result = &callsub("end_$token->[1]", $target, $token, \@parstack,
1.16      albertel   88: 			       \@pars,$safeeval, \%style_for_target);
1.13      albertel   89:        }
1.16      albertel   90:      }
                     91:      if ($result ne "" ) {
                     92:        if ( $#parstack > -1 ) { 
1.13      albertel   93: 	 $finaloutput .= &Apache::run::evaluate($result,$safeeval,
                     94: 						$parstack[$#parstack]);
1.16      albertel   95:        } else {
                     96: 	 $finaloutput .= &Apache::run::evaluate($result,$safeeval,'');
1.13      albertel   97:        }
1.16      albertel   98:        $result = '';
1.2       sakharuk   99:      }
1.19      albertel  100:      if ($token->[0] eq 'E') { pop @stack;pop @parstack;&decreasedepth($token);}
1.5       albertel  101:    }
1.16      albertel  102:    pop @pars;
1.23    ! albertel  103:    pop @Apache::lonxml::pwd;
1.3       sakharuk  104:  }
                    105:  return $finaloutput;
1.15      albertel  106: }
                    107: 
                    108: sub recurse {
                    109:   
                    110:   my @innerstack = (); 
                    111:   my @innerparstack = ();
                    112:   my ($newarg,$target,$safeeval,$style_for_target,@parstack) = @_;
1.16      albertel  113:   my @pat = ();
1.23    ! albertel  114:   &newparser(\@pat,\$newarg);
1.15      albertel  115:   my $tokenpat;
                    116:   my $partstring = '';
                    117:   my $output='';
1.16      albertel  118:   my $decls='';
                    119:   while ( $#pat > -1 ) {
                    120:     while  ($tokenpat = $pat[$#pat]->get_token) {
                    121:       if ($tokenpat->[0] eq 'T') {
                    122: 	$partstring = $tokenpat->[1];
                    123:       } elsif ($tokenpat->[0] eq 'S') {
                    124: 	push (@innerstack,$tokenpat->[1]);
                    125: 	push (@innerparstack,&parstring($tokenpat));
1.19      albertel  126: 	&increasedepth($tokenpat);
1.16      albertel  127: 	$partstring = &callsub("start_$tokenpat->[1]", 
                    128: 			       $target, $tokenpat, \@innerparstack,
                    129: 			       \@pat, $safeeval, $style_for_target);
                    130:       } elsif ($tokenpat->[0] eq 'E') {
                    131: 	#clear out any tags that didn't end
                    132: 	while ($tokenpat->[1] ne $innerstack[$#innerstack] 
1.17      albertel  133: 	       && ($#innerstack > -1)) {pop @innerstack;pop @innerparstack;
1.19      albertel  134: 					&decreasedepth($tokenpat);}
1.16      albertel  135: 	$partstring = &callsub("end_$tokenpat->[1]",
                    136: 			       $target, $tokenpat, \@innerparstack,
                    137: 			       \@pat, $safeeval, $style_for_target);
                    138:       }
                    139:       #pass both the variable to the style tag, and the tag we 
                    140:       #are processing inside the <definedtag>
                    141:       if ( $partstring ne "" ) {
                    142: 	if ( $#parstack > -1 ) { 
                    143: 	  if ( $#innerparstack > -1 ) { 
                    144: 	    $decls= $parstack[$#parstack].$innerparstack[$#innerparstack];
                    145: 	  } else {
                    146: 	    $decls= $parstack[$#parstack];
                    147: 	  }
                    148: 	} else {
                    149: 	  if ( $#innerparstack > -1 ) { 
                    150: 	    $decls=$innerparstack[$#innerparstack];
                    151: 	  } else {
                    152: 	    $decls='';
                    153: 	  }
                    154: 	}
                    155: 	$output .= &Apache::run::evaluate($partstring,$safeeval,$decls);
                    156: 	$partstring = '';
                    157:       }
1.17      albertel  158:       if ($tokenpat->[0] eq 'E') { pop @innerstack;pop @innerparstack;
1.19      albertel  159: 				 &decreasedepth($tokenpat);}
1.15      albertel  160:     }
1.16      albertel  161:     pop @pat;
1.23    ! albertel  162:     pop @Apache::lonxml::pwd;
1.15      albertel  163:   }
                    164:   return $output;
1.7       albertel  165: }
                    166: 
                    167: sub callsub {
1.14      albertel  168:   my ($sub,$target,$token,$parstack,$parser,$safeeval,$style)=@_;
1.7       albertel  169:   my $currentstring='';
                    170:   {
                    171:     no strict 'refs';
                    172:     if (my $space=$Apache::lonxml::alltags{$token->[1]}) {
1.23    ! albertel  173:       #&Apache::lonxml::debug("Calling sub $sub in $space<br>\n");
1.7       albertel  174:       $sub="$space\:\:$sub";
1.17      albertel  175:       $Apache::lonxml::curdepth=join('_',@Apache::lonxml::depthcounter);
1.16      albertel  176:       $currentstring = &$sub($target,$token,$parstack,$parser,
                    177: 			     $safeeval,$style);
1.7       albertel  178:     } else {
1.23    ! albertel  179:       #&Apache::lonxml::debug("NOT Calling sub $sub in $space<br>\n");
1.7       albertel  180:       if (defined($token->[4])) {
                    181: 	$currentstring = $token->[4];
                    182:       } else {
                    183: 	$currentstring = $token->[2];
                    184:       }
                    185:     }
                    186:     use strict 'refs';
                    187:   }
                    188:   return $currentstring;
1.17      albertel  189: }
                    190: 
                    191: sub initdepth {
                    192:   @Apache::lonxml::depthcounter=();
                    193:   $Apache::lonxml::depth=-1;
                    194:   $Apache::lonxml::olddepth=-1;
                    195: }
                    196: 
                    197: sub increasedepth {
1.19      albertel  198:   my ($token) = @_;
1.17      albertel  199:   if ($Apache::lonxml::depth<$Apache::lonxml::olddepth-1) {
                    200:     $#Apache::lonxml::depthcounter--;
                    201:     $Apache::lonxml::olddepth=$Apache::lonxml::depth;
                    202:   }
                    203:   $Apache::lonxml::depth++;
1.19      albertel  204: #  print "<br>s $Apache::lonxml::depth : $Apache::lonxml::olddepth : $token->[1]<br>\n";
1.17      albertel  205:   $Apache::lonxml::depthcounter[$Apache::lonxml::depth]++;
                    206:   if ($Apache::lonxml::depthcounter[$Apache::lonxml::depth]==1) {
                    207:     $Apache::lonxml::olddepth=$Apache::lonxml::depth;
                    208:   }
                    209: }
                    210: 
                    211: sub decreasedepth {
1.19      albertel  212:   my ($token) = @_;
1.17      albertel  213:   $Apache::lonxml::depth--;
1.19      albertel  214: #  print "<br>e $Apache::lonxml::depth : $Apache::lonxml::olddepth : $token->[1]<br>\n";
1.1       sakharuk  215: }
1.19      albertel  216: 
                    217: sub get_all_text {
                    218: 
                    219:  my($tag,$pars)= @_;
                    220:  my $depth=0;
                    221:  my $token;
                    222:  my $result='';
                    223:  while (($depth >=0) && ($token = $pars->get_token)) {
                    224:    if ($token->[0] eq 'T') {
                    225:      $result.=$token->[1];
                    226:    } elsif ($token->[0] eq 'S') {
                    227:      if ($token->[1] eq $tag) { $depth++; }
                    228:      $result.=$token->[4];
                    229:    } elsif ($token->[0] eq 'E')  {
                    230:      if ($token->[1] eq $tag) { $depth--; }
                    231:      #skip sending back the last end tag
                    232:      if ($depth > -1) { $result.=$token->[2]; }
                    233:    }
                    234:  }
                    235:  return $result
                    236: }
                    237: 
1.23    ! albertel  238: sub newparser {
        !           239:   my ($parser,$contentref,$dir) = @_;
        !           240:   push (@$parser,HTML::TokeParser->new($contentref));
        !           241:   if ( $dir eq '' ) {
        !           242:     push (@Apache::lonxml::pwd, $Apache::lonxml::pwd[$#Apache::lonxml::pwd]);
        !           243:   } else {
        !           244:     push (@Apache::lonxml::pwd, $dir);
        !           245:   } 
        !           246: #  &Apache::lonxml::debug("pwd:$#Apache::lonxml::pwd");
        !           247: #  &Apache::lonxml::debug("pwd:$Apache::lonxml::pwd[$#Apache::lonxml::pwd]");
        !           248: }
1.1       sakharuk  249: 
1.8       albertel  250: sub parstring {
                    251:   my ($token) = @_;
                    252:   my $temp='';
1.20      albertel  253:   map {
                    254:     if ($_=~/\w+/) {
                    255:       $temp .= "my \$$_=\"$token->[2]->{$_}\";"
                    256:     }
                    257:   } @{$token->[3]};
1.8       albertel  258:   return $temp;
                    259: }
1.22      albertel  260: 
                    261: $Apache::lonxml::debug=0;
                    262: sub debug {
                    263:   if ($Apache::lonxml::debug eq 1) {
                    264:     print "DEBUG:".$_[0]."<br>\n";
                    265:   }
                    266: }
                    267: sub error {
1.23    ! albertel  268:   print "ERROR:".$_[0]."<br>\n";
1.22      albertel  269: }
                    270: sub warning {
                    271:   if ($Apache::lonxml::debug eq 1) {
                    272:     print "WARNING:".$_[0]."<br>\n";
                    273:   }
                    274: }
                    275: 
1.1       sakharuk  276: 1;
                    277: __END__
1.11      sakharuk  278: 
                    279: 
                    280: 
                    281: 
                    282: 

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