[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 08fad73709f11fe5a8f1ea83701ab71fc3088fdb
Author: Jakob Voss <jakob at nichtich.de>
Date:   Thu Oct 27 13:30:58 2011 +0200

    documentation

diff --git a/lib/RDF/NS.pm b/lib/RDF/NS.pm
index f3d2695..c706903 100644
--- a/lib/RDF/NS.pm
+++ b/lib/RDF/NS.pm
@@ -9,15 +9,16 @@ use File::ShareDir;
 our $AUTOLOAD;
 
 sub new {
-	my ($class, $version) = @_;
-	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, $only, $warn) = @_;
+	my ($class, $file, %options) = @_;
 	$class = ref($class) || $class;
 
-	# TODO: filter $only
+	my $warn = $options{'warn'};
 
 	my $ns = { };
 	open (my $fh, '<', $file) or die "failed to open $file";
@@ -29,7 +30,11 @@ sub LOAD {
 			warn "Cannot support prefix $prefix!" if $warn;
 			next;
 		} elsif ( $prefix =~ /^[a-z][a-z0-9]+$/ ) {
-			$ns->{$prefix} = $namespace;
+			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";
 		}
@@ -38,20 +43,47 @@ sub LOAD {
 	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];
+}
+
+sub SPARQL {
+	my $self = shift;
+	my @ns = map { "PREFIX $_: <".$self->{$_}.">" } 
+		grep { $self->{$_} } sort map { split /[|, ]+/ } @_;
+	return wantarray ? @ns : $ns[0];
+}
+
+sub XMLNS {
+	my $self = shift;
+	my @ns = map { "xmlns:$_=\"".$self->{$_}."\"" } 
+		grep { $self->{$_} } sort map { split /[|, ]+/ } @_;
+	return wantarray ? @ns : $ns[0];
+}
+
+sub GET {
+	$_[1];
+}
+
 sub URI {
 	my $self = shift;
-	return unless shift =~ /^([a-z][a-z0-9]+)(:([^:]+))?$/;
+	return unless shift =~ /^([a-z][a-z0-9]+)([:_]([^:]+))?$/;
 	my $ns = $self->{$1} or return;
-	return $ns unless $3;
-	return $ns.$3;
+	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;
-	return $ns unless $3;
-	return $ns.$3;
+	my $local = defined $3 ? $3 : shift;
+	return $self->GET($ns) unless defined $local;
+	return $self->GET($ns.$local);
 }
 
 1;
@@ -61,19 +93,78 @@ sub AUTOLOAD {
   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                # 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");
 
+  # 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
 
 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 L<http://prefix.cc>. This module ...
+as collected at L<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 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>.
+
+=method new ( $version [, %options ] )
+
+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 
+options include C<warn> to enable warnings.
+
+=method URI ( $short )
+
+Expand a prefixed URI, such as C<foaf:Person>. Alternatively you can expand
+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.
+
+=method 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, or spaces.
+
+=method 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, or spaces.
+
+=method GET ( $uri )
+
+This method is used internally to create URIs. By default it returns C<$uri>.
 
 =head1 SEE ALSO
 
@@ -82,4 +173,3 @@ 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/t/namespaces.t b/t/namespaces.t
index 2ebc076..61d1aa5 100644
--- a/t/namespaces.t
+++ b/t/namespaces.t
@@ -5,14 +5,36 @@ use Test::More;
 use RDF::NS;
 
 # this should never change
-my $rdf = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#';
+my $rdf  = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#';
+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->URI("rdf:type"), $rdf.'type', '$ns->URI("rdf:type")' );
+is( $ns->URI("rdf_type"), $rdf.'type', '$ns->URI("rdf_type")' );
+
+is( $ns->SPARQL('rdf'), "PREFIX rdf: <$rdf>", 'SPARQL("rdf")' );
+is( $ns->TTL('rdfs'), "\@prefix rdfs: <$rdfs> .", 'TTL("rdfs")' );
+
+my $sparql = ["PREFIX rdf: <$rdf>","PREFIX rdfs: <$rdfs>"];
+my $turtle = ["\@prefix rdf: <$rdf> .","\@prefix rdfs: <$rdfs> ."];
+my $xmlns  = ["xmlns:rdf=\"$rdf\"","xmlns:rdfs=\"$rdfs\""];
+
+my @args = (['rdfs','rdf'],['rdf|rdfs'],['rdf,xxxxxx','rdfs'],['rdfs  rdf']);
+foreach (@args) {
+    my @list = $ns->SPARQL(@$_);
+    is_deeply( \@list, $sparql, 'SPARQL(...)' );
+    @list= $ns->TTL(@$_);
+    is_deeply( \@list, $turtle, 'TTL(...)' );
+    @list= $ns->XMLNS(@$_);
+    is_deeply( \@list, $xmlns, 'XMLNS(...)' );
+}
 
 done_testing;
diff --git a/t/trine.t b/t/trine.t
new file mode 100644
index 0000000..6e7dc63
--- /dev/null
+++ b/t/trine.t
@@ -0,0 +1,21 @@
+use strict;
+use warnings;
+use Test::More;
+
+use RDF::NS;
+
+my $trine = 'RDF::Trine::Node::Resource';
+eval { require $trine };
+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->new('20111028', as => 'trine');
+#isa_ok( $ns->foaf, $trine );
+
+done_testing;
diff --git a/update.pl b/update.pl
index 8520db6..cc9b994 100755
--- a/update.pl
+++ b/update.pl
@@ -10,7 +10,7 @@ my $dist = do { local( @ARGV, $/ ) = 'dist.ini'; <> }
 $dist =~ /^\s*version\s*=\s*([^\s]+)/m 
 	or die "dist.ini must include version number";
 
-# TODO: check version number format and date
+# TODO: check version number format and date to give a warning
 
 my $file = "share/$1.txt";
 
@@ -24,7 +24,7 @@ my $tmp = File::Temp->new->filename;
 my $url = "http://prefix.cc/popular/all.file.txt";
 my $prefixcc = mirror($url,$tmp) or die "Failed to load $url";
 
-my $ns = RDF::NS->LOAD($tmp,undef,1);
+my $ns = RDF::NS->LOAD( $tmp, warn => 1 );
 
 foreach my $prefix (sort keys %$ns) {
 	print $fh "$prefix\t" . $ns->{$prefix} . "\n";

-- 
librdf-ns-perl Debian packaging



More information about the Pkg-perl-cvs-commits mailing list