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 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
|
package RDF::Helper::Properties;
use warnings;
use strict;
use RDF::Trine;
use RDF::Trine qw(iri variable statement);
use Scalar::Util qw(blessed);
# Uhm, well, this should probably also be a role at some point...
# Next line is a workaround to problem documented in Error.pm#COMPATIBILITY
BEGIN { require Moose; Moose->import; *with_role = *with; undef *with };
use Error qw(:try);
=head1 NAME
RDF::Helper::Properties - Module that provides shortcuts to retrieve certain information
=head1 VERSION
Version 0.10
=cut
our $VERSION = '0.10';
=head1 SYNOPSIS
my $pred = RDF::Helper::Properties->new($model);
print $pred->title
=head1 METHODS
=over
=item C<< new ( model => $model ) >>
Constructor for getting predicates from a model, which is passed to the constructor.
=item C<< model >>
Will return the current model.
=cut
has model => (is => 'ro', required => 1, isa => 'RDF::Trine::Model');
has _cache => (is => 'rw', default => sub { {
title => {
'<http://www.w3.org/2000/01/rdf-schema#label>' => 'label',
'<http://www.w3.org/1999/02/22-rdf-syntax-ns#type>' => 'type',
},
pred => {
'<http://www.w3.org/2000/01/rdf-schema#label>' => 'label',
'<http://www.w3.org/1999/02/22-rdf-syntax-ns#type>' => 'type',
'<http://purl.org/dc/elements/1.1/type>' => 'Type',
},
} }
);
# foreach (1 .. 50) {
# $self->{_cache}{pred}{"<http://www.w3.org/1999/02/22-rdf-syntax-ns#_$_>"} = "#$_";
# }
=item C<< page ( $node ) >>
A suitable page to redirect to, based on foaf:page or foaf:homepage
=cut
sub page {
my $self = shift;
my $node = shift;
throw Error -text => "Node argument needs to be a RDF::Trine::Node::Resource." unless ($node && $node->isa('RDF::Trine::Node::Resource'));
my $model = $self->model;
my @props = (
iri( 'http://xmlns.com/foaf/0.1/homepage' ),
iri( 'http://xmlns.com/foaf/0.1/page' ),
);
# optimistically assume that we'll get back a valid page on the first try
my $objects = $model->objects_for_predicate_list( $node, @props );
if (blessed($objects) && $objects->is_resource) {
return $objects->uri_value;
}
# Return the common link to ourselves
return $node->uri_value . '/page';
}
=item C<< title ( $node ) >>
A suitable title for the document will be returned, based on document contents
=cut
sub title {
my $self = shift;
my $node = shift;
my $nodestr = $node->as_string;
if (my $title = $self->_cache->{title}{$nodestr}) {
return $title;
} else {
my $model = $self->model;
my @label = (
iri( 'http://xmlns.com/foaf/0.1/name' ),
iri( 'http://purl.org/dc/terms/title' ),
iri( 'http://purl.org/dc/elements/1.1/title' ),
iri( 'http://www.w3.org/2000/01/rdf-schema#label' ),
);
{
# optimistically assume that we'll get back a valid name on the first try
my $name = $model->objects_for_predicate_list( $node, @label );
if (blessed($name) and $name->is_literal) {
my $str = $name->literal_value;
$self->_cache->{title}{$nodestr} = $str;
return $str;
}
}
# if that didn't work, continue to try to find a valid literal title node
my @names = $model->objects_for_predicate_list( $node, @label );
foreach my $name (@names) {
if ($name->is_literal) {
my $str = $name->literal_value;
$self->_cache->{title}{$nodestr} = $str;
return $str;
}
}
# and finally fall back on just returning a string version of the node
if ($node->is_resource) {
my $uri = $node->uri_value;
$self->_cache->{title}{$nodestr} = $uri;
return $uri;
} else {
my $str = $node->as_string;
$self->_cache->{title}{$nodestr} = $str;
return $str;
}
}
}
=item C<< description ( $node ) >>
A suitable description for the document will be returned, based on document contents
=cut
sub description {
my $self = shift;
my $node = shift;
my $model = $self->model;
my $iter = $model->get_statements( $node );
my @label = (
iri( 'http://www.w3.org/2000/01/rdf-schema#label' ),
# iri( 'http://purl.org/dc/elements/1.1/description' ),
);
my @desc;
while (my $st = $iter->next) {
my $p = $st->predicate;
my $ps;
if (my $pname = $self->_cache->{pred}{$p->as_string}) {
$ps = $pname;
} elsif (my $pn = $model->objects_for_predicate_list( $p, @label )) {
$ps = $self->html_node_value( $pn );
} elsif ($p->is_resource and $p->uri_value =~ m<^http://www.w3.org/1999/02/22-rdf-syntax-ns#_(\d+)$>) {
$ps = '#' . $1;
} else {
# try to turn the predicate into a qname and use the local part as the printable name
my $name;
try {
(my $ns, $name) = $p->qname;
} catch RDF::Trine::Error with {};
if ($name) {
my $title = _escape( $name );
$ps = $title;
} else {
$ps = _escape( $p->uri_value );
}
}
$self->_cache->{pred}{$p->as_string} = $ps;
my $obj = $st->object;
my $os = $self->html_node_value( $obj, $p );
push(@desc, [$ps, $os]);
}
return \@desc;
}
=item C<< html_node_value >>
Formats the nodes for HTML output.
=cut
sub html_node_value {
my $self = shift;
my $n = shift;
my $rdfapred = shift;
my $qname = '';
my $xmlns = '';
if ($rdfapred) {
try {
my ($ns, $ln) = $rdfapred->qname;
$xmlns = qq[xmlns:ns="${ns}"];
$qname = qq[ns:$ln];
} catch RDF::Trine::Error with {};
}
return '' unless (blessed($n));
if ($n->is_literal) {
my $l = _escape( $n->literal_value );
if ($qname) {
return qq[<span $xmlns property="${qname}">$l</span>];
} else {
return $l;
}
} elsif ($n->is_resource) {
my $uri = _escape( $n->uri_value );
my $title = _escape( $self->title( $n ) );
if ($qname) {
return qq[<a $xmlns rel="${qname}" href="${uri}">$title</a>];
} else {
return qq[<a href="${uri}">$title</a>];
}
} else {
return $n->as_string;
}
}
sub _escape {
my $l = shift;
for ($l) {
s/&/&/g;
s/</</g;
s/"/"/g;
}
return $l;
}
=item with_role
Is there just because of quirk in L<Error>, don't worry about it.
=back
=head1 AUTHOR
Most of the code was written by Gregory Todd Williams C<<
<gwilliams@cpan.org> >> for L<RDF::LinkedData::Apache>, but refactored
into this class for use by other modules by Kjetil Kjernsmo, C<<
<kjetilk at cpan.org> >>
=head1 COPYRIGHT & LICENSE
Copyright 2010 Gregory Todd Williams and ABC Startsiden AS.
This program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.
=cut
1;
|