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

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

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