File Coverage

blib/lib/RDF/Simple/Parser/YAML.pm
Criterion Covered Total %
statement 21 21 100.0
branch 5 8 62.5
condition 2 3 66.6
subroutine 4 4 100.0
pod 1 1 100.0
total 33 37 89.1


line stmt bran cond sub pod time code
1             # $File: //member/autrijus/RDF-YAML/lib/RDF/Simple/Parser/YAML.pm $ $Author: autrijus $
2             # $Revision: #2 $ $Change: 8524 $ $DateTime: 2003/10/22 05:20:04 $
3              
4             package RDF::Simple::Parser::YAML;
5             $RDF::Simple::Parser::YAML::VERSION = '0.01';
6              
7 1     1   6 use strict;
  1         3  
  1         37  
8 1     1   898 use YAML;
  1         9730  
  1         71  
9 1     1   1677 use Class::MethodMaker new_hash_init => 'new', get_set => [ qw(base ns)];
  1         29784  
  1         10  
10              
11             =head1 NAME
12              
13             RDF::Simple::Parser::YAML - Simple RDF/YAML parser
14              
15             =head1 DESCRIPTION
16              
17             This module is a simple RDF/XML parser. It reads a string containing
18             RDF in YAML, and returns an array of RDF triples.
19              
20             =head1 SYNOPSIS
21              
22             my $uri = 'http://www.w3.org/2000/08/w3c-synd/home.rss';
23             my $rdf = LWP::Simple::get($uri);
24              
25             my $parser = RDF::Simple::Parser::YAML->new(base => $uri)
26             my @triples = $parser->parse_rdf($rdf);
27              
28             # returns an array of array references which are triples
29              
30             =head1 METHODS
31              
32             =head2 new( base => $uri )
33              
34             Create a new RDF::Simple::Parser::YAML object.
35              
36             The optional parameter C supplies a base URI for relative URIs found
37             in the document. (This function is currently unimplemented.)
38              
39             =head2 parse_rdf($rdf)
40              
41             Accepts a string which is an RDF/YAML document.
42              
43             Returns an array of array references which are RDF triples.
44              
45             =cut
46              
47             sub parse_rdf {
48 1     1 1 48 my ($self, $rdf) = @_;
49              
50 1 50       6 my $hash = YAML::Load($rdf) or return [];
51 1         43532 my $ns = $self->ns($hash->{''});
52 1 50       19 $ns->{''} = 'urn:empty' unless exists $ns->{''};
53              
54 1         2 my @rv;
55 1         9 foreach my $subject (sort grep length, keys %$hash) {
56 1 50       4 my $node = $hash->{$subject} or next;
57 1         9 foreach my $predicate (sort grep length, keys %$node) {
58 7         13 my $object = $node->{$predicate};
59 7 100 66     50 $predicate =~ s/^(\w[^:]*):/$ns->{$1}/g
60             or $predicate =~ /:/
61             or $predicate = $ns->{''} . $predicate;
62 7         21 push @rv, [ $subject, $predicate, $object ];
63             }
64             }
65 1         13 return @rv;
66             }
67              
68             1;
69              
70             =head1 SEE ALSO
71              
72             L, L
73              
74             =head1 AUTHORS
75              
76             Autrijus Tang Eautrijus@autrijus.orgE
77              
78             =head1 COPYRIGHT
79              
80             Copyright 2003 by Autrijus Tang Eautrijus@autrijus.orgE.
81              
82             This program is free software; you can redistribute it and/or
83             modify it under the same terms as Perl itself.
84              
85             See L
86              
87             =cut