File:  [LON-CAPA] / rat / lonratsrv.pm
Revision 1.10: download - view: text, annotated - select for diffs
Thu May 3 13:18:49 2001 UTC (23 years ago) by www
Branches: MAIN
CVS tags: HEAD
Clean up the onload stuff

    1: # The LearningOnline Network with CAPA
    2: # Server for RAT Maps
    3: #
    4: # (Edit Handler for RAT Maps
    5: # (TeX Content Handler
    6: #
    7: # 05/29/00,05/30 Gerd Kortemeyer)
    8: # 7/1 Gerd Kortemeyer)
    9: # 7/1,7/3,7/4,7/7,7/8,7/10,7/26,10/2 Gerd Kortemeyer
   10: # 4/30/2001 Scott Harrison
   11: # 5/3 Gerd Kortemeyer
   12: 
   13: package Apache::lonratsrv;
   14: 
   15: use strict;
   16: use Apache::Constants qw(:common);
   17: use Apache::File;
   18: use HTML::TokeParser;
   19: 
   20: 
   21: # ------------------------------------------------------------- From RAT to XML
   22: 
   23: sub qtescape {
   24:     my $str=shift;
   25:     $str=~s/\&\#58\;/\:/g;
   26:     $str=~s/\&\#39\;/\'/g;
   27:     $str=~s/\&\#44\;/\,/g;
   28:     $str=~s/\"/\&\#34\;/g;
   29:     return $str;
   30: }
   31: 
   32: # ------------------------------------------------------------- From XML to RAT
   33: 
   34: sub qtunescape {
   35:     my $str=shift;
   36:     $str=~s/\:/\&\#58\;/g;
   37:     $str=~s/\'/\&\#39\;/g;
   38:     $str=~s/\,/\&\#44\;/g;
   39:     $str=~s/\"/\&\#34\;/g;
   40:     return $str;
   41: }
   42: 
   43: # --------------------------------------------------------- Loads map from disk
   44: 
   45: sub loadmap {
   46:     my ($fn,$errtext)=@_;
   47:     my $outstr='';
   48:     my @content=();
   49:     my @obj=();
   50:     my @links=();
   51:     if (-e $fn) {
   52:         {
   53: 	    my $fh=Apache::File->new($fn);
   54:             @content=<$fh>;
   55:         }
   56:         my $instr=join('',@content);
   57:         my $parser = HTML::TokeParser->new(\$instr);
   58:         my $token;
   59:         my $graphmode=0;
   60: 
   61:         $fn=~/\.(\w+)$/;
   62:         $outstr="mode<:>$1";
   63: 
   64:         while ($token = $parser->get_token) {
   65: 	    if ($token->[0] eq 'S') {
   66:                 if ($token->[1] eq 'map') {
   67: 		    $graphmode=($token->[2]->{'mode'} eq 'rat/graphical');
   68:                 } elsif ($token->[1] eq 'resource') {
   69: # -------------------------------------------------------------------- Resource
   70:                     $outstr.='<&>objcont';
   71:                     if ($token->[2]->{'id'}) {
   72: 			$outstr.='<:>'.$token->[2]->{'id'};
   73:                         if ($obj[$token->[2]->{'id'}]==1) {
   74:                            $errtext.='Error: multiple use of ID '.
   75:                                      $token->[2]->{'id'}.'. ';
   76:                         }
   77:                         $obj[$token->[2]->{'id'}]=1; 
   78:                     } else {
   79:                         my $i=1;
   80:                         while (($i<=$#obj) && ($obj[$i]!=0)) { $i++; }
   81:                         $outstr.='<:>'.$i;
   82:                         $obj[$i]=1;
   83:                     }
   84:                     $outstr.='<:>';
   85:                     $outstr.=qtunescape($token->[2]->{'title'}).":";
   86:                     $outstr.=qtunescape($token->[2]->{'src'}).":";
   87:                     if ($token->[2]->{'src'}=~/\/\//) {
   88:                         $outstr.='true:';
   89:                     } else {
   90:                         $outstr.='false:';
   91:                     }
   92:                     if ($token->[2]->{'type'}) {
   93: 			$outstr.=$token->[2]->{'type'}.':';
   94:                     }  else {
   95:                         $outstr.='normal:';
   96:                     }
   97:                     $outstr.='res';
   98:                 } elsif ($token->[1] eq 'condition') {
   99: # ------------------------------------------------------------------- Condition
  100:                     $outstr.='<&>objcont';
  101:                     if ($token->[2]->{'id'}) {
  102: 			$outstr.='<:>'.$token->[2]->{'id'};
  103:                         if ($obj[$token->[2]->{'id'}]==1) {
  104:                            $errtext.='Error: multiple use of ID '.
  105:                                      $token->[2]->{'id'}.'. ';
  106:                         }
  107:                         $obj[$token->[2]->{'id'}]=1; 
  108:                     } else {
  109:                         my $i=1;
  110:                         while (($i<=$#obj) && ($obj[$i]!=0)) { $i++; }
  111:                         $outstr.='<:>'.$i;
  112:                         $obj[$i]=1;
  113:                     }
  114:                     $outstr.='<:>';
  115:                     $outstr.=qtunescape($token->[2]->{'value'}).':';
  116:                     if ($token->[2]->{'type'}) {
  117: 			$outstr.=$token->[2]->{'type'}.':';
  118:                     } else {
  119:                         $outstr.='normal:';
  120:                     }
  121:                     $outstr.='cond';
  122:                 } elsif ($token->[1] eq 'link') {
  123: # ----------------------------------------------------------------------- Links
  124:                     $outstr.='<&>objlinks';
  125: 
  126:                         if ($token->[2]->{'index'}) {
  127: 			   if ($links[$token->[2]->{'index'}]) {
  128:                                $errtext.='Error: multiple use of link index '.
  129: 			       $token->[2]->{'index'}.'. ';
  130:                            }
  131: 			   $outstr.='<:>'.$token->[2]->{'index'};
  132:                            $links[$token->[2]->{'index'}]=1;
  133:                         } else {
  134:                            my $i=1;
  135:                            while (($i<=$#links) && ($links[$i]==1)) { $i++; }
  136:                            $outstr.='<:>'.$i;
  137:                            $links[$i]=1;
  138: 		       }
  139: 		    
  140:                     $outstr.='<:>'.$token->[2]->{'from'}.
  141:                              ':'.$token->[2]->{'to'};
  142:                     if ($token->[2]->{'condition'}) {
  143: 			$outstr.=':'.$token->[2]->{'condition'};
  144:                     } else {
  145:  			$outstr.=':0';
  146:                     }
  147:                 } elsif ($graphmode) {
  148: # --------------------------------------------- All other tags (graphical only)
  149:                     $outstr.='<&>'.$token->[1];
  150:                     if (defined($token->[2]->{'index'})) {
  151: 			$outstr.='<:>'.$token->[2]->{'index'};
  152:                         if ($token->[1] eq 'obj') {
  153: 			    $obj[$token->[2]->{'index'}]=2;
  154:                         }
  155:                     }
  156:                     $outstr.='<:>'.$token->[2]->{'value'};
  157:                 }
  158:             }
  159:         }
  160: 
  161:     } else {
  162:         $errtext.='Map not loaded: The file does not exist. ';
  163:     }
  164:     return($outstr,$errtext);
  165: }
  166: 
  167: 
  168: # ----------------------------------------------------------- Saves map to disk
  169: 
  170: sub savemap {
  171:     my ($fn,$errtext)=@_;
  172:     if (($fn=~/\.sequence$/) ||
  173:         ($fn=~/\.page$/)) {
  174: 
  175: # ------------------------------------------------------------- Deal with input
  176:         my @tags=split(/<&>/,$ENV{'form.output'});
  177:         my $outstr='';
  178:         my $graphdef=0;
  179:         if ($tags[0] eq 'graphdef<:>yes') {
  180: 	    $outstr='<map mode="rat/graphical">'."\n";
  181:             $graphdef=1;
  182:         } else {
  183:             $outstr="<map>\n";
  184:         }
  185:         map {
  186: 	   my @parts=split(/<:>/,$_);
  187:            if ($parts[0] eq 'objcont') {
  188:                my @comp=split(/:/,$parts[$#parts]);
  189: # --------------------------------------------------------------- Logical input
  190: 	       if ($comp[$#comp] eq 'res') {
  191:                    $comp[0]=qtescape($comp[0]);
  192:                    $comp[1]=qtescape($comp[1]);
  193:                    if ($comp[2] eq 'true') {
  194: 		       if ($comp[1]!~/^http\:\/\//) {
  195: 			   $comp[1]='http://'.$comp[1];
  196:                        }
  197:                    } else {
  198: 		       if ($comp[1]=~/^http\:\/\//) {
  199: 			   $comp[1]=~s/^http\:\/\/[^\/]*\//\//;
  200:                        }
  201:                    }
  202: 		   $outstr.='<resource id="'.$parts[1].'" src="'
  203:                           .$comp[1].'"';
  204: 
  205:                    if (($comp[3] ne '') && ($comp[3] ne 'normal')) {
  206: 		       $outstr.=' type="'.$comp[3].'"';
  207:                    }
  208:                    if ($comp[0] ne '') {
  209: 		       $outstr.=' title="'.$comp[0].'"';
  210:                    }
  211:                    $outstr.="></resource>\n";
  212:                } elsif ($comp[$#comp] eq 'cond') {
  213:                    $outstr.='<condition id="'.$parts[1].'"';
  214:                    if (($comp[1] ne '') && ($comp[1] ne 'normal')) {
  215: 		       $outstr.=' type="'.$comp[1].'"';
  216:                    }
  217:                    $outstr.=' value="'.qtescape($comp[0]).'"';
  218:                    $outstr.="></condition>\n";
  219:                }
  220:            } elsif ($parts[0] eq 'objlinks') {
  221:                my @comp=split(/:/,$parts[$#parts]);
  222:                $outstr.='<link';
  223:                $outstr.=' from="'.$comp[0].'"';
  224:                $outstr.=' to="'.$comp[1].'"';
  225:                if (($comp[2] ne '') && ($comp[2]!=0)) {
  226:                   $outstr.=' condition="'.$comp[2].'"';
  227:                }
  228:                $outstr.=' index="'.$parts[1].'"';
  229:                $outstr.="></link>\n";
  230:            } elsif (($parts[0] ne '') && ($graphdef)) {
  231: # ------------------------------------------------------------- Graphical input
  232:                $outstr.='<'.$parts[0];
  233:                if ($#parts==2) {
  234: 		   $outstr.=' index="'.$parts[1].'"';
  235:                }
  236:                $outstr.=' value="'.qtescape($parts[$#parts]).'"></'.
  237:                         $parts[0].">\n";
  238:            }
  239:         } @tags;
  240:         $outstr.="</map>\n";
  241:         {
  242:           my $fh;
  243:           if ($fh=Apache::File->new(">$fn")) {
  244:              print $fh $outstr;
  245:              $errtext.="Map saved as $fn. ";
  246: 	  } else {
  247:              $errtext.='Could not write file $fn. Map not saved. ';
  248: 	  }
  249:         }
  250:     } else {
  251: # -------------------------------------------- Cannot write to that file, error
  252:         $errtext.='Map not saved: The specified path does not exist. ';
  253:     }
  254:     return $errtext;
  255: }
  256: 
  257: # ================================================================ Main Handler
  258: 
  259: sub handler {
  260:   my $r=shift;
  261:   $r->content_type('text/html');
  262:   $r->send_http_header;
  263: 
  264:   return OK if $r->header_only;
  265: 
  266:   my $url=$r->uri;
  267:   $url=~/\/(\w+)\/ratserver$/;
  268:   my $mode=$1;
  269: 
  270:   $url=~s/\/loadonly\/ratserver$/\/save\/ratserver/;
  271:   
  272:   my $fn=$r->filename;
  273:   my $errtext='';
  274:   my $outtext='';
  275: 
  276:   if ($mode ne 'loadonly') {
  277:      $errtext=&savemap($fn,$errtext);
  278:   }
  279:   ($outtext,$errtext)=&loadmap($fn,$errtext);
  280: 
  281:   $r->print(<<ENDDOCUMENT);
  282: <html>
  283: <body bgcolor="#FFFFFF">
  284: <form name=storage method=post action="$url">
  285: <input type=hidden name=output value="$outtext">
  286: </form>
  287: <script>
  288:     parent.flag=1;
  289: </script>
  290: ENDDOCUMENT
  291:     if ($errtext ne '') {
  292: 	$r->print(<<ENDSCRIPT);
  293: <script>
  294:     alert("$errtext");
  295: </script>
  296: ENDSCRIPT
  297:     }
  298:     $r->print("</body>\n</html>\n");
  299: 
  300:   return OK;
  301: }
  302: 
  303: 1;
  304: __END__
  305: 
  306: 
  307: 
  308: 
  309: 
  310: 
  311: 

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