File Coverage

blib/lib/SemanticWeb/OAI/ORE/TriX.pm
Criterion Covered Total %
statement 10 12 83.3
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 14 16 87.5


line stmt bran cond sub pod time code
1             package SemanticWeb::OAI::ORE::TriX;
2             #$Id: TriX.pm,v 1.13 2010-12-06 14:44:15 simeon Exp $
3              
4             =head1 NAME
5              
6             SemanticWeb::OAI::ORE::TriX - Parse/serialize OAI-ORE Resource Maps in TriX format
7              
8             =head1 SYNPOSIS
9              
10             Module to parse and serialize OAI-ORE ReMs in TriX format.
11              
12             =head1 DESCRIPTION
13              
14             The TriX format is describe at L
15              
16             =cut
17              
18 1     1   1956 use strict;
  1         2  
  1         42  
19 1     1   5 use warnings;
  1         2  
  1         25  
20 1     1   5 use Carp;
  1         2  
  1         67  
21              
22 1     1   43 use SemanticWeb::OAI::ORE::ReM;
  0            
  0            
23             use XML::Writer;
24              
25             =head1 METHODS
26              
27             =head2 Creator
28              
29             =head3 new(%args)
30              
31             =cut
32              
33             sub new {
34             my $class=shift;
35             my $self={@_};
36             bless $self, (ref($class) || $class);
37             return($self);
38             }
39              
40             =head2 Output
41              
42             =head3 serialize()
43              
44             Seialize in TriX format, returns serializetion as a string.
45              
46             =cut
47              
48             sub serialize {
49             my $self=shift;
50             my $out='';
51             my $rem=$self->{rem};
52             if (ref($rem) and $rem->isa('SemanticWeb::OAI::ORE::ReM')) {
53             # Get the info from the ReM
54             my $name=$rem->name();
55              
56             # create an XML writer
57             my $writer=XML::Writer->new('OUTPUT'=>\$out,'DATA_MODE'=>1,'DATA_INDENT'=>1);
58             $writer->startTag('trix',
59             'xmlns'=>'http://www.w3.org/2004/03/trix/trix-1/',
60             'xmlns:ore'=>'http://www.openarchives.org/ORE');
61             # http://www.w3.org/2004/03/trix/trix-1/trix-1.0.xsd
62             $writer->startTag('graph');
63              
64             # write name
65             $writer->dataElement('uri',$name);
66            
67             # write metadata
68             $writer->comment('required metadata');
69             write_triple($writer,$name,'ore:remTimeStamp',$rem->timestamp_as_iso8601());
70             write_triple($writer,$name,'ore:remAuthority',$rem->authority());
71              
72             # write aggregated resources
73             $writer->comment('aggregated resources');
74             foreach my $uri ($rem->aggregated_resources()) {
75             write_triple($writer,$name,'ore:aggregates',$uri);
76             }
77            
78             # write relations
79             $writer->comment('rels');
80             foreach my $rel (@{$rem->rels->as_array()}) {
81             write_triple($writer,@$rel);
82             }
83              
84             $writer->endTag('graph');
85             $writer->endTag('trix');
86             $writer->end();
87             } else {
88             carp "Can't serialize something that isn't a rem: $rem, ".ref($rem)."\n";
89             }
90             return($out);
91             }
92              
93             =head2 SUBROUTINES
94              
95             =head3 write_triple($writer,$subject,$predicate,$object)
96              
97             Write the ($subject,$predicate,$object) to $writer.
98              
99             =cut
100              
101             sub write_triple {
102             my ($writer,$subject,$predicate,$object)=@_;
103             $writer->startTag('triple');
104             # Subject and object may be id, uri, plainLiteral or typedLiteral
105             $writer->dataElement('uri',$subject);
106             $writer->dataElement('uri',$predicate);
107             $writer->dataElement('uri',$object);
108             $writer->endTag('triple');
109             }
110              
111             =head1 SEE ALSO
112              
113             L
114              
115             =head1 AUTHORS
116              
117             Simeon Warner
118              
119             =head1 LICENSE AND COPYRIGHT
120              
121             Copyright 2007-2010 Simeon Warner.
122              
123             This module is free software; you can redistribute it and/or
124             modify it under the same terms as Perl itself. See L.
125              
126             =cut
127              
128             1;