| 1 | # RDF::Trine::NamespaceMap | ||||||||||||||||||||||||||||
| 2 | # ----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | =head1 NAME | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | RDF::Trine::NamespaceMap - Collection of Namespaces | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | =head1 VERSION | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | This document describes RDF::Trine::NamespaceMap version 1.019 | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | use RDF::Trine::NamespaceMap; | ||||||||||||||||||||||||||||
| 16 | my $map = RDF::Trine::NamespaceMap->new( \%namespaces ); | ||||||||||||||||||||||||||||
| 17 | $serializer->serialize_model_to_string( $model, namespaces => $map ); | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | $map->add_mapping( foaf => 'http://xmlns.com/foaf/0.1/' ); | ||||||||||||||||||||||||||||
| 20 | my $foaf_namespace = $map->foaf; | ||||||||||||||||||||||||||||
| 21 | my $foaf_person = $map->foaf('Person'); | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | This module provides an object to manage multiple namespaces for | ||||||||||||||||||||||||||||
| 26 | creating L<RDF::Trine::Node::Resource> objects and for serializing. | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | =head1 METHODS | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | =over 4 | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | =cut | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | package RDF::Trine::NamespaceMap; | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 68 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 37 | 68 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 38 | 68 | ▶ | no warnings 'redefine'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 39 | 68 | ▶ | use Scalar::Util qw(blessed); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 40 | 68 | ▶ | use Data::Dumper; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | ###################################################################### | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 68 | our ($VERSION); | |||||||||||||||||||||||||||
| 45 | 68 | ▶ | BEGIN { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 46 | 68 | $VERSION = '1.019'; | |||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | ###################################################################### | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | =item C<< new ( [ \%namespaces ] ) >> | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | Returns a new namespace map object. You can pass a hash reference with | ||||||||||||||||||||||||||||
| 54 | mappings from local names to namespace URIs (given as string or | ||||||||||||||||||||||||||||
| 55 | L<RDF::Trine::Node::Resource>). | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | =cut | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 385 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 60 | 385 | my $class = shift; | |||||||||||||||||||||||||||
| 61 | 385 | ▶ | my $map = shift || {}; | ||||||||||||||||||||||||||
|
Condition100%2/2
shift() || {}
MC/DC100%1/1
shift() || {}
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 62 | 385 | my $self = bless( { fwd => {}, rev => {} }, $class ); | |||||||||||||||||||||||||||
| 63 | 385 | foreach my $name ( keys %$map ) { | |||||||||||||||||||||||||||
| 64 | 7 | $self->add_mapping( $name => $map->{$name} ); | |||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | 385 | return $self; | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | =item C<< add_mapping ( $name => $uri ) >> | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | Adds a new namespace to the map. The namespace URI can be passed | ||||||||||||||||||||||||||||
| 72 | as string or some object, that provides an uri_value method. | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | =cut | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 266 | ▶ | sub add_mapping { | ||||||||||||||||||||||||||
|
Subroutine
add_mapping: called CC=6 SCAR=18.2
Pod
documented
| |||||||||||||||||||||||||||||
| 77 | 266 | my $self = shift; | |||||||||||||||||||||||||||
| 78 | 266 | my $name = shift; | |||||||||||||||||||||||||||
| 79 | 266 | ▶ | if ($name =~ /^(new|uri|can|isa|VERSION|DOES)$/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | # reserved names | ||||||||||||||||||||||||||||
| 81 | 0 | throw RDF::Trine::Error::MethodInvocationError | |||||||||||||||||||||||||||
| 82 | -text => "Cannot use reserved name '$name' as a namespace prefix"; | ||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 266 | my $ns = shift; | |||||||||||||||||||||||||||
| 86 | 266 | foreach (qw(1 2)) { | |||||||||||||||||||||||||||
| 87 | # loop twice because the first call to C<<uri_value>> might | ||||||||||||||||||||||||||||
| 88 | # return a RDF::Trine::Namespace. Calling C<<uri_value>> on | ||||||||||||||||||||||||||||
| 89 | # the namespace object will then return a URI string value. | ||||||||||||||||||||||||||||
| 90 | 532 | ▶ | if (blessed($ns) and $ns->can('uri_value')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
builtin::blessed($ns) and $ns->can("uri_value")
MC/DC50%1/2
builtin::blessed($ns) and $ns->can("uri_value")
builtin::blessed($ns)
$ns->can("uri_value")
Truth table66%2/3
A and B
A= builtin::blessed($ns)B= $ns->can("uri_value")
| |||||||||||||||||||||||||||||
| 91 | 8 | $ns = $ns->uri_value; | |||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | # reverse lookup is many-to-one | ||||||||||||||||||||||||||||
| 96 | 266 | ▶ | $self->{rev}{$ns} ||= {}; | ||||||||||||||||||||||||||
|
Condition100%2/2
$self->{"rev"}{$ns} ||= {}
MC/DC100%1/1
$self->{"rev"}{$ns} ||= {}
$self->{"rev"}{$ns}
Truth table100%2/2
A
A= $self->{"rev"}{$ns}
| |||||||||||||||||||||||||||||
| 97 | # forward lookup | ||||||||||||||||||||||||||||
| 98 | return $self->{fwd}{$name} = | ||||||||||||||||||||||||||||
| 99 | 266 | $self->{rev}{$ns}{$name} = RDF::Trine::Namespace->new($ns); | |||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | =item C<< remove_mapping ( $name ) >> | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | Removes a namespace from the map. | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | =cut | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 1 | ▶ | sub remove_mapping { | ||||||||||||||||||||||||||
|
Subroutine
remove_mapping: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 109 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 110 | 1 | my $name = shift; | |||||||||||||||||||||||||||
| 111 | 1 | my $ns = delete $self->{fwd}{$name}; | |||||||||||||||||||||||||||
| 112 | 1 | delete $self->{rev}{$ns->uri_value}{$name}; | |||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | =item C<< namespace_uri ( $name ) >> | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | Returns the namespace object (if any) associated with the given name. | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | =cut | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 2870 | ▶ | sub namespace_uri { | ||||||||||||||||||||||||||
|
Subroutine
namespace_uri: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 122 | 2870 | my $self = shift; | |||||||||||||||||||||||||||
| 123 | 2870 | my $name = shift; | |||||||||||||||||||||||||||
| 124 | 2870 | return $self->{fwd}{$name}; | |||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | =item C<< list_namespaces >> | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | Returns an array of L<RDF::Trine::Namespace> objects with all the namespaces. | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | =cut | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 1 | ▶ | sub list_namespaces { | ||||||||||||||||||||||||||
|
Subroutine
list_namespaces: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 134 | # this has to be explicit or the context won't work | ||||||||||||||||||||||||||||
| 135 | 1 | my @out = sort { $a cmp $b } values %{$_[0]{fwd}}; | |||||||||||||||||||||||||||
| 136 | 1 | return @out; | |||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | =item C<< list_prefixes >> | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | Returns an array of prefixes. | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | =cut | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | 3 | ▶ | sub list_prefixes { | ||||||||||||||||||||||||||
|
Subroutine
list_prefixes: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 146 | 3 | my @out = sort keys %{$_[0]{fwd}}; | |||||||||||||||||||||||||||
| 147 | 3 | return @out; | |||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | =item C<< uri ( $prefixed_name ) >> | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | Returns a URI (as L<RDF::Trine::Node::Resource>) for an abbreviated | ||||||||||||||||||||||||||||
| 153 | string such as 'foaf:Person'. | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | =cut | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | 5 | ▶ | sub uri { | ||||||||||||||||||||||||||
|
Subroutine
uri: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 158 | 5 | my $self = shift; | |||||||||||||||||||||||||||
| 159 | 5 | my $abbr = shift; | |||||||||||||||||||||||||||
| 160 | 5 | my $ns; | |||||||||||||||||||||||||||
| 161 | 5 | my $local = ""; | |||||||||||||||||||||||||||
| 162 | 5 | ▶ | if ($abbr =~ m/^([^:]*):(.*)$/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 163 | 4 | $ns = $self->{fwd}{ $1 }; | |||||||||||||||||||||||||||
| 164 | 4 | $local = $2; | |||||||||||||||||||||||||||
| 165 | } else { | ||||||||||||||||||||||||||||
| 166 | 1 | $ns = $self->{fwd}{$abbr}; | |||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||
| 168 | 5 | ▶ | return unless (blessed($ns)); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 169 | 5 | ▶ | if ($local ne '') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 170 | # don't invoke the AUTOLOAD here | ||||||||||||||||||||||||||||
| 171 | 3 | return $ns->uri($local); | |||||||||||||||||||||||||||
| 172 | } else { | ||||||||||||||||||||||||||||
| 173 | 2 | return $ns->uri_value; | |||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | =item prefix_for C<< uri ($uri) >> | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | Returns the associated prefix (or potentially multiple prefixes, in | ||||||||||||||||||||||||||||
| 180 | list context) for the given URI. | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | =cut | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | 3 | ▶ | sub prefix_for { | ||||||||||||||||||||||||||
|
Subroutine
prefix_for: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||||||||||||||||||
| 185 | 3 | my ($self, $uri) = @_; | |||||||||||||||||||||||||||
| 186 | 3 | ▶ | $uri = $uri->uri_value if ref $uri; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | 3 | my @candidates; | |||||||||||||||||||||||||||
| 189 | 3 | for my $vuri (keys %{$self->{rev}}) { | |||||||||||||||||||||||||||
| 190 | 9 | ▶ | next if length $vuri > length $uri; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | # candidate namespace must match exactly | ||||||||||||||||||||||||||||
| 193 | 4 | my $cns = substr($uri, 0, length $vuri); | |||||||||||||||||||||||||||
| 194 | 4 | ▶ | push @candidates, keys %{$self->{rev}{$vuri}} if $cns eq $vuri; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 195 | } | ||||||||||||||||||||||||||||
| 196 | # my @candidates; | ||||||||||||||||||||||||||||
| 197 | # while (my ($k, $v) = each %{$self->{fwd}}) { | ||||||||||||||||||||||||||||
| 198 | # my $vuri = $v->uri->uri_value; | ||||||||||||||||||||||||||||
| 199 | # # the input should always be longer than the namespace | ||||||||||||||||||||||||||||
| 200 | # next if length $vuri > length $uri; | ||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | # # candidate namespace must match exactly | ||||||||||||||||||||||||||||
| 203 | # my $cns = substr($uri, 0, length $vuri); | ||||||||||||||||||||||||||||
| 204 | # push @candidates, $k if $cns eq $vuri; | ||||||||||||||||||||||||||||
| 205 | # } | ||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | # make sure this behaves correctly when empty | ||||||||||||||||||||||||||||
| 208 | 3 | ▶ | return unless @candidates; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | # if this returns more than one prefix, take the | ||||||||||||||||||||||||||||
| 211 | # shortest/lexically lowest one. | ||||||||||||||||||||||||||||
| 212 | 2 | @candidates = sort @candidates; | |||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | 2 | ▶ | return wantarray ? @candidates : $candidates[0]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 215 | } | ||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | =item abbreviate C<< uri ($uri) >> | ||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||
| 219 | Complement to L</namespace_uri>. Returns the given URI in C<foo:bar> | ||||||||||||||||||||||||||||
| 220 | format or C<undef> if it wasn't matched, therefore the idiom | ||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||
| 222 | my $str = $nsmap->abbreviate($uri_node) || $uri_node->uri_value; | ||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||
| 224 | may be useful for certain serialization tasks. | ||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||
| 226 | =cut | ||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||
| 228 | 3 | ▶ | sub abbreviate { | ||||||||||||||||||||||||||
|
Subroutine
abbreviate: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 229 | 3 | my ($self, $uri) = @_; | |||||||||||||||||||||||||||
| 230 | 3 | ▶ | $uri = $uri->uri_value if ref $uri; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 231 | 3 | my $prefix = $self->prefix_for($uri); | |||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||
| 233 | # XXX is this actually the most desirable behaviour? | ||||||||||||||||||||||||||||
| 234 | 3 | ▶ | return unless defined $prefix; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||
| 236 | 2 | my $offset = length $self->namespace_uri($prefix)->uri->uri_value; | |||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||
| 238 | 2 | return sprintf('%s:%s', $prefix, substr($uri, $offset)); | |||||||||||||||||||||||||||
| 239 | } | ||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||
| 242 | 390 | ▶ | sub AUTOLOAD { | ||||||||||||||||||||||||||
|
Subroutine
AUTOLOAD: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 243 | 390 | my $self = shift; | |||||||||||||||||||||||||||
| 244 | 390 | our $AUTOLOAD; | |||||||||||||||||||||||||||
| 245 | 390 | ▶ | return if ($AUTOLOAD =~ /:DESTROY$/); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 246 | 5 | my ($name) = ($AUTOLOAD =~ m/^.*:(.*)$/); | |||||||||||||||||||||||||||
| 247 | 5 | my $ns = $self->{fwd}{$name}; | |||||||||||||||||||||||||||
| 248 | 5 | ▶ | return unless (blessed($ns)); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 249 | 4 | ▶ | if (scalar(@_)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 250 | 2 | my $local = shift(@_); | |||||||||||||||||||||||||||
| 251 | 2 | return $ns->$local( @_ ); | |||||||||||||||||||||||||||
| 252 | } else { | ||||||||||||||||||||||||||||
| 253 | 2 | return $ns; | |||||||||||||||||||||||||||
| 254 | } | ||||||||||||||||||||||||||||
| 255 | } | ||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||
| 257 | 68 | 1; # Magic true value required at end of module | |||||||||||||||||||||||||||
| 258 | __END__ | ||||||||||||||||||||||||||||