XPathScript.pm

filter: Statementstmt 87.5% 338 / 386 Branchbran 51.5% 98 / 190 Conditioncond 56.6% 17 / 30 MC/DCmcdc 35.2% 6 / 17 Subroutinesub 100% 57 / 57 Podpod 85.7% 18 / 21 totaltotal 76.1% 534 / 701 CC 105 SCAR 30.0
CC
105
Cov
75%
extract
95.1
document
39.7
set_xml
37.1
CRAP 20.0
« XPathScript/Stylesheet/DocBook2LaTeX.pm ↑ Summary XPathScript/Processor.pm »
1 package XML::XPathScript;
2 25 our $AUTHORITY = 'cpan:YANICK';
3 # ABSTRACT: a Perl framework for XML stylesheets
4 25 $XML::XPathScript::VERSION = '2.00';
5 27 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 27 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 27 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9  
10 177 ▶sub current {
Subroutine current: called CC=2 SCAR=7.2
Pod documented
11 177 ▶ croak 'Wrong context for calling current()'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $XML::XPathScript::current 0 176 176
12 unless defined $XML::XPathScript::current;
13  
14 177 return $XML::XPathScript::current;
15 }
16  
17  
18 66 ▶sub interpolation {
Subroutine interpolation: called CC=1 SCAR=0.0
Pod documented
19 66 my $self = shift;
20 66 return $self->interpolating( @_ );
21 }
22  
23 66 ▶sub interpolating {
Subroutine interpolating: called CC=3 SCAR=11.0
Pod undocumented
24 66 my $self=shift;
25  
26 66 ▶ if ( @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 65 0 65
27 $self->processor->set_interpolation(
28 $self->{interpolating} = shift
29 66 );
30 }
31  
32 66 ▶ return $self->{interpolating} || 0;
Condition100%2/2
$self->{'interpolating'} || 0
l!l
641
MC/DC100%1/1
$self->{'interpolating'} || 0
$self->{'interpolating'}
Truth table100%2/2
A
A= $self->{'interpolating'}
Aresult
11
00
33 }
34  
35  
36 64 ▶sub interpolation_regex {
Subroutine interpolation_regex: called CC=2 SCAR=7.0
Pod documented
37 64 my $self = shift;
38  
39 64 ▶ if ( my $regex = shift ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $regex = shift()) 63 0 63
40 $self->processor->set_interpolation_regex(
41 64 $self->{interpolation_regex} = $regex
42 )
43 }
44  
45 64 return $self->{interpolation_regex};
46 }
47  
48  
49  
50 2 ▶sub binmode {
Subroutine binmode: called CC=2 SCAR=7.0
Pod documented
51 2 my ($self)=@_;
52 2 $self->{binmode}=1;
53 2 $self->{processor}->enable_binmode;
54 2 ▶ binmode ORIGINAL_STDOUT if (! defined $self->{printer});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"printer"} 0 1 1
55 2 return;
56 }
57  
58  
59 27 ▶use vars qw( $XML_parser $debug_level );
Subroutine BEGIN: called CC=1 SCAR=0.0
60  
61 27 ▶use Symbol;
Subroutine BEGIN: called CC=1 SCAR=0.0
62 27 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
63 27 ▶use XML::XPathScript::Processor;
Subroutine BEGIN: called CC=1 SCAR=0.0
64 27 ▶use XML::XPathScript::Template;
Subroutine BEGIN: called CC=1 SCAR=0.0
65  
66 25 our $XML_parser = 'XML::LibXML';
67  
68 25 my %use_parser = (
69 'XML::LibXML' => 'use XML::LibXML',
70 'XML::XPath' => <<'END_USE',
71 use XML::XPath 1.0;
72 use XML::XPath::XMLParser;
73 use XML::XPath::Node;
74 use XML::XPath::NodeSet;
75 use XML::Parser;
76 END_USE
77 );
78  
79 25 ▶die "parser $XML_parser unknown\n" unless $use_parser{$XML_parser};
Branch: 1 branch
BranchTrueFalseTotal
unless $use_parser{$XML::XPathScript::XML_parser} 0 25 25
80 26 ▶eval $use_parser{$XML_parser}.";1"
Subroutine BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
eval $use_parser{$XML::XPathScript::XML_parser} . ';1' or die "couldn't import $XML::XPathScript::XML_parser" 25 0 25
81 or die "couldn't import $XML_parser";
82  
83 # internal variable for debugging information.
84 # 0 is total silence and 10 is complete verbiage
85 25 $debug_level = 0;
86  
87 sub import
88 30 ▶{
Subroutine import: called CC=3 SCAR=11.2
89 30 my $self = shift @_;
90  
91 30 ▶ if ( grep { $_ eq 'XML::XPath' } @_ ) {
Branch: 2 branches
BranchTrueFalseTotal
if (grep {$_ eq 'XML::XPath';} @_) { } 0 29 29
elsif (grep {$_ eq 'XML::LibXML';} @_) { } 0 29 29
92 1 $XML::XPathScript::XML_parser = 'XML::XPath';
93 }
94 1 elsif ( grep { $_ eq 'XML::LibXML' } @_ ) {
95 1 $XML::XPathScript::XML_parser = 'XML::LibXML';
96 }
97 30 return;
98 }
99  
100  
101 64 ▶sub new {
Subroutine new: called CC=7 SCAR=19.8
Pod documented
102 64 my $class = shift;
103 64 ▶ die "Invalid hash call to new" if @_ % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ % 2 0 63 63
104 64 my %params = @_;
105 64 my $self = \%params;
106 64 bless $self, $class;
107 64 $self->{processor} = XML::XPathScript::Processor->new;
108 64 ▶ $self->set_xml( $params{xml} ) if $params{xml};
Branch: 1 branch
BranchTrueFalseTotal
if $params{"xml"} 53 10 63
109  
110 $self->interpolation( exists $params{interpolation}
111 64 ▶ ? $params{interpolation} : 1 );
Branch: 1 branch
BranchTrueFalseTotal
exists $params{'interpolation'} ? : 0 63 63
112  
113 $self->interpolation_regex( $params{interpolation_regex}
114 64 ▶ || qr/{(.*?)}/ );
Condition50%1/2
$params{'interpolation_regex'} || qr/{(.*?)}/
l!l
063
MC/DC0%0/1
$params{'interpolation_regex'} || qr/{(.*?)}/
$params{'interpolation_regex'}
Truth table50%1/2
A
A= $params{'interpolation_regex'}
Aresult
11
00
115  
116  
117  
118 64 ▶ if ( $XML::XPathScript::XML_parser eq 'XML::XPath' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($XML::XPathScript::XML_parser eq 'XML::XPath') { } 0 63 63
119 1 require XML::XPath;
120 1 require XML::XPath::XMLParser;
121 1 require XML::XPath::Node;
122 1 require XML::XPath::NodeSet;
123 1 require XML::Parser;
124 }
125 else {
126 64 require XML::LibXML;
127 }
128  
129 64 ▶ croak $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 63 63
130
131 64 return $self;
132 }
133  
134  
135 22 ▶sub transform {
Subroutine transform: called CC=4 SCAR=13.9
Pod documented
136 22 my( $self, $xml, $stylesheet, $args ) = @_;
137 22 my $output;
138
139 22 ▶ $self->set_xml( $xml ) if $xml;
Branch: 1 branch
BranchTrueFalseTotal
if $xml 6 15 21
140  
141 22 ▶ if ( $stylesheet ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stylesheet) 5 16 21
142 6 $self->{compiledstylesheet} = undef;
143 6 $self->{stylesheet} = $stylesheet;
144 }
145  
146 22 ▶ $self->process( \$output, $args ? @$args : () );
Branch: 1 branch
BranchTrueFalseTotal
$args ? : 2 19 21
147  
148 21 return $output;
149 }
150  
151  
152 1 ▶sub set_dom {
Subroutine set_dom: called CC=1 SCAR=0.0
Pod documented
153 1 my( $self, $dom ) = @_;
154 1 $self->{dom} = $dom;
155 1 $self->{processor}->set_dom( $dom );
156 1 return $self;
157 }
158  
159  
160 77 ▶sub set_xml {
Subroutine set_xml: called CC=14 SCAR=36.1
Pod documented
161 77 my( $self, $xml ) = @_;
162  
163 77 $self->{xml} = $xml;
164  
165 77 ▶ my $retval = ref $xml ? $self->_set_xml_ref()
Branch: 1 branch
BranchTrueFalseTotal
ref $xml ? : 7 69 76
166 : $self->_set_xml_scalar()
167 ;
168  
169 77 $self->{processor}->set_dom( $self->{dom} );
170
171 77 return $retval;
172  
173 # FIXME
174  
175 1 my $xpath;
176  
177  
178 # a third option should be auto, for which we
179 # would use the already-defined object
180 1 ▶ if( $XML_parser eq 'auto' )
Branch: 1 branch
BranchTrueFalseTotal
if ($XML::XPathScript::XML_parser eq "auto") 0 0 0
181 {
182 1 ▶ if (UNIVERSAL::isa($self->{xml},"XML::XPath"))
Branch: 2 branches
BranchTrueFalseTotal
if (UNIVERSAL::isa($self->{'xml'}, 'XML::XPath')) { } 0 0 0
elsif (UNIVERSAL::isa($self->{'xml'}, 'XML::LibXML')) { } 0 0 0
183 {
184 1 $xpath=$self->{xml};
185 1 $XML_parser = 'XML::XPath';
186 }
187 elsif(UNIVERSAL::isa($self->{xml},"XML::LibXML" ))
188 {
189 1 $xpath=$self->{xml};
190 1 $XML_parser = 'XML::LibXML';
191 }
192 }
193  
194 1 ▶ if (UNIVERSAL::isa($self->{xml},"XML::XPath"))
Branch: 2 branches
BranchTrueFalseTotal
if (UNIVERSAL::isa($self->{'xml'}, 'XML::XPath')) { } 0 0 0
elsif (UNIVERSAL::isa($self->{'xml'}, 'XML::libXML')) { } 0 0 0
195 {
196 1 ▶ if( $XML_parser eq 'XML::XPath' or $XML_parser eq 'auto' )
Branch: 1 branch
BranchTrueFalseTotal
if ($XML::XPathScript::XML_parser eq 'XML::XPath' or $XML::XPathScript::XML_parser eq 'auto') { } 0 0 0
Condition0%0/3
$XML::XPathScript::XML_parser eq "XML::XPath" or $XML::XPathScript::XML_parser eq "auto"
l!l&&r!l&&!r
000
MC/DC0%0/2
$XML::XPathScript::XML_parser eq "XML::XPath" or $XML::XPathScript::XML_parser eq "auto"
$XML::XPathScript::XML_parser eq "XML::XPath" $XML::XPathScript::XML_parser eq "auto"
Truth table0%0/3
A or B
A= $XML::XPathScript::XML_parser eq "XML::XPath"B= $XML::XPathScript::XML_parser eq "auto"
ABresult
1X1
011
000
197 {
198 1 $xpath=$self->{xml};
199 1 $XML_parser = 'XML::XPath';
200 }
201 else # parser if XML::LibXML
202 {
203 1 $xpath = XML::LibXML->parse_string( $self->{xml}->toString )->documentElement;
204 }
205 }
206 elsif (UNIVERSAL::isa($self->{xml},"XML::libXML"))
207 {
208 1 ▶ if( $XML_parser eq 'XML::LibXML' or $XML_parser eq 'auto' )
Branch: 1 branch
BranchTrueFalseTotal
if ($XML::XPathScript::XML_parser eq 'XML::LibXML' or $XML::XPathScript::XML_parser eq 'auto') { } 0 0 0
Condition0%0/3
$XML::XPathScript::XML_parser eq "XML::LibXML" or $XML::XPathScript::XML_parser eq "auto"
l!l&&r!l&&!r
000
MC/DC0%0/2
$XML::XPathScript::XML_parser eq "XML::LibXML" or $XML::XPathScript::XML_parser eq "auto"
$XML::XPathScript::XML_parser eq "XML::LibXML" $XML::XPathScript::XML_parser eq "auto"
Truth table0%0/3
A or B
A= $XML::XPathScript::XML_parser eq "XML::LibXML"B= $XML::XPathScript::XML_parser eq "auto"
ABresult
1X1
011
000
209 {
210 1 $xpath=$self->{xml};
211 1 $XML_parser = 'XML::LibXML';
212 }
213 else # parser if xpath
214 {
215 1 $xpath = new XML::XPath( xml => $self->{xml}->toString );
216 }
217 }
218 else
219 {
220 1 ▶ $XML_parser = 'XML::LibXML' if $XML_parser eq 'auto';
Branch: 1 branch
BranchTrueFalseTotal
if $XML::XPathScript::XML_parser eq "auto" 0 0 0
221  
222 1 ▶ if (ref($self->{xml}))
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->{"xml"}) 0 0 0
223 {
224 $xpath= ( $XML_parser eq 'XML::LibXML' ) ?
225 XML::LibXML->new->parse_fh( $self->{xml} )->documentElement :
226 XML::XPath->new(ioref => $self->{xml})
227 1 ▶ }
Branch: 1 branch
BranchTrueFalseTotal
$XML::XPathScript::XML_parser eq 'XML::LibXML' ? : 0 0 0
228 }
229  
230 1 $self->{dom} = $xpath;
231 }
232  
233 8 ▶sub _set_xml_ref {
Subroutine _set_xml_ref: called CC=7 SCAR=19.9
234 8 my $self = shift;
235 8 my $xml = $self->{xml};
236  
237 8 ▶ if ( $XML_parser eq 'XML::LibXML' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($XML::XPathScript::XML_parser eq 'XML::LibXML') { } 7 0 7
238 8 ▶ if ( $xml->isa( 'XML::LibXML::Document' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($xml->isa("XML::LibXML::Document")) 1 6 7
239 2 $self->{dom} = $xml;
240 2 return;
241 }
242  
243 7 ▶ if ( $xml->isa( 'XML::LibXML::Node' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($xml->isa("XML::LibXML::Node")) 1 5 6
244 2 my $dom = XML::LibXML::Document->new;
245 2 $dom->setDocumentElement( $xml );
246 2 $self->{dom} = $dom;
247 2 return;
248 }
249 }
250 else { # XML::XPath
251 1 ▶ if ( $xml->isa( 'XML::XPath' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($xml->isa("XML::XPath")) 0 0 0
252 1 $self->{dom} = $xml;
253 1 return;
254 }
255  
256 1 ▶ if( $xml->isa( 'XML::XPath::Node' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($xml->isa("XML::XPath::Node")) 0 0 0
257 # evil hack
258 1 my $dom = XML::XPath->new( xml => $xml->toString );
259 1 $self->{dom} = $dom;
260 1 return;
261 }
262 }
263  
264 # try to read it as an io
265 6 ▶ $self->{dom} = $XML_parser eq 'XML::LibXML'
Branch: 1 branch
BranchTrueFalseTotal
$XML::XPathScript::XML_parser eq 'XML::LibXML' ? : 5 0 5
266 ? XML::LibXML->new->parse_fh( $xml )->documentElement
267 : XML::XPath->new(ioref => $xml)
268 ;
269  
270 6 return;
271 }
272  
273 70 ▶sub _set_xml_scalar {
Subroutine _set_xml_scalar: called CC=8 SCAR=21.4
274 70 my $self = shift;
275 70 my $xml = $self->{xml};
276  
277 # is it a file?
278 70 ▶ if( index( $xml, "\n" ) == -1 and # quick'n'dirty checks
Branch: 1 branch
BranchTrueFalseTotal
if (index($xml, "\n") == -1 and index($xml, "<") == -1 and index($xml, ">") == -1 and -f $xml) 1 68 69
Condition77%7/9
index($xml, "\n") == -1 and index($xml, "<") == -1 and index($xml, ">") == -1 and -f $xml
!ll&&!rl&&r
6801
index($xml, "\n") == -1 and index($xml, "<") == -1 and index($xml, ">") == -1
!ll&&!rl&&r
6801
index($xml, "\n") == -1 and index($xml, "<") == -1
!ll&&!rl&&r
17511
MC/DC50%2/4
index($xml, "\n") == -1 and index($xml, "<") == -1 and index($xml, ">") == -1 and -f $xml
index($xml, "\n") == -1 index($xml, "<") == -1 index($xml, ">") == -1 -f $xml
Truth table60%3/5
A and B and C and D
A= index($xml, "\n") == -1B= index($xml, "<") == -1C= index($xml, ">") == -1D= -f $xml
ABCDresult
0XXX0
10XX0
110X0
11100
11111
279 index( $xml, '<' ) == -1 and # for non-filename characters
280 index( $xml, '>' ) == -1 and -f $xml ) {
281 2 ▶ open my $fh, '<', $xml or croak "couldn't open xml file $xml: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $xml or croak("couldn't open xml file ${xml}: $!") 1 0 1
282  
283 2 ▶ $self->{dom} = $XML_parser eq 'XML::LibXML'
Branch: 1 branch
BranchTrueFalseTotal
$XML::XPathScript::XML_parser eq 'XML::LibXML' ? : 1 0 1
284 ? XML::LibXML->new->parse_file( $xml )->documentElement
285 : XML::XPath->new( filename => $xml )
286 ;
287  
288 2 return;
289 }
290  
291 # then it must be a string
292  
293 69 ▶ $self->{dom} = $XML_parser eq 'XML::LibXML'
Branch: 1 branch
BranchTrueFalseTotal
$XML::XPathScript::XML_parser eq 'XML::LibXML' ? : 68 0 68
294 ? XML::LibXML->new->parse_string( $xml )->documentElement
295 : XML::XPath->new( xml => $xml );
296  
297 69 return;
298 }
299  
300  
301 10 ▶sub set_stylesheet {
Subroutine set_stylesheet: called CC=2 SCAR=7.0
Pod documented
302 10 my ( $self, $stylesheet ) = @_;
303  
304 10 $self->{compiledstylesheet} = undef;
305 10 $self->{stylesheet} = $stylesheet;
306  
307 10 ▶ $self->compile if $self->{stylesheet};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"stylesheet"} 9 0 9
308 }
309  
310  
311 74 ▶sub process {
Subroutine process: called CC=7 SCAR=19.5
Pod documented
312 74 my ($self, $printer, @extravars) = @_;
313  
314 74 ▶ do { $$printer="" } if (UNIVERSAL::isa($printer, "SCALAR"));
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($printer, "SCALAR")) 66 7 73
315 74 ▶ $self->{printer}=$printer if $printer;
Branch: 1 branch
BranchTrueFalseTotal
if $printer 72 1 73
316  
317 74 ▶ croak "xml document not defined" unless $self->{dom};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"dom"} 0 73 73
318  
319 # FIXME
320 74 ▶ eval { $self->{dom}->ownerDocument->setEncoding( "UTF-8" ) }
Branch: 1 branch
BranchTrueFalseTotal
if $XML::XPathScript::XML_parser eq "XML::LibXML" 73 0 73
321 if $XML_parser eq 'XML::LibXML';
322  
323 {
324 74 local *ORIGINAL_STDOUT;
325 74 *ORIGINAL_STDOUT = *STDOUT;
326 74 local *STDOUT;
327  
328 # Perl 5.6.1 dislikes closed but tied descriptors (causes SEGVage)
329 74 ▶ *STDOUT = *ORIGINAL_STDOUT if $^V lt v5.7.0;
Branch: 1 branch
BranchTrueFalseTotal
if $^V lt v5.7.0 0 73 73
330  
331 74 tie *STDOUT, __PACKAGE__;
332 74 ▶ $self->compile unless $self->{compiledstylesheet};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"compiledstylesheet"} 58 15 73
333 74 my $retval = $self->{compiledstylesheet}->( $self, @extravars );
334 72 untie *STDOUT;
335 72 return $retval;
336 }
337 }
338  
339  
340 95 ▶sub extract {
Subroutine extract: called CC=30 SCAR=45.5
Pod documented
341 95 my ($self,$stylesheet,@includestack) = @_;
342  
343 95 ▶ my $filename = $self->{stylesheet_dependencies}[0] || "stylesheet";
Condition50%1/2
$self->{'stylesheet_dependencies'}[0] || 'stylesheet'
l!l
094
MC/DC0%0/1
$self->{'stylesheet_dependencies'}[0] || 'stylesheet'
$self->{'stylesheet_dependencies'}[0]
Truth table50%1/2
A
A= $self->{'stylesheet_dependencies'}[0]
Aresult
11
00
344  
345 95 my $contents = $self->read_stylesheet( $stylesheet );
346  
347 95 my @tokens = split /(<%[-=~#@]*|-?%>)/, $contents;
348  
349 27 ▶ no warnings qw/ uninitialized /;
Subroutine BEGIN: called CC=1 SCAR=0.0
350  
351 95 my $script;
352 95 my $line = 1;
353 TOKEN:
354 95 ▶ while ( @tokens ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@tokens) 337 94 431
355 337 my $token = shift @tokens;
356  
357 337 ▶ if ( -1 == index $token, '<%' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($token, "<%") == -1) 206 131 337
358 206 $line += $token =~ tr/\n//;
359 206 ▶ $token =~ s/\s+$// if -1 < index $tokens[0], '<%'
Branch: 1 branch
BranchTrueFalseTotal
if index($tokens[0], "<%") != -1 and index($tokens[0], "-") != -1 14 192 206
Condition100%3/3
index($tokens[0], "<%") != -1 and index($tokens[0], "-") != -1
!ll&&!rl&&r
7511714
MC/DC100%2/2
index($tokens[0], "<%") != -1 and index($tokens[0], "-") != -1
index($tokens[0], "<%") != -1 index($tokens[0], "-") != -1
Truth table100%3/3
A and B
A= index($tokens[0], "<%") != -1B= index($tokens[0], "-") != -1
ABresult
0X0
100
111
360 and -1 < index $tokens[0], '-';
361 206 $token =~ s/\|/\\\|/g;
362 # check for include
363 206 $token =~ s{<!--#include.+file=(['"])(.*?)\1.*?-->}
364 19 { '|);'
365 . $self->include_file( $2, @includestack)
366 . 'print(q|'}seg;
367 206 ▶ $script .= 'print(q|'.$token.'|);' if length $token;
Branch: 1 branch
BranchTrueFalseTotal
if length $token 101 105 206
368  
369 206 next TOKEN;
370 }
371  
372 131 $script .= "\n#line $line $filename\n";
373  
374 131 my $opening_tag = $token;
375 131 my $code;
376 my $closing_tag;
377 131 my $level = 1;
378 131 ▶ while( @tokens ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@tokens) 326 0 326
379 326 my $t = shift @tokens;
380 326 ▶ $level++ if -1 < index $t, '<%';
Branch: 1 branch
BranchTrueFalseTotal
if index($t, "<%") != -1 16 310 326
381 326 ▶ $level-- if -1 < index $t, '%>';
Branch: 1 branch
BranchTrueFalseTotal
if index($t, "%>") != -1 147 179 326
382 326 ▶ if ( $level == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($level == 0) 131 195 326
383 131 $closing_tag = $t;
384 131 last;
385 }
386 195 $code .= $t;
387 }
388  
389 131 ▶ die "stylesheet <% %>s are unbalanced: $opening_tag$code\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $closing_tag 0 131 131
390 unless $closing_tag;
391  
392 131 $line += $code =~ tr/\n//;
393  
394 131 ▶ if ( -1 < index $opening_tag, '=' ) {
Branch: 4 branches
BranchTrueFalseTotal
if (index($opening_tag, '=') != -1) { } 53 78 131
elsif (index($opening_tag, '~') != -1) { } 15 63 78
elsif (index($opening_tag, '#') != -1) { } 4 59 63
elsif (index($opening_tag, '@') != -1) { } 3 56 59
395 53 $script .= 'print( '.$code.' );';
396 }
397 elsif ( -1 < index $opening_tag, '~' ) {
398 15 $code =~ s/^\s+//;
399 15 $code =~ s/\s+$//;
400 15 $script .= 'print $processor->apply_templates( qq<'. $code .'> );';
401 }
402 elsif( -1 < index $opening_tag, '#' ) {
403 # do nothing
404 }
405 elsif( -1 < index $opening_tag, '@' ) {
406 3 $code =~ s/^\s+(\S+).*?\n//; # strip first line
407 3 ▶ my $tag = $1
Branch: 1 branch
BranchTrueFalseTotal
my $tag = $1 or die "tag name missing in <%\@ %> at line $line\n" 3 0 3
408 or die "tag name missing in <%\@ %> at line $line\n";
409  
410 3 my $here_delimiter = 'END_TAG';
411 3 ▶ while ( $code =~ /$here_delimiter/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /$here_delimiter/) 0 3 3
412 0 $here_delimiter .= 'x';
413 }
414 3 $script .= <<END_SNIPPET;
415 \$template->set( $tag => { content => <<'$here_delimiter' } );
416 $code
417 $here_delimiter
418 END_SNIPPET
419 }
420 else {
421 # always add a ';', just in case
422 56 $script .= $code . ';';
423 }
424  
425 131 ▶ if ( -1 < index $closing_tag, '-' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($closing_tag, "-") != -1) 18 113 131
426 18 $tokens[0] =~ s/^\s*//;
427 18 my $temp = $&;
428 18 $line += $temp =~ tr/\n//;
429 }
430 }
431  
432 94 return $script;
433  
434 # FIXME not needed anymore
435 # <%- -%> magic
436 0 $contents =~ s#(\s+)<%-([=~]?)#<%$2$1#gs;
437 0 $contents =~ s#-%>(\s+)#$1%>#gs;
438  
439 # <%~ %> magic
440 0 $contents =~ s#<%~\s+(\S+)\s+%>#<%= apply_templates( qq<$1> ) %>#gs;
441  
442 0 $script="#line 1 $filename\n",
443 $line = 1;
444  
445 0 ▶ while ($contents =~ /\G(.*?)(<!--#include|<%[=#]?)/gcs) {
Branch: 1 branch
BranchTrueFalseTotal
if ($contents =~ /\G(.*?)(<!--#include|<%[=#]?)/cgs) 0 0 0
446 0 my ($text, $type) = ($1, $2);
447 0 $line += $text =~ tr/\n//; # count \n's in text
448 0 $text =~ s/\|/\\\|/g;
449 0 $script .= "print(q|$text|);";
450 0 $script .= "\n#line $line $filename\n";
451 0 ▶ if ($type eq '<%=') {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq '<%=') { } 0 0 0
elsif ($type eq '<!--#include') { } 0 0 0
452 0 ▶ $contents =~ /\G(.*?)%>/gcs || die "No terminating '%>' after line $line";
Branch: 1 branch
BranchTrueFalseTotal
$contents =~ /\G(.*?)%>/cgs or die "No terminating '%>' after line $line" 0 0 0
453 0 my $perl = $1;
454 0 $script .= "print( $perl );\n";
455 0 $line += $perl =~ tr/\n//;
456 }
457 elsif ($type eq '<!--#include') {
458 0 my %params;
459 0 ▶ while ($contents =~ /\G(\s+(\w+)\s*=\s*(["'])([^\3]*?)\3|\s*-->)/gcs) {
Branch: 1 branch
BranchTrueFalseTotal
if ($contents =~ /\G(\s+(\w+)\s*=\s*(["'])([^\3]*?)\3|\s*-->)/cgs) 0 0 0
460 0 ▶ last if $1 eq '-->';
Branch: 1 branch
BranchTrueFalseTotal
if $1 eq "-->" 0 0 0
461 0 ▶ $params{$2} = $4 if (defined $2);
Branch: 1 branch
BranchTrueFalseTotal
if defined $2 0 0 0
462 }
463  
464 die "No matching file attribute in #include at line $line"
465 0 ▶ unless $params{file};
Branch: 1 branch
BranchTrueFalseTotal
unless $params{"file"} 0 0 0
466  
467 26 ▶ no warnings qw/ uninitialized /;
Subroutine BEGIN: called CC=1 SCAR=0.0
468 0 $script .= $self->include_file($params{file},@includestack);
469 }
470 else {
471 0 ▶ $contents =~ /\G(.*?)%>/gcs || die "No terminating '%>' after line $line";
Branch: 1 branch
BranchTrueFalseTotal
$contents =~ /\G(.*?)%>/cgs or die "No terminating '%>' after line $line" 0 0 0
472 0 my $perl = $1;
473 0 ▶ if( $type ne '<%#' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type ne "<%#") 0 0 0
474 0 $perl =~ s/;?$/;/s; # add on ; if its missing. As in <% $foo = 'Hello' %>
475 0 $script .= $perl;
476 }
477 0 $line += $perl =~ tr/\n//;
478 }
479 }
480  
481 0 ▶ if ($contents =~ /\G(.+)/gcs) {
Branch: 1 branch
BranchTrueFalseTotal
if ($contents =~ /\G(.+)/cgs) 0 0 0
482 0 my $text = $1;
483 0 $text =~ s/\|/\\\|/g;
484 0 $script .= "print(q|$text|);";
485 }
486  
487 0 return $script;
488 }
489  
490  
491 sub read_stylesheet
492 112 ▶{
Subroutine read_stylesheet: called CC=2 SCAR=6.9
Pod documented
493 111 my( $self, $stylesheet ) = @_;
494
495 # $stylesheet can be a filehandler
496 # or a string
497 111 ▶ if( ref($stylesheet) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $stylesheet) { } 20 91 111
498 20 local $/;
499 20 return <$stylesheet>;
500 }
501 else {
502 91 return $stylesheet;
503 }
504
505 }
506  
507  
508 20 ▶sub include_file {
Subroutine include_file: called CC=7 SCAR=19.7
Pod documented
509 19 my ($self, $filename, @includestack) = @_;
510  
511 19 ▶ if ( $filename !~ m#^\.?/# ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($filename =~ m[^\.?/]) 19 0 19
512 # We guarantee that all values we insert into @includestack begin
513 # either with "/" or "./". This allows us to do the relative
514 # directory thing, and at the same time we get to safely ignore
515 # bizarre URIs inserted by inheriting classes.
516  
517 19 ▶ my $reldir = $includestack[0] && $includestack[0] =~ m#^\.?/#
Branch: 1 branch
BranchTrueFalseTotal
$includestack[0] && $includestack[0] =~ m[^\.?/] ? : 5 14 19
Condition66%2/3
$includestack[0] and $includestack[0] =~ m[^\.?/]
!ll&&!rl&&r
1405
MC/DC50%1/2
$includestack[0] and $includestack[0] =~ m[^\.?/]
$includestack[0] $includestack[0] =~ m[^\.?/]
Truth table66%2/3
A and B
A= $includestack[0]B= $includestack[0] =~ m[^\.?/]
ABresult
0X0
100
111
518 ? dirname($includestack[0])
519 : '.'
520 ;
521  
522 19 $filename = "$reldir/$filename";
523 }
524
525 # are we going recursive?
526 19 ▶ if ( grep { $_ eq $filename } @includestack ) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$_ eq $filename;} @includestack) 2 17 19
527 2 warn 'loop detected in stylesheet include chain: ',
528 join( ' => ', reverse(@includestack), $filename ), "\n";
529 2 return undef;
530 }
531  
532 17 my $stylesheet;
533 17 ▶ unless ( $stylesheet = $self->{stylesheet_cache}{$filename} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($stylesheet = $self->{"stylesheet_cache"}{$filename}) 17 0 17
534 17 ▶ open my $fh, '<', $filename
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $filename or Carp::croak("Can't read include file '${filename}': $!") 17 0 17
535 or Carp::croak "Can't read include file '$filename': $!";
536 17 $stylesheet = $self->{stylesheet_cache}{$filename}
537 = $self->read_stylesheet( $fh );
538 }
539  
540 17 return $self->extract($stylesheet, $filename, @includestack);
541 }
542  
543  
544  
545 # Internal documentation: the return value is an anonymous sub whose
546 # prototype is
547 # &$compiledfunc($xpathscriptobj, $val1, $val2,...);
548  
549 70 ▶sub compile {
Subroutine compile: called CC=4 SCAR=13.9
Pod documented
550 69 my ($self,@extravars) = @_;
551  
552 69 $self->{compiledstylesheet} = undef;
553  
554 69 my $stylesheet;
555 69 $self->{stylesheet_cache} = {};
556  
557 69 ▶ if (exists $self->{stylesheet}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $self->{'stylesheet'}) { } 59 10 69
elsif (exists $self->{'stylesheetfile'}) { } 10 0 10
558 59 $stylesheet=$self->{stylesheet};
559 }
560 elsif (exists $self->{stylesheetfile}) {
561 # This hack fails if $self->{stylesheetfile} contains
562 # double quotes. I think we can ignore this and get
563 # away.
564 10 $stylesheet=qq:<!--#include file="$self->{stylesheetfile}" -->:;
565 }
566 else {
567 0 die "Cannot compile without a stylesheet\n";
568 };
569  
570 69 my $script = $self->extract($stylesheet);
571  
572 69 my $package=gen_package_name();
573  
574 69 my $extravars = join ',', @extravars;
575  
576 69 my $processor = $self->{processor};
577  
578 # needs to be eval'ed first for the constants
579 # to be seen
580 69 eval "package $package;"
581 ."\$processor->import_functional();";
582
583 69 my $eval = <<EOT;
584 package $package;
585 no strict; # Don't moan on sloppyly
586 no warnings; # written stylesheets
587
588 use $XML_parser;
589  
590 sub {
591 my (\$self, $extravars ) = \@_;
592 my \$processor = processor();
593 local \$XML::XPathScript::current=\$self;
594 my \$t = \$processor->{template}
595 = XML::XPathScript::Template->new();
596 my \$template = \$t;
597 local \$XML::XPathScript::trans = \$t;
598 #\$processor->{doc} = \$self->{dom};
599 #\$processor->{parser} = '$XML_parser';
600 #\$processor->{binmode} = \$self->{binmode};
601 #\$processor->{is_interpolating} = \$self->interpolation;
602 #\$processor->{interpolation_regex} = \$self->interpolation_regex;
603  
604 $script
605 }
606 EOT
607  
608 #warn "script ready for compil: $eval";
609 69 local $^W;
610 69 $self->debug( 10, "Compiling code:\n $eval" );
611 69 ▶ my $retval = eval $eval;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
612 69 ▶ die $@ unless defined $retval;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $retval 0 69 69
613  
614 69 return $self->{compiledstylesheet} = $retval;
615 }
616  
617  
618  
619 167 ▶sub print {
Subroutine print: called CC=4 SCAR=13.9
Pod documented
620 26 ▶ no warnings qw/ uninitialized /;
Subroutine BEGIN: called CC=1 SCAR=0.0
621 166 my ($self, @text)=@_;
622 166 my $printer=$self->{printer};
623  
624 166 ▶ if (!defined $printer) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $printer) { } 1 165 166
elsif (ref $printer eq 'CODE') { } 10 155 165
elsif (UNIVERSAL::isa($printer, 'SCALAR')) { } 151 4 155
625 1 print ORIGINAL_STDOUT @text;
626 } elsif (ref($printer) eq 'CODE') {
627 10 $printer->(@text);
628 } elsif (UNIVERSAL::isa($printer, 'SCALAR')) {
629 151 $$printer.= join '', @text;
630 } else {
631 4 local $\=undef;
632 4 print $printer @text;
633 };
634  
635 166 return;
636 }
637  
638  
639 # $self->debug( $level, $message )
640 # Display debugging information
641  
642 72 ▶sub debug {
Subroutine debug: called CC=2 SCAR=7.6
Pod undocumented
643 71 ▶ warn $_[2] if $_[1] <= $debug_level;
Branch: 1 branch
BranchTrueFalseTotal
if $_[1] <= $XML::XPathScript::debug_level 0 71 71
644 }
645  
646  
647 2 ▶sub get_stylesheet_dependencies {
Subroutine get_stylesheet_dependencies: called CC=2 SCAR=7.0
Pod documented
648 1 my $self = shift;
649 1 ▶ $self->compile unless $self->{compiledstylesheet};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"compiledstylesheet"} 1 0 1
650 1 return sort keys %{$self->{stylesheet_cache}};
651 }
652  
653  
654 132 ▶sub processor {
Subroutine processor: called CC=1 SCAR=0.0
Pod documented
655 131 return $_[0]->{processor};
656 }
657  
658  
659 do {
660 25 my $uniquifier;
661 70 ▶sub gen_package_name {
Subroutine gen_package_name: called CC=1 SCAR=0.0
Pod undocumented
662 69 $uniquifier++;
663 69 return "XML::XPathScript::STYLESHEET$uniquifier";
664 }
665 25 };
666  
667  
668 2 ▶sub document {
Subroutine document: called CC=12 SCAR=36.8
Pod documented
669 # warn "Document function called\n";
670 1 my( $self, $uri ) = @_;
671
672 1 my( $results, $parser );
673 1 ▶ if( $XML_parser eq 'XML::XPath' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($XML::XPathScript::XML_parser eq 'XML::XPath') { } 0 1 1
elsif ($XML::XPathScript::XML_parser eq 'XML::LibXML') { } 1 0 1
674 0 ▶ my $xml_parser = XML::Parser->new(
Branch: 1 branch
BranchTrueFalseTotal
$XML::XPath::VERSION < 1.07 ? : 0 0 0
675 ErrorContext => 2,
676 Namespaces => $XML::XPath::VERSION < 1.07 ? 1 : 0,
677 # ParseParamEnt => 1,
678 );
679
680 0 $parser = XML::XPath::XMLParser->new(parser => $xml_parser);
681 0 $results = XML::XPath::NodeSet->new();
682 }
683 elsif ( $XML_parser eq 'XML::LibXML' ) {
684 1 $parser = XML::LibXML->new;
685 1 $results = XML::LibXML::Document->new;
686 }
687 else {
688 0 $self->die( "xml parser not valid: $XML_parser" );
689 }
690  
691
692 1 my $newdoc;
693 # TODO: must handle axkit: scheme a little more cleverly
694 1 ▶ if ($uri =~ /^\w\w+:/ and $uri !~ /^axkit:/ ) { # assume it's scheme://foo uri
Branch: 1 branch
BranchTrueFalseTotal
if ($uri =~ /^\w\w+:/ and not $uri =~ /^axkit:/) { } 0 1 1
Condition33%1/3
$uri =~ /^\w\w+:/ and not $uri =~ /^axkit:/
!ll&&!rl&&r
100
MC/DC0%0/2
$uri =~ /^\w\w+:/ and not $uri =~ /^axkit:/
$uri =~ /^\w\w+:/ not $uri =~ /^axkit:/
Truth table33%1/3
A and not(B)
A= $uri =~ /^\w\w+:/B= not $uri =~ /^axkit:/
ABresult
0X0
100
111
695 0 eval {
696 0 $self->debug( 5, "trying to parse $uri" );
697 0 eval "use LWP::Simple";
698 0 $newdoc = $parser->parse_string( LWP::Simple::get( $uri ) );
699 0 $self->debug( 5, "Parsed OK into $newdoc\n" );
700 };
701 0 ▶ if (my $E = $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $E = $@) 0 0 0
702 0 $self->debug("Parse of '$uri' failed: $E" );
703 }
704 }
705 else {
706 1 $self->debug(3, "Parsing local: $uri\n");
707 1 ▶ if( $XML_parser eq 'XML::LibXML' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($XML::XPathScript::XML_parser eq 'XML::LibXML') { } 1 0 1
elsif ($XML::XPathScript::XML_parser eq 'XML::XPath') { } 0 0 0
708 1 $newdoc = $parser->parse_file( $uri );
709 } elsif( $XML_parser eq 'XML::XPath' ) {
710 0 $newdoc = XML::XPath->new( filename => $uri );
711 }
712 0 else { die "invalid parser: $XML_parser\n"; }
713 }
714  
715 1 ▶ if( $newdoc ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($newdoc) 1 0 1
716 1 ▶ if( $XML_parser eq 'XML::LibXML' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($XML::XPathScript::XML_parser eq 'XML::LibXML') { } 1 0 1
elsif ($XML::XPathScript::XML_parser eq 'XML::XPath') { } 0 0 0
717 1 $results = $newdoc->documentElement();
718 }
719 elsif( $XML_parser eq 'XML::XPath' ) {
720 0 $results = $newdoc->findnodes('/')->[0]->getChildNodes->[0];
721 }
722 }
723
724 1 $self->debug(8, "XPathScript: document() returning");
725 1 return $results;
726 }
727  
728 73 ▶sub TIEHANDLE { my $self = ''; bless \$self, $_[0] }
Subroutine TIEHANDLE: called CC=1 SCAR=0.0
729 167 ▶sub PRINT {
Subroutine PRINT: called CC=1 SCAR=0.0
730 166 my $self = shift;
731 166 return XML::XPathScript::current()->print( @_ );
732 }
733 2 ▶sub BINMODE {
Subroutine BINMODE: called CC=1 SCAR=0.0
734 1 return XML::XPathScript::current()->binmode( @_ );
735 }
736  
737 25 1;
738  
739 __END__
« XPathScript/Stylesheet/DocBook2LaTeX.pm ↑ Summary XPathScript/Processor.pm »