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