/usr/share/perl5/RDF/vCard/Importer.pm is in librdf-vcard-perl 0.012-1.
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 | package RDF::vCard::Importer;
use 5.008;
use strict;
use warnings;
no warnings qw(uninitialized);
use Encode qw[];
use RDF::TrineX::Functions -shortcuts;
use RDF::vCard::Entity;
use RDF::vCard::Line;
use Text::vFile::asData;
use namespace::clean;
our $VERSION = '0.012';
sub new
{
my ($class, %options) = @_;
my $self = bless { %options }, $class;
$self->init unless $self->model;
return $self;
}
sub init
{
my ($self, $model, %opts) = @_;
$self->{model} = rdf_parse($model, %opts);
return $self;
}
sub model
{
my ($self) = @_;
return $self->{model};
}
*TO_RDF = \&model;
sub _ua
{
my ($self) = @_;
$self->{ua} ||= LWP::UserAgent->new(agent => sprintf('%s/%s', __PACKAGE__, $VERSION));
return $self->{ua};
}
sub import_file
{
my ($self, $file, %options) = @_;
open my $fh, "<:encoding(UTF-8)", $file;
my $cards = Text::vFile::asData->new->parse($fh);
close $fh;
return $self->_process($cards, %options);
}
sub import_fh
{
my ($self, $fh, %options) = @_;
my $cards = Text::vFile::asData->new->parse($fh);
return $self->_process($cards, %options);
}
sub import_url
{
my ($self, $url) = @_;
my $r = $self->_ua->get($url, Accept=>'text/directory;profile=vCard, text/vcard, text/x-vcard, text/directory;q=0.1');
return unless $r->is_success;
return $self->import_string($r->decoded_content, lang => $r->content_language);
}
sub import_string
{
my ($self, $data, %options) = @_;
my @lines = split /\r?\n/, $data;
my $cards = Text::vFile::asData->new->parse_lines(@lines);
return $self->_process($cards, %options);
}
sub _process
{
my ($self, $cards, %options) = @_;
my @Cards;
foreach my $c (@{ $cards->{objects} })
{
push @Cards, $self->_process_card($c, %options);
}
return @Cards;
}
sub _process_card
{
my ($self, $card, %options) = @_;
my $C = RDF::vCard->new_entity( profile => $card->{type} );
while (my ($prop, $vals) = each %{ $card->{properties} })
{
my $group;
if ($prop =~ /^(.+)\.([^\.]+)$/) # ignore vCard 4.0 grouping construct
{
$prop = $2;
$group = $1;
}
foreach my $val (@$vals)
{
my $strval = $val->{value};
# I wish Text::vFile::asData did this for me!
my $structured_value = ($prop =~ /^(ADR|CATEGORIES|GEO|N|ORG)$/i)
? $self->_extract_structure($strval)
: RDF::vCard::Line->_unescape_value($strval);
# Could technically extract structure for all properties,
# but it's a waste of time, and some of the RDF::vCard::Line
# code might cope badly.
my $L = RDF::vCard::Line->new(
property => uc $prop,
value => $structured_value,
type_parameters => do {
# force keys to uppercase
my (%tp, $k, $v);
$tp{uc $k} = $v while ($k, $v) = each %{$val->{param}};
\%tp;
},
);
$L->type_parameters->{TYPE} = [split /,/, $L->type_parameters->{TYPE}]
if ($L->type_parameters and $L->type_parameters->{TYPE});
$L->type_parameters->{_GROUP} = $group
if $group;
$L->type_parameters->{LANG} ||= $options{lang} if defined $options{lang};
$C->add($L);
}
}
$C->add_to_model( $self->model );
return $C;
}
sub _extract_structure
{
my ($self, $string) = @_;
my @naive_parts = split /;/, $string;
my @parts;
while (my $part = shift @naive_parts)
{
push @parts, $part;
while ($parts[-1] =~ /\\$/ and $parts[-1] !~ /\\\\$/ and @naive_parts)
{
$parts[-1] =~ s/\\$/;/;
$parts[-1] .= shift @naive_parts;
}
}
my @rv;
foreach my $part (@parts)
{
my @naive_subparts = split /,/, $part;
my @subparts;
while (my $subpart = shift @naive_subparts)
{
push @subparts, $subpart;
while ($subparts[-1] =~ /\\$/ and $subparts[-1] !~ /\\\\$/ and @naive_subparts)
{
$subparts[-1] =~ s/\\$/,/;
$subparts[-1] .= shift @naive_subparts;
}
}
push @rv, [ map { RDF::vCard::Line->_unescape_value($_) } @subparts ];
}
return [@rv];
}
1;
__END__
=head1 NAME
RDF::vCard::Importer - import RDF data from vCard format
=head1 SYNOPSIS
use RDF::vCard;
use RDF::TrineShortcuts qw':all';
my $importer = RDF::vCard::Importer->new;
print $_
foreach $importer->import_file('contacts.vcf');
print rdf_string($importer->model => 'RDFXML');
=head1 DESCRIPTION
This module reads vCards and writes RDF.
=head2 Constructor
=over
=item * C<< new(%options) >>
Returns a new RDF::vCard::Importer object and initialises it.
The only valid option currently is B<ua> which can be set to an LWP::UserAgent
for those rare occasions that the Importer needs to fetch stuff from the Web.
=back
=head2 Methods
=over
=item * C<< init >>
Reinitialise the importer. Forgets any cards that have already been imported.
=item * C<< model >>
Return an RDF::Trine::Model containing data for all cards that have been
imported since the importer was last initialised.
=item * C<< import_file($filename, %options) >>
Imports vCard data from a file on the file system.
The data is added to the importer's model (and can be retrieved using the
C<model> method).
This function returns a list of L<RDF::vCard::Entity> objects, so it's
also possible to access the data that way.
There is currently only one supported option: C<lang> which takes an
ISO language code indicating the default language of text within the vCard
data.
=item * C<< import_fh($filehandle, %options) >>
As per C<import_file>, but operates on a file handle.
=item * C<< import_string($string, %options) >>
As per C<import_file>, but operates on vCard data in a string.
=item * C<< import_url($url) >>
As per C<import_file>, but fetches vCard data from a Web address.
Sends an HTTP Accept header of:
text/directory;profile=vCard,
text/vcard,
text/x-vcard,
text/directory;q=0.1
=back
=begin private
=item TO_RDF
=end private
=head2 vCard Input
vCard 3.0 should be supported fairly completely. Some vCard 4.0 constructs
will also work.
Much of the heavy lifting is performed by L<Text::vFile::asData>, so this
module may be affected by bugs in that distribution.
=head2 RDF Output
Output uses 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 output.
The AGENT property is currently omitted from output. This will be added
in a later version.
=head1 SEE ALSO
L<RDF::vCard>.
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.
|