| 1 | package SemanticWeb::OAI::ORE::RDFXML; | ||
| 2 | #$Id: RDFXML.pm,v 1.6 2010-12-06 14:44:15 simeon Exp $ | ||
| 3 | |||
| 4 | =head1 NAME | ||
| 5 | |||
| 6 | SemanticWeb::OAI::ORE::RDFXML - Parse/serialize OAI-ORE Resource Maps in RDF/XML format | ||
| 7 | |||
| 8 | =head1 SYNPOSIS | ||
| 9 | |||
| 10 | Module to parse and serialize OAI-ORE resource maps in RDF/XML format. | ||
| 11 | See L<SemanticWeb::OAI::ORE>. | ||
| 12 | |||
| 13 | =cut | ||
| 14 | |||
| 15 | 5 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 16 | 5 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 17 | 5 | ▶ | use Carp; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 18 | |||
| 19 | 5 | ▶ | use SemanticWeb::OAI::ORE::ReM; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 20 | 5 | ▶ | use RDF::Core::Storage::Memory; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 21 | 5 | ▶ | use RDF::Core::Model; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 22 | 0 | ▶ | use RDF::Core::Model::Parser; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 23 | use RDF::Core::Model::Serializer; | ||
| 24 | |||
| 25 | =head1 METHODS | ||
| 26 | |||
| 27 | =head2 new() | ||
| 28 | |||
| 29 | Create a new RDFXML handler object. | ||
| 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 | |||
| 41 | =head2 parse($rdfxml,$uri_rem) | ||
| 42 | |||
| 43 | Parse input string or read from open filehandle $rdfxml which contains the | ||
| 44 | RDF/XML serialization of the ReM with base URI $uri_rem. A base URI must be | ||
| 45 | supplied otherwise the parser (L<RDF::Core::Model::Parser>) will fail. | ||
| 46 | |||
| 47 | Returns a L<RDF::Core::Model> model. | ||
| 48 | |||
| 49 | =cut | ||
| 50 | |||
| 51 | sub parse { | ||
| 52 | my $self=shift; | ||
| 53 | my ($rdfxml,$uri_rem)=@_; | ||
| 54 | |||
| 55 | # Read in file if necessary | ||
| 56 | if (ref($rdfxml)) { | ||
| 57 | # Assume filehandle | ||
| 58 | my $fh=$rdfxml; | ||
| 59 | local $/=undef; #to slurp file | ||
| 60 | $rdfxml=<$fh>; | ||
| 61 | } | ||
| 62 | |||
| 63 | my $storage=RDF::Core::Storage::Memory->new(); | ||
| 64 | my $model=RDF::Core::Model->new(Storage => $storage); | ||
| 65 | # The BaseURI must be supplied otherwise the parser will fail | ||
| 66 | my $parser=RDF::Core::Model::Parser->new(Model=>$model, | ||
| 67 | BaseURI=>$uri_rem, | ||
| 68 | Source=>$rdfxml, | ||
| 69 | SourceType=>'string'); | ||
| 70 | $parser->parse(); | ||
| 71 | return($model); | ||
| 72 | } | ||
| 73 | |||
| 74 | |||
| 75 | =head2 serialize() | ||
| 76 | |||
| 77 | Serialize resource map as RDF/XML. Returns serializetion | ||
| 78 | as a string. | ||
| 79 | |||
| 80 | See L<RDF::Core::Model::Serializer> and L<RDF::Core::Serializer>. | ||
| 81 | |||
| 82 | =cut | ||
| 83 | |||
| 84 | sub serialize { | ||
| 85 | my $self=shift; | ||
| 86 | my $out=''; | ||
| 87 | my $rem=$self->{rem}; | ||
| 88 | if (ref($rem) and $rem->isa('SemanticWeb::OAI::ORE::ReM')) { | ||
| 89 | # Get the info from the ReM | ||
| 90 | $out.="<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"; | ||
| 91 | $out.="<!-- OAI-ORE Resource Map (".$rem->uri.") -->\n"; | ||
| 92 | |||
| 93 | # Do not specify a BaseURI option as we do not want relative URIs in output | ||
| 94 | my $serializer = RDF::Core::Model::Serializer->new(Model=>$rem->model, | ||
| 95 | Output => \$out); | ||
| 96 | $serializer->serialize; | ||
| 97 | } else { | ||
| 98 | die "Can't serialize something that isn't a resource map: have a ".ref($rem)."\n"; | ||
| 99 | } | ||
| 100 | return($out); | ||
| 101 | } | ||
| 102 | |||
| 103 | |||
| 104 | =head1 SEE ALSO | ||
| 105 | |||
| 106 | L<SemanticWeb::OAI::ORE::ReM> | ||
| 107 | |||
| 108 | =head1 AUTHORS | ||
| 109 | |||
| 110 | Simeon Warner | ||
| 111 | |||
| 112 | =head1 LICENSE AND COPYRIGHT | ||
| 113 | |||
| 114 | Copyright 2007-2010 Simeon Warner. | ||
| 115 | |||
| 116 | This module is free software; you can redistribute it and/or | ||
| 117 | modify it under the same terms as Perl itself. See L<perlartistic>. | ||
| 118 | |||
| 119 | =cut | ||
| 120 | |||
| 121 | 1; | ||