File Coverage

blib/lib/RDF/Trine/Exporter/CSV.pm
Criterion Covered Total %
statement 64 69 92.7
branch 13 20 65.0
condition 4 8 50.0
subroutine 12 12 100.0
pod 3 3 100.0
total 96 112 85.7


line stmt bran cond sub pod time code
1             # RDF::Trine::Exporter::CSV
2             # -----------------------------------------------------------------------------
3              
4             =head1 NAME
5              
6             RDF::Trine::Exporter::CSV - Export RDF data to CSV
7              
8             =head1 VERSION
9              
10             This document describes RDF::Trine::Exporter::CSV version 1.017
11              
12             =head1 SYNOPSIS
13              
14             use RDF::Trine::Exporter::CSV;
15              
16             =head1 DESCRIPTION
17              
18             The RDF::Trine::Exporter::CSV class provides an API for serializing RDF data
19             to CSV strings and files.
20              
21             =cut
22              
23             package RDF::Trine::Exporter::CSV;
24              
25 1     1   496 use strict;
  1         3  
  1         25  
26 1     1   5 use warnings;
  1         2  
  1         22  
27 1     1   5 no warnings 'redefine';
  1         2  
  1         32  
28              
29 1     1   5 use Data::Dumper;
  1         2  
  1         43  
30 1     1   675 use Text::CSV_XS;
  1         6868  
  1         48  
31 1     1   7 use Scalar::Util qw(blessed);
  1         2  
  1         45  
32 1     1   7 use RDF::Trine::Error qw(:try);
  1         2  
  1         13  
33              
34             our ($VERSION);
35             BEGIN {
36 1     1   527 $VERSION = '1.017';
37             }
38              
39             =head1 METHODS
40              
41             =over 4
42              
43             =item C<< new ( sep_char => $sep_char, quote => $bool ) >>
44              
45             Returns a new RDF::Trine::Exporter::CSV object. If C<< $sep_char >> is provided,
46             it is used as the separator character in CSV serialization, otherwise a comma
47             (",") is used.
48              
49             =cut
50              
51             sub new {
52 4     4 1 493 my $class = shift;
53 4         14 my %args = @_;
54 4   100     19 my $sep = $args{ sep_char } || ',';
55 4         7 my $quote = $args{ quote };
56 4         26 my $csv = Text::CSV_XS->new ( { binary => 1, sep_char => $sep } );
57 4         484 my $self = bless( { %args, csv => $csv }, $class );
58 4         12 return $self;
59             }
60              
61             =item C<< serialize_iterator_to_file ( $file, $iterator ) >>
62              
63             Serializes the bindings objects produced by C<< $iterator >>, printing the
64             results to the supplied filehandle C<<$fh>>.
65              
66             =cut
67              
68             sub serialize_iterator_to_file {
69 3     3 1 59 my $self = shift;
70 3         5 my $file = shift;
71 3         5 my $iter = shift;
72              
73 3 50 33     29 unless (blessed($iter) and ($iter->isa('RDF::Trine::Iterator::Bindings') or $iter->isa('RDF::Trine::Iterator::Graph'))) {
      33        
74 0         0 my $type = ref($iter);
75 0         0 $type =~ s/^RDF::Trine::Iterator:://;
76 0         0 throw RDF::Trine::Error::MethodInvocationError -text => "CSV Exporter must be called with a Graph or VariableBindings iterator, not a $type iterator";
77             }
78              
79 3 50       13 my $type = ($iter->isa('RDF::Trine::Iterator::Bindings')) ? 'bindings' : 'graph';
80              
81 3         11 my $csv = $self->{csv};
82 3         6 my $quote = $self->{quote};
83 3         6 my @keys;
84 3         11 while (my $row = $iter->next) {
85 9 100       23 unless (scalar(@keys)) {
86 3 50       12 @keys = ($type eq 'bindings') ? (sort keys %$row) : qw(subject predicate object);
87 3         67 $csv->print( $file, \@keys );
88 3         35 print {$file} "\n";
  3         8  
89             }
90 9         18 my @data;
91 9         17 foreach my $k (@keys) {
92 18 50       41 my $v = ($type eq 'bindings') ? $row->{$k} : $row->$k();
93 18 100       52 if ($quote) {
    50          
94 6         17 push(@data, $v->as_string);
95             } elsif (blessed($v)) {
96 12 100       62 if ($v->isa('RDF::Trine::Node::Resource')) {
    50          
    50          
97 8         23 push(@data, $v->uri_value);
98             } elsif ($v->isa('RDF::Trine::Node::Blank')) {
99 0         0 push(@data, $v->blank_identifier);
100             } elsif ($v->isa('RDF::Trine::Node::Literal')) {
101 4         12 push(@data, $v->literal_value);
102             }
103             } else {
104 0         0 push(@data, '');
105             }
106             }
107 9         51 $csv->print( $file, \@data );
108 9         70 print {$file} "\n";
  9         36  
109             }
110             }
111              
112             =item C<< serialize_iterator_to_string ( $iterator ) >>
113              
114             Serializes the bindings objects produced by C<< $iterator >>, returning the
115             result as a string.
116              
117             =cut
118              
119             sub serialize_iterator_to_string {
120 1     1 1 6 my $self = shift;
121 1         2 my $iter = shift;
122 1         3 my $string = '';
123 1     1   7 open( my $fh, '>', \$string );
  1         2  
  1         6  
  1         34  
124 1         759 $self->serialize_iterator_to_file( $fh, $iter );
125 1         3 close($fh);
126 1         5 return $string;
127             }
128              
129             1;
130              
131             __END__
132              
133             =back
134              
135             =head1 BUGS
136              
137             Please report any bugs or feature requests to through the GitHub web interface
138             at L<https://github.com/kasei/perlrdf/issues>.
139              
140             =head1 AUTHOR
141              
142             Gregory Todd Williams C<< <gwilliams@cpan.org> >>
143              
144             =head1 COPYRIGHT
145              
146             Copyright (c) 2006-2012 Gregory Todd Williams. This
147             program is free software; you can redistribute it and/or modify it under
148             the same terms as Perl itself.
149              
150             =cut