View/XSLT.pm

filter: Statementstmt 72.5% 95 / 131 Branchbran 50.0% 30 / 60 Conditioncond 57.4% 31 / 54 MC/DCmcdc 25.9% 7 / 27 Subroutinesub 91.6% 11 / 12 Podpod 100% 3 / 3 totaltotal 61.6% 177 / 287 CC 51 SCAR 35.1
CC
51
Cov
64%
render
58.6
_coerce_paths
30.0
new
19.1
CRAP 33.6
« View/XSLT/XML/LibXSLT.pm ↑ Summary Helper/View/XSLT.pm »
1 package Catalyst::View::XSLT;
2  
3 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 13 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 13 ▶use base 'Catalyst::View';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 13 ▶use Catalyst::View::XSLT::XML::LibXSLT;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 13 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 13 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 13 our $VERSION = '0.09';
11  
12 # check if this is a MS Windows
13 13 my $isMS = $^O eq 'MSWin32';
14  
15 =head1 NAME
16  
17 Catalyst::View::XSLT - XSLT View Class
18  
19 =head1 SYNOPSIS
20  
21 # use the helper to create your view
22 myapp_create.pl view XSLT XSLT
23  
24 # configure in lib/MyApp/View/XSLT.pm (can be done from a config file too)
25 package MyApp::View::XSLT;
26  
27 use base 'Catalyst::View::XSLT';
28  
29 __PACKAGE__->config(
30 # paths to the directories with templates
31 INCLUDE_PATH => [
32 MyApp->path_to( 'root', 'xslt' ),
33 MyApp->path_to( 'templates', 'xsl' ),
34 ],
35  
36 # default template extension to use
37 # when you don't provide template name
38 TEMPLATE_EXTENSION => '.xsl',
39  
40 # use this for debug purposes
41 # it will dump the the final (merged) config
42 DUMP_CONFIG => 1,
43  
44 # XML::LibXSLT specific configuration
45 LibXSLT => {
46 register_function => [
47 {
48 uri => 'urn:catalyst',
49 name => 'add',
50 subref => sub { return $_[0] + $_[1] },
51 },
52 {
53 uri => 'urn:foo',
54 name => 'Hello',
55 subref => sub { return 'Hello, Catalyst\'s user.' },
56 },
57 ],
58 },
59 );
60  
61 # don't need nothing more
62  
63 1;
64  
65 # in your controller(s) :
66 sub someAction : Local {
67  
68 # 'template' could be string or path to file
69 # see 'xml' for more info about string version
70  
71 # path to the template could be absolute
72 $c->stash->{template} = $c->config->{home} . 'root/some.xsl';
73  
74 # or relative
75 $c->stash->{template} = 'some.xsl'; # this file will be searched in include paths
76  
77 # or if you didn't provide any template name
78 # then the last chance is 'someAction.xsl' ($c->action . $config->{TEMPLATE_EXTENSION})
79  
80 # 'xml' could be string
81 $c->stash->{xml} =<<XML;
82 <root>
83 <level1>data</level>
84 </root>
85 XML
86 # or a relative path which will se searched in include paths
87 # $c->stash->{xml} = 'my.xml';
88  
89 # or an absolute path
90 # $c->stash->{xml} = '/some/where/around.xml';
91  
92 # add more subrefs (these will predefine config ones if they overlap)
93 $c->stash->{additional_register_function} = [
94 {
95 uri => 'urn:catalyst',
96 name => 'doIt',
97 subref => sub { return $obj->method(@_) },
98 }
99 ];
100  
101 # everything else in the stash will be used for parameters (<xsl:param name="param1" />)
102 $c->stash->{param1} = 'Param1 value';'
103 $c->stash->{param2} = 'Param2 value';
104 }
105  
106 # Meanwhile, maybe in an 'end' action
107  
108 $c->forward('MyApp::View::XSLT');
109  
110 # to use your registered functions in some.xsl:
111 <xsl:stylesheet
112 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
113 xmlns:catalyst="urn:catalyst"
114 xmlns:foo="urn:foo"
115 version="1.0">
116 ...
117 <xsl:value-of select="catalyst:add(4, 5)" />
118 <xsl:value-of select="foo:Hello()" />
119 <xsl:value-of select="catalyst:doIt($param1, 3)" />
120 ...
121 </xsl:stylesheet>
122  
123 =head1 DESCRIPTION
124  
125 This is a C<XSLT> view class for Catalyst.
126 Your application should defined a view class which is a subclass of
127 this module. The easiest way to achieve this is using the
128 F<myapp_create.pl> script (where F<myapp> should be replaced with
129 whatever your application is called). This script is created as part
130 of the Catalyst setup.
131  
132 =head1 METHODS
133  
134 =over 4
135  
136 =item new
137  
138 The constructor for the XSLT view.
139 Reads the application config.
140  
141 =cut
142  
143 # this code is borrowed from Catalyst::View::TT
144 0 ▶sub _coerce_paths {
Subroutine _coerce_paths: not called CC=5 SCAR=34.0
145 0 my ( $paths, $dlim ) = shift;
146 0 ▶ return () if ( !$paths );
Branch: 1 branch
BranchTrueFalseTotal
unless $paths 0 0 0
147 0 ▶ return @{$paths} if ( ref $paths eq 'ARRAY' );
Branch: 1 branch
BranchTrueFalseTotal
if ref $paths eq "ARRAY" 0 0 0
148  
149 # tweak delim to ignore C:/
150 0 ▶ unless ( defined $dlim ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $dlim) 0 0 0
151 0 ▶ $dlim = ( $isMS ) ? ':(?!\\/)' : ':';
Branch: 1 branch
BranchTrueFalseTotal
$isMS ? : 0 0 0
152 }
153 0 return split( /$dlim/, $paths );
154 }
155  
156  
157 12 ▶sub new {
Subroutine new: called CC=9 SCAR=29.5
Pod documented
158 12 my ($proto, $c) = @_;
159  
160 12 ▶ my $class = ref $proto || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
0120
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
161  
162 # default configuration
163 my $config = {
164 # DEFAULT VALUES
165  
166 # this the default internal implementation
167 # can be overwritten in application or class config
168 PROCESSOR => 'Catalyst::View::XSLT::XML::LibXSLT',
169  
170 # default file extension for xslt files
171 TEMPLATE_EXTENSION => '.xsl',
172  
173 # don't dump XSLT view config by default
174 DUMP_CONFIG => 0,
175  
176 # DEFAULT VALUES END
177  
178  
179 # global app config
180 12 ▶ %{ $c->config->{'View::XSLT'} || {} },
Condition50%1/2
$c->config->{'View::XSLT'} || {}
l!l
012
MC/DC0%0/1
$c->config->{'View::XSLT'} || {}
$c->config->{'View::XSLT'}
Truth table50%1/2
A
A= $c->config->{'View::XSLT'}
Aresult
11
00
181 12 ▶ %{ $c->config->{'V::XSLT'} || {} },
Condition50%1/2
$c->config->{'V::XSLT'} || {}
l!l
012
MC/DC0%0/1
$c->config->{'V::XSLT'} || {}
$c->config->{'V::XSLT'}
Truth table50%1/2
A
A= $c->config->{'V::XSLT'}
Aresult
11
00
182  
183 # class' config has precedence
184 12 ▶ %{ $class->config() || {} },
Condition50%1/2
$class->config || {}
l!l
120
MC/DC0%0/1
$class->config || {}
$class->config
Truth table50%1/2
A
A= $class->config
Aresult
11
00
185 };
186  
187  
188 12 ▶ if ( ! (ref $config->{INCLUDE_PATH} eq 'ARRAY') ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $config->{"INCLUDE_PATH"} eq "ARRAY") 0 12 12
189 0 my $delim = $config->{DELIMITER};
190 my @include_path
191 0 = _coerce_paths( $config->{INCLUDE_PATH}, $delim );
192 0 ▶ if ( !@include_path ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@include_path) 0 0 0
193 0 my $root = $c->config->{root};
194 0 my $base = Path::Class::dir( $root, 'base' );
195 0 @include_path = ( "$root", "$base" );
196 }
197 0 $config->{INCLUDE_PATH} = \@include_path;
198 }
199  
200 12 ▶ if ( $c->debug && $config->{DUMP_CONFIG} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->debug and $config->{"DUMP_CONFIG"}) 0 12 12
Condition33%1/3
$c->debug and $config->{"DUMP_CONFIG"}
!ll&&!rl&&r
1200
MC/DC0%0/2
$c->debug and $config->{"DUMP_CONFIG"}
$c->debug $config->{"DUMP_CONFIG"}
Truth table33%1/3
A and B
A= $c->debugB= $config->{"DUMP_CONFIG"}
ABresult
0X0
100
111
201 0 $c->log->debug( 'XSLT Config: ', Dumper($config) );
202 }
203  
204 12 my $self = {};
205  
206 12 bless($self, $class);
207  
208 12 $self->{CONFIG} = $config;
209  
210 12 return $self;
211 }
212  
213 =item render
214  
215 Renders the template specified via C<< $template >>.
216 The template parameters are set to C<%$args> if $args is a hashref, or
217 C<< $c->stash >> otherwise.
218  
219 Templates are accepted as strings, filehandles or objects of the corresponding
220 view types (L<XML::LibXML::Document> for example).
221  
222 =cut
223  
224 27 ▶sub render {
Subroutine render: called CC=28 SCAR=40.7
Pod documented
225 27 my ( $self, $c, $template, $args ) = @_;
226 27 my $basePath;
227  
228 27 ▶ unless ( $template =~ m/\</ || (ref($template) && $template->isa('GLOB')) || -e $template ||
Branch: 1 branch
BranchTrueFalseTotal
unless ($template =~ /\</ or ref $template and $template->isa("GLOB") or -e $template or ref $template and not $template->isa("Path::Class::File")) 23 4 27
Condition66%10/15
$template =~ /\</ or ref $template and $template->isa("GLOB") or -e $template or ref $template and not $template->isa("Path::Class::File")
l!l&&r!l&&!r
4023
$template =~ /\</ or ref $template and $template->isa("GLOB") or -e $template
l!l&&r!l&&!r
4023
$template =~ /\</ or ref $template and $template->isa("GLOB")
l!l&&r!l&&!r
2223
ref $template and $template->isa("GLOB")
!ll&&!rl&&r
2302
ref $template and not $template->isa("Path::Class::File")
!ll&&!rl&&r
2300
MC/DC33%2/6
$template =~ /\</ or ref $template and $template->isa("GLOB") or -e $template or ref $template and not $template->isa("Path::Class::File")
$template =~ /\</ ref $template $template->isa("GLOB") -e $template ref $template not $template->isa("Path::Class::File")
Truth table30%3/10
A or B and C or D or E and not(F)
A= $template =~ /\</B= ref $templateC= $template->isa("GLOB")D= -e $templateE= ref $templateF= not $template->isa("Path::Class::File")
ABCDEFresult
1XXXXX1
011XXX1
00X1XX1
0101XX1
00X0111
0100111
00X00X0
00X0100
01000X0
0100100
229 ( ref($template) && !$template->isa('Path::Class::File') ) ) {
230 23 my $error;
231  
232 23 ($basePath, $error) = $self->_searchInIncPath($c, $template);
233  
234 23 ▶ if (defined $error) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $error) { } 0 23 23
235 0 $c->error("Template [$template] does not exists in include path");
236 0 return 0;
237 } else {
238 23 $template = File::Spec->catfile($basePath, $template);
239 }
240 }
241  
242 27 ▶ unless ($basePath) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($basePath) 4 23 27
243 4 $basePath = $c->config->{root};
244 }
245  
246 my $vars = {
247 27 ▶ (ref $args eq 'HASH' ? %$args : %{ $c->stash() }),
Branch: 1 branch
BranchTrueFalseTotal
ref $args eq 'HASH' ? : 4 23 27
248 };
249  
250 27 ▶ unless (exists $vars->{ xml } && defined $vars->{ xml }) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $vars->{"xml"} and defined $vars->{"xml"}) 2 25 27
Condition66%2/3
exists $vars->{"xml"} and defined $vars->{"xml"}
!ll&&!rl&&r
0225
MC/DC50%1/2
exists $vars->{"xml"} and defined $vars->{"xml"}
exists $vars->{"xml"} defined $vars->{"xml"}
Truth table66%2/3
A and B
A= exists $vars->{"xml"}B= defined $vars->{"xml"}
ABresult
0X0
100
111
251 2 $c->log->error( 'No xml provided' );
252 2 return undef;
253 }
254  
255 25 my $xml = $vars->{xml};
256  
257 # if xml is not string (therefore is a file (what about file descriptors ?!))
258 # and is not existsting in the file system
259 25 ▶ unless ( $xml =~ m/\</ || (ref($template) && $xml->isa('GLOB')) || -e $xml ||
Branch: 1 branch
BranchTrueFalseTotal
unless ($xml =~ /\</ or ref $template and $xml->isa("GLOB") or -e $xml or ref $xml and not $xml->isa("Path::Class::File")) 2 23 25
Condition60%9/15
$xml =~ /\</ or ref $template and $xml->isa("GLOB") or -e $xml or ref $xml and not $xml->isa("Path::Class::File")
l!l&&r!l&&!r
2302
$xml =~ /\</ or ref $template and $xml->isa("GLOB") or -e $xml
l!l&&r!l&&!r
1942
$xml =~ /\</ or ref $template and $xml->isa("GLOB")
l!l&&r!l&&!r
1906
ref $template and $xml->isa("GLOB")
!ll&&!rl&&r
600
ref $xml and not $xml->isa("Path::Class::File")
!ll&&!rl&&r
020
MC/DC33%2/6
$xml =~ /\</ or ref $template and $xml->isa("GLOB") or -e $xml or ref $xml and not $xml->isa("Path::Class::File")
$xml =~ /\</ ref $template $xml->isa("GLOB") -e $xml ref $xml not $xml->isa("Path::Class::File")
Truth table30%3/10
A or B and C or D or E and not(F)
A= $xml =~ /\</B= ref $templateC= $xml->isa("GLOB")D= -e $xmlE= ref $xmlF= not $xml->isa("Path::Class::File")
ABCDEFresult
1XXXXX1
011XXX1
00X1XX1
0101XX1
00X0111
0100111
00X00X0
00X0100
01000X0
0100100
260 ( ref($xml) && !$xml->isa('Path::Class::File') ) ) {
261 2 my ($incPath, $error) = $self->_searchInIncPath($c, $xml);
262  
263 2 ▶ if (defined $error) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $error) { } 0 2 2
264 0 $c->error("XML file [$xml] does not exists in include path");
265 0 return undef;
266 } else {
267 2 $vars->{xml} = File::Spec->catfile($incPath, $xml);
268 }
269 }
270  
271 25 ▶ $c->log->debug( qq{Rendering template "$template"} ) if $c->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $c->debug 0 25 25
272  
273 # add runtime register_function(s) from stash
274 25 ▶ if (exists $vars->{additional_register_function} &&
Branch: 1 branch
BranchTrueFalseTotal
if (exists $vars->{"additional_register_function"} and ref $vars->{"additional_register_function"} eq "ARRAY") 1 24 25
Condition66%2/3
exists $vars->{"additional_register_function"} and ref $vars->{"additional_register_function"} eq "ARRAY"
!ll&&!rl&&r
2401
MC/DC50%1/2
exists $vars->{"additional_register_function"} and ref $vars->{"additional_register_function"} eq "ARRAY"
exists $vars->{"additional_register_function"} ref $vars->{"additional_register_function"} eq "ARRAY"
Truth table66%2/3
A and B
A= exists $vars->{"additional_register_function"}B= ref $vars->{"additional_register_function"} eq "ARRAY"
ABresult
0X0
100
111
275 ref($vars->{additional_register_function}) eq 'ARRAY' ) {
276 1 my @additional_subrefs = @{ $vars->{additional_register_function} };
277 1 delete $vars->{additional_register_function};
278  
279 1 ▶ unless (ref($self->{CONFIG}->{LibXSLT}->{register_function}) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $self->{"CONFIG"}{"LibXSLT"}{"register_function"} eq "ARRAY") 1 0 1
280 1 $self->{CONFIG}->{LibXSLT}->{register_function} = [];
281 }
282  
283 unshift(
284 1 @{ $self->{CONFIG}->{LibXSLT}->{register_function} },
285 @additional_subrefs
286 );
287 }
288  
289 25 my $processor = undef;
290 25 eval {
291 25 $processor = $self->_getProcessor()->new($c, $self->{CONFIG}->{LibXSLT});
292 };
293  
294 25 ▶ if ($@ && (! defined $processor)) {
Branch: 2 branches
BranchTrueFalseTotal
if ($@ and not defined $processor) { } 0 25 25
elsif (scalar @{$c->error;}) { } 25 0 25
Condition33%1/3
$@ and not defined $processor
!ll&&!rl&&r
2500
MC/DC0%0/2
$@ and not defined $processor
$@ not defined $processor
Truth table33%1/3
A and not(B)
A= $@B= not defined $processor
ABresult
0X0
100
111
295 0 $c->error("Could not instanciate XSLT processor: $@");
296 0 return undef;
297 25 } elsif (scalar @{$c->error}) {
298 25 return undef;
299 }
300  
301 0 ▶ $c->log->debug("Processing...") if $c->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $c->debug 0 0 0
302 0 my ($output, $error) = $processor->process($template, $vars, $basePath);
303  
304 0 ▶ if ($error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error) { } 0 0 0
305 0 chomp $error;
306 0 $error = qq{Couldn't render template "$template". Error: "$error"};
307 0 $c->error($error);
308 0 return undef;
309 }
310 else {
311 0 return $output;
312 }
313 }
314  
315 =item process
316  
317 Renders the template specified in C<< $c->stash->{template} >> or C<<
318 $c->action >>. Calls C<< render >> to perform actual rendering.
319 Template params are set up from the contents of C<< $c->stash >>.
320 Output is stored in C<< $c->response->body >>.
321  
322 =cut
323  
324 23 ▶sub process {
Subroutine process: called CC=5 SCAR=16.6
Pod documented
325 23 my ( $self, $c ) = @_;
326  
327 23 my $template = undef;
328  
329 23 ▶ if (exists $c->stash->{template} && defined $c->stash->{template}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $c->stash->{'template'} and defined $c->stash->{'template'}) { } 19 4 23
Condition66%2/3
exists $c->stash->{"template"} and defined $c->stash->{"template"}
!ll&&!rl&&r
4019
MC/DC50%1/2
exists $c->stash->{"template"} and defined $c->stash->{"template"}
exists $c->stash->{"template"} defined $c->stash->{"template"}
Truth table66%2/3
A and B
A= exists $c->stash->{"template"}B= defined $c->stash->{"template"}
ABresult
0X0
100
111
330 19 $template = delete $c->stash->{template};
331 } else {
332 4 my $actionName = $c->action;
333 4 my $ext = $self->{CONFIG}->{'TEMPLATE_EXTENSION'};
334  
335 4 $template = $actionName . $ext;
336 4 ▶ $c->log->debug( "Going to create template name from the action name and default extension: [$template]" ) if $c->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $c->debug 0 4 4
337 }
338  
339 23 ▶ unless ($template) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($template) 0 23 23
340 # probably this will never happen, but for any case
341 0 $c->log->error( 'No template specified for rendering' );
342 0 return 0;
343 }
344  
345 23 my $output = $self->render($c, $template);
346  
347 23 $c->response->body($output);
348  
349 23 return 1;
350 }
351  
352 # INTERNAL METHODS
353  
354 # returns the current set internal processor
355 25 ▶sub _getProcessor {
Subroutine _getProcessor: called CC=1 SCAR=0.0
356 25 my ($self) = @_;
357  
358 25 return $self->{CONFIG}->{PROCESSOR};
359 }
360  
361 # searchs for a file ($filename) in INCLUDE_PATH
362 # returns the first occurence
363 25 ▶sub _searchInIncPath {
Subroutine _searchInIncPath: called CC=8 SCAR=23.9
364 25 my ($self, $c, $filename) = @_;
365  
366 25 ▶ $c->log->debug( "searching in include path for [$filename]") if $c->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $c->debug 0 25 25
367  
368 25 my $arefIncludePath = $self->{CONFIG}->{'INCLUDE_PATH'};
369  
370 25 ▶ if (ref $c->stash->{additional_template_paths} eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $c->stash->{"additional_template_paths"} eq "ARRAY") 0 25 25
371 0 unshift( @{ $arefIncludePath },
372 0 @{ $c->stash->{additional_template_paths} } );
373 }
374  
375 25 foreach my $incEntry ( @{ $arefIncludePath} ) {
376  
377 25 ▶ $c->log->debug( "Going to search for file [$filename] in [$incEntry]" ) if $c->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $c->debug 0 25 25
378 25 my $tmpTemplateName = '';
379 25 my $incPath = '';
380  
381 25 ▶ if (ref $incEntry eq 'Path::Class::File') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $incEntry eq 'Path::Class::File') { } 0 25 25
382 0 $incPath = $incEntry->absolute();
383 } else {
384 25 $incPath = $incEntry;
385 }
386  
387 25 ▶ if (-e File::Spec->catfile($incPath, $filename)) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e "File::Spec"->catfile($incPath, $filename)) 25 0 25
388  
389 25 ▶ $c->log->debug( "File [$filename] found in [$incEntry]") if $c->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $c->debug 0 25 25
390 25 return ($incPath, undef);
391 }
392  
393 }
394  
395 0 return (undef, 1);
396 }
397  
398 =back
399  
400 =head1 NOTE
401  
402 This version works only with L<XML::LibXSLT>.
403  
404 =head1 SEE ALSO
405  
406 L<Catalyst>, L<Catalyst::Base>, L<XML::LibXSLT>
407  
408 =head1 AUTHORS
409  
410 Martin Grigorov, E<lt>mcgregory {at} e-card {dot} bgE<gt>
411  
412 Simon Bertrang, E<lt>simon.bertrang@puzzworks.comE<gt>
413  
414 =head1 COPYRIGHT
415  
416 This program is free software, you can redistribute it and/or modify it
417 under the same terms as Perl itself.
418  
419 =cut
420  
421 13 1;
« View/XSLT/XML/LibXSLT.pm ↑ Summary Helper/View/XSLT.pm »