Compile/Schema/BuiltInTypes.pm

filter: Statementstmt 78.9% 218 / 276 Branchbran 47.4% 37 / 78 Conditioncond 26.0% 6 / 23 MC/DCmcdc 13.3% 2 / 15 Subroutinesub 58.8% 53 / 90 Podpod 11.1% 1 / 9 totaltotal 64.5% 317 / 491 CC 48 SCAR 13.2
CC
48
Cov
69%
__ANON__
12.0
__ANON__
12.0
__ANON__
12.0
CRAP 3.8
« Compile/Util.pm ↑ Summary Compile/Iterator.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::BuiltInTypes;{
10 50 our $VERSION = '1.64';
11 }
12  
13 50 ▶use base 'Exporter';
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 50 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 50 ▶no warnings 'recursion';
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 50 our @EXPORT = qw/%builtin_types builtin_type_info/;
21  
22 50 our %builtin_types;
23  
24 50 ▶use Log::Report 'xml-compile';
Subroutine BEGIN: called CC=1 SCAR=0.0
25 50 ▶use POSIX qw/strftime/;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 50 ▶use Math::BigInt;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 50 ▶use Math::BigFloat;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 50 ▶use MIME::Base64;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 50 ▶use Types::Serialiser;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 50 ▶use Scalar::Util qw(dualvar);
Subroutine BEGIN: called CC=1 SCAR=0.0
31 50 ▶use POSIX qw/floor log10/;
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 50 ▶use XML::Compile::Util qw/pack_type unpack_type/;
Subroutine BEGIN: called CC=1 SCAR=0.0
34  
35 50 ▶use Config '%Config';
Subroutine BEGIN: called CC=1 SCAR=0.0
36 50 my $iv_bits = $Config{ivsize} * 8 -1;
37 50 my $iv_digits = floor($iv_bits * log10(2));
38 50 my $fits_iv = qr/^[+-]?[0-9]{1,$iv_digits}$/;
39  
40  
41 0 ▶sub builtin_type_info($) { $builtin_types{$_[0]} }
Subroutine builtin_type_info: not called CC=1 SCAR=6.9
Pod documented
42  
43  
44 # The XML reader calls
45 # check(parse(value)) or check_read(parse(value))
46  
47 # The XML writer calls
48 # check(format(value)) or check_write(format(value))
49  
50 # Parse has a second argument, only for QNAME: the node
51 # Format has a second argument for QNAME as well.
52  
53 0 ▶sub identity { $_[0] }
Subroutine identity: not called CC=1 SCAR=6.9
Pod undocumented
54  
55 # already validated, unless that is disabled.
56 559 ▶sub str2int { $_[0] + 0 }
Subroutine str2int: called CC=1 SCAR=0.0
Pod undocumented
57  
58 # sprintf returns '0' if non-int, with warning. We need a validation error
59 479 ▶sub int2str { $_[0] =~ m/^\s*([0-9]+)\s*$/ ? $1 : $_[0] }
Subroutine int2str: called CC=2 SCAR=6.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
$_[0] =~ /^\s*([0-9]+)\s*$/ ? : 472 7 479
60  
61 0 ▶sub str { "$_[0]" }
Subroutine str: not called CC=1 SCAR=6.9
Pod undocumented
62 0 ▶sub _replace { $_[0] =~ s/[\t\r\n]/ /g; $_[0]}
Subroutine _replace: not called CC=1 SCAR=6.9
63 17 ▶sub _collapse { local $_ = $_[0]; s/[\t\r\n]+/ /g; s/^ +//; s/ +$//; $_}
Subroutine _collapse: called CC=1 SCAR=0.0
64  
65  
66 # format not useful, because xsi:type not supported
67 $builtin_types{anySimpleType} =
68 { example => 'anySimple'
69 0 ▶ , parse => sub {shift}
Subroutine __ANON__: not called CC=1 SCAR=6.9
70 50 , extends => 'anyType'
71 };
72  
73 $builtin_types{anyType} =
74 { example => 'anything'
75 0 ▶ , parse => sub {shift}
Subroutine __ANON__: not called CC=1 SCAR=6.9
76 50 , extends => undef # the root type
77 };
78  
79 $builtin_types{anyAtomicType} =
80 { example => 'anyAtomic'
81 0 ▶ , parse => sub {shift}
Subroutine __ANON__: not called CC=1 SCAR=6.9
82 50 , extends => 'anySimpleType'
83 };
84  
85  
86 50 $builtin_types{error} = {example => '[some error structure]'};
87  
88 #----------------
89  
90  
91 $builtin_types{boolean} =
92 4 ▶ { parse => sub { $_[0] =~ m/^\s*false|0\s*/i ? 0 : 1 }
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_[0] =~ /^\s*false|0\s*/i ? : 2 2 4
93 4 ▶ , format => sub { $_[0] eq 'false' || $_[0] eq 'true' ? $_[0]
Subroutine __ANON__: called CC=4 SCAR=13.9
Branch: 2 branches
BranchTrueFalseTotal
$_[0] eq 'false' || $_[0] eq 'true' ? : 2 2 4
$_[0] ? : 1 1 2
Condition100%3/3
$_[0] eq "false" or $_[0] eq "true"
l!l&&r!l&&!r
112
MC/DC100%2/2
$_[0] eq "false" or $_[0] eq "true"
$_[0] eq "false" $_[0] eq "true"
Truth table100%3/3
A or B
A= $_[0] eq "false"B= $_[0] eq "true"
ABresult
1X1
011
000
94 : $_[0] ? 1 : 0 }
95 8 ▶ , check => sub { $_[0] =~ m/^\s*(?:false|true|0|1)\s*$/i }
Subroutine __ANON__: called CC=1 SCAR=0.0
96 50 , example => 'true'
97 , extends => 'anyAtomicType'
98 };
99  
100 $builtin_types{boolean_with_Types_Serialiser} =
101 50 { %{$builtin_types{boolean}}
102 0 ▶ , parse => sub {
Subroutine __ANON__: not called CC=2 SCAR=9.2
103 50 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
104 0 ▶ $_[0] =~ m/^\s*(false|0)\s*/i
Branch: 1 branch
BranchTrueFalseTotal
$_[0] =~ /^\s*(false|0)\s*/i ? : 0 0 0
105 ? $Types::Serialiser::false
106 : $Types::Serialiser::true;
107 }
108 50 };
109  
110  
111 $builtin_types{pattern} =
112 50 { example => '*.exe'
113 };
114  
115  
116 sub bigint
117 15 ▶{ my $v = shift;
Subroutine bigint: called CC=3 SCAR=11.0
Pod undocumented
118 15 $v =~ s/\s+//g;
119  
120 # The automatic rewrite into JSON wants real ints, not strings. Therefore,
121 # we need to numify. On the other hand, pattern matching/enumeration
122 # requires the original string. Regression tests prove this trick works.
123 15 ▶ return dualvar($v+0, $v) if $v =~ $fits_iv;
Branch: 1 branch
BranchTrueFalseTotal
if $v =~ /$fits_iv/ 7 8 15
124  
125 8 my $big = Math::BigInt->new($v);
126 8 ▶ error __x"Value `{val}' is not a (big) integer", val => $big
Branch: 1 branch
BranchTrueFalseTotal
if $big->is_nan 0 8 8
127 if $big->is_nan;
128 8 $big;
129 }
130  
131 $builtin_types{integer} =
132 { parse => \&bigint
133 23 ▶ , check => sub { $_[0] =~ m/^\s*[-+]?\s*[0-9][\s0-9]*$/ }
Subroutine __ANON__: called CC=1 SCAR=0.0
134 50 , example => 42
135 , extends => 'decimal'
136 };
137  
138  
139 $builtin_types{negativeInteger} =
140 { parse => \&bigint
141 0 ▶ , check => sub { $_[0] =~ m/^\s*\-\s*[0-9][\s0-9]*$/ }
Subroutine __ANON__: not called CC=1 SCAR=6.9
142 50 , example => '-1'
143 , extends => 'nonPositiveInteger'
144 };
145  
146  
147 $builtin_types{nonNegativeInteger} =
148 { parse => \&bigint
149 0 ▶ , check => sub { $_[0] =~ m/^\s*(?:\+\s*)?[0-9][\s0-9]*$/ }
Subroutine __ANON__: not called CC=1 SCAR=6.9
150 50 , example => '17'
151 , extends => 'integer'
152 };
153  
154  
155 $builtin_types{positiveInteger} =
156 { parse => \&bigint
157 2 ▶ , check => sub { $_[0] =~ m/^\s*(?:\+\s*)?[0-9][\s0-9]*$/ && $_[0] =~ m/[1-9]/ }
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
$_[0] =~ /^\s*(?:\+\s*)?[0-9][\s0-9]*$/ and $_[0] =~ /[1-9]/ 2 0 2
158 50 , example => '+3'
159 , extends => 'nonNegativeInteger'
160 };
161  
162  
163 $builtin_types{nonPositiveInteger} =
164 { parse => \&bigint
165 0 ▶ , check => sub { $_[0] =~ m/^\s*(?:\-\s*)?[0-9][\s0-9]*$/
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$_[0] =~ /^\s*(?:\-\s*)?[0-9][\s0-9]*$/ or $_[0] =~ /^\s*(?:\+\s*)0[0\s]*$/ 0 0 0
166 || $_[0] =~ m/^\s*(?:\+\s*)0[0\s]*$/ }
167 50 , example => '-42'
168 , extends => 'integer'
169 };
170  
171  
172 $builtin_types{long} =
173 { parse => \&bigint
174 , check =>
175 0 ▶ sub { $_[0] =~ m/^\s*[-+]?\s*[0-9][\s0-9]*$/ && ($_[0] =~ tr/0-9//) < 20 }
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$_[0] =~ /^\s*[-+]?\s*[0-9][\s0-9]*$/ and $_[0] =~ tr/0-9// < 20 0 0 0
176 50 , example => '-100'
177 , extends => 'integer'
178 };
179  
180  
181 $builtin_types{unsignedLong} =
182 { parse => \&bigint
183 0 ▶ , check => sub {$_[0] =~ m/^\s*\+?\s*[0-9][\s0-9]*$/ && ($_[0] =~ tr/0-9//) < 21}
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$_[0] =~ /^\s*\+?\s*[0-9][\s0-9]*$/ and $_[0] =~ tr/0-9// < 21 0 0 0
184 50 , example => '100'
185 , extends => 'nonNegativeInteger'
186 };
187  
188  
189 $builtin_types{unsignedInt} =
190 { parse => \&bigint
191 0 ▶ , check => sub {$_[0] =~ m/^\s*\+?\s*[0-9][\s0-9]*$/ && ($_[0] =~ tr/0-9//) <=10}
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$_[0] =~ /^\s*\+?\s*[0-9][\s0-9]*$/ and $_[0] =~ tr/0-9// <= 10 0 0 0
192 50 , example => '42'
193 , extends => 'unsignedLong'
194 };
195  
196 # Used when 'sloppy_integers' was set: the size of the values
197 # is illegally limited to the size of Perl's 32-bit signed integers.
198  
199 $builtin_types{non_pos_int} =
200 { parse => \&str2int
201 , format => \&int2str
202 0 ▶ , check => sub {$_[0] =~ m/^\s*[+-]?\s*[0-9][0-9\s]*$/ && $_[0] <= 0}
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$_[0] =~ /^\s*[+-]?\s*[0-9][0-9\s]*$/ and $_[0] <= 0 0 0 0
203 50 , example => '-12'
204 };
205  
206 $builtin_types{positive_int} =
207 { parse => \&str2int
208 , format => \&int2str
209 0 ▶ , check => sub {$_[0] =~ m/^\s*(?:\+\s*)?[0-9][0-9\s]*$/ }
Subroutine __ANON__: not called CC=1 SCAR=6.9
210 50 , example => '+42'
211 };
212  
213 $builtin_types{negative_int} =
214 { parse => \&str2int
215 , format => \&int2str
216 0 ▶ , check => sub {$_[0] =~ m/^\s*\-\s*[0-9][0-9\s]*$/ }
Subroutine __ANON__: not called CC=1 SCAR=6.9
217 50 , example => '-12'
218 };
219  
220 $builtin_types{unsigned_int} =
221 { parse => \&str2int
222 , format => \&int2str
223 6 ▶ , check => sub {$_[0] =~ m/^\s*(?:\+\s*)?[0-9][0-9\s]*$/ && $_[0] >= 0}
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
$_[0] =~ /^\s*(?:\+\s*)?[0-9][0-9\s]*$/ and $_[0] >= 0 6 0 6
224 50 , example => '42'
225 };
226  
227  
228 $builtin_types{int} =
229 { parse => \&str2int
230 , format => \&int2str
231 1040 ▶ , check => sub {$_[0] =~ m/^\s*[+-]?[0-9]+\s*$/}
Subroutine __ANON__: called CC=1 SCAR=0.0
232 50 , example => '42'
233 , extends => 'long'
234 };
235  
236  
237 $builtin_types{short} =
238 { parse => \&str2int
239 , format => \&int2str
240 , check =>
241 0 ▶ sub { $_[0] =~ m/^\s*[+-]?[0-9]+\s*$/ && $_[0] >= -32768 && $_[0] <= 32767 }
Subroutine __ANON__: not called CC=3 SCAR=24.8
Branch: 1 branch
BranchTrueFalseTotal
$_[0] =~ /^\s*[+-]?[0-9]+\s*$/ and $_[0] >= -32768 and $_[0] <= 32767 0 0 0
Condition0%0/3
$_[0] =~ /^\s*[+-]?[0-9]+\s*$/ and $_[0] >= -32768
!ll&&!rl&&r
000
MC/DC0%0/2
$_[0] =~ /^\s*[+-]?[0-9]+\s*$/ and $_[0] >= -32768
$_[0] =~ /^\s*[+-]?[0-9]+\s*$/ $_[0] >= -32768
Truth table0%0/3
A and B
A= $_[0] =~ /^\s*[+-]?[0-9]+\s*$/B= $_[0] >= -32768
ABresult
0X0
100
111
242 50 , example => '-7'
243 , extends => 'int'
244 };
245  
246  
247 $builtin_types{unsignedShort} =
248 { parse => \&str2int
249 , format => \&int2str
250 , check =>
251 0 ▶ sub { $_[0] =~ m/^\s*[+-]?[0-9]+\s*$/ && $_[0] >= 0 && $_[0] <= 65535 }
Subroutine __ANON__: not called CC=3 SCAR=24.8
Branch: 1 branch
BranchTrueFalseTotal
$_[0] =~ /^\s*[+-]?[0-9]+\s*$/ and $_[0] >= 0 and $_[0] <= 65535 0 0 0
Condition0%0/3
$_[0] =~ /^\s*[+-]?[0-9]+\s*$/ and $_[0] >= 0
!ll&&!rl&&r
000
MC/DC0%0/2
$_[0] =~ /^\s*[+-]?[0-9]+\s*$/ and $_[0] >= 0
$_[0] =~ /^\s*[+-]?[0-9]+\s*$/ $_[0] >= 0
Truth table0%0/3
A and B
A= $_[0] =~ /^\s*[+-]?[0-9]+\s*$/B= $_[0] >= 0
ABresult
0X0
100
111
252 50 , example => '7'
253 , extends => 'unsignedInt'
254 };
255  
256  
257 $builtin_types{byte} =
258 { parse => \&str2int
259 , format => \&int2str
260 0 ▶ , check => sub {$_[0] =~ m/^\s*[+-]?[0-9]+\s*$/ && $_[0] >= -128 && $_[0] <=127}
Subroutine __ANON__: not called CC=3 SCAR=24.8
Branch: 1 branch
BranchTrueFalseTotal
$_[0] =~ /^\s*[+-]?[0-9]+\s*$/ and $_[0] >= -128 and $_[0] <= 127 0 0 0
Condition0%0/3
$_[0] =~ /^\s*[+-]?[0-9]+\s*$/ and $_[0] >= -128
!ll&&!rl&&r
000
MC/DC0%0/2
$_[0] =~ /^\s*[+-]?[0-9]+\s*$/ and $_[0] >= -128
$_[0] =~ /^\s*[+-]?[0-9]+\s*$/ $_[0] >= -128
Truth table0%0/3
A and B
A= $_[0] =~ /^\s*[+-]?[0-9]+\s*$/B= $_[0] >= -128
ABresult
0X0
100
111
261 50 , example => '-2'
262 , extends => 'short'
263 };
264  
265  
266 $builtin_types{unsignedByte} =
267 { parse => \&str2int
268 , format => \&int2str
269 0 ▶ , check => sub {$_[0] =~ m/^\s*[+-]?[0-9]+\s*$/ && $_[0] >= 0 && $_[0] <= 255}
Subroutine __ANON__: not called CC=3 SCAR=24.8
Branch: 1 branch
BranchTrueFalseTotal
$_[0] =~ /^\s*[+-]?[0-9]+\s*$/ and $_[0] >= 0 and $_[0] <= 255 0 0 0
Condition0%0/3
$_[0] =~ /^\s*[+-]?[0-9]+\s*$/ and $_[0] >= 0
!ll&&!rl&&r
000
MC/DC0%0/2
$_[0] =~ /^\s*[+-]?[0-9]+\s*$/ and $_[0] >= 0
$_[0] =~ /^\s*[+-]?[0-9]+\s*$/ $_[0] >= 0
Truth table0%0/3
A and B
A= $_[0] =~ /^\s*[+-]?[0-9]+\s*$/B= $_[0] >= 0
ABresult
0X0
100
111
270 50 , example => '2'
271 , extends => 'unsignedShort'
272 };
273  
274  
275 $builtin_types{decimal} =
276 6 ▶ { parse => sub {$_[0] =~ s/\s+//g; Math::BigFloat->new($_[0]) },
Subroutine __ANON__: called CC=1 SCAR=0.0
277 8 ▶ , check => sub {$_[0] =~ m/^(\+|\-)?([0-9]+(\.[0-9]*)?|\.[0-9]+)$/}
Subroutine __ANON__: called CC=1 SCAR=0.0
278 50 , example => '3.1415'
279 , extends => 'anyAtomicType'
280 };
281  
282  
283 sub str2num
284 13 ▶{ my $s = shift;
Subroutine str2num: called CC=4 SCAR=13.9
Pod undocumented
285 13 $s =~ s/\s//g;
286  
287 13 ▶ $s =~ m/[^0-9]/ ? Math::BigFloat->new($s eq 'NaN' ? $s : lc $s) # INF->inf
Branch: 3 branches
BranchTrueFalseTotal
$s =~ /[^0-9]/ ? : 3 10 13
$s eq 'NaN' ? : 1 2 3
length $s < 9 ? : 10 0 10
288 : length $s < 9 ? dualvar($s+0, $s)
289 : Math::BigInt->new($s);
290 }
291  
292 sub num2str
293 15 ▶{ my $f = shift;
Subroutine num2str: called CC=5 SCAR=17.0
Pod undocumented
294 !ref $f ? $f
295 : !(UNIVERSAL::isa($f,'Math::BigInt') || UNIVERSAL::isa($f,'Math::BigFloat'))
296 0 ▶ ? eval {use warnings FATAL => 'all'; $f + 0.0}
Subroutine BEGIN: called CC=1 SCAR=0.2
Branch: 3 branches
BranchTrueFalseTotal
!ref($f) ? : 12 3 15
!(UNIVERSAL::isa($f, 'Math::BigInt') || UNIVERSAL::isa($f, 'Math::BigFloat')) ? : 0 3 3
$f->is_nan ? : 1 2 3
Condition33%1/3
UNIVERSAL::isa($f, 'Math::BigInt') || UNIVERSAL::isa($f, 'Math::BigFloat')
l!l&&r!l&&!r
300
MC/DC0%0/2
UNIVERSAL::isa($f, 'Math::BigInt') || UNIVERSAL::isa($f, 'Math::BigFloat')
UNIVERSAL::isa($f, 'Math::BigInt') UNIVERSAL::isa($f, 'Math::BigFloat')
Truth table33%1/3
A || B
A= UNIVERSAL::isa($f, 'Math::BigInt')B= UNIVERSAL::isa($f, 'Math::BigFloat')
ABresult
1X1
011
000
297 : $f->is_nan ? 'NaN'
298 : uc $f->bstr; # [+-]inf -> [+-]INF, e->E doesn't matter
299 }
300  
301 sub numcheck($)
302 29 ▶{ $_[0] =~
Subroutine numcheck: called CC=1 SCAR=0.0
Pod undocumented
303 m# [+-]? (?: [0-9]+(?:\.[0-9]*)?|\.[0-9]+) (?:[Ee][+-]?[0-9]+)?
304 | [+-]? INF
305 | NaN #x
306 }
307  
308 $builtin_types{precisionDecimal} =
309 $builtin_types{float} =
310 $builtin_types{double} =
311 50 { parse => \&str2num
312 , format => \&num2str
313 , check => \&numcheck
314 , example => '3.1415'
315 , extends => 'anyAtomicType'
316 };
317  
318 $builtin_types{sloppy_float} =
319 13 ▶ { parse => sub { $_[0] }
Subroutine __ANON__: called CC=1 SCAR=0.0
320 26 ▶ , check => sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
321 50 ▶ my $v = eval {use warnings FATAL => 'all'; $_[0] + 0.0};
Subroutine BEGIN: called CC=1 SCAR=0.0
322 26 ▶ $@ ? undef : 1;
Branch: 1 branch
BranchTrueFalseTotal
$@ ? : 0 26 26
323 }
324 50 , example => '3.1415'
325 , extends => 'anyAtomicType'
326 };
327  
328 $builtin_types{sloppy_float_force_NV} =
329 50 { %{$builtin_types{sloppy_float}}
330 0 ▶ , parse => sub { $_[0] + 0 }
Subroutine __ANON__: not called CC=1 SCAR=6.9
331 50 };
332  
333  
334 $builtin_types{base64Binary} =
335 3 ▶ { parse => sub { eval { decode_base64 $_[0] }; }
Subroutine __ANON__: called CC=1 SCAR=0.0
336 3 ▶ , format => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
337 3 my $a = $_[0];
338 3 eval { utf8::downgrade($a) };
339 3 ▶ if($@)
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 1 2 3
340 1 { error __x"use Encode::encode() for base64Binary field at {path}"
341 , path => $_[2];
342 }
343 2 encode_base64 $a, '';
344 }
345 5 ▶ , check => sub { !$@ }
Subroutine __ANON__: called CC=1 SCAR=0.0
346 50 , example => 'decoded bytes'
347 , extends => 'anyAtomicType'
348 };
349  
350  
351 # (Use of) an XS implementation would be nice
352 $builtin_types{hexBinary} =
353 3 ▶ { parse => sub { (my $v = $_[0]) =~ s/\s+//g; pack 'H*', $v }
Subroutine __ANON__: called CC=1 SCAR=0.0
354 2 ▶ , format => sub { uc unpack 'H*', $_[0]}
Subroutine __ANON__: called CC=1 SCAR=0.0
355 5 ▶ , check => sub { (my $v = $_[0]) !~ m/[^0-9a-fA-F\s]/ or return 0;
Subroutine __ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
(my $v = $_[0]) =~ /[^0-9a-fA-F\s]/ and return 0 0 5 5
356 5 ($v =~ tr/0-9a-fA-F//) % 2 == 0}
357 50 , example => 'F00F'
358 , extends => 'anyAtomicType'
359 };
360  
361  
362 50 my $yearFrag = qr/ \-? (?: [1-9][0-9]{3,} | 0[0-9][0-9][0-9] ) /x;
363 50 my $monthFrag = qr/ 0[1-9] | 1[0-2] /x;
364 50 my $dayFrag = qr/ 0[1-9] | [12][0-9] | 3[01] /x;
365 50 my $hourFrag = qr/ [01][0-9] | 2[0-3] /x;
366 50 my $minuteFrag = qr/ [0-5][0-9] /x;
367 50 my $secondFrag = qr/ [0-5][0-9] (?: \.[0-9]+)? /x;
368 50 my $endOfDayFrag = qr/24\:00\:00 (?: \.[0-9]+)? /x;
369 50 my $timezoneFrag = qr/Z | [+-] (?: 0[0-9] | 1[0-4] ) \: $minuteFrag/x;
370 50 my $timeFrag = qr/ (?: $hourFrag \: $minuteFrag \: $secondFrag )
371 | $endOfDayFrag
372 /x;
373  
374 50 my $date = qr/^ $yearFrag \- $monthFrag \- $dayFrag $timezoneFrag? $/x;
375  
376 $builtin_types{date} =
377 { parse => \&_collapse
378 4 ▶ , format => sub { $_[0] =~ /^[0-9]+$/ ? strftime("%Y-%m-%d", gmtime $_[0]) : $_[0]}
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
$_[0] =~ /^[0-9]+$/ ? : 0 4 4
379 8 ▶ , check => sub { (my $val = $_[0]) =~ s/\s+//g; $val =~ $date }
Subroutine __ANON__: called CC=1 SCAR=0.0
380 50 , example => '2006-10-06'
381 , extends => 'anyAtomicType'
382 };
383  
384  
385 50 my $time = qr /^ $timeFrag $timezoneFrag? $/x;
386  
387 $builtin_types{time} =
388 { parse => \&_collapse
389 0 ▶ , format => sub { return $_[0] if $_[0] =~ /[^0-9.]/;
Subroutine __ANON__: not called CC=3 SCAR=24.8
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] =~ /[^0-9.]/ 0 0 0
390 0 ▶ my $subsec = $_[0] =~ /(\.[0-9]+)/ ? $1 : '';
Branch: 1 branch
BranchTrueFalseTotal
$_[0] =~ /(\.[0-9]+)/ ? : 0 0 0
391 0 strftime "%T$subsec", gmtime $_[0] }
392 0 ▶ , check => sub { (my $val = $_[0]) =~ s/\s+//g; $val =~ $time }
Subroutine __ANON__: not called CC=1 SCAR=6.9
393 50 , example => '11:12:13'
394 , extends => 'anyAtomicType'
395 };
396  
397  
398 50 my $dateTime
399 = qr/^ $yearFrag \- $monthFrag \- $dayFrag T $timeFrag $timezoneFrag? $/x;
400 50 my $dateTimeStamp
401 = qr/^ $yearFrag \- $monthFrag \- $dayFrag T $timeFrag $timezoneFrag $/x;
402  
403 sub _dt_format
404 5 ▶{ return $_[0] if $_[0] =~ /[^0-9.]/; # already formated
Subroutine _dt_format: called CC=3 SCAR=18.4
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] =~ /[^0-9.]/ 5 0 5
405 0 ▶ my $subsec = $_[0] =~ /(\.[0-9]+)/ ? $1 : '';
Branch: 1 branch
BranchTrueFalseTotal
$_[0] =~ /(\.[0-9]+)/ ? : 0 0 0
406 0 strftime "%Y-%m-%dT%H:%M:%S${subsec}Z", gmtime $_[0];
407 }
408  
409 $builtin_types{dateTime} =
410 { parse => \&_collapse
411 , format => \&_dt_format
412 10 ▶ , check => sub { (my $val = $_[0]) =~ s/\s+//g; $val =~ $dateTime }
Subroutine __ANON__: called CC=1 SCAR=0.0
413 50 , example => '2006-10-06T00:23:02Z'
414 , extends => 'anyAtomicType'
415 };
416  
417  
418 $builtin_types{dateTimeStamp} =
419 { parse => \&_collapse
420 , format => \&_dt_format
421 0 ▶ , check => sub { (my $val = $_[0]) =~ s/\s+//g; $val =~ $dateTimeStamp }
Subroutine __ANON__: not called CC=1 SCAR=6.9
422 50 , example => '2006-10-06T00:23:02Z'
423 , extends => 'dateTime'
424 };
425  
426  
427 50 my $gDay = qr/^ \- \- \- $dayFrag $timezoneFrag? $/x;
428 $builtin_types{gDay} =
429 { parse => \&_collapse
430 0 ▶ , check => sub { (my $val = $_[0]) =~ s/\s+//g; $val =~ $gDay }
Subroutine __ANON__: not called CC=1 SCAR=6.9
431 50 , example => '---12+09:00'
432 , extends => 'anyAtomicType'
433 };
434  
435  
436 50 my $gMonth = qr/^ \- \- $monthFrag $timezoneFrag? $/x;
437 $builtin_types{gMonth} =
438 { parse => \&_collapse
439 0 ▶ , check => sub { (my $val = $_[0]) =~ s/\s+//g; $val =~ $gMonth }
Subroutine __ANON__: not called CC=1 SCAR=6.9
440 50 , example => '--09+07:00'
441 , extends => 'anyAtomicType'
442 };
443  
444  
445 50 my $gMonthDay = qr/^ \- \- $monthFrag \- $dayFrag $timezoneFrag? /x;
446 $builtin_types{gMonthDay} =
447 { parse => \&_collapse
448 0 ▶ , check => sub { (my $val = $_[0]) =~ s/\s+//g; $val =~ $gMonthDay }
Subroutine __ANON__: not called CC=1 SCAR=6.9
449 50 , example => '--09-12+07:00'
450 , extends => 'anyAtomicType'
451 };
452  
453  
454 50 my $gYear = qr/^ $yearFrag $timezoneFrag? $/x;
455 $builtin_types{gYear} =
456 { parse => \&_collapse
457 0 ▶ , check => sub { (my $val = $_[0]) =~ s/\s+//g; $val =~ $gYear }
Subroutine __ANON__: not called CC=1 SCAR=6.9
458 50 , example => '2006+07:00'
459 , extends => 'anyAtomicType'
460 };
461  
462  
463 50 my $gYearMonth = qr/^ $yearFrag \- $monthFrag $timezoneFrag? $/x;
464 $builtin_types{gYearMonth} =
465 { parse => \&_collapse
466 0 ▶ , check => sub { (my $val = $_[0]) =~ s/\s+//g; $val =~ $gYearMonth }
Subroutine __ANON__: not called CC=1 SCAR=6.9
467 50 , example => '2006-11+07:00'
468 , extends => 'anyAtomicType'
469 };
470  
471  
472 $builtin_types{duration} =
473 { parse => \&_collapse
474 7 ▶ , check => sub { my $val = $_[0]; $val =~ s/\s+//g;
Subroutine __ANON__: called CC=1 SCAR=0.0
475 7 $val =~ m/^\-?P(?:[0-9]+Y)?(?:[0-9]+M)?(?:[0-9]+D)?
476 (?:T(?:[0-9]+H)?(?:[0-9]+M)?(?:[0-9]+(?:\.[0-9]+)?S)?)?$/x }
477  
478 50 , example => 'P9M2DT3H5M'
479 };
480  
481  
482 $builtin_types{dayTimeDuration} =
483 { parse => \&_collapse
484 0 ▶ , check => sub { my $val = $_[0]; $val =~ s/\s+//g; $val =~
Subroutine __ANON__: not called CC=1 SCAR=6.9
485 m/^\-?P(?:[0-9]+D)?(?:T(?:[0-9]+H)?(?:[0-9]+M)?(?:[0-9]+(?:\.[0-9]+)?S)?)?$/ }
486 50 , example => 'P2DT3H5M10S'
487 , extends => 'duration'
488 };
489  
490  
491 $builtin_types{yearMonthDuration} =
492 { parse => \&_collapse
493 0 ▶ , check => sub { my $val = $_[0]; $val =~ s/\s+//g; $val =~
Subroutine __ANON__: not called CC=1 SCAR=6.9
494 m/^\-?P(?:[0-9]+Y)?(?:[0-9]+M)?$/ }
495 50 , example => 'P40Y5M'
496 , extends => 'duration'
497 };
498  
499 #-------------
500  
501  
502 $builtin_types{string} =
503 50 { example => 'example'
504 , extends => 'anyAtomicType'
505 };
506  
507  
508 $builtin_types{normalizedString} =
509 50 { parse => \&_replace
510 , example => 'example'
511 , extends => 'string'
512 };
513  
514  
515 $builtin_types{language} =
516 { parse => \&_collapse
517 0 ▶ , check => sub { my $v = $_[0]; $v =~ s/\s+//g; $v =~
Subroutine __ANON__: not called CC=1 SCAR=6.9
518 m/^[a-zA-Z]{1,8}(?:\-[a-zA-Z0-9]{1,8})*$/ }
519 50 , example => 'nl-NL'
520 , extends => 'token'
521 };
522  
523  
524 # NCName matches pattern [\i-[:]][\c-[:]]*
525 sub _ncname($)
526 2170 ▶{ (my $name = $_[0]) =~ s/\s//;
Subroutine _ncname: called CC=1 SCAR=0.0
527 2170 $name =~ m/^[[:alpha:]_](?:[\w.-]*)$/;
528 }
529  
530 50 my $ids = 0;
531 $builtin_types{ID} =
532 50 { parse => \&_collapse
533 , check => \&_ncname
534 , example => 'id_'.$ids++
535 , extends => 'NCName'
536 };
537  
538 $builtin_types{IDREF} =
539 50 { parse => \&_collapse
540 , check => \&_ncname
541 , example => 'id-ref'
542 , extends => 'NCName'
543 };
544  
545  
546 $builtin_types{NCName} =
547 50 { parse => \&_collapse
548 , check => \&_ncname
549 , example => 'label'
550 , extends => 'Name'
551 };
552  
553 $builtin_types{ENTITY} =
554 50 { parse => \&_collapse
555 , check => \&_ncname
556 , example => 'entity'
557 , extends => 'NCName'
558 };
559  
560 $builtin_types{IDREFS} =
561 $builtin_types{ENTITIES} =
562 0 ▶ { parse => sub { [ split ' ', shift ] }
Subroutine __ANON__: not called CC=1 SCAR=6.9
563 0 ▶ , format => sub { my $v = shift; ref $v eq 'ARRAY' ? join(' ',@$v) : $v }
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
ref $v eq 'ARRAY' ? : 0 0 0
564 0 ▶ , check => sub { $_[0] !~ m/\:/ }
Subroutine __ANON__: not called CC=1 SCAR=6.9
565 50 , example => 'labels'
566 , is_list => 1
567 , extends => 'anySimpleType'
568 };
569  
570  
571 $builtin_types{Name} =
572 50 { parse => \&_collapse
573 , example => 'name'
574 , extends => 'token'
575 };
576  
577  
578 $builtin_types{token} =
579 50 { parse => \&_collapse
580 , example => 'token'
581 , extends => 'normalizedString'
582 };
583  
584 # check required! \c
585 $builtin_types{NMTOKEN} =
586 0 ▶ { parse => sub { $_[0] =~ s/\s+//g; $_[0] }
Subroutine __ANON__: not called CC=1 SCAR=6.9
587 50 , example => 'nmtoken'
588 , extends => 'token'
589 };
590  
591 $builtin_types{NMTOKENS} =
592 2 ▶ { parse => sub { [ split ' ', shift ] }
Subroutine __ANON__: called CC=1 SCAR=0.0
593 5 ▶ , check => sub { $_[0] =~ /\S/ }
Subroutine __ANON__: called CC=1 SCAR=0.0
594 3 ▶ , format => sub { my $v = shift; ref $v eq 'ARRAY' ? join(' ',@$v) : $v }
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
ref $v eq 'ARRAY' ? : 1 2 3
595 50 , example => 'nmtokens'
596 , is_list => 1
597 , extends => 'anySimpleType'
598 };
599  
600  
601 # relative uri's are also correct, so even empty strings... it
602 # cannot be checked without context.
603 # use Regexp::Common qw/URI/;
604 # check => sub { $_[0] =~ $RE{URI} }
605  
606 $builtin_types{anyURI} =
607 50 { parse => \&_collapse
608 , example => 'http://example.com'
609 , extends => 'anyAtomicType'
610 };
611  
612  
613 $builtin_types{QName} =
614 { parse =>
615 6 ▶ sub { my ($qname, $node) = @_;
Subroutine __ANON__: called CC=4 SCAR=14.0
616 6 $qname =~ s/\s//g;
617 6 ▶ my $prefix = $qname =~ s/^([^:]*)\:// ? $1 : '';
Branch: 1 branch
BranchTrueFalseTotal
$qname =~ s/^([^:]*)\:// ? : 6 0 6
618  
619 6 ▶ $node = $node->node if $node->isa('XML::Compile::Iterator');
Branch: 1 branch
BranchTrueFalseTotal
if $node->isa("XML::Compile::Iterator") 4 2 6
620 6 ▶ my $ns = $node->lookupNamespaceURI($prefix) || '';
Condition50%1/2
$node->lookupNamespaceURI($prefix) || ''
l!l
60
MC/DC0%0/1
$node->lookupNamespaceURI($prefix) || ''
$node->lookupNamespaceURI($prefix)
Truth table50%1/2
A
A= $node->lookupNamespaceURI($prefix)
Aresult
11
00
621 6 pack_type $ns, $qname;
622 }
623 , format =>
624 6 ▶ sub { my ($type, $trans) = @_;
Subroutine __ANON__: called CC=5 SCAR=17.8
625 6 my ($ns, $local) = unpack_type $type;
626 6 ▶ length $ns or return $local;
Branch: 1 branch
BranchTrueFalseTotal
length $ns or return $local 6 0 6
627  
628 6 my $def = $trans->{$ns};
629 # let's hope that the namespace will get used somewhere else as
630 # well, to make it into the xmlns.
631 defined $def && exists $def->{used}
632 6 ▶ or error __x"QName formatting only works if the namespace is used for an element, not found {ns} for {local}", ns => $ns, local => $local;
Branch: 1 branch
BranchTrueFalseTotal
defined $def and exists $def->{"used"} or &error(&__x("QName formatting only works if the namespace is used for an element, not found {ns} for {local}", "ns", $ns, "local", $local)) 6 0 6
Condition33%1/3
defined $def and exists $def->{"used"}
!ll&&!rl&&r
006
MC/DC0%0/2
defined $def and exists $def->{"used"}
defined $def exists $def->{"used"}
Truth table33%1/3
A and B
A= defined $defB= exists $def->{"used"}
ABresult
0X0
100
111
633  
634 6 ▶ length $def->{prefix} ? "$def->{prefix}:$local" : $local;
Branch: 1 branch
BranchTrueFalseTotal
length $def->{'prefix'} ? : 6 0 6
635 }
636 50 , example => 'myns:local'
637 , extends => 'anyAtomicType'
638 };
639  
640  
641 $builtin_types{NOTATION} =
642 {
643 50 extends => 'anyAtomicType'
644 };
645  
646 #-------------
647  
648  
649 50 $builtin_types{binary} = { example => 'binary string' };
650  
651  
652 50 $builtin_types{timeDuration} = $builtin_types{duration};
653  
654  
655 50 $builtin_types{uriReference} = $builtin_types{anyURI};
656  
657 # These constants where removed from the spec in 2001. Probably
658 # no-one is using these (anymore)
659 # century = period => 'P100Y'
660 # recurringDate = duration => 'P24H', period => 'P1Y'
661 # recurringDay = duration => 'P24H', period => 'P1M'
662 # timeInstant = duration => 'P0Y', period => 'P0Y'
663 # timePeriod = duration => 'P0Y'
664 # year = period => 'P1Y'
665 # recurringDuration = ??
666  
667 # only in 2000/10 schemas
668 $builtin_types{CDATA} =
669 50 { parse => \&_replace
670 , example => 'CDATA'
671 };
672  
673 50 1;
« Compile/Util.pm ↑ Summary Compile/Iterator.pm »