blib/lib/XML/Generator/vCard/RDF.pm

filter: Statementstmt 83.3% 350 / 420 Branchbran 49.0% 51 / 104 Conditioncond 38.8% 7 / 18 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 91.3% 53 / 58 Podpod 85.7% 6 / 7 totaltotal 75.5% 467 / 618 CC 72 SCAR 20.6
CC
72
Cov
75%
_render_n
40.5
_renderlist_mediaitems
23.1
_renderlist
10.6
CRAP 7.8
↑ Summary
1 # $Id: RDF.pm,v 1.24 2004/12/28 21:50:27 asc Exp $
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 package XML::Generator::vCard::RDF;
5 2 ▶use base qw (XML::Generator::vCard::Base);
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 $XML::Generator::vCard::RDF::VERSION = '1.4';
8  
9 =head1 NAME
10  
11 XML::Generator::vCard::RDF - generate RDF/XML SAX2 events for vCard 3.0
12  
13 =head1 SYNOPSIS
14  
15 use XML::SAX::Writer;
16 use XML::Generator::vCard::RDF;
17  
18 my $writer = XML::SAX::Writer->new();
19 my $driver = XML::Generator::vCard::RDF->new(Handler=>$writer);
20  
21 $driver->parse_files("test.vcf");
22  
23 =head1 DESCRIPTION
24  
25 Generate RDF/XML SAX2 events for vCard 3.0
26  
27 =head1 DOCUMENT FORMAT
28  
29 SAX2 events map to the I<Representing vCard Objects in RDF/XML>
30 W3C note:
31  
32 http://www.w3.org/TR/2001/NOTE-vcard-rdf-20010222/
33  
34 Additionally, an extra description will be added for each unique
35 email address. Each description will be identified by the value of
36 SHA1 digest of the address and simply point back to the vCard
37 description.
38  
39 For example, the test file for this package contains the email
40 address I<senzala@example.com> which will cause the following
41 description to be added to the final output :
42  
43 <rdf:RDF>
44 <rdf:Description rdf:about = 't/Senzala.vcf'>
45 <!-- vcard data here -->
46 </rdf:Description>
47  
48 <!-- c0e0c54660f33a3ec7f22f902d0e5ead8bd4e4f4 == SHA1(senzala@example.com) -->
49  
50 <rdf:Description rdf:about='http://xmlns.com/foaf/0.1/mbox_sha1sum#c0e0c54660f33a3ec7f22f902d0e5ead8bd4e4f4'>
51 <rdfs:seeAlso rdf:resource='t/Senzala.vcf' /></rdf:Description>
52 </rdf:Description>
53 </rdf:RDF>
54  
55 This is done to facilitate merging vCard data with RDF representations
56 of email messages, using XML::Generator::RFC822::RDF. For example :
57  
58 <rdf:RDF>
59  
60 <rdf:Description rdf:about='x-urn:ietf:params:rfc822#5b0c8c9f9b2b782375f515a0b24b3a821a59a34a'>
61 <rfc822:To rdf:resource='http://xmlns.com/foaf/0.1/mbox_sha1sum#c0e0c54660f33a3ec7f22f902d0e5ead8bd4e4f4' />
62 <!-- ... -->
63 </rdf:Description>
64  
65 <rdf:Description rdf:about='http://xmlns.com/foaf/0.1/mbox_sha1sum#c0e0c54660f33a3ec7f22f902d0e5ead8bd4e4f4'>
66 <vCard:FN>Senzala Restaurant</vCard:FN>
67 <vCard:EMAIL>senzala@example.com</vCard:EMAIL>
68 </rdf:Description>
69  
70 </rdf:RDF>
71  
72 =cut
73  
74 2 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
75 2 ▶use MIME::Base64;
Subroutine BEGIN: called CC=1 SCAR=0.0
76 2 ▶use Text::vCard::Addressbook;
Subroutine BEGIN: called CC=1 SCAR=0.0
77 2 ▶use Memoize;
Subroutine BEGIN: called CC=1 SCAR=0.0
78 2 ▶use Digest::SHA1 qw (sha1_hex);
Subroutine BEGIN: called CC=1 SCAR=0.0
79  
80 2 ▶sub import {
Subroutine import: called CC=1 SCAR=0.0
81 2 my $pkg = shift;
82 2 $pkg->SUPER::import(@_);
83  
84 2 &memoize("_prepare_mbox");
85 2 return 1;
86 }
87  
88 =head1 PACKAGE METHODS
89  
90 =cut
91  
92 =head2 __PACKAGE__->new(%args)
93  
94 This method inherits from I<XML::SAX::Base>
95  
96 =cut
97  
98 1 ▶sub new {
Subroutine new: called CC=2 SCAR=7.1
Pod documented
99 1 my $pkg = shift;
100 1 my $self = $pkg->SUPER::new(@_);
101  
102 1 ▶ if (! $self) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self) 0 1 1
103 0 return undef;
104 }
105  
106 1 $self->{'__uri'} = "#";
107 1 $self->{'__current'} = 0;
108  
109 1 $self->{'__files'} = [];
110 1 $self->{'__mboxes'} = {};
111  
112 1 return bless $self, $pkg;
113 }
114  
115 =head1 OBJECT METHODS
116  
117 =cut
118  
119 =head1 OBJECT METHODS
120  
121 =cut
122  
123 3 ▶sub base {
Subroutine base: called CC=3 SCAR=11.0
Pod undocumented
124 3 my $self = shift;
125 3 my $uri = shift;
126  
127 3 ▶ if ($uri) {
Branch: 1 branch
BranchTrueFalseTotal
if ($uri) 1 2 3
128 1 $self->{'__uri'} = $self->prepare_uri($uri);
129 }
130  
131 3 ▶ return ($self->{'__uri'} || "#");
Condition50%1/2
$self->{'__uri'} || '#'
l!l
30
MC/DC0%0/1
$self->{'__uri'} || '#'
$self->{'__uri'}
Truth table50%1/2
A
A= $self->{'__uri'}
Aresult
11
00
132 }
133  
134 =head2 $pkg->parse_files(@files)
135  
136 Generate SAX2 events for one, or more, vCard files.
137  
138 Returns true or false.
139  
140 =cut
141  
142 =head2 $pkg->parse_files(@files)
143  
144 =cut
145  
146 1 ▶sub parse_files {
Subroutine parse_files: called CC=2 SCAR=7.2
Pod documented
147 1 my $self = shift;
148 1 my @files = @_;
149  
150 1 my $book = undef;
151  
152 1 eval {
153 1 $book = Text::vCard::Addressbook->load(\@files);
154 };
155  
156 1 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 1 1
157 0 warn $@;
158 0 return 0;
159 }
160  
161 1 $self->{'__files'} = \@files;
162 1 $self->{'__current'} = 0;
163  
164 1 return $self->_render_doc([ $book->vcards() ]);
165 }
166  
167 =head1 PRIVATE METHODS
168  
169 Private methods are documented below in case you need to subclass
170 this package to tweak its output.
171  
172 =cut
173  
174 =head2 $obj->_render_doc(\@vcards)
175  
176 =cut
177  
178 1 ▶sub _render_doc {
Subroutine _render_doc: called CC=2 SCAR=6.9
179 1 my $self = shift;
180 1 my $cards = shift;
181  
182 1 $self->start_document();
183  
184 1 $self->start_element({Name => "rdf:RDF"});
185
186 1 foreach my $vcard (@$cards) {
187  
188 1 $self->base($self->{'__files'}->[$self->{'__current'} ++]);
189 1 $self->_render_card($vcard);
190 }
191  
192 # Now render rdf:Description blocks for all
193 # the email addresses we've collected that
194 # point back to the current document using
195 # rdf:seeAlso
196  
197 1 $self->_render_foaf_mboxes();
198
199 1 $self->end_element({Name => "rdf:RDF"});
200
201 1 $self->end_document();
202 1 return 1;
203 }
204  
205 =head2 $obj->_render_card(Text::vCard)
206  
207 =cut
208  
209 1 ▶sub _render_card {
Subroutine _render_card: called CC=4 SCAR=13.9
210 1 my $self = shift;
211 1 my $vcard = shift;
212  
213 1 $self->start_element({Name => "rdf:Description",
214 Attributes => {"{}about" => {Name => "rdf:about",
215 Value => $self->base()}}});
216
217 #
218  
219 1 ▶ $self->_pcdata({Name => "vCard:CLASS",
Condition50%1/2
$vcard->class || 'PUBLIC'
l!l
01
MC/DC0%0/1
$vcard->class || 'PUBLIC'
$vcard->class
Truth table50%1/2
A
A= $vcard->class
Aresult
11
00
220 Value => ($vcard->class() || "PUBLIC")});
221  
222 1 foreach my $prop ("uid", "rev", "prodid") {
223  
224 3 ▶ if (my $value = $vcard->$prop()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $value = $vcard->$prop) 0 3 3
225 0 $self->_pcdata({Name => sprintf("vCard:%s",uc($prop)),
226 Value => $value});
227 }
228 }
229  
230 #
231  
232 1 $self->_render_fn($vcard);
233 1 $self->_render_n($vcard);
234 1 $self->_render_nickname($vcard);
235 1 $self->_render_photo($vcard);
236 1 $self->_render_bday($vcard);
237 1 $self->_render_adrs($vcard);
238 1 $self->_render_labels($vcard);
239 1 $self->_render_tels($vcard);
240 1 $self->_render_emails($vcard);
241 1 $self->_render_instantmessaging($vcard);
242 1 $self->_render_mailer($vcard);
243 1 $self->_render_tz($vcard);
244 1 $self->_render_geo($vcard);
245 1 $self->_render_org($vcard);
246 1 $self->_render_title($vcard);
247 1 $self->_render_role($vcard);
248 1 $self->_render_logo($vcard);
249 # AGENT
250 1 $self->_render_categories($vcard);
251 1 $self->_render_note($vcard);
252 # SORT
253 1 $self->_render_sound($vcard);
254 1 $self->_render_url($vcard);
255 1 $self->_render_key($vcard);
256 1 $self->_render_custom($vcard);
257  
258 1 $self->end_element({Name=>"rdf:Description"});
259  
260 1 return 1;
261 }
262  
263 =head2 $obj->_render_fn(Text::vCard)
264  
265 =cut
266  
267 1 ▶sub _render_fn {
Subroutine _render_fn: called CC=1 SCAR=0.0
268 1 my $self = shift;
269 1 my $vcard = shift;
270  
271 1 $self->_pcdata({Name => "vCard:FN",
272 Value => $vcard->fn()});
273
274 1 return 1;
275 }
276  
277 =head2 $obj->_render_n(Text::vCard)
278  
279 =cut
280  
281 1 ▶sub _render_n {
Subroutine _render_n: called CC=9 SCAR=37.0
282 1 my $self = shift;
283 1 my $vcard = shift;
284  
285 1 my $n = $vcard->get({"node_type" => "name"});
286  
287 1 ▶ if (! $n) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($n) 0 1 1
288 0 return 1;
289 }
290  
291 1 $n = $n->[0];
292  
293 #
294  
295 1 ▶ if (($n->family()) || ($n->given())) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n->family or $n->given) 0 1 1
Condition33%1/3
$n->family or $n->given
l!l&&r!l&&!r
001
MC/DC0%0/2
$n->family or $n->given
$n->family $n->given
Truth table33%1/3
A or B
A= $n->familyB= $n->given
ABresult
1X1
011
000
296  
297 0 $self->start_element({Name => "vCard:N",
298 Attributes => {"{}parseType"=>{Name => "rdf:parseType",
299 Value => "Resource"}},});
300
301 0 ▶ if (my $f = $n->family()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $f = $n->family) 0 0 0
302 0 $self->_pcdata({Name => "vCard:Family",
303 Value => $n->family()});
304 }
305  
306 0 ▶ if (my $g = $n->given()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $g = $n->given) 0 0 0
307 0 $self->_pcdata({Name => "vCard:Given",
308 Value => $n->given()});
309 }
310
311 0 ▶ if (my $o = $n->middle()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $o = $n->middle) 0 0 0
312 0 $self->_pcdata({Name => "vCard:Other",
313 Value => $o});
314 }
315
316 0 ▶ if (my $p = $n->prefixes()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $p = $n->prefixes) 0 0 0
317 0 $self->_pcdata({Name => "vCard:Prefix",
318 Value => $p});
319 }
320
321 0 ▶ if (my $s = $n->suffixes()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $s = $n->suffixes) 0 0 0
322 0 $self->_pcdata({Name => "vCard:Suffix",
323 Value => $s});
324 }
325
326 0 $self->end_element({Name => "vCard:N"});
327 }
328
329 1 return 1;
330 }
331  
332 =head2 $obj->_render_nickname(Text::vCard)
333  
334 =cut
335  
336 1 ▶sub _render_nickname {
Subroutine _render_nickname: called CC=2 SCAR=7.4
337 1 my $self = shift;
338 1 my $vcard = shift;
339  
340 1 ▶ if (my $nick = $vcard->nickname()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $nick = $vcard->nickname) 0 1 1
341 0 $self->_pcdata({Name => "vCard:NICKNAME",
342 Value => $nick});
343 }
344  
345 1 return 1;
346 }
347  
348 =head2 $obj->_render_photo(Text::vCard)
349  
350 =cut
351  
352 1 ▶sub _render_photo {
Subroutine _render_photo: called CC=1 SCAR=0.0
353 1 my $self = shift;
354 1 my $vcard = shift;
355  
356 1 my $photos = $vcard->get({"node_type" => "photo"});
357  
358 1 $self->_renderlist_mediaitems("vCard:PHOTO",
359 $photos);
360 1 return 1;
361 }
362  
363  
364 =head2 $obj->_render_bday(Text::vCard)
365  
366 =cut
367  
368 1 ▶sub _render_bday {
Subroutine _render_bday: called CC=2 SCAR=7.4
369 1 my $self = shift;
370 1 my $vcard = shift;
371  
372 1 ▶ if (my $bday = $vcard->bday()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $bday = $vcard->bday) 0 1 1
373 0 $self->_pcdata({Name => "vCard:BDAY",
374 Value => $bday});
375 }
376  
377 1 return 1;
378 }
379  
380 =head2 $obj->_render_adrs(Text::vCard)
381  
382 =cut
383  
384 1 ▶sub _render_adrs {
Subroutine _render_adrs: called CC=1 SCAR=0.2
385 1 my $self = shift;
386 1 my $vcard = shift;
387  
388 1 my $addresses = $vcard->get({"node_type" => "addresses"});
389  
390 #
391  
392 $self->_renderlist("vCard:ADR",
393 $addresses,
394 1 ▶ sub {
Subroutine __ANON__: called CC=8 SCAR=23.4
395 1 my $self = shift;
396 1 my $adr = shift;
397
398 1 ▶ if (my $p = $adr->po_box()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $p = $adr->po_box) 0 1 1
399 0 $self->_pcdata({Name => "vCard:pobox",
400 Value => $p});
401 }
402
403 1 ▶ if (my $e = $adr->extended()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $e = $adr->extended) 0 1 1
404 0 $self->_pcdata({Name => "vCard:extadr",
405 Value => $e});
406 }
407
408 1 ▶ if (my $s = $adr->street()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $s = $adr->street) 1 0 1
409 1 $self->_pcdata({Name => "vCard:Street",
410 Value => $s});
411 }
412
413 1 ▶ if (my $c = $adr->city()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $c = $adr->city) 1 0 1
414 1 $self->_pcdata({Name => "vCard:Locality",
415 Value => $c});
416 }
417
418 1 ▶ if (my $r = $adr->region()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $r = $adr->region) 1 0 1
419 1 $self->_pcdata({Name => "vCard:Region",
420 Value => $r});
421 }
422
423 1 ▶ if (my $p = $adr->post_code()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $p = $adr->post_code) 0 1 1
424 0 $self->_pcdata({Name => "vCard:Pcode",
425 Value => $p});
426 }
427
428 1 ▶ if (my $c = $adr->country()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $c = $adr->country) 1 0 1
429 1 $self->_pcdata({Name => "vCard:Country",
430 Value => $c});
431 }
432 1 });
433 1 return 1;
434 }
435  
436 =head2 $obj->_render_labels(Text::vCard)
437  
438 =cut
439  
440 1 ▶sub _render_labels {
Subroutine _render_labels: called CC=1 SCAR=0.5
441 1 my $self = shift;
442 1 my $vcard = shift;
443  
444 1 my $labels = $vcard->get({"node_type" => "labels"});
445  
446 #
447  
448 $self->_renderlist("vCard:LABEL",
449 $labels,
450 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
451 0 my $self = shift;
452 0 my $label = shift;
453  
454 0 $self->_pcdata({Name => "rdf:value",
455 Value => $label->value(),
456 Attributes => {$self->_parsetype("Literal")},
457 CDATA => 1,});
458 1 });
459 1 return 1;
460 }
461  
462 =head2 $obj->_render_tels(Text::vCard)
463  
464 =cut
465  
466 1 ▶sub _render_tels {
Subroutine _render_tels: called CC=1 SCAR=0.0
467 1 my $self = shift;
468 1 my $vcard = shift;
469  
470 1 my $tels = $vcard->get({'node_type' => 'tel'});
471  
472 $self->_renderlist("vCard:TEL",
473 $tels,
474 1 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
475 1 my $self = shift;
476 1 my $tel = shift;
477  
478 1 $self->_pcdata({Name => "rdf:value",
479 Value => $tel->value()});
480 1 });
481 1 return 1;
482 }
483  
484 =head2 $obj->_render_emails(Text::vCard)
485  
486 =cut
487  
488 1 ▶sub _render_emails {
Subroutine _render_emails: called CC=3 SCAR=11.0
489 1 my $self = shift;
490 1 my $vcard = shift;
491  
492 1 my $addresses = $vcard->get({"node_type" => "email"});
493  
494 $self->_renderlist("vCard:EMAIL",
495 $addresses,
496 1 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
497 1 my $self = shift;
498 1 my $email = shift;
499  
500 1 $self->_pcdata({Name => "rdf:value",
501 Value => $email->value()});
502 1 });
503  
504 # Keep track of email addresses for
505 # dumping by '_render_foaf_mboxes'
506  
507 1 my $base = $self->base();
508  
509 1 foreach my $email (@$addresses) {
510 1 my $mbox = &_prepare_mbox($email->value());
511
512 1 ▶ $self->{'__mboxes'}->{$mbox} ||= [];
Condition50%1/2
$self->{"__mboxes"}{$mbox} ||= []
l!l
01
MC/DC0%0/1
$self->{"__mboxes"}{$mbox} ||= []
$self->{"__mboxes"}{$mbox}
Truth table50%1/2
A
A= $self->{"__mboxes"}{$mbox}
Aresult
11
00
513 1 push @{$self->{'__mboxes'}->{$mbox}}, $base;
514 }
515  
516 1 return 1;
517 }
518  
519 =head2 $obj->_render_instantmessaging(Text::vCard)
520  
521 =cut
522  
523 1 ▶sub _render_instantmessaging {
Subroutine _render_instantmessaging: called CC=2 SCAR=6.9
524 1 my $self = shift;
525 1 my $vcard = shift;
526  
527 1 my $im_list = $self->_im_services();
528  
529 1 foreach my $service (sort {$a cmp $b} keys %$im_list) {
530  
531 5 my $addresses = $vcard->get({"node_type" => "x-$service"});
532
533 5 $self->_render_im_service($im_list->{$service},
534 $addresses);
535 }
536
537 1 return 1;
538 }
539  
540 5 ▶sub _render_im_service {
Subroutine _render_im_service: called CC=2 SCAR=9.2
541 5 my $self = shift;
542 5 my $service = shift;
543 5 my $accounts = shift;
544  
545 5 ▶ if (! $accounts) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($accounts) 5 0 5
546 5 return 1;
547 }
548  
549 $self->_renderlist($service,
550 $accounts,
551 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
552 0 my $self = shift;
553 0 my $im = shift;
554  
555 0 $self->_pcdata({Name => "rdf:value",
556 Value => $im->value()});
557 0 });
558  
559 0 return 1;
560 }
561  
562 =head2 $obj->_render_mailer(Text::vCard)
563  
564 =cut
565  
566 1 ▶sub _render_mailer {
Subroutine _render_mailer: called CC=2 SCAR=7.4
567 1 my $self = shift;
568 1 my $vcard = shift;
569  
570 1 ▶ if (my $m = $vcard->mailer()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $m = $vcard->mailer) 0 1 1
571 0 $self->_pcdata({Name => "vCard:MAILER",
572 Value => $m});
573 }
574  
575 1 return 1;
576 }
577  
578 =head2 $obj->_render_tz(Text::vCard)
579  
580 =cut
581  
582 1 ▶sub _render_tz {
Subroutine _render_tz: called CC=2 SCAR=7.4
583 1 my $self = shift;
584 1 my $vcard = shift;
585  
586 1 ▶ if (my $tz = $vcard->tz()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $tz = $vcard->tz) 0 1 1
587 0 $self->_pcdata({Name => "vCard:TZ",
588 Value => $tz});
589 }
590  
591 1 return 1;
592 }
593  
594 =head2 $obj->_render_geo(Text::vCard)
595  
596 =cut
597  
598 1 ▶sub _render_geo {
Subroutine _render_geo: called CC=2 SCAR=7.0
599 1 my $self = shift;
600 1 my $vcard = shift;
601  
602 1 my $geo = $vcard->get({'node_type' => "geo"});
603  
604 1 ▶ if (! $geo) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($geo) 0 1 1
605 0 return 1;
606 }
607  
608 1 $geo = $geo->[0];
609  
610 #
611  
612 1 $self->start_element({Name => "vCard:GEO",
613 Attributes => {"{}parseType"=>{Name => "rdf:parseType",
614 Value => "Resource"}},});
615
616 1 $self->_pcdata({Name => "geo:lat",
617 Value => $geo->lat()});
618  
619 1 $self->_pcdata({Name => "geo:lon",
620 Value => $geo->long()});
621
622 1 $self->end_element({Name=>"vCard:GEO"});
623  
624 1 return 1;
625 }
626  
627 =head2 $obj->_render_org(Text::vCard)
628  
629 =cut
630  
631 1 ▶sub _render_org {
Subroutine _render_org: called CC=8 SCAR=22.2
632 1 my $self = shift;
633 1 my $vcard = shift;
634  
635 1 my $orgs = $vcard->get({'node_type' => "org"});
636  
637 1 ▶ if (! $orgs) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($orgs) 0 1 1
638 0 return 1;
639 }
640  
641 1 my $org = $orgs->[0];
642  
643 1 ▶ if ((! $org->name()) && ((! $org->unit()))) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($org->name or $org->unit) 0 1 1
Condition33%1/3
$org->name or $org->unit
l!l&&r!l&&!r
100
MC/DC0%0/2
$org->name or $org->unit
$org->name $org->unit
Truth table33%1/3
A or B
A= $org->nameB= $org->unit
ABresult
1X1
011
000
644 0 return 1;
645 }
646  
647 1 my %parsetype = $self->_parsetype("Resource");
648  
649 1 $self->start_element({Name => "vCard:ORG",
650 Attributes => \%parsetype});
651
652 1 ▶ if (my $n = $org->name()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $n = $org->name) 1 0 1
653 1 $self->_pcdata({Name => "vCard:Orgnam",
654 Value => $n});
655 }
656  
657 1 ▶ if (my $u = $org->unit()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $u = $org->unit) 1 0 1
658  
659 1 my @units = grep { /\w/ } @$u;
660 1 my $count = scalar(@units);
661  
662 1 ▶ if ($count == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if ($count == 1) { } 0 1 1
elsif ($count) { } 1 0 1
663 0 $self->_pcdata({Name => "vCard:Orgunit",
664 Value => $units[0]});
665 }
666  
667 elsif ($count) {
668 1 $self->start_element({Name => "vCard:Orgunit"});
669 1 $self->start_element({Name => "rdf:Seq"});
670  
671 map {
672 1 $self->_pcdata({Name => "rdf:li",
673 Value => $_});
674 } @units;
675  
676 1 $self->end_element({Name => "rdf:Seq"});
677 1 $self->end_element({Name => "vCard:Orgunit"});
678 }
679  
680 else {}
681 }
682  
683 1 $self->end_element({Name=>"vCard:ORG"});
684 1 return 1;
685 }
686  
687 =head2 $obj->_render_title(Text::vCard)
688  
689 =cut
690  
691 1 ▶sub _render_title {
Subroutine _render_title: called CC=2 SCAR=7.4
692 1 my $self = shift;
693 1 my $vcard = shift;
694  
695 1 ▶ if (my $t = $vcard->title()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $t = $vcard->title) 0 1 1
696 0 $self->_pcdata({Name => "vCard:TITLE",
697 Value => $t});
698 }
699  
700 1 return 1;
701 }
702  
703 =head2 $obj->_render_role(Text::vCard)
704  
705 =cut
706  
707 1 ▶sub _render_role {
Subroutine _render_role: called CC=2 SCAR=7.4
708 1 my $self = shift;
709 1 my $vcard = shift;
710  
711 1 ▶ if (my $r = $vcard->role()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $r = $vcard->role) 0 1 1
712 0 $self->_pcdata({Name => "vCard:ROLE",
713 Value => $r});
714 }
715  
716 1 return 1;
717 }
718  
719 =head2 $obj->_render_logo(Text::vCard)
720  
721 =cut
722  
723 1 ▶sub _render_logo {
Subroutine _render_logo: called CC=1 SCAR=0.0
724 1 my $self = shift;
725 1 my $vcard = shift;
726  
727 1 my $logos = $vcard->get({"node_type" => "logo"});
728  
729 1 $self->_renderlist_mediaitems("vcard:LOGO",
730 $logos);
731  
732 1 return 1;
733 }
734  
735 =head2 $obj->_render_categories(Text::vCard)
736  
737 =cut
738  
739 1 ▶sub _render_categories {
Subroutine _render_categories: called CC=4 SCAR=14.4
740 1 my $self = shift;
741 1 my $vcard = shift;
742  
743 1 ▶ my $cats = $vcard->get({'node_type' => 'categories'}) ||
Condition33%1/3
$vcard->get({'node_type', 'categories'}) || $vcard->get({'node_type', 'category'})
l!l&&r!l&&!r
010
MC/DC0%0/2
$vcard->get({'node_type', 'categories'}) || $vcard->get({'node_type', 'category'})
$vcard->get({'node_type', 'categories'}) $vcard->get({'node_type', 'category'})
Truth table33%1/3
A || B
A= $vcard->get({'node_type', 'categories'})B= $vcard->get({'node_type', 'category'})
ABresult
1X1
011
000
744 $vcard->get({'node_type' => 'category'});
745  
746 1 ▶ if (! $cats) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($cats) 0 1 1
747 0 return 1;
748 }
749  
750 # we don't call '_renderlist' since it
751 # generates rdf:Bags and we need a 'Seq'
752 # here
753  
754 1 $self->start_element({Name => "vCard:CATEGORIES"});
755 1 $self->start_element({Name => "rdf:Seq"});
756  
757 1 foreach my $c (@$cats) {
758 1 $self->_pcdata({Name => "rdf:li",
759 Value => $c->value()});
760 }
761
762 1 $self->end_element({Name => "rdf:Seq"});
763 1 $self->end_element({Name => "vCard:CATEGORIES"});
764  
765 1 return 1;
766 }
767  
768 =head2 $obj->_render_note(Text::vCard)
769  
770 =cut
771  
772 1 ▶sub _render_note {
Subroutine _render_note: called CC=2 SCAR=7.1
773 1 my $self = shift;
774 1 my $vcard = shift;
775  
776 1 my $notes = $vcard->get({"node_type" => "note"});
777
778 1 ▶ if (! $notes) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($notes) 0 1 1
779 0 return 1;
780 }
781
782 1 $self->_pcdata({Name => "vCard:NOTE",
783 Attributes => {$self->_parsetype("Literal")},
784 CDATA => 1,
785 Value => $notes->[0]->value()});
786 1 return 1;
787 }
788  
789 =head2 $self->_render_sound(Text::vCard)
790  
791 =cut
792  
793 1 ▶sub _render_sound {
Subroutine _render_sound: called CC=1 SCAR=0.0
794 1 my $self = shift;
795 1 my $vcard = shift;
796  
797 1 my $snds = $vcard->get({'node_type' => 'sound'});
798  
799 1 $self->_renderlist_mediaitems("vCard:SOUND",
800 $snds);
801 1 return 1;
802 }
803  
804 =head2 $self->_render_url(Text::vCard)
805  
806 =cut
807  
808 1 ▶sub _render_url {
Subroutine _render_url: called CC=2 SCAR=7.4
809 1 my $self = shift;
810 1 my $vcard = shift;
811  
812 1 ▶ if (my $url = $vcard->url()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $url = $vcard->url) 0 1 1
813 0 $self->_pcdata({Name => "vCard:URL",
814 Attributes => {"{}resource" => {Name => "rdf:resource",
815 Value => $url}}});
816 }
817
818 1 return 1;
819 }
820  
821 =head2 $obj->_render_key(Text::vCard)
822  
823 =cut
824  
825 1 ▶sub _render_key {
Subroutine _render_key: called CC=1 SCAR=0.0
826 1 my $self = shift;
827 1 my $vcard = shift;
828  
829 1 my $keys = $vcard->get({'node_type' => 'key'});
830  
831 1 $self->_renderlist_mediaitems("vCard:KEY",
832 $keys);
833 1 return 1;
834 }
835  
836 =head2 $obj->_render_custom(Text::vCard)
837  
838 By default this method does nothing. It is here to
839 be subclassed.
840  
841 =cut
842  
843 1 ▶sub _render_custom { }
Subroutine _render_custom: called CC=1 SCAR=0.0
844  
845 =head2 $obj->_im_services()
846  
847 Returns a hash ref mapping an instant messaging service
848 type to an XML element. Default is :
849  
850 {"aim" => "foaf:aimChatID",
851 "yahoo" => "foaf:yahooChatID",
852 "msn" => "foaf:msnChatID",
853 "jabber" => "foaf:JabberID",
854 "icq" => "foaf:icqChatId"}
855  
856 This is called by the I<_render_instantmessaging> method.
857  
858 =cut
859  
860 1 ▶sub _im_services {
Subroutine _im_services: called CC=1 SCAR=0.0
861 1 return {"aim" => "foaf:aimChatID",
862 "yahoo" => "foaf:yahooChatID",
863 "msn" => "foaf:msnChatID",
864 "jabber" => "foaf:JabberID",
865 "icq" => "foaf:icqChatID"};
866 }
867  
868 16 ▶sub _pcdata {
Subroutine _pcdata: called CC=4 SCAR=13.9
869 16 my $self = shift;
870 16 my $data = shift;
871  
872 16 $self->start_element($data);
873  
874 16 ▶ if ($data->{CDATA}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data->{"CDATA"}) 1 15 16
875 1 $self->start_cdata();
876 }
877  
878 16 ▶ if ($data->{Value}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data->{"Value"}) 15 1 16
879 15 $self->characters({Data => encode_utf8($data->{Value})});
880 }
881  
882 16 ▶ if ($data->{CDATA}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data->{"CDATA"}) 1 15 16
883 1 $self->end_cdata();
884 }
885  
886 16 $self->end_element($data);
887 16 return 1;
888 }
889  
890 0 ▶sub _media {
Subroutine _media: not called CC=1 SCAR=6.9
891 0 my $self = shift;
892 0 my $obj = shift;
893  
894  
895 0 return 1;
896 }
897  
898 3 ▶sub _types {
Subroutine _types: called CC=3 SCAR=11.2
899 3 my $self = shift;
900
901 3 my $ns = $self->namespaces();
902  
903 3 ▶ foreach my $type (grep { defined($_) && $_ =~ m/\w/ } @_) {
Condition33%1/3
defined $_ && $_ =~ /\w/
!ll&&!rl&&r
007
MC/DC0%0/2
defined $_ && $_ =~ /\w/
defined $_ $_ =~ /\w/
Truth table33%1/3
A && B
A= defined $_B= $_ =~ /\w/
ABresult
0X0
100
111
904
905 $self->start_element({Name => "rdf:type",
906 Attributes => {"{}resource" => {Name => "rdf:resource",
907 7 Value => $ns->{vCard}.$type}}
908 });
909 7 $self->end_element({Name => "rdf:type"});
910 }
911  
912 3 return 1;
913 }
914  
915 6 ▶sub _parsetype {
Subroutine _parsetype: called CC=1 SCAR=0.0
916 6 my $self = shift;
917 6 my $resource = shift;
918  
919 6 return ("{}parseType" => {Name => "rdf:parseType",
920 Value => $resource});
921 }
922  
923 1 ▶sub start_document {
Subroutine start_document: called CC=2 SCAR=6.9
Pod documented
924 1 my $self = shift;
925  
926 1 $self->SUPER::start_document();
927  
928 1 $self->xml_decl({Version => "1.0",
929 Encoding => "UTF-8"});
930  
931 1 my $ns = $self->namespaces();
932  
933 1 foreach my $prefix (keys %$ns) {
934 $self->start_prefix_mapping({Prefix => $prefix,
935 5 NamespaceURI => $ns->{$prefix}});
936 }
937
938 1 return 1;
939 }
940  
941 1 ▶sub end_document {
Subroutine end_document: called CC=2 SCAR=6.9
Pod documented
942 1 my $self = shift;
943  
944 1 foreach my $prefix (keys %{$self->namespaces()}) {
945 5 $self->end_prefix_mapping({Prefix => $prefix});
946 }
947  
948 1 $self->SUPER::end_document();
949 1 return 1;
950 }
951  
952 36 ▶sub start_element {
Subroutine start_element: called CC=1 SCAR=0.0
Pod documented
953 36 my $self = shift;
954 36 my $data = shift;
955  
956 36 my $name = $self->prepare_qname($data->{Name});
957 36 my $attrs = $self->prepare_attrs($data->{Attributes});
958  
959 36 $self->SUPER::start_element({ %$name, %$attrs });
960 }
961  
962 36 ▶sub end_element {
Subroutine end_element: called CC=1 SCAR=0.0
Pod documented
963 36 my $self = shift;
964 36 my $data = shift;
965  
966 36 my $name = $self->prepare_qname($data->{Name});
967  
968 36 $self->SUPER::end_element($name);
969 }
970  
971 4 ▶sub _renderlist {
Subroutine _renderlist: called CC=9 SCAR=23.6
972 4 my $self = shift;
973 4 my $el = shift;
974 4 my $list = shift;
975 4 my $sub = shift;
976  
977 4 ▶ if (! $list) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($list) 1 3 4
978 1 return 1;
979 }
980  
981 3 ▶ my $bag = (scalar(@$list) > 1) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
scalar @$list > 1 ? : 0 3 3
982  
983 #
984  
985 3 my %parsetype = $self->_parsetype("Resource");
986 3 ▶ my %attrs = ($bag) ? (): %parsetype;
Branch: 1 branch
BranchTrueFalseTotal
$bag ? : 0 3 3
987  
988 3 $self->start_element({Name => $el,
989 Attributes => \%attrs});
990
991 3 ▶ if ($bag) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bag) 0 3 3
992 0 $self->start_element({Name => "rdf:Bag"});
993 }
994  
995 3 foreach my $obj (@$list) {
996  
997 3 ▶ if ($bag) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bag) 0 3 3
998 0 $self->start_element({Name => "rdf:li",
999 Attributes => {%parsetype}});
1000 }
1001  
1002 3 $self->_types($obj->types());
1003  
1004 3 &$sub($self,$obj);
1005  
1006 3 ▶ if ($bag) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bag) 0 3 3
1007 0 $self->end_element({Name=>"rdf:li"});
1008 }
1009 }
1010  
1011 3 ▶ if ($bag) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bag) 0 3 3
1012 0 $self->end_element({Name => "rdf:Bag"});
1013 }
1014  
1015 3 $self->end_element({Name => $el});
1016 3 return 1;
1017 }
1018  
1019 4 ▶sub _renderlist_mediaitems {
Subroutine _renderlist_mediaitems: called CC=9 SCAR=31.4
1020 4 my $self = shift;
1021 4 my $el = shift;
1022 4 my $list = shift;
1023  
1024 4 ▶ if (! $list) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($list) 3 1 4
1025 3 return 1;
1026 }
1027  
1028 1 ▶ my $bag = (scalar(@$list) > 1) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
scalar @$list > 1 ? : 0 1 1
1029  
1030 #
1031  
1032 1 my %parsetype = $self->_parsetype("Resource");
1033 1 ▶ my %attrs = ($bag) ? (): %parsetype;
Branch: 1 branch
BranchTrueFalseTotal
$bag ? : 0 1 1
1034  
1035 # aside from the normal hoop jumping
1036 # involved in bags/single items we
1037 # also need to contend with whether an
1038 # item has data or is simply a reference
1039 # to another resource
1040  
1041 1 ▶ if (! $bag) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($bag) 1 0 1
1042  
1043 1 my $obj = $list->[0];
1044  
1045 1 ▶ if (! $obj->is_type("base64")) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $obj->is_type('base64')) { } 1 0 1
1046 1 $self->_mediaref($el,$obj);
1047 }
1048  
1049 else {
1050 0 $self->start_element({Name => $el,
1051 Attributes => {$self->_parsetype("Resource")}});
1052 0 $self->_mediaobj($obj);
1053 0 $self->end_element({Name => $el});
1054 }
1055  
1056 1 return 1;
1057 }
1058  
1059 # bag
1060  
1061 0 $self->start_element({Name => $el,
1062 Attributes => \%attrs});
1063
1064 0 $self->start_element({Name => "rdf:Bag"});
1065
1066 0 foreach my $obj (@$list) {
1067
1068 0 ▶ if (! $obj->is_type("base64")) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $obj->is_type('base64')) { } 0 0 0
1069 0 %attrs = ("{}resource" => {Name => "rdf:resource",
1070 Value => $obj->value()});
1071 }
1072
1073 else {
1074 0 %attrs = %parsetype;
1075 }
1076  
1077 #
1078
1079 0 $self->start_element({Name => "rdf:li",
1080 Attributes => \%attrs});
1081
1082 0 ▶ if ($obj->is_type("base64")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($obj->is_type("base64")) 0 0 0
1083 0 $self->_mediaobj($obj);
1084 }
1085
1086 0 $self->end_element({Name => "rdf:li"});
1087 }
1088  
1089 #
1090  
1091 0 $self->end_element({Name => "rdf:Bag"});
1092 0 $self->end_element({Name => $el});
1093  
1094 0 return 1;
1095 }
1096  
1097 1 ▶sub _mediaref {
Subroutine _mediaref: called CC=1 SCAR=0.0
1098 1 my $self = shift;
1099 1 my $el = shift;
1100 1 my $obj = shift;
1101  
1102 1 $self->_pcdata({Name => $el,
1103 Attributes => {"{}resource" => {Name => "rdf:resource",
1104 Value => $obj->value()}}});
1105 }
1106  
1107 0 ▶sub _mediaobj {
Subroutine _mediaobj: not called CC=1 SCAR=6.9
1108 0 my $self = shift;
1109 0 my $obj = shift;
1110  
1111 0 $self->_types($obj->types());
1112  
1113 0 $self->_pcdata({Name => "vCard:ENCODING",
1114 Value => "b"});
1115  
1116 0 $self->_pcdata({Name => "rdf:value",
1117 Attributes => {$self->_parsetype("Literal")},
1118 Value => encode_base64($obj->value()),
1119 CDATA => 1});
1120  
1121 0 return 1;
1122 }
1123  
1124 # memoized
1125  
1126 1 ▶sub _prepare_mbox {
Subroutine _prepare_mbox: called CC=1 SCAR=0.0
1127 1 my $email_addr = shift;
1128 return encode_utf8(sprintf("%smbox_sha1sum#%s",
1129 __PACKAGE__->namespaces()->{foaf},
1130 1 sha1_hex($email_addr)));
1131 }
1132  
1133 1 ▶sub _render_foaf_mboxes {
Subroutine _render_foaf_mboxes: called CC=3 SCAR=11.0
1134 1 my $self = shift;
1135  
1136 1 foreach my $mbox (keys %{$self->{'__mboxes'}}) {
1137  
1138 1 $self->start_element({Name => "rdf:Description",
1139 Attributes => {"{}rdf:about" => {Name => "rdf:about",
1140 Value => $mbox}}});
1141 1 foreach my $uri (@{$self->{'__mboxes'}->{$mbox}}) {
1142  
1143 1 $self->start_element({Name => "rdfs:seeAlso",
1144 Attributes => {"{}rdf:resource" => {Name => "rdf:resource",
1145 Value => $uri}}});
1146 1 $self->end_element({Name => "rdfs:seeAlso"});
1147 }
1148  
1149 1 $self->end_element({Name => "rdf:Description"});
1150 }
1151  
1152 1 return 1;
1153 }
1154  
1155 0 ▶sub DESTROY {}
Subroutine DESTROY: not called CC=1 SCAR=0.0
1156  
1157 =head1 NAMESPACES
1158  
1159 This package generates SAX events using the following XML
1160 namespaces :
1161  
1162 =over 4
1163  
1164 =item * B<vCard>
1165  
1166 http://www.w3.org/2001/vcard-rdf/3.0#
1167  
1168 =item * B<rdf>
1169  
1170 http://www.w3.org/1999/02/22-rdf-syntax-ns#
1171  
1172 =item * B<foaf:>
1173  
1174 http://xmlns.com/foaf/0.1/
1175  
1176 =item * B<geo>
1177  
1178 http://www.w3.org/2003/01/geo/wgs84_pos#
1179  
1180 =back
1181  
1182 =cut
1183  
1184 =head1 VERSION
1185  
1186 1.4
1187  
1188 =head1 DATE
1189  
1190 $Date: 2004/12/28 21:50:27 $
1191  
1192 =head1 AUTHOR
1193  
1194 Aaron Straup Cope E<lt>ascope@cpan.orgE<gt>
1195  
1196 =head1 SEE ALSO
1197  
1198 L<Text::vCard>
1199  
1200 L<XML::Generator::vCard>
1201  
1202 L<XML::Generator::RFC822::RDF>
1203  
1204 =head1 BUGS
1205  
1206 vCards containg binary PHOTO images may cause Perl to segfault on
1207 Mac OSX and come flavours of Linux (but not FreeBSD.) The source of
1208 this problem has been traced, I think, to a regular expression issue
1209 in the Perl Text::ParseWords library. A bug report has been filed.
1210  
1211 Please report all other bugs via http://rt.cpan.org
1212  
1213 =head1 LICENSE
1214  
1215 Copyright (c) 2004, Aaron Straup Cope. All Rights Reserved.
1216  
1217 This is free software, you may use it and distribute it
1218 under the same terms as Perl itself.
1219  
1220 =cut
1221  
1222 2 return 1;
↑ Summary