vFile.pm

filter: Statementstmt 79.3% 115 / 145 Branchbran 59.0% 39 / 66 Conditioncond 57.1% 8 / 14 MC/DCmcdc 22.2% 2 / 9 Subroutinesub 64.7% 11 / 17 Podpod 91.6% 11 / 12 totaltotal 70.7% 186 / 263 CC 40 SCAR 23.6
CC
40
Cov
72%
load
19.2
load_singleDate
12.0
load_singleDuration
12.0
CRAP 10.6
« vCard.pm ↑ Summary vCard/ADR.pm »
1 package Net::vFile;
2  
3 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use Encode qw(decode is_utf8);
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 # This stuff will be in Net::vCalendar
8 # use DateTime::Format::ICal;
9  
10 =head1 NAME
11  
12 Net::vFile - Generic module which can read and write "vFile" files such as vCard (RFC 2426) and vCalendar (RFC 2445).
13 The result of loading this data is a collection of objects which will grant you easy access to the properties. Then
14 the module can write your objects back to a data file.
15  
16 =head1 SYNOPIS
17  
18 use Net::vCard;
19  
20 my $cards = Net::vCard->loadFile( "foo.vCard", "blort.vCard", "whee.vCard" );
21  
22 foreach my $card (@$cards) {
23 spam ( $card->EMAIL('default') );
24 }
25  
26 =head1 DETAILS
27  
28 The way this processor works is that it reads the vFile line by line.
29  
30 1 - BEGIN:(.*) tag
31  
32 $1 is looked up in classMap; class is loaded; new object of this class is created
33 ie/ $Net::vFile::classMap{'VCARD'}="Net::vCard";
34 $object=$classMap{'VCARD'}->new;
35  
36 n.b. classMap is a package variable for Net::vFile
37  
38 2 - All lines are read and stored until a BEGIN tag (goto 1) or END tag (goto 3) is reached
39  
40 3 - END:(.*) tag
41  
42 Signals that all entry data has been obtained and now the rows of data are processed
43  
44 4 - Data is concatenated - thanks to Net::iCal for the strategy; the tag label and data are obtained
45  
46 5 - The data handler is identified via $object->varHandler->{$label}
47  
48 There are some generic handlers for common data types such as simple strings, dates, etc. More
49 elaborate data types such as N, ADR, etc. need special treatment and are declared explititly
50 in classes as "load_XXX" such as "load_N"
51  
52 You should be able to override and extend the processing by taking Net::vCard.pm as your example
53 and adjusting as necessary.
54  
55 The resulting data structure is a bit bulky - but is such that it can express vCard data completely and
56 reliably
57  
58 Put in a dump of a vCard here
59  
60 =head1 DEPENDENCIES
61  
62 DateTime::Format::ICal
63  
64 =over 4
65  
66 =item \@objects = loadFile( filename [, filename ... ] )
67  
68 Loads the vFiles and returns an array of objects
69  
70 =cut
71  
72 4 ▶sub loadFile {
Subroutine loadFile: called CC=7 SCAR=21.5
Pod documented
73  
74 4 my $self=shift;
75 4 ▶ $self=$self->new unless ref($self);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 4 0 4
76  
77 4 foreach my $fn (@_) {
78 5 my $fh;
79 5 ▶ if (open $fh, $fn) {
Branch: 1 branch
BranchTrueFalseTotal
if (open $fh, $fn) { } 5 0 5
80 5 ▶ until (eof $fh) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eof $fh) 5 5 10
81 5 $self->load($fh, $fn);
82 }
83 } else {
84 0 warn "Cannot open $fn\n";
85 0 next;
86 }
87 5 close $fh;
88 }
89  
90 # Usually people only load one type - VCARD
91 # - which will be represented by a single top level
92 # hash entry
93 # However it is not impossible to load VCARD, VCALENDAR and more all at once
94 # - in which case they get a scalar hashref
95
96 4 ▶ if ( scalar values %$self == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar values %$self == 0) 0 4 4
97 0 return undef;
98 }
99  
100 4 ▶ if ( scalar values %$self == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar values %$self == 1) { } 4 0 4
101 4 return (values %$self)[0];
102 } else {
103 0 return $self;
104 }
105  
106 }
107  
108 # Classes inject their desired mappings
109 5 our %classMap=(
110  
111 # VCARD => "Net::vCard",
112  
113 # VCALENDAR => "Net::vCalendar",
114 # VALARM => "Net::vCalendar::vAlarm",
115 # VEVENT => "Net::vCalendar::vEvent",
116 # VTODO => "Net::vCalendar::vTodo",
117  
118 );
119  
120 =item $object = class->new
121  
122 Make a new object
123  
124 =cut
125  
126 9 ▶sub new {
Subroutine new: called CC=2 SCAR=7.1
Pod documented
127  
128 9 ▶ my $class = ref($_[0]) ? ref(shift) : shift;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 9 9
129  
130 9 my $self=bless {}, $class;
131  
132 9 return $self;
133  
134 }
135  
136 =item \@objects = Class->load( filehandle )
137  
138 Loads data from file handle and creates objects as necessary
139  
140 =cut
141  
142  
143 10 ▶sub load {
Subroutine load: called CC=18 SCAR=29.6
Pod documented
144  
145 10 my ($class, $self);
146 10 ▶ if (ref ($_[0])) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0]) { } 10 0 10
147 10 $self=shift;
148 10 $class=ref($self);
149 }
150 else {
151 0 $class=shift;
152 0 $self=$class->new;
153 }
154 10 my $fh =shift;
155 10 my $parent=shift;
156  
157 10 ▶ $self->{'_parent'}=$parent if ref $parent;
Branch: 1 branch
BranchTrueFalseTotal
if ref $parent 5 5 10
158  
159 10 my @lines=();
160 10 my $varHandler=$class->varHandler;
161  
162 10 my $decoder;
163 10 $_ = <$fh>;
164 10 $decoder=Encode::find_encoding("UTF-8");
165 10 ▶ if ( /^\000/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^\000/) 4 6 10
166 4 $decoder=Encode::find_encoding("UTF-16BE");
167 }
168 10 ▶ if ( /^[^\000]\000/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^[^\000]\000/) 0 10 10
169 0 $decoder=Encode::find_encoding("UTF-16LE");
170 }
171  
172 10 my $thing="";
173 10 ▶ while ( $_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_) 81 0 81
174  
175 81 my $line = $decoder->decode($_);
176 81 $line =~ s/[\r\n]+$//;
177  
178 81 ▶ if ($line =~ /^BEGIN:(.+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^BEGIN:(.+)/) 5 76 81
179 5 $thing=$1;
180 5 ▶ my $subclass= $classMap{uc $thing} || die "Don't know how to load ${thing}s\n";
Condition50%1/2
$Net::vFile::classMap{uc $thing} || die("Don't know how to load ${thing}s\n")
l!l
50
MC/DC0%0/1
$Net::vFile::classMap{uc $thing} || die("Don't know how to load ${thing}s\n")
$Net::vFile::classMap{uc $thing}
Truth table50%1/2
A
A= $Net::vFile::classMap{uc $thing}
Aresult
11
00
181 5 ▶ eval "use $subclass"; die $@ if $@;
Subroutine BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 5 5
182 5 push @{$self->{$thing}}, $subclass->new->load($fh, $self);
183 5 next;
184 }
185  
186 76 ▶ last if $line =~ /^END:${thing}/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^END:$thing/ 10 66 76
187  
188 66 push @lines, $line;
189  
190 66 $_ = <$fh>;
191 }
192  
193 10 ▶ while ( @lines ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@lines) 66 10 76
194  
195 66 my $line = shift @lines;
196 66 ▶ while ( @lines && $lines[0] =~ /^\s(.*)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@lines and $lines[0] =~ /^\s(.*)/) 0 66 66
Condition66%2/3
@lines and $lines[0] =~ /^\s(.*)/
!ll&&!rl&&r
5610
MC/DC0%0/2
@lines and $lines[0] =~ /^\s(.*)/
@lines $lines[0] =~ /^\s(.*)/
Truth table66%2/3
A and B
A= @linesB= $lines[0] =~ /^\s(.*)/
ABresult
0X0
100
111
197 0 $line .= $1;
198 0 shift @lines;
199 }
200  
201 # Non-typed line data
202 66 ▶ if ( $line =~ /^([\w\-]+):(.*)/ && exists $varHandler->{$1} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^([\w\-]+):(.*)/ and exists $varHandler->{$1}) 27 39 66
Condition66%2/3
$line =~ /^([\w\-]+):(.*)/ and exists $varHandler->{$1}
!ll&&!rl&&r
39027
MC/DC50%1/2
$line =~ /^([\w\-]+):(.*)/ and exists $varHandler->{$1}
$line =~ /^([\w\-]+):(.*)/ exists $varHandler->{$1}
Truth table66%2/3
A and B
A= $line =~ /^([\w\-]+):(.*)/B= exists $varHandler->{$1}
ABresult
0X0
100
111
203 27 my $h="load_$varHandler->{$1}";
204 27 $self->$h($1, undef, $2);
205 27 next;
206 }
207  
208 # Typed line data
209 39 ▶ if ( $line =~ /^([\w\-]+);([^:]*):(.*)/ && exists $varHandler->{$1} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^([\w\-]+);([^:]*):(.*)/ and exists $varHandler->{$1}) 24 15 39
Condition66%2/3
$line =~ /^([\w\-]+);([^:]*):(.*)/ and exists $varHandler->{$1}
!ll&&!rl&&r
01524
MC/DC50%1/2
$line =~ /^([\w\-]+);([^:]*):(.*)/ and exists $varHandler->{$1}
$line =~ /^([\w\-]+);([^:]*):(.*)/ exists $varHandler->{$1}
Truth table66%2/3
A and B
A= $line =~ /^([\w\-]+);([^:]*):(.*)/B= exists $varHandler->{$1}
ABresult
0X0
100
111
210  
211 24 my $h="load_$varHandler->{$1}";
212 24 my ($var, $data)=($1, $3);
213  
214 24 my %attr=();
215 24 map { /([^=]+)=(.*)/; push @{$attr{uc $1}}, $2 } split (/(?<!\\);/, $2);
216  
217 # XXX I want to split up the attributes here
218 24 $self->$h($var, \%attr, $data);
219 24 next;
220 }
221  
222 # X-values
223 15 ▶ if ( $line =~ /^(X-[\w\-]+);?([^:]*):(.*)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^(X-[\w\-]+);?([^:]*):(.*)/) 15 0 15
224  
225 15 my ($var, $data)=($1, $3);
226  
227 15 my %attr=();
228 15 map { /([^=]+)=(.*)/; push @{$attr{uc $1}}, $2 } split (/(?<!\\);/, $2);
229  
230 15 $self->load_singleTextTyped($var, \%attr, $data);
231 15 next;
232 }
233  
234 0 $self->error( $line );
235  
236 }
237  
238 10 return $self;
239  
240 }
241  
242 =item $object->error
243  
244 Called when a line cannot be successfully decoded
245  
246 =back
247  
248 =cut
249  
250 0 ▶sub error { warn ref($_[0]) . " ERRORLINE: $_[1]\n"; }
Subroutine error: not called CC=1 SCAR=6.9
Pod documented
251  
252 =head1 DATA HANDLERS
253  
254 =over 4
255  
256 =item varHandler
257  
258 Returns a hash ref mapping the item label to a handler name. Ie:
259  
260 {
261 'FN' => 'singleText',
262 'N' => 'N',
263 'NICKNAME' => 'multipleText',
264 'PHOTO' => 'singleBinary',
265 'BDAY' => 'singleText',
266 'ADR' => 'ADR',
267 };
268  
269 =cut
270
271 0 ▶sub varHandler {
Subroutine varHandler: not called CC=1 SCAR=6.9
Pod documented
272 0 return {};
273 }
274  
275 =item typeDefault
276  
277 Additional information where handlers require type info. Such as ADR - is this
278 a home, postal, or whatever? If not supplied the RFC specifies what types they should
279 default to.
280  
281 from vCard:
282  
283 {
284 'ADR' => [ qw(intl postal parcel work) ],
285 'LABEL' => [ qw(intl postal parcel work) ],
286 'TEL' => [ qw(voice) ],
287 'EMAIL' => [ qw(internet) ],
288 };
289  
290 =cut
291  
292 0 ▶sub typeDefault {
Subroutine typeDefault: not called CC=1 SCAR=6.9
Pod documented
293 0 return {};
294 }
295  
296 =item load_singleText
297  
298 Loads a single text item with no processing other than unescape text
299  
300 =cut
301  
302 22 ▶sub load_singleText {
Subroutine load_singleText: called CC=2 SCAR=6.9
Pod documented
303  
304 22 my $val=$_[3];
305 22 $val=~s/\\([\n,])/$1/gs;
306 # $val=~s/\\n/\n/gs;
307 22 $_[0]->{$_[1]}{'val'}=$val;
308 22 ▶ $_[0]->{$_[1]}{'_attr'}=$_[2] if $_[2];
Branch: 1 branch
BranchTrueFalseTotal
if $_[2] 3 19 22
309  
310 }
311  
312 =item _singleText
313  
314 Accessor for single text items
315  
316 =cut
317  
318 1 ▶sub _singleText {
Subroutine _singleText: called CC=2 SCAR=8.1
319  
320 1 ▶ if ($_[2]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[2]) 0 1 1
321 0 $_[0]->{$_[1]}{'val'}=$_[0];
322 }
323 1 return $_[0]->{$_[1]}{'val'};
324  
325 }
326  
327 =item load_singleDate
328  
329 Loads a date creating a DateTime::Format::ICal object. Thanks Dave!
330  
331 =cut
332  
333 0 ▶sub load_singleDate {
Subroutine load_singleDate: not called CC=3 SCAR=24.8
Pod documented
334  
335 0 my $val=$_[3];
336 0 eval {
337 0 $_[0]->{$_[1]}{'val'}=DateTime::Format::ICal->parse_datetime( iso8601 => $val );
338 0 ▶ }; if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
339 0 warn "$val; $@\n";
340 }
341 0 ▶ $_[0]->{$_[1]}{'_attr'}=$_[2] if $_[2];
Branch: 1 branch
BranchTrueFalseTotal
if $_[2] 0 0 0
342  
343 }
344  
345 =item load_singleDuration
346  
347 Loads a data duration using DateTime::Format::ICal.
348  
349 =cut
350  
351 0 ▶sub load_singleDuration {
Subroutine load_singleDuration: not called CC=3 SCAR=24.8
Pod documented
352  
353 0 my $val=$_[3];
354  
355 0 eval {
356 0 $_[0]->{$_[1]}{'val'}=DateTime::Format::ICal->parse_duration( $val );
357 0 ▶ }; if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
358 0 warn "$val; $@\n";
359 }
360  
361 0 ▶ $_[0]->{$_[1]}{'_attr'}=$_[2] if $_[2];
Branch: 1 branch
BranchTrueFalseTotal
if $_[2] 0 0 0
362  
363 }
364  
365 =item load_multipleText
366  
367 This is text that is separated by commas. The text is then unescaped. An array
368 of items is created.
369  
370 =cut
371  
372 3 ▶sub load_multipleText {
Subroutine load_multipleText: called CC=2 SCAR=7.0
Pod documented
373  
374 3 my @vals=split /(?<!\\),/, $_[3];
375 3 map { s/\\,/,/ } @vals;
376  
377 3 $_[0]->{$_[1]}{'val'}=\@vals;
378 3 ▶ $_[0]->{$_[1]}{'_attr'}=$_[2] if $_[2];
Branch: 1 branch
BranchTrueFalseTotal
if $_[2] 0 3 3
379  
380 }
381  
382 =item load_singleTextType
383  
384 Load text that has a type attribute. Each text of different type attributes
385 will be handled independantly in as a hash entry. If no type attribute is supplied
386 then the typeDefaults types will be used. A line can have multiple types. In the
387 case where multiple types have the same value "_alias" indicators are created.
388 The preferred type is stored in "_pref"
389  
390 =cut
391  
392 28 ▶sub load_singleTextTyped {
Subroutine load_singleTextTyped: called CC=9 SCAR=23.8
Pod undocumented
393
394 28 my $typeDefault=$_[0]->typeDefault;
395  
396 28 my $attr=$_[2];
397  
398 28 my %type=();
399 28 map { map { $type{lc $_}=1 } split /,/, $_ } @{$attr->{TYPE}};
400 0 ▶ map { $type{ lc $_ }=1 } @{$typeDefault->{$_[1]}} unless scalar(keys %type);
Branch: 1 branch
BranchTrueFalseTotal
unless scalar keys %type 0 28 28
401  
402 28 my $pref=0;
403 28 ▶ if ($type{pref}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type{"pref"}) 25 3 28
404 25 delete $type{pref};
405 25 $pref=1;
406 }
407  
408 28 my @types=sort keys %type;
409 28 ▶ my $actual=@types ? shift @types : "default";
Branch: 1 branch
BranchTrueFalseTotal
@types ? : 28 0 28
410  
411 28 $_[0]->{$_[1]}{$actual}=$_[3];
412  
413 28 ▶ $_[0]->{$_[1]}{_pref}=$actual if $pref;
Branch: 1 branch
BranchTrueFalseTotal
if $pref 25 3 28
414 28 ▶ delete $_[0]->{$_[1]}{_alias}{$actual} if exists $_[0]->{$_[1]}{_alias} && $_[0]->{$_[1]}{_alias}{$actual};
Branch: 1 branch
BranchTrueFalseTotal
if exists $_[0]{$_[1]}{"_alias"} and $_[0]{$_[1]}{"_alias"}{$actual} 0 28 28
Condition33%1/3
exists $_[0]{$_[1]}{"_alias"} and $_[0]{$_[1]}{"_alias"}{$actual}
!ll&&!rl&&r
2800
MC/DC0%0/2
exists $_[0]{$_[1]}{"_alias"} and $_[0]{$_[1]}{"_alias"}{$actual}
exists $_[0]{$_[1]}{"_alias"} $_[0]{$_[1]}{"_alias"}{$actual}
Truth table33%1/3
A and B
A= exists $_[0]{$_[1]}{"_alias"}B= $_[0]{$_[1]}{"_alias"}{$actual}
ABresult
0X0
100
111
415 0 ▶ map { $_[0]->{$_[1]}{_alias}{$_}=$actual unless exists $_[0]->{$_[1]}{$_} } @types;
Branch: 1 branch
BranchTrueFalseTotal
unless exists $_[0]{$_[1]}{$_} 0 0 0
416  
417 28 ▶ $_[0]->{$_[1]}{'_attr'}{$actual}=$_[2] if $_[2];
Branch: 1 branch
BranchTrueFalseTotal
if $_[2] 28 0 28
418  
419 }
420  
421 =item load_singleBinary
422  
423 Not done as I don't have example data yet.
424  
425 =cut
426  
427 0 ▶sub load_singleBinary {
Subroutine load_singleBinary: not called CC=1 SCAR=6.9
Pod documented
428 0 die "_singleBinary not done\n";
429 }
430  
431  
432 # sub setDefault {
433 # $_[0]->{"_setDefault"}=$_[1] if exists $_[1];
434 # return $_[0]->{"_setDefault"} if exists $_[0]->{"_setDefault"};
435 # die ref($_[0]) . " does not have a default set to iterate\n";
436 # }
437  
438 =back
439  
440 =head1 SUPPORT
441  
442 For technical support please email to jlawrenc@cpan.org ...
443 for faster service please include "Net::vFile" and "help" in your subject line.
444  
445 =head1 AUTHOR
446  
447 Jay J. Lawrence - jlawrenc@cpan.org
448 Infonium Inc., Canada
449 http://www.infonium.ca/
450  
451 =head1 COPYRIGHT
452  
453 Copyright (c) 2003 Jay J. Lawrence, Infonium Inc. All rights reserved.
454 This program is free software; you can redistribute
455 it and/or modify it under the same terms as Perl itself.
456  
457 The full text of the license can be found in the
458 LICENSE file included with this module.
459  
460 =head1 ACKNOWLEDGEMENTS
461  
462 Net::iCal - whose loading code inspired me for mine
463  
464 =head1 SEE ALSO
465  
466 RFC 2426, Net::iCal
467  
468 =cut
469  
470 5 1;
471  
« vCard.pm ↑ Summary vCard/ADR.pm »