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

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

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