1: # The LearningOnline Network with CAPA
2: # XML Parser Module
3: #
4: # last modified 06/26/00 by Alexander Sakharuk
5: # 11/6 Gerd Kortemeyer
6:
7: package Apache::lonxml;
8: use vars
9: qw(@pwd $outputstack $redirection $textredirection $on_offimport @extlinks);
10: use strict;
11: use HTML::TokeParser;
12: use Safe;
13: use Opcode;
14:
15: sub register {
16: my $space;
17: my @taglist;
18: my $temptag;
19: ($space,@taglist) = @_;
20: foreach $temptag (@taglist) {
21: $Apache::lonxml::alltags{$temptag}=$space;
22: }
23: }
24:
25: use Apache::style;
26: use Apache::lontexconvert;
27: use Apache::run;
28: use Apache::londefdef;
29: use Apache::scripttag;
30: #================================================== Main subroutine: xmlparse
31: @pwd=();
32: $outputstack = '';
33: $redirection = 1;
34: $textredirection = 1;
35: $on_offimport = 0;
36: @extlinks=();
37:
38: sub xmlparse {
39:
40: my ($target,$content_file_string,$safeinit,%style_for_target) = @_;
41: if ($target eq 'meta')
42: {$Apache::lonxml::textredirection = 0;
43: $Apache::lonxml::on_offimport = 1;
44: }
45: my @pars = ();
46: @Apache::lonxml::pwd=();
47: my $pwd=$ENV{'request.filename'};
48: $pwd =~ s:/[^/]*$::;
49: &newparser(\@pars,\$content_file_string,$pwd);
50: my $currentstring = '';
51: my $finaloutput = '';
52: my $newarg = '';
53: my $result;
54:
55: my $safeeval = new Safe;
56: $safeeval->permit("entereval");
57: $safeeval->permit(":base_math");
58: $safeeval->deny(":base_io");
59: #need to inspect this class of ops
60: # $safeeval->deny(":base_orig");
61: $safeinit .= ';$external::target='.$target.';';
62: $safeinit .= ';$external::randomseed='.&Apache::lonnet::rndseed().';';
63: $safeinit .= &Apache::lonnet::getfile('/home/httpd/html/res/adm/includes/default_xml.lcpm');
64: &Apache::run::run($safeinit,$safeeval);
65: #-------------------- Redefinition of the target in the case of compound target
66:
67: ($target, my @tenta) = split('&&',$target);
68:
69: my @stack = ();
70: my @parstack = ();
71: &initdepth;
72: my $token;
73: while ( $#pars > -1 ) {
74: while ($token = $pars[$#pars]->get_token) {
75: if ($token->[0] eq 'T') {
76: if ($Apache::lonxml::textredirection == 1) {$result=$token->[1];}
77: # $finaloutput .= &Apache::run::evaluate($token->[1],$safeeval,'');
78: } elsif ($token->[0] eq 'S') {
79: # if ($target eq 'meta' and $token->[2]->{metaout} eq 'ON') {$Apache::lonxml::textredirection = 1;}
80: # add tag to stack
81: push (@stack,$token->[1]);
82: # add parameters list to another stack
83: push (@parstack,&parstring($token));
84: &increasedepth($token);
85: if (exists $style_for_target{$token->[1]}) {
86:
87: if ($Apache::lonxml::redirection == 1) {
88: $finaloutput .= &recurse($style_for_target{$token->[1]},
89: $target,$safeeval,\%style_for_target,
90: @parstack);
91: } else {
92: $Apache::lonxml::outputstack .= &recurse($style_for_target{$token->[1]},
93: $target,$safeeval,\%style_for_target,
94: @parstack);
95: }
96:
97: } else {
98: $result = &callsub("start_$token->[1]", $target, $token,\@parstack,
99: \@pars, $safeeval, \%style_for_target);
100: }
101: } elsif ($token->[0] eq 'E') {
102: # if ($target eq 'meta') {$Apache::lonxml::textredirection = 0;}
103: #clear out any tags that didn't end
104: while ($token->[1] ne $stack[$#stack]
105: && ($#stack > -1)) {pop @stack;pop @parstack;&decreasedepth($token);}
106:
107: if (exists $style_for_target{'/'."$token->[1]"}) {
108:
109: if ($Apache::lonxml::redirection == 1) {
110: $finaloutput .= &recurse($style_for_target{'/'."$token->[1]"},
111: $target,$safeeval,\%style_for_target,
112: @parstack);
113: } else {
114: $Apache::lonxml::outputstack .= &recurse($style_for_target{'/'."$token->[1]"},
115: $target,$safeeval,\%style_for_target,
116: @parstack);
117: }
118:
119: } else {
120: $result = &callsub("end_$token->[1]", $target, $token, \@parstack,
121: \@pars,$safeeval, \%style_for_target);
122: }
123: }
124: if ($result ne "") {
125: if ( $#parstack > -1 ) {
126:
127: if ($Apache::lonxml::redirection == 1) {
128: $finaloutput .= &Apache::run::evaluate($result,$safeeval,
129: $parstack[$#parstack]);
130: } else {
131: $Apache::lonxml::outputstack .= &Apache::run::evaluate($result,$safeeval,
132: $parstack[$#parstack]);
133: }
134:
135: } else {
136: $finaloutput .= &Apache::run::evaluate($result,$safeeval,'');
137: }
138: $result = '';
139: } else {
140: $finaloutput .= $result;
141: }
142: if ($token->[0] eq 'E') { pop @stack;pop @parstack;&decreasedepth($token);}
143: }
144: pop @pars;
145: pop @Apache::lonxml::pwd;
146: }
147:
148: return $finaloutput;
149: }
150:
151: sub recurse {
152:
153: my @innerstack = ();
154: my @innerparstack = ();
155: my ($newarg,$target,$safeeval,$style_for_target,@parstack) = @_;
156: my @pat = ();
157: &newparser(\@pat,\$newarg);
158: my $tokenpat;
159: my $partstring = '';
160: my $output='';
161: my $decls='';
162: while ( $#pat > -1 ) {
163: while ($tokenpat = $pat[$#pat]->get_token) {
164: if ($tokenpat->[0] eq 'T') {
165: if ($Apache::lonxml::textredirection == 1) {$partstring = $tokenpat->[1];}
166: } elsif ($tokenpat->[0] eq 'S') {
167: push (@innerstack,$tokenpat->[1]);
168: push (@innerparstack,&parstring($tokenpat));
169: &increasedepth($tokenpat);
170: $partstring = &callsub("start_$tokenpat->[1]",
171: $target, $tokenpat, \@innerparstack,
172: \@pat, $safeeval, $style_for_target);
173: } elsif ($tokenpat->[0] eq 'E') {
174: #clear out any tags that didn't end
175: while ($tokenpat->[1] ne $innerstack[$#innerstack]
176: && ($#innerstack > -1)) {pop @innerstack;pop @innerparstack;
177: &decreasedepth($tokenpat);}
178: $partstring = &callsub("end_$tokenpat->[1]",
179: $target, $tokenpat, \@innerparstack,
180: \@pat, $safeeval, $style_for_target);
181: }
182: #pass both the variable to the style tag, and the tag we
183: #are processing inside the <definedtag>
184: if ( $partstring ne "" ) {
185: if ( $#parstack > -1 ) {
186: if ( $#innerparstack > -1 ) {
187: $decls= $parstack[$#parstack].$innerparstack[$#innerparstack];
188: } else {
189: $decls= $parstack[$#parstack];
190: }
191: } else {
192: if ( $#innerparstack > -1 ) {
193: $decls=$innerparstack[$#innerparstack];
194: } else {
195: $decls='';
196: }
197: }
198: $output .= &Apache::run::evaluate($partstring,$safeeval,$decls);
199: $partstring = '';
200: }
201: if ($tokenpat->[0] eq 'E') { pop @innerstack;pop @innerparstack;
202: &decreasedepth($tokenpat);}
203: }
204: pop @pat;
205: pop @Apache::lonxml::pwd;
206: }
207: return $output;
208: }
209:
210: sub callsub {
211: my ($sub,$target,$token,$parstack,$parser,$safeeval,$style)=@_;
212: my $currentstring='';
213: {
214: my $sub1;
215: no strict 'refs';
216: if (my $space=$Apache::lonxml::alltags{$token->[1]}) {
217: #&Apache::lonxml::debug("Calling sub $sub in $space<br>\n");
218: $sub1="$space\:\:$sub";
219: $Apache::lonxml::curdepth=join('_',@Apache::lonxml::depthcounter);
220: $currentstring = &$sub1($target,$token,$parstack,$parser,
221: $safeeval,$style);
222: } else {
223: #&Apache::lonxml::debug("NOT Calling sub $sub in $space<br>\n");
224: if (defined($token->[4])) {
225: $currentstring = $token->[4];
226: } else {
227: $currentstring = $token->[2];
228: }
229: }
230: use strict 'refs';
231: }
232: return $currentstring;
233: }
234:
235: sub initdepth {
236: @Apache::lonxml::depthcounter=();
237: $Apache::lonxml::depth=-1;
238: $Apache::lonxml::olddepth=-1;
239: }
240:
241: sub increasedepth {
242: my ($token) = @_;
243: $Apache::lonxml::depth++;
244: $Apache::lonxml::depthcounter[$Apache::lonxml::depth]++;
245: if ($Apache::lonxml::depthcounter[$Apache::lonxml::depth]==1) {
246: $Apache::lonxml::olddepth=$Apache::lonxml::depth;
247: }
248: # my $curdepth=join('_',@Apache::lonxml::depthcounter);
249: # print "<br>s $Apache::lonxml::depth : $Apache::lonxml::olddepth : $curdepth : $token->[1]<br>\n";
250: }
251:
252: sub decreasedepth {
253: my ($token) = @_;
254: $Apache::lonxml::depth--;
255: if ($Apache::lonxml::depth<$Apache::lonxml::olddepth-1) {
256: $#Apache::lonxml::depthcounter--;
257: $Apache::lonxml::olddepth=$Apache::lonxml::depth+1;
258: }
259: # my $curdepth=join('_',@Apache::lonxml::depthcounter);
260: # print "<br>e $Apache::lonxml::depth : $Apache::lonxml::olddepth : $token->[1] : $curdepth <br>\n";
261: }
262:
263: sub get_all_text {
264:
265: my($tag,$pars)= @_;
266: my $depth=0;
267: my $token;
268: my $result='';
269: my $tag=substr($tag,1); #strip the / off the tag
270: # &Apache::lonxml::debug("have:$tag:");
271: while (($depth >=0) && ($token = $pars->get_token)) {
272: if ($token->[0] eq 'T') {
273: $result.=$token->[1];
274: } elsif ($token->[0] eq 'S') {
275: if ($token->[1] eq $tag) { $depth++; }
276: $result.=$token->[4];
277: } elsif ($token->[0] eq 'E') {
278: if ( $token->[1] eq $tag) { $depth--; }
279: #skip sending back the last end tag
280: if ($depth > -1) { $result.=$token->[2]; } else {
281: $pars->unget_token($token);
282: }
283: }
284: }
285: return $result
286: }
287:
288: sub newparser {
289: my ($parser,$contentref,$dir) = @_;
290: push (@$parser,HTML::TokeParser->new($contentref));
291: if ( $dir eq '' ) {
292: push (@Apache::lonxml::pwd, $Apache::lonxml::pwd[$#Apache::lonxml::pwd]);
293: } else {
294: push (@Apache::lonxml::pwd, $dir);
295: }
296: # &Apache::lonxml::debug("pwd:$#Apache::lonxml::pwd");
297: # &Apache::lonxml::debug("pwd:$Apache::lonxml::pwd[$#Apache::lonxml::pwd]");
298: }
299:
300: sub parstring {
301: my ($token) = @_;
302: my $temp='';
303: map {
304: unless ($_=~/\W/) {
305: $temp .= "my \$$_=\"$token->[2]->{$_}\";"
306: }
307: } @{$token->[3]};
308: return $temp;
309: }
310:
311: sub writeallows {
312: my $thisurl='/res/'.&Apache::lonnet::declutter(shift);
313: my $thisdir=$thisurl;
314: $thisdir=~s/\/[^\/]+$//;
315: my %httpref=();
316: map {
317: $httpref{'httpref.'.
318: &Apache::lonnet::hreflocation($thisdir,$_)}=$thisurl; } @extlinks;
319: &Apache::lonnet::appenv(%httpref);
320: }
321:
322: sub handler {
323: my $request=shift;
324:
325: my $target='web';
326: $Apache::lonxml::debug=1;
327: if ($ENV{'browser.mathml'}) {
328: $request->content_type('text/xml');
329: } else {
330: $request->content_type('text/html');
331: }
332:
333: # $request->print(<<ENDHEADER);
334: #<html>
335: #<head>
336: #<title>Just test</title>
337: #</head>
338: #<body bgcolor="#FFFFFF">
339: #ENDHEADER
340: # &Apache::lonhomework::send_header($request);
341: $request->send_http_header;
342:
343: return 'OK' if $request->header_only;
344:
345: $request->print(&Apache::lontexconvert::header());
346:
347: $request->print('<body bgcolor="#FFFFFF">'."\n");
348:
349: my $file = "/home/httpd/html".$request->uri;
350: my %mystyle;
351: my $result = '';
352: $result = Apache::lonxml::xmlparse($target, &Apache::lonnet::getfile($file),'',%mystyle);
353: $request->print($result);
354:
355: $request->print('</body>');
356: $request->print(&Apache::lontexconvert::footer());
357: writeallows($request->uri);
358: return 'OK';
359: }
360:
361: $Apache::lonxml::debug=0;
362: sub debug {
363: if ($Apache::lonxml::debug eq 1) {
364: print "DEBUG:".$_[0]."<br>\n";
365: }
366: }
367: sub error {
368: print "ERROR:".$_[0]."<br>\n";
369: }
370: sub warning {
371: if ($Apache::lonxml::debug eq 1) {
372: print "WARNING:".$_[0]."<br>\n";
373: }
374: }
375:
376: 1;
377: __END__
378:
379:
380:
381:
382:
383:
384:
385:
386:
387:
388:
389:
390:
391:
392:
393:
394:
395:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>