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

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.33      www         5: # 11/6 Gerd Kortemeyer
1.45      www         6: # 6/1/1 Gerd Kortemeyer
1.2       sakharuk    7: 
1.4       albertel    8: package Apache::lonxml; 
1.33      www         9: use vars 
                     10: qw(@pwd $outputstack $redirection $textredirection $on_offimport @extlinks);
1.1       sakharuk   11: use strict;
                     12: use HTML::TokeParser;
1.3       sakharuk   13: use Safe;
1.40      albertel   14: use Safe::Hole;
1.13      albertel   15: use Opcode;
1.46      www        16: use Apache::Constants qw(:common);
1.7       albertel   17: 
                     18: sub register {
                     19:   my $space;
                     20:   my @taglist;
                     21:   my $temptag;
                     22:   ($space,@taglist) = @_;
                     23:   foreach $temptag (@taglist) {
                     24:     $Apache::lonxml::alltags{$temptag}=$space;
                     25:   }
                     26: }
1.11      sakharuk   27:                                      
1.4       albertel   28: use Apache::style;
1.3       sakharuk   29: use Apache::lontexconvert;
1.7       albertel   30: use Apache::run;
1.4       albertel   31: use Apache::londefdef;
1.7       albertel   32: use Apache::scripttag;
1.3       sakharuk   33: #==================================================   Main subroutine: xmlparse  
1.33      www        34: @pwd=();
                     35: $outputstack = '';
                     36: $redirection = 1;
                     37: $textredirection = 1;
                     38: $on_offimport = 0;
                     39: @extlinks=();
1.31      sakharuk   40: 
1.3       sakharuk   41: sub xmlparse {
                     42: 
1.18      albertel   43:  my ($target,$content_file_string,$safeinit,%style_for_target) = @_;
1.41      albertel   44:  if ($target eq 'meta') {
                     45:    $Apache::lonxml::textredirection = 0;
                     46:    $Apache::lonxml::on_offimport = 1;
1.47    ! albertel   47:  } elsif ($target eq 'grade') {
        !            48:    $Apache::lonxml::textredirection = 0;
        !            49:    $Apache::lonxml::on_offimport = 0;
1.44      albertel   50:  } else {
                     51:    $Apache::lonxml::textredirection = 1;
                     52:    $Apache::lonxml::on_offimport = 0;
1.32      sakharuk   53:  }
1.16      albertel   54:  my @pars = ();
1.23      albertel   55:  @Apache::lonxml::pwd=();
                     56:  my $pwd=$ENV{'request.filename'};
                     57:  $pwd =~ s:/[^/]*$::;
                     58:  &newparser(\@pars,\$content_file_string,$pwd);
1.3       sakharuk   59:  my $currentstring = '';
                     60:  my $finaloutput = ''; 
                     61:  my $newarg = '';
1.16      albertel   62:  my $result;
1.24      sakharuk   63: 
1.3       sakharuk   64:  my $safeeval = new Safe;
1.40      albertel   65:  my $safehole = new Safe::Hole;
1.6       albertel   66:  $safeeval->permit("entereval");
1.13      albertel   67:  $safeeval->permit(":base_math");
1.19      albertel   68:  $safeeval->deny(":base_io");
1.40      albertel   69:  $safehole->wrap(\&Apache::lonnet::EXT,$safeeval,'&EXT');
1.19      albertel   70: #need to inspect this class of ops
                     71: # $safeeval->deny(":base_orig");
1.21      albertel   72:  $safeinit .= ';$external::target='.$target.';';
1.26      albertel   73:  $safeinit .= ';$external::randomseed='.&Apache::lonnet::rndseed().';';
1.21      albertel   74:  &Apache::run::run($safeinit,$safeeval);
1.3       sakharuk   75: #-------------------- Redefinition of the target in the case of compound target
                     76: 
                     77:  ($target, my @tenta) = split('&&',$target);
                     78: 
                     79:  my @stack = (); 
                     80:  my @parstack = ();
1.17      albertel   81:  &initdepth;
1.3       sakharuk   82:  my $token;
1.16      albertel   83:  while ( $#pars > -1 ) {
                     84:    while ($token = $pars[$#pars]->get_token) {
                     85:      if ($token->[0] eq 'T') {
1.41      albertel   86:        if ($Apache::lonxml::textredirection == 1) {$result=$token->[1];}
                     87:        # $finaloutput .= &Apache::run::evaluate($token->[1],$safeeval,'');
1.16      albertel   88:      } elsif ($token->[0] eq 'S') {
1.41      albertel   89:        # if ($target eq 'meta' and $token->[2]->{metaout} eq 'ON') {$Apache::lonxml::textredirection = 1;}
1.16      albertel   90:        # add tag to stack 	    
                     91:        push (@stack,$token->[1]);
                     92:        # add parameters list to another stack
                     93:        push (@parstack,&parstring($token));
1.19      albertel   94:        &increasedepth($token);       
1.16      albertel   95:        if (exists $style_for_target{$token->[1]}) {
1.41      albertel   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: 	 
1.16      albertel  107:        } else {
1.17      albertel  108: 	 $result = &callsub("start_$token->[1]", $target, $token,\@parstack,
1.41      albertel  109: 			    \@pars, $safeeval, \%style_for_target);
1.16      albertel  110:        }              
                    111:      } elsif ($token->[0] eq 'E')  {
1.41      albertel  112:        #if ($target eq 'meta') {$Apache::lonxml::textredirection = 0;}
1.16      albertel  113:        #clear out any tags that didn't end
                    114:        while ($token->[1] ne $stack[$#stack] 
1.43      albertel  115: 	      && ($#stack > -1)) {
                    116: 	 &Apache::lonxml::error("Unbalanced tags in resource $stack['-1']");   
                    117: 	 pop @stack;pop @parstack;&decreasedepth($token);
                    118:        }
1.16      albertel  119:        
                    120:        if (exists $style_for_target{'/'."$token->[1]"}) {
1.24      sakharuk  121: 
1.25      sakharuk  122: 	if ($Apache::lonxml::redirection == 1) {
1.16      albertel  123: 	 $finaloutput .= &recurse($style_for_target{'/'."$token->[1]"},
                    124: 				  $target,$safeeval,\%style_for_target,
                    125: 				  @parstack);
1.24      sakharuk  126:         } else {
1.25      sakharuk  127:          $Apache::lonxml::outputstack .=  &recurse($style_for_target{'/'."$token->[1]"},
1.24      sakharuk  128: 				  $target,$safeeval,\%style_for_target,
                    129: 				  @parstack);
                    130:         }
                    131: 
1.16      albertel  132:        } else {
1.17      albertel  133: 	 $result = &callsub("end_$token->[1]", $target, $token, \@parstack,
1.16      albertel  134: 			       \@pars,$safeeval, \%style_for_target);
1.13      albertel  135:        }
1.16      albertel  136:      }
1.25      sakharuk  137:      if ($result ne "") {
1.24      sakharuk  138:        if ( $#parstack > -1 ) {
                    139:  
1.25      sakharuk  140: 	if ($Apache::lonxml::redirection == 1) {
1.13      albertel  141: 	 $finaloutput .= &Apache::run::evaluate($result,$safeeval,
                    142: 						$parstack[$#parstack]);
1.24      sakharuk  143:         } else {
1.25      sakharuk  144:          $Apache::lonxml::outputstack .= &Apache::run::evaluate($result,$safeeval,
1.24      sakharuk  145: 						$parstack[$#parstack]);
                    146:         }
                    147: 
1.16      albertel  148:        } else {
                    149: 	 $finaloutput .= &Apache::run::evaluate($result,$safeeval,'');
1.13      albertel  150:        }
1.16      albertel  151:        $result = '';
1.25      sakharuk  152:      } else {
                    153:          $finaloutput .= $result;
1.2       sakharuk  154:      }
1.19      albertel  155:      if ($token->[0] eq 'E') { pop @stack;pop @parstack;&decreasedepth($token);}
1.5       albertel  156:    }
1.16      albertel  157:    pop @pars;
1.23      albertel  158:    pop @Apache::lonxml::pwd;
1.3       sakharuk  159:  }
1.24      sakharuk  160: 
1.3       sakharuk  161:  return $finaloutput;
1.15      albertel  162: }
                    163: 
                    164: sub recurse {
                    165:   
                    166:   my @innerstack = (); 
                    167:   my @innerparstack = ();
                    168:   my ($newarg,$target,$safeeval,$style_for_target,@parstack) = @_;
1.16      albertel  169:   my @pat = ();
1.23      albertel  170:   &newparser(\@pat,\$newarg);
1.15      albertel  171:   my $tokenpat;
                    172:   my $partstring = '';
                    173:   my $output='';
1.16      albertel  174:   my $decls='';
                    175:   while ( $#pat > -1 ) {
                    176:     while  ($tokenpat = $pat[$#pat]->get_token) {
                    177:       if ($tokenpat->[0] eq 'T') {
1.30      sakharuk  178: 	  if ($Apache::lonxml::textredirection == 1) {$partstring = $tokenpat->[1];}
1.16      albertel  179:       } elsif ($tokenpat->[0] eq 'S') {
                    180: 	push (@innerstack,$tokenpat->[1]);
                    181: 	push (@innerparstack,&parstring($tokenpat));
1.19      albertel  182: 	&increasedepth($tokenpat);
1.16      albertel  183: 	$partstring = &callsub("start_$tokenpat->[1]", 
                    184: 			       $target, $tokenpat, \@innerparstack,
                    185: 			       \@pat, $safeeval, $style_for_target);
                    186:       } elsif ($tokenpat->[0] eq 'E') {
                    187: 	#clear out any tags that didn't end
                    188: 	while ($tokenpat->[1] ne $innerstack[$#innerstack] 
1.43      albertel  189: 	       && ($#innerstack > -1)) {
                    190: 	  &Apache::lonxml::error("Unbalanced tags in resource $innerstack['-1']");
                    191: 	  pop @innerstack;pop @innerparstack;&decreasedepth($tokenpat);
                    192: 	}
1.16      albertel  193: 	$partstring = &callsub("end_$tokenpat->[1]",
                    194: 			       $target, $tokenpat, \@innerparstack,
                    195: 			       \@pat, $safeeval, $style_for_target);
                    196:       }
                    197:       #pass both the variable to the style tag, and the tag we 
                    198:       #are processing inside the <definedtag>
                    199:       if ( $partstring ne "" ) {
                    200: 	if ( $#parstack > -1 ) { 
                    201: 	  if ( $#innerparstack > -1 ) { 
                    202: 	    $decls= $parstack[$#parstack].$innerparstack[$#innerparstack];
                    203: 	  } else {
                    204: 	    $decls= $parstack[$#parstack];
                    205: 	  }
                    206: 	} else {
                    207: 	  if ( $#innerparstack > -1 ) { 
                    208: 	    $decls=$innerparstack[$#innerparstack];
                    209: 	  } else {
                    210: 	    $decls='';
                    211: 	  }
                    212: 	}
                    213: 	$output .= &Apache::run::evaluate($partstring,$safeeval,$decls);
                    214: 	$partstring = '';
                    215:       }
1.17      albertel  216:       if ($tokenpat->[0] eq 'E') { pop @innerstack;pop @innerparstack;
1.19      albertel  217: 				 &decreasedepth($tokenpat);}
1.15      albertel  218:     }
1.16      albertel  219:     pop @pat;
1.23      albertel  220:     pop @Apache::lonxml::pwd;
1.15      albertel  221:   }
                    222:   return $output;
1.7       albertel  223: }
                    224: 
                    225: sub callsub {
1.14      albertel  226:   my ($sub,$target,$token,$parstack,$parser,$safeeval,$style)=@_;
1.7       albertel  227:   my $currentstring='';
                    228:   {
1.24      sakharuk  229:       my $sub1;
1.7       albertel  230:     no strict 'refs';
                    231:     if (my $space=$Apache::lonxml::alltags{$token->[1]}) {
1.26      albertel  232:       #&Apache::lonxml::debug("Calling sub $sub in $space<br>\n");
1.24      sakharuk  233:       $sub1="$space\:\:$sub";
1.17      albertel  234:       $Apache::lonxml::curdepth=join('_',@Apache::lonxml::depthcounter);
1.24      sakharuk  235:       $currentstring = &$sub1($target,$token,$parstack,$parser,
1.16      albertel  236: 			     $safeeval,$style);
1.7       albertel  237:     } else {
1.23      albertel  238:       #&Apache::lonxml::debug("NOT Calling sub $sub in $space<br>\n");
1.7       albertel  239:       if (defined($token->[4])) {
                    240: 	$currentstring = $token->[4];
                    241:       } else {
                    242: 	$currentstring = $token->[2];
                    243:       }
                    244:     }
                    245:     use strict 'refs';
                    246:   }
                    247:   return $currentstring;
1.17      albertel  248: }
                    249: 
                    250: sub initdepth {
                    251:   @Apache::lonxml::depthcounter=();
                    252:   $Apache::lonxml::depth=-1;
                    253:   $Apache::lonxml::olddepth=-1;
                    254: }
                    255: 
                    256: sub increasedepth {
1.19      albertel  257:   my ($token) = @_;
1.17      albertel  258:   $Apache::lonxml::depth++;
                    259:   $Apache::lonxml::depthcounter[$Apache::lonxml::depth]++;
                    260:   if ($Apache::lonxml::depthcounter[$Apache::lonxml::depth]==1) {
                    261:     $Apache::lonxml::olddepth=$Apache::lonxml::depth;
                    262:   }
1.42      albertel  263:   my $curdepth=join('_',@Apache::lonxml::depthcounter);
                    264:   &Apache::lonxml::debug("s $Apache::lonxml::depth : $Apache::lonxml::olddepth : $curdepth : $token->[1]\n");
1.43      albertel  265: #print "<br>s $Apache::lonxml::depth : $Apache::lonxml::olddepth : $curdepth : $token->[1]\n";
1.17      albertel  266: }
                    267: 
                    268: sub decreasedepth {
1.19      albertel  269:   my ($token) = @_;
1.17      albertel  270:   $Apache::lonxml::depth--;
1.36      albertel  271:   if ($Apache::lonxml::depth<$Apache::lonxml::olddepth-1) {
                    272:     $#Apache::lonxml::depthcounter--;
                    273:     $Apache::lonxml::olddepth=$Apache::lonxml::depth+1;
                    274:   }
1.43      albertel  275:   if (  $Apache::lonxml::depth < -1) {
                    276:     &Apache::lonxml::error("Unbalanced tags in resource");   
                    277:     $Apache::lonxml::depth='-1';
                    278:   }
1.42      albertel  279:   my $curdepth=join('_',@Apache::lonxml::depthcounter);
                    280:   &Apache::lonxml::debug("e $Apache::lonxml::depth : $Apache::lonxml::olddepth : $token->[1] : $curdepth\n");
1.43      albertel  281: #print "<br>e $Apache::lonxml::depth : $Apache::lonxml::olddepth : $token->[1] : $curdepth\n";
1.1       sakharuk  282: }
1.19      albertel  283: 
                    284: sub get_all_text {
                    285: 
                    286:  my($tag,$pars)= @_;
                    287:  my $depth=0;
                    288:  my $token;
                    289:  my $result='';
1.24      sakharuk  290:  my $tag=substr($tag,1); #strip the / off the tag
                    291: # &Apache::lonxml::debug("have:$tag:");
1.19      albertel  292:  while (($depth >=0) && ($token = $pars->get_token)) {
                    293:    if ($token->[0] eq 'T') {
                    294:      $result.=$token->[1];
                    295:    } elsif ($token->[0] eq 'S') {
                    296:      if ($token->[1] eq $tag) { $depth++; }
                    297:      $result.=$token->[4];
                    298:    } elsif ($token->[0] eq 'E')  {
1.24      sakharuk  299:      if ( $token->[1] eq $tag) { $depth--; }
1.19      albertel  300:      #skip sending back the last end tag
1.36      albertel  301:      if ($depth > -1) { $result.=$token->[2]; } else {
                    302:        $pars->unget_token($token);
                    303:      }
1.19      albertel  304:    }
                    305:  }
                    306:  return $result
                    307: }
                    308: 
1.23      albertel  309: sub newparser {
                    310:   my ($parser,$contentref,$dir) = @_;
                    311:   push (@$parser,HTML::TokeParser->new($contentref));
                    312:   if ( $dir eq '' ) {
                    313:     push (@Apache::lonxml::pwd, $Apache::lonxml::pwd[$#Apache::lonxml::pwd]);
                    314:   } else {
                    315:     push (@Apache::lonxml::pwd, $dir);
                    316:   } 
                    317: #  &Apache::lonxml::debug("pwd:$#Apache::lonxml::pwd");
                    318: #  &Apache::lonxml::debug("pwd:$Apache::lonxml::pwd[$#Apache::lonxml::pwd]");
                    319: }
1.1       sakharuk  320: 
1.8       albertel  321: sub parstring {
                    322:   my ($token) = @_;
                    323:   my $temp='';
1.20      albertel  324:   map {
1.35      www       325:     unless ($_=~/\W/) {
1.42      albertel  326:       my $val=$token->[2]->{$_};
                    327:       if ($val =~ m/^[\%\@]/) { $val="\\".$val; }
                    328:       $temp .= "my \$$_=\"$val\";"
1.20      albertel  329:     }
                    330:   } @{$token->[3]};
1.8       albertel  331:   return $temp;
                    332: }
1.22      albertel  333: 
1.34      www       334: sub writeallows {
                    335:     my $thisurl='/res/'.&Apache::lonnet::declutter(shift);
                    336:     my $thisdir=$thisurl;
                    337:     $thisdir=~s/\/[^\/]+$//;
                    338:     my %httpref=();
                    339:     map {
                    340:        $httpref{'httpref.'.
                    341:  	        &Apache::lonnet::hreflocation($thisdir,$_)}=$thisurl;              } @extlinks;
                    342:     &Apache::lonnet::appenv(%httpref);
                    343: }
                    344: 
1.24      sakharuk  345: sub handler {
                    346:   my $request=shift;
1.44      albertel  347:   
1.31      sakharuk  348:   my $target='web';
1.43      albertel  349: #  $Apache::lonxml::debug=1;
1.25      sakharuk  350:   if ($ENV{'browser.mathml'}) {
1.27      albertel  351:     $request->content_type('text/xml');
                    352:   } else {
                    353:     $request->content_type('text/html');
1.25      sakharuk  354:   }
1.29      sakharuk  355: 
                    356: #  $request->print(<<ENDHEADER);
                    357: #<html>
                    358: #<head>
                    359: #<title>Just test</title>
                    360: #</head>
                    361: #<body bgcolor="#FFFFFF">
                    362: #ENDHEADER
                    363: #  &Apache::lonhomework::send_header($request);
1.27      albertel  364:   $request->send_http_header;
                    365: 
1.45      www       366:   return OK if $request->header_only;
1.27      albertel  367: 
                    368:   $request->print(&Apache::lontexconvert::header());
                    369: 
1.28      albertel  370:   $request->print('<body bgcolor="#FFFFFF">'."\n");
1.27      albertel  371: 
1.24      sakharuk  372:   my $file = "/home/httpd/html".$request->uri;
                    373:   my %mystyle;
                    374:   my $result = '';
                    375:   $result = Apache::lonxml::xmlparse($target, &Apache::lonnet::getfile($file),'',%mystyle);
                    376:   $request->print($result);
1.29      sakharuk  377: 
1.28      albertel  378:   $request->print('</body>');
                    379:   $request->print(&Apache::lontexconvert::footer());
1.34      www       380:   writeallows($request->uri);
1.45      www       381:   return OK;
1.24      sakharuk  382: }
                    383:  
1.22      albertel  384: $Apache::lonxml::debug=0;
                    385: sub debug {
                    386:   if ($Apache::lonxml::debug eq 1) {
                    387:     print "DEBUG:".$_[0]."<br>\n";
                    388:   }
                    389: }
                    390: sub error {
1.44      albertel  391: #  print "ERROR:".$_[0]."<br>\n";
1.22      albertel  392: }
                    393: sub warning {
                    394:   if ($Apache::lonxml::debug eq 1) {
                    395:     print "WARNING:".$_[0]."<br>\n";
                    396:   }
                    397: }
                    398: 
1.1       sakharuk  399: 1;
                    400: __END__
1.25      sakharuk  401: 
                    402: 
                    403: 
                    404: 
                    405: 
                    406: 
                    407: 
                    408: 
                    409: 
                    410: 
                    411: 
                    412: 
                    413: 
1.11      sakharuk  414: 
                    415: 
                    416: 
                    417: 
                    418: 

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