| 1 | package RDF::vCard::Importer; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use 5.008; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | 1 | ▶ | no warnings qw(uninitialized); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use Encode qw[]; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use RDF::TrineX::Functions -shortcuts; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use RDF::vCard::Entity; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use RDF::vCard::Line; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | 1 | ▶ | use Text::vFile::asData; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||
| 14 | 1 | ▶ | use namespace::clean; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||
| 16 | 1 | our $VERSION = '0.012'; | |||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||
| 18 | sub new | ||||||||||||||||||||||||||||||||||||||||||
| 19 | 0 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 20 | 0 | my ($class, %options) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 21 | 0 | my $self = bless { %options }, $class; | |||||||||||||||||||||||||||||||||||||||||
| 22 | 0 | ▶ | $self->init unless $self->model; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 23 | 0 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 24 | } | ||||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||
| 26 | sub init | ||||||||||||||||||||||||||||||||||||||||||
| 27 | 0 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
init: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 28 | 0 | my ($self, $model, %opts) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 29 | 0 | $self->{model} = rdf_parse($model, %opts); | |||||||||||||||||||||||||||||||||||||||||
| 30 | 0 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||||||
| 33 | sub model | ||||||||||||||||||||||||||||||||||||||||||
| 34 | 0 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
model: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 35 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 36 | 0 | return $self->{model}; | |||||||||||||||||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||
| 39 | 1 | *TO_RDF = \&model; | |||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||
| 41 | sub _ua | ||||||||||||||||||||||||||||||||||||||||||
| 42 | 0 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_ua: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||
| 43 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 44 | 0 | ▶ | $self->{ua} ||= LWP::UserAgent->new(agent => sprintf('%s/%s', __PACKAGE__, $VERSION)); | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$self->{"ua"} ||= "LWP::UserAgent"->new("agent", sprintf('%s/%s', __PACKAGE__, $RDF::vCard::Importer::VERSION))
MC/DC0%0/2
$self->{"ua"} ||= "LWP::UserAgent"->new("agent", sprintf('%s/%s', __PACKAGE__, $RDF::vCard::Importer::VERSION))
$self->{"ua"}
"LWP::UserAgent"->new("agent", sprintf('%s/%s', __PACKAGE__, $RDF::vCard::Importer::VERSION))
Truth table0%0/3
A ||= B
A= $self->{"ua"}B= "LWP::UserAgent"->new("agent", sprintf('%s/%s', __PACKAGE__, $RDF::vCard::Importer::VERSION))
| |||||||||||||||||||||||||||||||||||||||||||
| 45 | 0 | return $self->{ua}; | |||||||||||||||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||
| 48 | sub import_file | ||||||||||||||||||||||||||||||||||||||||||
| 49 | 0 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
import_file: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 50 | 0 | my ($self, $file, %options) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 51 | 0 | open my $fh, "<:encoding(UTF-8)", $file; | |||||||||||||||||||||||||||||||||||||||||
| 52 | 0 | my $cards = Text::vFile::asData->new->parse($fh); | |||||||||||||||||||||||||||||||||||||||||
| 53 | 0 | close $fh; | |||||||||||||||||||||||||||||||||||||||||
| 54 | 0 | return $self->_process($cards, %options); | |||||||||||||||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||
| 57 | sub import_fh | ||||||||||||||||||||||||||||||||||||||||||
| 58 | 0 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
import_fh: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 59 | 0 | my ($self, $fh, %options) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 60 | 0 | my $cards = Text::vFile::asData->new->parse($fh); | |||||||||||||||||||||||||||||||||||||||||
| 61 | 0 | return $self->_process($cards, %options); | |||||||||||||||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||||||
| 64 | sub import_url | ||||||||||||||||||||||||||||||||||||||||||
| 65 | 0 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
import_url: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 66 | 0 | my ($self, $url) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 67 | 0 | my $r = $self->_ua->get($url, Accept=>'text/directory;profile=vCard, text/vcard, text/x-vcard, text/directory;q=0.1'); | |||||||||||||||||||||||||||||||||||||||||
| 68 | 0 | ▶ | return unless $r->is_success; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 69 | 0 | return $self->import_string($r->decoded_content, lang => $r->content_language); | |||||||||||||||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||
| 72 | sub import_string | ||||||||||||||||||||||||||||||||||||||||||
| 73 | 0 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
import_string: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 74 | 0 | my ($self, $data, %options) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 75 | 0 | my @lines = split /\r?\n/, $data; | |||||||||||||||||||||||||||||||||||||||||
| 76 | 0 | my $cards = Text::vFile::asData->new->parse_lines(@lines); | |||||||||||||||||||||||||||||||||||||||||
| 77 | 0 | return $self->_process($cards, %options); | |||||||||||||||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||||||
| 80 | sub _process | ||||||||||||||||||||||||||||||||||||||||||
| 81 | 0 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_process: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||
| 82 | 0 | my ($self, $cards, %options) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||||||||||
| 84 | 0 | my @Cards; | |||||||||||||||||||||||||||||||||||||||||
| 85 | 0 | foreach my $c (@{ $cards->{objects} }) | |||||||||||||||||||||||||||||||||||||||||
| 86 | { | ||||||||||||||||||||||||||||||||||||||||||
| 87 | 0 | push @Cards, $self->_process_card($c, %options); | |||||||||||||||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||
| 90 | 0 | return @Cards; | |||||||||||||||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||||
| 93 | sub _process_card | ||||||||||||||||||||||||||||||||||||||||||
| 94 | 0 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_process_card: not called CC=11 SCAR=48.8
| |||||||||||||||||||||||||||||||||||||||||||
| 95 | 0 | my ($self, $card, %options) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 96 | 0 | my $C = RDF::vCard->new_entity( profile => $card->{type} ); | |||||||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||||||||
| 98 | 0 | ▶ | while (my ($prop, $vals) = each %{ $card->{properties} }) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 99 | { | ||||||||||||||||||||||||||||||||||||||||||
| 100 | 0 | my $group; | |||||||||||||||||||||||||||||||||||||||||
| 101 | 0 | ▶ | if ($prop =~ /^(.+)\.([^\.]+)$/) # ignore vCard 4.0 grouping construct | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 102 | { | ||||||||||||||||||||||||||||||||||||||||||
| 103 | 0 | $prop = $2; | |||||||||||||||||||||||||||||||||||||||||
| 104 | 0 | $group = $1; | |||||||||||||||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||||||
| 107 | 0 | foreach my $val (@$vals) | |||||||||||||||||||||||||||||||||||||||||
| 108 | { | ||||||||||||||||||||||||||||||||||||||||||
| 109 | 0 | my $strval = $val->{value}; | |||||||||||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||||||||||||
| 111 | # I wish Text::vFile::asData did this for me! | ||||||||||||||||||||||||||||||||||||||||||
| 112 | 0 | ▶ | my $structured_value = ($prop =~ /^(ADR|CATEGORIES|GEO|N|ORG)$/i) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 113 | ? $self->_extract_structure($strval) | ||||||||||||||||||||||||||||||||||||||||||
| 114 | : RDF::vCard::Line->_unescape_value($strval); | ||||||||||||||||||||||||||||||||||||||||||
| 115 | # Could technically extract structure for all properties, | ||||||||||||||||||||||||||||||||||||||||||
| 116 | # but it's a waste of time, and some of the RDF::vCard::Line | ||||||||||||||||||||||||||||||||||||||||||
| 117 | # code might cope badly. | ||||||||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||||||||
| 119 | my $L = RDF::vCard::Line->new( | ||||||||||||||||||||||||||||||||||||||||||
| 120 | property => uc $prop, | ||||||||||||||||||||||||||||||||||||||||||
| 121 | value => $structured_value, | ||||||||||||||||||||||||||||||||||||||||||
| 122 | 0 | type_parameters => do { | |||||||||||||||||||||||||||||||||||||||||
| 123 | # force keys to uppercase | ||||||||||||||||||||||||||||||||||||||||||
| 124 | 0 | my (%tp, $k, $v); | |||||||||||||||||||||||||||||||||||||||||
| 125 | 0 | ▶ | $tp{uc $k} = $v while ($k, $v) = each %{$val->{param}}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 126 | 0 | \%tp; | |||||||||||||||||||||||||||||||||||||||||
| 127 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 128 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 129 | $L->type_parameters->{TYPE} = [split /,/, $L->type_parameters->{TYPE}] | ||||||||||||||||||||||||||||||||||||||||||
| 130 | 0 | ▶ | if ($L->type_parameters and $L->type_parameters->{TYPE}); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$L->type_parameters and $L->type_parameters->{"TYPE"}
MC/DC0%0/2
$L->type_parameters and $L->type_parameters->{"TYPE"}
$L->type_parameters
$L->type_parameters->{"TYPE"}
Truth table0%0/3
A and B
A= $L->type_parametersB= $L->type_parameters->{"TYPE"}
| |||||||||||||||||||||||||||||||||||||||||||
| 131 | 0 | ▶ | $L->type_parameters->{_GROUP} = $group | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 132 | if $group; | ||||||||||||||||||||||||||||||||||||||||||
| 133 | 0 | ▶ | $L->type_parameters->{LANG} ||= $options{lang} if defined $options{lang}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$L->type_parameters->{"LANG"} ||= $options{"lang"}
MC/DC0%0/2
$L->type_parameters->{"LANG"} ||= $options{"lang"}
$L->type_parameters->{"LANG"}
$options{"lang"}
Truth table0%0/3
A ||= B
A= $L->type_parameters->{"LANG"}B= $options{"lang"}
| |||||||||||||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||||||||||||
| 135 | 0 | $C->add($L); | |||||||||||||||||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||||||||||||
| 139 | 0 | $C->add_to_model( $self->model ); | |||||||||||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||||||||||||
| 141 | 0 | return $C; | |||||||||||||||||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||||||||||||
| 144 | sub _extract_structure | ||||||||||||||||||||||||||||||||||||||||||
| 145 | 0 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_extract_structure: not called CC=10 SCAR=47.0
| |||||||||||||||||||||||||||||||||||||||||||
| 146 | 0 | my ($self, $string) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 147 | 0 | my @naive_parts = split /;/, $string; | |||||||||||||||||||||||||||||||||||||||||
| 148 | 0 | my @parts; | |||||||||||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||||||||||||
| 150 | 0 | ▶ | while (my $part = shift @naive_parts) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 151 | { | ||||||||||||||||||||||||||||||||||||||||||
| 152 | 0 | push @parts, $part; | |||||||||||||||||||||||||||||||||||||||||
| 153 | 0 | ▶ | while ($parts[-1] =~ /\\$/ and $parts[-1] !~ /\\\\$/ and @naive_parts) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
$parts[-1] =~ /\\$/ and not $parts[-1] =~ /\\\\$/ and @naive_parts
$parts[-1] =~ /\\$/ and not $parts[-1] =~ /\\\\$/
MC/DC0%0/3
$parts[-1] =~ /\\$/ and not $parts[-1] =~ /\\\\$/ and @naive_parts
$parts[-1] =~ /\\$/
not $parts[-1] =~ /\\\\$/
@naive_parts
Truth table0%0/4
A and not(B) and C
A= $parts[-1] =~ /\\$/B= not $parts[-1] =~ /\\\\$/C= @naive_parts
| |||||||||||||||||||||||||||||||||||||||||||
| 154 | { | ||||||||||||||||||||||||||||||||||||||||||
| 155 | 0 | $parts[-1] =~ s/\\$/;/; | |||||||||||||||||||||||||||||||||||||||||
| 156 | 0 | $parts[-1] .= shift @naive_parts; | |||||||||||||||||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||||||||||||||
| 160 | 0 | my @rv; | |||||||||||||||||||||||||||||||||||||||||
| 161 | 0 | foreach my $part (@parts) | |||||||||||||||||||||||||||||||||||||||||
| 162 | { | ||||||||||||||||||||||||||||||||||||||||||
| 163 | 0 | my @naive_subparts = split /,/, $part; | |||||||||||||||||||||||||||||||||||||||||
| 164 | 0 | my @subparts; | |||||||||||||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||||||||||||||
| 166 | 0 | ▶ | while (my $subpart = shift @naive_subparts) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 167 | { | ||||||||||||||||||||||||||||||||||||||||||
| 168 | 0 | push @subparts, $subpart; | |||||||||||||||||||||||||||||||||||||||||
| 169 | 0 | ▶ | while ($subparts[-1] =~ /\\$/ and $subparts[-1] !~ /\\\\$/ and @naive_subparts) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
$subparts[-1] =~ /\\$/ and not $subparts[-1] =~ /\\\\$/ and @naive_subparts
$subparts[-1] =~ /\\$/ and not $subparts[-1] =~ /\\\\$/
MC/DC0%0/3
$subparts[-1] =~ /\\$/ and not $subparts[-1] =~ /\\\\$/ and @naive_subparts
$subparts[-1] =~ /\\$/
not $subparts[-1] =~ /\\\\$/
@naive_subparts
Truth table0%0/4
A and not(B) and C
A= $subparts[-1] =~ /\\$/B= not $subparts[-1] =~ /\\\\$/C= @naive_subparts
| |||||||||||||||||||||||||||||||||||||||||||
| 170 | { | ||||||||||||||||||||||||||||||||||||||||||
| 171 | 0 | $subparts[-1] =~ s/\\$/,/; | |||||||||||||||||||||||||||||||||||||||||
| 172 | 0 | $subparts[-1] .= shift @naive_subparts; | |||||||||||||||||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||||||||||||||||
| 176 | 0 | push @rv, [ map { RDF::vCard::Line->_unescape_value($_) } @subparts ]; | |||||||||||||||||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||||||||||||||||
| 178 | 0 | return [@rv]; | |||||||||||||||||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||||||||||||||||
| 181 | 1 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||||||||||||||||
| 183 | __END__ | ||||||||||||||||||||||||||||||||||||||||||