/usr/share/perl5/RDF/Trine/Node/Literal.pm is in librdf-trine-perl 1.007-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 | # RDF::Trine::Node::Literal
# -----------------------------------------------------------------------------
=head1 NAME
RDF::Trine::Node::Literal - RDF Node class for literals
=head1 VERSION
This document describes RDF::Trine::Node::Literal version 1.007
=cut
package RDF::Trine::Node::Literal;
use strict;
use warnings;
no warnings 'redefine';
use base qw(RDF::Trine::Node);
use RDF::Trine::Error;
use Data::Dumper;
use Scalar::Util qw(blessed looks_like_number);
use Carp qw(carp croak confess);
######################################################################
our ($VERSION, $USE_XMLLITERALS, $USE_FORMULAE);
BEGIN {
$VERSION = '1.007';
eval "use RDF::Trine::Node::Literal::XML;"; ## no critic (ProhibitStringyEval)
$USE_XMLLITERALS = (RDF::Trine::Node::Literal::XML->can('new')) ? 1 : 0;
eval "use RDF::Trine::Node::Formula;"; ## no critic (ProhibitStringyEval)
$USE_FORMULAE = (RDF::Trine::Node::Formula->can('new')) ? 1 : 0;
}
######################################################################
use overload '""' => sub { $_[0]->sse },
;
=head1 METHODS
Beyond the methods documented below, this class inherits methods from the
L<RDF::Trine::Node> class.
=over 4
=cut
=item C<new ( $string, $lang, $datatype, $canonical_flag )>
Returns a new Literal structure.
=cut
sub new {
my $class = shift;
my $literal = shift;
my $lang = shift;
my $dt = shift;
my $canon = shift;
unless (defined($literal)) {
throw RDF::Trine::Error::MethodInvocationError -text => "Literal constructor called with an undefined value";
}
if (blessed($dt) and $dt->isa('RDF::Trine::Node::Resource')) {
$dt = $dt->uri_value;
}
if ($dt and $canon) {
$literal = $class->canonicalize_literal_value( $literal, $dt );
}
if ($USE_XMLLITERALS and defined($dt) and $dt eq 'http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral') {
return RDF::Trine::Node::Literal::XML->new( $literal, $lang, $dt );
} elsif ($USE_FORMULAE and defined($dt) and $dt eq RDF::Trine::Node::Formula->literal_datatype) {
return RDF::Trine::Node::Formula->new( $literal );
} else {
return $class->_new( $literal, $lang, $dt );
}
}
sub _new {
my $class = shift;
my $literal = shift;
my $lang = shift;
my $dt = shift;
my $self;
if ($lang and $dt) {
throw RDF::Trine::Error::MethodInvocationError ( -text => "Literal values cannot have both language and datatype" );
}
if ($lang) {
$self = [ $literal, lc($lang), undef ];
} elsif ($dt) {
if (blessed($dt)) {
$dt = $dt->uri_value;
}
$self = [ $literal, undef, $dt ];
} else {
$self = [ $literal ];
}
return bless($self, $class);
}
=item C<< literal_value >>
Returns the string value of the literal.
=cut
sub literal_value {
my $self = shift;
if (@_) {
$self->[0] = shift;
}
return $self->[0];
}
=item C<< literal_value_language >>
Returns the language tag of the ltieral.
=cut
sub literal_value_language {
my $self = shift;
return $self->[1];
}
=item C<< literal_datatype >>
Returns the datatype of the literal.
=cut
sub literal_datatype {
my $self = shift;
return $self->[2];
}
=item C<< value >>
Returns the literal value.
=cut
sub value {
my $self = shift;
return $self->literal_value;
}
=item C<< sse >>
Returns the SSE string for this literal.
=cut
sub sse {
my $self = shift;
my $literal = $self->literal_value;
my $escaped = $self->_unicode_escape( $literal );
$literal = $escaped;
if (defined(my $lang = $self->literal_value_language)) {
return qq("${literal}"\@${lang});
} elsif (defined(my $dt = $self->literal_datatype)) {
return qq("${literal}"^^<${dt}>);
} else {
return qq("${literal}");
}
}
=item C<< as_string >>
Returns a string representation of the node.
=cut
sub as_string {
my $self = shift;
my $string = '"' . $self->literal_value . '"';
if (defined(my $dt = $self->literal_datatype)) {
$string .= '^^<' . $dt . '>';
} elsif (defined(my $lang = $self->literal_value_language)) {
$string .= '@' . $lang;
}
return $string;
}
=item C<< as_ntriples >>
Returns the node in a string form suitable for NTriples serialization.
=cut
sub as_ntriples {
my $self = shift;
my $literal = $self->literal_value;
my $escaped = $self->_unicode_escape( $literal );
$literal = $escaped;
if (defined(my $lang = $self->literal_value_language)) {
return qq("${literal}"\@${lang});
} elsif (defined(my $dt = $self->literal_datatype)) {
return qq("${literal}"^^<${dt}>);
} else {
return qq("${literal}");
}
}
=item C<< type >>
Returns the type string of this node.
=cut
sub type {
return 'LITERAL';
}
=item C<< has_language >>
Returns true if this literal is language-tagged, false otherwise.
=cut
sub has_language {
my $self = shift;
return defined($self->literal_value_language) ? 1 : 0;
}
=item C<< has_datatype >>
Returns true if this literal is datatyped, false otherwise.
=cut
sub has_datatype {
my $self = shift;
return defined($self->literal_datatype) ? 1 : 0;
}
=item C<< equal ( $node ) >>
Returns true if the two nodes are equal, false otherwise.
=cut
sub equal {
my $self = shift;
my $node = shift;
return 0 unless (blessed($node) and $node->isa('RDF::Trine::Node::Literal'));
return 0 unless ($self->literal_value eq $node->literal_value);
if ($self->literal_datatype or $node->literal_datatype) {
no warnings 'uninitialized';
return 0 unless ($self->literal_datatype eq $node->literal_datatype);
}
if ($self->literal_value_language or $node->literal_value_language) {
no warnings 'uninitialized';
return 0 unless ($self->literal_value_language eq $node->literal_value_language);
}
return 1;
}
# called to compare two nodes of the same type
sub _compare {
my $a = shift;
my $b = shift;
if ($a->literal_value ne $b->literal_value) {
return ($a->literal_value cmp $b->literal_value);
}
# the nodes have the same lexical value
if ($a->has_language and $b->has_language) {
return ($a->literal_value_language cmp $b->literal_value_language);
}
if ($a->has_datatype and $b->has_datatype) {
return ($a->literal_datatype cmp $b->literal_datatype);
} elsif ($a->has_datatype) {
return 1;
} elsif ($b->has_datatype) {
return -1;
}
return 0;
}
=item C<< canonicalize >>
Returns a new literal node object whose value is in canonical form (where applicable).
=cut
sub canonicalize {
my $self = shift;
my $class = ref($self);
my $dt = $self->literal_datatype;
my $lang = $self->literal_value_language;
my $value = $self->value;
if (defined $dt) {
$value = RDF::Trine::Node::Literal->canonicalize_literal_value( $value, $dt, 1 );
}
return $class->new($value, $lang, $dt);
}
=item C<< canonicalize_literal_value ( $string, $datatype, $warn ) >>
If C<< $datatype >> is a recognized datatype, returns the canonical lexical
representation of the value C<< $string >>. Otherwise returns C<< $string >>.
Currently, xsd:integer, xsd:decimal, and xsd:boolean are canonicalized.
Additionally, invalid lexical forms for xsd:float, xsd:double, and xsd:dateTime
will trigger a warning.
=cut
sub canonicalize_literal_value {
my $self = shift;
my $value = shift;
my $dt = shift;
my $warn = shift;
if ($dt eq 'http://www.w3.org/2001/XMLSchema#integer') {
if ($value =~ m/^([-+])?(\d+)$/) {
my $sign = $1 || '';
my $num = $2;
$sign = '' if ($sign eq '+');
$num =~ s/^0+(\d)/$1/;
return "${sign}${num}";
} else {
warn "Bad lexical form for xsd:integer: '$value'" if ($warn);
}
} elsif ($dt eq 'http://www.w3.org/2001/XMLSchema#decimal') {
if ($value =~ m/^([-+])?((\d+)([.]\d*)?)$/) {
my $sign = $1 || '';
my $num = $2;
my $int = $3;
my $frac = $4;
$sign = '' if ($sign eq '+');
$num =~ s/^0+(.)/$1/;
$num =~ s/[.](\d)0+$/.$1/;
if ($num =~ /^[.]/) {
$num = "0$num";
}
if ($num !~ /[.]/) {
$num = "${num}.0";
}
return "${sign}${num}";
} elsif ($value =~ m/^([-+])?([.]\d+)$/) {
my $sign = $1 || '';
my $num = $2;
$sign = '' if ($sign eq '+');
$num =~ s/^0+(.)/$1/;
return "${sign}${num}";
} else {
warn "Bad lexical form for xsd:deciaml: '$value'" if ($warn);
$value = sprintf('%f', $value);
}
} elsif ($dt eq 'http://www.w3.org/2001/XMLSchema#float') {
if ($value =~ m/^(?:([-+])?(?:(\d+(?:\.\d*)?|\.\d+)([Ee][-+]?\d+)?|(INF)))|(NaN)$/) {
my $sign = $1;
my $inf = $4;
my $nan = $5;
no warnings 'uninitialized';
$sign = '' if ($sign eq '+');
return "${sign}$inf" if ($inf);
return $nan if ($nan);
$value = sprintf('%E', $value);
$value =~ m/^(?:([-+])?(?:(\d+(?:\.\d*)?|\.\d+)([Ee][-+]?\d+)?|(INF)))|(NaN)$/;
$sign = $1;
$inf = $4;
$nan = $5;
my $num = $2;
my $exp = $3;
$num =~ s/[.](\d+?)0+/.$1/;
$exp =~ tr/e/E/;
$exp =~ s/E[+]/E/;
$exp =~ s/E(-?)0+([1-9])$/E$1$2/;
$exp =~ s/E(-?)0+$/E${1}0/;
return "${sign}${num}${exp}";
} else {
warn "Bad lexical form for xsd:float: '$value'" if ($warn);
$value = sprintf('%E', $value);
$value =~ s/E[+]/E/;
$value =~ s/E0+(\d)/E$1/;
$value =~ s/(\d)0+E/$1E/;
}
} elsif ($dt eq 'http://www.w3.org/2001/XMLSchema#double') {
if ($value =~ m/^(?:([-+])?(?:(\d+(?:\.\d*)?|\.\d+)([Ee][-+]?\d+)?|(INF)))|(NaN)$/) {
my $sign = $1;
my $inf = $4;
my $nan = $5;
no warnings 'uninitialized';
$sign = '' if ($sign eq '+');
return "${sign}$inf" if ($inf);
return $nan if ($nan);
$value = sprintf('%E', $value);
$value =~ m/^(?:([-+])?(?:(\d+(?:\.\d*)?|\.\d+)([Ee][-+]?\d+)?|(INF)))|(NaN)$/;
$sign = $1;
$inf = $4;
$nan = $5;
my $num = $2;
my $exp = $3;
$num =~ s/[.](\d+?)0+/.$1/;
$exp =~ tr/e/E/;
$exp =~ s/E[+]/E/;
$exp =~ s/E(-?)0+([1-9])$/E$1$2/;
$exp =~ s/E(-?)0+$/E${1}0/;
return "${sign}${num}${exp}";
} else {
warn "Bad lexical form for xsd:double: '$value'" if ($warn);
$value = sprintf('%E', $value);
$value =~ s/E[+]/E/;
$value =~ s/E0+(\d)/E$1/;
$value =~ s/(\d)0+E/$1E/;
}
} elsif ($dt eq 'http://www.w3.org/2001/XMLSchema#boolean') {
if ($value =~ m/^(true|false|0|1)$/) {
$value = 'true' if ($value eq '1');
$value = 'false' if ($value eq '0');
return $value;
} else {
warn "Bad lexical form for xsd:boolean: '$value'" if ($warn);
}
} elsif ($dt eq 'http://www.w3.org/2001/XMLSchema#dateTime') {
if ($value =~ m/^-?([1-9]\d{3,}|0\d{3})-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])T(([01]\d|2[0-3]):[0-5]\d:[0-5]\d(\.\d+)?|(24:00:00(\.0+)?))(Z|(\+|-)((0\d|1[0-3]):[0-5]\d|14:00))?$/) {
# XXX need to canonicalize the dateTime
return $value;
} else {
warn "Bad lexical form for xsd:boolean: '$value'" if ($warn);
}
}
return $value;
}
=item C<< is_canonical_lexical_form >>
=cut
sub is_canonical_lexical_form {
my $self = shift;
my $value = $self->literal_value;
my $dt = $self->literal_datatype;
unless ($dt =~ qr<^http://www.w3.org/2001/XMLSchema#(integer|decimal|float|double|boolean|dateTime|non(Positive|Negative)Integer|(positive|negative)Integer|long|int|short|byte|unsigned(Long|Int|Short|Byte))>) {
return '0E0'; # zero but true (it's probably ok, but we don't recognize the datatype)
}
if ($dt =~ m<http://www.w3.org/2001/XMLSchema#(integer|non(Positive|Negative)Integer|(positive|negative)Integer|long|int|short|byte|unsigned(Long|Int|Short|Byte))>) {
if ($value =~ m/^([-+])?(\d+)$/) {
return 1;
} else {
return 0;
}
} elsif ($dt eq 'http://www.w3.org/2001/XMLSchema#decimal') {
if ($value =~ m/^([-+])?((\d+)[.]\d+)$/) {
return 1;
} elsif ($value =~ m/^([-+])?([.]\d+)$/) {
return 1;
} else {
return 0;
}
} elsif ($dt eq 'http://www.w3.org/2001/XMLSchema#float') {
if ($value =~ m/^[-+]?(\d+\.\d*|\.\d+)([Ee][-+]?\d+)?|[-+]?INF|NaN$/) {
return 1;
} elsif ($value =~ m/^[-+]?(\d+(\.\d*)?|\.\d+)([Ee][-+]?\d+)|[-+]?INF|NaN$/) {
return 1;
} else {
return 0;
}
} elsif ($dt eq 'http://www.w3.org/2001/XMLSchema#double') {
if ($value =~ m/^[-+]?((\d+(\.\d*))|(\.\d+))([Ee][-+]?\d+)?|[-+]?INF|NaN$/) {
return 1;
} elsif ($value =~ m/^[-+]?((\d+(\.\d*)?)|(\.\d+))([Ee][-+]?\d+)|[-+]?INF|NaN$/) {
return 1;
} else {
return 0;
}
} elsif ($dt eq 'http://www.w3.org/2001/XMLSchema#boolean') {
if ($value =~ m/^(true|false)$/) {
return 1;
} else {
return 0;
}
} elsif ($dt eq 'http://www.w3.org/2001/XMLSchema#dateTime') {
if ($value =~ m/^-?([1-9]\d{3,}|0\d{3})-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])T(([01]\d|2[0-3]):[0-5]\d:[0-5]\d(\.\d+)?|(24:00:00(\.0+)?))(Z|(\+|-)((0\d|1[0-3]):[0-5]\d|14:00))?$/) {
return 1;
} else {
return 0;
}
}
return 0;
}
=item C<< is_valid_lexical_form >>
Returns true if the node is of a recognized datatype and has a valid lexical form
for that datatype. If the lexical form is invalid, returns false. If the datatype
is unrecognized, returns zero-but-true.
=cut
sub is_valid_lexical_form {
my $self = shift;
my $value = $self->literal_value;
my $dt = $self->literal_datatype;
unless ($dt =~ qr<^http://www.w3.org/2001/XMLSchema#(integer|decimal|float|double|boolean|dateTime|non(Positive|Negative)Integer|(positive|negative)Integer|long|int|short|byte|unsigned(Long|Int|Short|Byte))>) {
return '0E0'; # zero but true (it's probably ok, but we don't recognize the datatype)
}
if ($dt =~ m<http://www.w3.org/2001/XMLSchema#(integer|non(Positive|Negative)Integer|(positive|negative)Integer|long|int|short|byte|unsigned(Long|Int|Short|Byte))>) {
if ($value =~ m/^([-+])?(\d+)$/) {
return 1;
} else {
return 0;
}
} elsif ($dt eq 'http://www.w3.org/2001/XMLSchema#decimal') {
if ($value =~ m/^([-+])?((\d+)([.]\d*)?)$/) {
return 1;
} elsif ($value =~ m/^([-+])?([.]\d+)$/) {
return 1;
} else {
return 0;
}
} elsif ($dt eq 'http://www.w3.org/2001/XMLSchema#float') {
if ($value =~ m/^[-+]?(\d+(\.\d*)?|\.\d+)([Ee][-+]?\d+)?|[-+]?INF|NaN$/) {
return 1;
} else {
return 0;
}
} elsif ($dt eq 'http://www.w3.org/2001/XMLSchema#double') {
if ($value =~ m/^[-+]?((\d+(\.\d*)?)|(\.\d+))([Ee][-+]?\d+)?|[-+]?INF|NaN$/) {
return 1;
} else {
return 0;
}
} elsif ($dt eq 'http://www.w3.org/2001/XMLSchema#boolean') {
if ($value =~ m/^(true|false|0|1)$/) {
return 1;
} else {
return 0;
}
} elsif ($dt eq 'http://www.w3.org/2001/XMLSchema#dateTime') {
if ($value =~ m/^-?([1-9]\d{3,}|0\d{3})-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])T(([01]\d|2[0-3]):[0-5]\d:[0-5]\d(\.\d+)?|(24:00:00(\.0+)?))(Z|(\+|-)((0\d|1[0-3]):[0-5]\d|14:00))?$/) {
return 1;
} else {
return 0;
}
}
return 0;
}
=item C<< is_numeric_type >>
Returns true if the literal is a known (xsd) numeric type.
=cut
sub is_numeric_type {
my $self = shift;
return 0 unless ($self->has_datatype);
my $type = $self->literal_datatype;
if ($type =~ qr<^http://www.w3.org/2001/XMLSchema#(integer|decimal|float|double|non(Positive|Negative)Integer|(positive|negative)Integer|long|int|short|byte|unsigned(Long|Int|Short|Byte))>) {
return 1;
} else {
return 0;
}
}
=item C<< numeric_value >>
Returns the numeric value of the literal (even if the literal isn't a known numeric type.
=cut
sub numeric_value {
my $self = shift;
if ($self->is_numeric_type) {
my $value = $self->literal_value;
if (looks_like_number($value)) {
my $v = 0 + eval "$value"; ## no critic (ProhibitStringyEval)
return $v;
} else {
throw RDF::Query::Error::TypeError -text => "Literal with numeric type does not appear to have numeric value.";
}
} elsif (not $self->has_datatype) {
if (looks_like_number($self->literal_value)) {
return 0+$self->literal_value;
} else {
return;
}
} elsif ($self->literal_datatype eq 'http://www.w3.org/2001/XMLSchema#boolean') {
return ($self->literal_value eq 'true') ? 1 : 0;
} else {
return;
}
}
1;
__END__
=back
=head1 BUGS
Please report any bugs or feature requests to through the GitHub web interface
at L<https://github.com/kasei/perlrdf/issues>.
=head1 AUTHOR
Gregory Todd Williams C<< <gwilliams@cpan.org> >>
=head1 COPYRIGHT
Copyright (c) 2006-2012 Gregory Todd Williams. This
program is free software; you can redistribute it and/or modify it under
the same terms as Perl itself.
=cut
|