lib/XML/Compile/Cache.pm

filter: Statementstmt 62.1% 199 / 320 Branchbran 40.5% 107 / 264 Conditioncond 63.6% 28 / 44 MC/DCmcdc 36.0% 9 / 25 Subroutinesub 65.9% 29 / 44 Podpod 88.4% 23 / 26 totaltotal 54.6% 395 / 723 CC 158 SCAR 35.6
CC
158
Cov
53%
mergeCompileOptions
145.5
compileAll
110.0
_cleanup_hooks
72.0
CRAP 35.3
« blib/lib/XML/Compile/Cache.pm ↑ Summary
1 # Copyrights 2008-2016 by [Mark Overmeer].
2 # For other contributors see ChangeLog.
3 # See the manual pages for details on the licensing terms.
4 # Pod stripped from pm file by OODoc 2.02.
5 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 package XML::Compile::Cache;
9 3 ▶use vars '$VERSION';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 $VERSION = '1.05';
11  
12 3 ▶use base 'XML::Compile::Schema';
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 3 ▶use Log::Report 'xml-compile-cache', syntax => 'SHORT';
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 3 ▶use XML::Compile::Util qw/pack_type unpack_type/;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 3 ▶use List::Util qw/first/;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 3 ▶use Scalar::Util qw/weaken/;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 3 ▶use XML::LibXML::Simple qw/XMLin/;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21  
22 sub init($)
23 2 ▶{ my ($self, $args) = @_;
Subroutine init: called CC=6 SCAR=18.3
Pod undocumented
24 2 $self->addPrefixes($args->{prefixes});
25  
26 2 $self->SUPER::init($args);
27  
28 2 ▶ $self->{XCC_opts} = delete $args->{opts_rw} || [];
Condition50%1/2
delete $args->{'opts_rw'} || []
l!l
02
MC/DC0%0/1
delete $args->{'opts_rw'} || []
delete $args->{'opts_rw'}
Truth table50%1/2
A
A= delete $args->{'opts_rw'}
Aresult
11
00
29 2 ▶ $self->{XCC_ropts} = delete $args->{opts_readers} || [];
Condition50%1/2
delete $args->{'opts_readers'} || []
l!l
02
MC/DC0%0/1
delete $args->{'opts_readers'} || []
delete $args->{'opts_readers'}
Truth table50%1/2
A
A= delete $args->{'opts_readers'}
Aresult
11
00
30 2 ▶ $self->{XCC_wopts} = delete $args->{opts_writers} || [];
Condition50%1/2
delete $args->{'opts_writers'} || []
l!l
02
MC/DC0%0/1
delete $args->{'opts_writers'} || []
delete $args->{'opts_writers'}
Truth table50%1/2
A
A= delete $args->{'opts_writers'}
Aresult
11
00
31 2 ▶ $self->{XCC_undecl} = delete $args->{allow_undeclared} || 0;
Condition50%1/2
delete $args->{'allow_undeclared'} || 0
l!l
02
MC/DC0%0/1
delete $args->{'allow_undeclared'} || 0
delete $args->{'allow_undeclared'}
Truth table50%1/2
A
A= delete $args->{'allow_undeclared'}
Aresult
11
00
32  
33 2 $self->{XCC_dropts} = {}; # declared opts
34 2 $self->{XCC_dwopts} = {};
35 2 $self->{XCC_uropts} = {}; # undeclared opts
36 2 $self->{XCC_uwopts} = {};
37  
38 2 $self->{XCC_readers} = {}; # compiled code refs;
39 2 $self->{XCC_writers} = {};
40  
41 2 $self->typemap($args->{typemap});
42 2 $self->xsiType($args->{xsi_type});
43 2 ▶ $self->anyElement($args->{any_element} || 'ATTEMPT');
Condition50%1/2
$args->{'any_element'} || 'ATTEMPT'
l!l
02
MC/DC0%0/1
$args->{'any_element'} || 'ATTEMPT'
$args->{'any_element'}
Truth table50%1/2
A
A= $args->{'any_element'}
Aresult
11
00
44  
45 2 $self;
46 }
47  
48 #----------------------
49  
50  
51 sub typemap(@)
52 2 ▶{ my $self = shift;
Subroutine typemap: called CC=6 SCAR=24.2
Pod documented
53 2 ▶ my $t = $self->{XCC_typemap} ||= {};
Condition50%1/2
$self->{"XCC_typemap"} ||= {}
l!l
02
MC/DC0%0/1
$self->{"XCC_typemap"} ||= {}
$self->{"XCC_typemap"}
Truth table50%1/2
A
A= $self->{"XCC_typemap"}
Aresult
11
00
54 my @d = @_ > 1 ? @_ : !defined $_[0] ? ()
55 2 ▶ : ref $_[0] eq 'HASH' ? %{$_[0]} : @{$_[0]};
Branch: 3 branches
BranchTrueFalseTotal
@_ > 1 ? : 0 2 2
!defined($_[0]) ? : 2 0 2
ref $_[0] eq 'HASH' ? : 0 0 0
56 2 ▶ while(@d) { my $k = $self->findName(shift @d); $t->{$k} = shift @d }
Branch: 1 branch
BranchTrueFalseTotal
if (@d) 0 2 2
57 2 $t;
58 }
59  
60  
61 sub addXsiType(@)
62 3 ▶{ my $self = shift;
Subroutine addXsiType: called CC=9 SCAR=23.9
Pod documented
63 3 ▶ my $x = $self->{XCC_xsi_type} ||= {};
Condition100%2/2
$self->{"XCC_xsi_type"} ||= {}
l!l
12
MC/DC100%1/1
$self->{"XCC_xsi_type"} ||= {}
$self->{"XCC_xsi_type"}
Truth table100%2/2
A
A= $self->{"XCC_xsi_type"}
Aresult
11
00
64 my @d = @_ > 1 ? @_ : !defined $_[0] ? ()
65 3 ▶ : ref $_[0] eq 'HASH' ? %{$_[0]} : @{$_[0]};
Branch: 3 branches
BranchTrueFalseTotal
@_ > 1 ? : 1 2 3
!defined($_[0]) ? : 2 0 2
ref $_[0] eq 'HASH' ? : 0 0 0
66  
67 3 ▶ while(@d)
Branch: 1 branch
BranchTrueFalseTotal
if (@d) 1 3 4
68 1 { my $k = $self->findName(shift @d);
69 1 my $a = shift @d;
70 1 ▶ $a = $self->namespaces->autoexpand_xsi_type($k) || []
Branch: 1 branch
BranchTrueFalseTotal
if $a eq "AUTO" 1 0 1
Condition50%1/2
$self->namespaces->autoexpand_xsi_type($k) || []
l!l
10
MC/DC0%0/1
$self->namespaces->autoexpand_xsi_type($k) || []
$self->namespaces->autoexpand_xsi_type($k)
Truth table50%1/2
A
A= $self->namespaces->autoexpand_xsi_type($k)
Aresult
11
00
71 if $a eq 'AUTO';
72  
73 1 ▶ push @{$x->{$k}}
Branch: 1 branch
BranchTrueFalseTotal
ref $a eq 'ARRAY' ? : 1 0 1
74 , ref $a eq 'ARRAY' ? (map $self->findName($_), @$a)
75 : $self->findName($a);
76 }
77  
78 3 $x;
79 }
80 3 *xsiType = \&addXsiType;
81  
82  
83 sub allowUndeclared(;$)
84 7 ▶{ my $self = shift;
Subroutine allowUndeclared: called CC=2 SCAR=6.9
Pod documented
85 7 ▶ @_ ? ($self->{XCC_undecl} = shift) : $self->{XCC_undecl};
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 1 6 7
86 }
87  
88  
89 sub anyElement($)
90 2 ▶{ my ($self, $anyelem) = @_;
Subroutine anyElement: called CC=3 SCAR=13.0
Pod documented
91  
92 # the "$self" in XCC_ropts would create a ref-cycle, causing a
93 # memory leak.
94 2 my $s = $self; weaken $s;
95  
96 my $code
97 0 ▶ = $anyelem eq 'ATTEMPT' ? sub {$s->_convertAnyTyped(@_)}
Subroutine __ANON__: not called CC=1 SCAR=6.9
98 0 ▶ : $anyelem eq 'SLOPPY' ? sub {$s->_convertAnySloppy(@_)}
Subroutine __ANON__: not called CC=1 SCAR=6.9
99 2 ▶ : $anyelem;
Branch: 2 branches
BranchTrueFalseTotal
$anyelem eq 'ATTEMPT' ? : 2 0 2
$anyelem eq 'SLOPPY' ? : 0 0 0
100
101 2 $self->addCompileOptions(READERS => any_element => $code);
102 2 $code;
103 }
104  
105 #----------------------
106  
107  
108 sub addPrefixes(@)
109 4 ▶{ my $self = shift;
Subroutine addPrefixes: called CC=12 SCAR=25.1
Pod documented
110 4 ▶ my $p = $self->{XCC_namespaces} ||= {};
Condition100%2/2
$self->{"XCC_namespaces"} ||= {}
l!l
22
MC/DC100%1/1
$self->{"XCC_namespaces"} ||= {}
$self->{"XCC_namespaces"}
Truth table100%2/2
A
A= $self->{"XCC_namespaces"}
Aresult
11
00
111 4 my $first = shift;
112 4 ▶ @_ or defined $first
Branch: 1 branch
BranchTrueFalseTotal
@_ or defined $first or return $p 1 3 4
Condition100%3/3
@_ or defined $first
l!l&&r!l&&!r
211
MC/DC100%2/2
@_ or defined $first
@_ defined $first
Truth table100%3/3
A or B
A= @_B= defined $first
ABresult
1X1
011
000
113 or return $p;
114  
115 my @pairs
116 3 ▶ = @_ ? ($first, @_)
Branch: 3 branches
BranchTrueFalseTotal
@_ ? : 2 1 3
ref $first eq 'ARRAY' ? : 1 0 1
ref $first eq 'HASH' ? : 0 0 0
117 : ref $first eq 'ARRAY' ? @$first
118 : ref $first eq 'HASH' ? %$first
119 : error __x"prefixes() expects list of PAIRS, an ARRAY or a HASH";
120  
121 3 ▶ my $a = $self->{XCC_prefixes} ||= {};
Condition100%2/2
$self->{"XCC_prefixes"} ||= {}
l!l
21
MC/DC100%1/1
$self->{"XCC_prefixes"} ||= {}
$self->{"XCC_prefixes"}
Truth table100%2/2
A
A= $self->{"XCC_prefixes"}
Aresult
11
00
122 3 ▶ while(@pairs)
Branch: 1 branch
BranchTrueFalseTotal
if (@pairs) 3 3 6
123 3 { my ($prefix, $ns) = (shift @pairs, shift @pairs);
124 3 ▶ $p->{$ns} ||= { uri => $ns, prefix => $prefix, used => 0 };
Condition100%2/2
$p->{$ns} ||= {"uri", $ns, "prefix", $prefix, "used", 0}
l!l
12
MC/DC100%1/1
$p->{$ns} ||= {"uri", $ns, "prefix", $prefix, "used", 0}
$p->{$ns}
Truth table100%2/2
A
A= $p->{$ns}
Aresult
11
00
125  
126 3 ▶ if(my $def = $a->{$prefix})
Branch: 1 branch
BranchTrueFalseTotal
if (my $def = $a->{$prefix}) { } 1 2 3
127 1 ▶ { if($def->{uri} ne $ns)
Branch: 1 branch
BranchTrueFalseTotal
if ($def->{"uri"} ne $ns) 0 1 1
128 { error __x"prefix `{prefix}' already refers to {uri}, cannot use it for {ns}"
129 0 , prefix => $prefix, uri => $def->{uri}, ns => $ns;
130 }
131 }
132 else
133 2 { $a->{$prefix} = $p->{$ns};
134 2 trace "register prefix $prefix for '$ns'";
135 }
136 }
137 3 $p;
138 }
139  
140  
141  
142 sub prefixes(@)
143 3 ▶{ my $self = shift;
Subroutine prefixes: called CC=3 SCAR=11.1
Pod documented
144 3 ▶ return $self->addPrefixes(@_) if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 1 2 3
145 2 ▶ $self->{XCC_namespaces} || {};
Branch: 1 branch
BranchTrueFalseTotal
$self->{"XCC_namespaces"} or {} 0 2 2
146 }
147  
148  
149 1 ▶sub prefix($) { $_[0]->{XCC_prefixes}{$_[1]} }
Subroutine prefix: called CC=1 SCAR=0.0
Pod documented
150  
151 # [0.995] should this be public?
152 0 ▶sub byPrefixTable() { shift->{XCC_prefixes} }
Subroutine byPrefixTable: not called CC=1 SCAR=6.9
Pod undocumented
153  
154  
155 sub prefixFor($)
156 0 ▶{ my $def = $_[0]->{XCC_namespaces}{$_[1]} or return ();
Subroutine prefixFor: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
my $def = $_[0]{"XCC_namespaces"}{$_[1]} or return () 0 0 0
157 0 $def->{used}++;
158 0 $def->{prefix};
159 }
160  
161  
162 sub addNicePrefix($$)
163 0 ▶{ my ($self, $base, $ns) = @_;
Subroutine addNicePrefix: not called CC=6 SCAR=37.4
Pod documented
164 0 ▶ if(my $def = $self->prefix($base))
Branch: 1 branch
BranchTrueFalseTotal
if (my $def = $self->prefix($base)) { } 0 0 0
165 0 ▶ { return $base if $def->{uri} eq $ns;
Branch: 1 branch
BranchTrueFalseTotal
if $def->{"uri"} eq $ns 0 0 0
166 }
167 else
168 0 { $self->addPrefixes($base => $ns);
169 0 return $base;
170 }
171  
172 0 ▶ $base .= '01' if $base !~ m/[0-9]$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $base =~ /[0-9]$/ 0 0 0
173 0 ▶ while(my $def = $self->prefix($base))
Branch: 1 branch
BranchTrueFalseTotal
if (my $def = $self->prefix($base)) 0 0 0
174 0 ▶ { return $base if $def->{uri} eq $ns;
Branch: 1 branch
BranchTrueFalseTotal
if $def->{"uri"} eq $ns 0 0 0
175 0 $base++;
176 }
177 0 $self->addPrefixes($base => $ns);
178 0 $base;
179 }
180  
181  
182 sub learnPrefixes($)
183 2 ▶{ my ($self, $node) = @_;
Subroutine learnPrefixes: called CC=8 SCAR=27.2
Pod documented
184 2 my $namespaces = $self->prefixes;
185  
186 PREFIX:
187 2 foreach my $ns ($node->getNamespaces) # learn preferred ns
188 4 { my ($prefix, $uri) = ($ns->getLocalName, $ns->getData);
189 4 ▶ next if !defined $prefix || $namespaces->{$uri};
Branch: 1 branch
BranchTrueFalseTotal
if not defined $prefix or $namespaces->{$uri} 3 1 4
Condition100%3/3
not defined $prefix or $namespaces->{$uri}
l!l&&r!l&&!r
211
MC/DC100%2/2
not defined $prefix or $namespaces->{$uri}
not defined $prefix $namespaces->{$uri}
Truth table100%3/3
not(A) or B
A= not defined $prefixB= $namespaces->{$uri}
ABresult
1X1
011
000
190  
191 1 ▶ if(my $def = $self->prefix($prefix))
Branch: 1 branch
BranchTrueFalseTotal
if (my $def = $self->prefix($prefix)) { } 0 1 1
192 0 ▶ { next PREFIX if $def->{uri} eq $uri;
Branch: 1 branch
BranchTrueFalseTotal
if $def->{"uri"} eq $uri 0 0 0
193 }
194 else
195 1 { $self->addPrefixes($prefix => $uri);
196 1 next PREFIX;
197 }
198  
199 0 $prefix =~ s/0?$/0/;
200 0 ▶ while(my $def = $self->prefix($prefix))
Branch: 1 branch
BranchTrueFalseTotal
if (my $def = $self->prefix($prefix)) 0 0 0
201 0 ▶ { next PREFIX if $def->{uri} eq $uri;
Branch: 1 branch
BranchTrueFalseTotal
if $def->{"uri"} eq $uri 0 0 0
202 0 $prefix++;
203 }
204 0 $self->addPrefixes($prefix => $uri);
205 }
206 }
207  
208 sub addSchemas($@)
209 2 ▶{ my ($self, $xml) = (shift, shift);
Subroutine addSchemas: called CC=1 SCAR=0.0
Pod documented
210 2 $self->learnPrefixes($xml);
211 2 $self->SUPER::addSchemas($xml, @_);
212 }
213  
214  
215 sub prefixed($;$)
216 0 ▶{ my $self = shift;
Subroutine prefixed: not called CC=5 SCAR=34.0
Pod documented
217 0 ▶ my ($ns, $local) = @_==2 ? @_ : unpack_type(shift);
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 ? : 0 0 0
218 0 ▶ $ns or return $local;
Branch: 1 branch
BranchTrueFalseTotal
$ns or return $local 0 0 0
219 0 my $prefix = $self->prefixFor($ns);
220 0 ▶ defined $prefix
Branch: 1 branch
BranchTrueFalseTotal
defined $prefix or &error(&__x("no prefix known for namespace `{ns}', use addPrefixes()", "ns", $ns)) 0 0 0
221 or error __x"no prefix known for namespace `{ns}', use addPrefixes()"
222 , ns => $ns;
223  
224 0 ▶ length $prefix ? "$prefix:$local" : $local;
Branch: 1 branch
BranchTrueFalseTotal
length $prefix ? : 0 0 0
225 }
226  
227 #----------------------
228  
229  
230 sub compileAll(;$$)
231 0 ▶{ my ($self, $need, $usens) = @_;
Subroutine compileAll: not called CC=10 SCAR=47.0
Pod documented
232 0 ▶ my ($need_r, $need_w) = $self->_need($need || 'RW');
Condition0%0/2
$need || 'RW'
l!l
00
MC/DC0%0/1
$need || 'RW'
$need
Truth table0%0/2
A
A= $need
Aresult
11
00
233  
234 0 ▶ if($need_r)
Branch: 1 branch
BranchTrueFalseTotal
if ($need_r) 0 0 0
235 0 { foreach my $type (keys %{$self->{XCC_dropts}})
236 0 ▶ { if(defined $usens)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $usens) 0 0 0
237 0 { my ($myns, $local) = unpack_type $type;
238 0 ▶ next if $usens eq $myns;
Branch: 1 branch
BranchTrueFalseTotal
if $usens eq $myns 0 0 0
239 }
240 0 $self->reader($type);
241 }
242 }
243  
244 0 ▶ if($need_w)
Branch: 1 branch
BranchTrueFalseTotal
if ($need_w) 0 0 0
245 0 { foreach my $type (keys %{$self->{XCC_dwopts}})
246 0 ▶ { if(defined $usens)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $usens) 0 0 0
247 0 { my ($myns, $local) = unpack_type $type;
248 0 ▶ next if $usens eq $myns;
Branch: 1 branch
BranchTrueFalseTotal
if $usens eq $myns 0 0 0
249 }
250 0 $self->writer($type);
251 }
252 }
253 }
254  
255  
256 sub _same_params($$)
257 2 ▶{ my ($f, $s) = @_;
Subroutine _same_params: called CC=6 SCAR=26.2
258 2 ▶ @$f==@$s or return 0;
Branch: 1 branch
BranchTrueFalseTotal
@$f == @$s or return 0 0 2 2
259 2 ▶ for(my $i=0; $i<@$f; $i++)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$f) 0 2 2
260 0 ▶ { return 0 if !defined $f->[$i] ? defined $s->[$i]
Branch: 3 branches
BranchTrueFalseTotal
if !defined($f->[$i]) ? defined $s->[$i] : (!defined($s->[$i]) ? 1 : $f->[$i] ne $s->[$i]) 0 0 0
!defined($f->[$i]) ? : 0 0 0
!defined($s->[$i]) ? : 0 0 0
261 : !defined $s->[$i] ? 1 : $f->[$i] ne $s->[$i];
262 }
263 2 1;
264 }
265  
266 sub reader($@)
267 8 ▶{ my ($self, $name) = (shift, shift);
Subroutine reader: called CC=11 SCAR=24.6
Pod documented
268 8 my %args = @_;
269 8 my $type = $self->findName($name);
270 8 my $readers = $self->{XCC_readers};
271  
272 8 ▶ if(exists $self->{XCC_dropts}{$type})
Branch: 3 branches
BranchTrueFalseTotal
if (exists $self->{'XCC_dropts'}{$type}) { } 4 4 8
elsif ($self->allowUndeclared) { } 2 2 4
elsif (exists $self->{'XCC_dwopts'}{$type}) { } 1 1 2
273 4 ▶ { trace __x"ignoring options to pre-declared reader {name}"
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 4 4
274 , name => $name if @_;
275  
276 return $readers->{$type}
277 4 ▶ if $readers->{$type};
Branch: 1 branch
BranchTrueFalseTotal
if $readers->{$type} 2 2 4
278 }
279 elsif($self->allowUndeclared)
280 2 ▶ { if(my $ur = $self->{XCC_uropts}{$type})
Branch: 1 branch
BranchTrueFalseTotal
if (my $ur = $self->{'XCC_uropts'}{$type}) { } 1 1 2
281 { # do not use cached version when options differ
282 _same_params $ur, \@_
283 or return $args{is_type}
284 1 ▶ ? $self->compileType(READER => $type, @_)
Branch: 2 branches
BranchTrueFalseTotal
&_same_params($ur, \@_) or return $args{"is_type"} ? $self->compileType("READER", $type, @_) : $self->compile("READER", $type, @_) 0 1 1
$args{'is_type'} ? : 0 0 0
285 : $self->compile(READER => $type, @_);
286 }
287 else
288 1 { $self->{XCC_uropts}{$type} = \@_;
289 }
290 }
291 elsif(exists $self->{XCC_dwopts}{$type})
292 1 { error __x"type {name} is only declared as writer", name => $name }
293 1 else { error __x"type {name} is not declared", name => $name }
294  
295 $readers->{$type} ||= $args{is_type}
296 4 ▶ ? $self->compileType(READER => $type, @_)
Branch: 1 branch
BranchTrueFalseTotal
$args{'is_type'} ? : 0 3 3
Condition66%2/3
$readers->{$type} ||= $args{"is_type"} ? $self->compileType("READER", $type, @_) : $self->compile("READER", $type, @_)
l!l&&r!l&&!r
130
MC/DC0%0/2
$readers->{$type} ||= $args{"is_type"} ? $self->compileType("READER", $type, @_) : $self->compile("READER", $type, @_)
$readers->{$type} $args{"is_type"} ? $self->compileType("READER", $type, @_) : $self->compile("READER", $type, @_)
Truth table66%2/3
A ||= B
A= $readers->{$type}B= $args{"is_type"} ? $self->compileType("READER", $type, @_) : $self->compile("READER", $type, @_)
ABresult
1X1
011
000
297 : $self->compile(READER => $type, @_);
298 }
299  
300  
301 sub writer($%)
302 8 ▶{ my ($self, $name) = (shift, shift);
Subroutine writer: called CC=11 SCAR=24.6
Pod documented
303 8 my %args = @_;
304 8 my $type = $self->findName($name);
305 8 my $writers = $self->{XCC_writers};
306  
307 8 ▶ if(exists $self->{XCC_dwopts}{$type})
Branch: 3 branches
BranchTrueFalseTotal
if (exists $self->{'XCC_dwopts'}{$type}) { } 4 4 8
elsif ($self->{'XCC_undecl'}) { } 2 2 4
elsif (exists $self->{'XCC_dropts'}{$type}) { } 1 1 2
308 4 ▶ { trace __x"ignoring options to pre-declared writer {name}"
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 4 4
309 , name => $name if @_;
310  
311 return $writers->{$type}
312 4 ▶ if $writers->{$type};
Branch: 1 branch
BranchTrueFalseTotal
if $writers->{$type} 2 2 4
313 }
314 elsif($self->{XCC_undecl})
315 2 ▶ { if(my $ur = $self->{XCC_uwopts}{$type})
Branch: 1 branch
BranchTrueFalseTotal
if (my $ur = $self->{'XCC_uwopts'}{$type}) { } 1 1 2
316 { # do not use cached version when options differ
317 _same_params $ur, \@_
318 or return $args{is_type}
319 1 ▶ ? $self->compileType(WRITER => $type, @_)
Branch: 2 branches
BranchTrueFalseTotal
&_same_params($ur, \@_) or return $args{"is_type"} ? $self->compileType("WRITER", $type, @_) : $self->compile("WRITER", $type, @_) 0 1 1
$args{'is_type'} ? : 0 0 0
320 : $self->compile(WRITER => $type, @_);
321 }
322 else
323 1 { $self->{XCC_uwopts}{$type} = \@_;
324 }
325 }
326 elsif(exists $self->{XCC_dropts}{$type})
327 1 { error __x"type {name} is only declared as reader", name => $name;
328 }
329 else
330 1 { error __x"type {name} is not declared", name => $name;
331 }
332  
333 $writers->{$type} ||= $args{is_type}
334 4 ▶ ? $self->compileType(WRITER => $type, @_)
Branch: 1 branch
BranchTrueFalseTotal
$args{'is_type'} ? : 0 3 3
Condition66%2/3
$writers->{$type} ||= $args{"is_type"} ? $self->compileType("WRITER", $type, @_) : $self->compile("WRITER", $type, @_)
l!l&&r!l&&!r
130
MC/DC0%0/2
$writers->{$type} ||= $args{"is_type"} ? $self->compileType("WRITER", $type, @_) : $self->compile("WRITER", $type, @_)
$writers->{$type} $args{"is_type"} ? $self->compileType("WRITER", $type, @_) : $self->compile("WRITER", $type, @_)
Truth table66%2/3
A ||= B
A= $writers->{$type}B= $args{"is_type"} ? $self->compileType("WRITER", $type, @_) : $self->compile("WRITER", $type, @_)
ABresult
1X1
011
000
335 : $self->compile(WRITER => $type, @_);
336  
337 }
338  
339 sub template($$@)
340 0 ▶{ my ($self, $action, $name) = (shift, shift, shift);
Subroutine template: not called CC=2 SCAR=17.9
Pod documented
341 0 ▶ $action =~ m/^[A-Z]*$/
Branch: 1 branch
BranchTrueFalseTotal
$action =~ /^[A-Z]*$/ or &error(&__x("missing or illegal action parameter to template()")) 0 0 0
342 or error __x"missing or illegal action parameter to template()";
343  
344 0 my $type = $self->findName($name);
345 0 my @opts = $self->mergeCompileOptions($action, $type, \@_);
346 0 $self->SUPER::template($action, $type, @opts);
347 }
348  
349  
350 sub addCompileOptions(@)
351 2 ▶{ my $self = shift;
Subroutine addCompileOptions: called CC=7 SCAR=26.4
Pod documented
352 2 ▶ my $need = @_%2 ? shift : 'RW';
Branch: 1 branch
BranchTrueFalseTotal
@_ % 2 ? : 2 0 2
353  
354 my $set
355 = $need eq 'RW' ? $self->{XCC_opts}
356 : $need eq 'READERS' ? $self->{XCC_ropts}
357 : $need eq 'WRITERS' ? $self->{XCC_wopts}
358 2 ▶ : error __x"addCompileOptions() requires option set name, not {got}"
Branch: 3 branches
BranchTrueFalseTotal
$need eq 'RW' ? : 0 2 2
$need eq 'READERS' ? : 2 0 2
$need eq 'WRITERS' ? : 0 0 0
359 , got => $need;
360  
361 2 ▶ if(ref $set eq 'HASH')
Branch: 1 branch
BranchTrueFalseTotal
if (ref $set eq 'HASH') { } 0 2 2
362 0 ▶ { while(@_) { my $k = shift; $set->{$k} = shift } }
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
363 2 else { push @$set, @_ }
364 2 $set;
365 }
366  
367 # Create a list with options for X::C::Schema::compile(), from a list of ARRAYs
368 # and HASHES with options. The later options overrule the older, but in some
369 # cases, the new values are added. This method knows how some of the options
370 # of ::compile() behave. [last update X::C v0.98]
371  
372 sub mergeCompileOptions($$$)
373 6 ▶{ my ($self, $action, $type, $opts) = @_;
Subroutine mergeCompileOptions: called CC=24 SCAR=49.8
Pod undocumented
374  
375 my @action_opts
376 = ($action eq 'READER' || $action eq 'PERL')
377 ? ($self->{XCC_ropts}, $self->{XCC_dropts}{$type})
378 6 ▶ : ($self->{XCC_wopts}, $self->{XCC_dwopts}{$type});
Branch: 1 branch
BranchTrueFalseTotal
$action eq 'READER' || $action eq 'PERL' ? : 3 3 6
Condition66%2/3
$action eq "READER" or $action eq "PERL"
l!l&&r!l&&!r
303
MC/DC50%1/2
$action eq "READER" or $action eq "PERL"
$action eq "READER" $action eq "PERL"
Truth table66%2/3
A or B
A= $action eq "READER"B= $action eq "PERL"
ABresult
1X1
011
000
379  
380 6 my %p = %{$self->{XCC_namespaces}};
381 6 my %t = %{$self->{XCC_typemap}};
382 6 my %x = %{$self->{XCC_xsi_type}};
383 6 my %opts = (prefixes => \%p, hooks => [], typemap => \%t, xsi_type => \%x);
384  
385 # flatten list of parameters
386 24 ▶ my @take = map {!defined $_ ? () : ref $_ eq 'ARRAY' ? @$_ : %$_ }
Branch: 2 branches
BranchTrueFalseTotal
!defined($_) ? : 2 22 24
ref $_ eq 'ARRAY' ? : 22 0 22
387 6 $self->{XCC_opts}, @action_opts, $opts;
388  
389 6 ▶ while(@take)
Branch: 1 branch
BranchTrueFalseTotal
if (@take) 3 6 9
390 3 { my ($opt, $val) = (shift @take, shift @take);
391 3 ▶ defined $val or next;
Branch: 1 branch
BranchTrueFalseTotal
defined $val or next 0 3 3
392  
393 3 ▶ if($opt eq 'prefixes')
Branch: 6 branches
BranchTrueFalseTotal
if ($opt eq 'prefixes') { } 0 3 3
elsif ($opt eq 'hooks' or $opt eq 'hook') { } 0 3 3
elsif ($opt eq 'typemap') { } 0 3 3
elsif ($opt eq 'key_rewrite') { } 0 3 3
elsif ($opt eq 'xsi_type') { } 0 3 3
elsif ($opt eq 'ignore_unused_tags') { } 0 3 3
394 0 { my $t = $self->_namespaceTable($val, 1, 0); # expand
395 0 @p{keys %$t} = values %$t; # overwrite old def if exists
396 }
397 ▶ elsif($opt eq 'hooks' || $opt eq 'hook')
Condition33%1/3
$opt eq "hooks" or $opt eq "hook"
l!l&&r!l&&!r
003
MC/DC0%0/2
$opt eq "hooks" or $opt eq "hook"
$opt eq "hooks" $opt eq "hook"
Truth table33%1/3
A or B
A= $opt eq "hooks"B= $opt eq "hook"
ABresult
1X1
011
000
398 0 { my $hooks = $self->_cleanup_hooks($val);
399 0 ▶ unshift @{$opts{hooks}}, ref $hooks eq 'ARRAY' ? @$hooks : $hooks
Branch: 2 branches
BranchTrueFalseTotal
if $hooks 0 0 0
ref $hooks eq 'ARRAY' ? : 0 0 0
400 if $hooks;
401 }
402 elsif($opt eq 'typemap')
403 0 ▶ { $val ||= {};
Condition0%0/2
$val ||= {}
l!l
00
MC/DC0%0/1
$val ||= {}
$val
Truth table0%0/2
A
A= $val
Aresult
11
00
404 0 ▶ if(ref $val eq 'ARRAY')
Branch: 1 branch
BranchTrueFalseTotal
if (ref $val eq 'ARRAY') { } 0 0 0
405 0 ▶ { while(@$val)
Branch: 1 branch
BranchTrueFalseTotal
if (@$val) 0 0 0
406 0 { my $k = $self->findName(shift @$val);
407 0 $t{$k} = shift @$val;
408 }
409 }
410 else
411 0 ▶ { while(my($k, $v) = each %$val)
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$val) 0 0 0
412 0 { $t{$self->findName($k)} = $v;
413 }
414 }
415 }
416 elsif($opt eq 'key_rewrite')
417 0 ▶ { unshift @{$opts{key_rewrite}}, ref $val eq 'ARRAY' ? @$val : $val;
Branch: 1 branch
BranchTrueFalseTotal
ref $val eq 'ARRAY' ? : 0 0 0
418 }
419 elsif($opt eq 'xsi_type')
420 0 ▶ { while(my ($t, $a) = each %$val)
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$val) 0 0 0
421 0 ▶ { my @a = ref $a eq 'ARRAY' ? map($self->findName($_), @$a)
Branch: 1 branch
BranchTrueFalseTotal
ref $a eq 'ARRAY' ? : 0 0 0
422 : $self->findName($a);
423 0 push @{$x{$self->findName($t)}}, @a;
424 }
425 }
426 elsif($opt eq 'ignore_unused_tags')
427 0 ▶ { $opts{$opt} = defined $opts{$opt} ? qr/$opts{$opt}|$val/ : $val;
Branch: 1 branch
BranchTrueFalseTotal
defined $opts{$opt} ? : 0 0 0
428 }
429 else
430 3 { $opts{$opt} = $val;
431 }
432 }
433  
434 6 %opts;
435 }
436  
437 # rewrite hooks
438 sub _cleanup_hooks($)
439 0 ▶{ my ($self, $hooks) = @_;
Subroutine _cleanup_hooks: not called CC=8 SCAR=42.8
440 0 ▶ $hooks or return;
Branch: 1 branch
BranchTrueFalseTotal
$hooks or return 0 0 0
441  
442 # translate prefixed type names into full names
443 0 ▶ foreach my $hook (ref $hooks eq 'ARRAY' ? @$hooks : $hooks)
Branch: 1 branch
BranchTrueFalseTotal
ref $hooks eq 'ARRAY' ? : 0 0 0
444 0 ▶ { if(my $types = $hook->{type})
Branch: 2 branches
BranchTrueFalseTotal
if (my $types = $hook->{'type'}) { } 0 0 0
elsif (my $ext = $hook->{'extends'}) { } 0 0 0
445 { $hook->{type} =
446 0 ▶ [ map {ref $_ eq 'Regexp' ? $_ : $self->findName($_)}
Branch: 2 branches
BranchTrueFalseTotal
ref $_ eq 'Regexp' ? : 0 0 0
ref $types eq 'ARRAY' ? : 0 0 0
447 ref $types eq 'ARRAY' ? @$types : $types ];
448 }
449 elsif(my $ext = $hook->{extends})
450 0 { $hook->{extends} = $self->findName($ext);
451 }
452 }
453 0 $hooks;
454 }
455  
456 3 my %need = (READER => [1,0], WRITER => [0,1], RW => [1,1]);
457 3 $need{READERS} = $need{READER};
458 3 $need{WRITERS} = $need{WRITER};
459  
460 sub _need($)
461 3 ▶{ my $need = $need{$_[1]}
Subroutine _need: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
my $need = $need{$_[1]} or &error(&__x("use READER, WRITER or RW, not {dir}", "dir", $_[1])) 0 3 3
462 or error __x"use READER, WRITER or RW, not {dir}", dir => $_[1];
463 3 @$need;
464 }
465  
466 # support prefixes on types
467 sub addHook(@)
468 0 ▶{ my $self = shift;
Subroutine addHook: not called CC=2 SCAR=17.9
Pod documented
469 0 ▶ my $hook = @_ > 1 ? {@_} : shift;
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 ? : 0 0 0
470 0 $self->_cleanup_hooks($hook);
471 0 $self->SUPER::addHook($hook);
472 }
473  
474 sub compile($$@)
475 6 ▶{ my ($self, $action, $elem) = splice @_, 0, 3;
Subroutine compile: called CC=2 SCAR=7.1
Pod documented
476 6 ▶ defined $elem
Branch: 1 branch
BranchTrueFalseTotal
defined $elem or &error(&__x("compile() requires action and type parameters")) 0 6 6
477 or error __x"compile() requires action and type parameters";
478  
479 6 $self->SUPER::compile
480 ( $action => $self->findName($elem)
481 , $self->mergeCompileOptions($action, $elem, \@_)
482 );
483 }
484  
485 sub compileType($$@)
486 0 ▶{ my ($self, $action, $type) = splice @_, 0, 3;
Subroutine compileType: not called CC=2 SCAR=17.9
Pod documented
487 0 ▶ defined $type
Branch: 1 branch
BranchTrueFalseTotal
defined $type or &error(&__x("compileType() requires action and type parameters")) 0 0 0
488 or error __x"compileType() requires action and type parameters";
489  
490 0 $self->SUPER::compileType
491 ( $action => $self->findName($type)
492 , $self->mergeCompileOptions($action, $type, \@_)
493 );
494 }
495  
496 #----------------------
497  
498  
499 sub declare($$@)
500 3 ▶{ my ($self, $need, $names, @opts) = @_;
Subroutine declare: called CC=9 SCAR=22.5
Pod documented
501 3 ▶ my $opts = @opts==1 ? shift @opts : \@opts;
Branch: 1 branch
BranchTrueFalseTotal
@opts == 1 ? : 0 3 3
502 3 ▶ $opts = [ %$opts ] if ref $opts eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $opts eq "HASH" 0 3 3
503  
504 3 my ($need_r, $need_w) = $self->_need($need);
505  
506 3 ▶ foreach my $name (ref $names eq 'ARRAY' ? @$names : $names)
Branch: 1 branch
BranchTrueFalseTotal
ref $names eq 'ARRAY' ? : 0 3 3
507 3 { my $type = $self->findName($name);
508 3 trace "declare $type $need";
509  
510 3 ▶ if($need_r)
Branch: 1 branch
BranchTrueFalseTotal
if ($need_r) 2 1 3
511 2 ▶ { defined $self->{XCC_dropts}{$type}
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{"XCC_dropts"}{$type} and &warning(&__x("reader {name} declared again", "name", $name)) 0 2 2
512 and warning __x"reader {name} declared again", name => $name;
513 2 $self->{XCC_dropts}{$type} = $opts;
514 }
515  
516 3 ▶ if($need_w)
Branch: 1 branch
BranchTrueFalseTotal
if ($need_w) 2 1 3
517 2 ▶ { defined $self->{XCC_dwopts}{$type}
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{"XCC_dwopts"}{$type} and &warning(&__x("writer {name} declared again", "name", $name)) 0 2 2
518 and warning __x"writer {name} declared again", name => $name;
519 2 $self->{XCC_dwopts}{$type} = $opts;
520 }
521 }
522  
523 3 $self;
524 }
525  
526  
527 sub findName($)
528 28 ▶{ my ($self, $name) = @_;
Subroutine findName: called CC=7 SCAR=24.0
Pod documented
529 28 ▶ defined $name
Branch: 1 branch
BranchTrueFalseTotal
defined $name or &panic("findName called without name") 0 28 28
530 or panic "findName called without name";
531  
532 28 ▶ return $name
Branch: 1 branch
BranchTrueFalseTotal
if substr($name, 0, 1) eq "{" 10 18 28
533 if substr($name, 0, 1) eq '{';
534  
535 18 ▶ my ($prefix,$local) = $name =~ m/^([\w-]*)\:(\S*)$/ ? ($1,$2) : ('',$name);
Branch: 1 branch
BranchTrueFalseTotal
$name =~ /^([\w-]*)\:(\S*)$/ ? : 18 0 18
536 18 my $def = $self->{XCC_prefixes}{$prefix};
537 18 ▶ unless($def)
Branch: 1 branch
BranchTrueFalseTotal
unless ($def) 0 18 18
538 0 ▶ { return $name if $prefix eq ''; # namespace-less
Branch: 1 branch
BranchTrueFalseTotal
if $prefix eq "" 0 0 0
539 trace __x"known prefixes: {prefixes}"
540 0 , prefixes => [ sort keys %{$self->{XCC_prefixes}} ];
541 0 error __x"unknown name prefix `{prefix}' for `{name}'"
542 , prefix => $prefix, name => $name;
543 }
544  
545 18 ▶ length $local ? pack_type($def->{uri}, $local) : $def->{uri};
Branch: 1 branch
BranchTrueFalseTotal
length $local ? : 18 0 18
546 }
547  
548  
549 sub printIndex(@)
550 3 ▶{ my $self = shift;
Subroutine printIndex: called CC=11 SCAR=24.0
Pod documented
551 3 ▶ my $fh = @_ % 2 ? shift : select;
Branch: 1 branch
BranchTrueFalseTotal
@_ % 2 ? : 3 0 3
552 3 my %args = @_;
553 3 ▶ my $decl = exists $args{show_declared} ? delete $args{show_declared} : 1;
Branch: 1 branch
BranchTrueFalseTotal
exists $args{'show_declared'} ? : 0 3 3
554  
555 3 ▶ return $self->SUPER::printIndex($fh, %args)
Branch: 1 branch
BranchTrueFalseTotal
unless $decl 0 3 3
556 unless $decl;
557  
558 3 my $output = '';
559 3 open my($out), '>', \$output;
560  
561 3 $self->SUPER::printIndex($out, %args);
562  
563 3 close $out;
564 3 my @output = split /(?<=\n)/, $output;
565 3 my $ns = '';
566 3 foreach (@output)
567 18 ▶ { $ns = $1 if m/^namespace\:\s+(\S+)/;
Branch: 1 branch
BranchTrueFalseTotal
if /^namespace\:\s+(\S+)/ 3 15 18
568 18 ▶ my $local = m/^\s+(\S+)\s*$/ ? $1 : next;
Branch: 1 branch
BranchTrueFalseTotal
/^\s+(\S+)\s*$/ ? : 12 6 18
569 12 my $type = pack_type $ns, $local;
570  
571 substr($_, 1, 1)
572 = $self->{XCC_readers}{$type} ? 'R'
573 12 ▶ : $self->{XCC_dropts}{$type} ? 'r' : ' ';
Branch: 2 branches
BranchTrueFalseTotal
$self->{'XCC_readers'}{$type} ? : 3 9 12
$self->{'XCC_dropts'}{$type} ? : 2 7 9
574  
575 substr($_, 2, 1)
576 = $self->{XCC_writers}{$type} ? 'W'
577 12 ▶ : $self->{XCC_dwopts}{$type} ? 'w' : ' ';
Branch: 2 branches
BranchTrueFalseTotal
$self->{'XCC_writers'}{$type} ? : 3 9 12
$self->{'XCC_dwopts'}{$type} ? : 2 7 9
578 }
579 3 $fh->print(@output);
580 }
581  
582 #---------------
583 # Convert ANY elements and attributes
584  
585 sub _convertAnyTyped(@)
586 0 ▶{ my ($self, $type, $nodes, $path, $read) = @_;
Subroutine _convertAnyTyped: not called CC=4 SCAR=30.0
587  
588 0 my $key = $read->keyRewrite($type);
589 0 ▶ my $reader = try { $self->reader($type) };
Subroutine __ANON__: not called CC=1 SCAR=6.9
590 0 ▶ if($@)
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
591 0 { trace "cannot auto-convert 'any': ".$@->wasFatal->message;
592 0 return ($key => $nodes);
593 }
594 0 trace "auto-convert known type for 'any': $type";
595  
596 0 ▶ my @nodes = ref $nodes eq 'ARRAY' ? @$nodes : $nodes;
Branch: 1 branch
BranchTrueFalseTotal
ref $nodes eq 'ARRAY' ? : 0 0 0
597 0 my @convert = map $reader->($_), @nodes;
598 0 ▶ ($key => (@convert==1 ? $convert[0] : \@convert) );
Branch: 1 branch
BranchTrueFalseTotal
@convert == 1 ? : 0 0 0
599 }
600  
601 sub _convertAnySloppy(@)
602 0 ▶{ my ($self, $type, $nodes, $path, $read) = @_;
Subroutine _convertAnySloppy: not called CC=5 SCAR=34.0
603  
604 0 my $key = $read->keyRewrite($type);
605 0 ▶ my $reader = try { $self->reader($type) };
Subroutine __ANON__: not called CC=1 SCAR=6.9
606 0 ▶ if($@)
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 0 0
607 { # unknown type or untyped...
608 0 my @convert = map XMLin($_), @$nodes;
609 0 ▶ return ($key => @convert==1 ? $convert[0] : \@convert);
Branch: 1 branch
BranchTrueFalseTotal
@convert == 1 ? : 0 0 0
610 }
611 else
612 0 { trace "auto-convert known 'any' $type";
613 0 ▶ my @nodes = ref $nodes eq 'ARRAY' ? @$nodes : $nodes;
Branch: 1 branch
BranchTrueFalseTotal
ref $nodes eq 'ARRAY' ? : 0 0 0
614 0 my @convert = map $reader->($_), @nodes;
615  
616 0 ▶ ($key => @convert==1 ? $convert[0] : \@convert);
Branch: 1 branch
BranchTrueFalseTotal
@convert == 1 ? : 0 0 0
617 }
618 }
619  
620 3 1;
« blib/lib/XML/Compile/Cache.pm ↑ Summary