Skip to content

Commit

Permalink
new method SET to add/modify mappings
Browse files Browse the repository at this point in the history
  • Loading branch information
nichtich committed Oct 2, 2013
1 parent f355996 commit de7e5e8
Showing 1 changed file with 26 additions and 14 deletions.
40 changes: 26 additions & 14 deletions lib/RDF/NS.pm
Expand Up @@ -33,30 +33,37 @@ sub new {
croak "prefix file or date not found: $from"
unless -f $from;

my $ns = { };
my $self = bless { }, (ref($class) || $class);

open (my $fh, '<', $from) or croak "failed to open $from";
foreach (<$fh>) {
chomp;
next if /^#/;
my ($prefix, $namespace, $date) = split "\t", $_;
last if $date and $at ne 'any' and $date > $at;

if ( $prefix =~ /^(isa|can|new|uri)$/ ) {
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'";
}
$self->SET( $prefix => $namespace, $warn );
}
close($fh);

bless $ns, (ref($class) || $class);
$self;
}

sub SET {
my ($self, $prefix, $namespace, $warn) = @_;

if ( $prefix =~ /^(isa|can|new|uri)$/ ) {
carp "Cannot support prefix '$prefix'" if $warn;
next;
} elsif ( $prefix =~ /^[a-z][a-z0-9]*$/ ) {
if ( $namespace =~ /^[a-z][a-z0-9]*:[^"<>]*$/ ) {
$self->{$prefix} = $namespace;
} elsif( $warn ) {
carp "Skipping invalid $prefix namespace $namespace";
}
} elsif ( $warn ) {
carp "Skipping unusual prefix '$prefix'";
}
}

*LOAD = *new;
Expand Down Expand Up @@ -367,6 +374,11 @@ or separated by commas, vertical bars, and spaces.
=head1 INTERNAL METHODS
=head2 SET ( $prefix => $namespaces [, $warn ] )
Set or add a namespace mapping. Errors are ignored unless enabled as warnings
with the third argument.
=head2 MAP ( $code [, prefix[es] ] )
Internally used to map particular or all prefixes. Prefixes can be selected as
Expand Down

0 comments on commit de7e5e8

Please sign in to comment.