File: rdfns.pm

package info (click to toggle)
librdf-ns-perl 20140910-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 252 kB
  • ctags: 44
  • sloc: cpp: 1,488; perl: 360; makefile: 13
file content (114 lines) | stat: -rwxr-xr-x 2,444 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
use strict;
use warnings;
package App::rdfns;
#ABSTRACT: quickly get common URI namespaces
#VERSION
$App::rdfns::VERSION = '20140910';
use v5.10;

use RDF::NS;

sub new {
    bless {}, shift;
}

sub run {
    my ($self, @ARGV) = @_;
    my $format = '';

    return $self->usage if !@ARGV or $ARGV[0] =~ /^(-[?h]|--help)$/;
    return $self->version if $ARGV[0] =~ /^(-v|--version)$/;

    my $ns = RDF::NS->new;
    my $sn = $ns->REVERSE;

    foreach my $a (@ARGV) {
        if ( $a =~ /^([0-9]{8})$/ ) {
            $ns = RDF::NS->new($a);
            $sn = $ns->REVERSE;
            next;
        }
        if ( $a =~ qr{^https?://} ) {
            my $qname = $sn->qname($a);
            if ($qname) {
                $qname =~ s/:$//;
                say $qname;
            }
        } elsif ( $a =~ /:/ ) {
            print map { $ns->URI($_)."\n" } split(/[|, ]+/, $a);
        } elsif ( $a =~ s/\.([^.]+)$// ) {
            my $f = $1;
            if ( $f eq 'prefix' ) {
               print map { "$_\n" if defined $_ } map {
                   $sn->{$_}
               } $ns->FORMAT( $format, $a );
               next;
            } elsif ( $f =~ $RDF::NS::FORMATS ) {
                $format = $f;
            } else {
                print STDERR "Unknown format: $f\n";
            }
        }
        if ( lc($format) eq 'json' ) {
            say join ",\n", $ns->FORMAT( $format, $a );
        } else {
            say $_ for $ns->FORMAT( $format, $a );
        }
    }
}

sub usage {
    print <<'USAGE';
USAGE: rdfns { [YYYYMMDD] ( <prefix[es]>[.format] | prefix:name | URL ) }+

  formats: txt, sparql, ttl, n3, xmlns, json, beacon, prefix
  options: --help | --version
 
  examples:
    rdfns 20111102 foaf,owl.ttl
    rdfns foaf.xmlns foaf.n3
    rdfns rdfs:seeAlso
    rdfns http://www.w3.org/2003/01/geo/wgs84_pos#
    rdfns http://purl.org/dc/elements/1.1/title
    rdfns wgs.prefix
USAGE
    0;
}

sub version {
    print $RDF::NS::VERSION . "\n";
    0;
}

1;

__END__

=pod

=encoding UTF-8

=head1 NAME

App::rdfns - quickly get common URI namespaces

=head1 VERSION

version 20140910

=head1 SEE ALSO

This module implements the command line client L<rdfns>.

=head1 AUTHOR

Jakob Voß

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2013 by Jakob Voß.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.

=cut