Annotation of rat/lonratsrv.pm, revision 1.11
1.1 www 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)
1.7 www 9: # 7/1,7/3,7/4,7/7,7/8,7/10,7/26,10/2 Gerd Kortemeyer
1.8 harris41 10: # 4/30/2001 Scott Harrison
1.11 ! www 11: # 5/3,06/25 Gerd Kortemeyer
1.1 www 12:
13: package Apache::lonratsrv;
14:
15: use strict;
16: use Apache::Constants qw(:common);
1.2 www 17: use Apache::File;
18: use HTML::TokeParser;
19:
20:
1.4 www 21: # ------------------------------------------------------------- From RAT to XML
1.2 www 22:
23: sub qtescape {
24: my $str=shift;
1.4 www 25: $str=~s/\&\#58\;/\:/g;
26: $str=~s/\&\#39\;/\'/g;
27: $str=~s/\&\#44\;/\,/g;
28: $str=~s/\"/\&\#34\;/g;
1.2 www 29: return $str;
30: }
31:
1.4 www 32: # ------------------------------------------------------------- From XML to RAT
1.2 www 33:
1.4 www 34: sub qtunescape {
1.2 www 35: my $str=shift;
1.4 www 36: $str=~s/\:/\&\#58\;/g;
37: $str=~s/\'/\&\#39\;/g;
38: $str=~s/\,/\&\#44\;/g;
39: $str=~s/\"/\&\#34\;/g;
1.2 www 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') {
1.3 www 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.='<:>';
1.4 www 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';
1.2 www 98: } elsif ($token->[1] eq 'condition') {
1.3 www 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.='<:>';
1.4 www 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';
1.2 www 122: } elsif ($token->[1] eq 'link') {
1.3 www 123: # ----------------------------------------------------------------------- Links
1.2 www 124: $outstr.='<&>objlinks';
1.7 www 125:
1.3 www 126: if ($token->[2]->{'index'}) {
1.4 www 127: if ($links[$token->[2]->{'index'}]) {
128: $errtext.='Error: multiple use of link index '.
1.3 www 129: $token->[2]->{'index'}.'. ';
1.4 www 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: }
1.7 www 139:
1.2 www 140: $outstr.='<:>'.$token->[2]->{'from'}.
1.5 www 141: ':'.$token->[2]->{'to'};
1.2 www 142: if ($token->[2]->{'condition'}) {
1.5 www 143: $outstr.=':'.$token->[2]->{'condition'};
1.2 www 144: } else {
1.5 www 145: $outstr.=':0';
1.4 www 146: }
1.11 ! www 147: # ------------------------------------------------------------------- Parameter
! 148: } elsif ($token->[1] eq 'param') {
! 149: $outstr.='<&>objparms<:>'.$token->[2]->{'to'}.'<:>'.
! 150: $token->[2]->{'type'}.'___'.$token->[2]->{'name'}
! 151: .'___'.$token->[2]->{'value'};
1.2 www 152: } elsif ($graphmode) {
1.3 www 153: # --------------------------------------------- All other tags (graphical only)
154: $outstr.='<&>'.$token->[1];
1.4 www 155: if (defined($token->[2]->{'index'})) {
1.3 www 156: $outstr.='<:>'.$token->[2]->{'index'};
157: if ($token->[1] eq 'obj') {
158: $obj[$token->[2]->{'index'}]=2;
159: }
160: }
161: $outstr.='<:>'.$token->[2]->{'value'};
1.2 www 162: }
163: }
164: }
165:
166: } else {
1.3 www 167: $errtext.='Map not loaded: The file does not exist. ';
1.2 www 168: }
169: return($outstr,$errtext);
170: }
171:
172:
173: # ----------------------------------------------------------- Saves map to disk
174:
175: sub savemap {
176: my ($fn,$errtext)=@_;
1.7 www 177: if (($fn=~/\.sequence$/) ||
1.2 www 178: ($fn=~/\.page$/)) {
1.4 www 179:
1.2 www 180: # ------------------------------------------------------------- Deal with input
181: my @tags=split(/<&>/,$ENV{'form.output'});
182: my $outstr='';
183: my $graphdef=0;
184: if ($tags[0] eq 'graphdef<:>yes') {
185: $outstr='<map mode="rat/graphical">'."\n";
186: $graphdef=1;
187: } else {
188: $outstr="<map>\n";
189: }
190: map {
191: my @parts=split(/<:>/,$_);
192: if ($parts[0] eq 'objcont') {
193: my @comp=split(/:/,$parts[$#parts]);
194: # --------------------------------------------------------------- Logical input
195: if ($comp[$#comp] eq 'res') {
1.4 www 196: $comp[0]=qtescape($comp[0]);
197: $comp[1]=qtescape($comp[1]);
1.2 www 198: if ($comp[2] eq 'true') {
199: if ($comp[1]!~/^http\:\/\//) {
200: $comp[1]='http://'.$comp[1];
201: }
202: } else {
203: if ($comp[1]=~/^http\:\/\//) {
204: $comp[1]=~s/^http\:\/\/[^\/]*\//\//;
205: }
206: }
207: $outstr.='<resource id="'.$parts[1].'" src="'
1.4 www 208: .$comp[1].'"';
1.2 www 209:
210: if (($comp[3] ne '') && ($comp[3] ne 'normal')) {
211: $outstr.=' type="'.$comp[3].'"';
212: }
213: if ($comp[0] ne '') {
1.4 www 214: $outstr.=' title="'.$comp[0].'"';
1.2 www 215: }
216: $outstr.="></resource>\n";
217: } elsif ($comp[$#comp] eq 'cond') {
218: $outstr.='<condition id="'.$parts[1].'"';
219: if (($comp[1] ne '') && ($comp[1] ne 'normal')) {
220: $outstr.=' type="'.$comp[1].'"';
221: }
222: $outstr.=' value="'.qtescape($comp[0]).'"';
223: $outstr.="></condition>\n";
224: }
225: } elsif ($parts[0] eq 'objlinks') {
226: my @comp=split(/:/,$parts[$#parts]);
227: $outstr.='<link';
228: $outstr.=' from="'.$comp[0].'"';
229: $outstr.=' to="'.$comp[1].'"';
230: if (($comp[2] ne '') && ($comp[2]!=0)) {
231: $outstr.=' condition="'.$comp[2].'"';
232: }
233: $outstr.=' index="'.$parts[1].'"';
234: $outstr.="></link>\n";
1.11 ! www 235: } elsif ($parts[0] eq 'objparms') {
! 236: map {
! 237: my ($type,$name,$value)=split(/\_\_\_/,$_);
! 238: $outstr.='<param to="'.$parts[1].'" type="'.$type.'"'
! 239: .' name="'.$name.'" value="'.$value.'">'."\n";
! 240: } split(/:/,$parts[$#parts]);
1.2 www 241: } elsif (($parts[0] ne '') && ($graphdef)) {
242: # ------------------------------------------------------------- Graphical input
243: $outstr.='<'.$parts[0];
244: if ($#parts==2) {
245: $outstr.=' index="'.$parts[1].'"';
246: }
247: $outstr.=' value="'.qtescape($parts[$#parts]).'"></'.
248: $parts[0].">\n";
249: }
250: } @tags;
251: $outstr.="</map>\n";
252: {
253: my $fh;
254: if ($fh=Apache::File->new(">$fn")) {
255: print $fh $outstr;
1.3 www 256: $errtext.="Map saved as $fn. ";
1.2 www 257: } else {
1.3 www 258: $errtext.='Could not write file $fn. Map not saved. ';
1.2 www 259: }
260: }
261: } else {
262: # -------------------------------------------- Cannot write to that file, error
1.3 www 263: $errtext.='Map not saved: The specified path does not exist. ';
1.2 www 264: }
265: return $errtext;
266: }
1.1 www 267:
268: # ================================================================ Main Handler
269:
270: sub handler {
271: my $r=shift;
272: $r->content_type('text/html');
273: $r->send_http_header;
274:
275: return OK if $r->header_only;
276:
277: my $url=$r->uri;
1.2 www 278: $url=~/\/(\w+)\/ratserver$/;
279: my $mode=$1;
280:
281: $url=~s/\/loadonly\/ratserver$/\/save\/ratserver/;
282:
283: my $fn=$r->filename;
284: my $errtext='';
285: my $outtext='';
286:
287: if ($mode ne 'loadonly') {
288: $errtext=&savemap($fn,$errtext);
289: }
290: ($outtext,$errtext)=&loadmap($fn,$errtext);
1.1 www 291:
292: $r->print(<<ENDDOCUMENT);
293: <html>
1.8 harris41 294: <body bgcolor="#FFFFFF">
1.2 www 295: <form name=storage method=post action="$url">
296: <input type=hidden name=output value="$outtext">
1.1 www 297: </form>
1.8 harris41 298: <script>
1.9 harris41 299: parent.flag=1;
1.8 harris41 300: </script>
1.2 www 301: ENDDOCUMENT
302: if ($errtext ne '') {
303: $r->print(<<ENDSCRIPT);
304: <script>
305: alert("$errtext");
306: </script>
307: ENDSCRIPT
308: }
309: $r->print("</body>\n</html>\n");
1.1 www 310:
311: return OK;
312: }
313:
314: 1;
315: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>