File Coverage

blib/lib/ODO/Jena/Node.pm
Criterion Covered Total %
statement 21 53 39.6
branch 0 26 0.0
condition 0 8 0.0
subroutine 7 18 38.8
pod 3 5 60.0
total 31 110 28.1


line stmt bran cond sub pod time code
1             #
2             # Copyright (c) 2005-2006 IBM Corporation.
3             #
4             # All rights reserved. This program and the accompanying materials
5             # are made available under the terms of the Eclipse Public License v1.0
6             # which accompanies this distribution, and is available at
7             # http://www.eclipse.org/legal/epl-v10.html
8             #
9             # File: $Source: /var/lib/cvs/ODO/lib/ODO/Jena/Node.pm,v $
10             # Created by: Stephen Evanchik( evanchik@us.ibm.com )
11             # Created on: 12/20/2006
12             # Revision: $Id: Node.pm,v 1.3 2009-11-25 17:58:25 ubuntu Exp $
13             #
14             # Contributors:
15             # IBM Corporation - initial API and implementation
16             #
17             package ODO::Jena::Node;
18              
19 1     1   1004 use strict;
  1         2  
  1         28  
20 1     1   6 use warnings;
  1         2  
  1         28  
21              
22 1     1   5 use vars qw /$VERSION/;
  1         1  
  1         78  
23             $VERSION = sprintf "%d.%02d", q$Revision: 1.3 $ =~ /: (\d+)\.(\d+)/;
24              
25 1         8 use Class::Interfaces('ODO::Jena::Node'=>
26             {
27             'methods'=> [ 'serialize' ],
28             }
29 1     1   887 );
  1         728  
30              
31             =head1 NAME
32              
33             ODO::Jena::Node - Jena node definitions
34              
35             =head1 SYNOPSIS
36              
37             use ODO::Jena::Node;
38              
39             =head1 DESCRIPTION
40              
41             Description.
42              
43             =head1 COMMON METHODS
44              
45             =over
46              
47             =item reference( )
48              
49             =item reference( $boolean )
50              
51             Manipulates whether or not this object is a reference in to the LONG_* tables
52              
53             Parameters:
54             $boolean - Optional. If set, the reference will be set to the given parameter.
55              
56             Returns:
57             Whether or not this object is a reference.
58              
59             =item is_reference( )
60              
61             =cut
62              
63             sub is_reference {
64 0     0 1 0 my $self = shift;
65 0 0       0 return ($self->reference() ? 1 : 0);
66             }
67              
68              
69             =item is_value( )
70              
71             =cut
72              
73             sub is_value( ) {
74 0     0 1 0 my $self = shift;
75 0 0       0 return (!$self->reference() ? 1 : 0);
76             }
77              
78             sub reference {
79 0     0 1 0 my $self = shift;
80 0 0       0 defined($_[0]) ? $self->{'reference'} = $_[0] : return $self->{'reference'};
81             }
82              
83              
84             sub long_id {
85 0     0 0 0 my $self = shift;
86 0 0       0 defined($_[0]) ? $self->{'long_id'} = $_[0] : return $self->{'long_id'};
87             }
88              
89              
90             =item jena_node( $node )
91              
92             =cut
93              
94             sub to_jena_node {
95 0     0 0 0 my ($self, $node) = @_;
96            
97 0 0       0 return $node
98             if(UNIVERSAL::isa($node, 'ODO::Jena::Node'));
99            
100 0         0 my $node_ref = ref $node;
101 0         0 my ($type) = $node_ref =~ m/::(Literal|Resource|Blank|Variable|Any)$/;
102            
103 0         0 return bless $node, "ODO::Jena::Node::${type}";
104             }
105              
106              
107             =back
108              
109             =head1 NODE TYPES
110              
111             =cut
112              
113              
114             package ODO::Jena::Node::Literal;
115              
116             our @ISA = qw/ODO::Jena::Node ODO::Node::Literal/;
117              
118             =head2 ODO::Jena::Node::Literal
119              
120             =over
121              
122             =item serialize( )
123              
124             Literal node encoding:
125             Short: Lv:[length(language)]:[length(datatype)]:[language][datatype]value[:]
126             Long: Lr:long_id
127              
128             Literal node encoding for long literals:
129             Lv:[length(language)]:[length(datatype)]:[language][datatype]head[:] hash tail
130              
131             =cut
132              
133             sub serialize {
134 0     0   0 my $self = shift;
135              
136 0 0       0 if($self->is_value()) {
    0          
137 0 0       0 return ${ODO::Jena::Node::Constants::LITERAL_HEADER}
    0          
138             . ${ODO::Jena::Node::Constants::VALUE_DELIMITER}
139             . ':' . ($self->language() ? length($self->language()) : '0')
140             . ':' . ($self->datatype() ? length($self->datatype()) : '')
141             . ':' . $self->value()
142             . ':';
143             }
144             elsif($self->is_reference()) {
145 0         0 return ${ODO::Jena::Node::Constants::LITERAL_HEADER}
146             . ${ODO::Jena::Node::Constants::REFERENCE_DELIMITER}
147             . ':' . $self->long_id();
148             }
149             else {
150             # TODO: Throw an exception
151             }
152             }
153              
154              
155             sub init {
156 1     1   28 my ($self, $config) = @_;
157 1         9 $self->params($config, qw/reference long_id/);
158 1         36 return $self->SUPER::init($config);
159             }
160              
161             =back
162              
163             =cut
164              
165              
166             package ODO::Jena::Node::Resource;
167              
168             our @ISA = qw/ODO::Jena::Node ODO::Node::Resource/;
169              
170 1     1   592 use ODO::Node;
  1         1  
  1         716  
171              
172             __PACKAGE__->mk_ro_accessors(qw/prefix_id/);
173              
174             =head2 ODO::Jena::Node::Resource
175              
176             =over
177              
178             =item serialize( )
179              
180             URI node encoding:
181             Short: Uv:[prefix_id]:value[:]
182             Long: Ur:[prefix_id]:long_id
183              
184             URI node encoding for long URIs:
185             Uv:head[:] hash tail
186              
187             =cut
188              
189             sub serialize {
190 0     0   0 my $self = shift;
191              
192 0 0       0 if($self->is_value()) {
    0          
193 0   0     0 return ${ODO::Jena::Node::Constants::RESOURCE_HEADER}
194             . ${ODO::Jena::Node::Constants::VALUE_DELIMITER}
195             . ':' . ($self->prefix_id() || '') . ':'
196             . $self->value()
197             . ':';
198             }
199             elsif($self->is_reference()) {
200 0   0     0 return ${ODO::Jena::Node::Constants::RESOURCE_HEADER}
201             . ${ODO::Jena::Node::Constants::REFERENCE_DELIMITER}
202             . ($self->prefix_id() || '') . ':'
203             . $self->long_id();
204             }
205             else {
206             # TODO: Throw an exception
207             }
208             }
209              
210              
211             sub init {
212 2     2   63 my ($self, $config) = @_;
213 2         15 $self->params($config, qw/prefix_id reference long_id/);
214 2         111 return $self->SUPER::init($config);
215             }
216              
217             =back
218              
219             =cut
220              
221              
222             package ODO::Jena::Node::Blank;
223              
224             our @ISA = qw/ODO::Jena::Node::Resource/;
225              
226             =head2 ODO::Jena::Node::Blank
227              
228             =over
229              
230             =item serialize( )
231              
232             Blank node encoding:
233             Short: Bv:[prefix_id]:value[:]
234             Long: Br:[prefix_id]:long_id
235              
236             Blank node encoding for long bnodes:
237             Bv:head[:] hash tail
238              
239             =cut
240              
241             sub serialize {
242 0     0     my $self = shift;
243              
244 0 0         if($self->is_value()) {
    0          
245 0   0       return ${ODO::Jena::Node::Constants::BLANK_HEADER}
246             . ${ODO::Jena::Node::Constants::VALUE_DELIMITER}
247             . ($self->prefix_id() || '') . ':'
248             . $self->value()
249             . ':';
250             }
251             elsif ( $self->is_reference() ) {
252 0   0       return ${ODO::Jena::Node::Constants::BLANK_HEADER}
253             . ${ODO::Jena::Node::Constants::REFERENCE_DELIMITER}
254             . ($self->prefix_id() || '') . ':'
255             . $self->long_id();
256             }
257             else {
258             # TODO: Throw an exception
259             }
260             }
261              
262              
263             =back
264              
265             =cut
266              
267              
268             package ODO::Jena::Node::Variable;
269              
270             our @ISA = qw/ODO::Jena::Node ODO::Node::Variable/;
271              
272             =head2 ODO::Jena::Node::Variable
273              
274             =over
275              
276             =item serialize( )
277              
278             Variable node encoding: 'Vv:name'
279              
280             =cut
281              
282             sub serialize {
283 0     0     my $self = shift;
284             return
285 0           ${ODO::Jena::Node::Constants::VARIABLE_HEADER}
286             . ${ODO::Jena::Node::Constants::VALUE_DELIMITER}
287             . $self->name();
288             }
289              
290             sub init {
291 0     0     my ($self, $config) = @_;
292 0           $self->params($config, qw/reference long_id/);
293 0           return $self->SUPER::init($config);
294             }
295              
296             =back
297              
298             =cut
299              
300              
301             package ODO::Jena::Node::Any;
302              
303             our @ISA = qw/ODO::Jena::Node ODO::Graph::Node::Any/;
304              
305             =head2 ODO::Jena::Node::Any
306              
307             =over
308              
309             =item serialize( )
310              
311             Any node encoding: 'Av:'
312              
313             =cut
314              
315             sub serialize {
316 0     0     my $self = shift;
317             return
318 0           ${ODO::Jena::Node::Constants::ANY_HEADER}
319             . ${ODO::Jena::Node::Constants::VALUE_DELIMITER}
320             . ':';
321             }
322              
323             =back
324              
325             =cut
326              
327              
328             package ODO::Jena::Node::Constants;
329              
330             our $LITERAL_HEADER = 'L';
331             our $RESOURCE_HEADER = 'U';
332             our $VARIABLE_HEADER = 'V';
333             our $BLANK_HEADER = 'B';
334             our $ANY_HEADER = 'A';
335              
336             our $VALUE_DELIMITER = 'v';
337             our $REFERENCE_DELIMITER = 'r';
338              
339             =head1 AUTHOR
340              
341             IBM Corporation
342              
343             =head1 SEE ALSO
344              
345             L, L, L
346              
347             =head1 COPYRIGHT
348              
349             Copyright (c) 2004-2006 IBM Corporation.
350              
351             All rights reserved. This program and the accompanying materials
352             are made available under the terms of the Eclipse Public License v1.0
353             which accompanies this distribution, and is available at
354             http://www.eclipse.org/legal/epl-v10.html
355              
356             =cut
357              
358              
359             1;
360              
361             __END__