| 1 | # $Id: Base.pm,v 1.4 2004/12/28 21:49:53 asc Exp $ | ||
| 2 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | |||
| 4 | package XML::Generator::vCard::Base; | ||
| 5 | 1 | ▶ | use base qw (XML::SAX::Base); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | |||
| 7 | 1 | $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 | ▶ | use File::Spec; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 27 | 1 | ▶ | use URI::Escape; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 28 | 1 | ▶ | use URI::Split; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 29 | 1 | ▶ | use Encode; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 30 | 1 | ▶ | use Memoize; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 31 | |||
| 32 | 1 | ▶ | use constant NS => {"vCard" => "http://www.w3.org/2001/vcard-rdf/3.0#", |
|
Subroutine
BEGIN: called CC=1 SCAR=0.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 | "foaf" => "http://xmlns.com/foaf/0.1/"}; | |
| 37 | |||
| 38 | 1 | ▶ | sub import { |
|
Subroutine
import: called CC=1 SCAR=0.0
| |||
| 39 | 1 | my $pkg = shift; | |
| 40 | |||
| 41 | 1 | &memoize("_prepare_qname","prepare_uri","_prepare_path"); | |
| 42 | 1 | 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 | 0 | ▶ | sub prepare_uri { |
|
Subroutine
prepare_uri: not called CC=1 SCAR=6.9
Pod
documented
| |||
| 62 | 0 | my $pkg = shift; | |
| 63 | 0 | return &_prepare_uri(@_); | |
| 64 | } | ||
| 65 | |||
| 66 | # memoized | ||
| 67 | |||
| 68 | 0 | ▶ | sub _prepare_uri { |
|
Subroutine
_prepare_uri: not called CC=1 SCAR=6.9
| |||
| 69 | 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)); | |
| 74 | 0 | return URI::Split::uri_join($scheme, $auth, $path, $query, $frag); | |
| 75 | } | ||
| 76 | |||
| 77 | # memoized | ||
| 78 | |||
| 79 | 0 | ▶ | sub _prepare_path { |
|
Subroutine
_prepare_path: not called CC=1 SCAR=6.9
| |||
| 80 | 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; | |
| 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<XML::SAX>. | ||
| 91 | |||
| 92 | Returns a hash reference. | ||
| 93 | |||
| 94 | =cut | ||
| 95 | |||
| 96 | 0 | ▶ | sub prepare_qname { |
|
Subroutine
prepare_qname: not called CC=1 SCAR=6.9
Pod
documented
| |||
| 97 | 0 | my $pkg = shift; | |
| 98 | 0 | return &_prepare_qname(@_); | |
| 99 | } | ||
| 100 | |||
| 101 | # memoized | ||
| 102 | |||
| 103 | 0 | ▶ | sub _prepare_qname { |
|
Subroutine
_prepare_qname: not called CC=1 SCAR=6.9
| |||
| 104 | 0 | my $qname = shift; | |
| 105 | |||
| 106 | 0 | $qname =~ /^([^:]+):(.*)$/; | |
| 107 | |||
| 108 | 0 | my $prefix = $1; | |
| 109 | 0 | my $name = $2; | |
| 110 | |||
| 111 | 0 | my $ns = NS->{ $prefix }; | |
| 112 | |||
| 113 | 0 | 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<XML::SAX>. | ||
| 123 | |||
| 124 | Returns a hash reference. | ||
| 125 | |||
| 126 | =cut | ||
| 127 | |||
| 128 | 0 | ▶ | sub prepare_attrs { |
|
Subroutine
prepare_attrs: not called CC=2 SCAR=17.9
Pod
documented
| |||
| 129 | 0 | 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 | 0 | ▶ | sub _prepare_attr { |
|
Subroutine
_prepare_attr: not called CC=1 SCAR=6.9
| |||
| 144 | 0 | my $attr = shift; | |
| 145 | |||
| 146 | 0 | my $data = &_prepare_qname($attr->{Name}); | |
| 147 | 0 | $data->{Value} = $attr->{Value}; | |
| 148 | |||
| 149 | my $fq_uri = sprintf("{%s}%s", | ||
| 150 | $data->{NamespaceURI}, | ||
| 151 | 0 | $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 | 0 | ▶ | sub namespaces { |
|
Subroutine
namespaces: not called CC=1 SCAR=6.9
Pod
documented
| |||
| 164 | 0 | return NS; | |
| 165 | } | ||
| 166 | |||
| 167 | # deprecated | ||
| 168 | |||
| 169 | 0 | ▶ | sub _namespaces { |
|
Subroutine
_namespaces: not called CC=1 SCAR=6.9
| |||
| 170 | 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 E<lt>ascope@cpan.orgE<gt> | ||
| 184 | |||
| 185 | =head1 SEE ALSO | ||
| 186 | |||
| 187 | L<XML::SAX> | ||
| 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 | 1 | return 1; | |