Permalink
Browse files

reverse lookup

  • Loading branch information...
1 parent 4eaeb64 commit 124003b676368f9248a7e97655eff1f3fb881110 @nichtich committed May 22, 2012
Showing with 44 additions and 0 deletions.
  1. +27 −0 lib/RDF/NS.pm
  2. +17 −0 t/prefix.t
View
@@ -55,6 +55,23 @@ sub FORMAT {
}
}
+sub PREFIX {
+ my ($self, $uri) = @_;
+ while ( my ($prefix, $namespace) = each %$self ) {
+ return $prefix if $uri eq $namespace;
+ }
+ undef;
+}
+
+sub PREFIXES {
+ my ($self, $uri) = @_;
+ my @prefixes;
+ while ( my ($prefix, $namespace) = each %$self ) {
+ push @prefixes, $prefix if $uri eq $namespace;
+ }
+ return @prefixes;
+}
+
sub TTL {
my $self = shift;
$self->MAP( sub { "\@prefix $_: <".$self->{$_}."> ." } , @_ );
@@ -234,6 +251,16 @@ Returns a list of tabular-separated prefix-namespace-mappings.
Returns a list of BEACON format prefix definitions (not including prefixes).
+=method PREFIX ( $uri )
+
+Get a prefix of a namespace URI, if it is defined. This method does a reverse
+lookup which is less performant than the other direction. If multiple prefixes
+are defined, it is not determinstic which one is returned.
+
+=method PREFIXES ( $uri )
+
+Get all known prefixes of a namespace URI.
+
=method SELECT ( prefix[es] )
In list context, returns a sorted list of prefix-namespace pairs, which
View
@@ -0,0 +1,17 @@
+use strict;
+use warnings;
+use Test::More;
+
+use RDF::NS;
+
+my $rdfs = 'http://www.w3.org/2000/01/rdf-schema#';
+
+my $ns = RDF::NS->new('20111028');
+
+is $ns->PREFIX('http://www.w3.org/1999/02/22-rdf-syntax-ns#'), 'rdf', 'PREFIX';
+
+my @nslist = sort $ns->PREFIXES('http://purl.org/dc/elements/1.1/');
+
+is_deeply \@nslist, [qw(dc dc11)], 'PREFIXES';
+
+done_testing;

0 comments on commit 124003b

Please sign in to comment.