blib/lib/XML/Compile/Schema/BuiltInFacets.pm untested

filter: Statementstmt 0.0% 0 / 206 Branchbran 0.0% 0 / 90 Conditioncond 0.0% 0 / 56 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 43 Podpod 0.0% 0 / 29 totaltotal 0.0% 0 / 424 CC 46 SCAR 28.9
CC
46
Cov
0%
_maybe_big
90.0
builtin_facet
30.0
_s_whiteSpace
20.0
CRAP 17.9
1 # Copyrights 2006-2018 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.02.
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::BuiltInFacets;
10 use vars '$VERSION';
11 $VERSION = '1.60';
12  
13 use base 'Exporter';
14  
15 use warnings;
16 use strict;
17 no warnings 'recursion';
18  
19 our @EXPORT = qw/builtin_facet/;
20  
21 use Log::Report 'xml-compile';
22  
23 use Math::BigInt;
24 use Math::BigFloat;
25 use XML::LibXML; # for ::RegExp
26 use XML::Compile::Util qw/SCHEMA2001 pack_type duration2secs/;
27  
28 use POSIX qw/DBL_MAX_10_EXP DBL_DIG/;
29  
30 # depend on Perl's compile flags
31 use constant INT_MAX => int((sprintf"%u\n",-1)/2);
32 use constant INT_MIN => -1 - INT_MAX;
33  
34  
35 my %facets_simple =
36 ( enumeration => \&_enumeration
37 , fractionDigits => \&_s_fractionDigits
38 , length => \&_s_length
39 , maxExclusive => \&_s_maxExclusive
40 , maxInclusive => \&_s_maxInclusive
41 , maxLength => \&_s_maxLength
42 , maxScale => undef # ignore
43 , minExclusive => \&_s_minExclusive
44 , minInclusive => \&_s_minInclusive
45 , minLength => \&_s_minLength
46 , minScale => undef # ignore
47 , pattern => \&_pattern
48 , totalDigits => \&_s_totalDigits
49 , whiteSpace => \&_s_whiteSpace
50 , _totalFracDigits=> \&_s_totalFracDigits
51 );
52  
53 my %facets_list =
54 ( enumeration => \&_enumeration
55 , length => \&_list_length
56 , maxLength => \&_list_maxLength
57 , minLength => \&_list_minLength
58 , pattern => \&_pattern
59 , whiteSpace => \&_list_whiteSpace
60 );
61  
62 my %facets_date = # inclusive or exclusive times is rather useless.
63 ( enumeration => \&_enumeration
64 , explicitTimeZone=> \&_date_expl_tz
65 , maxExclusive => \&_date_max
66 , maxInclusive => \&_date_max
67 , minExclusive => \&_date_min
68 , minInclusive => \&_date_min
69 , pattern => \&_pattern
70 , whiteSpace => \&_date_whitespace
71 );
72  
73 my %facets_duration =
74 ( enumeration => \&_enumeration
75 , maxExclusive => \&_dur_max_excl
76 , maxInclusive => \&_dur_max_incl
77 , minExclusive => \&_dur_min_excl
78 , minInclusive => \&_dur_min_incl
79 , pattern => \&_pattern
80 , whiteSpace => \&_s_whiteSpace
81 );
82  
83 my $date_time_type = pack_type SCHEMA2001, 'dateTime';
84 my $date_type = pack_type SCHEMA2001, 'date';
85 my $duration_type = pack_type SCHEMA2001, 'duration';
86  
87 sub builtin_facet($$$$$$$$)
88 { my ($path, $args, $facet, $value, $is_list, $type, $nss, $action) = @_;
89  
90 my $def
91 = $is_list ? $facets_list{$facet}
92 : $nss->doesExtend($type, $date_time_type) ? $facets_date{$facet}
93 : $nss->doesExtend($type, $date_type) ? $facets_date{$facet}
94 : $nss->doesExtend($type, $duration_type) ? $facets_duration{$facet}
95 : $facets_simple{$facet};
96  
97 $def or error __x"facet {facet} not implemented at {where}"
98 , facet => $facet, where => $path;
99  
100 $def->($path, $args, $value, $type, $nss, $action);
101 }
102  
103 sub _list_whiteSpace($$$)
104 { my ($path, undef, $ws) = @_;
105 $ws eq 'collapse'
106 or error __x"list whiteSpace facet fixed to 'collapse', not '{ws}' in {path}"
107 , ws => $ws, path => $path;
108 ();
109 }
110  
111 sub _s_whiteSpace($$$)
112 { my ($path, undef, $ws) = @_;
113 $ws eq 'replace' ? \&_whitespace_replace
114 : $ws eq 'collapse' ? \&_whitespace_collapse
115 : $ws eq 'preserve' ? ()
116 : error __x"illegal whiteSpace facet '{ws}' in {path}"
117 , ws => $ws, path => $path;
118 }
119  
120 sub _date_whiteSpace($$$)
121 { my ($path, undef, $ws) = @_;
122  
123 # whitespace processing already in the dateTime parser
124 $ws eq 'collapse'
125 or error __x"illegal whiteSpace facet '{ws}' in {path}"
126 , ws => $ws, path => $path;
127 ();
128 }
129  
130 sub _whitespace_replace($)
131 { my $value = ref $_[0] ? $_[0]->textContent : $_[0];
132 $value =~ s/[\t\r\n]/ /gs;
133 $value;
134 }
135  
136 sub _whitespace_collapse($)
137 { my $value = ref $_[0] ? $_[0]->textContent : $_[0];
138 for($value)
139 { s/[\t\r\n ]+/ /gs;
140 s/^ +//;
141 s/ +$//;
142 }
143 $value;
144 }
145  
146 sub _maybe_big($$$)
147 { my ($path, $args, $value) = @_;
148 return $value if $args->{sloppy_integers};
149  
150 # modules Math::Big* loaded by Schema::Spec when not sloppy
151  
152 $value =~ s/\s//g;
153 if($value =~ m/[.eE]/)
154 { my $c = $value;
155 my $exp = $c =~ s/[eE][+-]?([0-9]+)// ? $1 : 0;
156 my $pre = $c =~ /^[-+]?([0-9]*)/ ? length($1) : 0;
157 return Math::BigFloat->new($value)
158 if $pre >= DBL_DIG || $pre+$exp >= DBL_MAX_10_EXP;
159 }
160 # compare ints as strings, because they will overflow!!
161 elsif(substr($value, 0, 1) eq '-')
162 { return Math::BigInt->new($value)
163 if length($value) > length(INT_MIN)
164 || (length($value)==length(INT_MIN) && $value gt INT_MIN);
165 }
166 else
167 { return Math::BigInt->new($value)
168 if length($value) > length(INT_MAX)
169 || (length($value)==length(INT_MAX) && $value gt INT_MAX);
170 }
171  
172 $value;
173 }
174  
175 sub _s_minInclusive($$$)
176 { my ($path, $args, $min) = @_;
177 $min = _maybe_big $path, $args, $min;
178 sub { return $_[0] if $_[0] >= $min;
179 error __x"too small inclusive {value}, min {min} at {where}"
180 , value => $_[0], min => $min, where => $path;
181 };
182 }
183  
184 sub _s_minExclusive($$$)
185 { my ($path, $args, $min) = @_;
186 $min = _maybe_big $path, $args, $min;
187 sub { return $_[0] if $_[0] > $min;
188 error __x"too small exclusive {value}, larger {min} at {where}"
189 , value => $_[0], min => $min, where => $path;
190 };
191 }
192  
193 sub _s_maxInclusive($$$)
194 { my ($path, $args, $max) = @_;
195 $max = _maybe_big $path, $args, $max;
196 sub { return $_[0] if $_[0] <= $max;
197 error __x"too large inclusive {value}, max {max} at {where}"
198 , value => $_[0], max => $max, where => $path;
199 };
200 }
201  
202 sub _s_maxExclusive($$$)
203 { my ($path, $args, $max) = @_;
204 $max = _maybe_big $path, $args, $max;
205 sub { return $_[0] if $_[0] < $max;
206 error __x"too large exclusive {value}, smaller {max} at {where}"
207 , value => $_[0], max => $max, where => $path;
208 };
209 }
210  
211 my $qname = pack_type SCHEMA2001, 'QName';
212 sub _enumeration($$$$$$)
213 { my ($path, $args, $enums, $type, $nss, $action) = @_;
214  
215 my %enum = map +($_ => 1), @$enums;
216 sub { my $v = ref $_[0] eq 'ARRAY' ? join(' ', @{$_[0]}) : $_[0];
217 return $v if exists $enum{$v};
218 error __x"invalid enumerate `{string}' at {where}"
219 , string => $v, where => $path;
220 };
221 }
222  
223 sub _s_totalDigits($$$)
224 { my ($path, undef, $total) = @_;
225  
226 # this accidentally also works correctly for NaN +INF -INF
227 sub
228 { my $v = $_[0];
229 $v =~ s/[eE].*//;
230 $v =~ s/^[+-]?0*//;
231 return $_[0] if $total >= ($v =~ tr/0-9//);
232  
233 error __x"decimal too long, got {length} digits max {max} at {where}"
234 , length => ($v =~ tr/0-9//), max => $total, where => $path;
235 };
236 }
237  
238 sub _s_fractionDigits($$$)
239 { my $frac = $_[2];
240 # can be result from Math::BigFloat, so too long to use %f But rounding
241 # is very hard to implement. If you need this accuracy, then format your
242 # value yourself!
243 sub
244 { my $v = $_[0];
245 $v =~ s/(\.[0-9]{$frac}).*/$1/;
246 $v;
247 };
248 }
249  
250 sub _s_totalFracDigits($$$)
251 { my ($path, undef, $dig) = @_;
252 my ($total, $frac) = @$dig;
253 sub
254 { my $w = $_[0];
255  
256 my $v = $w; # total is checking length
257 $v =~ s/[eE].*//;
258 $v =~ s/^[+-]?0*//;
259  
260 if( $v !~ /^(?:[+-]?)(?:[0-9]*)(?:\.([0-9]*))?$/ )
261 { error __x"Invalid numeric format, got {value} at {where}"
262 , value => $w, where => $path;
263 }
264  
265 if($1 && length($1) > $frac)
266 { error __x"fractional part for {value} too long, got {l} digits max {max} at {where}"
267 , value => $w, l => length($1), max => $frac, where => $path;
268 }
269  
270 return $w if $total >= ($v =~ tr/0-9//);
271 error __x"decimal too long, got {length} digits max {max} at {where}"
272 , length => ($v =~ tr/0-9//), max => $total, where => $path;
273 };
274 }
275  
276 sub _s_length($$$$$$)
277 { my ($path, $args, $len, $type, $nss, $action) = @_;
278  
279 sub { return $_[0] if defined $_[0] && length($_[0])==$len;
280 error __x"string `{string}' does not have required length {len} but {size} at {where}"
281 , string => $_[0], len => $len, size => length($_[0]), where => $path;
282 };
283 }
284  
285 sub _list_length($$$)
286 { my ($path, $args, $len) = @_;
287 sub { return $_[0] if defined $_[0] && @{$_[0]}==$len;
288 error __x"list `{list}' does not have required length {len} at {where}"
289 , list => $_[0], len => $len, where => $path;
290 };
291 }
292  
293 sub _s_minLength($$$)
294 { my ($path, $args, $len, $type, $nss, $action) = @_;
295  
296 sub { return $_[0] if defined $_[0] && length($_[0]) >=$len;
297 error __x"string `{string}' does not have minimum length {len} at {where}"
298 , string => $_[0], len => $len, where => $path;
299 };
300 }
301  
302 sub _list_minLength($$$)
303 { my ($path, $args, $len) = @_;
304 sub { return $_[0] if defined $_[0] && @{$_[0]} >=$len;
305 error __x"list `{list}' does not have minimum length {len} at {where}"
306 , list => $_[0], len => $len, where => $path;
307 };
308 }
309  
310 sub _s_maxLength($$$)
311 { my ($path, $args, $len, $type, $nss, $action) = @_;
312  
313 sub { return $_[0] if defined $_[0] && length $_[0] <= $len;
314 error __x"string `{string}' longer than maximum length {len} at {where}"
315 , string => $_[0], len => $len, where => $path;
316 };
317 }
318  
319 sub _list_maxLength($$$)
320 { my ($path, $args, $len) = @_;
321 sub { return $_[0] if defined $_[0] && @{$_[0]} <= $len;
322 error __x"list `{list}' longer than maximum length {len} at {where}"
323 , list => $_[0], len => $len, where => $path;
324 };
325 }
326  
327 sub _pattern($$$)
328 { my ($path, $args, $pats) = @_;
329 @$pats or return ();
330 my $regex = @$pats==1 ? $pats->[0] : "(".join(')|(', @$pats).")";
331 my $compiled = XML::LibXML::RegExp->new($regex);
332  
333 sub {
334 use Carp 'cluck';
335 defined $_[0] or cluck "PATTERN";
336 my $v = ref $_[0] ? $_[0]->textContent : $_[0];
337 return $_[0] if $compiled->matches($v);
338 error __x"string `{string}' does not match pattern `{pat}' at {where}"
339 , string => $v, pat => $regex, where => $path;
340 };
341 }
342  
343 sub _date_min($$$)
344 { my ($path, $args, $min) = @_;
345 sub { return $_[0] if $_[0] gt $min;
346 error __x"too small inclusive {value}, min {min} at {where}"
347 , value => $_[0], min => $min, where => $path;
348 };
349 }
350  
351 sub _date_max($$$)
352 { my ($path, $args, $max) = @_;
353 sub { return $_[0] if $_[0] lt $max;
354 error __x"too large inclusive {value}, max {max} at {where}"
355 , value => $_[0], max => $max, where => $path;
356 };
357 }
358  
359 sub _date_expl_tz($$$)
360 { my ($path, $args, $enum) = @_;
361 my $tz = qr/Z$ | [+-](?:(?:0[0-9]|1[0-3])\:[0-5][0-9] | 14\:00)$/x;
362  
363 $enum eq 'optional' ? ()
364 : $enum eq 'prohibited'
365 ? sub { $_[0] !~ $tz
366 or error __x"timezone forbidden on {date} at {where}"
367 , date => $_[0], where => $path;
368 }
369 : $enum eq 'required'
370 ? sub { $_[0] =~ $tz
371 or error __x"timezone required on {date} at {where}"
372 , date => $_[0], where => $path;
373 }
374 : error __x"illegal explicitTimeZone facet '{enum}' in {path}"
375 , enum => $enum, path => $path;
376 }
377  
378 sub _dur_min_incl($$$)
379 { my ($path, $args, $min) = @_;
380 my $secs = duration2secs $min;
381  
382 sub { return $_[0] if duration2secs $_[0] >= $secs;
383 error __x"too small minInclusive duration {value}, min {min} at {where}"
384 , value => $_[0], min => $min, where => $path;
385 };
386 }
387  
388  
389 sub _dur_min_excl($$$)
390 { my ($path, $args, $min) = @_;
391 my $secs = duration2secs $min;
392  
393 sub { return $_[0] if duration2secs $_[0] > $secs;
394 error __x"too small minExclusive duration {value}, min {min} at {where}"
395 , value => $_[0], min => $min, where => $path;
396 };
397 }
398  
399  
400 sub _dur_max_incl($$$)
401 { my ($path, $args, $max) = @_;
402 my $secs = duration2secs $max;
403  
404 sub { return $_[0] if duration2secs $_[0] <= $secs;
405 error __x"too large maxInclusive duration {value}, max {max} at {where}"
406 , value => $_[0], max => $max, where => $path;
407 };
408 }
409  
410  
411 sub _dur_max_excl($$$)
412 { my ($path, $args, $max) = @_;
413 my $secs = duration2secs $max;
414  
415 sub { return $_[0] if duration2secs $_[0] < $secs;
416 error __x"too large maxExclusive duration {value}, max {max} at {where}"
417 , value => $_[0], max => $max, where => $path;
418 };
419 }
420  
421 1;