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

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

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