Compile/Schema.pm

filter: Statementstmt 79.1% 212 / 268 Branchbran 58.0% 87 / 150 Conditioncond 60.8% 45 / 74 MC/DCmcdc 38.6% 17 / 44 Subroutinesub 81.5% 31 / 38 Podpod 94.4% 17 / 18 totaltotal 69.0% 409 / 592 CC 115 SCAR 30.1
CC
115
Cov
70%
compile
50.2
template
25.2
_parseFile
20.0
CRAP 20.3
« Compile/Translate.pm ↑ Summary Compile/Schema/NameSpaces.pm »
1 # Copyrights 2006-2024 by [Mark Overmeer <markov@cpan.org>].
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.03.
5 # This code is part of distribution XML-Compile. Meta-POD processed with
6 # OODoc into POD and HTML manual-pages. See README.md
7 # Copyright Mark Overmeer. Licensed under the same terms as Perl itself.
8  
9 package XML::Compile::Schema;{
10 50 our $VERSION = '1.64';
11 }
12  
13 50 ▶use base 'XML::Compile';
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 50 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 50 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 50 ▶use Log::Report 'xml-compile';
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 50 ▶use List::Util qw/first/;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 50 ▶use XML::LibXML ();
Subroutine BEGIN: called CC=1 SCAR=0.0
22 50 ▶use File::Spec ();
Subroutine BEGIN: called CC=1 SCAR=0.0
23 50 ▶use File::Basename qw/basename/;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 50 ▶use Digest::MD5 qw/md5_hex/;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 50 ▶use XML::Compile::Schema::Specs;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 50 ▶use XML::Compile::Schema::Instance;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 50 ▶use XML::Compile::Schema::NameSpaces;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 50 ▶use XML::Compile::Util qw/SCHEMA2001 SCHEMA2001i unpack_type/;
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 50 ▶use XML::Compile::Translate ();
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33  
34 sub init($)
35 52 ▶{ my ($self, $args) = @_;
Subroutine init: called CC=10 SCAR=28.7
Pod undocumented
36 52 $self->{namespaces} = XML::Compile::Schema::NameSpaces->new;
37 52 $self->SUPER::init($args);
38  
39 $self->importDefinitions($args->{top}, %$args)
40 52 ▶ if $args->{top};
Branch: 1 branch
BranchTrueFalseTotal
if $args->{"top"} 52 0 52
41  
42 52 $self->{hooks} = [];
43 52 ▶ if(my $h1 = $args->{hook})
Branch: 1 branch
BranchTrueFalseTotal
if (my $h1 = $args->{"hook"}) 0 52 52
44 0 ▶ { $self->addHook(ref $h1 eq 'ARRAY' ? @$h1 : $h1);
Branch: 1 branch
BranchTrueFalseTotal
ref $h1 eq 'ARRAY' ? : 0 0 0
45 }
46 52 ▶ if(my $h2 = $args->{hooks})
Branch: 1 branch
BranchTrueFalseTotal
if (my $h2 = $args->{"hooks"}) 0 52 52
47 0 ▶ { $self->addHook($_) for ref $h2 eq 'ARRAY' ? @$h2 : $h2;
Branch: 1 branch
BranchTrueFalseTotal
ref $h2 eq 'ARRAY' ? : 0 0 0
48 }
49
50 52 $self->{key_rewrite} = [];
51 52 ▶ if(my $kr = $args->{key_rewrite})
Branch: 1 branch
BranchTrueFalseTotal
if (my $kr = $args->{"key_rewrite"}) 0 52 52
52 0 ▶ { $self->addKeyRewrite(ref $kr eq 'ARRAY' ? @$kr : $kr);
Branch: 1 branch
BranchTrueFalseTotal
ref $kr eq 'ARRAY' ? : 0 0 0
53 }
54  
55 52 $self->{block_nss} = [];
56 52 $self->blockNamespace($args->{block_namespace});
57  
58 52 ▶ $self->{typemap} = $args->{typemap} || {};
Condition50%1/2
$args->{'typemap'} || {}
l!l
052
MC/DC0%0/1
$args->{'typemap'} || {}
$args->{'typemap'}
Truth table50%1/2
A
A= $args->{'typemap'}
Aresult
11
00
59 52 $self->{unused_tags} = $args->{ignore_unused_tags};
60  
61 52 $self;
62 }
63  
64 #--------------------------------------
65  
66  
67 sub addHook(@)
68 1 ▶{ my $self = shift;
Subroutine addHook: called CC=3 SCAR=12.5
Pod documented
69 1 ▶ push @{$self->{hooks}}, @_>1 ? {@_} : defined $_[0] ? shift : ();
Branch: 2 branches
BranchTrueFalseTotal
@_ > 1 ? : 1 0 1
defined $_[0] ? : 0 0 0
70 1 $self;
71 }
72  
73  
74 sub addHooks(@)
75 0 ▶{ my $self = shift;
Subroutine addHooks: not called CC=2 SCAR=17.9
Pod documented
76 0 $self->addHook($_) for @_;
77 0 $self;
78 }
79  
80  
81 sub hooks(;$)
82 760 ▶{ my $hooks = shift->{hooks};
Subroutine hooks: called CC=3 SCAR=12.5
Pod documented
83 760 ▶ my $dir = shift or return @$hooks;
Branch: 1 branch
BranchTrueFalseTotal
my $dir = shift() or return @$hooks 760 0 760
84 760 ▶ grep +(!$_->{action} || $_->{action} eq $dir), @$hooks;
Condition33%1/3
!$_->{'action'} || $_->{'action'} eq $dir
l!l&&r!l&&!r
800
MC/DC0%0/2
!$_->{'action'} || $_->{'action'} eq $dir
!$_->{'action'} $_->{'action'} eq $dir
Truth table33%1/3
not(A) || B
A= !$_->{'action'}B= $_->{'action'} eq $dir
ABresult
1X1
011
000
85 }
86  
87  
88 sub addTypemaps(@)
89 0 ▶{ my $map = shift->{typemap};
Subroutine addTypemaps: not called CC=2 SCAR=17.9
Pod documented
90 0 ▶ while(@_ > 1)
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) 0 0 0
91 0 { my $k = shift;
92 0 $map->{$k} = shift;
93 }
94 0 $map;
95 }
96 50 *addTypemap = \&addTypemaps;
97  
98  
99 sub addSchemas($@)
100 54 ▶{ my ($self, $node, %opts) = @_;
Subroutine addSchemas: called CC=7 SCAR=19.7
Pod documented
101 54 ▶ defined $node or return ();
Branch: 1 branch
BranchTrueFalseTotal
defined $node or return () 54 0 54
102  
103 54 my @nsopts;
104 54 foreach my $o (qw/source filename target_namespace
105 element_form_default attribute_form_default/)
106 270 ▶ { push @nsopts, $o => delete $opts{$o} if exists $opts{$o};
Branch: 1 branch
BranchTrueFalseTotal
if exists $opts{$o} 56 214 270
107 }
108  
109 54 ▶ UNIVERSAL::isa($node, __PACKAGE__)
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::isa($node, __PACKAGE__) and &error(&__x("use useSchema(), not addSchemas() for a {got} object", "got", ref $node)) 0 54 54
110 and error __x"use useSchema(), not addSchemas() for a {got} object"
111 , got => ref $node;
112  
113 54 ▶ UNIVERSAL::isa($node, 'XML::LibXML::Node')
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::isa($node, "XML::LibXML::Node") or &error(&__x("addSchema() requires an XML::LibXML::Node")) 54 0 54
114 or error __x"addSchema() requires an XML::LibXML::Node";
115  
116 54 ▶ $node = $node->documentElement
Branch: 1 branch
BranchTrueFalseTotal
if $node->isa("XML::LibXML::Document") 0 54 54
117 if $node->isa('XML::LibXML::Document');
118  
119 54 my $nss = $self->namespaces;
120 54 my @schemas;
121  
122 $self->walkTree
123 ( $node,
124 76 ▶ sub { my $this = shift;
Subroutine __ANON__: called CC=4 SCAR=13.9
125 76 ▶ return 1 unless $this->isa('XML::LibXML::Element')
Branch: 1 branch
BranchTrueFalseTotal
unless $this->isa("XML::LibXML::Element") and $this->localName eq "schema" 18 58 76
Condition100%3/3
$this->isa("XML::LibXML::Element") and $this->localName eq "schema"
!ll&&!rl&&r
14458
MC/DC100%2/2
$this->isa("XML::LibXML::Element") and $this->localName eq "schema"
$this->isa("XML::LibXML::Element") $this->localName eq "schema"
Truth table100%3/3
A and B
A= $this->isa("XML::LibXML::Element")B= $this->localName eq "schema"
ABresult
0X0
100
111
126 && $this->localName eq 'schema';
127  
128 58 ▶ my $schema = XML::Compile::Schema::Instance->new($this, @nsopts)
Branch: 1 branch
BranchTrueFalseTotal
my $schema = "XML::Compile::Schema::Instance"->new($this, @nsopts) or next 58 0 58
129 or next;
130  
131 58 $nss->add($schema);
132 58 push @schemas, $schema;
133 58 return 0;
134 }
135 54 );
136 54 @schemas;
137 }
138  
139  
140 sub useSchema(@)
141 0 ▶{ my $self = shift;
Subroutine useSchema: not called CC=3 SCAR=24.8
Pod documented
142 0 foreach my $schema (@_)
143 0 ▶ { error __x"useSchema() accepts only {pkg} extensions, not {got}"
Condition0%0/3
ref $schema || $schema
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $schema || $schema
ref $schema $schema
Truth table0%0/3
A || B
A= ref $schemaB= $schema
ABresult
1X1
011
000
144 , pkg => __PACKAGE__, got => (ref $schema || $schema);
145 0 $self->namespaces->use($schema);
146 }
147 0 $self;
148 }
149  
150  
151 sub addKeyRewrite(@)
152 0 ▶{ my $self = shift;
Subroutine addKeyRewrite: not called CC=2 SCAR=17.9
Pod documented
153 0 unshift @{$self->{key_rewrite}}, @_;
154 0 ▶ defined wantarray ? $self->_key_rewrite(undef) : ();
Branch: 1 branch
BranchTrueFalseTotal
defined wantarray ? : 0 0 0
155 }
156  
157 sub _key_rewrite($)
158 779 ▶{ my $self = shift;
Subroutine _key_rewrite: called CC=9 SCAR=22.2
159 779 ▶ my @more = map { ref $_ eq 'ARRAY' ? @$_ : defined $_ ? $_ : () } @_;
Branch: 2 branches
BranchTrueFalseTotal
ref $_ eq 'ARRAY' ? : 3 776 779
defined $_ ? : 10 766 776
160  
161 779 my ($pref_all, %pref, @other);
162 779 foreach my $rule (@more, @{$self->{key_rewrite}})
163 18 ▶ { if($rule eq 'PREFIXED') { $pref_all++ }
Branch: 2 branches
BranchTrueFalseTotal
if ($rule eq 'PREFIXED') { } 4 14 18
elsif ($rule =~ /^PREFIXED\((.*)\)/) { } 0 14 14
164 0 elsif($rule =~ m/^PREFIXED\((.*)\)/) { $pref{$_}++ for split /\,/, $1 }
165 14 else { push @other, $rule }
166 }
167  
168 779 ▶ ( ( $pref_all ? 'PREFIXED'
Branch: 2 branches
BranchTrueFalseTotal
$pref_all ? : 4 775 779
keys %pref ? : 0 775 775
169 : keys %pref ? 'PREFIXED('.join(',', sort keys %pref).')'
170 : ()), @other );
171 }
172  
173  
174 sub blockNamespace(@)
175 52 ▶{ my $self = shift;
Subroutine blockNamespace: called CC=1 SCAR=0.0
Pod documented
176 52 push @{$self->{block_nss}}, @_;
177 }
178  
179 sub _block_nss(@)
180 779 ▶{ my $self = shift;
Subroutine _block_nss: called CC=2 SCAR=7.0
181 1558 ▶ grep defined, map {ref $_ eq 'ARRAY' ? @$_ : $_}
Branch: 1 branch
BranchTrueFalseTotal
ref $_ eq 'ARRAY' ? : 0 1558 1558
182 779 @_, @{$self->{block_nss}};
183 }
184  
185 #--------------------------------------
186  
187  
188 sub compile($$@)
189 760 ▶{ my ($self, $action, $type, %args) = @_;
Subroutine compile: called CC=34 SCAR=39.2
Pod documented
190 760 ▶ defined $type or return ();
Branch: 1 branch
BranchTrueFalseTotal
defined $type or return () 760 0 760
191  
192 760 ▶ if(exists $args{validation})
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{'validation'}) { } 0 760 760
193 0 { $args{check_values} = $args{validation};
194 0 $args{check_occurs} = $args{validation};
195 0 $args{ignore_facets} = ! $args{validation};
196 }
197 else
198 760 ▶ { exists $args{check_values} or $args{check_values} = 1;
Branch: 1 branch
BranchTrueFalseTotal
exists $args{"check_values"} or $args{"check_values"} = 1 758 2 760
199 760 ▶ exists $args{check_occurs} or $args{check_occurs} = 1;
Branch: 1 branch
BranchTrueFalseTotal
exists $args{"check_occurs"} or $args{"check_occurs"} = 1 3 757 760
200 }
201  
202 my $iut = exists $args{ignore_unused_tags}
203 760 ▶ ? $args{ignore_unused_tags} : $self->{unused_tags};
Branch: 1 branch
BranchTrueFalseTotal
exists $args{'ignore_unused_tags'} ? : 0 760 760
204  
205 $args{ignore_unused_tags}
206 760 ▶ = !defined $iut ? undef : ref $iut eq 'Regexp' ? $iut : qr/^/;
Branch: 2 branches
BranchTrueFalseTotal
!defined($iut) ? : 760 0 760
ref $iut eq 'Regexp' ? : 0 0 0
207  
208 exists $args{include_namespaces}
209 760 ▶ or $args{include_namespaces} = 1;
Branch: 1 branch
BranchTrueFalseTotal
exists $args{"include_namespaces"} or $args{"include_namespaces"} = 1 755 5 760
210  
211 760 ▶ if($args{sloppy_integers} ||= 0)
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"sloppy_integers"} ||= 0) 94 666 760
Condition100%2/2
$args{"sloppy_integers"} ||= 0
l!l
94666
MC/DC100%1/1
$args{"sloppy_integers"} ||= 0
$args{"sloppy_integers"}
Truth table100%2/2
A
A= $args{"sloppy_integers"}
Aresult
11
00
212 94 { eval "require Math::BigInt";
213 94 ▶ panic "require Math::BigInt or sloppy_integers:\n$@"
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 94 94
214 if $@;
215 }
216  
217 760 ▶ if($args{sloppy_floats} ||= 0)
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"sloppy_floats"} ||= 0) 43 717 760
Condition100%2/2
$args{"sloppy_floats"} ||= 0
l!l
43717
MC/DC100%1/1
$args{"sloppy_floats"} ||= 0
$args{"sloppy_floats"}
Truth table100%2/2
A
A= $args{"sloppy_floats"}
Aresult
11
00
218 43 { eval "require Math::BigFloat";
219 43 ▶ panic "require Math::BigFloat by sloppy_floats:\n$@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 43 43
220 }
221  
222 760 ▶ if($args{json_friendly} ||= 0)
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"json_friendly"} ||= 0) 0 760 760
Condition50%1/2
$args{"json_friendly"} ||= 0
l!l
0760
MC/DC0%0/1
$args{"json_friendly"} ||= 0
$args{"json_friendly"}
Truth table50%1/2
A
A= $args{"json_friendly"}
Aresult
11
00
223 0 { eval "require Types::Serialiser";
224 0 ▶ panic "require Types::Serialiser by json_friendly:\n$@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
225 }
226  
227 $args{prefixes} = $self->_namespaceTable
228 (($args{prefixes} || $args{output_namespaces})
229 , $args{namespace_reset}
230 , !($args{use_default_namespace} || $args{use_default_prefix})
231 # use_default_prefix renamed in 0.90
232 760 ▶ );
Condition66%4/6
$args{'prefixes'} || $args{'output_namespaces'}
l!l&&r!l&&!r
430717
$args{'use_default_namespace'} || $args{'use_default_prefix'}
l!l&&r!l&&!r
3400420
MC/DC50%1/2
$args{'prefixes'} || $args{'output_namespaces'}
$args{'prefixes'} $args{'output_namespaces'}
MC/DC50%1/2
$args{'use_default_namespace'} || $args{'use_default_prefix'}
$args{'use_default_namespace'} $args{'use_default_prefix'}
Truth table66%2/3
A || B
A= $args{'prefixes'}B= $args{'output_namespaces'}
ABresult
1X1
011
000
Truth table66%2/3
A || B
A= $args{'use_default_namespace'}B= $args{'use_default_prefix'}
ABresult
1X1
011
000
233  
234 760 my $nss = $self->namespaces;
235  
236 760 my ($h1, $h2) = (delete $args{hook}, delete $args{hooks});
237 760 my @hooks = $self->hooks($action);
238 760 ▶ push @hooks, ref $h1 eq 'ARRAY' ? @$h1 : $h1 if $h1;
Branch: 2 branches
BranchTrueFalseTotal
if $h1 8 752 760
ref $h1 eq 'ARRAY' ? : 0 8 8
239 760 ▶ push @hooks, ref $h2 eq 'ARRAY' ? @$h2 : $h2 if $h2;
Branch: 2 branches
BranchTrueFalseTotal
if $h2 0 760 760
ref $h2 eq 'ARRAY' ? : 0 0 0
240  
241 760 ▶ my %map = ( %{$self->{typemap}}, %{$args{typemap} || {}} );
Condition100%2/2
$args{'typemap'} || {}
l!l
6754
MC/DC100%1/1
$args{'typemap'} || {}
$args{'typemap'}
Truth table100%2/2
A
A= $args{'typemap'}
Aresult
11
00
242 760 trace "schema compile $action for $type";
243  
244 760 my @rewrite = $self->_key_rewrite(delete $args{key_rewrite});
245 760 my @blocked = $self->_block_nss(delete $args{block_namespace});
246  
247 760 ▶ $args{abstract_types} ||= 'ERROR';
Condition50%1/2
$args{"abstract_types"} ||= "ERROR"
l!l
0760
MC/DC0%0/1
$args{"abstract_types"} ||= "ERROR"
$args{"abstract_types"}
Truth table50%1/2
A
A= $args{"abstract_types"}
Aresult
11
00
248 760 ▶ $args{mixed_elements} ||= 'ATTRIBUTES';
Condition100%2/2
$args{"mixed_elements"} ||= "ATTRIBUTES"
l!l
6754
MC/DC100%1/1
$args{"mixed_elements"} ||= "ATTRIBUTES"
$args{"mixed_elements"}
Truth table100%2/2
A
A= $args{"mixed_elements"}
Aresult
11
00
249 760 ▶ $args{default_values} ||= $action eq 'READER' ? 'EXTEND' : 'IGNORE';
Branch: 1 branch
BranchTrueFalseTotal
$action eq 'READER' ? : 393 354 747
Condition66%2/3
$args{"default_values"} ||= $action eq "READER" ? "EXTEND" : "IGNORE"
l!l&&r!l&&!r
137470
MC/DC0%0/2
$args{"default_values"} ||= $action eq "READER" ? "EXTEND" : "IGNORE"
$args{"default_values"} $action eq "READER" ? "EXTEND" : "IGNORE"
Truth table66%2/3
A ||= B
A= $args{"default_values"}B= $action eq "READER" ? "EXTEND" : "IGNORE"
ABresult
1X1
011
000
250  
251 # Option rename in 0.88
252 760 ▶ $args{any_element} ||= delete $args{anyElement};
Condition66%2/3
$args{"any_element"} ||= delete $args{"anyElement"}
l!l&&r!l&&!r
70753
MC/DC50%1/2
$args{"any_element"} ||= delete $args{"anyElement"}
$args{"any_element"} delete $args{"anyElement"}
Truth table66%2/3
A ||= B
A= $args{"any_element"}B= delete $args{"anyElement"}
ABresult
1X1
011
000
253 760 ▶ $args{any_attribute} ||= delete $args{anyAttribute};
Condition66%2/3
$args{"any_attribute"} ||= delete $args{"anyAttribute"}
l!l&&r!l&&!r
70753
MC/DC50%1/2
$args{"any_attribute"} ||= delete $args{"anyAttribute"}
$args{"any_attribute"} delete $args{"anyAttribute"}
Truth table66%2/3
A ||= B
A= $args{"any_attribute"}B= delete $args{"anyAttribute"}
ABresult
1X1
011
000
254  
255 760 ▶ if(my $xi = $args{xsi_type})
Branch: 1 branch
BranchTrueFalseTotal
if (my $xi = $args{"xsi_type"}) 6 754 760
256 6 { my $nss = $self->namespaces;
257 6 foreach (keys %$xi)
258 6 ▶ { $xi->{$_} = $nss->autoexpand_xsi_type($_) if $xi->{$_} eq 'AUTO';
Branch: 1 branch
BranchTrueFalseTotal
if $xi->{$_} eq "AUTO" 2 4 6
259 }
260 }
261  
262 760 my $transl = XML::Compile::Translate->new
263 ( $action
264 , nss => $self->namespaces
265 );
266  
267 760 $transl->compile
268 ( $type, %args
269 , hooks => \@hooks
270 , typemap => \%map
271 , rewrite => \@rewrite
272 , block_namespace => \@blocked
273 );
274 }
275  
276 # also used in ::Cache init()
277 sub _namespaceTable($;$$)
278 779 ▶{ my ($self, $table, $reset_count, $block_default) = @_;
Subroutine _namespaceTable: called CC=7 SCAR=19.6
279 779 ▶ $table = +{ reverse @$table }
Branch: 1 branch
BranchTrueFalseTotal
if ref $table eq "ARRAY" 37 742 779
280 if ref $table eq 'ARRAY';
281  
282 $table->{$_} = +{ uri => $_, prefix => $table->{$_} }
283 779 for grep ref $table->{$_} ne 'HASH', keys %$table;
284  
285 779 ▶ if($reset_count)
Branch: 1 branch
BranchTrueFalseTotal
if ($reset_count) 0 779 779
286 0 { $_->{used} = 0 for values %$table;
287 }
288  
289 $table->{''} = +{ uri => '', prefix => '', used => 0 }
290 779 ▶ if $block_default && !grep $_->{prefix} eq '', values %$table;
Branch: 1 branch
BranchTrueFalseTotal
if $block_default and not grep(($_->{"prefix"} eq ""), values %$table) 433 346 779
Condition100%3/3
$block_default and not grep(($_->{"prefix"} eq ""), values %$table)
!ll&&!rl&&r
3424433
MC/DC100%2/2
$block_default and not grep(($_->{"prefix"} eq ""), values %$table)
$block_default not grep(($_->{"prefix"} eq ""), values %$table)
Truth table100%3/3
A and not(B)
A= $block_defaultB= not grep(($_->{"prefix"} eq ""), values %$table)
ABresult
0X0
100
111
291  
292 # very strong preference for 'xsi'
293 779 $table->{&SCHEMA2001i} = +{ uri => SCHEMA2001i, prefix => 'xsi', used => 0 };
294  
295 779 $table;
296 }
297  
298  
299 sub compileType($$@)
300 0 ▶{ my ($self, $action, $type, %args) = @_;
Subroutine compileType: not called CC=3 SCAR=24.8
Pod documented
301  
302 # translator can only create elements, not types.
303 0 ▶ my $elem = delete $args{element} || $type;
Condition0%0/3
delete $args{'element'} || $type
l!l&&r!l&&!r
000
MC/DC0%0/2
delete $args{'element'} || $type
delete $args{'element'} $type
Truth table0%0/3
A || B
A= delete $args{'element'}B= $type
ABresult
1X1
011
000
304 0 my ($ens, $elocal) = unpack_type $elem;
305 0 my ($ns, $local) = unpack_type $type;
306  
307 0 my $SchemaNS = SCHEMA2001;
308  
309 0 ▶ my $defs = $ns ? <<_DIRTY_TRICK1 : <<_DIRTY_TRICK2;
Branch: 1 branch
BranchTrueFalseTotal
$ns ? : 0 0 0
310 <schema xmlns="$SchemaNS"
311 targetNamespace="$ens"
312 xmlns:tns="$ns">
313 <element name="$elocal" type="tns:$local" />
314 </schema>
315 _DIRTY_TRICK1
316 <schema xmlns="$SchemaNS"
317 targetNamespace="$ens"
318 elementFormDefault="unqualified"
319 >
320 <element name="$elocal" type="$local" />
321 </schema>
322 _DIRTY_TRICK2
323  
324 0 $self->importDefinitions($defs);
325 0 $self->compile($action, $elem, %args);
326 }
327  
328  
329 sub template($@)
330 19 ▶{ my ($self, $action, $type, %args) = @_;
Subroutine template: called CC=23 SCAR=32.3
Pod documented
331  
332 19 ▶ my ($to_perl, $to_xml)
Branch: 3 branches
BranchTrueFalseTotal
$action eq 'PERL' ? : 16 3 19
$action eq 'XML' ? : 2 1 3
$action eq 'TREE' ? : 1 0 1
333 = $action eq 'PERL' ? (1, 0)
334 : $action eq 'XML' ? (0, 1)
335 : $action eq 'TREE' ? (0, 0)
336 : error __x"template output is either in XML or PERL layout, not '{action}'"
337 , action => $action;
338  
339 my $show
340 = exists $args{show_comments} ? $args{show_comments}
341 : exists $args{show} ? $args{show} # pre-0.79 option name
342 19 ▶ : 'ALL';
Branch: 2 branches
BranchTrueFalseTotal
exists $args{'show_comments'} ? : 0 19 19
exists $args{'show'} ? : 6 13 19
343  
344 19 ▶ $show = 'struct,type,occur,facets' if $show eq 'ALL';
Branch: 1 branch
BranchTrueFalseTotal
if $show eq "ALL" 17 2 19
345 19 ▶ $show = '' if $show eq 'NONE';
Branch: 1 branch
BranchTrueFalseTotal
if $show eq "NONE" 2 17 19
346 19 my %show = map {("show_$_" => 1)} split m/\,/, $show;
347 19 my $nss = $self->namespaces;
348  
349 19 ▶ my $indent = $args{indent} || " ";
Condition100%2/2
$args{'indent'} || ' '
l!l
118
MC/DC100%1/1
$args{'indent'} || ' '
$args{'indent'}
Truth table100%2/2
A
A= $args{'indent'}
Aresult
11
00
350 19 $args{check_occurs} = 1;
351 19 ▶ $args{mixed_elements} ||= 'ATTRIBUTES';
Condition50%1/2
$args{"mixed_elements"} ||= "ATTRIBUTES"
l!l
019
MC/DC0%0/1
$args{"mixed_elements"} ||= "ATTRIBUTES"
$args{"mixed_elements"}
Truth table50%1/2
A
A= $args{"mixed_elements"}
Aresult
11
00
352 19 ▶ $args{default_values} ||= 'EXTEND';
Condition50%1/2
$args{"default_values"} ||= "EXTEND"
l!l
019
MC/DC0%0/1
$args{"default_values"} ||= "EXTEND"
$args{"default_values"}
Truth table50%1/2
A
A= $args{"default_values"}
Aresult
11
00
353 19 ▶ $args{abstract_types} ||= 'ERROR';
Condition100%2/2
$args{"abstract_types"} ||= "ERROR"
l!l
217
MC/DC100%1/1
$args{"abstract_types"} ||= "ERROR"
$args{"abstract_types"}
Truth table100%2/2
A
A= $args{"abstract_types"}
Aresult
11
00
354  
355 exists $args{include_namespaces}
356 19 ▶ or $args{include_namespaces} = 1;
Branch: 1 branch
BranchTrueFalseTotal
exists $args{"include_namespaces"} or $args{"include_namespaces"} = 1 18 1 19
357  
358 # it could be used to add extra comment lines
359 error __x"typemaps not implemented for XML template examples"
360 0 ▶ if $to_xml && defined $args{typemap} && keys %{$args{typemap}};
Branch: 1 branch
BranchTrueFalseTotal
if $to_xml and defined $args{"typemap"} and keys %{$args{"typemap"};} 0 19 19
Condition50%3/6
$to_xml and defined $args{"typemap"} and keys %{$args{"typemap"};}
!ll&&!rl&&r
1900
$to_xml and defined $args{"typemap"}
!ll&&!rl&&r
1720
MC/DC0%0/3
$to_xml and defined $args{"typemap"} and keys %{$args{"typemap"};}
$to_xml defined $args{"typemap"} keys %{$args{"typemap"};}
Truth table50%2/4
A and B and C
A= $to_xmlB= defined $args{"typemap"}C= keys %{$args{"typemap"};}
ABCresult
0XX0
10X0
1100
1111
361  
362 19 my @rewrite = $self->_key_rewrite(delete $args{key_rewrite});
363 19 my @blocked = $self->_block_nss(delete $args{block_namespace});
364  
365 my $table = $args{prefixes} = $self->_namespaceTable
366 (($args{prefixes} || $args{output_namespaces})
367 , $args{namespace_reset}
368 , !$args{use_default_namespace}
369 19 ▶ );
Condition66%2/3
$args{'prefixes'} || $args{'output_namespaces'}
l!l&&r!l&&!r
2017
MC/DC50%1/2
$args{'prefixes'} || $args{'output_namespaces'}
$args{'prefixes'} $args{'output_namespaces'}
Truth table66%2/3
A || B
A= $args{'prefixes'}B= $args{'output_namespaces'}
ABresult
1X1
011
000
370  
371 19 ▶ my $used = $to_xml && $show{show_type};
Condition100%3/3
$to_xml && $show{'show_type'}
!ll&&!rl&&r
1711
MC/DC100%2/2
$to_xml && $show{'show_type'}
$to_xml $show{'show_type'}
Truth table100%3/3
A && B
A= $to_xmlB= $show{'show_type'}
ABresult
0X0
100
111
372 19 ▶ $table->{&SCHEMA2001}
Condition50%1/2
$$table{&SCHEMA2001} ||= {"prefix", "xs", "uri", "http://www.w3.org/2001/XMLSchema", "used", $used}
l!l
019
MC/DC0%0/1
$$table{&SCHEMA2001} ||= {"prefix", "xs", "uri", "http://www.w3.org/2001/XMLSchema", "used", $used}
$$table{&SCHEMA2001}
Truth table50%1/2
A
A= $$table{&SCHEMA2001}
Aresult
11
00
373 ||= +{prefix => 'xs', uri => SCHEMA2001, used => $used};
374 19 ▶ $table->{&SCHEMA2001i}
Condition50%1/2
$$table{&SCHEMA2001i} ||= {"prefix", "xsi", "uri", "http://www.w3.org/2001/XMLSchema-instance", "used", $used}
l!l
190
MC/DC0%0/1
$$table{&SCHEMA2001i} ||= {"prefix", "xsi", "uri", "http://www.w3.org/2001/XMLSchema-instance", "used", $used}
$$table{&SCHEMA2001i}
Truth table50%1/2
A
A= $$table{&SCHEMA2001i}
Aresult
11
00
375 ||= +{prefix => 'xsi', uri => SCHEMA2001i, used => $used};
376  
377 19 my $transl = XML::Compile::Translate->new
378 ( 'TEMPLATE'
379 , nss => $self->namespaces
380 );
381  
382 19 my $compiled = $transl->compile
383 ( $type
384 , %args
385 , rewrite => \@rewrite
386 , block_namespace => \@blocked # not yet supported
387 , output => $action
388 );
389 19 ▶ $compiled or return;
Branch: 1 branch
BranchTrueFalseTotal
$compiled or return 19 0 19
390  
391 19 my $ast = $compiled->();
392 #use Data::Dumper; $Data::Dumper::Indent = 1; warn Dumper $ast;
393  
394 19 ▶ if($to_perl)
Branch: 1 branch
BranchTrueFalseTotal
if ($to_perl) 16 3 19
395 { return $transl->toPerl($ast, %show, indent => $indent
396 , skip_header => $args{skip_header})
397 16 }
398  
399 3 ▶ if($to_xml)
Branch: 1 branch
BranchTrueFalseTotal
if ($to_xml) 2 1 3
400 2 { my $doc = XML::LibXML::Document->new('1.1', 'UTF-8');
401 my $node = $transl->toXML($doc, $ast, %show
402 2 , indent => $indent, skip_header => $args{skip_header});
403 2 return $node->toString(1);
404 }
405  
406 # return tree
407 1 $ast;
408 }
409  
410 #------------------------------------------
411  
412  
413 1645 ▶sub namespaces() { shift->{namespaces} }
Subroutine namespaces: called CC=1 SCAR=0.0
Pod documented
414  
415  
416 # The cache will certainly avoid penalties by the average module user,
417 # which does not understand the sharing schema definitions between objects
418 # especially in SOAP implementations.
419 50 my (%schemaByFilestamp, %schemaByChecksum);
420  
421 sub importDefinitions($@)
422 54 ▶{ my ($self, $frags, %options) = @_;
Subroutine importDefinitions: called CC=10 SCAR=28.5
Pod documented
423 54 ▶ my @data = ref $frags eq 'ARRAY' ? @$frags : $frags;
Branch: 1 branch
BranchTrueFalseTotal
ref $frags eq 'ARRAY' ? : 0 54 54
424  
425 # this is a horrible hack, but by far the simpelest solution to
426 # avoid dataToXML process the same info twice.
427 54 local $self->{_use_cache} = 1;
428  
429 54 my @schemas;
430 54 foreach my $data (@data)
431 54 ▶ { defined $data or next;
Branch: 1 branch
BranchTrueFalseTotal
defined $data or next 54 0 54
432 54 my ($xml, %details) = $self->dataToXML($data);
433 0 ▶ %details = %{delete $options{details}} if $options{details};
Branch: 1 branch
BranchTrueFalseTotal
if $options{"details"} 0 54 54
434  
435 54 ▶ if(defined $xml)
Branch: 3 branches
BranchTrueFalseTotal
if (defined $xml) { } 54 0 54
elsif (my $filestamp = $details{'filestamp'}) { } 0 0 0
elsif (my $checksum = $details{'checksum'}) { } 0 0 0
436 54 { my @added = $self->addSchemas($xml, %details, %options);
437 54 ▶ if(my $checksum = $details{checksum})
Branch: 2 branches
BranchTrueFalseTotal
if (my $checksum = $details{'checksum'}) { } 53 1 54
elsif (my $filestamp = $details{'filestamp'}) { } 0 1 1
438 53 { $self->{_cache_checksum}{$checksum} = \@added;
439 }
440 elsif(my $filestamp = $details{filestamp})
441 0 { $self->{_cache_file}{$filestamp} = \@added;
442 }
443 54 push @schemas, @added;
444 }
445 elsif(my $filestamp = $details{filestamp})
446 0 { my $cached = $self->{_cache_file}{$filestamp};
447 0 $self->namespaces->add(@$cached);
448 }
449 elsif(my $checksum = $details{checksum})
450 0 { my $cached = $self->{_cache_checksum}{$checksum};
451 0 $self->namespaces->add(@$cached);
452 }
453 }
454 54 @schemas;
455 }
456  
457 sub _parseScalar($)
458 53 ▶{ my ($thing, $data) = @_;
Subroutine _parseScalar: called CC=4 SCAR=15.8
459  
460 ref $thing && $thing->{_use_cache}
461 53 ▶ or return $thing->SUPER::_parseScalar($data);
Branch: 1 branch
BranchTrueFalseTotal
ref $thing and $thing->{"_use_cache"} or return $thing->SUPER::_parseScalar($data) 53 0 53
Condition33%1/3
ref $thing and $thing->{"_use_cache"}
!ll&&!rl&&r
0053
MC/DC0%0/2
ref $thing and $thing->{"_use_cache"}
ref $thing $thing->{"_use_cache"}
Truth table33%1/3
A and B
A= ref $thingB= $thing->{"_use_cache"}
ABresult
0X0
100
111
462  
463 53 my $self = $thing;
464 53 my $checksum = md5_hex $$data;
465 53 ▶ if($self->{_cache_checksum}{$checksum})
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_cache_checksum"}{$checksum}) 0 53 53
466 0 { trace "reusing string data with checksum $checksum";
467 0 return (undef, checksum => $checksum);
468 }
469  
470 53 trace "cache parsed scalar with checksum $checksum";
471  
472 53 ( $self->SUPER::_parseScalar($data)
473 , checksum => $checksum
474 );
475 }
476  
477 sub _parseFile($)
478 0 ▶{ my ($thing, $fn) = @_;
Subroutine _parseFile: not called CC=4 SCAR=30.0
479  
480 ref $thing && $thing->{_use_cache}
481 0 ▶ or return $thing->SUPER::_parseFile($fn);
Branch: 1 branch
BranchTrueFalseTotal
ref $thing and $thing->{"_use_cache"} or return $thing->SUPER::_parseFile($fn) 0 0 0
Condition0%0/3
ref $thing and $thing->{"_use_cache"}
!ll&&!rl&&r
000
MC/DC0%0/2
ref $thing and $thing->{"_use_cache"}
ref $thing $thing->{"_use_cache"}
Truth table0%0/3
A and B
A= ref $thingB= $thing->{"_use_cache"}
ABresult
0X0
100
111
482 0 my $self = $thing;
483  
484 0 my ($mtime, $size) = (stat $fn)[9,7];
485 0 my $filestamp = File::Spec->rel2abs($fn) . '-'. $mtime . '-' . $size;
486  
487 0 ▶ if($self->{_cache_file}{$filestamp})
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_cache_file"}{$filestamp}) 0 0 0
488 0 { trace "reusing schemas from file $filestamp";
489 0 return (undef, filestamp => $filestamp);
490 }
491  
492 0 trace "cache parsed file $filestamp";
493  
494 0 ( $self->SUPER::_parseFile($fn)
495 , filestamp => $filestamp
496 );
497 }
498  
499  
500 sub types()
501 3 ▶{ my $nss = shift->namespaces;
Subroutine types: called CC=1 SCAR=0.0
Pod documented
502 4 sort map {$_->types}
503 3 map {$nss->schemas($_)}
504 $nss->list;
505 }
506  
507  
508 sub elements()
509 3 ▶{ my $nss = shift->namespaces;
Subroutine elements: called CC=1 SCAR=0.0
Pod documented
510 4 sort map {$_->elements}
511 3 map {$nss->schemas($_)}
512 $nss->list;
513 }
514  
515  
516 sub printIndex(@)
517 0 ▶{ my $self = shift;
Subroutine printIndex: not called CC=1 SCAR=6.9
Pod documented
518 0 $self->namespaces->printIndex(@_);
519 }
520  
521  
522 sub doesExtend($$)
523 20 ▶{ my $self = shift;
Subroutine doesExtend: called CC=1 SCAR=0.0
Pod documented
524 20 $self->namespaces->doesExtend(@_);
525 }
526  
527  
528 50 1;
« Compile/Translate.pm ↑ Summary Compile/Schema/NameSpaces.pm »