| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package RDF::Converter::CSV; |
|
2
|
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
44558
|
use warnings; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
35
|
|
|
4
|
1
|
|
|
1
|
|
6
|
use strict; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
38
|
|
|
5
|
1
|
|
|
1
|
|
1277
|
use Text::CSV; |
|
|
1
|
|
|
|
|
27733
|
|
|
|
1
|
|
|
|
|
7
|
|
|
6
|
1
|
|
|
1
|
|
965
|
use IO::File; |
|
|
1
|
|
|
|
|
11421
|
|
|
|
1
|
|
|
|
|
187
|
|
|
7
|
1
|
|
|
1
|
|
10
|
use Carp; |
|
|
1
|
|
|
|
|
3
|
|
|
|
1
|
|
|
|
|
61
|
|
|
8
|
1
|
|
|
1
|
|
1012
|
use Class::Std::Utils; |
|
|
1
|
|
|
|
|
3987
|
|
|
|
1
|
|
|
|
|
6
|
|
|
9
|
1
|
|
|
1
|
|
995
|
use XML::Writer; |
|
|
1
|
|
|
|
|
8614
|
|
|
|
1
|
|
|
|
|
1049
|
|
|
10
|
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
=head1 NAME |
|
12
|
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
RDF::Converter::CSV - Converts comma separated CSV to RDF |
|
14
|
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
=head1 VERSION |
|
16
|
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
Version 0.01 |
|
18
|
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
=cut |
|
20
|
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
our $VERSION = '0.02'; |
|
22
|
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
25
|
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
use RDF::Converter::CSV; |
|
27
|
|
|
|
|
|
|
use strict; |
|
28
|
|
|
|
|
|
|
use warnings; |
|
29
|
|
|
|
|
|
|
my $rdf = RDF::Converter::CSV->new( |
|
30
|
|
|
|
|
|
|
FILENAME => 'books.csv', #MANDATORY |
|
31
|
|
|
|
|
|
|
URI =>'http://nothing.nul/', #MANDATORY |
|
32
|
|
|
|
|
|
|
PREFIX => 'lib', #MANDATORY |
|
33
|
|
|
|
|
|
|
PRIMARY => 'id', #OPTIONAL - will take one of the field as identifier, if not given |
|
34
|
|
|
|
|
|
|
OUTPUT => 'books.rdf',#OPTIONAL - will output on the terminal, if not given |
|
35
|
|
|
|
|
|
|
COLUMNS => [ |
|
36
|
|
|
|
|
|
|
qw/ |
|
37
|
|
|
|
|
|
|
id |
|
38
|
|
|
|
|
|
|
title |
|
39
|
|
|
|
|
|
|
author |
|
40
|
|
|
|
|
|
|
price |
|
41
|
|
|
|
|
|
|
/ |
|
42
|
|
|
|
|
|
|
] #OPTIONAL - will take the first row as the field names, |
|
43
|
|
|
|
|
|
|
#if COLUMNS not given or |
|
44
|
|
|
|
|
|
|
#the number of elements in COLUMN != the number of fields in the CSV file |
|
45
|
|
|
|
|
|
|
); |
|
46
|
|
|
|
|
|
|
$rdf->write; |
|
47
|
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
=head1 OTHER METHODS |
|
49
|
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
$rdf->get_file; |
|
51
|
|
|
|
|
|
|
returns the array ref of the file content |
|
52
|
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
$rdf->csv_process; |
|
54
|
|
|
|
|
|
|
returns the csv data as a array ref of hash refs |
|
55
|
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
=cut |
|
57
|
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
my %file_name; |
|
59
|
|
|
|
|
|
|
my %uri; |
|
60
|
|
|
|
|
|
|
my %prefix; |
|
61
|
|
|
|
|
|
|
my %primary; |
|
62
|
|
|
|
|
|
|
my %columns; |
|
63
|
|
|
|
|
|
|
my %output; |
|
64
|
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
sub new |
|
66
|
|
|
|
|
|
|
{ |
|
67
|
0
|
|
|
0
|
0
|
|
my ($class, %params) = @_; |
|
68
|
0
|
0
|
0
|
|
|
|
if(! exists $params{FILENAME} || ! exists $params{URI} || ! exists $params{PREFIX}) |
|
|
|
|
0
|
|
|
|
|
|
69
|
|
|
|
|
|
|
{ |
|
70
|
0
|
|
|
|
|
|
croak("One or more mandatory attributes are missing!!"); |
|
71
|
|
|
|
|
|
|
} |
|
72
|
0
|
|
|
|
|
|
my $this = bless \do{my $ghost}, $class; |
|
|
0
|
|
|
|
|
|
|
|
73
|
0
|
|
|
|
|
|
$file_name{ident $this} = $params{FILENAME}; |
|
74
|
0
|
|
|
|
|
|
$uri{ident $this} = $params{URI}; |
|
75
|
0
|
|
|
|
|
|
$prefix{ident $this} = $params{PREFIX}; |
|
76
|
0
|
|
|
|
|
|
$primary{ident $this} = $params{PRIMARY}; |
|
77
|
0
|
|
|
|
|
|
$columns{ident $this} = $params{COLUMNS}; |
|
78
|
0
|
|
|
|
|
|
$output{ident $this} = $params{OUTPUT}; |
|
79
|
0
|
|
|
|
|
|
return $this; |
|
80
|
|
|
|
|
|
|
} |
|
81
|
|
|
|
|
|
|
#This retrieves the CSV data as an array ref |
|
82
|
|
|
|
|
|
|
sub get_file |
|
83
|
|
|
|
|
|
|
{ |
|
84
|
0
|
|
|
0
|
0
|
|
my ($this) = shift; |
|
85
|
0
|
|
|
|
|
|
my $file_name = $file_name{ident $this}; |
|
86
|
0
|
0
|
|
|
|
|
open my $io ,'<',$file_name or die "$file_name: $!"; |
|
87
|
0
|
|
|
|
|
|
my @data; |
|
88
|
0
|
|
|
|
|
|
while (<$io>) |
|
89
|
|
|
|
|
|
|
{ |
|
90
|
0
|
|
|
|
|
|
push @data, $_; |
|
91
|
|
|
|
|
|
|
} |
|
92
|
0
|
|
|
|
|
|
close $io; |
|
93
|
0
|
|
|
|
|
|
return \@data; |
|
94
|
|
|
|
|
|
|
} |
|
95
|
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
#This converts the csv content as a data structure |
|
97
|
|
|
|
|
|
|
sub csv_process |
|
98
|
|
|
|
|
|
|
{ |
|
99
|
0
|
|
|
0
|
0
|
|
my ($this) = shift; |
|
100
|
0
|
|
|
|
|
|
my $csv = Text::CSV->new({binary => 1,eol => $/}); |
|
101
|
0
|
|
|
|
|
|
my $file_name = $file_name{ident $this}; |
|
102
|
0
|
|
|
|
|
|
my $field_names = $columns{ident $this}; |
|
103
|
0
|
0
|
|
|
|
|
open my $io, "<", $file_name or die "$file_name: $!"; |
|
104
|
|
|
|
|
|
|
|
|
105
|
0
|
|
|
|
|
|
my $row = $csv->getline ($io); |
|
106
|
0
|
0
|
0
|
|
|
|
$field_names = $row if !$field_names || scalar @$row != scalar @$field_names; |
|
107
|
0
|
0
|
0
|
|
|
|
$primary{ident $this} = $$field_names[0] if !$primary{ident $this} || |
|
|
|
|
0
|
|
|
|
|
|
108
|
|
|
|
|
|
|
($primary{ident $this} && |
|
109
|
|
|
|
|
|
|
! grep(/^$primary{ident $this}$/, @$field_names) |
|
110
|
|
|
|
|
|
|
); |
|
111
|
0
|
|
|
|
|
|
$csv->column_names (@$field_names); |
|
112
|
0
|
|
|
|
|
|
my @perl_data; |
|
113
|
|
|
|
|
|
|
|
|
114
|
0
|
|
|
|
|
|
while (my $hr = $csv->getline_hr ($io)) |
|
115
|
|
|
|
|
|
|
{ |
|
116
|
0
|
0
|
|
|
|
|
push @perl_data, $hr if $hr; |
|
117
|
|
|
|
|
|
|
} |
|
118
|
0
|
|
|
|
|
|
close $io; |
|
119
|
0
|
|
|
|
|
|
return \@perl_data; |
|
120
|
|
|
|
|
|
|
} |
|
121
|
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
#Converts CSV to RDF |
|
123
|
|
|
|
|
|
|
sub write |
|
124
|
|
|
|
|
|
|
{ |
|
125
|
0
|
|
|
0
|
0
|
|
my ($this) = shift; |
|
126
|
0
|
|
|
|
|
|
my $rdfns = "http://www.w3.org/1999/02/22-rdf-syntax-ns#"; |
|
127
|
0
|
|
|
|
|
|
my $uri = $uri{ident $this}; |
|
128
|
0
|
|
|
|
|
|
my $prefix = $prefix{ident $this}; |
|
129
|
0
|
|
|
|
|
|
my $data = $this->csv_process; |
|
130
|
0
|
|
|
|
|
|
my $primary = $primary{ident $this}; |
|
131
|
0
|
|
|
|
|
|
my $out_file = $output{ident $this}; |
|
132
|
0
|
|
|
|
|
|
my $output = new IO::File(">$out_file"); |
|
133
|
|
|
|
|
|
|
|
|
134
|
0
|
|
|
|
|
|
my $writer = new XML::Writer( |
|
135
|
|
|
|
|
|
|
OUTPUT => $output, |
|
136
|
|
|
|
|
|
|
NAMESPACES => 1, |
|
137
|
|
|
|
|
|
|
PREFIX_MAP => { $rdfns => 'rdf', $uri => $prefix}, |
|
138
|
|
|
|
|
|
|
DATA_INDENT =>1, |
|
139
|
|
|
|
|
|
|
DATA_MODE =>1 |
|
140
|
|
|
|
|
|
|
); |
|
141
|
0
|
|
|
|
|
|
$writer->xmlDecl("UTF-8"); |
|
142
|
0
|
|
|
|
|
|
$writer->forceNSDecl($uri); |
|
143
|
0
|
|
|
|
|
|
$writer->startTag([$rdfns => "RDF"]); |
|
144
|
|
|
|
|
|
|
|
|
145
|
0
|
|
|
|
|
|
for my $row(@$data) |
|
146
|
|
|
|
|
|
|
{ |
|
147
|
0
|
|
|
|
|
|
$writer->startTag( |
|
148
|
|
|
|
|
|
|
[$rdfns => "Description"], |
|
149
|
|
|
|
|
|
|
[$rdfns => 'about'] => $uri.$row->{$primary} |
|
150
|
|
|
|
|
|
|
); |
|
151
|
0
|
|
|
|
|
|
for my $key (keys %$row) |
|
152
|
|
|
|
|
|
|
{ |
|
153
|
0
|
|
|
|
|
|
$writer->startTag([$uri => $key]); |
|
154
|
0
|
|
|
|
|
|
$writer->characters($row->{$key}); |
|
155
|
0
|
|
|
|
|
|
$writer->endTag([$uri => $key]); |
|
156
|
|
|
|
|
|
|
} |
|
157
|
0
|
|
|
|
|
|
$writer->endTag([$rdfns, "Description"]); |
|
158
|
|
|
|
|
|
|
} |
|
159
|
0
|
|
|
|
|
|
$writer->endTag([$rdfns, "RDF"]); |
|
160
|
0
|
|
|
|
|
|
$writer->end(); |
|
161
|
|
|
|
|
|
|
} |
|
162
|
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
=head1 AUTHORS |
|
164
|
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
Arshad Mohamed, Khader Shameer and R. Sowdhamini RDF::Converter Team C<< >>, C<< >>, C<< >> |
|
166
|
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
=head1 ACKNOWLEDGEMENTS |
|
168
|
|
|
|
|
|
|
Funding : |
|
169
|
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
=head1 BUGS |
|
172
|
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
Please report any bugs or feature requests to C, or through |
|
174
|
|
|
|
|
|
|
the web interface at L. I will be notified, and then you'll |
|
175
|
|
|
|
|
|
|
automatically be notified of progress on your bug as I make changes. |
|
176
|
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
=over 4 |
|
178
|
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
=item * RT: CPAN's request tracker |
|
180
|
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
L |
|
182
|
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
=item * CPAN Ratings |
|
184
|
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
L |
|
186
|
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
=item * Search CPAN |
|
188
|
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
L |
|
190
|
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
=back |
|
192
|
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
=head1 COPYRIGHT & LICENSE |
|
195
|
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
Copyright 2009 Arshad Mohamed, Khader Shameer & R. Sowdhamini, all rights reserved. |
|
197
|
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify it |
|
199
|
|
|
|
|
|
|
under the same terms as Perl itself. |
|
200
|
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
=cut |
|
203
|
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
1; # End of RDF::Converter::CSV |