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