File Coverage

blib/lib/RDF/Simple/Serialiser/N3.pm
Criterion Covered Total %
statement 13 15 86.6
branch n/a
condition n/a
subroutine 5 5 100.0
pod n/a
total 18 20 90.0


line stmt bran cond sub pod time code
1            
2             # $Id: N3.pm,v 1.17 2009-07-04 14:28:18 Martin Exp $
3            
4             =head1 NAME
5            
6             RDF::Simple::Serialiser::N3 - Output RDF triples in Notation3 format
7            
8             =head1 SYNOPSIS
9            
10             Same as L,
11             except when you call serialise(),
12             you get back a string in Notation3 format.
13            
14             =head1 PRIVATE METHODS
15            
16             =over
17            
18             =cut
19            
20             package RDF::Simple::Serialiser::N3;
21            
22 2     2   49942 use strict;
  2         7  
  2         85  
23 2     2   13 use warnings;
  2         3  
  2         56  
24            
25 2     2   1522 use Data::Dumper; # for debugging only
  2         8633  
  2         166  
26 2     2   1245 use Regexp::Common;
  2         2945  
  2         18  
27             # We need the version with the new render() method:
28 2     2   78129 use RDF::Simple::Serialiser 1.007;
  0            
  0            
29            
30             use base 'RDF::Simple::Serialiser';
31            
32             our
33             $VERSION = do { my @r = (q$Revision: 1.17 $ =~ /\d+/g); sprintf "%d."."%03d" x $#r, @r };
34            
35             use constant DEBUG => 0;
36            
37             =item render
38            
39             This method does all the Notation3 formatting.
40             You should not be calling this method,
41             you should be calling the serialise() method.
42            
43             =cut
44            
45             sub render
46             {
47             my $self = shift;
48             # Required arg1 = arrayref:
49             my $raObjects = shift;
50             # Required arg2 = hashref of namespaces:
51             my $rhNS = shift;
52             my $sRet = q{};
53             foreach my $sNS (keys %$rhNS)
54             {
55             if (defined $rhNS->{$sNS} && ($rhNS->{$sNS}ne q{}))
56             {
57             $sRet .= qq"\@prefix $sNS: <$rhNS->{$sNS}> .\n";
58             $self->{_iTriples_}++;
59             } # if
60             } # foreach
61             $sRet .= qq{\n};
62             my %hsClassPrinted;
63             OBJECT:
64             foreach my $object (@$raObjects)
65             {
66             DEBUG && print STDERR " DDD in render(), object is ", Dumper($object);
67             # We delete elements as we process them, so that during debugging
68             # we can see what's leftover:
69             my $sId = delete $object->{NodeId} || q{};
70             if ($sId ne q{})
71             {
72             $sId = qq{:$sId};
73             }
74             else
75             {
76             $sId = delete $object->{Uri};
77             }
78             my $sClass = delete $object->{Class};
79             if (! $sClass)
80             {
81             print STDERR " EEE object has no Class: ", Dumper($object);
82             next OBJECT;
83             } # if
84             if ($sClass !~ m/[^:]+:[^:]+/)
85             {
86             # Class is not explicitly qualified with a "prefix:", ergo now
87             # explicitly qualify in the default namespace:
88             $sClass = qq{:$sClass};
89             if (! $hsClassPrinted{$sClass})
90             {
91             $sRet .= qq{$sClass a owl:Class .\n\n};
92             $self->{_iTriples_}++;
93             $hsClassPrinted{$sClass}++;
94             } # if
95             } # if
96             $sRet .= qq{$sId a $sClass .\n};
97             $self->{_iTriples_}++;
98             if ($object->{Uri})
99             {
100             $sRet .= qq{$sId rdf:about <$object->{Uri}> .\n};
101             $self->{_iTriples_}++;
102             delete $object->{Uri};
103             } # if
104             LITERAL:
105             foreach my $sProp (keys %{$object->{literal}})
106             {
107             LITERAL_PROPERTY:
108             foreach my $sVal (@{$object->{literal}->{$sProp}})
109             {
110             if ($sVal !~ m/\A$RE{num}{decimal}\z/)
111             {
112             # Value is non-numeric; assume it's a string and put quotes
113             # around it:
114             $sVal = qq{"$sVal"};
115             } # if
116             $sRet .= qq{$sId $sProp $sVal .\n};
117             $self->{_iTriples_}++;
118             } # foreach LITERAL_PROPERTY
119             } # foreach LITERAL
120             delete $object->{literal};
121             NODEID:
122             foreach my $sProp (keys %{$object->{nodeid}})
123             {
124             NODEID_PROPERTY:
125             foreach my $sVal (@{$object->{nodeid}->{$sProp}})
126             {
127             $sRet .= qq{$sId $sProp :$sVal .\n};
128             $self->{_iTriples_}++;
129             } # foreach NODEID_PROPERTY
130             } # foreach NODEID
131             delete $object->{nodeid};
132             RESOURCE:
133             foreach my $sProp (keys %{$object->{resource}})
134             {
135             RESOURCE_PROPERTY:
136             foreach my $sVal (@{$object->{resource}->{$sProp}})
137             {
138             if ($self->_looks_like_uri($sVal))
139             {
140             $sVal = qq{<$sVal>};
141             } # if
142             $sRet .= qq{$sId $sProp $sVal .\n};
143             $self->{_iTriples_}++;
144             } # foreach RESOURCE_PROPERTY
145             } # foreach RESOURCE
146             delete $object->{resource};
147             print STDERR Dumper($object) if keys %$object;
148             $sRet .= qq{\n};
149             } # foreach OBJECT
150             return $sRet;
151             } # render
152            
153            
154             =back
155            
156             =head1 PUBLIC METHODS
157            
158             =over
159            
160             =item get_triple_count
161            
162             Returns the number of triples created since the last call to
163             reset_triple_count().
164            
165             =cut
166            
167             sub get_triple_count
168             {
169             my $self = shift;
170             return $self->{_iTriples_};
171             } # get_triple_count
172            
173            
174             =item reset_triple_count
175            
176             Resets the internal counter of triples to zero.
177            
178             =cut
179            
180             sub reset_triple_count
181             {
182             my $self = shift;
183             $self->{_iTriples_} = 0;
184             } # get_triple_count
185            
186             1;
187            
188             __END__