HTMLTemplate.pm

filter: Statementstmt 79.5% 105 / 132 Branchbran 42.8% 18 / 42 Conditioncond 40.0% 4 / 10 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 73.9% 17 / 23 Podpod 88.8% 8 / 9 totaltotal 68.4% 152 / 222 CC 28 SCAR 14.9
CC
28
Cov
69%
filename
12.0
items
6.7
new
6.5
CRAP 4.4
« HTMLTemplate/WithApacheSession.pm ↑ Summary
1 package MasonX::Request::HTMLTemplate;
2  
3 #use vars qw(@ISA);
4  
5 1 ▶use HTML::Mason 1.16;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use HTML::Mason::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use HTML::Template::Extension;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Params::Validate qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 $MasonX::Request::HTMLTemplate::VERSION = '0.06';
12  
13 #@ISA = qw(HTML::Mason::Request HTML::Template::Extension);
14 1 ▶use base qw(HTML::Mason::Request HTML::Template::Extension);
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 # definition of localizated error string for unexistent template
17 1 my $err_tmpl_notfound_string = {
18 'it' => q|
19 <h2>Modello per il componente <b>%comp_name%</b>
20 non trovato.</h2>
21 Il modello mancante dovrebbe essere posto
22 nel percorso <br><b><pre>%tmpl_file_path%</pre></b>
23 <p>Contattate il webmaster
24 |,
25 'en' => q|
26 <h2>Unable to find template for <b>%comp_name%</b>
27 .</h2>
28 The missing template should be located in the path
29 <br><b><pre>%tmpl_file_path%</pre></b>
30 <p>Please contact webmaster
31 |,
32 'fr' => q|
33 <h2>Unable to find template for <b>%comp_name%</b>
34 .</h2>
35 The missing template should be located in the path
36 <br><b><pre>%tmpl_file_path%</pre></b>
37 <p>Please contact webmaster
38 <p>Please traslate it in french language
39 |,
40 };
41  
42 0 ▶Params::Validate::validation_options( on_fail => sub { param_error( join '', @_ ) } );
Subroutine __ANON__: not called CC=1 SCAR=1.2
43  
44 1 __PACKAGE__->valid_params(
45 template_base_path => {
46 parse =>'string',
47 type => Params::Validate::SCALAR,
48 optional => 1,
49 default => 'undef',
50 },
51 default_language => {
52 parse =>'string',
53 type => Params::Validate::SCALAR,
54 optional => 1,
55 default => 'en',
56 }
57 );
58  
59 1 my %fields =
60 (
61 autoDeleteHeader => 0,
62 file => '',
63 args => {},
64 plugins=>["SLASH_VAR","CSTART","HEAD_BODY","IF_TERN"],
65 absolute_path => 0,
66 );
67  
68  
69 5 ▶sub new {
Subroutine new: called CC=5 SCAR=18.7
Pod documented
70 5 my $class = shift;
71 5 my $htmpl = $class->HTML::Template::Extension::new(%fields);
72 5 $class->alter_superclass( &{"${class}::_alter_superclass"} );
73 5 my $mason = $class->SUPER::new(@_);
74 5 $self = {%{$mason},%{$htmpl}};
75 5 bless $self, $class;
76 5 ▶ while (my ($key,$value) = each(%options)) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %options) 0 5 5
77 0 ▶ if (exists($fields{$key})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $fields{$key}) { } 0 0 0
78 0 ▶ $self->{$key} = $value if ($key ne 'file');
Branch: 1 branch
BranchTrueFalseTotal
if $key ne "file" 0 0 0
79 } else {
80 0 die ref($self) . "::new: invalid option '$key'\n";
81 }
82 }
83 5 ▶ $self->filename($options{file}) if (exists($options{file}));
Branch: 1 branch
BranchTrueFalseTotal
if exists $options{"file"} 0 5 5
84 5 return $self;
85 }
86  
87 4 ▶sub print_template {
Subroutine print_template: called CC=4 SCAR=14.5
Pod documented
88 4 my $self = shift;
89 4 ▶ my $c_args = shift || {};
Condition50%1/2
shift() || {}
l!l
04
MC/DC0%0/1
shift() || {}
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
90 4 ▶ my $tmpl_file_path = shift || $self->callers(0)->name;
Condition33%1/3
shift() || $self->callers(0)->name
l!l&&r!l&&!r
040
MC/DC0%0/2
shift() || $self->callers(0)->name
shift() $self->callers(0)->name
Truth table33%1/3
A || B
A= shift()B= $self->callers(0)->name
ABresult
1X1
011
000
91 4 $tmpl_file_path = $self->_convFileName($tmpl_file_path);
92 # merging $c_args and items
93 4 my $html_args = { %{$self->items}, %{$c_args} };
94 4 my $html = $self->html($html_args,$tmpl_file_path);
95 4 ▶ $self->print($html) if (defined $html);
Branch: 1 branch
BranchTrueFalseTotal
if defined $html 4 0 4
96 }
97 4 ▶sub items {
Subroutine items: called CC=6 SCAR=19.1
Pod documented
98 # return a reference to an hash with union of %ARGS, $self->{args},
99 # the traslaslation of session variable using _convStructToHash and,
100 # for each elements of this hash, items in the form "key=value" => 1
101 4 my $self = shift;
102 4 ▶ my $more_args = shift || {};
Condition50%1/2
shift() || {}
l!l
04
MC/DC0%0/1
shift() || {}
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
103  
104 4 ▶ my %self_args = $self->{args} ? %{$self->{args}} : {};
Branch: 1 branch
BranchTrueFalseTotal
$self->{'args'} ? : 4 0 4
105 # merging client args + add_template_args + more_args (from WithApacheSession)
106 4 $ret = {%{$more_args} , %{$self->request_args} , %self_args};
107  
108 # add key=value => 1 to be used with TMPL_IF or with
109 # HTML::Template::Extension::IF_TERM
110 4 my @keys = keys %$ret;
111 4 foreach $key (@keys) {
112 11 my $value = $ret->{$key};
113 11 ▶ if (ref($value) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value eq 'ARRAY') { } 0 11 11
114 # is an array. Traslate and add all elements in form key=value => 1
115 0 foreach (@{$value}) { $ret->{"$key=$_"} = 1; }
116 } else {
117 # scalar...ok...single element.
118 11 $ret->{"$key=$value"} = 1;
119 }
120 }
121 4 return $ret;
122 }
123 0 ▶sub filename {
Subroutine filename: not called CC=3 SCAR=24.8
Pod documented
124 0 my $self = shift;
125 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
126 0 my $filename = $self->_tmplFilePath(shift);
127 0 ▶ $self->SUPER::filename($self->_tmplFilePath(shift)) if (defined $filename);
Branch: 1 branch
BranchTrueFalseTotal
if defined $filename 0 0 0
128 }
129 0 return $self->{filename};
130 }
131  
132 0 ▶sub file {
Subroutine file: not called CC=1 SCAR=6.9
Pod documented
133 0 return shift()->filename(@_);
134 }
135  
136 4 ▶sub html {
Subroutine html: called CC=4 SCAR=14.7
Pod documented
137 # overraide defalt html method to support base_root_dir
138 # and return html error string if selected template doesn't exist.
139 4 my $self = shift;
140 4 my $args = shift;
141 4 my $file = shift;
142 # define lang bypass cache
143 4 ▶ if (exists($args->{lang})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args->{"lang"}) 1 3 4
144 1 $self->{default_language} = $args->{lang};
145 }
146 4 ▶ if (defined $file) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $file) { } 4 0 4
147 4 my $file = $self->_tmplFilePath($file);
148 4 ▶ if (-e $file) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $file) { } 4 0 4
149 4 return $self->SUPER::html($args,$file);
150 } else {
151 # template file don't exists...print error to client
152 0 $self->_throw_error_tmpl_notfound($file);
153 # undef to stop print above.
154 0 return undef;
155 }
156 } else {
157 0 return $self->SUPER::html($args);
158 }
159 }
160  
161 6 ▶sub add_template_args() {
Subroutine add_template_args: called CC=1 SCAR=0.0
Pod documented
162 6 my $self = shift;
163 6 my %args = @_;
164 6 $self->{args} = {%{$self->{args}}, %args };
165 6 return @_;
166 }
167  
168 1 ▶sub template_arg() {
Subroutine template_arg: called CC=1 SCAR=0.0
Pod documented
169 1 my $self = shift;
170 1 my $key = shift;
171 1 return $self->{args}->{$key};
172 }
173  
174 4 ▶sub absolute_path {
Subroutine absolute_path: called CC=2 SCAR=7.2
Pod undocumented
175 # the template file is chroot(/) or chroot(mason_root_component_path)?
176 4 ▶ my $s=shift; return @_ ? ($s->{absolute_path}=shift) : $s->{absolute_path};
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 4 4
177 }
178  
179 5 ▶sub _alter_superclass() {
Subroutine _alter_superclass: called CC=3 SCAR=12.7
180 #return $MasonX::Request::WithApacheSession::VERSION
181 # ? 'MasonX::Request::WithApacheSession'
182 5 ▶ return $HTML::Mason::ApacheHandler::VERSION
Branch: 2 branches
BranchTrueFalseTotal
$HTML::Mason::ApacheHandler::VERSION ? : 0 5 5
$HTML::Mason::CGIHandler::VERSION ? : 0 5 5
183 ? 'HTML::Mason::Request::ApacheHandler'
184 : $HTML::Mason::CGIHandler::VERSION
185 ? 'HTML::Mason::Request::CGI'
186 : 'HTML::Mason::Request';
187 }
188  
189 4 ▶sub _tmplFilePath {
Subroutine _tmplFilePath: called CC=5 SCAR=17.6
190 # convert the file path based on absolute/relative path
191 # and to base_dir and language
192 4 my $self = shift;
193 4 ▶ my $comp_name = shift || $self->current_comp->name;
Condition33%1/3
shift() || $self->current_comp->name
l!l&&r!l&&!r
400
MC/DC0%0/2
shift() || $self->current_comp->name
shift() $self->current_comp->name
Truth table33%1/3
A || B
A= shift()B= $self->current_comp->name
ABresult
1X1
011
000
194 4 my $abs_path;
195 4 ▶ if ($self->absolute_path) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->absolute_path) { } 0 4 4
196 # client request that the param it set is absolute...
197 # try only to see if exists a language version
198 0 $abs_path = $comp_name;
199 } else {
200 # built absolute path
201 4 my $base_root = $self->interp->comp_root;
202 my $tbp = $self->{template_base_path} eq 'undef' ? '' :
203 4 ▶ $self->{template_base_path};
Branch: 1 branch
BranchTrueFalseTotal
$self->{'template_base_path'} eq 'undef' ? : 4 0 4
204 4 ▶ if (File::Spec->file_name_is_absolute($comp_name)) {
Branch: 1 branch
BranchTrueFalseTotal
if ('File::Spec'->file_name_is_absolute($comp_name)) { } 0 4 4
205 0 $abs_path = File::Spec->catfile($base_root,$tbp,$comp_name);
206 } else {
207 4 my $comp_dir= $self->callers(0)->path;
208 4 (undef,$comp_dir,undef) = File::Spec->splitpath($comp_dir);
209 4 $abs_path = File::Spec->catfile($base_root,$tbp,$comp_dir,$comp_name);
210 }
211 }
212 4 return $self->_tmplLang($abs_path);
213 }
214  
215 4 ▶sub _tmplLang {
Subroutine _tmplLang: called CC=2 SCAR=6.9
216 # try to see if exists file for language selected
217 4 my $self = shift;
218 4 my $abs_path = shift;
219 4 my ($volume,$dirs,$file) = File::Spec->splitpath( $abs_path );
220 4 my ($fn,$ext) = split(/\./,$file);
221 4 my $file_lang = $fn . '.' . $self->{default_language} . '.' . $ext;
222 4 my $path_lang = File::Spec->catpath($volume,$dirs,$file_lang);
223 4 $path_lang = File::Spec->canonpath($path_lang);
224 # return it if exists language file
225 4 ▶ return $path_lang if (-e $path_lang);
Branch: 1 branch
BranchTrueFalseTotal
if -e $path_lang 1 3 4
226 # else return the original after a cleanup
227 3 return File::Spec->canonpath($abs_path);
228 }
229  
230 4 ▶sub _convFileName {
Subroutine _convFileName: called CC=2 SCAR=7.0
231 # convert component name in template file subst extention with ".htt"
232 4 my $self = shift;
233 4 my $abs_path = shift;
234 4 my ($volume,$dirs,$file) = File::Spec->splitpath( $abs_path );
235 4 my ($fn,$ext) = split(/\./,$file);
236 4 ▶ return $abs_path if ($ext !~ /^m(pl|htm|html)$/);
Branch: 1 branch
BranchTrueFalseTotal
unless $ext =~ /^m(pl|htm|html)$/ 0 4 4
237 4 $file = "$fn.htt";
238 4 return File::Spec->canonpath(File::Spec->catpath($volume,$dirs,$file));
239 }
240  
241 0 ▶sub _print_html() {
Subroutine _print_html: not called CC=1 SCAR=6.9
242 0 my $self = shift;
243 0 return "<HTML>\n<HEAD>\n</HEAD>\n<BODY>\n" . shift() . "\n</BODY>\n</HTML>";
244 }
245  
246 0 ▶sub _throw_error_tmpl_notfound {
Subroutine _throw_error_tmpl_notfound: not called CC=1 SCAR=6.9
247 0 my $self = shift;
248 0 my $tmpl_file_path = shift;
249 0 my $comp_name = $self->callers(0)->path;
250 0 my $htmlerr = $self->_print_html($self->_err_tmpl_notfound);
251 0 $self->scalarref(\$htmlerr);
252 0 $self->print($self->html({ comp_name => $comp_name ,
253 tmpl_file_path => $tmpl_file_path}));
254 }
255  
256 0 ▶sub _err_tmpl_notfound {
Subroutine _err_tmpl_notfound: not called CC=2 SCAR=17.9
257 # return localized error string for unexistent template
258 # see err_tmpl_notfound_string hash in the header of this package
259 return exists($err_tmpl_notfound_string->{$self->{default_language}}) ?
260 $err_tmpl_notfound_string->{$self->{default_language}} :
261 0 ▶ $err_tmpl_notfound_string->{en};
Branch: 1 branch
BranchTrueFalseTotal
exists $$err_tmpl_notfound_string{$self->{'default_language'}} ? : 0 0 0
262 }
263  
264 1 1;
« HTMLTemplate/WithApacheSession.pm ↑ Summary