/usr/share/perl5/RDF/vCard/Exporter.pm is in librdf-vcard-perl 0.010-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 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 | package RDF::vCard::Exporter;
use 5.008;
use strict;
use MIME::Base64 qw[];
use RDF::vCard;
use RDF::vCard::Entity;
use RDF::vCard::Line;
use RDF::TrineX::Functions
-shortcuts,
iri => { -as => 'rdf_resource' };
use Scalar::Util qw[blessed];
use URI;
# kinda constants
sub V { return 'http://www.w3.org/2006/vcard/ns#' . shift; }
sub VX { return 'http://buzzword.org.uk/rdf/vcardx#' . shift; }
sub RDF { return 'http://www.w3.org/1999/02/22-rdf-syntax-ns#' . shift; }
sub XSD { return 'http://www.w3.org/2001/XMLSchema#' . shift; }
sub flatten_node
{
my $node = shift;
return $node->value if $node->is_resource || $node->is_literal;
return $node->as_ntriples;
}
use namespace::clean;
our $VERSION = '0.010';
our $PRODID = sprintf("+//IDN cpan.org//NONSGML %s v %s//EN", __PACKAGE__, $VERSION);
our %dispatch = (
V('adr') => \&_prop_export_adr,
V('n') => \&_prop_export_n,
V('geo') => \&_prop_export_geo,
V('org') => \&_prop_export_org,
V('agent') => \&_prop_export_agent,
V('tel') => \&_prop_export_typed,
V('email') => \&_prop_export_typed,
V('label') => \&_prop_export_typed,
VX('impp') => \&_prop_export_typed,
V('fax') => \&_prop_export_shortcut,
V('homeAdr') => \&_prop_export_shortcut,
V('homeTel') => \&_prop_export_shortcut,
V('mobileEmail') => \&_prop_export_shortcut,
V('mobileTel') => \&_prop_export_shortcut,
V('personalEmail') => \&_prop_export_shortcut,
V('unlabeledAdr') => \&_prop_export_shortcut,
V('unlabeledEmail') => \&_prop_export_shortcut,
V('unlabeledTel') => \&_prop_export_shortcut,
V('workAdr') => \&_prop_export_shortcut,
V('workEmail') => \&_prop_export_shortcut,
V('workTel') => \&_prop_export_shortcut,
V('photo') => \&_prop_export_binary,
V('sound') => \&_prop_export_binary,
V('logo') => \&_prop_export_binary,
V('key') => \&_prop_export_binary,
);
sub new
{
my ($class, %options) = @_;
bless { %options }, $class;
}
sub is_v4
{
my ($self) = @_;
return ($self->{vcard_version} =~ /4/) ? 4 : 0;
}
sub is_v3
{
my ($self) = @_;
return $self->is_v4 ? 0 : 3;
}
sub export_cards
{
my ($self, $model, %options) = @_;
$model = rdf_parse($model)
unless blessed($model) && $model->isa('RDF::Trine::Model');
my @subjects = $model->subjects(rdf_resource(RDF('type')), rdf_resource(V('VCard')));
push @subjects, $model->subjects(rdf_resource(V('fn')), undef);
my %subjects = map { flatten_node($_) => $_ } @subjects;
my @cards;
foreach my $s (values %subjects)
{
push @cards, $self->export_card($model, $s, %options);
}
if ($options{sort})
{
return sort { $a->entity_order cmp $b->entity_order } @cards;
}
return @cards;
}
sub export_card
{
my ($self, $model, $subject, %options) = @_;
$model = RDF::TrineShortcuts::rdf_parse($model)
unless blessed($model) && $model->isa('RDF::Trine::Model');
my $card = RDF::vCard->new_entity( profile=>'VCARD' );
my %categories;
my $triples = $model->get_statements($subject, undef, undef);
while (my $triple = $triples->next)
{
next
unless (substr($triple->predicate->uri, 0, length(&V)) eq &V or
substr($triple->predicate->uri, 0, length(&VX)) eq &VX);
if (defined $dispatch{$triple->predicate->uri}
and ref($dispatch{$triple->predicate->uri}) eq 'CODE')
{
my $code = $dispatch{$triple->predicate->uri};
$card->add($code->($self, $model, $triple));
}
elsif ($triple->predicate->uri eq V('category')
or $triple->predicate->uri eq VX('category'))
{
my $c = $self->_prop_extract_category($model, $triple);
$categories{$c}++;
}
elsif (! $triple->object->is_blank)
{
$card->add($self->_prop_export_simple($model, $triple));
}
}
if (keys %categories)
{
$card->add(
RDF::vCard::Line->new(
property => 'categories',
value => [[ sort keys %categories ]],
)
);
}
$card->add(
RDF::vCard::Line->new(
property => 'version',
value => $self->is_v4 ? '4.0' : '3.0',
)
);
$card->add(
RDF::vCard::Line->new(
property => 'prodid',
value => (defined $options{prodid} ? $options{prodid} : $PRODID),
)
) unless exists $options{prodid} && !defined $options{prodid};
$card->add(
RDF::vCard::Line->new(
property => 'source',
value => $options{source},
type_parameters => {value=>'URI'},
)
) if defined $options{source};
return $card;
}
{
my %dtmap = (
XSD('anyURI') => 'URI',
XSD('string') => 'TEXT',
XSD('integer') => 'INTEGER',
XSD('date') => 'DATE',
XSD('dateTime') => 'DATE-TIME',
XSD('duration') => 'DURATION',
'urn:iso:std:iso:8601#timeInterval' => 'PERIOD',
XSD('decimal') => 'FLOAT',
# BOOLEAN ?
);
sub _prop_export_simple
{
my ($self, $model, $triple) = @_;
my $prop = 'x-data';
if ($triple->predicate->uri =~ m/([^\#\/]+)$/)
{
$prop = $1;
}
my $val = flatten_node($triple->object);
my $params = undef;
if ($triple->object->is_literal
and $triple->object->has_datatype
and defined $dtmap{ $triple->object->literal_datatype })
{
$params = { value => $dtmap{ $triple->object->literal_datatype } };
}
elsif ($triple->object->is_literal
and $triple->object->has_language)
{
$params = { value=>'TEXT', language=>$triple->object->literal_value_language };
}
elsif ($triple->object->is_resource)
{
$params = { value=>'URI' };
}
return RDF::vCard::Line->new(
property => $prop,
value => $val,
type_parameters => $params,
);
}
}
sub _prop_export_adr
{
my ($self, $model, $triple) = @_;
my $adr = [];
foreach my $part (qw(post-office-box extended-address street-address locality
region postal-code country-name))
{
my @objects = $model->objects($triple->object, rdf_resource(V($part)));
push @$adr, [ map { flatten_node($_) } @objects ];
}
my $params = {};
my $types = {};
unless ($triple->object->is_literal)
{
my @types = $model->objects($triple->object, rdf_resource(RDF('type')));
push @types, $model->objects($triple->object, rdf_resource(VX('usage')));
foreach my $type (@types)
{
if ($type->is_resource and $type->uri =~ m/([^\#\/]+)$/)
{
$types->{ uc $1 } = 1;
}
elsif ($type->is_literal)
{
$types->{ uc $type->literal_value } = 1;
}
}
}
delete $types->{TEL};
delete $types->{EMAIL};
delete $types->{IMPP};
delete $types->{ADDRESS};
delete $types->{LABEL};
my @geos;
my $iter = $model->get_statements($triple->object, rdf_resource(VX('geo')), undef);
while (my $st = $iter->next)
{
my $gline = $self->_prop_export_geo($model, $st);
push @geos, $gline->_unescape_value($gline->value_to_string);
}
if (@geos)
{
$params->{geo} = \@geos;
}
if (%$types and $self->is_v4)
{
$params->{type} = [sort grep { !/^pref$/i } keys %$types];
$params->{pref} = 1 if $types->{PREF};
}
elsif (%$types)
{
$params->{type} = [sort keys %$types];
}
$params = undef unless %$params;
return RDF::vCard::Line->new(
property => 'adr',
value => $adr,
type_parameters => $params,
);
}
sub _prop_export_n
{
my ($self, $model, $triple) = @_;
my $n = [];
foreach my $part (qw(family-name given-name additional-name honorific-prefix honorific-suffix))
{
my @objects = $model->objects($triple->object, rdf_resource(V($part)));
push @$n, [ map { flatten_node($_) } @objects ];
}
return RDF::vCard::Line->new(
property => 'n',
value => $n,
);
}
sub _prop_export_agent
{
my ($self, $model, $triple) = @_;
if ($triple->object->is_literal)
{
return RDF::vCard::Line->new(
property => 'agent',
value => flatten_node($triple->object),
type_parameters => { value=>'TEXT' },
);
}
my $agent = $self->export_card($model, $triple->object);
return RDF::vCard::Line->new(
property => 'agent',
value => $agent->to_string,
type_parameters => { value=>'VCARD' },
);
}
sub _prop_export_org
{
my ($self, $model, $triple) = @_;
# Note: W3C Member Submission is inconsistent with regards to organiSation/organiZation.
my @objects = $model->objects_for_predicate_list($triple->object, rdf_resource(V('organization-name')), rdf_resource(V('organisation-name')));
my @values = map { flatten_node($_) } grep {$_->is_literal} @objects;
my $org = [ ($values[0] || '') ];
@objects = $model->objects_for_predicate_list($triple->object, rdf_resource(V('organization-unit')), rdf_resource(V('organisation-unit')));
push @$org, map { flatten_node($_) } grep {$_->is_literal} @objects;
return RDF::vCard::Line->new(
property => 'org',
value => $org,
);
}
sub _prop_export_geo
{
my ($self, $model, $triple) = @_;
my $g = [];
foreach my $part (qw(latitude longitude))
{
my @objects = $model->objects($triple->object, rdf_resource(V($part)));
my @values = map { flatten_node($_) } grep {$_->is_literal} @objects;
push @$g, ($values[0] || '');
}
if ($self->is_v4)
{
return RDF::vCard::Line->new(
property => 'geo',
value => sprintf('geo:%f,%f', @$g),
type_parameters => { value=>'URI' },
);
}
else
{
return RDF::vCard::Line->new(
property => 'geo',
value => $g,
type_parameters => { value=>'TEXT' },
);
}
}
# tel, email, label and impp may be typed
sub _prop_export_typed
{
my ($self, $model, $triple) = @_;
my $value_node = $triple->object;
unless ($value_node->is_literal)
{
my @objects = $model->objects($value_node, rdf_resource(RDF('value')));
foreach my $o (@objects)
{
unless ($o->is_blank)
{
$value_node = $o;
last;
}
}
}
my $value = flatten_node($value_node);
my $prop = 'x-data';
if ($triple->predicate->uri =~ m/([^\#\/]+)$/)
{
$prop = lc $1;
}
my $types = {};
my $params = {};
if ($prop eq 'email' and $value =~ /^mailto:(.+)$/i)
{
$value = $1;
$types->{INTERNET} = 1;
}
elsif ($prop eq 'tel' and $value =~ /^(tel|fax|modem):(.+)$/i)
{
if ($self->is_v4) #v4 telephone numbers are URIs
{
$params = { value=>'URI' };
}
else #v3 telephone numbers are text (well VALUE=PHONE-NUMBER technically)
{
$value = $2;
}
$types->{FAX} = 1 if lc $1 eq 'fax';
$types->{MODEM} = 1 if lc $1 eq 'modem';
}
elsif ($value_node->is_resource)
{
$params = { value=>'URI' };
}
unless ($triple->object->is_literal)
{
my @types = $model->objects($triple->object, rdf_resource(RDF('type')));
push @types, $model->objects($triple->object, rdf_resource(VX('usage')));
foreach my $type (@types)
{
if ($type->is_resource and $type->uri =~ m/([^\#\/]+)$/)
{
$types->{ uc $1 } = 1;
}
elsif ($type->is_literal)
{
$types->{ uc $type->literal_value } = 1;
}
}
}
delete $types->{TEL};
delete $types->{EMAIL};
delete $types->{IMPP};
delete $types->{ADDRESS};
delete $types->{LABEL};
if (%$types and $self->is_v4)
{
$params->{type} = [sort grep { !/^pref$/i } keys %$types];
$params->{pref} = 1 if $types->{PREF};
}
elsif (%$types)
{
$params->{type} = [sort keys %$types];
}
$params = undef unless %$params;
if ($prop eq 'tel')
{
$params->{'value'} ||= 'PHONE-NUMBER';
if ($self->is_v4 and $params->{'value'} ne 'URI' and $value =~ /^\+[0-9\s\-]+$/)
{
$value =~ s/\s/-/g;
$value = "tel:${value}";
$params->{'value'} = 'URI';
}
}
return RDF::vCard::Line->new(
property => $prop,
value => $value,
type_parameters => $params,
);
}
sub _prop_export_shortcut
{
my ($self, $model, $triple) = @_;
my $shortcuts = {
V('fax') => [V('tel') => ['FAX']],
V('homeAdr') => [V('adr') => ['HOME']],
V('homeTel') => [V('tel') => ['HOME']],
V('mobileEmail') => [V('email') => undef], # EMAIL;TYPE=CELL not allowed by RFC 2426
V('mobileTel') => [V('tel') => ['CELL']],
V('personalEmail') => [V('email') => undef], # RFC 2426 doesn't define TYPE=PERSONAL
V('unlabeledAdr') => [V('adr') => undef],
V('unlabeledEmail') => [V('email') => undef],
V('unlabeledTel') => [V('tel') => undef],
V('workAdr') => [V('adr') => ['WORK']],
V('workEmail') => [V('email') => undef], # EMAIL;TYPE=WORK not allowed by RFC 2426
V('workTel') => [V('tel') => ['WORK']],
};
if (exists $shortcuts->{$triple->predicate->uri})
{
my ($property_uri, $types) = @{ $shortcuts->{$triple->predicate->uri} };
my $line;
if (defined $dispatch{$property_uri}
and ref($dispatch{$property_uri}) eq 'CODE')
{
my $code = $dispatch{$property_uri};
$line = $code->($self, $model, $triple);
}
elsif (! $triple->object->is_blank)
{
$line = $self->_prop_export_simple($model, $triple);
}
if ($line)
{
push @{ $line->type_parameters->{type} }, @$types;
return $line;
}
}
}
sub _prop_export_binary
{
my ($self, $model, $triple) = @_;
my $line = $self->_prop_export_simple($model, $triple);
if ($self->is_v3 and $line->value->[0] =~ /^data:\S+$/)
{
my $data_uri = URI->new( $line->value->[0] );
my $data = $data_uri->data;
my $medium = $data_uri->media_type;
$line->value->[0] = MIME::Base64::encode_base64($data, '');
$line->type_parameters->{value} = 'BINARY';
$line->type_parameters->{encoding} = 'B';
$line->type_parameters->{type} = [ uc($1) ]
if $medium =~ m'^image/([a-z0-9\_\-\+]+)'i;
$line->type_parameters->{fmtype} = $medium
if $medium =~ m'.+/.+';
}
return $line;
}
sub _prop_extract_category
{
my ($self, $model, $triple) = @_;
if ($triple->object->is_literal)
{
return uc $triple->object->literal_value;
}
my @labels = grep
{ $_->is_literal }
$model->objects_for_predicate_list(
$triple->object,
rdf_resource('http://www.w3.org/2004/02/skos/core#prefLabel'),
rdf_resource('http://www.holygoat.co.uk/owl/redwood/0.1/tags/name'),
rdf_resource('http://www.w3.org/2000/01/rdf-schema#label'),
rdf_resource('http://www.w3.org/2004/02/skos/core#altLabel'),
rdf_resource('http://www.w3.org/2004/02/skos/core#notation'),
rdf_resource(RDF('value')),
);
if (@labels)
{
return uc $labels[0]->literal_value;
}
elsif ($triple->object->is_resource)
{
return $triple->object->uri;
}
}
1;
__END__
=head1 NAME
RDF::vCard::Exporter - export RDF data to vCard format
=head1 SYNOPSIS
use RDF::vCard;
my $input = "http://example.com/contact-data.rdf";
my $exporter = RDF::vCard::Exporter->new(vcard_version => 3);
print $_ foreach $exporter->export_cards($input);
=head1 DESCRIPTION
This module reads RDF and writes vCards.
=head2 Constructor
=over
=item * C<< new(%options) >>
Returns a new RDF::vCard::Exporter object.
Options:
=over
=item * B<vcard_version> - '3' or '4'. This module will happily use
vCard 3.0 constructs in vCard 4.0 and vice versa. But in certain places
it can lean one way or the other. This option allows you to influence
that.
=back
=back
=head2 Methods
=over
=item * C<< export_cards($input, %options) >>
Returns a list of vCards found in the input, in no particular order.
The input may be a URI, file name, L<RDF::Trine::Model> or anything else
that can be handled by the C<rdf_parse> method of L<RDF::TrineShortcuts>.
Supported options include B<sort> which, if set to true, causes the
output to be sorted by name (as well as is possible); B<source> which
allows you to provide the URL where the cards were sourced from; and
B<prodid> which allows you to set the product ID used in the output.
(A prodid must be in FPI format to be valid, though the module doesn't
check this. undef is allowed. By default, RDF::vCard:Exporter uses
its own prodid, and unless you have a good reason to change this, you
should probably let it.)
e.g.
my @cards = $exporter->export_cards(
$some_data,
sort => 1,
source => 'http://bigcorp.example.com/data.rdf',
prodid => '+//IDN example.net//NONSGML MyScript v 0.1//EN',
);
Each item in the list returned is an L<RDF::vCard::Entity>, though
that class overloads stringification, so you can just treat each item
as a string mostly.
=item * C<< export_card($input, $subject, %options) >>
As per C<export_cards> but exports just a single card.
The subject provided must be an RDF::Trine::Node::Blank or
RDF::Trine::Node::Resource of type v:VCard.
=item * C<< is_v3 >>
Returns true if this exporter is in vCard 3.0 mode.
=item * C<< is_v4 >>
Returns true if this exporter is in vCard 4.0 mode.
=back
=head2 RDF Input
Input is expected to use the newer of the 2010 revision of the W3C's
vCard vocabulary L<http://www.w3.org/Submission/vcard-rdf/>. (Note that
even though this was revised in 2010, the term URIs include "2006" in
them.)
Some extensions from the namespace L<http://buzzword.org.uk/rdf/vcardx#>
are also supported. (Namely: vx:usage, vx:kind, vx:gender, vx:sex,
vx:dday, vx:anniversary, vx:lang, vx:caladruri, vx:caluri, vx:fburl,
vx:impp, vx:source.)
The module author has made the decision not to support FOAF and
other RDF vocabularies that may be used to model contact information
for people and organisations, as they do not necessarily map cleanly
onto vCard. People hoping to map non-vCard RDF to vCard using
this module may have some luck pre-processing their RDF using a
rules-based reasoner.
=head2 vCard Output
The output of this module mostly aims at vCard 3.0 (RFC 2426) compliance.
In the face of weird input data though, (e.g. an FN property that is a
URI instead of a literal) it can pretty easily descend into exporting
junk, non-compliant vCards.
Many vCard 4.0 properties, such as the IMPP and KIND, are also supported.
The B<vcard_version> constructor option allows you to influence how some
properties like GEO and TEL (which differ between 3.0 and 4.0) are output.
=head1 SEE ALSO
L<RDF::vCard>, L<HTML::Microformats>, L<RDF::TrineShortcuts>.
L<http://www.w3.org/Submission/vcard-rdf/>.
L<http://www.perlrdf.org/>.
=head1 AUTHOR
Toby Inkster E<lt>tobyink@cpan.orgE<gt>.
=head1 COPYRIGHT
Copyright 2011 Toby Inkster
This library is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.
|