| RDF-Trine documentation | Contained in the RDF-Trine distribution. |
RDF::Trine::Exporter::CSV - Export RDF data to CSV
This document describes RDF::Trine::Exporter::CSV version 0.135
use RDF::Trine::Exporter::CSV;
The RDF::Trine::Exporter::CSV class provides an API for serializing RDF data to CSV strings and files.
new ( sep_char => $sep_char, quote => $bool )Returns a new RDF::Trine::Exporter::CSV object. If $sep_char is provided,
it is used as the seperator character in CSV serialization, otherwise a comma
(",") is used.
serialize_iterator_to_file ( $file, $iterator )Serializes the bindings objects produced by $iterator, printing the
results to the supplied filehandle <$fh>.
serialize_iterator_to_string ( $iterator )Serializes the bindings objects produced by $iterator, returning the
result as a string.
Gregory Todd Williams <gwilliams@cpan.org>
Copyright (c) 2006-2010 Gregory Todd Williams. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
| RDF-Trine documentation | Contained in the RDF-Trine distribution. |
# RDF::Trine::Exporter::CSV # -----------------------------------------------------------------------------
package RDF::Trine::Exporter::CSV; use strict; use warnings; no warnings 'redefine'; use Data::Dumper; use Text::CSV; use Scalar::Util qw(blessed); use RDF::Trine::Error qw(:try); our ($VERSION); BEGIN { $VERSION = '0.135'; }
sub new { my $class = shift; my %args = @_; my $sep = $args{ sep_char } || ','; my $quote = $args{ quote }; my $csv = Text::CSV->new ( { binary => 1, sep_char => $sep } ); my $self = bless( { %args, csv => $csv }, $class ); return $self; }
sub serialize_iterator_to_file { my $self = shift; my $file = shift; my $iter = shift; unless (blessed($iter) and ($iter->isa('RDF::Trine::Iterator::Bindings') or $iter->isa('RDF::Trine::Iterator::Graph'))) { my $type = ref($iter); $type =~ s/^RDF::Trine::Iterator:://; throw RDF::Trine::Error::MethodInvocationError -text => "CSV Exporter must be called with a Graph or VariableBindings iterator, not a $type iterator"; } my $type = ($iter->isa('RDF::Trine::Iterator::Bindings')) ? 'bindings' : 'graph'; my $csv = $self->{csv}; my $quote = $self->{quote}; my @keys; while (my $row = $iter->next) { unless (scalar(@keys)) { @keys = ($type eq 'bindings') ? (keys %$row) : qw(subject predicate object); $csv->print( $file, \@keys ); print {$file} "\n"; } my @data; foreach my $k (@keys) { my $v = ($type eq 'bindings') ? $row->{$k} : $row->$k(); if ($quote) { push(@data, $v->as_string); } elsif (blessed($v)) { if ($v->isa('RDF::Trine::Node::Resource')) { push(@data, $v->uri_value); } elsif ($v->isa('RDF::Trine::Node::Blank')) { push(@data, $v->blank_identifier); } elsif ($v->isa('RDF::Trine::Node::Literal')) { push(@data, $v->literal_value); } } else { push(@data, ''); } } $csv->print( $file, \@data ); print {$file} "\n"; } }
sub serialize_iterator_to_string { my $self = shift; my $iter = shift; my $string = ''; open( my $fh, '>', \$string ); $self->serialize_iterator_to_file( $fh, $iter ); close($fh); return $string; } 1; __END__