[SCM] librdf-ns-perl Debian packaging branch, master, updated. debian/20130208-1-22-g47de138
Jakob Voss
jakob at nichtich.de
Fri Apr 19 13:28:37 UTC 2013
The following commit has been merged in the master branch:
commit 04a1886057e659f455c734ce557e1c4a87e7a2d1
Author: Jakob Voss <jakob at nichtich.de>
Date: Fri Oct 28 14:42:55 2011 +0200
first full-featured version
diff --git a/Changes b/Changes
new file mode 100644
index 0000000..d15005e
--- /dev/null
+++ b/Changes
@@ -0,0 +1 @@
+20111028 - first full-featured version
diff --git a/README.mkdn b/README.mkdn
new file mode 100644
index 0000000..10ccef7
--- /dev/null
+++ b/README.mkdn
@@ -0,0 +1,126 @@
+# NAME
+
+RDF::NS - Just use popular RDF namespace prefixes from prefix.cc
+
+# VERSION
+
+version 20111028
+
+# SYNOPSIS
+
+ use RDF::NS '20111028';
+ my $ns = RDF::NS->new('20111028');
+
+ $ns->foaf; # http://xmlns.com/foaf/0.1/
+ $ns->foaf_Person; # http://xmlns.com/foaf/0.1/Person
+ $ns->foaf('Person'); # http://xmlns.com/foaf/0.1/Person
+ $ns->URI('foaf:Person'); # http://xmlns.com/foaf/0.1/Person
+
+ $ns->SPAQRL('foaf'); # PREFIX foaf: <http://xmlns.com/foaf/0.1/>
+ $ns->TTL('foaf'); # @prefix foaf: <http://xmlns.com/foaf/0.1/> .
+ $ns->XMLNS('foaf'); # xmlns:foaf="http://xmlns.com/foaf/0.1/"
+
+ # To get RDF::Trine::Node::Resource instead of strings
+ my $ns = RDF::NS->new( '20111028', as => 'trine' );
+ $ns->foaf_Person; # iri('http://xmlns.com/foaf/0.1/Person')
+
+ # load your own mapping
+ $ns = RDF::NS::LOAD("mapping.txt");
+
+ # select particular mappings
+ %map = $ns->SELECT('rdf,dc,foaf');
+ $uri = $ns->SELECT('foo|bar|doz'); # returns first existing namespace
+
+ # instances are just blessed hash references
+ $ns->{'foaf'} # http://xmlns.com/foaf/0.1/
+ bless { foaf => 'http://xmlns.com/foaf/0.1/' }, 'RDF::NS';
+ print (scalar %$ns) . "prefixes\n";
+
+# DESCRIPTION
+
+Hardcoding URI namespaces and prefixes for RDF applications is neither fun nor
+maintainable. In the end we all use more or less the same prefix definitions,
+as collected at [http://prefix.cc](http://prefix.cc). This module includes all these prefixes as
+defined at specific snapshots in time. These snapshots correspond to version
+numbers of this module. By selecting particular versions, you make sure that
+changes at prefix.cc won't affect your scripts.
+
+This module does not require [RDF::Trine](http://search.cpan.org/perldoc?RDF::Trine) which is recommended nevertheless.
+If you prefer RDF::NS to return instances of [RDF::Trine::Node::Resource](http://search.cpan.org/perldoc?RDF::Trine::Node::Resource)
+instead of plain strings, use [RDF::NS::Trine](http://search.cpan.org/perldoc?RDF::NS::Trine).
+
+The code repository of this module contains an
+[update script](https://github.com/nichtich/RDF-NS/blob/master/update.pl)
+to download the current prefix-namespace mappings from [http://prefix.cc](http://prefix.cc).
+
+# METHODS
+
+## new ( $version [, %options ] )
+
+Create a new namespace mapping with a selected version (mandatory).
+See LOAD for supported options.
+
+## LOAD ( $file [, %options ] )
+
+Load namespace mappings from a particular tab-separated file. Supported
+options include `warn` to enable warnings.
+
+## URI ( $short )
+
+Expand a prefixed URI, such as `foaf:Person`. Alternatively you can expand
+prefixed URIs with method calls, such as `<$ns-`foaf_Person>>.
+
+## TTL ( prefix[es] )
+
+Returns a Turtle/Notation3 @prefix definition or a list of such definitions
+in list context. Prefixes can be passed as single arguments or separated by
+commas, vertical bars, and spaces.
+
+## SPARQL ( prefix[es] )
+
+Returns a SPARQL PREFIX definition or a list of such definitions in list
+context. Prefixes can be passed as single arguments or separated by commas,
+vertical bars, and spaces.
+
+## XMLNS ( prefix[es] )
+
+Returns an XML namespace declaration or a list of such declarations in list
+context. Prefixes can be passed as single arguments or separated by commas,
+vertical bars, and spaces.
+
+## SELECT ( prefix[es] )
+
+In list context, returns a sorted list of prefix-namespace pairs, which
+can be used to assign to a hash. In scalar context, returns the namespace
+of the first prefix that was found. Prefixes can be passed as single arguments
+or separated by commas, vertical bars, and spaces.
+
+## MAP ( $code, prefix[es] )
+
+Internally used to select and map particular prefixes, that can be passed as
+single arguments or separated by commas, vertical bars, and spaces. In scalar
+context, `$_` is set to the first existing prefix (if found) and `$code` is
+called. In list context, found prefixes are sorted at mapped with `$code`.
+
+## GET ( $uri )
+
+This method is used internally to create URIs as return value of the URI
+method and all lowercase shortcut methods, such as `foaf_Person`. By default
+it just returns `$uri` unmodified.
+
+# SEE ALSO
+
+There are several CPAN modules to deal with IRI namespaces, for instance
+[RDF::Trine::Namespace](http://search.cpan.org/perldoc?RDF::Trine::Namespace), L<RDF::Trine::NamespaceMap<>, [RDF::Prefixes](http://search.cpan.org/perldoc?RDF::Prefixes),
+[RDF::Simple::NS](http://search.cpan.org/perldoc?RDF::Simple::NS), [RDF::RDFa::Parser::Profile::PrefixCC](http://search.cpan.org/perldoc?RDF::RDFa::Parser::Profile::PrefixCC) etc.
+
+# AUTHOR
+
+Jakob Voss
+
+# COPYRIGHT AND LICENSE
+
+This software is copyright (c) 2011 by Jakob Voss.
+
+This is free software; you can redistribute it and/or modify it under
+the same terms as the Perl 5 programming language system itself.
\ No newline at end of file
diff --git a/lib/RDF/NS.pm b/lib/RDF/NS.pm
index c706903..de46246 100644
--- a/lib/RDF/NS.pm
+++ b/lib/RDF/NS.pm
@@ -9,81 +9,92 @@ use File::ShareDir;
our $AUTOLOAD;
sub new {
- my $class = shift;
- my $version = shift;
- LOAD( $class, File::ShareDir::dist_file('RDF-NS', "$version.txt" ), @_ );
+ my $class = shift;
+ my $version = shift;
+ LOAD( $class, File::ShareDir::dist_file('RDF-NS', "$version.txt" ), @_ );
}
sub LOAD {
- my ($class, $file, %options) = @_;
- $class = ref($class) || $class;
-
- my $warn = $options{'warn'};
-
- my $ns = { };
- open (my $fh, '<', $file) or die "failed to open $file";
- foreach (<$fh>) {
- chomp;
- next if /^#/;
- my ($prefix, $namespace) = split "\t", $_;
- if ( $prefix =~ /^(isa|can|new)$/ ) {
- warn "Cannot support prefix $prefix!" if $warn;
- next;
- } elsif ( $prefix =~ /^[a-z][a-z0-9]+$/ ) {
- if ( $namespace =~ /^[a-z][a-z0-9]+:[^"<>]*$/ ) {
- $ns->{$prefix} = $namespace;
- } elsif( $warn ) {
- warn "Skipping invalid $prefix namespace $namespace";
- }
- } elsif ( $warn ) {
- warn "Skipping unusual prefix $prefix";
- }
- }
-
- bless $ns, $class;
+ my ($class, $file, %options) = @_;
+ $class = ref($class) || $class;
+
+ my $warn = $options{'warn'};
+
+ my $ns = { };
+ open (my $fh, '<', $file) or die "failed to open $file";
+ foreach (<$fh>) {
+ chomp;
+ next if /^#/;
+ my ($prefix, $namespace) = split "\t", $_;
+ if ( $prefix =~ /^(isa|can|new)$/ ) {
+ warn "Cannot support prefix $prefix!" if $warn;
+ next;
+ } elsif ( $prefix =~ /^[a-z][a-z0-9]+$/ ) {
+ if ( $namespace =~ /^[a-z][a-z0-9]+:[^"<>]*$/ ) {
+ $ns->{$prefix} = $namespace;
+ } elsif( $warn ) {
+ warn "Skipping invalid $prefix namespace $namespace";
+ }
+ } elsif ( $warn ) {
+ warn "Skipping unusual prefix $prefix";
+ }
+ }
+
+ bless $ns, $class;
}
sub TTL {
- my $self = shift;
- # functional programming rulez
- my @ns = map { "\@prefix $_: <".$self->{$_}."> ." }
- grep { $self->{$_} } sort map { split /[|, ]+/ } @_;
- return wantarray ? @ns : $ns[0];
+ my $self = shift;
+ $self->MAP( sub { "\@prefix $_: <".$self->{$_}."> ." } , @_ );
}
sub SPARQL {
- my $self = shift;
- my @ns = map { "PREFIX $_: <".$self->{$_}.">" }
- grep { $self->{$_} } sort map { split /[|, ]+/ } @_;
- return wantarray ? @ns : $ns[0];
+ my $self = shift;
+ $self->MAP( sub { "PREFIX $_: <".$self->{$_}.">" } , @_ );
}
sub XMLNS {
- my $self = shift;
- my @ns = map { "xmlns:$_=\"".$self->{$_}."\"" }
- grep { $self->{$_} } sort map { split /[|, ]+/ } @_;
- return wantarray ? @ns : $ns[0];
+ my $self = shift;
+ $self->MAP( sub { "xmlns:$_=\"".$self->{$_}."\"" } , @_ );
+}
+
+sub SELECT {
+ my $self = shift;
+ $self->MAP( sub { $_ => $self->{$_} } , @_ );
+}
+
+# functional programming rulez!
+sub MAP {
+ my $self = shift;
+ my $code = shift;
+ my @ns = grep { $self->{$_} } map { split /[|, ]+/ } @_;
+ if (wantarray) {
+ return map { $code->() } sort @ns;
+ } else {
+ local $_ = $ns[0];
+ return $code->();
+ }
}
sub GET {
- $_[1];
+ $_[1];
}
sub URI {
- my $self = shift;
- return unless shift =~ /^([a-z][a-z0-9]+)([:_]([^:]+))?$/;
- my $ns = $self->{$1} or return;
- return $self->GET($ns) unless $3;
- return $self->GET($ns.$3);
+ my $self = shift;
+ return unless shift =~ /^([a-z][a-z0-9]+)([:_]([^:]+))?$/;
+ my $ns = $self->{$1} or return;
+ return $self->GET($ns) unless $3;
+ return $self->GET($ns.$3);
}
sub AUTOLOAD {
- my $self = shift;
- return unless $AUTOLOAD =~ /:([a-z][a-z0-9]+)(_([^:]+))?$/;
- my $ns = $self->{$1} or return;
- my $local = defined $3 ? $3 : shift;
- return $self->GET($ns) unless defined $local;
- return $self->GET($ns.$local);
+ my $self = shift;
+ return unless $AUTOLOAD =~ /:([a-z][a-z0-9]+)(_([^:]+))?$/;
+ my $ns = $self->{$1} or return;
+ my $local = defined $3 ? $3 : shift;
+ return $self->GET($ns) unless defined $local;
+ return $self->GET($ns.$local);
}
1;
@@ -91,12 +102,12 @@ sub AUTOLOAD {
=head1 SYNOPSIS
use RDF::NS '20111028';
- my $ns = RDF::NS->new('20111028')
+ my $ns = RDF::NS->new('20111028');
- $ns->foaf # http://xmlns.com/foaf/0.1/
- $ns->foaf_Person # http://xmlns.com/foaf/0.1/Person
- $ns->foaf('Person') # http://xmlns.com/foaf/0.1/Person
- $ns->URI('foaf:Person') # http://xmlns.com/foaf/0.1/Person
+ $ns->foaf; # http://xmlns.com/foaf/0.1/
+ $ns->foaf_Person; # http://xmlns.com/foaf/0.1/Person
+ $ns->foaf('Person'); # http://xmlns.com/foaf/0.1/Person
+ $ns->URI('foaf:Person'); # http://xmlns.com/foaf/0.1/Person
$ns->SPAQRL('foaf'); # PREFIX foaf: <http://xmlns.com/foaf/0.1/>
$ns->TTL('foaf'); # @prefix foaf: <http://xmlns.com/foaf/0.1/> .
@@ -104,16 +115,18 @@ sub AUTOLOAD {
# To get RDF::Trine::Node::Resource instead of strings
my $ns = RDF::NS->new( '20111028', as => 'trine' );
- $ns->foaf_Person # iri('http://xmlns.com/foaf/0.1/Person')
+ $ns->foaf_Person; # iri('http://xmlns.com/foaf/0.1/Person')
# load your own mapping
$ns = RDF::NS::LOAD("mapping.txt");
+ # select particular mappings
+ %map = $ns->SELECT('rdf,dc,foaf');
+ $uri = $ns->SELECT('foo|bar|doz'); # returns first existing namespace
+
# instances are just blessed hash references
$ns->{'foaf'} # http://xmlns.com/foaf/0.1/
-
bless { foaf => 'http://xmlns.com/foaf/0.1/' }, 'RDF::NS';
-
print (scalar %$ns) . "prefixes\n";
=head1 DESCRIPTION
@@ -129,14 +142,18 @@ This module does not require L<RDF::Trine> which is recommended nevertheless.
If you prefer RDF::NS to return instances of L<RDF::Trine::Node::Resource>
instead of plain strings, use L<RDF::NS::Trine>.
+The code repository of this module contains an
+L<update script|https://github.com/nichtich/RDF-NS/blob/master/update.pl>
+to download the current prefix-namespace mappings from L<http://prefix.cc>.
+
=method new ( $version [, %options ] )
-Create a new namespace mapping with a selected version (mandatory).
+Create a new namespace mapping with a selected version (mandatory).
See LOAD for supported options.
=method LOAD ( $file [, %options ] )
-Load namespace mappings from a particular tab-separated file. Supported
+Load namespace mappings from a particular tab-separated file. Supported
options include C<warn> to enable warnings.
=method URI ( $short )
@@ -146,30 +163,46 @@ prefixed URIs with method calls, such as C<<$ns->foaf_Person>>.
=method TTL ( prefix[es] )
-Returns a Turtle/Notation3 @prefix definition or a list of such definitions
-in list context. Prefixes can be passed as single arguments or separated by
-commas, vertical bars, or spaces.
+Returns a Turtle/Notation3 @prefix definition or a list of such definitions
+in list context. Prefixes can be passed as single arguments or separated by
+commas, vertical bars, and spaces.
=method SPARQL ( prefix[es] )
-Returns a SPARQL PREFIX definition or a list of such definitions in list
+Returns a SPARQL PREFIX definition or a list of such definitions in list
context. Prefixes can be passed as single arguments or separated by commas,
-vertical bars, or spaces.
+vertical bars, and spaces.
=method XMLNS ( prefix[es] )
-Returns an XML namespace declaration or a list of such declarations in list
+Returns an XML namespace declaration or a list of such declarations in list
context. Prefixes can be passed as single arguments or separated by commas,
-vertical bars, or spaces.
+vertical bars, and spaces.
+
+=method SELECT ( prefix[es] )
+
+In list context, returns a sorted list of prefix-namespace pairs, which
+can be used to assign to a hash. In scalar context, returns the namespace
+of the first prefix that was found. Prefixes can be passed as single arguments
+or separated by commas, vertical bars, and spaces.
+
+=method MAP ( $code, prefix[es] )
+
+Internally used to select and map particular prefixes, that can be passed as
+single arguments or separated by commas, vertical bars, and spaces. In scalar
+context, C<$_> is set to the first existing prefix (if found) and C<$code> is
+called. In list context, found prefixes are sorted at mapped with C<$code>.
=method GET ( $uri )
-This method is used internally to create URIs. By default it returns C<$uri>.
+This method is used internally to create URIs as return value of the URI
+method and all lowercase shortcut methods, such as C<foaf_Person>. By default
+it just returns C<$uri> unmodified.
=head1 SEE ALSO
There are several CPAN modules to deal with IRI namespaces, for instance
-L<RDF::Trine::Namespace>, L<RDF::Trine::NamespaceMap<>, L<RDF::Prefixes>,
+L<RDF::Trine::Namespace>, L<RDF::Trine::NamespaceMap<>, L<RDF::Prefixes>,
L<RDF::Simple::NS>, L<RDF::RDFa::Parser::Profile::PrefixCC> etc.
=cut
diff --git a/lib/RDF/NS/Trine.pm b/lib/RDF/NS/Trine.pm
new file mode 100644
index 0000000..ac5ccbb
--- /dev/null
+++ b/lib/RDF/NS/Trine.pm
@@ -0,0 +1,34 @@
+use strict;
+use warnings;
+package RDF::NS::Trine;
+#ABSTRACT: Popular RDF namespace prefixes from prefix.cc as RDF::Trine nodes
+
+use RDF::Trine::Node::Resource;
+
+use parent 'RDF::NS';
+
+sub GET {
+ RDF::Trine::Node::Resource->new($_[1]);
+}
+
+1;
+
+=head1 SYNOPSIS
+
+ use RDF::NS::Trine;
+
+ my $ns = RDF::NS::Trine->new('20111028');
+
+ $ns->foaf_Person; # a RDF::Trine::Node::Resource
+ $ns->URI('foaf:Person); # same
+ $ns->foaf_Person->uri; # http://xmlns.com/foaf/0.1/Person
+
+=head1 DESCRIPTION
+
+In contrast to L<RDF::NS>, which should be consulted for documentation, this
+returns no plain string URIs but instances of L<RDF::Trine::Node::Resource>.
+
+Before using this module, make sure to install L<RDF::Trine>, which is not
+automatically installed together with L<RDF::NS>!
+
+=cut
diff --git a/share/20111028.txt b/share/20111028.txt
index a04ea3f..96e73c9 100644
--- a/share/20111028.txt
+++ b/share/20111028.txt
@@ -213,6 +213,7 @@ fresnel http://www.w3.org/2004/09/fresnel#
frir http://purl.org/twc/ontology/frir.owl#
game http://data.totl.net/game/
gazetteer http://data.ordnancesurvey.co.uk/ontology/50kGazetteer/
+gbv http://purl.org/ontology/gbv/
gc http://www.oegov.org/core/owl/gc#
gd http://rdf.data-vocabulary.org/#
gelo http://krauthammerlab.med.yale.edu/ontologies/gelo#
@@ -453,6 +454,7 @@ psych http://purl.org/vocab/psychometric-profile/
pto http://www.productontology.org/id/
ptr http://www.w3.org/2009/pointers#
puc http://purl.org/NET/puc#
+puelia http://kwijibo.talis.com/vocabs/puelia#
push http://www.w3.org/2007/uwa/context/push.owl#
qb http://purl.org/linked-data/cube#
qdoslf http://foaf.qdos.com/lastfm/schema/
@@ -462,6 +464,7 @@ qudt http://qudt.org/1.1/schema/qudt#
r2r http://www4.wiwiss.fu-berlin.de/bizer/r2r/
rail http://ontologi.es/rail/vocab#
rdacarrier http://rdvocab.info/termList/RDACarrierType/
+rdacontent http://rdvocab.info/termList/RDAContentType/
rdamedia http://rdvocab.info/termList/RDAMediaType/
rdb http://www.dbs.cs.uni-duesseldorf.de/RDF/relational#
rdf http://www.w3.org/1999/02/22-rdf-syntax-ns#
diff --git a/t/namespaces.t b/t/namespaces.t
index 61d1aa5..42ad3bc 100644
--- a/t/namespaces.t
+++ b/t/namespaces.t
@@ -11,22 +11,27 @@ my $rdfs = 'http://www.w3.org/2000/01/rdf-schema#';
# get some prefixed URIs
my $ns = RDF::NS->new('20111028');
-is( $ns->rdf, $rdf, '$ns->rdf' );
-is( $ns->rdf_type, $rdf.'type', '$ns->rdf_type' );
-is( $ns->rdf_type('x'), $rdf.'type', '$ns->rdf_type' );
-is( $ns->rdf('f-o'), $rdf."f-o", '$ns->rdf("f-o")' );
-is( $ns->rdf(0), $rdf."0", '$ns->rdf("0")' );
+is( $ns->rdf, $rdf, '$ns->rdf' );
+is( $ns->rdf_type, $rdf.'type', '$ns->rdf_type' );
+is( $ns->rdf_type('x'), $rdf.'type', '$ns->rdf_type' );
+is( $ns->rdf('f-o'), $rdf."f-o", '$ns->rdf("f-o")' );
+is( $ns->rdf(0), $rdf."0", '$ns->rdf("0")' );
is( $ns->URI("rdf:type"), $rdf.'type', '$ns->URI("rdf:type")' );
is( $ns->URI("rdf_type"), $rdf.'type', '$ns->URI("rdf_type")' );
+# scalar context
is( $ns->SPARQL('rdf'), "PREFIX rdf: <$rdf>", 'SPARQL("rdf")' );
is( $ns->TTL('rdfs'), "\@prefix rdfs: <$rdfs> .", 'TTL("rdfs")' );
+# order is relevant
+is( $ns->XMLNS('rdfs,rdf'), "xmlns:rdfs=\"$rdfs\"" );
+is( $ns->XMLNS('rdf,rdfs'), "xmlns:rdf=\"$rdf\"" );
my $sparql = ["PREFIX rdf: <$rdf>","PREFIX rdfs: <$rdfs>"];
my $turtle = ["\@prefix rdf: <$rdf> .","\@prefix rdfs: <$rdfs> ."];
my $xmlns = ["xmlns:rdf=\"$rdf\"","xmlns:rdfs=\"$rdfs\""];
+# list context
my @args = (['rdfs','rdf'],['rdf|rdfs'],['rdf,xxxxxx','rdfs'],['rdfs rdf']);
foreach (@args) {
my @list = $ns->SPARQL(@$_);
@@ -37,4 +42,10 @@ foreach (@args) {
is_deeply( \@list, $xmlns, 'XMLNS(...)' );
}
+my %s = $ns->SELECT('rdfs,xx','rdf');
+is_deeply( \%s, { rdfs => $rdfs, rdf => $rdf }, 'SELECT (list)' );
+
+my $first = $ns->SELECT('xxxxx,,rdf');
+is( $first, $rdf, 'SELECT (scalar)' );
+
done_testing;
diff --git a/t/trine.t b/t/trine.t
index 6e7dc63..1e15ab2 100644
--- a/t/trine.t
+++ b/t/trine.t
@@ -2,20 +2,29 @@ use strict;
use warnings;
use Test::More;
-use RDF::NS;
-
-my $trine = 'RDF::Trine::Node::Resource';
-eval { require $trine };
+eval { require RDF::Trine::Node::Resource; 1; };
if ( $@ ) {
diag("RDF::Trine missing - skip tests of RDF::NS::Trine");
exit 0;
}
-
+
use_ok('RDF::NS::Trine');
-# TODO: check SPARQL, URI etc.
+my $ns = RDF::NS::Trine->new('20111028');
+
+# should return resources
+my $trine = 'RDF::Trine::Node::Resource';
+isa_ok( $ns->rdf, $trine );
+isa_ok( $ns->rdf_type, $trine );
+isa_ok( $ns->URI('rdf:type'), $trine );
+
+# this should never change
+my $rdf = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#';
+my $rdfs = 'http://www.w3.org/2000/01/rdf-schema#';
-#my $ns = RDF::NS->new('20111028', as => 'trine');
-#isa_ok( $ns->foaf, $trine );
+# should still return strings
+is( $ns->SPARQL('rdf'), "PREFIX rdf: <$rdf>", 'SPARQL("rdf")' );
+is( $ns->TTL('rdfs'), "\@prefix rdfs: <$rdfs> .", 'TTL("rdfs")' );
+is( $ns->XMLNS('rdfs'), "xmlns:rdfs=\"$rdfs\"", 'XMLNS("rdfs")' );
done_testing;
--
librdf-ns-perl Debian packaging
More information about the Pkg-perl-cvs-commits
mailing list