XPathScript/Processor.pm

filter: Statementstmt 85.1% 286 / 336 Branchbran 67.6% 111 / 164 Conditioncond 61.5% 40 / 65 MC/DCmcdc 40.0% 16 / 40 Subroutinesub 85.7% 54 / 63 Podpod 67.6% 23 / 34 totaltotal 75.5% 530 / 702 CC 115 SCAR 23.4
CC
115
Cov
77%
call_template
42.0
get_xpath_of_node
28.9
translate_element_node
20.0
CRAP 10.4
« XPathScript.pm ↑ Summary XPathScript/Template.pm »
1 package XML::XPathScript::Processor;
2 30 our $AUTHORITY = 'cpan:YANICK';
3 # ABSTRACT: XML::XPathScript transformation engine
4 30 $XML::XPathScript::Processor::VERSION = '2.00';
5 30 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 30 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 30 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 30 ▶use base qw/ Exporter /;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 30 ▶use XML::XPathScript::Template;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 30 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 30 our @EXPORT = qw/
15 $DO_SELF_AS_CHILD
16 $DO_SELF_AND_KIDS
17 $DO_SELF_ONLY
18 $DO_NOT_PROCESS
19 DO_SELF_AND_KIDS
20 DO_SELF_ONLY
21 DO_NOT_PROCESS
22 DO_TEXT_AS_CHILD
23 /;
24  
25 30 our @EXPORT_OK = qw(
26 $DO_SELF_AS_CHILD
27 $DO_SELF_AND_KIDS
28 $DO_SELF_ONLY
29 $DO_NOT_PROCESS
30 DO_SELF_AND_KIDS
31 DO_SELF_ONLY
32 DO_NOT_PROCESS
33 DO_TEXT_AS_CHILD
34 processor
35 findnodes
36 findvalue
37 findvalues
38 findnodes_as_string
39 xpath_to_string
40 apply_templates
41 matches
42 set_namespace
43 is_element_node
44 is_text_node
45 is_comment_node
46 is_pi_node
47 is_nodelist
48 is_utf8_tainted
49 get_xpath_of_node
50 set_dom
51 get_dom
52 get_parser
53 enable_binmode
54 set_binmode
55 get_binmode
56 set_template
57 get_template
58 set_interpolation
59 get_interpolation
60 set_interpolation_regex
61 get_interpolation_regex
62 );
63  
64 30 ▶use constant DO_TEXT_AS_CHILD => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
65 30 ▶use constant DO_SELF_AND_KIDS => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
66 30 ▶use constant DO_SELF_ONLY => -1;
Subroutine BEGIN: called CC=1 SCAR=0.0
67 30 ▶use constant DO_NOT_PROCESS => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
68  
69 30 our( $DO_SELF_AS_CHILD, $DO_SELF_AND_KIDS,
70 $DO_SELF_ONLY, $DO_NOT_PROCESS );
71 30 Readonly::Scalar $DO_SELF_AS_CHILD => 2;
72 30 Readonly::Scalar $DO_SELF_AND_KIDS => 1;
73 30 Readonly::Scalar $DO_SELF_ONLY => -1;
74 30 Readonly::Scalar $DO_NOT_PROCESS => 0;
75  
76  
77 64 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
78 64 my $class = shift;
79 64 my $self = {};
80 64 bless $self, $class;
81 64 return $self;
82 # $XML::XPathScript::xp => {doc}
83 # {parser}
84 # {binmode}
85 # {template}
86 # $XML::XPathScript::current->interpolating() {is_interpolating}
87 # $XML::XPathScript::current->{interpolation_regex} {interpolation_regex}
88 }
89  
90 #### accessors #############################################
91
92 73 ▶sub processor {
Subroutine processor: called CC=1 SCAR=0.0
Pod documented
93 73 return $_[0];
94 }
95  
96 77 ▶sub set_dom {
Subroutine set_dom: called CC=4 SCAR=17.2
Pod documented
97 77 my( $self, $dom ) = @_;
98 77 ▶ my $class = ref( $self->{dom} = $dom )
Branch: 1 branch
BranchTrueFalseTotal
my $class = ref($self->{"dom"} = $dom) or croak("usage: \$processor->set_dom( \$dom )") 77 0 77
99 or croak "usage: \$processor->set_dom( \$dom )";
100  
101 77 ▶ if ( $class =~ /((?:XML::)(?:LibXML|XPath))/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($class =~ /((?:XML::)(?:LibXML|XPath))/) { } 77 0 77
elsif ($class =~ /B::XPath/) { } 0 0 0
102 77 $self->set_parser( $1 );
103 }
104 elsif ( $class =~ /B::XPath/ ) {
105 0 $self->set_parser( 'B::XPath' );
106 }
107 else {
108 0 die "no parser assigned to class $class\n";
109 }
110  
111  
112 77 return;
113 }
114  
115 77 ▶sub set_parser {
Subroutine set_parser: called CC=4 SCAR=20.7
Pod undocumented
116 77 my ( $self, $parser ) = @_;
117  
118 77 $self->{parser} = $parser;
119 77 ▶ if ( $parser eq 'XML::LibXML' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($parser eq 'XML::LibXML') { } 77 0 77
elsif ($parser eq 'XML::XPath') { } 0 0 0
elsif ($parser eq 'B::XPath') { } 0 0 0
120 77 require XML::XPathScript::Processor::LibXML;
121 77 bless $self, 'XML::XPathScript::Processor::LibXML';
122 }
123 elsif ( $parser eq 'XML::XPath' ) {
124 0 require XML::XPathScript::Processor::XPath;
125 0 bless $self, 'XML::XPathScript::Processor::XPath';
126 }
127 elsif ( $parser eq 'B::XPath' ) {
128 0 require XML::XPathScript::Processor::B;
129 0 bless $self, 'XML::XPathScript::Processor::B';
130 }
131 else {
132 0 die "parser $parser not supported\n";
133 }
134  
135 }
136  
137 0 ▶sub get_dom { $_[0]->{dom} }
Subroutine get_dom: not called CC=1 SCAR=6.9
Pod documented
138 2 ▶sub get_parser { $_[0]->{parser} }
Subroutine get_parser: called CC=1 SCAR=0.0
Pod documented
139 4 ▶sub enable_binmode { $_[0]->{binmode} = 1 }
Subroutine enable_binmode: called CC=1 SCAR=0.0
Pod documented
140 1 ▶sub set_binmode { $_[0]->enable_binmode; }
Subroutine set_binmode: called CC=1 SCAR=0.0
Pod undocumented
141 0 ▶sub get_binmode { $_[0]->{binmode} }
Subroutine get_binmode: not called CC=1 SCAR=6.9
Pod documented
142 7 ▶sub set_template { $_[0]->{template} = $_[1] }
Subroutine set_template: called CC=1 SCAR=0.0
Pod documented
143 0 ▶sub get_template { $_[0]->{template} }
Subroutine get_template: not called CC=1 SCAR=6.9
Pod documented
144 70 ▶sub set_interpolation { $_[0]->{is_interpolating} = $_[1] }
Subroutine set_interpolation: called CC=1 SCAR=0.0
Pod documented
145 4 ▶sub get_interpolation { $_[0]->{is_interpolating} }
Subroutine get_interpolation: called CC=1 SCAR=0.0
Pod documented
146 65 ▶sub set_interpolation_regex { $_[0]->{interpolation_regex} = $_[1] }
Subroutine set_interpolation_regex: called CC=1 SCAR=0.0
Pod documented
147 0 ▶sub get_interpolation_regex { $_[0]->{interpolation_regex} }
Subroutine get_interpolation_regex: not called CC=1 SCAR=6.9
Pod documented
148  
149  
150 # $processor->import_functional( $prefix )
151 # XML::XPathScript::Processor->import_functional( $prefix )
152  
153 73 ▶sub import_functional {
Subroutine import_functional: called CC=4 SCAR=13.9
Pod documented
154 73 my( $self, $prefix ) = @_;
155  
156 73 ▶ $self or croak "import_functional not called properly";
Branch: 1 branch
BranchTrueFalseTotal
$self or croak("import_functional not called properly") 73 0 73
157
158 # call as XML::XPathScript::Processor->import_functional
159 73 ▶ $self = XML::XPathScript::Processor->new unless ref $self;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 1 72 73
160  
161 73 my($caller) = caller;
162  
163 73 $self->_export( $caller, $_, $prefix ) for @EXPORT_OK;
164  
165 73 return;
166 }
167  
168 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
169  
170 2628 ▶sub _export {
Subroutine _export: called CC=7 SCAR=22.2
171 # heavily inspired by David James' Class::Exporter
172 # (which is a nice way to say I stole it and
173 # twisted it to my own perverted needs)
174
175 30 ▶ no strict qw/ refs /;
Subroutine BEGIN: called CC=1 SCAR=0.0
176 30 ▶ no warnings qw/ uninitialized /;
Subroutine BEGIN: called CC=1 SCAR=0.0
177  
178 2628 my( $self, $caller, $export, $prefix ) = @_;
179  
180 2628 ▶ my $type = $export =~ s/^(\W)// ? $1 : '&';
Branch: 1 branch
BranchTrueFalseTotal
$export =~ s/^(\W)// ? : 292 2336 2628
181 2628 my $export_sym = __PACKAGE__.'::'.$export;
182  
183 #if ( $export =~ /^DO_/ ) {
184 # #warn $caller.'::'.$prefix.$export;
185 # eval "sub ${caller}::$prefix$export () { ". $export->() ." };";
186 #warn &{$caller.'::'.$prefix.$export};
187 # \&XML::XPathScript::Processor::DO_SELF_AND_KIDS;
188 # return;
189 #}
190  
191 2628 ▶ *{$caller.'::'.$prefix.$export} = $type eq '&' ? sub { $self->$export(@_) }
Subroutine __ANON__: called CC=1 SCAR=0.0
192 292 ▶ : $type eq '$' ? \${$export_sym}
Branch: 1 branch
BranchTrueFalseTotal
$type eq '@' ? : 0 0 0
193 0 ▶ : $type eq '@' ? \@{$export_sym}
Branch: 1 branch
BranchTrueFalseTotal
$type eq '%' ? : 0 0 0
194 0 ▶ : $type eq '%' ? \%{$export_sym}
Branch: 1 branch
BranchTrueFalseTotal
$type eq '*' ? : 0 0 0
195 0 ▶ : $type eq '*' ? *{$export_sym}
Branch: 2 branches
BranchTrueFalseTotal
$type eq '&' ? : 2336 292 2628
$type eq '$' ? : 292 0 292
196 : croak "Can't export symbol: $type$export"
197 ;
198 }
199  
200 ##### stylesheet API #######################################
201  
202 2584 ▶sub findnodes {
Subroutine findnodes: called CC=3 SCAR=11.2
Pod documented
203 2584 my $self = shift;
204  
205 2584 ▶ if ($self->{parser} eq 'XML::XPath' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"parser"} eq "XML::XPath") 0 2584 2584
206 0 return $self->{dom}->findnodes(@_);
207 }
208  
209 2584 my ($path, $context) = @_;
210 2584 ▶ $context = $self->{dom} if (!defined $context);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $context 85 2499 2584
211 2584 return $context->findnodes($path);
212 }
213  
214 8070 ▶sub findvalue {
Subroutine findvalue: called CC=3 SCAR=11.2
Pod documented
215 8070 my $self = shift;
216 8070 ▶ if ( $self->{parser} eq 'XML::XPath' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"parser"} eq "XML::XPath") 0 8070 8070
217 0 return $self->xpath_to_string(scalar $self->{dom}->findvalue(@_));
218 }
219  
220 8070 my ($path, $context) = @_;
221 8070 ▶ $context = $self->{dom} if (!defined $context);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $context 8 8062 8070
222 8070 return $self->xpath_to_string($context->findvalue($path));
223 }
224  
225 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
226  
227 11566 ▶sub xpath_to_string {
Subroutine xpath_to_string: called CC=4 SCAR=13.9
Pod documented
228 11566 my $self = shift;
229 11566 my ($blob)=@_;
230 11566 ▶ return $blob unless ref $blob ;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $blob 10119 1447 11566
231  
232 # Was simply C<< return "$blob" >> but Perl 5.6.1 seems to have
233 # issues with UTF8-flag used in overloaded stringification :-(
234 1447 ▶ return $blob->can("data") ? $blob->data()
Branch: 2 branches
BranchTrueFalseTotal
$blob->can('data') ? : 1423 24 1447
$blob->can('value') ? : 24 0 24
235 : $blob->can("value") ? $blob->value()
236 : $blob->string_value()
237 ;
238 }
239  
240 0 ▶sub findvalues {
Subroutine findvalues: not called CC=1 SCAR=6.9
Pod documented
241 0 my $self = shift;
242 0 my @nodes = $self->findnodes(@_);
243 0 map { $self->findvalue('.', $_) } @nodes;
244 }
245  
246 0 ▶sub findnodes_as_string {
Subroutine findnodes_as_string: not called CC=1 SCAR=6.9
Pod documented
247 0 my $self = shift;
248 0 $self->{dom}->findnodes_as_string( @_ )
249 }
250  
251 0 ▶sub matches {
Subroutine matches: not called CC=1 SCAR=6.9
Pod documented
252 0 my $self = shift;
253 0 $self->{dom}->matches(@_)
254 }
255  
256 sub set_namespace
257 0 ▶{
Subroutine set_namespace: not called CC=2 SCAR=17.9
Pod undocumented
258 0 my $self = shift;
259 0 eval { $self->{dom}->set_namespace(@_) };
260 0 ▶ warn "set_namespace failed: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
261 }
262  
263 2962 ▶sub apply_templates {
Subroutine apply_templates: called CC=7 SCAR=19.5
Pod documented
264 2962 my $self = shift;
265  
266 2962 ▶ my $params = ref( $_[-1] ) eq 'HASH' ? pop @_ : {} ;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[-1] eq 'HASH' ? : 1724 1238 2962
267  
268 # catch the calls to apply_templates()
269 2962 ▶ @_ = $self->findnodes('/') unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 46 2916 2962
270  
271 2962 ▶ unless( ref $_[0] ) { # called with a path to find
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $_[0]) 35 2927 2962
272 35 @_ = $self->findnodes( @_ );
273 }
274  
275 2962 ▶ return unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 1 2961 2962
276  
277 2961 my $retval;
278  
279 30 ▶ no warnings qw/ uninitialized /;
Subroutine BEGIN: called CC=1 SCAR=0.0
280 $retval .= $self->translate_node( $_, $params )
281 2961 ▶ for $self->is_nodelist($_[0]) ? $_[0]->get_nodelist
Branch: 1 branch
BranchTrueFalseTotal
$self->is_nodelist($_[0]) ? : 0 2961 2961
282 : @_
283 ;
284 2960 return $retval;
285 }
286  
287 0 ▶sub call_template {
Subroutine call_template: not called CC=6 SCAR=37.4
Pod documented
288 0 my ($self,$node,$t,$template)=@_;
289  
290 0 ▶ if (defined(my $sub=$template->{testcode})) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined(my $sub = $template->{'testcode'})) { } 0 0 0
elsif (exists $t->{'prechild'} or exists $t->{'prechildren'} or exists $t->{'postchild'} or exists $t->{'postchildren'}) { } 0 0 0
291 0 return &$sub($node,$t);
292 ▶ } elsif (exists $t->{prechild} || exists $t->{prechildren} ||
Condition0%0/9
exists $t->{"prechild"} or exists $t->{"prechildren"} or exists $t->{"postchild"} or exists $t->{"postchildren"}
l!l&&r!l&&!r
000
exists $t->{"prechild"} or exists $t->{"prechildren"} or exists $t->{"postchild"}
l!l&&r!l&&!r
000
exists $t->{"prechild"} or exists $t->{"prechildren"}
l!l&&r!l&&!r
000
MC/DC0%0/4
exists $t->{"prechild"} or exists $t->{"prechildren"} or exists $t->{"postchild"} or exists $t->{"postchildren"}
exists $t->{"prechild"} exists $t->{"prechildren"} exists $t->{"postchild"} exists $t->{"postchildren"}
Truth table0%0/5
A or B or C or D
A= exists $t->{"prechild"}B= exists $t->{"prechildren"}C= exists $t->{"postchild"}D= exists $t->{"postchildren"}
ABCDresult
1XXX1
01XX1
001X1
00011
00000
293 exists $t->{postchild} || exists $t->{postchildren}) {
294 0 warn "XML::XPathScript::Processor::call_template: cannot handle this sort of templates yet";
295 # Attempt to recover
296 0 $t->{pre}="";
297 0 $t->{post}="";
298 0 return 1;
299 } else {
300 0 $t->{pre}=$template->{pre};
301 0 $t->{post}=$template->{post};
302 0 return 1;
303 };
304 }
305  
306 88 ▶sub _apply_templates {
Subroutine _apply_templates: called CC=1 SCAR=0.0
307 88 my $self = shift;
308 88 my $params = pop;
309 30 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
310 88 return join '', map $self->translate_node($_,$params), @_;
311 }
312  
313 3240 ▶sub is_utf8_tainted {
Subroutine is_utf8_tainted: called CC=6 SCAR=18.2
Pod documented
314 3240 my $self = shift;
315 3240 my ($string) = @_;
316  
317 3240 my $ghost = ($string x 0) .
318 "ab"; # Very quick and conserves UTF-8 flag (and taintedness)
319  
320 30 ▶ $ghost .= do { use bytes; "\xc3" };
Subroutine BEGIN: called CC=1 SCAR=0.0
321 30 ▶ $ghost .= do { use bytes; "\xa9" };
Subroutine BEGIN: called CC=1 SCAR=0.0
322 30 ▶ my $charlength = do { no bytes; length($ghost) };
Subroutine BEGIN: called CC=1 SCAR=0.0
323 30 ▶ my $bytelength = do { use bytes; length($ghost) };
Subroutine BEGIN: called CC=1 SCAR=0.0
324  
325 3240 ▶ if ($charlength == 3) {
Branch: 3 branches
BranchTrueFalseTotal
if ($charlength == 3) { } 0 3240 3240
elsif ($charlength == 4 and $bytelength == 4) { } 3237 3 3240
elsif ($charlength == 4 and $bytelength == 6) { } 3 0 3
326 # The two bytes we added got lumped in core into a single
327 # UTF-8 char. This is a Perl bug (arising e.g. because $string
328 # is tainted, see t/04unicode.t) but we recover gracefully.
329 0 return 1;
330 ▶ } elsif ($charlength == 4 && $bytelength == 4) {
Condition66%2/3
$charlength == 4 and $bytelength == 4
!ll&&!rl&&r
033237
MC/DC50%1/2
$charlength == 4 and $bytelength == 4
$charlength == 4 $bytelength == 4
Truth table66%2/3
A and B
A= $charlength == 4B= $bytelength == 4
ABresult
0X0
100
111
331 3237 return 0;
332 ▶ } elsif ($charlength == 4 && $bytelength == 6) {
Condition33%1/3
$charlength == 4 and $bytelength == 6
!ll&&!rl&&r
003
MC/DC0%0/2
$charlength == 4 and $bytelength == 6
$charlength == 4 $bytelength == 6
Truth table33%1/3
A and B
A= $charlength == 4B= $bytelength == 6
ABresult
0X0
100
111
333 3 return 1; # The bytes were upgraded
334 } else {
335 0 die "is_utf8_tainted assertion check failed".
336 " (charlength = $charlength, bytelength=$bytelength)";
337 }
338 }
339  
340 129 ▶ sub get_xpath_of_node {
Subroutine get_xpath_of_node: called CC=17 SCAR=33.6
Pod documented
341 129 my $self =shift;
342 129 my $node = shift;
343  
344 # ugly hacks all over in this function, because the quirky
345 # refcount-proof aliasing (i.e. XML::XPath::Element versus
346 # XML::XPath::ElementImpl) in XML::XPath gets in the way badly
347 129 ▶ $node = $$node if $node->isa( 'XML::XPath::Node::Element' )
Branch: 1 branch
BranchTrueFalseTotal
if $node->isa("XML::XPath::Node::Element") and not $self->isa("XML::XPath::Node::ElementImpl") and $node =~ /SCALAR/ 0 129 129
Condition33%2/6
$node->isa("XML::XPath::Node::Element") and not $self->isa("XML::XPath::Node::ElementImpl") and $node =~ /SCALAR/
!ll&&!rl&&r
12900
$node->isa("XML::XPath::Node::Element") and not $self->isa("XML::XPath::Node::ElementImpl")
!ll&&!rl&&r
12900
MC/DC0%0/3
$node->isa("XML::XPath::Node::Element") and not $self->isa("XML::XPath::Node::ElementImpl") and $node =~ /SCALAR/
$node->isa("XML::XPath::Node::Element") not $self->isa("XML::XPath::Node::ElementImpl") $node =~ /SCALAR/
Truth table25%1/4
A and not(B) and C
A= $node->isa("XML::XPath::Node::Element")B= not $self->isa("XML::XPath::Node::ElementImpl")C= $node =~ /SCALAR/
ABCresult
0XX0
10X0
1100
1111
348 and not $self->isa( 'XML::XPath::Node::ElementImpl' )
349 and $node =~ /SCALAR/;
350  
351 129 ▶ my $parent = $node->can("parentNode") ? $node->parentNode()
Branch: 1 branch
BranchTrueFalseTotal
$node->can('parentNode') ? : 129 0 129
352 : $node->getParentNode()
353 ;
354  
355 129 ▶ return "" unless defined $parent;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $parent 20 109 129
356  
357 109 my $name;
358 109 ▶ if ($self->is_element_node($node)) {
Branch: 4 branches
BranchTrueFalseTotal
if ($self->is_element_node($node)) { } 106 3 109
elsif ($self->is_text_node($node)) { } 3 0 3
elsif ($self->is_comment_node($node)) { } 0 0 0
elsif ($self->is_pi_node($node)) { } 0 0 0
359 106 $name = $node->findvalue('name()');
360 } elsif ($self->is_text_node($node)) {
361 3 $name = "text()";
362 } elsif ($self->is_comment_node($node)) {
363 0 $name = "comment()";
364 } elsif ($self->is_pi_node($node)) {
365 0 $name = "processing-instruction()";
366 } else {
367 # YKYBPTMNW...
368 0 return $self->get_xpath_of_node($parent)."/strange-node()";
369 }
370  
371 # ugly hack, part II
372 109 ▶ my @brothers = map{ ($_->isa( 'XML::XPath::Node::Element' ) ) ? $$_ : $_ } $parent->findnodes("./$name");
Branch: 1 branch
BranchTrueFalseTotal
$_->isa('XML::XPath::Node::Element') ? : 0 294 294
373  
374 # Short-cut for nodes that have an ID. FIXME: not all DTDs use
375 # attribute named "id" as the SGML ID!
376 109 ▶ if ($self->is_element_node($node) && (my $id=$self->findvalue('@id',$node))) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_element_node($node) and my $id = $self->findvalue("\@id", $node)) 4 105 109
Condition100%3/3
$self->is_element_node($node) and my $id = $self->findvalue("\@id", $node)
!ll&&!rl&&r
31024
MC/DC100%2/2
$self->is_element_node($node) and my $id = $self->findvalue("\@id", $node)
$self->is_element_node($node) my $id = $self->findvalue("\@id", $node)
Truth table100%3/3
A and B
A= $self->is_element_node($node)B= my $id = $self->findvalue("\@id", $node)
ABresult
0X0
100
111
377 4 return $self->get_xpath_of_node($parent).sprintf('/%s[@id="%s"]', $name, $id);
378 }
379  
380 # Bug: the matches() function from XML::XPath is hosed, and only
381 # works towards ancestors. We resort to comparing references for
382 # identity. See above for details on the $$self quirk.
383 105 ▶ my $theself=($node =~ m/SCALAR/?$$node:$node);
Branch: 1 branch
BranchTrueFalseTotal
$node =~ /SCALAR/ ? : 0 105 105
384  
385 105 for my $i ( 0..$#brothers ) {
386 my $thebrother=($brothers[$i] =~ m/SCALAR/?
387 0 ▶ ${$brothers[$i]}:$brothers[$i]);
Branch: 1 branch
BranchTrueFalseTotal
$brothers[$i] =~ /SCALAR/ ? : 0 173 173
388  
389 173 ▶ return sprintf '%s/%s[%d]', $self->get_xpath_of_node($parent), $name, $i+1
Branch: 1 branch
BranchTrueFalseTotal
if $theself eq $thebrother 105 68 173
390 if $theself eq $thebrother;
391 };
392  
393 0 return $self->get_xpath_of_node($parent)."/$name"."[?]";
394 }
395  
396  
397 ########################## End of exportable stuff ####################
398  
399 3104 ▶sub translate_node {
Subroutine translate_node: called CC=8 SCAR=22.9
Pod undocumented
400 3104 my( $self, $node, $params ) = @_;
401  
402 my $retval = $self->is_element_node( $node )
403 ? $self->translate_element_node( $node, $params )
404 : $self->is_text_node($node)
405 ? $self->translate_text_node( $node, $params )
406 : $self->is_comment_node($node)
407 ? $self->translate_comment_node( $node, $params )
408 : $self->is_pi_node($node)
409 0 ▶ ? eval { if ($node->getParentNode->getParentNode) {
Branch: 5 branches
BranchTrueFalseTotal
$self->is_element_node($node) ? : 1617 1487 3104
$self->is_text_node($node) ? : 1475 12 1487
$self->is_comment_node($node) ? : 12 0 12
$self->is_pi_node($node) ? : 0 0 0
if ($node->getParentNode->getParentNode) { } 0 0 0
410 0 return $node->toString;
411 0 } else { '' } }
412 : $self->to_string( $node )
413 ;
414  
415 3104 ▶ if ( $self->{binmode} &&
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"binmode"} and $self->is_utf8_tainted($retval)) 1 3103 3104
Condition100%3/3
$self->{"binmode"} and $self->is_utf8_tainted($retval)
!ll&&!rl&&r
21828851
MC/DC100%2/2
$self->{"binmode"} and $self->is_utf8_tainted($retval)
$self->{"binmode"} $self->is_utf8_tainted($retval)
Truth table100%3/3
A and B
A= $self->{"binmode"}B= $self->is_utf8_tainted($retval)
ABresult
0X0
100
111
416 $self->is_utf8_tainted($retval)) {
417 30 ▶ use Carp qw(confess); # TODO remove this
Subroutine BEGIN: called CC=1 SCAR=0.0
418 1 confess("Wrong translation by stylesheet".
419 " (result is Unicode-tainted) at ".$self->get_xpath_of_node($node).
420 "\n$retval\n");
421 }
422  
423 3103 return $retval;
424 }
425  
426 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
427  
428 1475 ▶sub translate_text_node {
Subroutine translate_text_node: called CC=9 SCAR=22.0
Pod undocumented
429 1475 my $self = shift;
430 1475 my $node = shift;
431 1475 my $params = shift;
432 1475 my $translations = $self->{template};
433  
434 1475 ▶ my $trans = $translations->{'#text'} || $translations->{'text()'};
Condition100%3/3
$translations->{'#text'} || $translations->{'text()'}
l!l&&r!l&&!r
2141657
MC/DC100%2/2
$translations->{'#text'} || $translations->{'text()'}
$translations->{'#text'} $translations->{'text()'}
Truth table100%3/3
A || B
A= $translations->{'#text'}B= $translations->{'text()'}
ABresult
1X1
011
000
435  
436 1475 ▶ return $node->toString unless $trans;
Branch: 1 branch
BranchTrueFalseTotal
unless $trans 57 1418 1475
437  
438 1418 my $action = $trans->{action};
439  
440 1418 my $t = new XML::XPathScript::Template::Tag;
441 1418 $t->{$_} = $trans->{$_} for keys %{$trans};
442 1418 ▶ if (my $code = $trans->{testcode})
Branch: 1 branch
BranchTrueFalseTotal
if (my $code = $trans->{"testcode"}) 1409 9 1418
443 {
444 1409 $action = $code->( $node, $t, $params );
445 }
446
447 30 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
448 1418 ▶ return if defined($action) and $action == DO_NOT_PROCESS();
Branch: 1 branch
BranchTrueFalseTotal
if defined $action and $action == 0 0 1418 1418
Condition66%2/3
defined $action and $action == 0
!ll&&!rl&&r
814100
MC/DC0%0/2
defined $action and $action == 0
defined $action $action == 0
Truth table66%2/3
A and B
A= defined $actionB= $action == 0
ABresult
0X0
100
111
449  
450 1418 my $middle;
451 1418 ▶ $middle = $node->toString if defined($action)
Branch: 1 branch
BranchTrueFalseTotal
if defined $action and $action == 2 2 1416 1418
Condition100%3/3
defined $action and $action == 2
!ll&&!rl&&r
814082
MC/DC100%2/2
defined $action and $action == 2
defined $action $action == 2
Truth table100%3/3
A and B
A= defined $actionB= $action == 2
ABresult
0X0
100
111
452 and $action == DO_TEXT_AS_CHILD();
453  
454 return $self->_transform_tag( $t->{pre}, $node, $t, $params )
455 . $middle
456 1418 . $self->_transform_tag( $t->{post}, $node, $t, $params );
457 }
458  
459 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
460  
461 1617 ▶sub translate_element_node {
Subroutine translate_element_node: called CC=20 SCAR=30.0
Pod undocumented
462 1617 my( $self, $node, $params ) = @_;
463 1617 my $translations = $self->{template};
464  
465 1617 my $node_name = $self->get_node_name( $node );
466  
467 1617 my $namespace = $self->get_namespace( $node );
468  
469 1617 my $trans = XML::XPathScript::Template::resolve( $translations,
470 $namespace, $node_name );
471  
472 1617 ▶ unless( $trans ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($trans) 88 1529 1617
473 # no specific and no generic? Okay, okay, return as is...
474 30 ▶ no warnings qw/ uninitialized /;
Subroutine BEGIN: called CC=1 SCAR=0.0
475 88 my @kids = $self->get_child_nodes( $node );
476 88 return $self->start_tag($node)
477 . $self->_apply_templates( @kids, $params )
478 . $self->end_tag($node);
479
480 }
481  
482 1529 my $t = new XML::XPathScript::Template::Tag;
483 1529 $t->{$_} = $trans->{$_} for keys %{$trans};
484  
485 # we officially support 'content', but also allow
486 # 'contents'
487 1529 ▶ if ( my $content = $trans->{content} || $trans->{contents} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $content = $trans->{"content"} || $trans->{"contents"}) 8 1521 1529
Condition66%2/3
$trans->{'content'} || $trans->{'contents'}
l!l&&r!l&&!r
801521
MC/DC50%1/2
$trans->{'content'} || $trans->{'contents'}
$trans->{'content'} $trans->{'contents'}
Truth table66%2/3
A || B
A= $trans->{'content'}B= $trans->{'contents'}
ABresult
1X1
011
000
488 8 return $self->_transform_content( $content, $node, $t, $params );
489 }
490  
491 # by default we do the kids
492 1521 my $dokids = 1;
493 1521 my $search;
494  
495 1521 my $action = $trans->{action};
496
497 1521 ▶ if ($trans->{testcode}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($trans->{"testcode"}) 1467 54 1521
498 1467 $action = $trans->{testcode}->($node, $t, $params );
499 }
500  
501 30 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
502 1521 ▶ return if $action =~ /^-?\d+$/ and $action == DO_NOT_PROCESS();
Branch: 1 branch
BranchTrueFalseTotal
if $action =~ /^-?\d+$/ and $action == 0 0 1521 1521
Condition66%2/3
$action =~ /^-?\d+$/ and $action == 0
!ll&&!rl&&r
8714340
MC/DC0%0/2
$action =~ /^-?\d+$/ and $action == 0
$action =~ /^-?\d+$/ $action == 0
Truth table66%2/3
A and B
A= $action =~ /^-?\d+$/B= $action == 0
ABresult
0X0
100
111
503  
504 1521 ▶ if( defined( $action) and $action !~ /^-?\d+/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $action and not $action =~ /^-?\d+/) { } 34 1487 1521
elsif ($action == -1) { } 71 1416 1487
Condition100%3/3
defined $action and not $action =~ /^-?\d+/
!ll&&!rl&&r
53143434
MC/DC100%2/2
defined $action and not $action =~ /^-?\d+/
defined $action not $action =~ /^-?\d+/
Truth table100%3/3
A and not(B)
A= defined $actionB= not $action =~ /^-?\d+/
ABresult
0X0
100
111
505 # ah, an xpath expression
506 34 $dokids = 0;
507 34 $search = $action;
508 }
509 elsif ($action == DO_SELF_ONLY() ) {
510 71 $dokids = 0;
511 }
512  
513 1521 ▶ my @kids = $dokids ? $node->getChildNodes()
Branch: 2 branches
BranchTrueFalseTotal
$dokids ? : 1416 105 1521
$search ? : 34 71 105
514 : $search ? $node->findnodes($search)
515 : ()
516 ;
517  
518 1521 my @args = ( $node, $t, $params );
519  
520 1521 my $pre = $self->_transform_tag( $t->{pre}, @args );
521 1521 ▶ $pre .= $self->start_tag( $node , $t->{rename}) if $t->{showtag};
Branch: 1 branch
BranchTrueFalseTotal
if $t->{"showtag"} 16 1505 1521
522 1521 $pre .= $self->_transform_tag( $t->{intro}, @args );
523 1521 ▶ $pre .= $self->_transform_tag( $t->{prechildren}, @args ) if @kids;
Branch: 1 branch
BranchTrueFalseTotal
if @kids 1087 434 1521
524
525 1521 my $post;
526 1521 ▶ $post .= $self->_transform_tag( $t->{postchildren}, @args ) if @kids;
Branch: 1 branch
BranchTrueFalseTotal
if @kids 1087 434 1521
527 1521 $post .= $self->_transform_tag( $t->{extro}, @args );
528 1521 ▶ $post .= $self->end_tag( $node, $t->{rename} ) if $t->{showtag};
Branch: 1 branch
BranchTrueFalseTotal
if $t->{"showtag"} 16 1505 1521
529 1521 $post .= $self->_transform_tag( $t->{post}, @args );
530  
531 1521 my $middle;
532  
533 1521 for my $kid ( @kids )
534 {
535 1720 my $kid_is_element = $self->is_element_node( $kid );
536  
537 $middle .= $self->_transform_tag( $trans->{prechild},
538 1720 ▶ $kid, $t, $params ) if $kid_is_element;
Branch: 1 branch
BranchTrueFalseTotal
if $kid_is_element 387 1333 1720
539  
540 1720 $middle .= $self->apply_templates($kid, $params );
541  
542 $middle .= $self->_transform_tag( $trans->{postchild},
543 1720 ▶ $kid, $t, $params ) if $kid_is_element;
Branch: 1 branch
BranchTrueFalseTotal
if $kid_is_element 387 1333 1720
544 }
545
546 1521 return $pre . $middle . $post;
547 }
548  
549 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
550  
551 8 ▶sub _transform_content {
Subroutine _transform_content: called CC=4 SCAR=14.0
552 8 my ( $self, $content, $node, $t, $params ) = @_;
553  
554 8 ▶ return $content->( $node, $t, $params ) if ref $content eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $content eq "CODE" 0 8 8
555  
556 8 my $interpolated = $self->interpolate( $node, $content );
557  
558 8 local *STDOUT;
559 8 my $output;
560 8 ▶ open STDOUT, '>', \$output or die "couldn't redirect STDOUT: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open STDOUT, ">", \$output or die "couldn't redirect STDOUT: $!\n" 8 0 8
561  
562 8 my $xps = XML::XPathScript::current();
563  
564 8 my $code = $xps->extract( $interpolated );
565  
566 8 local $self->{dom} = $node;
567  
568 8 eval <<"END_CONTENT";
569 package XML::XPathScript::Template::Content;
570 my \$processor = \$self;
571 my \%params = \%\$params;
572 \$self->import_functional unless exists \&get_template;
573 $code;
574 END_CONTENT
575  
576 8 ▶ die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 8 8
577  
578 8 return $output;
579 }
580  
581 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
582 # bad terminology, here a tag is 'pre', 'post', etc
583  
584 11890 ▶sub _transform_tag {
Subroutine _transform_tag: called CC=3 SCAR=11.0
585 11890 my ( $self, $tag, $node, $t, $params ) = @_;
586  
587 11890 ▶ return unless $tag;
Branch: 1 branch
BranchTrueFalseTotal
unless $tag 9898 1992 11890
588  
589 1992 ▶ return $tag->( $node, $t, $params ) if ref $tag eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $tag eq "CODE" 14 1978 1992
590  
591 1978 return $self->interpolate( $node, $tag );
592 }
593  
594 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
595  
596 12 ▶sub translate_comment_node {
Subroutine translate_comment_node: called CC=11 SCAR=24.0
Pod undocumented
597 12 my ( $self, $node, $params ) = @_;
598 12 my $translations = $self->{template};
599  
600 12 ▶ my $trans = $translations->{'#comment'} || $translations->{'comment()'};
Condition66%2/3
$translations->{'#comment'} || $translations->{'comment()'}
l!l&&r!l&&!r
2100
MC/DC0%0/2
$translations->{'#comment'} || $translations->{'comment()'}
$translations->{'#comment'} $translations->{'comment()'}
Truth table66%2/3
A || B
A= $translations->{'#comment'}B= $translations->{'comment()'}
ABresult
1X1
011
000
601  
602 12 ▶ return $node->toString unless $trans;
Branch: 1 branch
BranchTrueFalseTotal
unless $trans 0 12 12
603  
604 12 my $middle = $self->get_text_content( $node );
605  
606 12 my $t = new XML::XPathScript::Template::Tag;
607 12 $t->{$_} = $trans->{$_} for keys %{$trans};
608  
609 12 my $action = $trans->{action};
610  
611 12 ▶ if (my $code = $trans->{testcode})
Branch: 1 branch
BranchTrueFalseTotal
if (my $code = $trans->{"testcode"}) 10 2 12
612 {
613 10 $action = $code->( $node, $t, $params );
614 10 ▶ if ($action and %$t) {
Branch: 1 branch
BranchTrueFalseTotal
if ($action and %$t) 9 1 10
Condition66%2/3
$action and %$t
!ll&&!rl&&r
109
MC/DC50%1/2
$action and %$t
$action %$t
Truth table66%2/3
A and B
A= $actionB= %$t
ABresult
0X0
100
111
615 9 foreach my $tkey (keys %$t) {
616 26 $trans->{$tkey} = $t->{$tkey};
617 }
618 }
619 }
620  
621 30 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
622 12 ▶ return if $action =~ /^-?\d+$/ and $action == $DO_NOT_PROCESS;
Branch: 1 branch
BranchTrueFalseTotal
if $action =~ /^-?\d+$/ and $action == $XML::XPathScript::Processor::DO_NOT_PROCESS 1 11 12
Condition100%3/3
$action =~ /^-?\d+$/ and $action == $XML::XPathScript::Processor::DO_NOT_PROCESS
!ll&&!rl&&r
1101
MC/DC100%2/2
$action =~ /^-?\d+$/ and $action == $XML::XPathScript::Processor::DO_NOT_PROCESS
$action =~ /^-?\d+$/ $action == $XML::XPathScript::Processor::DO_NOT_PROCESS
Truth table100%3/3
A and B
A= $action =~ /^-?\d+$/B= $action == $XML::XPathScript::Processor::DO_NOT_PROCESS
ABresult
0X0
100
111
623
624 11 ▶ $middle = undef if $action == $DO_SELF_ONLY;
Branch: 1 branch
BranchTrueFalseTotal
if $action == $XML::XPathScript::Processor::DO_SELF_ONLY 10 1 11
625 return $self->_transform_tag( $trans->{pre}, $node, $t, $params )
626 . $middle
627 11 . $self->_transform_tag( $trans->{post}, $node, $t, $params );
628 }
629  
630 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
631  
632 104 ▶sub start_tag {
Subroutine start_tag: called CC=6 SCAR=18.8
Pod undocumented
633 104 my( $self, $node, $name ) = @_;
634  
635 104 ▶ $name ||= $self->get_qualified_name( $node ) or return;
Branch: 1 branch
BranchTrueFalseTotal
$name ||= $self->get_qualified_name($node) or return 104 0 104
Condition66%2/3
$name ||= $self->get_qualified_name($node)
l!l&&r!l&&!r
5990
MC/DC0%0/2
$name ||= $self->get_qualified_name($node)
$name $self->get_qualified_name($node)
Truth table66%2/3
A ||= B
A= $nameB= $self->get_qualified_name($node)
ABresult
1X1
011
000
636  
637 104 my $string = '<'.$name;
638  
639 # do we need this for libXML?
640 104 ▶ if( $self->{parser} eq 'XML::XPath' )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"parser"} eq "XML::XPath") 0 104 104
641 {
642 0 $string .= $_->toString for $node->getNamespaceNodes;
643 }
644  
645 104 for my $attr ( $self->get_attributes( $node ) ) {
646 9 $string .= $self->get_attribute( $attr );
647 }
648  
649 104 $string .= '>';
650  
651 104 return $string;
652 }
653  
654 104 ▶sub end_tag {
Subroutine end_tag: called CC=3 SCAR=12.0
Pod undocumented
655 104 my $self = shift;
656 104 ▶ if (my $name = $_[1] || $self->get_qualified_name( $_[0] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $name = $_[1] || $self->get_qualified_name($_[0])) 104 0 104
Condition66%2/3
$_[1] || $self->get_qualified_name($_[0])
l!l&&r!l&&!r
5990
MC/DC0%0/2
$_[1] || $self->get_qualified_name($_[0])
$_[1] $self->get_qualified_name($_[0])
Truth table66%2/3
A || B
A= $_[1]B= $self->get_qualified_name($_[0])
ABresult
1X1
011
000
657 104 return "</$name>";
658 }
659 0 return '';
660 }
661  
662 1986 ▶sub interpolate {
Subroutine interpolate: called CC=4 SCAR=13.9
Pod undocumented
663 1986 my ($self, $node, $string) = @_;
664
665 # if string is empty or no interpolation,
666 # we return
667 return( $string || '' ) unless
668 defined( $string ) and
669 1986 ▶ $self->{is_interpolating};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $string and $self->{"is_interpolating"} 1901 85 1986
Condition60%3/5
defined $string and $self->{"is_interpolating"}
!ll&&!rl&&r
0190185
$string || ''
l!l
19010
MC/DC50%1/2
defined $string and $self->{"is_interpolating"}
defined $string $self->{"is_interpolating"}
MC/DC0%0/1
$string || ''
$string
Truth table66%2/3
A and B
A= defined $stringB= $self->{"is_interpolating"}
ABresult
0X0
100
111
Truth table50%1/2
A
A= $string
Aresult
11
00
670  
671 85 my $regex = $self->{interpolation_regex};
672 85 $string =~ s/$regex/ $node->findvalue($1) /egs;
673
674 30 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
675 85 return $string;
676 }
677  
678 30 'end of module XML::XPathScript::Processor';
679  
680 __END__
« XPathScript.pm ↑ Summary XPathScript/Template.pm »