Annotation of loncom/xml/lonxml.pm, revision 1.5
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.4 albertel 11: use Apache::style;
1.3 sakharuk 12: use Apache::lontexconvert;
1.4 albertel 13: use Apache::londefdef;
1.3 sakharuk 14: #================================================== Main subroutine: xmlparse
15:
16: sub xmlparse {
17:
18: my ($target,$content_file_string,%style_for_target) = @_;
19: my $pars = HTML::TokeParser->new(\$content_file_string);
20: my $currentstring = '';
21: my $finaloutput = '';
22: my $newarg = '';
23: my $tempostring = '';
24: my $tempocont = '';
25: my $safeeval = new Safe;
26:
27: #-------------------- Redefinition of the target in the case of compound target
28:
29: ($target, my @tenta) = split('&&',$target);
30:
31: #------------------------- Stack definition (in stack we have all current tags)
32:
33: my @stack = ();
34: my @parstack = ();
35:
36: #------------------------------------- Parse input string (content_file_string)
37:
38: my $token;
1.5 ! albertel 39:
1.3 sakharuk 40: while ($token = $pars->get_token) {
1.5 ! albertel 41: if ($token->[0] eq 'T') {
! 42: $finaloutput .= $token->[1];
! 43: $tempocont .= $token->[1];
! 44: } elsif ($token->[0] eq 'S') {
1.3 sakharuk 45: #------------------------------------------------------------- add tag to stack
1.5 ! albertel 46: push (@stack,$token->[1]);
1.3 sakharuk 47: #----------------------------------------- add parameters list to another stack
1.5 ! albertel 48: map {$tempostring .= "$_=$token->[2]->{$_},"} @{$token->[3]};
! 49: push (@parstack,$tempostring);
! 50: $tempostring = '';
! 51:
! 52: if (exists $style_for_target{$token->[1]}) {
! 53:
1.3 sakharuk 54: #---------------------------------------------------- use style file definition
55:
1.5 ! albertel 56: $newarg = $style_for_target{$token->[1]};
! 57:
! 58: if (index($newarg,'script') != -1 ) {
! 59: my $pat = HTML::TokeParser->new(\$newarg);
! 60: my $tokenpat;
! 61: my $partstring = '';
! 62: my $oustring = '';
! 63: my $outputstring;
! 64:
! 65: while ($tokenpat = $pat->get_token) {
! 66: if ($tokenpat->[0] eq 'T') {
! 67: $oustring .= $tokenpat->[1];
! 68: } elsif ($tokenpat->[0] eq 'S') {
! 69: if ($tokenpat->[1] eq 'script') {
! 70: while ($tokenpat = $pat->get_token and $tokenpat->[1] ne 'script') {
! 71: if ($tokenpat->[0] eq 'S') {
! 72: $partstring .= $tokenpat->[4];
! 73: } elsif ($tokenpat->[0] eq 'T') {
! 74: $partstring .= $tokenpat->[1];
! 75: } elsif ($tokenpat->[0] eq 'E') {
! 76: $partstring .= $tokenpat->[2];
! 77: }
! 78: }
! 79:
! 80: map {$partstring =~ s/\$$_/$token->[2]->{$_}/g; } @{$token->[3]};
! 81:
! 82: &run($partstring,$safeeval);
! 83:
! 84: $partstring = '';
! 85: } elsif ($tokenpat->[1] eq 'evaluate') {
! 86: $outputstring = &evaluate($tokenpat->[2]{expression},$safeeval);
! 87: $oustring .= $outputstring;
! 88: } else {
! 89: $oustring .= $tokenpat->[4];
1.3 sakharuk 90: }
1.5 ! albertel 91: } elsif ($tokenpat->[0] eq 'E' and $tokenpat->[1] ne 'evaluate') {
! 92: $oustring .= $tokenpat->[1];
1.3 sakharuk 93: }
1.5 ! albertel 94: }
! 95: $newarg = $oustring;
! 96: } else {
! 97: map {$newarg =~ s/\$$_/$token->[2]->{$_}/g; } @{$token->[3]};
1.2 sakharuk 98: }
1.3 sakharuk 99: $finaloutput .= $newarg;
1.5 ! albertel 100: } else {
! 101: # use default definition of tag
! 102: my $sub="start_$token->[1]";
! 103: {
1.3 sakharuk 104: no strict 'refs';
1.5 ! albertel 105: if (defined (&$sub)) {
! 106: $currentstring = &$sub($target,$token,\@parstack);
! 107: $finaloutput .= $currentstring;
! 108: $currentstring = '';
1.3 sakharuk 109: } else {
110: $finaloutput .= $token->[4];
111: }
1.5 ! albertel 112: use strict 'refs';
! 113: }
! 114: }
! 115: } elsif ($token->[0] eq 'E') {
! 116: # Put here check for correct final tag (to avoid existence of
! 117: # starting tag only)
1.3 sakharuk 118:
119: pop @stack;
120: unless (exists $style_for_target{$token->[1]}) {
1.5 ! albertel 121: my $sub="end_$token->[1]";
1.3 sakharuk 122: {
1.5 ! albertel 123: no strict 'refs';
! 124: if (defined(&$sub)) {
! 125: $currentstring = &$sub($target,$token,\@parstack);
! 126: $finaloutput .= $currentstring;
! 127: $currentstring = '';
! 128: } else {
! 129: $finaloutput .= $token->[4];
! 130: }
! 131: use strict 'refs';
1.2 sakharuk 132: }
1.3 sakharuk 133: }
1.5 ! albertel 134: #---- end tag from the style file
1.3 sakharuk 135: if (exists $style_for_target{'/'."$token->[1]"}) {
136: $newarg = $style_for_target{'/'."$token->[1]"};
137: if (index($newarg,'script') != -1 ) {
138: my $pat = HTML::TokeParser->new(\$newarg);
139: my $tokenpat;
140: my $partstring = '';
141: my $oustring = '';
142: my $outputstring;
1.5 ! albertel 143:
1.3 sakharuk 144: while ($tokenpat = $pat->get_token) {
1.5 ! albertel 145: if ($tokenpat->[0] eq 'T') {
! 146: $oustring .= $tokenpat->[1];
! 147: } elsif ($tokenpat->[0] eq 'S') {
1.3 sakharuk 148: if ($tokenpat->[1] eq 'script') {
149: while ($tokenpat = $pat->get_token and $tokenpat->[1] ne 'script') {
1.5 ! albertel 150: if ($tokenpat->[0] eq 'S') {
! 151: $partstring .= $tokenpat->[4];
! 152: } elsif ($tokenpat->[0] eq 'T') {
! 153: $partstring .= $tokenpat->[1];
! 154: } elsif ($tokenpat->[0] eq 'E') {
! 155: $partstring .= $tokenpat->[2];
! 156: }
1.2 sakharuk 157: }
1.5 ! albertel 158:
1.3 sakharuk 159: my @tempor_list = split(',',$parstack[$#parstack]);
160: my @te_kl = ();
161: my %tempor_hash = ();
162: map {(my $onete,my $twote) = split('=',$_); push (@te_kl,$onete);
163: $tempor_hash{$onete} = $twote} @tempor_list;
164: map {$partstring =~ s/\$$_/$tempor_hash{$_}/g; } @te_kl;
1.5 ! albertel 165:
1.3 sakharuk 166: &run($partstring,$safeeval);
1.5 ! albertel 167:
1.3 sakharuk 168: $partstring = '';
169: } elsif ($tokenpat->[1] eq 'evaluate') {
1.5 ! albertel 170: $outputstring = &evaluate($tokenpat->[2]{expression},$safeeval);
! 171: $oustring .= $outputstring;
1.3 sakharuk 172: } else {
1.5 ! albertel 173: $oustring .= $tokenpat->[4];
1.3 sakharuk 174: }
1.5 ! albertel 175: } elsif ($tokenpat->[0] eq 'E' and $tokenpat->[1] ne 'evaluate') {
1.3 sakharuk 176: $oustring .= $tokenpat->[1];
1.5 ! albertel 177: }
1.3 sakharuk 178: }
1.5 ! albertel 179: $newarg = $oustring;
1.3 sakharuk 180: } else {
1.5 ! albertel 181: my @very_temp = split(',',$parstack[$#parstack]);
1.3 sakharuk 182: map {my @ret= split('=',$_); $newarg =~ s/\$$ret[0]/$ret[1]/g; } @very_temp;
1.2 sakharuk 183: }
1.5 ! albertel 184:
1.3 sakharuk 185: $finaloutput .= $newarg;
1.2 sakharuk 186: }
1.3 sakharuk 187: pop @parstack;
1.5 ! albertel 188: }
1.3 sakharuk 189: }
190: return $finaloutput;
1.1 sakharuk 191: }
192:
193: 1;
194: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>