File:
[LON-CAPA] /
loncom /
xml /
lonxml.pm
Revision
1.23:
download - view:
text,
annotated -
select for diffs
Thu Oct 5 19:30:14 2000 UTC (23 years, 9 months ago) by
albertel
Branches:
MAIN
CVS tags:
HEAD
- created &newparser routine to be used when adding a parser to the stack
it also updates:
- new global @Apache::lonxml::pwd which maintains the directory each parser was started in.
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: sub xmlparse {
31:
32: my ($target,$content_file_string,$safeinit,%style_for_target) = @_;
33: my @pars = ();
34: @Apache::lonxml::pwd=();
35: my $pwd=$ENV{'request.filename'};
36: $pwd =~ s:/[^/]*$::;
37: &newparser(\@pars,\$content_file_string,$pwd);
38: my $currentstring = '';
39: my $finaloutput = '';
40: my $newarg = '';
41: my $result;
42: my $safeeval = new Safe;
43: $safeeval->permit("entereval");
44: $safeeval->permit(":base_math");
45: $safeeval->deny(":base_io");
46: #need to inspect this class of ops
47: # $safeeval->deny(":base_orig");
48: $safeinit .= ';$external::target='.$target.';';
49: &Apache::run::run($safeinit,$safeeval);
50: #-------------------- Redefinition of the target in the case of compound target
51:
52: ($target, my @tenta) = split('&&',$target);
53:
54: my @stack = ();
55: my @parstack = ();
56: &initdepth;
57: my $token;
58: while ( $#pars > -1 ) {
59: while ($token = $pars[$#pars]->get_token) {
60: if ($token->[0] eq 'T') {
61: $result=$token->[1];
62: # $finaloutput .= &Apache::run::evaluate($token->[1],$safeeval,'');
63: } elsif ($token->[0] eq 'S') {
64: # add tag to stack
65: push (@stack,$token->[1]);
66: # add parameters list to another stack
67: push (@parstack,&parstring($token));
68: &increasedepth($token);
69: if (exists $style_for_target{$token->[1]}) {
70: $finaloutput .= &recurse($style_for_target{$token->[1]},
71: $target,$safeeval,\%style_for_target,
72: @parstack);
73: } else {
74: $result = &callsub("start_$token->[1]", $target, $token,\@parstack,
75: \@pars, $safeeval, \%style_for_target);
76: }
77: } elsif ($token->[0] eq 'E') {
78: #clear out any tags that didn't end
79: while ($token->[1] ne $stack[$#stack]
80: && ($#stack > -1)) {pop @stack;pop @parstack;&decreasedepth($token);}
81:
82: if (exists $style_for_target{'/'."$token->[1]"}) {
83: $finaloutput .= &recurse($style_for_target{'/'."$token->[1]"},
84: $target,$safeeval,\%style_for_target,
85: @parstack);
86: } else {
87: $result = &callsub("end_$token->[1]", $target, $token, \@parstack,
88: \@pars,$safeeval, \%style_for_target);
89: }
90: }
91: if ($result ne "" ) {
92: if ( $#parstack > -1 ) {
93: $finaloutput .= &Apache::run::evaluate($result,$safeeval,
94: $parstack[$#parstack]);
95: } else {
96: $finaloutput .= &Apache::run::evaluate($result,$safeeval,'');
97: }
98: $result = '';
99: }
100: if ($token->[0] eq 'E') { pop @stack;pop @parstack;&decreasedepth($token);}
101: }
102: pop @pars;
103: pop @Apache::lonxml::pwd;
104: }
105: return $finaloutput;
106: }
107:
108: sub recurse {
109:
110: my @innerstack = ();
111: my @innerparstack = ();
112: my ($newarg,$target,$safeeval,$style_for_target,@parstack) = @_;
113: my @pat = ();
114: &newparser(\@pat,\$newarg);
115: my $tokenpat;
116: my $partstring = '';
117: my $output='';
118: my $decls='';
119: while ( $#pat > -1 ) {
120: while ($tokenpat = $pat[$#pat]->get_token) {
121: if ($tokenpat->[0] eq 'T') {
122: $partstring = $tokenpat->[1];
123: } elsif ($tokenpat->[0] eq 'S') {
124: push (@innerstack,$tokenpat->[1]);
125: push (@innerparstack,&parstring($tokenpat));
126: &increasedepth($tokenpat);
127: $partstring = &callsub("start_$tokenpat->[1]",
128: $target, $tokenpat, \@innerparstack,
129: \@pat, $safeeval, $style_for_target);
130: } elsif ($tokenpat->[0] eq 'E') {
131: #clear out any tags that didn't end
132: while ($tokenpat->[1] ne $innerstack[$#innerstack]
133: && ($#innerstack > -1)) {pop @innerstack;pop @innerparstack;
134: &decreasedepth($tokenpat);}
135: $partstring = &callsub("end_$tokenpat->[1]",
136: $target, $tokenpat, \@innerparstack,
137: \@pat, $safeeval, $style_for_target);
138: }
139: #pass both the variable to the style tag, and the tag we
140: #are processing inside the <definedtag>
141: if ( $partstring ne "" ) {
142: if ( $#parstack > -1 ) {
143: if ( $#innerparstack > -1 ) {
144: $decls= $parstack[$#parstack].$innerparstack[$#innerparstack];
145: } else {
146: $decls= $parstack[$#parstack];
147: }
148: } else {
149: if ( $#innerparstack > -1 ) {
150: $decls=$innerparstack[$#innerparstack];
151: } else {
152: $decls='';
153: }
154: }
155: $output .= &Apache::run::evaluate($partstring,$safeeval,$decls);
156: $partstring = '';
157: }
158: if ($tokenpat->[0] eq 'E') { pop @innerstack;pop @innerparstack;
159: &decreasedepth($tokenpat);}
160: }
161: pop @pat;
162: pop @Apache::lonxml::pwd;
163: }
164: return $output;
165: }
166:
167: sub callsub {
168: my ($sub,$target,$token,$parstack,$parser,$safeeval,$style)=@_;
169: my $currentstring='';
170: {
171: no strict 'refs';
172: if (my $space=$Apache::lonxml::alltags{$token->[1]}) {
173: #&Apache::lonxml::debug("Calling sub $sub in $space<br>\n");
174: $sub="$space\:\:$sub";
175: $Apache::lonxml::curdepth=join('_',@Apache::lonxml::depthcounter);
176: $currentstring = &$sub($target,$token,$parstack,$parser,
177: $safeeval,$style);
178: } else {
179: #&Apache::lonxml::debug("NOT Calling sub $sub in $space<br>\n");
180: if (defined($token->[4])) {
181: $currentstring = $token->[4];
182: } else {
183: $currentstring = $token->[2];
184: }
185: }
186: use strict 'refs';
187: }
188: return $currentstring;
189: }
190:
191: sub initdepth {
192: @Apache::lonxml::depthcounter=();
193: $Apache::lonxml::depth=-1;
194: $Apache::lonxml::olddepth=-1;
195: }
196:
197: sub increasedepth {
198: my ($token) = @_;
199: if ($Apache::lonxml::depth<$Apache::lonxml::olddepth-1) {
200: $#Apache::lonxml::depthcounter--;
201: $Apache::lonxml::olddepth=$Apache::lonxml::depth;
202: }
203: $Apache::lonxml::depth++;
204: # print "<br>s $Apache::lonxml::depth : $Apache::lonxml::olddepth : $token->[1]<br>\n";
205: $Apache::lonxml::depthcounter[$Apache::lonxml::depth]++;
206: if ($Apache::lonxml::depthcounter[$Apache::lonxml::depth]==1) {
207: $Apache::lonxml::olddepth=$Apache::lonxml::depth;
208: }
209: }
210:
211: sub decreasedepth {
212: my ($token) = @_;
213: $Apache::lonxml::depth--;
214: # print "<br>e $Apache::lonxml::depth : $Apache::lonxml::olddepth : $token->[1]<br>\n";
215: }
216:
217: sub get_all_text {
218:
219: my($tag,$pars)= @_;
220: my $depth=0;
221: my $token;
222: my $result='';
223: while (($depth >=0) && ($token = $pars->get_token)) {
224: if ($token->[0] eq 'T') {
225: $result.=$token->[1];
226: } elsif ($token->[0] eq 'S') {
227: if ($token->[1] eq $tag) { $depth++; }
228: $result.=$token->[4];
229: } elsif ($token->[0] eq 'E') {
230: if ($token->[1] eq $tag) { $depth--; }
231: #skip sending back the last end tag
232: if ($depth > -1) { $result.=$token->[2]; }
233: }
234: }
235: return $result
236: }
237:
238: sub newparser {
239: my ($parser,$contentref,$dir) = @_;
240: push (@$parser,HTML::TokeParser->new($contentref));
241: if ( $dir eq '' ) {
242: push (@Apache::lonxml::pwd, $Apache::lonxml::pwd[$#Apache::lonxml::pwd]);
243: } else {
244: push (@Apache::lonxml::pwd, $dir);
245: }
246: # &Apache::lonxml::debug("pwd:$#Apache::lonxml::pwd");
247: # &Apache::lonxml::debug("pwd:$Apache::lonxml::pwd[$#Apache::lonxml::pwd]");
248: }
249:
250: sub parstring {
251: my ($token) = @_;
252: my $temp='';
253: map {
254: if ($_=~/\w+/) {
255: $temp .= "my \$$_=\"$token->[2]->{$_}\";"
256: }
257: } @{$token->[3]};
258: return $temp;
259: }
260:
261: $Apache::lonxml::debug=0;
262: sub debug {
263: if ($Apache::lonxml::debug eq 1) {
264: print "DEBUG:".$_[0]."<br>\n";
265: }
266: }
267: sub error {
268: print "ERROR:".$_[0]."<br>\n";
269: }
270: sub warning {
271: if ($Apache::lonxml::debug eq 1) {
272: print "WARNING:".$_[0]."<br>\n";
273: }
274: }
275:
276: 1;
277: __END__
278:
279:
280:
281:
282:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>