Annotation of loncom/xml/lonxml.pm, revision 1.24
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.3 sakharuk 30: sub xmlparse {
31:
1.18 albertel 32: my ($target,$content_file_string,$safeinit,%style_for_target) = @_;
1.16 albertel 33: my @pars = ();
1.23 albertel 34: @Apache::lonxml::pwd=();
35: my $pwd=$ENV{'request.filename'};
36: $pwd =~ s:/[^/]*$::;
37: &newparser(\@pars,\$content_file_string,$pwd);
1.3 sakharuk 38: my $currentstring = '';
39: my $finaloutput = '';
40: my $newarg = '';
1.16 albertel 41: my $result;
1.24 ! sakharuk 42:
! 43: my $indicator = 1;
! 44: my $pocketbox = '';
! 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:
! 75: if ($indicator == 1) {
! 76: $finaloutput .= &recurse($style_for_target{$token->[1]},
! 77: $target,$safeeval,\%style_for_target,
! 78: @parstack);
! 79: } else {
! 80: $pocketbox .= &recurse($style_for_target{$token->[1]},
! 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);
1.24 ! sakharuk 88:
! 89: if ($result eq "footnote") { &Apache::lonxml::debug($result);}
! 90:
1.16 albertel 91: }
92: } elsif ($token->[0] eq 'E') {
93: #clear out any tags that didn't end
94: while ($token->[1] ne $stack[$#stack]
1.19 albertel 95: && ($#stack > -1)) {pop @stack;pop @parstack;&decreasedepth($token);}
1.16 albertel 96:
97: if (exists $style_for_target{'/'."$token->[1]"}) {
1.24 ! sakharuk 98:
! 99: if ($indicator == 1) {
1.16 albertel 100: $finaloutput .= &recurse($style_for_target{'/'."$token->[1]"},
101: $target,$safeeval,\%style_for_target,
102: @parstack);
1.24 ! sakharuk 103: } else {
! 104: $pocketbox .= &recurse($style_for_target{'/'."$token->[1]"},
! 105: $target,$safeeval,\%style_for_target,
! 106: @parstack);
! 107: }
! 108:
1.16 albertel 109: } else {
1.17 albertel 110: $result = &callsub("end_$token->[1]", $target, $token, \@parstack,
1.16 albertel 111: \@pars,$safeeval, \%style_for_target);
1.13 albertel 112: }
1.16 albertel 113: }
114: if ($result ne "" ) {
1.24 ! sakharuk 115: if ( $#parstack > -1 ) {
! 116:
! 117: if ($indicator == 1) {
1.13 albertel 118: $finaloutput .= &Apache::run::evaluate($result,$safeeval,
119: $parstack[$#parstack]);
1.24 ! sakharuk 120: } else {
! 121: $pocketbox .= &Apache::run::evaluate($result,$safeeval,
! 122: $parstack[$#parstack]);
! 123: }
! 124:
1.16 albertel 125: } else {
126: $finaloutput .= &Apache::run::evaluate($result,$safeeval,'');
1.13 albertel 127: }
1.16 albertel 128: $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.24 ! sakharuk 205: # &Apache::lonxml::debug("Calling sub $sub in $space<br>\n");
! 206: # if ( $sub eq "start_parserlib" ) {
! 207: # print "me:".%$style.":\n";
! 208: # }
! 209: $sub1="$space\:\:$sub";
1.17 albertel 210: $Apache::lonxml::curdepth=join('_',@Apache::lonxml::depthcounter);
1.24 ! sakharuk 211: $currentstring = &$sub1($target,$token,$parstack,$parser,
1.16 albertel 212: $safeeval,$style);
1.24 ! sakharuk 213: # if ( $sub eq "start_parserlib" ) {
! 214: # print "me2:".%$style.":";
! 215: # }
1.7 albertel 216: } else {
1.23 albertel 217: #&Apache::lonxml::debug("NOT Calling sub $sub in $space<br>\n");
1.7 albertel 218: if (defined($token->[4])) {
219: $currentstring = $token->[4];
220: } else {
221: $currentstring = $token->[2];
222: }
223: }
224: use strict 'refs';
225: }
226: return $currentstring;
1.17 albertel 227: }
228:
229: sub initdepth {
230: @Apache::lonxml::depthcounter=();
231: $Apache::lonxml::depth=-1;
232: $Apache::lonxml::olddepth=-1;
233: }
234:
235: sub increasedepth {
1.19 albertel 236: my ($token) = @_;
1.17 albertel 237: if ($Apache::lonxml::depth<$Apache::lonxml::olddepth-1) {
238: $#Apache::lonxml::depthcounter--;
239: $Apache::lonxml::olddepth=$Apache::lonxml::depth;
240: }
241: $Apache::lonxml::depth++;
1.19 albertel 242: # print "<br>s $Apache::lonxml::depth : $Apache::lonxml::olddepth : $token->[1]<br>\n";
1.17 albertel 243: $Apache::lonxml::depthcounter[$Apache::lonxml::depth]++;
244: if ($Apache::lonxml::depthcounter[$Apache::lonxml::depth]==1) {
245: $Apache::lonxml::olddepth=$Apache::lonxml::depth;
246: }
247: }
248:
249: sub decreasedepth {
1.19 albertel 250: my ($token) = @_;
1.17 albertel 251: $Apache::lonxml::depth--;
1.19 albertel 252: # print "<br>e $Apache::lonxml::depth : $Apache::lonxml::olddepth : $token->[1]<br>\n";
1.1 sakharuk 253: }
1.19 albertel 254:
255: sub get_all_text {
256:
257: my($tag,$pars)= @_;
258: my $depth=0;
259: my $token;
260: my $result='';
1.24 ! sakharuk 261: my $tag=substr($tag,1); #strip the / off the tag
! 262: # &Apache::lonxml::debug("have:$tag:");
1.19 albertel 263: while (($depth >=0) && ($token = $pars->get_token)) {
264: if ($token->[0] eq 'T') {
265: $result.=$token->[1];
266: } elsif ($token->[0] eq 'S') {
267: if ($token->[1] eq $tag) { $depth++; }
268: $result.=$token->[4];
269: } elsif ($token->[0] eq 'E') {
1.24 ! sakharuk 270: if ( $token->[1] eq $tag) { $depth--; }
1.19 albertel 271: #skip sending back the last end tag
272: if ($depth > -1) { $result.=$token->[2]; }
273: }
274: }
275: return $result
276: }
277:
1.23 albertel 278: sub newparser {
279: my ($parser,$contentref,$dir) = @_;
280: push (@$parser,HTML::TokeParser->new($contentref));
281: if ( $dir eq '' ) {
282: push (@Apache::lonxml::pwd, $Apache::lonxml::pwd[$#Apache::lonxml::pwd]);
283: } else {
284: push (@Apache::lonxml::pwd, $dir);
285: }
286: # &Apache::lonxml::debug("pwd:$#Apache::lonxml::pwd");
287: # &Apache::lonxml::debug("pwd:$Apache::lonxml::pwd[$#Apache::lonxml::pwd]");
288: }
1.1 sakharuk 289:
1.8 albertel 290: sub parstring {
291: my ($token) = @_;
292: my $temp='';
1.20 albertel 293: map {
294: if ($_=~/\w+/) {
295: $temp .= "my \$$_=\"$token->[2]->{$_}\";"
296: }
297: } @{$token->[3]};
1.8 albertel 298: return $temp;
299: }
1.24 ! sakharuk 300: #<<<<<<< lonxml.pm
1.22 albertel 301:
1.24 ! sakharuk 302: sub handler {
! 303: my $request=shift;
! 304:
! 305: my $target='web';
! 306: $Apache::lonxml::debug=1;
! 307: $request->content_type('text/html');
! 308: $request->send_http_header;
! 309: $request->print(<<ENDHEADER);
! 310: <html>
! 311: <head>
! 312: <title>Just test</title>
! 313: </head>
! 314: <body bgcolor="#FFFFFF">
! 315: ENDHEADER
! 316: # &Apache::lonhomework::send_header($request);
! 317: my $file = "/home/httpd/html".$request->uri;
! 318: my %mystyle;
! 319: my $result = '';
! 320: $result = Apache::lonxml::xmlparse($target, &Apache::lonnet::getfile($file),'',%mystyle);
! 321: # $request->print("Result follows:");
! 322: $request->print($result);
! 323: # $request->print(":Result ends");
! 324: }
! 325:
1.22 albertel 326: $Apache::lonxml::debug=0;
327: sub debug {
328: if ($Apache::lonxml::debug eq 1) {
329: print "DEBUG:".$_[0]."<br>\n";
330: }
331: }
332: sub error {
1.23 albertel 333: print "ERROR:".$_[0]."<br>\n";
1.22 albertel 334: }
335: sub warning {
336: if ($Apache::lonxml::debug eq 1) {
337: print "WARNING:".$_[0]."<br>\n";
338: }
339: }
340:
1.1 sakharuk 341: 1;
342: __END__
1.11 sakharuk 343:
344:
345:
346:
347:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>