XPathScript/Template.pm

filter: Statementstmt 90.0% 90 / 100 Branchbran 59.0% 13 / 22 Conditioncond 62.9% 17 / 27 MC/DCmcdc 33.3% 5 / 15 Subroutinesub 92.0% 23 / 25 Podpod 100% 11 / 11 totaltotal 79.5% 159 / 200 CC 25 SCAR 14.8
CC
25
Cov
81%
clear
12.0
set
7.9
import_template
7.6
CRAP 4.4
« XPathScript/Processor.pm ↑ Summary XPathScript/Processor/B.pm »
1 package XML::XPathScript::Template;
2 34 our $AUTHORITY = 'cpan:YANICK';
3 # ABSTRACT: XML::XPathScript transformation template
4 34 $XML::XPathScript::Template::VERSION = '2.00';
5 34 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 34 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 34 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 34 ▶use Scalar::Util qw/ reftype /;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 34 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 34 ▶use XML::XPathScript::Template::Tag;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 34 ▶use Clone qw/ clone /;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 34 ▶use Scalar::Util qw/ refaddr /;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 34 ▶use overload '&{}' => \&_overload_func,
Subroutine BEGIN: called CC=1 SCAR=0.0
16 34 q{""} => \&_overload_quote;
17  
18 86 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
19 86 my( $class ) = @_;
20  
21 86 my $self = {};
22 86 bless $self, $class;
23  
24 86 return $self;
25 }
26  
27 57 ▶sub set { ##no critic
Subroutine set: called CC=7 SCAR=20.7
Pod documented
28 57 ▶ croak "method set called with more than two arguments" if @_ > 3;
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 3 0 57 57
29  
30 57 my( $self, $tag, $attribute_ref ) = @_;
31  
32 57 my $type = reftype $tag;
33 my @templates = # templates to change
34 !$type ? $self->{$tag}
35 ||= new XML::XPathScript::Template::Tag
36 57 ▶ : $type eq 'ARRAY' ? map { $self->{$_}
Branch: 2 branches
BranchTrueFalseTotal
!$type ? : 50 7 57
$type eq 'ARRAY' ? : 7 0 7
Condition50%3/6
$self->{$tag} ||= "XML::XPathScript::Template::Tag"->new
l!l&&r!l&&!r
6440
$self->{$_} ||= "XML::XPathScript::Template::Tag"->new
l!l&&r!l&&!r
0510
MC/DC0%0/2
$self->{$tag} ||= "XML::XPathScript::Template::Tag"->new
$self->{$tag} "XML::XPathScript::Template::Tag"->new
MC/DC0%0/2
$self->{$_} ||= "XML::XPathScript::Template::Tag"->new
$self->{$_} "XML::XPathScript::Template::Tag"->new
Truth table66%2/3
A ||= B
A= $self->{$tag}B= "XML::XPathScript::Template::Tag"->new
ABresult
1X1
011
000
Truth table33%1/3
A ||= B
A= $self->{$_}B= "XML::XPathScript::Template::Tag"->new
ABresult
1X1
011
000
37 ||= new XML::XPathScript::Template::Tag
38 } @$tag
39 : croak "tag cannot be of type $type"
40 ;
41  
42 57 $_->set( $attribute_ref ) for @templates;
43  
44 57 return;
45 }
46  
47 3 ▶sub copy {
Subroutine copy: called CC=3 SCAR=11.0
Pod documented
48 3 my( $self, $src, $copy, $attributes_ref ) = @_;
49  
50 croak "tag $src not found in template"
51 3 ▶ unless $self->{$src};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{$src} 0 3 3
52  
53 3 my %attributes = %{ $self->{$src} };
54 3 ▶ %attributes = map { $_ => $attributes{ $_ } }@$attributes_ref
Branch: 1 branch
BranchTrueFalseTotal
if $attributes_ref 1 2 3
55 if $attributes_ref;
56
57 3 $self->set( $copy, \%attributes );
58  
59 3 return;
60 }
61  
62 2 ▶sub alias {
Subroutine alias: called CC=3 SCAR=11.0
Pod documented
63 2 my( $self, $src, $copy ) = @_;
64  
65 2 ▶ $self->{$_} = $self->{$src} for ref( $copy ) ? @$copy : $copy;
Branch: 1 branch
BranchTrueFalseTotal
ref $copy ? : 1 1 2
66  
67 2 return;
68 }
69  
70  
71 1 ▶sub dump { ##no critic
Subroutine dump: called CC=2 SCAR=7.0
Pod documented
72 1 my( $self, @tags ) = @_;
73
74 1 my %template = %{$self};
75
76 1 ▶ @tags = keys %template unless @tags;
Branch: 1 branch
BranchTrueFalseTotal
unless @tags 1 0 1
77
78 1 %template = map { $_ => $template{ $_ } } @tags;
79
80 1 return Data::Dumper->Dump( [ \%template ], [ 'template' ] );
81 }
82  
83 0 ▶sub clear {
Subroutine clear: not called CC=3 SCAR=24.8
Pod documented
84 0 my( $self, $tags ) = @_;
85  
86 0 ▶ delete $self->{ $_ } for $tags
Branch: 1 branch
BranchTrueFalseTotal
$tags ? : 0 0 0
87 ? @$tags
88 0 : grep { !/^:/ } keys %$self; ##no critic
89 0 return;
90 }
91  
92  
93 2 ▶sub is_alias {
Subroutine is_alias: called CC=2 SCAR=6.9
Pod documented
94 2 my( $self, $tag ) = @_;
95  
96 2 my $id = $self->{$tag};
97  
98 my @aliases = grep { $_ ne $tag
99 14 ▶ and refaddr( $self->{$_} ) eq refaddr( $id ) }
Condition100%3/3
$_ ne $tag && builtin::refaddr($self->{$_}) eq builtin::refaddr($id)
!ll&&!rl&&r
2102
MC/DC100%2/2
$_ ne $tag && builtin::refaddr($self->{$_}) eq builtin::refaddr($id)
$_ ne $tag builtin::refaddr($self->{$_}) eq builtin::refaddr($id)
Truth table100%3/3
A && B
A= $_ ne $tagB= builtin::refaddr($self->{$_}) eq builtin::refaddr($id)
ABresult
0X0
100
111
100 2 keys %{$self};
101  
102 2 return @aliases;
103 }
104  
105 1 ▶sub unalias {
Subroutine unalias: called CC=1 SCAR=0.0
Pod documented
106 1 my( $self, $tag ) = @_;
107  
108 1 my $fresh = new XML::XPathScript::Template::Tag;
109  
110 1 $fresh->set( $self->{$tag} );
111  
112 1 $self->{$tag} = $fresh;
113  
114 1 return;
115 }
116  
117 2 ▶sub namespace {
Subroutine namespace: called CC=2 SCAR=8.1
Pod documented
118 2 my( $self, $namespace ) = @_;
119  
120 2 ▶ return $self->{ ":$namespace" } ||= new XML::XPathScript::Template;
Condition33%1/3
$$self{":$namespace"} ||= "XML::XPathScript::Template"->new
l!l&&r!l&&!r
020
MC/DC0%0/2
$$self{":$namespace"} ||= "XML::XPathScript::Template"->new
$$self{":$namespace"} "XML::XPathScript::Template"->new
Truth table33%1/3
A ||= B
A= $$self{":$namespace"}B= "XML::XPathScript::Template"->new
ABresult
1X1
011
000
121 }
122  
123 1617 ▶sub resolve {
Subroutine resolve: called CC=6 SCAR=18.3
Pod documented
124 1617 my $template = shift;
125 1617 ▶ my( $namespace, $tag ) = @_ == 2 ? @_ : ( undef, @_ );
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 ? : 1617 0 1617
126  
127 34 ▶ no warnings qw/ uninitialized /;
Subroutine BEGIN: called CC=1 SCAR=0.0
128 1617 $namespace = ':'.$namespace;
129  
130 return ( ( $template->{$namespace} && # selection order
131 ( $template->{$namespace}{$tag} # foo:bar
132 || $template->{$namespace}{'*'} ) ) # foo:*
133 || $template->{$tag} # bar
134 1617 ▶ || $template->{'*'} ); # *
Condition75%9/12
$template->{$namespace} && ($template->{$namespace}{$tag} || $template->{$namespace}{'*'}) || $template->{$tag} || $template->{'*'}
l!l&&r!l&&!r
1522788
$template->{$namespace} and $template->{$namespace}{$tag} || $template->{$namespace}{"*"} or $template->{$tag}
l!l&&r!l&&!r
2152095
$template->{$namespace} and $template->{$namespace}{$tag} || $template->{$namespace}{"*"}
!ll&&!rl&&r
161502
$template->{$namespace}{$tag} or $template->{$namespace}{"*"}
l!l&&r!l&&!r
200
MC/DC60%3/5
$template->{$namespace} && ($template->{$namespace}{$tag} || $template->{$namespace}{'*'}) || $template->{$tag} || $template->{'*'}
$template->{$namespace} $template->{$namespace}{$tag} $template->{$namespace}{"*"} $template->{$tag} $template->{'*'}
Truth table50%4/8
A and B or C or D || E
A= $template->{$namespace}B= $template->{$namespace}{$tag}C= $template->{$namespace}{"*"}D= $template->{$tag}E= $template->{'*'}
ABCDEresult
11XXX1
101XX1
0XX1X1
1001X1
0XX011
100011
0XX000
100000
135 # (and undef if nothing)
136 }
137  
138 1 ▶sub import_template {
Subroutine import_template: called CC=5 SCAR=20.3
Pod documented
139 1 my( $self, $other_template ) = @_;
140  
141 1 ▶ carp "incorrect call for import_template(): no argument or is not a template"
Branch: 1 branch
BranchTrueFalseTotal
unless $other_template and $other_template =~ /HASH/ 0 1 1
Condition33%1/3
$other_template and $other_template =~ /HASH/
!ll&&!rl&&r
001
MC/DC0%0/2
$other_template and $other_template =~ /HASH/
$other_template $other_template =~ /HASH/
Truth table33%1/3
A and B
A= $other_templateB= $other_template =~ /HASH/
ABresult
0X0
100
111
142 unless $other_template and $other_template =~ /HASH/;
143  
144 1 for my $k ( keys %$other_template ) {
145 1 ▶ if ( 0 == index $k, ':' ) { # it's a namespace
Branch: 1 branch
BranchTrueFalseTotal
if (0 == index($k, ':')) { } 0 1 1
146 0 my $ns = $k;
147 0 $ns =~ s/^://;
148 0 my $subtemplate = $self->namespace( $ns );
149 0 $subtemplate->import( $other_template->{$k} );
150 }
151 else { # it's a regular tag
152 1 $self->set( $k => $other_template->{$k} );
153 }
154 }
155  
156 1 return;
157 }
158  
159 1 ▶sub _overload_func {
Subroutine _overload_func: called CC=1 SCAR=0.0
160 1 my $self = shift;
161 1 ▶ return sub { $self->set( @_ ) }
Subroutine __ANON__: called CC=1 SCAR=0.0
162 1 }
163  
164 5 ▶sub _overload_quote {
Subroutine _overload_quote: called CC=1 SCAR=1.2
165 5 my $self = shift;
166 5 return $self;
167 0 ▶ return sub { $self };
Subroutine __ANON__: not called CC=1 SCAR=6.9
168 }
169  
170 34 1;
171  
172 =pod
173  
174 =encoding UTF-8
175  
176 =head1 NAME
177  
178 XML::XPathScript::Template - XML::XPathScript transformation template
179  
180 =head1 VERSION
181  
182 version 2.00
183  
184 =head1 SYNOPSIS
185  
186 <%
187 $t->set( 'important' => { 'pre' => '<blink>',
188 'post' => '</blink>',
189 'prechild' => '<u>',
190 'postchild' => '</u>',
191 } );
192  
193 # urgent and annoying share the 'pre' and 'post'
194 # of important
195 $t->copy( 'important' => [ qw/ urgent annoying / ],
196 [ qw/ pre post / ], );
197  
198 # redHot is a synonym of important
199 $t->alias( 'important' => 'redHot' );
200  
201 %>
202 <%= apply_templates() %>
203  
204 =head1 DESCRIPTION
205  
206 A stylesheet's template defines the transformations and actions that
207 are performed on the tags of a document as they are processed.
208  
209 The template of a stylesheet can be accessed via variables
210 I<$t>, I<$template> and I<$XML::XPathScript::trans>.
211  
212 =head1 METHODS
213  
214 =head2 new
215  
216 $template = XML::XPathScript::Template->new
217  
218 Creates and returns a new, empty template.
219  
220 =head2 set
221  
222 $template->set( $tag, \%attributes )
223 $template->set( \@tags , \%attributes )
224  
225 Updates the $tag or @tags in the template with the
226 given %attributes.
227  
228 Thank to the magic of overloading, using the $template
229 as a code reference acts as a shortcut to I<set>.
230  
231 Example:
232  
233 $template->set( 'foo' => { pre => '<a>', post => '</a>' } );
234 # or, if you prefer,
235 $template->( 'foo' => { pre => '<a>', post => '</a>' } );
236  
237 =head2 copy
238  
239 $template->copy( $original_tag, $copy_tag );
240 $template->copy( $original_tag, $copy_tag, \@attributes );
241 $template->copy( $original_tag, \@copy_tags );
242 $template->copy( $original_tag, \@copy_tags, \@attributes );
243  
244 Copies all attributes (or a subset of them if @attributes is given)
245 of $original_tag to $copy_tag.
246  
247 Note that subsequent modifications of the original tag will not
248 affect the copies. To bind several tags to the same behavior, see
249 L<alias>.
250  
251 Example:
252  
253 # copy the attributes 'pre' and 'post' of important
254 # to 'urgent' and 'redHot'
255 $template->copy( 'important' => [ qw/ urgent redHot / ],
256 [ qw/ pre post / ] );
257  
258 =head2 import_template
259  
260 $template->import_template( $other_template )
261  
262 Imports another template into the current one.
263  
264 =head2 alias
265  
266 $template->alias( $original_tag => $alias_tag )
267 $template->alias( $original_tag => \@alias_tags )
268  
269 Makes the target tags aliases to the original tag. Further
270 modifications that will be done on any of these tags will
271 be reflected on all others.
272  
273 Example:
274  
275 $template->alias( 'foo' => 'bar' );
276
277 # also modifies 'foo'
278 $template->set( 'bar' => { pre => '<u>' } );
279  
280 =head2 is_alias
281  
282 @aliases = $template->is_alias( $tag )
283  
284 Returns all tags that are aliases to $tag.
285  
286 =head2 unalias
287  
288 $template->unalias( $tag )
289  
290 Unmerge $tag of its aliases, if it has any. Further modifications to
291 $tag will not affect the erstwhile aliases, and vice versa.
292  
293 Example:
294  
295 $template->alias( 'foo' => [ qw/ bar baz / ] );
296 $template->set( 'foo' => { pre => '<a>' } ); # affects foo, bar and baz
297 $template->unalias( 'bar' );
298 $template->set( 'bar' => { pre => '<c>' } ); # affects only bar
299 $template->set( 'baz' => { pre => '<b>' } ); # affects foo and baz
300  
301 =head2 clear
302  
303 $template->clear()
304 $template->clear( \@tags )
305  
306 Delete all tags, or those given by @tags, from the template.
307  
308 Example:
309  
310 $template->clear([ 'foo', 'bar' ]);
311  
312 =head2 dump
313  
314 $template->dump()
315 $template->dump( @tags )
316  
317 Returns a pretty-printed dump of the templates. If @tags are
318 specified, only return their templates.
319  
320 Example:
321  
322 <%= $template->dump( 'foo' ) %>
323
324 # will yield something like
325 #
326 # $template = {
327 # foo => {
328 # post => '</bar>',
329 # pre => '<bar>',
330 # }
331 # };
332  
333 =head2 namespace
334  
335 my $subtemplate = $template->namespace( $uri );
336  
337 Returns the sub-template associated to the namespace defined by $uri.
338  
339 Example:
340  
341 $template->set( 'foo' => { 'pre' => 'within default namespace' } );
342 my $subtemplate = $template->namespace( 'http://www.www3c.org/blah/' );
343 $subtemplate->set( 'foo' => { 'pre' => "within 'blah' namespace" } );
344  
345 =head2 resolve
346  
347 $tag = $template->resolve( $namespace, $tagname );
348 $tag = $template->resolve( $tagname );
349  
350 Returns the tag object within $template that matches $namespace and
351 $tagname best. The returned match is the first one met in the following
352 list:
353  
354 =over
355  
356 =item *
357  
358 $namespace:$tagname
359  
360 =item *
361  
362 $namespace:*
363  
364 =item *
365  
366 $tagname
367  
368 =item *
369  
370 *
371  
372 =item *
373  
374 undef
375  
376 =back
377  
378 Example:
379  
380 $template->set( foo => { pre => 'a' } );
381 $template->set( '*' => { pre => 'b' } );
382 $template->namespace( 'http://blah' )->set( foo => { pre => 'c' } );
383 $template->namespace( 'http://blah' )->set( '*' => { pre => 'd' } );
384  
385 $template->resolve( 'foo' )->get( 'pre' ); # returns 'a'
386 $template->resolve( 'baz' )->get( 'pre' ); # returns 'b'
387 $template->resolve( 'http://meeh', 'foo' )->get( 'pre' ); # returns 'a'
388 $template->resolve( 'http://blah', 'foo' )->get( 'pre' ); # returns 'c'
389 $template->resolve( 'http://blah', 'baz' )->get( 'pre' ); # returns 'd'
390  
391 =head1 BACKWARD COMPATIBILITY
392  
393 Prior to version 1.0 of XML::XPathScript, the template of a
394 stylesheet was not an object but a simple hash reference. Modifications
395 to the template were done by manipulating the hash directly.
396  
397 <%
398 # pre-1.0 way of manipulating the template
399 $t->{important}{pre} = '<blink>';
400 $t->{important}{post} = '</blink>';
401
402 for my $tag ( qw/ urgent redHot / ) {
403 for my $attr ( qw/ pre post / ) {
404 $t->{$tag}{$attr} = $t->{important}{$attr};
405 }
406 }
407  
408 $t->{ alert } = $t->{ important };
409 %>
410  
411 Don't tell anyone, but as an XML::XPathScript::Template is
412 a blessed hash reference this way of doing things will
413 still work. However, direct manipulation of the template's hash
414 is deprecated. Instead, it is recommended to use the object's
415 access methods.
416  
417 <%
418 # correct way to manipulate the template
419 $t->set( important => { pre => '<blink>',
420 post => '</blink>',
421 showtag => 1
422 } );
423  
424 $t->copy( important => [ qw/ urgent redHot / ], [ qw/ pre post / ] );
425  
426 $t->alias( important => alert );
427 %>
428  
429 =head1 AUTHORS
430  
431 =over 4
432  
433 =item *
434  
435 Yanick Champoux <yanick@cpan.org>
436  
437 =item *
438  
439 Dominique Quatravaux <domq@cpan.org>
440  
441 =item *
442  
443 Matt Sergeant <matt@sergeant.org>
444  
445 =back
446  
447 =head1 COPYRIGHT AND LICENSE
448  
449 This software is copyright (c) 2019, 2018, 2008, 2007 by Matt Sergeant.
450  
451 This is free software; you can redistribute it and/or modify it under
452 the same terms as the Perl 5 programming language system itself.
453  
454 =cut
455  
456 __END__
« XPathScript/Processor.pm ↑ Summary XPathScript/Processor/B.pm »