| 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.019 | ||||||||||||||||||||||||||||||||||||||||||
| 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 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 26 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 27 | 1 | ▶ | no warnings 'redefine'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||||||||
| 29 | 1 | ▶ | use Data::Dumper; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 30 | 1 | ▶ | use Text::CSV_XS; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 31 | 1 | ▶ | use Scalar::Util qw(blessed); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 32 | 1 | ▶ | use RDF::Trine::Error qw(:try); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||
| 34 | 1 | our ($VERSION); | |||||||||||||||||||||||||||||||||||||||||
| 35 | 1 | ▶ | BEGIN { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 36 | 1 | $VERSION = '1.019'; | |||||||||||||||||||||||||||||||||||||||||
| 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 | 4 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 52 | 4 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||
| 53 | 4 | my %args = @_; | |||||||||||||||||||||||||||||||||||||||||
| 54 | 4 | ▶ | my $sep = $args{ sep_char } || ','; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$args{'sep_char'} || ','
MC/DC100%1/1
$args{'sep_char'} || ','
$args{'sep_char'}
Truth table100%2/2
A
A= $args{'sep_char'}
| |||||||||||||||||||||||||||||||||||||||||||
| 55 | 4 | my $quote = $args{ quote }; | |||||||||||||||||||||||||||||||||||||||||
| 56 | 4 | my $csv = Text::CSV_XS->new ( { binary => 1, sep_char => $sep } ); | |||||||||||||||||||||||||||||||||||||||||
| 57 | 4 | my $self = bless( { %args, csv => $csv }, $class ); | |||||||||||||||||||||||||||||||||||||||||
| 58 | 4 | 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 | 3 | ▶ | sub serialize_iterator_to_file { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
serialize_iterator_to_file: called CC=15 SCAR=29.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 69 | 3 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 70 | 3 | my $file = shift; | |||||||||||||||||||||||||||||||||||||||||
| 71 | 3 | my $iter = shift; | |||||||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||||||||
| 73 | 3 | ▶ | unless (blessed($iter) and ($iter->isa('RDF::Trine::Iterator::Bindings') or $iter->isa('RDF::Trine::Iterator::Graph'))) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
builtin::blessed($iter) and $iter->isa("RDF::Trine::Iterator::Bindings") || $iter->isa("RDF::Trine::Iterator::Graph")
$iter->isa("RDF::Trine::Iterator::Bindings") or $iter->isa("RDF::Trine::Iterator::Graph")
MC/DC0%0/3
builtin::blessed($iter) and $iter->isa("RDF::Trine::Iterator::Bindings") || $iter->isa("RDF::Trine::Iterator::Graph")
builtin::blessed($iter)
$iter->isa("RDF::Trine::Iterator::Bindings")
$iter->isa("RDF::Trine::Iterator::Graph")
Truth table25%1/4
A and B or C
A= builtin::blessed($iter)B= $iter->isa("RDF::Trine::Iterator::Bindings")C= $iter->isa("RDF::Trine::Iterator::Graph")
| |||||||||||||||||||||||||||||||||||||||||||
| 74 | 0 | my $type = ref($iter); | |||||||||||||||||||||||||||||||||||||||||
| 75 | 0 | $type =~ s/^RDF::Trine::Iterator:://; | |||||||||||||||||||||||||||||||||||||||||
| 76 | 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 | ▶ | my $type = ($iter->isa('RDF::Trine::Iterator::Bindings')) ? 'bindings' : 'graph'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||
| 81 | 3 | my $csv = $self->{csv}; | |||||||||||||||||||||||||||||||||||||||||
| 82 | 3 | my $quote = $self->{quote}; | |||||||||||||||||||||||||||||||||||||||||
| 83 | 3 | my @keys; | |||||||||||||||||||||||||||||||||||||||||
| 84 | 3 | ▶ | while (my $row = $iter->next) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 85 | 9 | ▶ | unless (scalar(@keys)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 86 | 3 | ▶ | @keys = ($type eq 'bindings') ? (sort keys %$row) : qw(subject predicate object); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 87 | 3 | $csv->print( $file, \@keys ); | |||||||||||||||||||||||||||||||||||||||||
| 88 | 3 | print {$file} "\n"; | |||||||||||||||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||||||||||||||||
| 90 | 9 | my @data; | |||||||||||||||||||||||||||||||||||||||||
| 91 | 9 | foreach my $k (@keys) { | |||||||||||||||||||||||||||||||||||||||||
| 92 | 18 | ▶ | my $v = ($type eq 'bindings') ? $row->{$k} : $row->$k(); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 93 | 18 | ▶ | if ($quote) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 94 | 6 | push(@data, $v->as_string); | |||||||||||||||||||||||||||||||||||||||||
| 95 | } elsif (blessed($v)) { | ||||||||||||||||||||||||||||||||||||||||||
| 96 | 12 | ▶ | if ($v->isa('RDF::Trine::Node::Resource')) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 97 | 8 | push(@data, $v->uri_value); | |||||||||||||||||||||||||||||||||||||||||
| 98 | } elsif ($v->isa('RDF::Trine::Node::Blank')) { | ||||||||||||||||||||||||||||||||||||||||||
| 99 | 0 | push(@data, $v->blank_identifier); | |||||||||||||||||||||||||||||||||||||||||
| 100 | } elsif ($v->isa('RDF::Trine::Node::Literal')) { | ||||||||||||||||||||||||||||||||||||||||||
| 101 | 4 | push(@data, $v->literal_value); | |||||||||||||||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||||||||||||||||
| 103 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 104 | 0 | push(@data, ''); | |||||||||||||||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||||||||||||||||
| 107 | 9 | $csv->print( $file, \@data ); | |||||||||||||||||||||||||||||||||||||||||
| 108 | 9 | print {$file} "\n"; | |||||||||||||||||||||||||||||||||||||||||
| 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 | 1 | ▶ | sub serialize_iterator_to_string { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
serialize_iterator_to_string: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 120 | 1 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 121 | 1 | my $iter = shift; | |||||||||||||||||||||||||||||||||||||||||
| 122 | 1 | my $string = ''; | |||||||||||||||||||||||||||||||||||||||||
| 123 | 1 | open( my $fh, '>', \$string ); | |||||||||||||||||||||||||||||||||||||||||
| 124 | 1 | $self->serialize_iterator_to_file( $fh, $iter ); | |||||||||||||||||||||||||||||||||||||||||
| 125 | 1 | close($fh); | |||||||||||||||||||||||||||||||||||||||||
| 126 | 1 | return $string; | |||||||||||||||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||||||||||
| 129 | 1 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||||||||
| 131 | __END__ | ||||||||||||||||||||||||||||||||||||||||||