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