File:
[LON-CAPA] /
rat /
lonratsrv.pm
Revision
1.8:
download - view:
text,
annotated -
select for diffs
Mon Apr 30 15:44:44 2001 UTC (23 years, 3 months ago) by
harris41
Branches:
MAIN
CVS tags:
HEAD
making sure the parent.code.srvloaded function is called
after definition of the <form name=server> element (and
not wait until the browser calls the onLoad method)
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:
12: package Apache::lonratsrv;
13:
14: use strict;
15: use Apache::Constants qw(:common);
16: use Apache::File;
17: use HTML::TokeParser;
18:
19:
20: # ------------------------------------------------------------- From RAT to XML
21:
22: sub qtescape {
23: my $str=shift;
24: $str=~s/\&\#58\;/\:/g;
25: $str=~s/\&\#39\;/\'/g;
26: $str=~s/\&\#44\;/\,/g;
27: $str=~s/\"/\&\#34\;/g;
28: return $str;
29: }
30:
31: # ------------------------------------------------------------- From XML to RAT
32:
33: sub qtunescape {
34: my $str=shift;
35: $str=~s/\:/\&\#58\;/g;
36: $str=~s/\'/\&\#39\;/g;
37: $str=~s/\,/\&\#44\;/g;
38: $str=~s/\"/\&\#34\;/g;
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') {
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.='<:>';
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';
97: } elsif ($token->[1] eq 'condition') {
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.='<:>';
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';
121: } elsif ($token->[1] eq 'link') {
122: # ----------------------------------------------------------------------- Links
123: $outstr.='<&>objlinks';
124:
125: if ($token->[2]->{'index'}) {
126: if ($links[$token->[2]->{'index'}]) {
127: $errtext.='Error: multiple use of link index '.
128: $token->[2]->{'index'}.'. ';
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: }
138:
139: $outstr.='<:>'.$token->[2]->{'from'}.
140: ':'.$token->[2]->{'to'};
141: if ($token->[2]->{'condition'}) {
142: $outstr.=':'.$token->[2]->{'condition'};
143: } else {
144: $outstr.=':0';
145: }
146: } elsif ($graphmode) {
147: # --------------------------------------------- All other tags (graphical only)
148: $outstr.='<&>'.$token->[1];
149: if (defined($token->[2]->{'index'})) {
150: $outstr.='<:>'.$token->[2]->{'index'};
151: if ($token->[1] eq 'obj') {
152: $obj[$token->[2]->{'index'}]=2;
153: }
154: }
155: $outstr.='<:>'.$token->[2]->{'value'};
156: }
157: }
158: }
159:
160: } else {
161: $errtext.='Map not loaded: The file does not exist. ';
162: }
163: return($outstr,$errtext);
164: }
165:
166:
167: # ----------------------------------------------------------- Saves map to disk
168:
169: sub savemap {
170: my ($fn,$errtext)=@_;
171: if (($fn=~/\.sequence$/) ||
172: ($fn=~/\.page$/)) {
173:
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') {
190: $comp[0]=qtescape($comp[0]);
191: $comp[1]=qtescape($comp[1]);
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="'
202: .$comp[1].'"';
203:
204: if (($comp[3] ne '') && ($comp[3] ne 'normal')) {
205: $outstr.=' type="'.$comp[3].'"';
206: }
207: if ($comp[0] ne '') {
208: $outstr.=' title="'.$comp[0].'"';
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;
244: $errtext.="Map saved as $fn. ";
245: } else {
246: $errtext.='Could not write file $fn. Map not saved. ';
247: }
248: }
249: } else {
250: # -------------------------------------------- Cannot write to that file, error
251: $errtext.='Map not saved: The specified path does not exist. ';
252: }
253: return $errtext;
254: }
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;
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='';
274: my $onload='';
275:
276: if ($mode ne 'loadonly') {
277: $errtext=&savemap($fn,$errtext);
278: # $onload='onLoad="parent.code.srvloaded();"';
279: }
280: ($outtext,$errtext)=&loadmap($fn,$errtext);
281:
282: $r->print(<<ENDDOCUMENT);
283: <html>
284: <body bgcolor="#FFFFFF">
285: <form name=storage method=post action="$url">
286: <input type=hidden name=output value="$outtext">
287: </form>
288: <script>
289: parent.code.srvloaded();
290: </script>
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");
300:
301: return OK;
302: }
303:
304: 1;
305: __END__
306:
307:
308:
309:
310:
311:
312:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>