File Coverage

blib/lib/RDF/Notation3/Template/TTriples.pm
Criterion Covered Total %
statement 25 87 28.7
branch 12 48 25.0
condition 2 29 6.9
subroutine 5 9 55.5
pod 0 6 0.0
total 44 179 24.5


line stmt bran cond sub pod time code
1 2     2   13 use strict;
  2         2  
  2         133  
2             #use warnings;
3              
4             package RDF::Notation3::Template::TTriples;
5              
6             require 5.005_62;
7 2     2   13 use RDF::Notation3;
  2         4  
  2         2722  
8              
9             ############################################################
10              
11             @RDF::Notation3::Template::TTriples::ISA = qw(RDF::Notation3);
12              
13             sub parse_file {
14 10     10 0 1857 my ($self, $path) = @_;
15 10 50       32 $self->_do_error(1, '') unless @_ > 1;
16              
17 10         33 $self->{triples} = [];
18              
19 10         66 $self->SUPER::parse_file($path);
20 10         14 return scalar @{$self->{triples}};
  10         41  
21             }
22              
23              
24             sub parse_string {
25 0     0 0 0 my ($self, $str) = @_;
26 0 0       0 $self->_do_error(3, '') unless @_ > 1;
27              
28 0         0 $self->{triples} = [];
29              
30 0         0 $self->SUPER::parse_string($str);
31 0         0 return scalar @{$self->{triples}};
  0         0  
32             }
33              
34              
35             sub get_triples {
36 0     0 0 0 my ($self, $subj, $verb, $obj, $context) = @_;
37 0         0 my @triples = ();
38              
39 0         0 foreach (@{$self->{triples}}) {
  0         0  
40 0 0 0     0 if (not $subj or ($subj eq $_->[0])) {
41 0 0 0     0 if (not $verb or ($verb eq $_->[1])) {
42 0 0 0     0 if (not $obj or ($obj eq $_->[2])) {
43 0 0 0     0 if (not $context or ($context eq $_->[3])) {
44 0         0 push @triples, $_;
45             }
46             }
47             }
48             }
49             }
50 0         0 return \@triples;
51             }
52              
53              
54             sub get_triples_as_string {
55 0     0 0 0 my ($self, $subj, $verb, $obj, $context) = @_;
56 0         0 my $triples = '';
57              
58 0         0 foreach (@{$self->{triples}}) {
  0         0  
59 0 0 0     0 if (not $subj or ($subj eq $_->[0])) {
60 0 0 0     0 if (not $verb or ($verb eq $_->[1])) {
61 0 0 0     0 if (not $obj or ($obj eq $_->[2])) {
62 0 0 0     0 if (not $context or ($context eq $_->[3])) {
63 0         0 $triples .= "$_->[0] $_->[1] $_->[2]\n";
64             }
65             }
66             }
67             }
68             }
69 0         0 return $triples;
70             }
71              
72              
73             sub get_n3 {
74 0     0 0 0 my ($self) = @_;
75 0         0 my $n3 = '';
76              
77             # for each context
78 0         0 foreach my $c (keys %{$self->{ns}}) {
  0         0  
79             # namespaces
80 0         0 foreach (keys %{$self->{ns}->{$c}}) {
  0         0  
81 0         0 $n3 .= "\@prefix $_: <$self->{ns}->{$c}->{$_}> .\n";
82             }
83             # statements
84 0         0 my $tri_tree = {};
85 0         0 my @tri_seq = ();
86             # building tree
87 0         0 foreach my $t (@{$self->{triples}}) {
  0         0  
88 0 0       0 if ($t->[3] eq $c) {
89              
90 0         0 push @{$tri_tree->{$t->[0]}->{$t->[1]}}, $t->[2];
  0         0  
91 0 0       0 push @tri_seq, $t->[0] unless grep ($_ eq $t->[0], @tri_seq);
92             }
93             }
94             # serializing tree
95 0         0 foreach my $s (@tri_seq) {
96 0         0 $n3 .= "$s\n";
97 0         0 my @pred = keys %{$tri_tree->{$s}};
  0         0  
98 0         0 for (my $i=0; $i < @pred; $i++) {
99 0         0 $n3 .= ' ' x 8;
100 0         0 $n3 .= "$pred[$i] ";
101             # object
102 0         0 for (my $j=0; $j < @{$tri_tree->{$s}->{$pred[$i]}}; $j++) {
  0         0  
103 0         0 $n3 .= $tri_tree->{$s}->{$pred[$i]}->[$j];
104 0 0 0     0 if ($i == $#pred && $j == @{$tri_tree->{$s}->{$pred[$i]}}-1) {
  0 0       0  
  0         0  
105 0         0 $n3 .= " .\n";
106             } elsif ($j == @{$tri_tree->{$s}->{$pred[$i]}}-1) {
107 0         0 $n3 .= " ;\n";
108             } else {
109 0         0 $n3 .= " , ";
110             }
111             }
112             }
113             }
114             }
115 0         0 return $n3;
116             }
117              
118              
119             sub add_prefix {
120 1     1 0 13 my ($self, $pref, $uri) = @_;
121              
122 1 50       12 if ($pref !~ /^[_a-zA-Z]\w*/) {
    50          
123 0         0 $self->_do_error(102, $pref);
124             } elsif ($uri !~ /^(?:[_a-zA-Z]\w*)?:[a-zA-Z]\w*$|^[^\{\}<>]*$/) {
125 0         0 $self->_do_error(103, $uri);
126             } else {
127 1         5 $self->{ns}->{'<>'}->{$pref} = $uri;
128             }
129 1         2 return scalar keys %{$self->{ns}->{'<>'}};
  1         4  
130             }
131              
132              
133             sub _check_resource {
134 3     3   7 my ($self, $s, $rs, $type) = @_;
135              
136 3 100       24 if ($rs =~ /^<[^\{\}<>]*>$/) {
    100          
    50          
    0          
137             # URI
138              
139             } elsif ($rs =~ /^(?:[_a-zA-Z]\w*)?:[a-zA-Z]\w*$/) {
140             # QName
141 1         2 my $bound = 0;
142 1         1 foreach (keys %{$self->{ns}->{'<>'}}) {
  1         5  
143 3 100 100     46 $rs =~ /^$_:(.*)$/ and $bound = 1 and last;
144             }
145 1 50       7 $self->_do_error(106, "$rs (subject: $s)") unless $bound;
146              
147             } elsif ($rs =~ /^"(?:\\"|[^\"])*"$/) {
148             # string1
149 1 50       4 $self->_do_error(202, "$rs (subject: $s)") unless $type eq 'l';
150              
151             } elsif ($rs =~ /^"""(.*)"""$/) {
152             # string2
153 0 0         $self->_do_error(202, "$rs (subject: $s)") unless $type eq 'l';
154              
155             } else {
156 0           $self->_do_error(201, "$rs (subject: $s)");
157             }
158             }
159              
160              
161             1;
162              
163              
164             __END__