Annotation of loncom/xml/lonxml.pm, revision 1.25
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.';';
53: &Apache::run::run($safeinit,$safeeval);
1.3 sakharuk 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 = ();
1.17 albertel 60: &initdepth;
1.3 sakharuk 61: my $token;
1.16 albertel 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));
1.19 albertel 72: &increasedepth($token);
1.16 albertel 73: if (exists $style_for_target{$token->[1]}) {
1.24 sakharuk 74:
1.25 ! sakharuk 75: if ($Apache::lonxml::redirection == 1) {
1.24 sakharuk 76: $finaloutput .= &recurse($style_for_target{$token->[1]},
77: $target,$safeeval,\%style_for_target,
78: @parstack);
79: } else {
1.25 ! sakharuk 80: $Apache::lonxml::outputstack .= &recurse($style_for_target{$token->[1]},
1.24 sakharuk 81: $target,$safeeval,\%style_for_target,
82: @parstack);
83: }
84:
1.16 albertel 85: } else {
1.17 albertel 86: $result = &callsub("start_$token->[1]", $target, $token,\@parstack,
1.16 albertel 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]
1.19 albertel 92: && ($#stack > -1)) {pop @stack;pop @parstack;&decreasedepth($token);}
1.16 albertel 93:
94: if (exists $style_for_target{'/'."$token->[1]"}) {
1.24 sakharuk 95:
1.25 ! sakharuk 96: if ($Apache::lonxml::redirection == 1) {
1.16 albertel 97: $finaloutput .= &recurse($style_for_target{'/'."$token->[1]"},
98: $target,$safeeval,\%style_for_target,
99: @parstack);
1.24 sakharuk 100: } else {
1.25 ! sakharuk 101: $Apache::lonxml::outputstack .= &recurse($style_for_target{'/'."$token->[1]"},
1.24 sakharuk 102: $target,$safeeval,\%style_for_target,
103: @parstack);
104: }
105:
1.16 albertel 106: } else {
1.17 albertel 107: $result = &callsub("end_$token->[1]", $target, $token, \@parstack,
1.16 albertel 108: \@pars,$safeeval, \%style_for_target);
1.13 albertel 109: }
1.16 albertel 110: }
1.25 ! sakharuk 111: if ($result ne "") {
1.24 sakharuk 112: if ( $#parstack > -1 ) {
113:
1.25 ! sakharuk 114: if ($Apache::lonxml::redirection == 1) {
1.13 albertel 115: $finaloutput .= &Apache::run::evaluate($result,$safeeval,
116: $parstack[$#parstack]);
1.24 sakharuk 117: } else {
1.25 ! sakharuk 118: $Apache::lonxml::outputstack .= &Apache::run::evaluate($result,$safeeval,
1.24 sakharuk 119: $parstack[$#parstack]);
120: }
121:
1.16 albertel 122: } else {
123: $finaloutput .= &Apache::run::evaluate($result,$safeeval,'');
1.13 albertel 124: }
1.16 albertel 125: $result = '';
1.25 ! sakharuk 126: } else {
! 127: $finaloutput .= $result;
1.2 sakharuk 128: }
1.19 albertel 129: if ($token->[0] eq 'E') { pop @stack;pop @parstack;&decreasedepth($token);}
1.5 albertel 130: }
1.16 albertel 131: pop @pars;
1.23 albertel 132: pop @Apache::lonxml::pwd;
1.3 sakharuk 133: }
1.24 sakharuk 134:
1.3 sakharuk 135: return $finaloutput;
1.15 albertel 136: }
137:
138: sub recurse {
139:
140: my @innerstack = ();
141: my @innerparstack = ();
142: my ($newarg,$target,$safeeval,$style_for_target,@parstack) = @_;
1.16 albertel 143: my @pat = ();
1.23 albertel 144: &newparser(\@pat,\$newarg);
1.15 albertel 145: my $tokenpat;
146: my $partstring = '';
147: my $output='';
1.16 albertel 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));
1.19 albertel 156: &increasedepth($tokenpat);
1.16 albertel 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]
1.17 albertel 163: && ($#innerstack > -1)) {pop @innerstack;pop @innerparstack;
1.19 albertel 164: &decreasedepth($tokenpat);}
1.16 albertel 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: }
1.17 albertel 188: if ($tokenpat->[0] eq 'E') { pop @innerstack;pop @innerparstack;
1.19 albertel 189: &decreasedepth($tokenpat);}
1.15 albertel 190: }
1.16 albertel 191: pop @pat;
1.23 albertel 192: pop @Apache::lonxml::pwd;
1.15 albertel 193: }
194: return $output;
1.7 albertel 195: }
196:
197: sub callsub {
1.14 albertel 198: my ($sub,$target,$token,$parstack,$parser,$safeeval,$style)=@_;
1.7 albertel 199: my $currentstring='';
200: {
1.24 sakharuk 201: my $sub1;
1.7 albertel 202: no strict 'refs';
203: if (my $space=$Apache::lonxml::alltags{$token->[1]}) {
1.24 sakharuk 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";
1.17 albertel 209: $Apache::lonxml::curdepth=join('_',@Apache::lonxml::depthcounter);
1.24 sakharuk 210: $currentstring = &$sub1($target,$token,$parstack,$parser,
1.16 albertel 211: $safeeval,$style);
1.24 sakharuk 212: # if ( $sub eq "start_parserlib" ) {
213: # print "me2:".%$style.":";
214: # }
1.7 albertel 215: } else {
1.23 albertel 216: #&Apache::lonxml::debug("NOT Calling sub $sub in $space<br>\n");
1.7 albertel 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;
1.17 albertel 226: }
227:
228: sub initdepth {
229: @Apache::lonxml::depthcounter=();
230: $Apache::lonxml::depth=-1;
231: $Apache::lonxml::olddepth=-1;
232: }
233:
234: sub increasedepth {
1.19 albertel 235: my ($token) = @_;
1.17 albertel 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++;
1.19 albertel 241: # print "<br>s $Apache::lonxml::depth : $Apache::lonxml::olddepth : $token->[1]<br>\n";
1.17 albertel 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 {
1.19 albertel 249: my ($token) = @_;
1.17 albertel 250: $Apache::lonxml::depth--;
1.19 albertel 251: # print "<br>e $Apache::lonxml::depth : $Apache::lonxml::olddepth : $token->[1]<br>\n";
1.1 sakharuk 252: }
1.19 albertel 253:
254: sub get_all_text {
255:
256: my($tag,$pars)= @_;
257: my $depth=0;
258: my $token;
259: my $result='';
1.24 sakharuk 260: my $tag=substr($tag,1); #strip the / off the tag
261: # &Apache::lonxml::debug("have:$tag:");
1.19 albertel 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') {
1.24 sakharuk 269: if ( $token->[1] eq $tag) { $depth--; }
1.19 albertel 270: #skip sending back the last end tag
271: if ($depth > -1) { $result.=$token->[2]; }
272: }
273: }
274: return $result
275: }
276:
1.23 albertel 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: }
1.1 sakharuk 288:
1.8 albertel 289: sub parstring {
290: my ($token) = @_;
291: my $temp='';
1.20 albertel 292: map {
293: if ($_=~/\w+/) {
294: $temp .= "my \$$_=\"$token->[2]->{$_}\";"
295: }
296: } @{$token->[3]};
1.8 albertel 297: return $temp;
298: }
1.24 sakharuk 299: #<<<<<<< lonxml.pm
1.22 albertel 300:
1.24 sakharuk 301: sub handler {
302: my $request=shift;
303:
304: my $target='web';
305: $Apache::lonxml::debug=1;
306: $request->content_type('text/html');
1.25 ! sakharuk 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
1.24 sakharuk 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:
1.22 albertel 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 {
1.23 albertel 349: print "ERROR:".$_[0]."<br>\n";
1.22 albertel 350: }
351: sub warning {
352: if ($Apache::lonxml::debug eq 1) {
353: print "WARNING:".$_[0]."<br>\n";
354: }
355: }
356:
1.1 sakharuk 357: 1;
358: __END__
1.25 ! sakharuk 359:
! 360:
! 361:
! 362:
! 363:
! 364:
! 365:
! 366:
! 367:
! 368:
! 369:
! 370:
! 371:
1.11 sakharuk 372:
373:
374:
375:
376:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>