blib/lib/ASP4/PageParser.pm

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