File Coverage

blib/lib/RDF/YAML.pm
Criterion Covered Total %
statement 21 44 47.7
branch 1 10 10.0
condition 3 8 37.5
subroutine 8 14 57.1
pod 11 11 100.0
total 44 87 50.5


line stmt bran cond sub pod time code
1             # $File: //member/autrijus/RDF-YAML/lib/RDF/YAML.pm $ $Author: autrijus $
2             # $Revision: #2 $ $Change: 8524 $ $DateTime: 2003/10/22 05:20:04 $
3              
4             package RDF::YAML;
5             $RDF::YAML::VERSION = '0.11';
6              
7 1     1   799 use strict;
  1         2  
  1         702  
8              
9             =head1 NAME
10              
11             RDF::YAML - RDF YAML parser and dumper
12              
13             =head1 VERSION
14              
15             This document describes version 0.11 of RDF::YAML, released October 22,
16             2003.
17              
18             =head1 SYNOPSIS
19              
20             # Get triples from a RDF/YAML file
21             $rdf = RDF::YAML->new;
22             $triples = $rdf->parse_file("input.yml");
23              
24             # Declare namespaces
25             my %namespaces = ( 'rss' => 'http://purl.org/rss/1.0/' );
26             $rdf->set_namespaces( \%namespaces );
27              
28             # Translate RDF/XML to RDF/YAML
29             my @triples = RDF::Simple::Parser->new->parse_rdf($xml_string);
30             $rdf->set_triples(\@triples);
31             $rdf->dump_file("output.yml");
32              
33             # Add new triples to a RDF/YAML string
34             $rdf->parse_string($yaml_string);
35             $rdf->add_triples(\@triples);
36             $yaml_string = $rdf->dump_string;
37              
38             =head1 DESCRIPTION
39              
40             This module is a RDF/YAML parser/dumper; it can parse RDF/YAML files or
41             strings, provide results as triples, and dump triples back to RDF/YAML
42             format. It is only a thin wrapper around L
43             and L.
44              
45             Note that this is a proof-of-concept work; the RDF/YAML format used by
46             this module is purely experimental, and may change without notice.
47              
48             =head1 METHODS
49              
50             =head2 new()
51              
52             Constructor. Currently takes no parameters.
53              
54             =cut
55              
56             sub new {
57 1     1 1 399 my $class = shift;
58 1         6 bless({
59             triples => [],
60             parser => undef,
61             serialiser => undef,
62             }, $class);
63             }
64              
65             =head2 parse_file($file)
66              
67             Parses a RDF/YAML file specified by $file. Returns an array reference
68             to parsed triples, in the standard C<[ $subject, $predicate, $object ]>
69             format. This also replaces all triples and namespaces stored within
70             the object.
71              
72             =head2 parse_string($string)
73              
74             Similar to C, but parses RDF/YAML data from string.
75              
76             =cut
77              
78             sub parse_file {
79 1     1 1 321 my ($self, $file) = @_;
80 1         3 local $/;
81 1         2 local *FH;
82 1 50       49 open FH, $file or die $!;
83 1         29 $self->parse_string();
84 0         0 close FH;
85 0         0 return $self->get_triples;
86             }
87              
88             sub parse_string {
89 1     1 1 2 my $self = shift;
90 1         5 $self->set_triples( [ $self->_parser->new->parse_rdf(@_) ] );
91 1         4 $self->set_ns( $self->_parser->ns );
92 0         0 return $self->get_triples;
93             }
94              
95             =head2 dump_file($file)
96              
97             Writes all triples stored in the object into a file specified by C<$file>,
98             in RDF/YAML format.
99              
100             =head2 dump_string()
101              
102             Similar to C, but returns a RDF/YAML string instead.
103              
104             =cut
105              
106             sub dump_file {
107 0     0 1 0 my ($self, $file) = @_;
108 0         0 local *FH;
109 0 0       0 open FH, "> $file" or die $!;
110 0         0 print FH $self->dump_string;
111 0         0 close FH;
112             }
113              
114             sub dump_string {
115 0     0 1 0 my $self = shift;
116 0         0 require RDF::Simple::Serialiser::YAML;
117 0 0       0 $self->_serialiser->serialise(@{$self->get_triples || []});
  0         0  
118             }
119              
120             =head2 get_triples()
121              
122             Returns a reference to array of triples currently stored in this object.
123              
124             =head2 set_triples(\@triples)
125              
126             Takes a reference to array of triples and stores it in the object,
127             replacing previous ones. If invoked with no arguments, clears the
128             stored triples.
129              
130             =head2 add_triples(\@triples)
131              
132             Similar to C, but adds to currently stored triples instead
133             of replacing them.
134              
135             =cut
136              
137 0     0 1 0 sub get_triples { $_[0]->{triples} }
138 1   50 1 1 6 sub set_triples { $_[0]->{triples} = ($_[1] || []) }
139 0 0   0 1 0 sub add_triples { push @{$_[0]->get_triples}, @{$_[1] || []} }
  0         0  
  0         0  
140              
141             =head2 get_ns()
142              
143             Returns a hash reference of namespaces currently in use.
144              
145             Default namespaces are the same as listed in L.
146              
147             =head2 set_ns(\%namespaces)
148              
149             Takes a hash reference to namespaces expressed as C<$qname =E $uri>
150             pairs, and adds them to subsequent RDF/YAML documents. This overrides
151             (but does not clear) the default ones.
152              
153             =head2 add_ns(\%namespaces)
154              
155             Similar to C, but adds to currently stored namespaces instead
156             of replacing them.
157              
158             =cut
159              
160             sub get_ns {
161 0     0 1 0 my $self = shift;
162 0         0 return { $self->_serialiser->ns->lookup };
163             }
164              
165             sub set_ns {
166 1     1 1 10 my $self = shift;
167 1         5 delete $self->_serialiser->ns->{_lookup};
168 0         0 return $self->add_ns(@_);
169             }
170              
171             sub add_ns {
172 0     0 1 0 my $self = shift;
173 0 0       0 return { $self->_serialiser->addns(%{$_[0]||{}}) };
  0         0  
174             }
175              
176             # Internal methods
177              
178             sub _parser {
179 2     2   5 my $self = shift;
180 2         594 require RDF::Simple::Parser::YAML;
181 2   66     92 $self->{parser} ||= RDF::Simple::Parser::YAML->new;
182             }
183              
184             sub _serialiser {
185 1     1   3 my $self = shift;
186 1         713 require RDF::Simple::Serialiser::YAML;
187 0   0       $self->{serialiser} ||= RDF::Simple::Serialiser::YAML->new;
188             }
189              
190             1;
191              
192             =head1 SEE ALSO
193              
194             L, L
195              
196             L, L, L
197              
198             =head1 AUTHORS
199              
200             Autrijus Tang Eautrijus@autrijus.orgE
201              
202             =head1 COPYRIGHT
203              
204             Copyright 2003 by Autrijus Tang Eautrijus@autrijus.orgE.
205              
206             This program is free software; you can redistribute it and/or
207             modify it under the same terms as Perl itself.
208              
209             See L
210              
211             =cut