File Coverage

blib/lib/XML/Generator/vCard/Base.pm
Criterion Covered Total %
statement 27 55 49.0
branch n/a
condition n/a
subroutine 9 17 52.9
pod 4 4 100.0
total 40 76 52.6


line stmt bran cond sub pod time code
1             # $Id: Base.pm,v 1.4 2004/12/28 21:49:53 asc Exp $
2 1     1   1689 use strict;
  1         3  
  1         74  
3              
4             package XML::Generator::vCard::Base;
5 1     1   6 use base qw (XML::SAX::Base);
  1         2  
  1         1485  
6              
7             $XML::Generator::vCard::Base::VERSION = '1.0';
8              
9             =head1 NAME
10              
11             XML::Generator::vCard::Base - base class for generating SAX2 events for vCard data
12              
13             =head1 SYNOPSIS
14              
15             # Ceci n'est pas une boite noire.
16            
17             package XML::Generator::vCard::FooBar;
18             use base qw (XML::Generator::vCard::Base);
19              
20             =head1 DESCRIPTION
21              
22             Base class for generating SAX2 events for vCard data
23              
24             =cut
25              
26 1     1   29557 use File::Spec;
  1         3  
  1         39  
27 1     1   1175 use URI::Escape;
  1         2236  
  1         103  
28 1     1   2537 use URI::Split;
  1         794  
  1         55  
29 1     1   1139 use Encode;
  1         15253  
  1         136  
30 1     1   1110 use Memoize;
  1         3359  
  1         164  
31              
32 1         768 use constant NS => {"vCard" => "http://www.w3.org/2001/vcard-rdf/3.0#",
33             "rdf" => "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
34             "rdfs" => "http://www.w3.org/2000/01/rdf-schema#",
35             "geo" => "http://www.w3.org/2003/01/geo/wgs84_pos#",
36 1     1   9 "foaf" => "http://xmlns.com/foaf/0.1/"};
  1         1  
37              
38             sub import {
39 1     1   11 my $pkg = shift;
40              
41 1         5 &memoize("_prepare_qname","prepare_uri","_prepare_path");
42 1         5095 return 1;
43             }
44              
45             =head1 PACKAGE METHODS
46              
47             =cut
48              
49             =head2 __PACKAGE__->prepare_uri($uri)
50              
51             Encodes (decoding first, where necessary) a URI's path as UTF-8.
52              
53             Returns a string.
54              
55             =cut
56              
57             # this is actually only used by ::RDF at the moment
58             # but it seems like a good candidate for inclusion
59             # here
60              
61             sub prepare_uri {
62 0     0 1   my $pkg = shift;
63 0           return &_prepare_uri(@_);
64             }
65              
66             # memoized
67              
68             sub _prepare_uri {
69 0     0     my $uri = shift;
70              
71 0           my ($scheme, $auth, $path, $query, $frag) = URI::Split::uri_split($uri);
72            
73 0           $path = File::Spec->catdir(map { &_prepare_path($_) } split("/",$path));
  0            
74 0           return URI::Split::uri_join($scheme, $auth, $path, $query, $frag);
75             }
76              
77             # memoized
78              
79             sub _prepare_path {
80 0     0     my $str = shift;
81            
82 0           $str =~ s/(?:%([a-fA-F0-9]{2})%([a-fA-F0-9]{2}))/pack("U0U*",hex($1),hex($2))/eg;
  0            
83 0           $str = decode_utf8($str);
84 0           return URI::Escape::uri_escape_utf8($str);
85             }
86              
87             =head2 __PACKAGE__->prepare_qname($qname)
88              
89             Utility method to return a hash reference suitable for passing
90             a XML QName to I.
91              
92             Returns a hash reference.
93              
94             =cut
95              
96             sub prepare_qname {
97 0     0 1   my $pkg = shift;
98 0           return &_prepare_qname(@_);
99             }
100              
101             # memoized
102              
103             sub _prepare_qname {
104             my $qname = shift;
105              
106             $qname =~ /^([^:]+):(.*)$/;
107              
108             my $prefix = $1;
109             my $name = $2;
110              
111             my $ns = NS->{ $prefix };
112            
113             return {Name => $qname,
114             LocalName => $name,
115             Prefix => $prefix,
116             NamespaceURI => $ns};
117             }
118              
119             =head2 __PACKAGE__->prepare_attrs(\%attrs)
120              
121             Utility method to return a hash reference suitable for passing
122             XML attributes to I.
123              
124             Returns a hash reference.
125              
126             =cut
127              
128             sub prepare_attrs {
129 0     0 1   my $pkg = shift;
130 0           my $attrs = shift;
131              
132 0           foreach my $uri (keys %$attrs) {
133            
134 0           my ($key, $data) = &_prepare_attr($attrs->{$uri});
135            
136 0           $attrs->{ $key } = $data;
137 0           delete $attrs->{$uri};
138             }
139              
140 0           return {Attributes => $attrs};
141             }
142              
143             sub _prepare_attr {
144 0     0     my $attr = shift;
145              
146 0           my $data = &_prepare_qname($attr->{Name});
147 0           $data->{Value} = $attr->{Value};
148              
149 0           my $fq_uri = sprintf("{%s}%s",
150             $data->{NamespaceURI},
151             $data->{LocalName});
152              
153 0           return ($fq_uri,$data);
154             }
155              
156             =head2 __PACKAGE__->namespaces()
157              
158             Returns a hash reference of commonly used prefixes
159             and namespace URIs.
160              
161             =cut
162              
163             sub namespaces {
164 0     0 1   return NS;
165             }
166              
167             # deprecated
168              
169             sub _namespaces {
170 0     0     return $_[0]->namespaces();
171             }
172              
173             =head1 VERSION
174              
175             1.0
176              
177             =head1 DATE
178              
179             $Date: 2004/12/28 21:49:53 $
180              
181             =head1 AUTHOR
182              
183             Aaron Straup Cope Eascope@cpan.orgE
184              
185             =head1 SEE ALSO
186              
187             L
188              
189             =head1 LICENSE
190              
191             Copyright (c) Aaron Straup Cope. All rights reserved.
192              
193             This is free software, you may use it and distribute it
194             under the same terms as Perl itself.
195              
196             =cut
197              
198             return 1;