ASP4/PageParser.pm

filter: Statementstmt 83.3% 10 / 12 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 4 / 4 Podpod n/a totaltotal 87.5% 14 / 16 CC 1 SCAR 0.7
CC
1
Cov
83%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« ASP4/API.pm ↑ Summary ASP4/Page.pm »
1  
2 package ASP4::PageParser;
3  
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use warnings 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use ASP4::ConfigLoader;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use ASP4::Page;
Subroutine BEGIN: called CC=1 SCAR=2.6
8 use ASP4::MasterPage;
9  
10  
11 sub new
12 {
13 my ($class, %args) = @_;
14
15 my $s = bless {
16 script_name => $args{script_name},
17 filename => undef,
18 package => undef,
19 compiled_as => undef,
20 base_class => undef,
21 source_code => \"",
22 }, $class;
23 $s->_init();
24
25 return $s;
26 }# end new()
27  
28 sub source_code { shift->{source_code} }
29  
30  
31 sub _init
32 {
33 my $s = shift;
34
35 my $config = ASP4::ConfigLoader->load();
36 my $filename = $config->web->www_root . $s->{script_name};
37 (my $package = $s->{script_name}) =~ s/[^a-z0-9]/_/ig;
38 $package = $config->web->application_name . '::' . $package;
39 (my $compiled_as = "$package.pm") =~ s/::/\//g;
40
41 # What we know so far:
42 $s->{filename} = $filename;
43 $s->{package} = $package;
44 $s->{compiled_as} = $compiled_as;
45 $s->{saved_to} = $config->web->page_cache_root . "/$compiled_as";
46 }# end _init()
47  
48  
49 sub parse
50 {
51 my $s = shift;
52
53 # Open up the file:
54 open my $ifh, '<', $s->{filename}
55 or die "Cannot open '$s->{filename}' for reading: $!";
56 local $/;
57 $s->{source_code} = \scalar(<$ifh>);
58
59 my $directives = $s->_get_directives;
60 if( my $master_uri = $directives->{page}->{usemasterpage} )
61 {
62 $s->{masterpage} = ASP4::PageLoader->load( script_name => $master_uri );
63 $s->{base_class} = $s->{masterpage}->{package};
64 }
65 elsif( $directives->{masterpage} )
66 {
67 $s->{base_class} = 'ASP4::MasterPage';
68 }
69 else
70 {
71 $s->{base_class} = 'ASP4::Page';
72 }# end if()
73
74 $s->_parse_scriptlet_tags;
75 $s->_parse_include_tags;
76 my $ref = $s->source_code;
77
78 # The <asp:ContentPlaceHolder ...>...</asp:ContentPlaceHolder> tags:
79 my $ident = 0;
80 my @placeholder_tags = ( );
81 my $depth = 0;
82 PLACEHOLDERS: {
83 my @stack = ( );
84 foreach my $tag ( $$ref =~ m{(<asp:ContentPlaceHolder\s+id\=".+?"\s*>|</\s*asp:ContentPlaceHolder>)}gis )
85 {
86 if( $tag =~ m{^</} )
87 {
88 # It's an "end" tag: </asp:ContentPlaceHolder>
89 my $item = pop(@stack);
90 $depth--;
91
92 my $repl = $item->{end_tag};
93 $$ref =~ s{$tag}{$repl}s;
94 unshift @placeholder_tags, $item;
95 }
96 else
97 {
98 # It's a "start" tag: <asp:ContentPlaceHolder id="...">
99 my ($id) = $tag =~ m{<asp:ContentPlaceHolder\s+id\="(.+?)">}is;
100 push @stack, {
101 ident => $ident,
102 id => $id,
103 depth => $depth++,
104 line => $s->_tag_line_number( $tag ),
105 start_tag => '______INP_' . sprintf('%03d',$ident) . '______',
106 end_tag => '______OUTP_' . sprintf('%03d',$ident) . '______'
107 };
108 $ident++;
109 my $repl = $stack[-1]->{start_tag};
110 $$ref =~ s{\Q$tag\E}{$repl}s;
111 }# end if()
112 }# end foreach()
113 };
114
115 foreach my $tag ( sort {$b->{depth} <=> $a->{depth} } @placeholder_tags )
116 {
117 my $start = $tag->{start_tag};
118 my $end = $tag->{end_tag};
119 my ($contents) = $$ref =~ m{$start(.*?)$end}s;
120  
121 $tag->{contents} = "\$Response->Write(q~$contents~);";
122 $$ref =~ s{$start\Q$contents\E$end}{\~); \$__self->$tag->{id}(\$__context); \$Response->Write(q\~}s;
123 }# end foreach()
124
125 # The <asp:Content PlaceHolderID="...">...</asp:Content> tags:
126 my @content_tags = ( );
127 CONTENT: {
128 my @stack = ( );
129 foreach my $tag ( $$ref =~ m{(<asp:Content\s+PlaceHolderID\=".+?"\s*>|</asp:Content\s*>)}gis )
130 {
131 if( $tag =~ m{^</} )
132 {
133 # It's an "end" tag: </asp:Content>
134 my $item = pop(@stack);
135 $depth--;
136 my $repl = $item->{end_tag};
137 $$ref =~ s{\Q$tag\E}{$repl}s;
138 }
139 else
140 {
141 # It's a "start" tag: <asp:Content PlaceHolderID="...">
142 my ($id) = $tag =~ m{<asp:Content\s+PlaceHolderID\="(.+?)"\s*>}is;
143 push @stack, {
144 ident => $ident,
145 id => $id,
146 depth => $depth++,
147 line => $s->_tag_line_number( $tag ),
148 start_tag => '______INC_' . sprintf('%03d',$ident) . '______',
149 end_tag => '______OUTC_' . sprintf('%03d',$ident) . '______'
150 };
151 $ident++;
152 my $repl = $stack[-1]->{start_tag};
153 $$ref =~ s{\Q$tag\E}{$repl}s;
154 unshift @content_tags, $stack[-1];
155 }# end if()
156 }# end foreach()
157 };
158
159 foreach my $tag ( sort {$b->{depth} <=> $a->{depth} } @content_tags )
160 {
161 my $start = $tag->{start_tag};
162 my $end = $tag->{end_tag};
163 my ($contents) = $$ref =~ m{$start(.*?)$end}s;
164  
165 $tag->{contents} = "\$Response->Write(q~$contents~);";
166 $$ref =~ s{$start\Q$contents\E$end}{\~); \$__self->$tag->{id}(\$__context); \$Response->Write(q\~}s;
167 }# end foreach()
168
169 my $code = <<"CODE";
170 package $s->{package};
171  
172 use strict;
173 use warnings 'all';
174 no warnings 'redefine';
175 use base '$s->{base_class}';
176 use vars __PACKAGE__->VARS;
177 use ASP4::PageLoader;
178  
179 sub _init {
180 my (\$s) = \@_;
181 \$s->{script_name} = q<$s->{script_name}>;
182 \$s->{filename} = q<$s->{filename}>;
183 \$s->{base_class} = q<$s->{base_class}>;
184 \$s->{compiled_as} = q<$s->{compiled_as}>;
185 \$s->{package} = q<$s->{package}>;
186 @{[
187 $s->{masterpage} ?
188 " \$s->{masterpage} = ASP4::PageLoader->load( script_name => q<$s->{masterpage}->{script_name}> );"
189 : ""
190 ]}
191 return;
192 }
193  
194 CODE
195  
196 unless( $s->{masterpage} )
197 {
198 $code .= <<"CODE";
199 sub run {
200 use warnings 'all';
201 my (\$__self, \$__context) = \@_;
202 #line 1
203 $$ref
204 }
205 CODE
206 }# end unless()
207
208 foreach( reverse ( @content_tags, @placeholder_tags ) )
209 {
210 $code .= <<"SUB";
211  
212 sub $_->{id} {
213 my (\$__self, \$__context) = \@_;
214 #line $_->{line}
215 $_->{contents}
216 }# end $_->{id}
217  
218 SUB
219 }# end foreach()
220
221 $code .= "\n1;# return true:\n";
222
223 open my $ofh, '>', $s->{saved_to}
224 or die "Cannot open '$s->{saved_to}' for writing: $!";
225 print $ofh $code;
226 close($ofh);
227 chmod(0766, $s->{saved_to});
228  
229 my $config = ASP4::ConfigLoader->load();
230 $config->load_class( $s->{package} );
231 return $s->{package}->new();
232 }# end parse()
233  
234  
235 sub _tag_line_number
236 {
237 my ($s, $tag) = @_;
238
239 my $number = 1;
240 for( split /\r?\n/, ${ $s->source_code } )
241 {
242 if( m/\Q$tag\E/s )
243 {
244 return $number;
245 }# end if()
246 $number++;
247 }# end for()
248
249 return;
250 }# end _tag_line_number()
251  
252  
253 sub _parse_include_tags
254 {
255 my ($s) = @_;
256
257 my $ref = $s->source_code;
258
259 $$ref =~ s{
260 \<\!\-\-\s*\#include\s+virtual\="(.*?)"\s*\-\-\>
261 }{~); \$Response->Include(\$Server->MapPath("$1")); \$Response->Write(q~}xsg;
262 }# end _parse_include_tags()
263  
264  
265 sub _parse_scriptlet_tags
266 {
267 my ($s) = @_;
268
269 my $ref = $s->source_code;
270
271 # Parse <%= %> items:
272 $$ref =~ s{
273 <%\=(.*?)%>
274 }{
275 '~);$Response->Write(' . $1 . ');$Response->Write(q~'
276 }xgse;
277
278 # TODO: Add <%& HTMLEncode($str) %>
279
280 # TODO: Add <%% URLEncode($str) %>
281  
282 $$ref =~ s{
283 <%\s*([^\@\#\=]?.*?)%>
284 }{
285 my $txt = $1; '~);' . $txt . ';$Response->Write(q~'
286 }gxse;
287
288 $$ref =~ s/(\$Response\->End)/return $1/gs;
289
290 $$ref = ';$Response->Write(q~' . $$ref . '~);';
291
292 # Now do the final ~ substitution:
293 $$ref =~ s{(\(q~)(.*?)(~\);)}{
294 my $pre = $1;
295 my $post = $3;
296 (my $txt = $2) =~ s/~/\\~/g;
297 "$pre$txt$post"
298 }xsge;
299 }# end _parse_scriptlet_tags()
300  
301  
302 sub _get_directives
303 {
304 my ($s) = @_;
305
306 my $ref = $s->source_code;
307 my %directives = ( );
308 while( my ($tag, $directive, $attr_str) = $$ref =~ m/(<%@\s*(.*?)\s+(.*?)\s*%>)/ )
309 {
310 my $attrs = $s->_parse_tag_attrs( $attr_str );
311 $$ref =~ s/\Q$tag\E//;
312 $directives{ lc($directive) } = $attrs;
313 }# end while()
314
315 $directives{page} ||= { };
316
317 return \%directives;
318 }# end _get_directives()
319  
320
321 sub _parse_tag_attrs
322 {
323 my ($s, $str) = @_;
324
325 my $attr = { };
326 while( $str =~ m@([^\s\=\"\']+)(\s*=\s*(?:(")(.*?)"|(')(.*?)'|([^'"\s=]+)['"]*))?@sg ) #@
327 {
328 my $key = $1;
329 my $test = $2;
330 my $val = ( $3 ? $4 : ( $5 ? $6 : $7 ));
331 if( $test )
332 {
333 $attr->{ lc($key) } = $val;
334 }
335 else
336 {
337 $attr->{ lc($key) } = $key;
338 }# end if()
339 }# end while()
340
341 return $attr;
342 }# end _parse_tag_attrs()
343  
344  
345 sub DESTROY
346 {
347 my $s = shift;
348 undef(%$s);
349 }# end DESTROY()
350  
351 1;# return true:
352  
« ASP4/API.pm ↑ Summary ASP4/Page.pm »