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