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