Core/Serializer.pm untested

filter: Statementstmt 0.0% 0 / 167 Branchbran 0.0% 0 / 52 Conditioncond 0.0% 0 / 29 MC/DCmcdc 0.0% 0 / 18 Subroutinesub 0.0% 0 / 22 Podpod 0.0% 0 / 20 totaltotal 0.0% 0 / 308 CC 27 SCAR 33.4
CC
27
Cov
0%
_descriptionNext
90.0
_predicateOpen
42.0
new
20.0
CRAP 28.2
1 #
2 # The contents of this file are subject to the Mozilla Public
3 # License Version 1.1 (the "License"); you may not use this file
4 # except in compliance with the License. You may obtain a copy of
5 # the License at http://www.mozilla.org/MPL/
6 #
7 # Software distributed under the License is distributed on an "AS
8 # IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
9 # implied. See the License for the specific language governing
10 # rights and limitations under the License.
11 #
12 # The Original Code is the RDF::Core module
13 #
14 # The Initial Developer of the Original Code is Ginger Alliance Ltd.
15 # Portions created by Ginger Alliance are
16 # Copyright (C) 2001 Ginger Alliance Ltd.
17 # All Rights Reserved.
18 #
19 # Contributor(s):
20 #
21 # Alternatively, the contents of this file may be used under the
22 # terms of the GNU General Public License Version 2 or later (the
23 # "GPL"), in which case the provisions of the GPL are applicable
24 # instead of those above. If you wish to allow use of your
25 # version of this file only under the terms of the GPL and not to
26 # allow others to use your version of this file under the MPL,
27 # indicate your decision by deleting the provisions above and
28 # replace them with the notice and other provisions required by
29 # the GPL. If you do not delete the provisions above, a recipient
30 # may use your version of this file under either the MPL or the
31 # GPL.
32 #
33  
34 package RDF::Core::Serializer;
35  
36 use strict;
37  
38  
39 use Carp;
40  
41 sub new {
42 my ($pkg,%options) = @_;
43 $pkg = ref $pkg || $pkg;
44 my $self = {};
45 carp "InlineURI parameter is deprecated" if $self->{_options}->{InlineURI};
46 #Implemented options are:
47 #getNamespaces, getSubjects, getStatements, existsStatement callback functions
48 #output - output filehandle reference (a reference to a typeglob or FileHandle) or scalar variable reference (default \*STDOUT)
49 $self->{_options} = \%options;
50 $self->{_options}->{Output} = \*STDOUT
51 unless defined $self->{_options}->{Output};
52 # $self->{_options}->{BaseURI};
53 $self->{_options}->{InlinePrefix} ||= 'genid'
54 unless defined $self->{_options}->{InlinePrefix};
55 $self->{_descriptions} = undef;
56 $self->{_namespaces} = undef;
57 $self->{_recursionlvl} = 0;
58 $self->{_idAttr} = 1;
59 $self->{_anonym} = undef;
60 bless $self, $pkg;
61 }
62 sub getOptions {
63 my $self = shift;
64 return $self->{_options};
65 }
66 sub serialize {
67 my $self = shift;
68 #get options if passed
69 $self->{_options} = $_[0]
70 if (@_ gt 0);
71 $self->_rdfOpen;
72 my $description = $self->_descriptionNext;
73 while (defined $description) {
74 $self->_descriptionProcess($description);
75 $description = $self->_descriptionNext;
76 }
77 $self->_rdfClose;
78 }
79 #callback functions
80 sub getNamespaces {
81 my $self = shift;
82 $self->{_namespaces} ||= &{$self->getOptions->{getNamespaces}}(@_);
83 return $self->{_namespaces};
84 }
85 sub getSubjects {
86 #Subjects are stored with 2 flags that say that corresponding description item was open/closed.
87 #Array ($subject, openedFlag, closedFlag) will be called description not to mess with $subject itself (RDF::Core::Resource instance)
88 my $self = shift;
89 $self->{_descriptions} ||= &{$self->getOptions->{getSubjects}}(@_);
90 return $self->{_descriptions};
91 }
92 sub getStatements {
93 my $self = shift;
94 return &{$self->getOptions->{getStatements}}(@_);
95 }
96 sub countStatements {
97 my $self = shift;
98 return &{$self->getOptions->{countStatements}}(@_);
99 }
100 sub existsStatement {
101 my $self = shift;
102 return &{$self->getOptions->{existsStatement}}(@_);
103 }
104 sub _rdfOpen {
105 my $self = shift;
106 my $namespaces = '';
107 foreach (keys %{$self->getNamespaces()}) {
108 $namespaces .= "xmlns:".$self->getNamespaces->{$_}."=\"$_\"\n";
109 }
110 $self->_print ("<rdf:RDF\n$namespaces>\n");
111 }
112 sub _rdfClose {
113 my $self = shift;
114 $self->_print ("</rdf:RDF>\n");
115 }
116 #get next description to be processed
117 sub _descriptionNext {
118 my $self = shift;
119 my $retval = undef;
120 my %searched;
121 #first, look for subjects that are not objects of any statement
122 foreach (values %{$self->getSubjects}) {
123 unless ($_->[1]) { #search in not yet opened descriptions
124 unless ($self->existsStatement(undef,undef,$_->[0])) {
125 $retval = $_;
126 last;
127 }
128 }
129 }
130 #then look for subjects that are objects of a statement already serialized
131 unless ($retval) {
132 foreach (values %{$self->getSubjects}) {
133 unless ($_->[1]) { #search in not yet opened descriptions
134 my $enum = $self->getStatements(undef,undef,$_->[0]);
135 my $stmt = $enum->getNext;
136 while (defined $stmt) {
137 if ($self->getSubjects->{$stmt->getSubject->getURI}->[2]) {
138 $retval = $_;
139 last;
140 }
141 $stmt = $enum->getNext;
142 }
143 $enum->close;
144 }
145 last if $retval;
146 }
147 }
148 #at last, return any subject not serialized yet
149 unless ($retval) {
150 foreach (values %{$self->getSubjects}) {
151 unless ($_->[1]) { #search in not yet opened descriptions
152 $retval = $_;
153 last;
154 }
155 }
156 }
157 return $retval;
158 }
159 sub _descriptionProcess {
160 my ($self, $description) = @_;
161 $self->_descriptionOpen($description);
162 my $enumerator = $self->getStatements($description->[0],undef,undef);
163 my $statement = $enumerator->getNext;
164 while (defined $statement) {
165 $self->_descriptionData($statement);
166 $statement = $enumerator->getNext;
167 }
168 $enumerator->close;
169 $self->_descriptionClose($description);
170 }
171 sub _descriptionOpen {
172 my ($self,$description) = @_;
173 my $subjectID = $description->[0]->getURI;
174 my $idAboutAttr;
175 #Anonymous subject can be serialized as anonymous if it's an object of one or zero statements
176 #and the referencing statement's subject has already been opened
177 my $InlineURI = "_:";
178 my $baseURI = $self->getOptions->{BaseURI};
179 if ($subjectID =~ /^$InlineURI/i) {
180 my $cnt = $self->countStatements(undef,undef,$description->[0]);
181 if (!$cnt || ($self->{_recursionlvl} && $cnt < 2)){
182 $idAboutAttr = '';
183 } else {
184 #deanonymize resource
185 my $idNew = $self->getOptions->{InlinePrefix}.$self->{idAttr}++;
186 $idAboutAttr = " rdf:nodeID=\"$idNew\"";
187 #carp "Giving attribute $idAboutAttr to blank node $subjectID.";
188 #store its ID to reference it in other statements
189 $self->{_anonym}->{$subjectID} = $idNew;
190 }
191 } elsif ($baseURI && $subjectID =~ /^$baseURI/i) {
192 #relative URI - choose whether idAttr or aboutAttr should be produced
193 #suggestion - produce aboutAttr every time
194 my $id = $';
195 # $id =~ s/^#//
196 # if $baseURI !~ /#$/;
197 $idAboutAttr = " rdf:about=\"$'\"";
198 } else {
199 #absolute URI - produce aboutAttr
200 $idAboutAttr = " rdf:about=\"$subjectID\"";
201 }
202 $idAboutAttr = $self->_escapeXML($idAboutAttr);
203 $self->_print ("<rdf:Description$idAboutAttr>\n");
204 $self->{_recursionlvl}++;
205 $description->[1] = 1;
206 }
207 sub _descriptionClose {
208 my ($self,$description) = @_;
209 $self->{_recursionlvl}--;
210 $self->_print ("</rdf:Description>\n");
211 $description->[2] = 1;
212 }
213 sub _predicateOpen {
214 my ($self,$statement,$inline) = @_; #inline says that nested resource should be scripted inline, not referenced
215 my $prefix = $self->{_namespaces}->{$statement->getPredicate->getNamespace};
216 my $propName = $prefix . ":".$statement->getPredicate->getLocalValue;
217 my $propertyElt;
218 if ($statement->getObject->isLiteral) {
219 #don't express xml:lang if not necessary
220 my $lang = $statement->getObject->getLang ?
221 " xml:lang=\"".($statement->getObject->getLang)."\"" : "";
222 my $datatype = $statement->getObject->getDatatype ?
223 " rdf:datatype=\"".$statement->getObject->getDatatype."\"" : "";
224 $propertyElt="<${propName}${lang}${datatype}>";
225 } else {
226 if ($inline) {
227 $propertyElt="<$propName>\n";
228 } else {
229 my $objectURI = $statement->getObject->getURI;
230 my $resAttr = "rdf:resource";
231 if ($self->{_anonym}->{$objectURI}) {
232 $objectURI = $self->{_anonym}->{$objectURI};
233 $resAttr = "rdf:nodeID";
234 }
235 $propertyElt="<$propName $resAttr=\"".$self->_cutBaseURI($objectURI)."\"/>\n";
236 }
237 }
238 $self->_print ($propertyElt);
239 }
240 sub _predicateClose {
241 my ($self,$statement,$inline) = @_;
242 my $prefix = $self->{_namespaces}->{$statement->getPredicate->getNamespace};
243 my $propName = $prefix . ":".$statement->getPredicate->getLocalValue;
244 my $propertyElt;
245 if ($inline || $statement->getObject->isLiteral) {
246 $propertyElt="</$propName>\n";
247 } else {
248 $propertyElt="";
249 }
250 $self->_print ($propertyElt);
251 }
252 sub _descriptionData {
253 my ($self,$statement) = @_;
254 if (!$statement->getObject->isLiteral && #object is resource
255 defined $self->getSubjects->{$statement->getObject->getURI} && #and statement about the resource exists
256 !$self->getSubjects->{$statement->getObject->getURI}->[1]) { #and is opened not yet
257 $self->_predicateOpen($statement,1);
258 $self->_descriptionProcess($self->getSubjects->{$statement->getObject->getURI});
259 $self->_predicateClose($statement,1);
260 } else {
261 $self->_predicateOpen($statement,0);
262 if ($statement->getObject->isLiteral) {
263 my $literal = $statement->getObject->getValue;
264 $self->_print($self->_escapeXML($literal));
265 }
266 $self->_predicateClose($statement,0);
267  
268 }
269 }
270 sub _print {
271 my ($self,@params) = @_;
272 if (ref($self->{_options}->{Output}) eq 'SCALAR') {
273 foreach (@params) {
274 ${$self->getOptions->{Output}} .= $_;
275 }
276 } elsif (ref($self->{_options}->{Output}) =~ /^GLOB|^FileHandle/) {
277 print {$self->getOptions->{Output}} @params;
278 }
279  
280 }
281 sub _cutBaseURI {
282 my ($self, $uriRef) = @_;
283 my $baseURI = $self->getOptions->{BaseURI};
284 if ($baseURI) {
285 $uriRef =~ s/^$baseURI//i;
286 }
287 return $uriRef;
288 }
289  
290 sub _escapeXML {
291 my ($self, $string) = @_;
292 $string =~ s/\&/\&amp;/g;
293 $string =~ s/\</\&lt;/g;
294 return $string;
295 }
296  
297 1;
298 __END__