vFile/Base.pm

filter: Statementstmt 51.9% 53 / 102 Branchbran 12.5% 5 / 40 Conditioncond 4.1% 2 / 48 MC/DCmcdc 0.0% 0 / 23 Subroutinesub 72.2% 13 / 18 Podpod 91.6% 11 / 12 totaltotal 34.5% 84 / 243 CC 35 SCAR 37.1
CC
35
Cov
32%
load_singleBinary
90.0
load_singleDate
72.0
load_singleDuration
72.0
CRAP 40.9
« vFile.pm ↑ Summary
1 package Text::vFile::Base;
2  
3 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 =head1 NAME
6  
7 Text::vFile::Base - Base class for most of the functional classes based on the vCard/vCalendar etc spec.
8 Most of the hard work in breaking apart lines of data happens using methods in here.
9  
10 =head1 SYNOPSIS
11  
12 package Text::vFoo;
13  
14 use Text::vFile::Base;
15 use vars qw(@ISA);
16 push @ISA, qw(Text::vCard::Base);
17  
18 # Tell vFile that BEGIN:VFOO line creates one of these objects
19 $Text::vFile::classMap{'VCARD'}=__PACKAGE__;
20
21 # Then you will need to create a varHandler - although there are defaults to try
22 # and get you going.
23 #
24 # As well you will need to define more exotic, type specific loaders
25 #
26 # And finally accessors are your responsibility
27  
28 =head1 END USAGE
29  
30 To tell your users how to use this module:
31  
32 use Text::vFoo;
33 my $loader = Text::vFoo->loader( source => "youppi.vfoo" );
34  
35 while (my $vfoo = $loader->next) {
36 $vfoo->all_night;
37 }
38  
39 # or even sexier
40
41 while (my $vfoo = <$loader> ) {
42 $vfoo->all_night;
43 }
44  
45 It may sound silly, but it should be mentioned. Just becase the user
46 says:
47  
48 my $loader = Text::vFoo->loader( );
49  
50 Does not mean there will be any complaints if they try to load non-vfoo data.
51 If the source has vcards in it - that's what they're going to get.
52  
53 =cut
54  
55  
56 5 ▶use Carp; $SIG{__DIE__} = \&Carp::confess;
Subroutine BEGIN: called CC=1 SCAR=0.0
57 5 ▶use Data::Dumper; $Data::Dumper::Indent=1; $Data::Dumper::Sortkeys=1;
Subroutine BEGIN: called CC=1 SCAR=0.0
58  
59 5 ▶use lib qw(lib);
Subroutine BEGIN: called CC=1 SCAR=0.0
60 5 ▶use Text::vFile;
Subroutine BEGIN: called CC=1 SCAR=0.0
61  
62 5 my $typeSequence=1;
63  
64 60 ▶sub _nextSequence {
Subroutine _nextSequence: called CC=1 SCAR=0.0
65 60 return $typeSequence++;
66 }
67  
68 =head1 CONVENIENCE METHODS
69  
70 =over 4
71  
72 =item @objects = $class->load( key => value )
73  
74 Calls the Text::vFile load routine. Should slurp all the objects
75 and return as an array/array ref.
76  
77 =cut
78  
79 1 ▶sub load {
Subroutine load: called CC=1 SCAR=0.0
Pod documented
80 1 shift;
81 1 Text::vFile->load(@_);
82 }
83  
84 =item $loader = $class->loader( key => value )
85  
86 Returns an object which can return back objects one at a time. Nice
87 if you want to iterate through a stack of vcards at your leisure.
88  
89 =cut
90  
91 4 ▶sub loader {
Subroutine loader: called CC=1 SCAR=0.0
Pod documented
92 4 shift;
93 4 return Text::vFile->new(@_);
94 }
95  
96 =item $object = class->new
97  
98 Make a new object object that represents this vFile data being parsed.
99  
100 =cut
101  
102 15 ▶sub new {
Subroutine new: called CC=3 SCAR=11.8
Pod documented
103  
104 15 ▶ my $class = ref($_[0]) ? ref(shift) : shift;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 15 15
105 15 ▶ my $opts = ref($_[0]) ? $_[0] : {@_};
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 15 15
106  
107 15 my $self = bless {}, $class;
108  
109 0 map { $self->$_( $opts->{$_} ) } keys %$opts;
110  
111 15 return $self;
112  
113 }
114  
115 =back
116  
117 =head1 DATA HANDLERS
118  
119 =over 4
120  
121 =item varHandler
122  
123 Returns a hash ref mapping the item label to a handler name. Ie:
124  
125 {
126 'FN' => 'singleText',
127 'NICKNAME' => 'multipleText',
128 'PHOTO' => 'singleBinary',
129 'BDAY' => 'singleText',
130 'ADR' => 'ADR', # Not really necessary
131 };
132  
133 By default if there is no explicit handler then the vFile loader will
134  
135 - is there a method called "load_NAME"?
136 - test does the item have type attributes or not
137 - yes? singleTextTyped
138 - no? singleText
139
140  
141 =cut
142
143 15 ▶sub varHandler {
Subroutine varHandler: called CC=1 SCAR=0.0
Pod documented
144 15 return {};
145 }
146  
147 =item typeDefault
148  
149 Additional information where handlers require type info. Such as ADR - is this
150 a home, postal, or whatever? If not supplied the RFC specifies what types they should
151 default to.
152  
153 from vCard:
154  
155 {
156 'ADR' => [ qw(intl postal parcel work) ],
157 'LABEL' => [ qw(intl postal parcel work) ],
158 'TEL' => [ qw(voice) ],
159 'EMAIL' => [ qw(internet) ],
160 };
161  
162 =cut
163  
164 60 ▶sub typeDefault {
Subroutine typeDefault: called CC=1 SCAR=0.0
Pod documented
165 60 return {};
166 }
167  
168 =item load_singleText
169  
170 Loads a single text item with no processing other than unescape text
171  
172 =cut
173  
174 15 ▶sub load_singleText {
Subroutine load_singleText: called CC=4 SCAR=16.6
Pod documented
175  
176 15 my $val=$_[3];
177 15 $val=~s/\\([\n,])/$1/gs;
178 # $val=~s/\\n/\n/gs;
179 15 $_[0]->{$_[1]}{'value'}=$val;
180 0 ▶ $_[0]->{$_[1]}{'attr'}=$_[2] if $_[2] && ref($_[2]) eq "HASH" && keys %{$_[2]};
Branch: 1 branch
BranchTrueFalseTotal
if $_[2] and ref $_[2] eq "HASH" and keys %{$_[2];} 0 15 15
Condition33%2/6
$_[2] and ref $_[2] eq "HASH" and keys %{$_[2];}
!ll&&!rl&&r
1500
$_[2] and ref $_[2] eq "HASH"
!ll&&!rl&&r
1500
MC/DC0%0/3
$_[2] and ref $_[2] eq "HASH" and keys %{$_[2];}
$_[2] ref $_[2] eq "HASH" keys %{$_[2];}
Truth table25%1/4
A and B and C
A= $_[2]B= ref $_[2] eq "HASH"C= keys %{$_[2];}
ABCresult
0XX0
10X0
1100
1111
181  
182 15 return $_[0]->{$_[1]};
183  
184 }
185  
186 =item load_singleDate
187  
188 Loads a date creating a DateTime::Format::ICal object. Thanks Dave!
189  
190 =cut
191  
192 0 ▶sub load_singleDate {
Subroutine load_singleDate: not called CC=8 SCAR=42.8
Pod documented
193  
194 0 my $val=$_[3];
195 0 ▶ unless (%DateTime::Format::ICal::) {
Branch: 1 branch
BranchTrueFalseTotal
unless (%DateTime::Format::ICal::) 0 0 0
196 0 eval "use DateTime::Format::ICal";
197 0 ▶ warn "Cannot create date/time objects: $@\n" and return if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
Condition0%0/5
$@ && (warn("Cannot create date/time objects: $@\n") && (return))
!ll&&!rl&&r
000
warn "Cannot create date/time objects: $@\n" and return
!ll
00
MC/DC0%0/2
$@ && (warn("Cannot create date/time objects: $@\n") && (return))
$@ warn "Cannot create date/time objects: $@\n"
Truth table0%0/3
A && B
A= $@B= warn "Cannot create date/time objects: $@\n"
ABresult
0X0
100
111
198 }
199  
200 0 eval {
201 0 $_[0]->{$_[1]}{'value'}=DateTime::Format::ICal->parse_datetime( iso8601 => $val );
202 0 ▶ }; if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
203 0 warn "$val; $@\n";
204 }
205  
206 0 ▶ $_[0]->{$_[1]}{'attr'}=$_[2] if $_[2] && ref($_[2]) eq "HASH" && keys %{$_[2]};
Branch: 1 branch
BranchTrueFalseTotal
if $_[2] and ref $_[2] eq "HASH" and keys %{$_[2];} 0 0 0
Condition0%0/6
$_[2] and ref $_[2] eq "HASH" and keys %{$_[2];}
!ll&&!rl&&r
000
$_[2] and ref $_[2] eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/3
$_[2] and ref $_[2] eq "HASH" and keys %{$_[2];}
$_[2] ref $_[2] eq "HASH" keys %{$_[2];}
Truth table0%0/4
A and B and C
A= $_[2]B= ref $_[2] eq "HASH"C= keys %{$_[2];}
ABCresult
0XX0
10X0
1100
1111
207  
208 0 return $_[0]->{$_[1]};
209  
210 }
211  
212 =item load_singleDuration
213  
214 Loads a data duration using DateTime::Format::ICal.
215  
216 =cut
217  
218 0 ▶sub load_singleDuration {
Subroutine load_singleDuration: not called CC=8 SCAR=42.8
Pod documented
219  
220 0 my $val=$_[3];
221  
222 0 ▶ unless (%DateTime::Format::ICal::) {
Branch: 1 branch
BranchTrueFalseTotal
unless (%DateTime::Format::ICal::) 0 0 0
223 0 eval "use DateTime::Format::ICal";
224 0 ▶ warn "Cannot create date/time objects: $@\n" and return if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
Condition0%0/5
$@ && (warn("Cannot create date/time objects: $@\n") && (return))
!ll&&!rl&&r
000
warn "Cannot create date/time objects: $@\n" and return
!ll
00
MC/DC0%0/2
$@ && (warn("Cannot create date/time objects: $@\n") && (return))
$@ warn "Cannot create date/time objects: $@\n"
Truth table0%0/3
A && B
A= $@B= warn "Cannot create date/time objects: $@\n"
ABresult
0X0
100
111
225 }
226  
227 0 eval {
228 0 $_[0]->{$_[1]}{'value'}=DateTime::Format::ICal->parse_duration( $val );
229 0 ▶ }; if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
230 0 warn "$val; $@\n";
231 }
232  
233 0 ▶ $_[0]->{$_[1]}{'attr'}=$_[2] if $_[2] && ref($_[2]) eq "HASH" && keys %{$_[2]};
Branch: 1 branch
BranchTrueFalseTotal
if $_[2] and ref $_[2] eq "HASH" and keys %{$_[2];} 0 0 0
Condition0%0/6
$_[2] and ref $_[2] eq "HASH" and keys %{$_[2];}
!ll&&!rl&&r
000
$_[2] and ref $_[2] eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/3
$_[2] and ref $_[2] eq "HASH" and keys %{$_[2];}
$_[2] ref $_[2] eq "HASH" keys %{$_[2];}
Truth table0%0/4
A and B and C
A= $_[2]B= ref $_[2] eq "HASH"C= keys %{$_[2];}
ABCresult
0XX0
10X0
1100
1111
234  
235 0 return $_[0]->{$_[1]};
236  
237 }
238  
239 =item load_multipleText
240  
241 This is text that is separated by commas. The text is then unescaped. An array
242 of items is created.
243  
244 =cut
245  
246 0 ▶sub load_multipleText {
Subroutine load_multipleText: not called CC=4 SCAR=30.0
Pod documented
247  
248 0 my @vals=split /(?<!\\),/, $_[3];
249 0 map { s/\\,/,/ } @vals;
250  
251 0 $_[0]->{$_[1]}{'value'}=\@vals;
252 0 ▶ $_[0]->{$_[1]}{'attr'}=$_[2] if $_[2] && ref($_[2]) eq "HASH" && keys %{$_[2]};
Branch: 1 branch
BranchTrueFalseTotal
if $_[2] and ref $_[2] eq "HASH" and keys %{$_[2];} 0 0 0
Condition0%0/6
$_[2] and ref $_[2] eq "HASH" and keys %{$_[2];}
!ll&&!rl&&r
000
$_[2] and ref $_[2] eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/3
$_[2] and ref $_[2] eq "HASH" and keys %{$_[2];}
$_[2] ref $_[2] eq "HASH" keys %{$_[2];}
Truth table0%0/4
A and B and C
A= $_[2]B= ref $_[2] eq "HASH"C= keys %{$_[2];}
ABCresult
0XX0
10X0
1100
1111
253  
254 0 return $_[0]->{$_[1]};
255  
256 }
257  
258 =item load_singleTextType
259  
260 Load text that has a type attribute. Each text of different type attributes
261 will be handled independantly in as a hash entry. If no type attribute is supplied
262 then the typeDefaults types will be used. A line can have multiple types. In the
263 case where multiple types have the same value "_alias" indicators are created.
264 The preferred type is stored in "_pref"
265  
266 =cut
267  
268 60 ▶sub load_singleTextTyped {
Subroutine load_singleTextTyped: called CC=3 SCAR=11.2
Pod undocumented
269
270 60 my $typeDefault=$_[0]->typeDefault;
271  
272 60 my $attr=$_[2];
273  
274 60 my %type=();
275 60 map { map { $type{lc $_}=1 } (split /,/, $_) } @{$attr->{'type'}};
276 # delete $attr->{'type'};
277 0 ▶ map { $type{ lc $_ }=1 } @{$typeDefault->{$_[1]}} unless scalar(keys %type);
Branch: 1 branch
BranchTrueFalseTotal
unless scalar keys %type 0 60 60
278  
279 60 my $item={};
280 60 push @{$_[0]->{$_[1]}}, $item;
281 60 $item->{'value'}=$_[3];
282 60 $item->{'type'}=\%type;
283 60 ▶ $item->{'attr'}=$attr if keys %$attr;
Branch: 1 branch
BranchTrueFalseTotal
if keys %$attr 60 0 60
284 60 $item->{'sequence'}=_nextSequence();
285  
286 60 return $item;
287  
288 }
289  
290 =item load_singleBinary
291  
292 Not done as I don't have example data yet.
293  
294 =cut
295  
296 0 ▶sub load_singleBinary {
Subroutine load_singleBinary: not called CC=9 SCAR=45.0
Pod documented
297 0 my ($self, $name, $attr, $value) = @_;
298  
299 0 ▶ my $encoding = $attr->{'encoding'} || $attr->{'ENCODING'};
Condition0%0/3
$attr->{'encoding'} || $attr->{'ENCODING'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$attr->{'encoding'} || $attr->{'ENCODING'}
$attr->{'encoding'} $attr->{'ENCODING'}
Truth table0%0/3
A || B
A= $attr->{'encoding'}B= $attr->{'ENCODING'}
ABresult
1X1
011
000
300  
301 # type=b means Base64; I don't know about others
302 0 ▶ if ($encoding) {
Branch: 1 branch
BranchTrueFalseTotal
if ($encoding) { } 0 0 0
303  
304 0 ▶ if (lc $encoding eq "b") {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $encoding eq 'b') { } 0 0 0
305 0 eval "use MIME::Base64";
306 0 ▶ warn "Cannot decode binary MIME encoded objects: $@\n" and return if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
Condition0%0/5
$@ && (warn("Cannot decode binary MIME encoded objects: $@\n") && (return))
!ll&&!rl&&r
000
warn "Cannot decode binary MIME encoded objects: $@\n" and return
!ll
00
MC/DC0%0/2
$@ && (warn("Cannot decode binary MIME encoded objects: $@\n") && (return))
$@ warn "Cannot decode binary MIME encoded objects: $@\n"
Truth table0%0/3
A && B
A= $@B= warn "Cannot decode binary MIME encoded objects: $@\n"
ABresult
0X0
100
111
307 0 $self->{$name}{'value'} = MIME::Base64::decode_base64($value);
308 } else {
309 0 warn "Unknown encoding $encoding for $name\n";
310 0 return undef;
311 }
312  
313 } else {
314
315 # This must be an URL
316  
317 }
318 0 ▶ $self->{$name}{'attr'}=$attr if $attr && ref($attr) eq "HASH" && keys %{$attr};
Branch: 1 branch
BranchTrueFalseTotal
if $attr and ref $attr eq "HASH" and keys %{$attr;} 0 0 0
Condition0%0/6
$attr and ref $attr eq "HASH" and keys %{$attr;}
!ll&&!rl&&r
000
$attr and ref $attr eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/3
$attr and ref $attr eq "HASH" and keys %{$attr;}
$attr ref $attr eq "HASH" keys %{$attr;}
Truth table0%0/4
A and B and C
A= $attrB= ref $attr eq "HASH"C= keys %{$attr;}
ABCresult
0XX0
10X0
1100
1111
319  
320 0 die "_singleBinary not done\n";
321 }
322  
323  
324 =item @split = $self->split_value($line [, $delimiter]);
325  
326 This method returns a array ref containing the $line elements
327 split by the delimiter, but ignores escaped delimiters.
328 If no $delimiter is supplied then a comma "," is used by default.
329  
330 =cut
331  
332 0 ▶sub split_value {
Subroutine split_value: not called CC=3 SCAR=24.8
Pod documented
333 0 my ($self, $line, $delim) = @_;
334  
335 0 ▶ $delim = ',' unless $delim;
Branch: 1 branch
BranchTrueFalseTotal
unless $delim 0 0 0
336  
337 0 my @list = split(/(?<!\\)$delim/,$line);
338  
339 0 ▶ return wantarray ? @list : \@list;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
340 }
341  
342 =back
343  
344 =head1 SUPPORT
345  
346 For technical support please email to jlawrenc@cpan.org ...
347 for faster service please include "Text::vFile" and "help" in your subject line.
348  
349 =head1 AUTHOR
350  
351 Jay J. Lawrence - jlawrenc@cpan.org
352 Infonium Inc., Canada
353 http://www.infonium.ca/
354  
355 =head1 COPYRIGHT
356  
357 Copyright (c) 2003 Jay J. Lawrence, Infonium Inc. All rights reserved.
358 This program is free software; you can redistribute
359 it and/or modify it under the same terms as Perl itself.
360  
361 The full text of the license can be found in the
362 LICENSE file included with this module.
363  
364 =head1 ACKNOWLEDGEMENTS
365  
366 =head1 SEE ALSO
367  
368 =cut
369  
370  
371 5 1;
372  
« vFile.pm ↑ Summary