ORE/ReM.pm

filter: Statementstmt 71.0% 150 / 211 Branchbran 56.6% 51 / 90 Conditioncond 35.0% 14 / 40 MC/DCmcdc 12.5% 3 / 24 Subroutinesub 75.6% 28 / 37 Podpod 100% 27 / 27 totaltotal 63.6% 273 / 429 CC 63 SCAR 24.6
CC
63
Cov
63%
timestamp_as_unix
72.0
parse
15.3
parseuri
14.1
CRAP 11.7
« ORE/Model.pm ↑ Summary ORE/N3.pm »
1 package SemanticWeb::OAI::ORE::ReM;
2 #$Id: ReM.pm,v 1.32 2010-12-07 16:38:29 simeon Exp $
3  
4 =head1 NAME
5  
6 SemanticWeb::OAI::ORE::ReM - Module implementing OAI-ORE Resource Map object
7  
8 =head1 SYNPOSIS
9  
10 This class is designed to provide convenient ways to interact with
11 OAI-ORE resource maps from Perl code. It as based around a data model
12 class L<SemanticWeb::OAI::ORE::Model> which is the RDF model and may be
13 accessed directly via $rem->model. The access methods here are intended
14 to hide the RDF and instead work more naturally with the constraints
15 and language of OAI-ORE.
16  
17 Written against the v1.0 OAI-ORE specification
18 (L<http://www.openarchives.org/ore/1.0/toc>).
19  
20 =head1 DESCRIPTION
21  
22 An ORE Resource Map is comprised of two things:
23  
24 1) a URI indicating its location
25  
26 2) an RDF graph expressing the relationship between an aggregation and
27 aggreted resources
28  
29 This class encapsulates these two things ($rem->uri and $rem->model),
30 some other useful informatino about paring and serialization methods,
31 and provides routines to create/read, update, and write the
32 resource map.
33  
34 =head2 CREATION OF A RESOURCE MAP
35  
36 For simple case where we have simply a set of aggregated resources
37 and minimal metadata is required:
38  
39 use SemanticWeb::OAI::ORE::ReM;
40  
41 my $rem=SemanticWeb::OAI::ORE::ReM->new('ar'=>['uri:1','uri:2']);
42 print $rem->serialize('rdfxml');
43  
44 =head2 PARSING A RESOURCE MAP
45  
46 use SemanticWeb::OAI::ORE::ReM;
47 my $rem=SemanticWeb::OAI::ORE::ReM->new;
48 $rem->parse('rdfxml',$rdfxml_string);
49  
50 =cut
51  
52 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
53 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
54 8 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
55  
56 8 ▶use SemanticWeb::OAI::ORE::Agent;
Subroutine BEGIN: called CC=1 SCAR=0.0
57 8 ▶use SemanticWeb::OAI::ORE::Constant qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
58 8 ▶use SemanticWeb::OAI::ORE::Model;
Subroutine BEGIN: called CC=1 SCAR=0.0
59 8 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
60 8 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
61  
62 8 ▶use base qw(Class::Accessor);
Subroutine BEGIN: called CC=1 SCAR=0.0
63 8 __PACKAGE__->mk_accessors(qw(default_format die_level warn_level));
64  
65 =head1 METHODS
66  
67 =head3 SemanticWeb::OAI::ORE::ReM->new(%args)
68  
69 Create a new Resource Map (ReM) object. The resource map is comprised
70 of a URI (URI-R) and a set of triples, the model.
71  
72 Any C<%args> supplied are used to set the object variables.
73 As a shorthand for construction argument C{ar} may be used to
74 provide an arrayref of aggregated resources which are added
75 using C<$rem->aggregated_resources($args{ar})>.
76  
77 =cut
78  
79 7 ▶sub new {
Subroutine new: called CC=3 SCAR=13.0
Pod documented
80 7 my $class=shift;
81 7 my $self={'uri'=>undef,
82 'uri_agg'=>undef,
83 'model'=>undef,
84 'io'=>{#'atom' =>'SemanticWeb::OAI::ORE::Atom',
85 'rdfxml' =>'SemanticWeb::OAI::ORE::RDFXML',
86 'trix' =>'SemanticWeb::OAI::ORE::TriX',
87 'n3' =>'SemanticWeb::OAI::ORE::N3'},
88 'default_format'=>'rdfxml',
89 'die_level'=>FATAL,
90 'warn_level'=>WARN,
91 @_};
92 7 ▶ bless $self, (ref($class) || $class);
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
070
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
93  
94 # As a shorthand we accept the {ar} parameter which may be
95 # an arrayref to a list of aggregated resources
96 7 ▶ if ($self->{ar}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"ar"}) 0 7 7
97 0 $self->aggregated_resources($self->{ar});
98 0 delete($self->{ar});
99 }
100  
101 7 return($self);
102 }
103  
104  
105 =head3 $rem->uri()
106  
107 Set or access the identity of the ReM (URI-R). This should be the first
108 thing set when building a Resource Map. The validity of the URI is checked
109 with C<check_valid_uri(..)>.
110  
111 =cut
112  
113 18 ▶sub uri {
Subroutine uri: called CC=2 SCAR=6.9
Pod documented
114 18 my $self=shift;
115 18 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 10 8 18
116 10 my $uri=shift;
117 10 $self->check_valid_uri($uri,'Resource Map URI');
118 10 $self->{uri}=$uri;
119 }
120 18 return($self->{uri});
121 }
122  
123  
124 =head3 $rem->model()
125  
126 Set or access the model (see L<SemanticWeb::OAI::ORE::Model>) of this Resource
127 Map. It is usually expected that ORE specific accessor methods such
128 as L<aggregated_resources>, L<creator> and such will be used to
129 add data to the model when building a resource map.
130  
131 See L<SemanticWeb::OAI::ORE::Model> for details of how the model may be
132 accessed and manipulated, and also methods that follow for convenient
133 accessors to ORE elements.
134  
135 Will always return the reference to the model object so one can do things
136 such as:
137  
138 $rem->model->add($ref_statement);
139  
140 =cut
141  
142 37 ▶sub model {
Subroutine model: called CC=6 SCAR=18.4
Pod documented
143 37 my $self=shift;
144 37 ▶ if (@_ or not $self->{model}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ or not $self->{"model"}) 7 30 37
Condition100%3/3
@_ or not $self->{"model"}
l!l&&r!l&&!r
6130
MC/DC100%2/2
@_ or not $self->{"model"}
@_ not $self->{"model"}
Truth table100%3/3
A or not(B)
A= @_B= not $self->{"model"}
ABresult
1X1
011
000
145 7 my $model=$_[0];
146 7 ▶ if (ref($model) and $model->isa('SemanticWeb::OAI::ORE::Model')) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $model and $model->isa('SemanticWeb::OAI::ORE::Model')) { } 0 7 7
Condition66%2/3
ref $model and $model->isa("SemanticWeb::OAI::ORE::Model")
!ll&&!rl&&r
430
MC/DC0%0/2
ref $model and $model->isa("SemanticWeb::OAI::ORE::Model")
ref $model $model->isa("SemanticWeb::OAI::ORE::Model")
Truth table66%2/3
A and B
A= ref $modelB= $model->isa("SemanticWeb::OAI::ORE::Model")
ABresult
0X0
100
111
147 0 $self->{model}=$model;
148 } else {
149 7 ▶ if ($self->{model}=SemanticWeb::OAI::ORE::Model->new(@_)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"model"} = "SemanticWeb::OAI::ORE::Model"->new(@_)) 7 0 7
150 7 $self->{model}->{errobj}=$self;
151 }
152 }
153 }
154 37 return($self->{model});
155 }
156  
157  
158 =head3 $rem->aggregation()
159  
160 Set or access the URI of the Aggregation described by this ReM (URI-A).
161 Must be set after the URI of the Resource Map (URI-R) and would usually
162 be the second thing set when building a resource map from scratch.
163  
164 WARNING - this routine does not have the facility to update all occurrences
165 in the model if changed when other statements (e.g. aggregated resources
166 or metadata) have been added that reference the aggregation.
167  
168 =cut
169  
170 18 ▶sub aggregation {
Subroutine aggregation: called CC=2 SCAR=6.9
Pod documented
171 18 my $self=shift;
172 18 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 3 15 18
173 3 my $aggregation=shift;
174 3 $self->check_valid_uri($aggregation,'Aggregation URI');
175 3 $self->{uri_agg}=$aggregation;
176 3 $self->model->add_rel_to_resource($self->{uri},DESCRIBES,$aggregation);
177 }
178 18 return($self->{uri_agg});
179 }
180  
181  
182 =head3 $rem->creators()
183  
184 Set or access the creator of the ReM. Returns If there is more than one creator then
185 the first will be returned. Returns nothing if there is no creator set.
186  
187 See L<http://www.openarchives.org/ore/1.0/datamodel#Metadata_about_the_ReM>:
188  
189 The identity of the authoring authority (human, organization, or agent) of
190 the Resource Map, using the dcterms:creator predicate, with an object that MUST
191 be a reference to a Resource of type L<http://purl.org/dc/terms/Agent>. This MAY
192 then be the subject of the following triples:
193  
194 * A triple with the predicate foaf:name and an object that is a text string
195 containing some descriptive name of the authoring authority.
196  
197 * A triple with the predicate foaf:mbox and an object that is a URI that is
198 the email address of the authoring authority.
199  
200 =cut
201  
202 1 ▶sub creators {
Subroutine creators: called CC=3 SCAR=11.7
Pod documented
203 1 my $self=shift;
204 1 foreach my $creator (@_) {
205 0 $self->model->add_rel_to_resource($self->{uri},CREATOR,$creator);
206 }
207 1 my @creators=();
208 1 foreach my $creator ($self->model->creators($self->{uri})) {
209 0 push(@creators,SemanticWeb::OAI::ORE::Agent->new(uri=>$creator->getURI,
210 name=>$self->model->literal_matching($creator,FOAF_NAME),
211 mbox=>$self->model->literal_matching($creator,FOAF_MBOX)));
212 }
213 1 return(@creators);
214 }
215  
216  
217 =head3 $rem->creator()
218  
219 Assumes one creator. Wrapper around $rem->creators() that does the same thing
220 except in the case where there are multiple creators it will return just the first.
221  
222 =cut
223  
224 1 ▶sub creator {
Subroutine creator: called CC=2 SCAR=7.1
Pod documented
225 1 my $self=shift;
226 1 my @creators=$self->creators;
227 1 ▶ return(@creators ? shift @creators : () );
Branch: 1 branch
BranchTrueFalseTotal
@creators ? : 0 1 1
228 }
229  
230  
231 =head3 $rem->creator_name()
232  
233 Set or access the creator as a URI of the ReM.
234  
235 =cut
236  
237 0 ▶sub creator_name {
Subroutine creator_name: not called CC=2 SCAR=17.9
Pod documented
238 0 my $self=shift;
239 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 0 0
240 0 $self->{creator_name}=shift;
241 0 $self->model->add_rel_to_literal($self->{uri},CREATOR,$self->{creator_name});
242 } else {
243  
244 }
245 0 return($self->{creator_name});
246 }
247  
248  
249 =head3 $rem->timestamp_as_unix($type,$timestamp)
250  
251 Set or access the option creation timestamp of the ReM. Returns
252 now if not set. Type should be either CREATED or MODIFIED constant.
253 Usually called via created_as_unix() or modified_as_unix() wrappers.
254  
255 Will set if timestamp if C<$timestamp> is defined.
256  
257 =cut
258  
259 0 ▶sub timestamp_as_unix {
Subroutine timestamp_as_unix: not called CC=8 SCAR=42.8
Pod documented
260 0 my $self=shift;
261 0 my ($type,$timestamp)=@_;
262 0 ▶ if (not defined $type or ($type ne CREATED and $type ne MODIFIED)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $type or $type ne "http://purl.org/dc/terms/created" and $type ne "http://purl.org/dc/terms/modified") 0 0 0
Condition0%0/6
not defined $type or $type ne "http://purl.org/dc/terms/created" and $type ne "http://purl.org/dc/terms/modified"
l!l&&r!l&&!r
000
$type ne "http://purl.org/dc/terms/created" and $type ne "http://purl.org/dc/terms/modified"
!ll&&!rl&&r
000
MC/DC0%0/3
not defined $type or $type ne "http://purl.org/dc/terms/created" and $type ne "http://purl.org/dc/terms/modified"
not defined $type $type ne "http://purl.org/dc/terms/created" $type ne "http://purl.org/dc/terms/modified"
Truth table0%0/4
not(A) or B and C
A= not defined $typeB= $type ne "http://purl.org/dc/terms/created"C= $type ne "http://purl.org/dc/terms/modified"
ABCresult
1XX1
0111
00X0
0100
263 0 confess "OOPS - call to timestamp_as_unix(type) without valid type (got: $type)";
264 }
265 0 ▶ if (defined $timestamp) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $timestamp) 0 0 0
266 0 ▶ if ($self->{timestamp_iso8601}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"timestamp_iso8601"}) 0 0 0
267 0 carp "WARNING - Already have ISO8601 timestamp set";
268 }
269 0 $self->{timestamp_unix}=$timestamp;
270 }
271 0 ▶ if ($self->{timestamp_iso8601}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'timestamp_iso8601'}) { } 0 0 0
272 0 croak "OOPS .. haven't implemented conversion of ISO8601 to unix time";
273 } else {
274 0 ▶ return($self->{timestamp_unix} || time());
Condition0%0/2
$self->{'timestamp_unix'} || time
l!l
00
MC/DC0%0/1
$self->{'timestamp_unix'} || time
$self->{'timestamp_unix'}
Truth table0%0/2
A
A= $self->{'timestamp_unix'}
Aresult
11
00
275 }
276 }
277  
278 =head3 $rem->created_as_unix($timestamp)
279  
280 Set or access the creation timestamp of the ReM as a Unix timestamp.
281  
282 Will set timestamp if C<$timestamp> is defined.
283  
284 =cut
285  
286 0 ▶sub created_as_unix {
Subroutine created_as_unix: not called CC=1 SCAR=6.9
Pod documented
287 0 my $self=shift;
288 0 return(timestamp_as_unix($self,CREATED,@_));
289 }
290  
291  
292 =head3 $rem->modified_as_unix($timestamp)
293  
294 Set or access the modification timestamp of the ReM as a Unix timestamp.
295  
296 Will set timestamp if C<$timestamp> is defined.
297  
298 =cut
299  
300 0 ▶sub modified_as_unix {
Subroutine modified_as_unix: not called CC=1 SCAR=6.9
Pod documented
301 0 my $self=shift;
302 0 return(timestamp_as_unix($self,MODIFIED,@_));
303 }
304  
305  
306
307 =head3 $rem->timestamp_as_iso8601($type,$timestamp)
308  
309 Set or access the timestamp of the rem as an ISO8601 string.
310 Type should be either CREATED or MODIFIED constant.
311 Usually called via created_as_iso8601() or modified_as_iso8601() wrappers.
312  
313 Will set timetstamp if C<$timestamp> is defined.
314  
315 =cut
316  
317 2 ▶sub timestamp_as_iso8601 {
Subroutine timestamp_as_iso8601: called CC=7 SCAR=23.6
Pod documented
318 2 my $self=shift;
319 2 my ($type,$timestamp)=@_;
320 2 ▶ if (not defined $type or ($type ne CREATED and $type ne MODIFIED)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $type or $type ne "http://purl.org/dc/terms/created" and $type ne "http://purl.org/dc/terms/modified") 0 2 2
Condition33%2/6
not defined $type or $type ne "http://purl.org/dc/terms/created" and $type ne "http://purl.org/dc/terms/modified"
l!l&&r!l&&!r
002
$type ne "http://purl.org/dc/terms/created" and $type ne "http://purl.org/dc/terms/modified"
!ll&&!rl&&r
020
MC/DC0%0/3
not defined $type or $type ne "http://purl.org/dc/terms/created" and $type ne "http://purl.org/dc/terms/modified"
not defined $type $type ne "http://purl.org/dc/terms/created" $type ne "http://purl.org/dc/terms/modified"
Truth table25%1/4
not(A) or B and C
A= not defined $typeB= $type ne "http://purl.org/dc/terms/created"C= $type ne "http://purl.org/dc/terms/modified"
ABCresult
1XX1
0111
00X0
0100
321 0 confess "OOPS - call to timestamp_as_iso8601(type) without valid type (got: $type)";
322 }
323 2 ▶ return(undef) if ($type eq CREATED); #FIXME - not yet implemented
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "http://purl.org/dc/terms/created" 0 2 2
324 2 ▶ if (defined $timestamp) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $timestamp) 2 0 2
325 2 ▶ if ($self->{timestamp_unix}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"timestamp_unix"}) 0 2 2
326 0 carp "WARNING - Already have unix timestamp set";
327 }
328 2 $self->{timestamp_iso8601}=$timestamp;
329 }
330 2 return($self->{timestamp_iso8601});
331 }
332  
333  
334 =head3 $rem->now_as_iso8601()
335  
336 Returns the current system time as an iso8601 string
337  
338 =cut
339  
340 0 ▶sub now_as_iso8601 {
Subroutine now_as_iso8601: not called CC=1 SCAR=6.9
Pod documented
341 0 my $self=shift;
342 0 my $dt=DateTime->from_epoch(time());
343 0 return($dt->iso8601().'Z');
344 }
345  
346  
347 =head3 $rem->created_as_iso8601($timestamp)
348  
349 Set or access the creation timestamp of the ReM as a ISO8601 timestamp.
350  
351 Will set timestamp if C<$timestamp> is defined.
352  
353 =cut
354  
355 0 ▶sub created_as_iso8601 {
Subroutine created_as_iso8601: not called CC=1 SCAR=6.9
Pod documented
356 0 my $self=shift;
357 0 return(timestamp_as_iso8601($self,CREATED,@_));
358 }
359  
360  
361 =head3 $rem->modified_as_iso8601($timestamp)
362  
363 Set or access the creation timestamp of the ReM as a ISO8601 timestamp.
364  
365 Will set timestamp if C<$timestamp> is defined.
366  
367 =cut
368  
369 0 ▶sub modified_as_iso8601 {
Subroutine modified_as_iso8601: not called CC=1 SCAR=6.9
Pod documented
370 0 my $self=shift;
371 0 return(timestamp_as_iso8601($self,MODIFIED,@_));
372 }
373  
374  
375 =head3 $rem->aggregation_metadata($predicate,$only)
376  
377 If C<$only> is not specified then the objects matching will be returned.
378  
379 If C<$only> is C<RESOURCE> then only resource labels will be included, if
380 C<LITERAL> then only literal labels will be returned.
381  
382 =cut
383  
384 1 ▶sub aggregation_metadata {
Subroutine aggregation_metadata: called CC=2 SCAR=7.6
Pod documented
385 1 my $self=shift;
386 1 my ($predicate,$only)=@_;
387 # Cannot do anything if we do not have an aggregation
388 1 ▶ return() if (not $self->aggregation);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->aggregation 1 0 1
389 # We do, search for all matching objects
390 0 return( $self->model->objects_matching($self->aggregation,$predicate,$only) );
391 }
392  
393  
394 =head3 $rem->aggregation_metadata_literal($predicate)
395  
396 Wrapper for C<$rem->aggregation_metadata> that will take just the first
397 matching literal, or return undef if there a no matches.
398  
399 =cut
400  
401 1 ▶sub aggregation_metadata_literal {
Subroutine aggregation_metadata_literal: called CC=2 SCAR=7.6
Pod documented
402 1 my $self=shift;
403 1 my ($predicate)=@_;
404 # Cannot do anything if we donot have an aggregation
405 1 ▶ return() if (not $self->aggregation);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->aggregation 1 0 1
406 # We do, get first matching literal
407 0 return( $self->model->literal_matching($self->aggregation,$predicate) );
408 }
409  
410  
411 =head3 $rem->aggregated_resources()
412  
413 Set or access the aggregated resources list.
414  
415 $rem->aggregated_resources('uri:3');
416  
417 This method will never remove an aggregated resource from this ReM. Use
418 delete_aggregated_resources to remove or clear the set of
419 aggregated resources for this ReM. Returns a list of all the
420 aggregated resources.
421  
422 =cut
423  
424 2 ▶sub aggregated_resources {
Subroutine aggregated_resources: called CC=2 SCAR=6.9
Pod documented
425 2 my $self=shift;
426 2 foreach my $ar (@_) {
427 2 $self->model->add_rel_to_resource($self->aggregation,AGGREGATES,$ar);
428 }
429 # now return the list of all aggregated resources
430 2 return($self->model->objects_matching($self->aggregation,AGGREGATES,RESOURCE));
431 }
432  
433  
434 =head3 $rem->rights()
435  
436 Set of access the rights for this resource map. Permits only
437 one rights statement to be associated with the resource map.
438 Returns undef if no rights value is set.
439  
440 =cut
441  
442 0 ▶sub rights {
Subroutine rights: not called CC=3 SCAR=24.8
Pod documented
443 0 my $self=shift;
444 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
445 0 my $rights=shift(@_);
446 0 $self->model->add_rel_to_resource($self->{uri},RIGHTS,$rights);
447 }
448 # now return the current rights URI
449 0 my @rights=$self->model->objects_matching($self->{uri},RIGHTS,RESOURCE);
450 0 ▶ return($rights[0] ? $rights[0] : undef);
Branch: 1 branch
BranchTrueFalseTotal
$rights[0] ? : 0 0 0
451 }
452  
453  
454 =head3 $rem->is_valid
455  
456 Run validation checks on the resource map model. Returns true (1) on succes,
457 false (nothing) on failures. Errors set in errstr.
458  
459 =cut
460  
461 3 ▶sub is_valid {
Subroutine is_valid: called CC=2 SCAR=6.9
Pod documented
462 3 my $self=shift;
463  
464 3 eval {
465 3 $self->model->check_model($self->uri,$self);
466 };
467 3 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 1 2 3
468 # Oops
469 1 $self->add_errstr("Invalid resource map: $@");
470 1 return;
471 }
472 2 return(1);
473 }
474  
475  
476  
477 #####################################################################
478  
479 =head2 INPUT AND OUTPUT METHODS
480  
481 =head3 $rem->parse($format,$src,$uri_rem)
482  
483 Parse resource C<$uri_rem>. Get it from C<$src>, where C<$src> may be
484 either a string containing the representation to be parsed,
485 or an open filehandle. If C<$src> is not set then attempt to
486 download from C<$uri_rem> using C<parseuri()>.
487  
488 To parse a file directly, use the C<parsefile()> wrapper. To parse
489 a URI directly, use the C<parseuri()> wrapper.
490  
491 Will run validation checks on the resource map model obtained. Set
492 C<$rem->die_level(RECKLESS)> to ignore errors.
493  
494 Will return true (1) on success, false (undef) on failure. Will
495 have set errstr on failure.
496  
497 =cut
498  
499 6 ▶sub parse {
Subroutine parse: called CC=14 SCAR=27.3
Pod documented
500 6 my $self=shift;
501 6 my ($format,$src,$uri_rem)=@_;
502  
503 6 ▶ if (not defined $src and defined $uri_rem) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $src and defined $uri_rem) 0 6 6
Condition33%1/3
not defined $src and defined $uri_rem
!ll&&!rl&&r
600
MC/DC0%0/2
not defined $src and defined $uri_rem
not defined $src defined $uri_rem
Truth table33%1/3
not(A) and B
A= not defined $srcB= defined $uri_rem
ABresult
0X0
100
111
504 # Try to get from URI
505 0 return($self->parseuri($format,$uri_rem));
506 }
507  
508 6 my $input_uri_rem=$uri_rem;
509 6 ▶ $uri_rem='http://unknown.example.org/' if (not defined $uri_rem);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $uri_rem 4 2 6
510  
511 6 my $model=undef;
512 6 ▶ if (my $io_class=$self->{io}{$format}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $io_class = $self->{'io'}{$format}) { } 6 0 6
513 eval {
514 6 ▶ if (not eval("require $io_class")) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eval "require $io_class") 3 3 6
515 3 croak "Failed to load class $io_class: $@";
516 }
517 3 my $reader=$io_class->new(%$self,'rem'=>$self);
518 #print "DEBUG ".__PACKAGE__."::parse: using reader: $reader isa ".ref($reader)."\n";
519 3 $model=$reader->parse($src,$uri_rem);
520 6 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { unless (eval "require $io_class") { croak("Failed to load class ${io_class}: $@"); } ; my $reader = $io_class->new(%$self, "rem", $self); $model = $reader->parse($src, $uri_rem) } } or do { carp('Error trying to parse (' . ($uri_rem ? $uri_rem : "ReM URI unknown") . ") in '${format}' using class '${io_class}': $@\n"); unless ($self->errstr) { $self->errstr('Error parsing (' . ($uri_rem ? $uri_rem : "ReM URI unknown") . ')'); } } 3 3 6
521 3 ▶ carp "Error trying to parse (".($uri_rem?$uri_rem:'ReM URI unknown').") in '$format' using class '$io_class': $@\n";
Branch: 1 branch
BranchTrueFalseTotal
$uri_rem ? : 3 0 3
522 3 ▶ if (not $self->errstr) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->errstr) 3 0 3
523 3 ▶ $self->errstr("Error parsing (".($uri_rem?$uri_rem:'ReM URI unknown').")");
Branch: 1 branch
BranchTrueFalseTotal
$uri_rem ? : 3 0 3
524 }
525 };
526 } else {
527 0 croak "Unknown serialization format to parse '$format' in parse(..)\n";
528 }
529  
530 # Now have model in $model, connect as model of this Resource Map
531 # and check that it is valid.
532 6 $model=$self->model($model);
533  
534 # If we didn't know the ReM URI to start, introspect to find it
535 6 ▶ if ($input_uri_rem) {
Branch: 1 branch
BranchTrueFalseTotal
if ($input_uri_rem) { } 2 4 6
536 2 $self->uri($input_uri_rem);
537 } else {
538 4 ▶ if (my $uri=$self->model->find_rem_uri) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $uri = $self->model->find_rem_uri) 3 1 4
539 3 $self->uri($uri);
540 }
541 }
542  
543 # If we didn't know the Aggregation URI to start, introspect to find it
544 6 ▶ if (not $self->aggregation) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->aggregation) 6 0 6
545 6 ▶ if (my $agg=$self->model->find_aggregation_uri) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $agg = $self->model->find_aggregation_uri) 3 3 6
546 3 $self->{uri_agg}=$agg;
547 }
548 }
549  
550 # Validate?
551 #$self->is_valid;
552  
553 6 return(1);
554 }
555  
556  
557 =head3 $rem->parsefile($format,$file,$uri_rem)
558  
559 Wrapper for X<$rem->parse($format,$uri_rem,$src)> which does nothing with
560 C<$format> and C<$uri_rem> but opens C<$file> and passes the reulting filehandle
561 on to C<$rem->parse(...)>. Returns C<undef> if the file cannot be opened, otherwise
562 return values as for C<$rem->parse(...)>.
563  
564 =cut
565  
566 5 ▶sub parsefile {
Subroutine parsefile: called CC=2 SCAR=7.3
Pod documented
567 5 my $self=shift;
568 5 my ($format,$file,$uri_rem)=@_;
569 5 my $srcfh=IO::File->new();
570 5 ▶ if ($srcfh->open($file,'<')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($srcfh->open($file, "<")) 5 0 5
571 5 my $retval=$self->parse($format,$srcfh,$uri_rem);
572 5 close($srcfh);
573 5 return($retval);
574 }
575 0 $self->errstr("Can't open source file '$file'");
576 0 return(undef);
577 }
578  
579  
580 =head3 $rem->parseuri($format,$uri_rem)
581  
582 Simple wrapper for X<$rem->parse($format,$uri_rem,$src)> that
583 downloads C<$uri_rem> with L<LWP::Simple> before passing it on.
584 Returns C<undef> if C<$uri_rem> cannot be downloaded, otherwise
585 return values as for C<$rem->parse(...)>.
586  
587 =cut
588  
589 0 ▶sub parseuri {
Subroutine parseuri: not called CC=4 SCAR=26.4
Pod documented
590 0 my $self=shift;
591 0 my ($format,$uri_rem)=@_;
592  
593 0 ▶ if (not defined $uri_rem) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $uri_rem) 0 0 0
594 0 croak("Attempt to call parseuri without a URI");
595 }
596  
597 # Wrap in eval so we can run this module without LWP::Simple if
598 # this method is not required
599 0 my $src_string;
600 0 eval {
601 8 ▶ use LWP::Simple (); #do not import
Subroutine BEGIN: called CC=1 SCAR=0.0
602 0 $src_string=LWP::Simple::get($uri_rem);
603 };
604 0 ▶ if ($@ or not defined $src_string) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@ or not defined $src_string) 0 0 0
Condition0%0/3
$@ or not defined $src_string
l!l&&r!l&&!r
000
MC/DC0%0/2
$@ or not defined $src_string
$@ not defined $src_string
Truth table0%0/3
A or not(B)
A= $@B= not defined $src_string
ABresult
1X1
011
000
605 0 $self->errstr("Can't get ReM from $uri_rem: $@");
606 0 return(undef);
607 }
608
609 0 return($self->parse($format,$src_string,$uri_rem));
610 }
611  
612  
613 =head3 $rem->serialize()
614  
615 Serialize in default format which has accessor C<$rem->default_format>.
616  
617 =head3 $rem->serialize($format)
618  
619 Serialize in C<$format>. This will use and call the appropriate
620 writer class.
621  
622 =cut
623  
624 2 ▶sub serialize {
Subroutine serialize: called CC=4 SCAR=15.2
Pod documented
625 2 my $self=shift;
626 2 ▶ my $format=shift || $self->{default_format};
Condition33%1/3
shift() || $self->{'default_format'}
l!l&&r!l&&!r
200
MC/DC0%0/2
shift() || $self->{'default_format'}
shift() $self->{'default_format'}
Truth table33%1/3
A || B
A= shift()B= $self->{'default_format'}
ABresult
1X1
011
000
627 2 my $out='';
628 2 ▶ if (my $io_class=$self->{io}{$format}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $io_class = $self->{'io'}{$format}) { } 2 0 2
629 eval {
630 2 eval("require $io_class");
631 2 my $writer=$io_class->new('rem'=>$self,@_);
632 #print "DEBUG ".__PACKAGE__."::serialize: using writer: $writer isa ".ref($writer)."\n";
633 2 $out=$writer->serialize();
634 2 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { eval "require $io_class"; my $writer = $io_class->new("rem", $self, @_); $out = $writer->serialize } } or do { carp("Error trying to serialize in '${format}' using class '${io_class}': $@\n") } 2 0 2
635 0 carp "Error trying to serialize in '$format' using class '$io_class': $@\n";
636 };
637 } else {
638 0 carp "Unknown serialization format '$format'\n";
639 }
640 2 return($out);
641 }
642  
643  
644 =head3 $rem->errstr($str) or $rem->errstr
645  
646 Resets the error string to C<$str> if C<$str> provided.
647  
648 Returns a string, either the error string if set, else ''.
649  
650 =cut
651  
652 8 ▶sub errstr {
Subroutine errstr: called CC=3 SCAR=11.0
Pod documented
653 8 my $self=shift;
654 8 my $str=shift;
655 8 ▶ $self->{errstr}=$str if (defined $str);
Branch: 1 branch
BranchTrueFalseTotal
if defined $str 3 5 8
656 8 ▶ return($self->{errstr}?$self->{errstr}:'');
Branch: 1 branch
BranchTrueFalseTotal
$self->{'errstr'} ? : 5 3 8
657 }
658  
659  
660 =head3 $rem->add_errstr($str)
661  
662 Add to the error string. Will append C<\n> if not present in C<$str>.
663  
664 =cut
665  
666 6 ▶sub add_errstr {
Subroutine add_errstr: called CC=2 SCAR=6.9
Pod documented
667 6 my $self=shift;
668 6 my ($str)=@_;
669 6 ▶ $str.="\n" if ($str!~/\n$/);
Branch: 1 branch
BranchTrueFalseTotal
unless $str =~ /\n$/ 5 1 6
670 6 $self->{errstr}.=$str;
671 }
672  
673  
674 =head3 $rem->err($level,$msg)
675  
676 Log and/or report an error C<$msg> at level C<$level>. Intended mainly
677 for internal use and use by particular format classes.
678  
679 =cut
680  
681 6 ▶sub err {
Subroutine err: called CC=6 SCAR=19.1
Pod documented
682 6 my $self=shift;
683 6 my ($level,$msg)=@_;
684 6 ▶ if ($self->die_level and $level>=$self->die_level) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->die_level and $level >= $self->die_level) { } 1 5 6
elsif ($self->warn_level and $level >= $self->warn_level) { } 5 0 5
Condition66%2/3
$self->die_level and $level >= $self->die_level
!ll&&!rl&&r
051
MC/DC50%1/2
$self->die_level and $level >= $self->die_level
$self->die_level $level >= $self->die_level
Truth table66%2/3
A and B
A= $self->die_levelB= $level >= $self->die_level
ABresult
0X0
100
111
685 1 croak "ERROR: $msg";
686 ▶ } elsif ($self->warn_level and $level>=$self->warn_level) {
Condition33%1/3
$self->warn_level and $level >= $self->warn_level
!ll&&!rl&&r
005
MC/DC0%0/2
$self->warn_level and $level >= $self->warn_level
$self->warn_level $level >= $self->warn_level
Truth table33%1/3
A and B
A= $self->warn_levelB= $level >= $self->warn_level
ABresult
0X0
100
111
687 5 ▶ my $code=ERROR_LEVEL->[$level] || 'UNKNOWN';
Condition50%1/2
['NO_ERROR', 'QUIET', 'WARN', 'FATAL']->[$level] || 'UNKNOWN'
l!l
50
MC/DC0%0/1
['NO_ERROR', 'QUIET', 'WARN', 'FATAL']->[$level] || 'UNKNOWN'
['NO_ERROR', 'QUIET', 'WARN', 'FATAL']->[$level]
Truth table50%1/2
A
A= ['NO_ERROR', 'QUIET', 'WARN', 'FATAL']->[$level]
Aresult
11
00
688 5 $self->add_errstr("[$code] $msg");
689 } #else ignore!
690 }
691  
692  
693 =head3 $rem->check_valid_uri($uri,$description)
694  
695 Check that the supplied C<$uri> is valid and create C<$rem->err> if not.
696 Returns true if valid, false otherwise.
697  
698 =cut
699  
700 13 ▶sub check_valid_uri {
Subroutine check_valid_uri: called CC=3 SCAR=11.4
Pod documented
701 13 my $self=shift;
702 13 my ($uri,$description)=@_;
703 13 ▶ if (not defined $uri) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $uri) { } 0 13 13
elsif (not $uri =~ /^[a-z]+:\S+$/) { } 4 9 13
704 0 $self->err(WARN,"$description is not defined and thus not a valid URI");
705 0 return();
706 } elsif ($uri!~/^[a-z]+:\S+$/) {
707 4 $self->err(WARN,"$description ($uri) is not a valid URI");
708 4 return();
709 }
710 9 return(1);
711 }
712  
713  
714 =head1 SEE ALSO
715  
716 Details of the Open Archive Initiative, including both the OAI-ORE and
717 OAI-PMH specification are found at L<http://www.openarchives.org/>.
718  
719 This module is the primary class for support of OAI-ORE resource maps.
720 Other parts include:
721 L<SemanticWeb::OAI::ORE::Model>
722 L<SemanticWeb::OAI::ORE::N3>
723 L<SemanticWeb::OAI::ORE::Trix>
724  
725 Support for the OAI-PMH protocol is provided by other modules
726 including L<SemanticWeb::OAI::Harvester>.
727  
728 =head1 AUTHOR
729  
730 Simeon Warner, C<< <simeon at cpan.org> >>
731  
732 =head1 BUGS
733  
734 Support for Atom format output is not yet provided, this should be
735 L<SemanticWeb::OAI::ORE::Atom>.
736  
737 Please report any bugs or feature requests to
738 C<bug-net-oai-ore at rt.cpan.org>, or through the web interface at
739 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Net-OAI-ORE>.
740 I will be notified, and then you'll automatically be notified of progress on
741 your bug as I make changes.
742  
743 =head1 SUPPORT
744  
745 You can find documentation for this module with the perldoc command.
746  
747 perldoc SemanticWeb::OAI::ORE
748  
749 You can also look for information at:
750  
751 =over 4
752  
753 =item * AnnoCPAN: Annotated CPAN documentation
754  
755 L<http://annocpan.org/dist/Net-OAI-ORE>
756  
757 =item * CPAN Ratings
758  
759 L<http://cpanratings.perl.org/d/Net-OAI-ORE>
760  
761 =item * RT: CPAN's request tracker
762  
763 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Net-OAI-ORE>
764  
765 =item * Search CPAN
766  
767 L<http://search.cpan.org/dist/Net-OAI-ORE>
768  
769 =back
770  
771 =head1 COPYRIGHT & LICENSE
772  
773 Copyright 2007-2010 Simeon Warner.
774  
775 This program is free software; you can redistribute it and/or modify it
776 under the same terms as Perl itself.
777  
778 =cut
779  
780 8 1;
« ORE/Model.pm ↑ Summary ORE/N3.pm »