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

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.55    ! albertel    7: # 2/21 Guy
1.2       sakharuk    8: 
1.4       albertel    9: package Apache::lonxml; 
1.33      www        10: use vars 
1.55    ! albertel   11: qw(@pwd @outputstack $redirection $textredirection $import @extlinks);
1.1       sakharuk   12: use strict;
                     13: use HTML::TokeParser;
1.3       sakharuk   14: use Safe;
1.40      albertel   15: use Safe::Hole;
1.13      albertel   16: use Opcode;
1.46      www        17: use Apache::Constants qw(:common);
1.7       albertel   18: 
                     19: sub register {
                     20:   my $space;
                     21:   my @taglist;
                     22:   my $temptag;
                     23:   ($space,@taglist) = @_;
                     24:   foreach $temptag (@taglist) {
                     25:     $Apache::lonxml::alltags{$temptag}=$space;
                     26:   }
                     27: }
1.48      albertel   28: 
                     29: sub printalltags {
                     30:   my $temp;
                     31:   foreach $temp (sort keys %Apache::lonxml::alltags) {
                     32:     &Apache::lonxml::debug("$temp -- $Apache::lonxml::alltags{$temp}");
                     33:   }
                     34: }
1.4       albertel   35: use Apache::style;
1.3       sakharuk   36: use Apache::lontexconvert;
1.7       albertel   37: use Apache::run;
1.4       albertel   38: use Apache::londefdef;
1.7       albertel   39: use Apache::scripttag;
1.3       sakharuk   40: #==================================================   Main subroutine: xmlparse  
1.33      www        41: @pwd=();
1.55    ! albertel   42: @outputstack = ();
        !            43: $redirection = 0;
        !            44: $import = 1;
1.33      www        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') {
1.55    ! albertel   51:    &startredirection;
        !            52:    $Apache::lonxml::import = 0;
1.47      albertel   53:  } elsif ($target eq 'grade') {
1.55    ! albertel   54:    &startredirection;
        !            55:    $Apache::lonxml::import = 1;
1.44      albertel   56:  } else {
1.55    ! albertel   57:    $Apache::lonxml::redirection = 0;
        !            58:    $Apache::lonxml::import = 1;
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.55    ! albertel   93:        $result=$token->[1];
1.16      albertel   94:      } elsif ($token->[0] eq 'S') {
                     95:        # add tag to stack 	    
                     96:        push (@stack,$token->[1]);
                     97:        # add parameters list to another stack
                     98:        push (@parstack,&parstring($token));
1.19      albertel   99:        &increasedepth($token);       
1.16      albertel  100:        if (exists $style_for_target{$token->[1]}) {
1.55    ! albertel  101: 	 if ($Apache::lonxml::redirection) {
        !           102: 	   $Apache::lonxml::outputstack['-1'] .=  
        !           103: 	     &recurse($style_for_target{$token->[1]},$target,$safeeval,
        !           104: 		      \%style_for_target,@parstack);
1.41      albertel  105: 	 } else {
1.55    ! albertel  106: 	   $finaloutput .= &recurse($style_for_target{$token->[1]},$target,
        !           107: 				    $safeeval,\%style_for_target,@parstack);
1.41      albertel  108: 	 }
1.16      albertel  109:        } else {
1.17      albertel  110: 	 $result = &callsub("start_$token->[1]", $target, $token,\@parstack,
1.41      albertel  111: 			    \@pars, $safeeval, \%style_for_target);
1.16      albertel  112:        }              
                    113:      } elsif ($token->[0] eq 'E')  {
                    114:        #clear out any tags that didn't end
1.55    ! albertel  115:        while ($token->[1] ne $stack[$#stack] && ($#stack > -1)) {
        !           116: 	 &Apache::lonxml::warning("Unbalanced tags in resource $stack['-1']");
1.43      albertel  117: 	 pop @stack;pop @parstack;&decreasedepth($token);
                    118:        }
1.16      albertel  119:        
                    120:        if (exists $style_for_target{'/'."$token->[1]"}) {
1.55    ! albertel  121: 	 if ($Apache::lonxml::redirection) {
        !           122: 	   $Apache::lonxml::outputstack['-1'] .=  
        !           123: 	     &recurse($style_for_target{'/'."$token->[1]"},
        !           124: 		      $target,$safeeval,\%style_for_target,@parstack);
        !           125: 	 } else {
        !           126: 	   $finaloutput .= &recurse($style_for_target{'/'."$token->[1]"},
        !           127: 				    $target,$safeeval,\%style_for_target,
        !           128: 				    @parstack);
        !           129: 	 }
        !           130: 	 
1.16      albertel  131:        } else {
1.17      albertel  132: 	 $result = &callsub("end_$token->[1]", $target, $token, \@parstack,
1.55    ! albertel  133: 			    \@pars,$safeeval, \%style_for_target);
1.13      albertel  134:        }
1.16      albertel  135:      }
1.55    ! albertel  136:      #evaluate variable refs in result
1.25      sakharuk  137:      if ($result ne "") {
1.24      sakharuk  138:        if ( $#parstack > -1 ) {
1.55    ! albertel  139: 	 if ($Apache::lonxml::redirection) {
        !           140: 	   $Apache::lonxml::outputstack['-1'] .= 
        !           141: 	     &Apache::run::evaluate($result,$safeeval,$parstack[$#parstack]);
        !           142: 	 } else {
        !           143: 	   $finaloutput .= &Apache::run::evaluate($result,$safeeval,
        !           144: 						  $parstack[$#parstack]);
        !           145: 	 }
1.16      albertel  146:        } else {
                    147: 	 $finaloutput .= &Apache::run::evaluate($result,$safeeval,'');
1.13      albertel  148:        }
1.16      albertel  149:        $result = '';
1.55    ! albertel  150:      } 
        !           151:      if ($token->[0] eq 'E') { 
        !           152:        pop @stack;pop @parstack;&decreasedepth($token);
1.2       sakharuk  153:      }
1.5       albertel  154:    }
1.16      albertel  155:    pop @pars;
1.23      albertel  156:    pop @Apache::lonxml::pwd;
1.3       sakharuk  157:  }
1.24      sakharuk  158: 
1.3       sakharuk  159:  return $finaloutput;
1.15      albertel  160: }
                    161: 
                    162: sub recurse {
                    163:   
                    164:   my @innerstack = (); 
                    165:   my @innerparstack = ();
                    166:   my ($newarg,$target,$safeeval,$style_for_target,@parstack) = @_;
1.16      albertel  167:   my @pat = ();
1.23      albertel  168:   &newparser(\@pat,\$newarg);
1.15      albertel  169:   my $tokenpat;
                    170:   my $partstring = '';
                    171:   my $output='';
1.16      albertel  172:   my $decls='';
                    173:   while ( $#pat > -1 ) {
                    174:     while  ($tokenpat = $pat[$#pat]->get_token) {
                    175:       if ($tokenpat->[0] eq 'T') {
1.55    ! albertel  176: 	$partstring = $tokenpat->[1];
1.16      albertel  177:       } elsif ($tokenpat->[0] eq 'S') {
                    178: 	push (@innerstack,$tokenpat->[1]);
                    179: 	push (@innerparstack,&parstring($tokenpat));
1.19      albertel  180: 	&increasedepth($tokenpat);
1.16      albertel  181: 	$partstring = &callsub("start_$tokenpat->[1]", 
                    182: 			       $target, $tokenpat, \@innerparstack,
                    183: 			       \@pat, $safeeval, $style_for_target);
                    184:       } elsif ($tokenpat->[0] eq 'E') {
                    185: 	#clear out any tags that didn't end
                    186: 	while ($tokenpat->[1] ne $innerstack[$#innerstack] 
1.43      albertel  187: 	       && ($#innerstack > -1)) {
1.49      albertel  188: 	  &Apache::lonxml::warning("Unbalanced tags in resource $innerstack['-1']");
1.43      albertel  189: 	  pop @innerstack;pop @innerparstack;&decreasedepth($tokenpat);
                    190: 	}
1.16      albertel  191: 	$partstring = &callsub("end_$tokenpat->[1]",
                    192: 			       $target, $tokenpat, \@innerparstack,
                    193: 			       \@pat, $safeeval, $style_for_target);
                    194:       }
                    195:       #pass both the variable to the style tag, and the tag we 
                    196:       #are processing inside the <definedtag>
                    197:       if ( $partstring ne "" ) {
                    198: 	if ( $#parstack > -1 ) { 
                    199: 	  if ( $#innerparstack > -1 ) { 
                    200: 	    $decls= $parstack[$#parstack].$innerparstack[$#innerparstack];
                    201: 	  } else {
                    202: 	    $decls= $parstack[$#parstack];
                    203: 	  }
                    204: 	} else {
                    205: 	  if ( $#innerparstack > -1 ) { 
                    206: 	    $decls=$innerparstack[$#innerparstack];
                    207: 	  } else {
                    208: 	    $decls='';
                    209: 	  }
                    210: 	}
                    211: 	$output .= &Apache::run::evaluate($partstring,$safeeval,$decls);
                    212: 	$partstring = '';
                    213:       }
1.17      albertel  214:       if ($tokenpat->[0] eq 'E') { pop @innerstack;pop @innerparstack;
1.19      albertel  215: 				 &decreasedepth($tokenpat);}
1.15      albertel  216:     }
1.16      albertel  217:     pop @pat;
1.23      albertel  218:     pop @Apache::lonxml::pwd;
1.15      albertel  219:   }
                    220:   return $output;
1.7       albertel  221: }
                    222: 
                    223: sub callsub {
1.14      albertel  224:   my ($sub,$target,$token,$parstack,$parser,$safeeval,$style)=@_;
1.7       albertel  225:   my $currentstring='';
                    226:   {
1.24      sakharuk  227:       my $sub1;
1.7       albertel  228:     no strict 'refs';
                    229:     if (my $space=$Apache::lonxml::alltags{$token->[1]}) {
1.54      albertel  230:       #&Apache::lonxml::debug("Calling sub $sub in $space<br />\n");
1.24      sakharuk  231:       $sub1="$space\:\:$sub";
1.17      albertel  232:       $Apache::lonxml::curdepth=join('_',@Apache::lonxml::depthcounter);
1.24      sakharuk  233:       $currentstring = &$sub1($target,$token,$parstack,$parser,
1.16      albertel  234: 			     $safeeval,$style);
1.7       albertel  235:     } else {
1.54      albertel  236:       #&Apache::lonxml::debug("NOT Calling sub $sub in $space<br />\n");
1.7       albertel  237:       if (defined($token->[4])) {
                    238: 	$currentstring = $token->[4];
                    239:       } else {
                    240: 	$currentstring = $token->[2];
                    241:       }
                    242:     }
                    243:     use strict 'refs';
                    244:   }
                    245:   return $currentstring;
1.17      albertel  246: }
                    247: 
1.55    ! albertel  248: sub startredirection {
        !           249:   $Apache::lonxml::redirection++;
        !           250:   push (@Apache::lonxml::outputstack, '');
        !           251: }
        !           252: 
        !           253: sub endredirection {
        !           254:   if (!$Apache::lonxml::redirection) {
        !           255:     &Apache::lonxml::error("Endredirection was called, before a startredirection, perhaps you have unbalanced tags. Some debuggin information:".join ":",caller);
        !           256:     return '';
        !           257:   }
        !           258:   $Apache::lonxml::redirection--;
        !           259:   pop @Apache::lonxml::outputstack;
        !           260: }
        !           261: 
1.17      albertel  262: sub initdepth {
                    263:   @Apache::lonxml::depthcounter=();
                    264:   $Apache::lonxml::depth=-1;
                    265:   $Apache::lonxml::olddepth=-1;
                    266: }
                    267: 
                    268: sub increasedepth {
1.19      albertel  269:   my ($token) = @_;
1.17      albertel  270:   $Apache::lonxml::depth++;
                    271:   $Apache::lonxml::depthcounter[$Apache::lonxml::depth]++;
                    272:   if ($Apache::lonxml::depthcounter[$Apache::lonxml::depth]==1) {
                    273:     $Apache::lonxml::olddepth=$Apache::lonxml::depth;
                    274:   }
1.42      albertel  275:   my $curdepth=join('_',@Apache::lonxml::depthcounter);
                    276:   &Apache::lonxml::debug("s $Apache::lonxml::depth : $Apache::lonxml::olddepth : $curdepth : $token->[1]\n");
1.54      albertel  277: #print "<br />s $Apache::lonxml::depth : $Apache::lonxml::olddepth : $curdepth : $token->[1]\n";
1.17      albertel  278: }
                    279: 
                    280: sub decreasedepth {
1.19      albertel  281:   my ($token) = @_;
1.17      albertel  282:   $Apache::lonxml::depth--;
1.36      albertel  283:   if ($Apache::lonxml::depth<$Apache::lonxml::olddepth-1) {
                    284:     $#Apache::lonxml::depthcounter--;
                    285:     $Apache::lonxml::olddepth=$Apache::lonxml::depth+1;
                    286:   }
1.43      albertel  287:   if (  $Apache::lonxml::depth < -1) {
1.49      albertel  288:     &Apache::lonxml::warning("Unbalanced tags in resource");   
1.43      albertel  289:     $Apache::lonxml::depth='-1';
                    290:   }
1.42      albertel  291:   my $curdepth=join('_',@Apache::lonxml::depthcounter);
                    292:   &Apache::lonxml::debug("e $Apache::lonxml::depth : $Apache::lonxml::olddepth : $token->[1] : $curdepth\n");
1.54      albertel  293: #print "<br />e $Apache::lonxml::depth : $Apache::lonxml::olddepth : $token->[1] : $curdepth\n";
1.1       sakharuk  294: }
1.19      albertel  295: 
                    296: sub get_all_text {
                    297: 
                    298:  my($tag,$pars)= @_;
                    299:  my $depth=0;
                    300:  my $token;
                    301:  my $result='';
1.24      sakharuk  302:  my $tag=substr($tag,1); #strip the / off the tag
1.49      albertel  303:  #&Apache::lonxml::debug("have:$tag:");
1.19      albertel  304:  while (($depth >=0) && ($token = $pars->get_token)) {
1.49      albertel  305:    #&Apache::lonxml::debug("token:$token->[0]:$depth:$token->[1]");
1.19      albertel  306:    if ($token->[0] eq 'T') {
                    307:      $result.=$token->[1];
                    308:    } elsif ($token->[0] eq 'S') {
                    309:      if ($token->[1] eq $tag) { $depth++; }
                    310:      $result.=$token->[4];
                    311:    } elsif ($token->[0] eq 'E')  {
1.24      sakharuk  312:      if ( $token->[1] eq $tag) { $depth--; }
1.19      albertel  313:      #skip sending back the last end tag
1.36      albertel  314:      if ($depth > -1) { $result.=$token->[2]; } else {
                    315:        $pars->unget_token($token);
                    316:      }
1.19      albertel  317:    }
                    318:  }
1.49      albertel  319: # &Apache::lonxml::debug("Exit:$result:");
1.19      albertel  320:  return $result
                    321: }
                    322: 
1.23      albertel  323: sub newparser {
                    324:   my ($parser,$contentref,$dir) = @_;
                    325:   push (@$parser,HTML::TokeParser->new($contentref));
                    326:   if ( $dir eq '' ) {
                    327:     push (@Apache::lonxml::pwd, $Apache::lonxml::pwd[$#Apache::lonxml::pwd]);
                    328:   } else {
                    329:     push (@Apache::lonxml::pwd, $dir);
                    330:   } 
                    331: #  &Apache::lonxml::debug("pwd:$#Apache::lonxml::pwd");
                    332: #  &Apache::lonxml::debug("pwd:$Apache::lonxml::pwd[$#Apache::lonxml::pwd]");
                    333: }
1.1       sakharuk  334: 
1.8       albertel  335: sub parstring {
                    336:   my ($token) = @_;
                    337:   my $temp='';
1.20      albertel  338:   map {
1.35      www       339:     unless ($_=~/\W/) {
1.42      albertel  340:       my $val=$token->[2]->{$_};
1.53      albertel  341:       $val =~ s/([\%\@\\])/\\$1/g;
1.51      albertel  342:       #if ($val =~ m/^[\%\@]/) { $val="\\".$val; }
1.42      albertel  343:       $temp .= "my \$$_=\"$val\";"
1.20      albertel  344:     }
                    345:   } @{$token->[3]};
1.8       albertel  346:   return $temp;
                    347: }
1.22      albertel  348: 
1.34      www       349: sub writeallows {
                    350:     my $thisurl='/res/'.&Apache::lonnet::declutter(shift);
                    351:     my $thisdir=$thisurl;
                    352:     $thisdir=~s/\/[^\/]+$//;
                    353:     my %httpref=();
                    354:     map {
                    355:        $httpref{'httpref.'.
                    356:  	        &Apache::lonnet::hreflocation($thisdir,$_)}=$thisurl;              } @extlinks;
                    357:     &Apache::lonnet::appenv(%httpref);
                    358: }
                    359: 
1.24      sakharuk  360: sub handler {
                    361:   my $request=shift;
1.44      albertel  362:   
1.31      sakharuk  363:   my $target='web';
1.48      albertel  364:   $Apache::lonxml::debug=0;
1.25      sakharuk  365:   if ($ENV{'browser.mathml'}) {
1.27      albertel  366:     $request->content_type('text/xml');
                    367:   } else {
                    368:     $request->content_type('text/html');
1.25      sakharuk  369:   }
1.29      sakharuk  370: 
                    371: #  $request->print(<<ENDHEADER);
                    372: #<html>
                    373: #<head>
                    374: #<title>Just test</title>
                    375: #</head>
                    376: #<body bgcolor="#FFFFFF">
                    377: #ENDHEADER
                    378: #  &Apache::lonhomework::send_header($request);
1.27      albertel  379:   $request->send_http_header;
                    380: 
1.45      www       381:   return OK if $request->header_only;
1.27      albertel  382: 
                    383:   $request->print(&Apache::lontexconvert::header());
                    384: 
1.28      albertel  385:   $request->print('<body bgcolor="#FFFFFF">'."\n");
1.27      albertel  386: 
1.50      albertel  387:   my $file=&Apache::lonnet::filelocation("",$request->uri);
1.24      sakharuk  388:   my %mystyle;
1.50      albertel  389:   my $result = ''; 
                    390:   my $filecontents=&Apache::lonnet::getfile($file);
                    391:   if ($filecontents == -1) {
                    392:     &Apache::lonxml::error("<b> Unable to find <i>$file</i></b>");
                    393:     $filecontents='';
                    394:   } else {
                    395:     $result = &Apache::lonxml::xmlparse($target,$filecontents,'',%mystyle);
                    396:   }
1.24      sakharuk  397:   $request->print($result);
1.29      sakharuk  398: 
1.50      albertel  399: 
1.28      albertel  400:   $request->print('</body>');
                    401:   $request->print(&Apache::lontexconvert::footer());
1.34      www       402:   writeallows($request->uri);
1.45      www       403:   return OK;
1.24      sakharuk  404: }
                    405:  
1.22      albertel  406: $Apache::lonxml::debug=0;
                    407: sub debug {
                    408:   if ($Apache::lonxml::debug eq 1) {
1.54      albertel  409:     print "DEBUG:".$_[0]."<br />\n";
1.22      albertel  410:   }
                    411: }
1.49      albertel  412: 
1.22      albertel  413: sub error {
1.52      albertel  414:   if ($Apache::lonxml::debug eq 1) {
1.55    ! albertel  415:     print "<b>ERROR:</b>".$_[0]."<br />\n";
1.52      albertel  416:   } else {
                    417:     print "<b>An Error occured while processing this resource. The instructor has been notified.</b> <br />";
                    418:     #notify author
                    419:     &Apache::lonmsg::author_res_msg($ENV{'request.filename'},$_[0]);
                    420:     #notify course
                    421:     if ( $ENV{'request.course.id'} ) {
                    422:       my $users=$ENV{'course.'.$ENV{'request.course.id'}.'.comment.email'};
                    423:       foreach my $user (split /\,/, $users) {
                    424: 	($user,my $domain) = split /:/, $user;
1.54      albertel  425: 	&Apache::lonmsg::user_normal_msg($user,$domain,"Error in $ENV{'request.filename'}",$_[0]);
1.52      albertel  426:       }
                    427:     }
                    428:     
                    429:     #FIXME probably shouldn't have me get everything forever.
1.54      albertel  430:     &Apache::lonmsg::user_normal_msg('albertel','msu',"Error in $ENV{'request.filename'}",$_[0]);
                    431:     #&Apache::lonmsg::user_normal_msg('albertel','103',"Error in $ENV{'request.filename'}",$_[0]);   
1.52      albertel  432:   }
1.22      albertel  433: }
1.49      albertel  434: 
1.22      albertel  435: sub warning {
                    436:   if ($Apache::lonxml::debug eq 1) {
1.55    ! albertel  437:     print "<b>W</b>ARNING<b>:</b>".$_[0]."<br />\n";
1.22      albertel  438:   }
                    439: }
                    440: 
1.1       sakharuk  441: 1;
                    442: __END__

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