line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
#!/usr/bin/env perl |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
1077
|
use warnings; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
41
|
|
4
|
1
|
|
|
1
|
|
5
|
use strict; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
43
|
|
5
|
|
|
|
|
|
|
|
6
|
1
|
|
|
1
|
|
5
|
use lib q{.}; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
8
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
package Gwybodaeth::Write::WriteFromCSV; |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
=head1 NAME |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
Write::WriteFromCSV - Write data into RDF/XML which was in CSV. |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
=head1 SYNOPSIS |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
use WriteFromCSV; |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
my $w = WriteFromCSV->new(); |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
$w->write_rdf($triples_data,$data); |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
=head1 DESCRIPTION |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
This module is subclassed from Write::Write and applies mapping to CSV data. |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
=over |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
=item new() |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
Returns an instance of WriteFromCSV. |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
=cut |
33
|
|
|
|
|
|
|
|
34
|
1
|
|
|
1
|
|
133
|
use base 'Gwybodaeth::Write'; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
686
|
|
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
use Carp qw(croak); |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
sub new { |
39
|
|
|
|
|
|
|
my $class = shift; |
40
|
|
|
|
|
|
|
my $self = { ids => {}, Data => qq{}}; |
41
|
|
|
|
|
|
|
$self->{XML} = XML::Twig->new(pretty_print => 'nice' ); |
42
|
|
|
|
|
|
|
bless $self, $class; |
43
|
|
|
|
|
|
|
return $self; |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
=item write_rdf($mapping_data, $data) |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
Applies $mapping_data to the array reference $data outputting RDF/XML. |
49
|
|
|
|
|
|
|
$mapping_data is expected to be the output from Parsers::N3. |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
=cut |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
sub write_rdf { |
54
|
|
|
|
|
|
|
ref(my $self = shift) or croak "instance variable needed"; |
55
|
|
|
|
|
|
|
my $triple_data = shift; |
56
|
|
|
|
|
|
|
my $data = shift; |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
# Check cleanliness of input data types |
59
|
|
|
|
|
|
|
$self->_check_data($triple_data,$data,'ARRAY'); |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
my $triples = ${ $triple_data }[0]; |
62
|
|
|
|
|
|
|
my $functions = ${ $triple_data }[1]; |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
my @pure_data; |
65
|
|
|
|
|
|
|
my($start,$end); |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
$start = 1; |
68
|
|
|
|
|
|
|
$end = $#{ $data }; |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
# Record any start end end point we are given. |
71
|
|
|
|
|
|
|
# This allows the interpreter to skip unwanted rows. |
72
|
|
|
|
|
|
|
for my $row (0..$#{ $data }) { |
73
|
|
|
|
|
|
|
if (@{$data}[$row]->[0] =~ m/start\s+row/mix) { |
74
|
|
|
|
|
|
|
$start = int @{$data}[$row]->[1] - 1; # Subtract 1 because |
75
|
|
|
|
|
|
|
# arrays start at 0. |
76
|
|
|
|
|
|
|
} |
77
|
|
|
|
|
|
|
if (@{ $data }[$row]->[0] =~ m/end\s+row/mix) { |
78
|
|
|
|
|
|
|
$end = int @{ $data }[$row]->[1] - 1; # Subtract 1 because |
79
|
|
|
|
|
|
|
# arrays start at 0. |
80
|
|
|
|
|
|
|
last; |
81
|
|
|
|
|
|
|
} |
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
for ($start..$end) { |
85
|
|
|
|
|
|
|
push @pure_data, @{ $data }[$_]; |
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
$self->_write_meta_data(); |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
for my $row (@pure_data) { |
91
|
|
|
|
|
|
|
$self->_really_write_triples($row,$triples); |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
my %ids; |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
# The %functions hash is tied to InsertOrderHash so the keys will be |
97
|
|
|
|
|
|
|
# given in the order they entered the hash. This gives the correct |
98
|
|
|
|
|
|
|
# precedence ordering. |
99
|
|
|
|
|
|
|
for my $key ( keys %{ $functions }) { |
100
|
|
|
|
|
|
|
for my $row (@pure_data) { |
101
|
|
|
|
|
|
|
my $id = $self->_extract_field($row,$key); |
102
|
|
|
|
|
|
|
next if (exists $ids{$id}); |
103
|
|
|
|
|
|
|
$ids{$id} = qq{}; |
104
|
|
|
|
|
|
|
$self->_really_write_triples($row, $functions->{$key},$key); |
105
|
|
|
|
|
|
|
} |
106
|
|
|
|
|
|
|
} |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
$self->_print2str(""); |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
my $twig = $self->_structurize; |
111
|
|
|
|
|
|
|
$twig->print(); |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
return 1; |
114
|
|
|
|
|
|
|
} |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
sub _get_field { |
117
|
|
|
|
|
|
|
my($self, $row, $field, $opt) = @_; |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
if (not defined($opt)) { $opt = qq{}; } |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
# We subtract 1 as arrays start at 0, and spreadsheets at 1 |
122
|
|
|
|
|
|
|
return @{ $row }[$field - 1] . $opt; |
123
|
|
|
|
|
|
|
} |
124
|
|
|
|
|
|
|
1; |
125
|
|
|
|
|
|
|
__END__ |