File Coverage

blib/lib/RDF/Query/Functions/Jena.pm
Criterion Covered Total %
statement 61 65 93.8
branch 11 16 68.7
condition 1 3 33.3
subroutine 12 12 100.0
pod 1 1 100.0
total 86 97 88.6


line stmt bran cond sub pod time code
1             =head1 NAME
2              
3             RDF::Query::Functions::Jena - Jena/ARQ work-alike functions
4              
5             =head1 VERSION
6              
7             This document describes RDF::Query::Functions::Jena version 2.915_01.
8              
9             =head1 DESCRIPTION
10              
11             Defines the following functions:
12              
13             =over
14              
15             =item * java:com.hp.hpl.jena.query.function.library.langeq
16              
17             =item * java:com.hp.hpl.jena.query.function.library.listMember
18              
19             =item * java:com.hp.hpl.jena.query.function.library.now
20              
21             =item * java:com.hp.hpl.jena.query.function.library.sha1sum
22              
23             =back
24              
25             =cut
26              
27             package RDF::Query::Functions::Jena;
28              
29 35     35   55859 use strict;
  35         85  
  35         902  
30 35     35   188 use warnings;
  35         81  
  35         907  
31 35     35   186 use Log::Log4perl;
  35         77  
  35         333  
32             our ($VERSION, $l);
33             BEGIN {
34 35     35   2982 $l = Log::Log4perl->get_logger("rdf.query.functions.jena");
35 35         14959 $VERSION = '2.915_01';
36             }
37              
38 35     35   224 use Digest::SHA qw(sha1_hex);
  35         75  
  35         1977  
39 35     35   208 use I18N::LangTags;
  35         77  
  35         1562  
40 35     35   182 use Scalar::Util qw(blessed reftype refaddr looks_like_number);
  35         75  
  35         23810  
41              
42             =begin private
43              
44             =item C<< install >>
45              
46             Documented in L<RDF::Query::Functions>.
47              
48             =end private
49              
50             =cut
51              
52             sub install {
53             RDF::Query::Functions->install_function(
54             ["http://jena.hpl.hp.com/ARQ/function#sha1sum", "java:com.hp.hpl.jena.query.function.library.sha1sum"],
55             sub {
56 2     2   8 my $query = shift;
57 2         7 my $node = shift;
58            
59 2         5 my $value;
60 2 100       28 if ($node->isa('RDF::Query::Node::Literal')) {
    50          
61 1         7 $value = $node->literal_value;
62             } elsif ($node->isa('RDF::Query::Node::Resource')) {
63 1         4 $value = $node->uri_value;
64             } else {
65 0         0 throw RDF::Query::Error::TypeError -text => "jena:sha1sum called without a literal or resource";
66             }
67 2         70 my $hash = sha1_hex( $value );
68 2         14 return RDF::Query::Node::Literal->new( $hash );
69             }
70 35     35 1 360 );
71            
72             RDF::Query::Functions->install_function(
73             ["http://jena.hpl.hp.com/ARQ/function#now", "java:com.hp.hpl.jena.query.function.library.now"],
74             sub {
75 2     2   9 my $query = shift;
76 2         27 my $dt = DateTime->now();
77 2 50       1005 my $f = ref($query) ? $query->dateparser : DateTime::Format::W3CDTF->new;
78 2         26 my $value = $f->format_datetime( $dt );
79 2         183 return RDF::Query::Node::Literal->new( $value, undef, 'http://www.w3.org/2001/XMLSchema#dateTime' );
80             }
81 35         327 );
82            
83             RDF::Query::Functions->install_function(
84             ["http://jena.hpl.hp.com/ARQ/function#langeq", "java:com.hp.hpl.jena.query.function.library.langeq"],
85             sub {
86 2     2   5 my $query = shift;
87 2         7 my $node = shift;
88 2         4 my $lang = shift;
89 2         11 my $litlang = $node->literal_value_language;
90 2         18 my $match = $lang->literal_value;
91 2 100       22 return I18N::LangTags::is_dialect_of( $litlang, $match )
92             ? RDF::Query::Node::Literal->new('true', undef, 'http://www.w3.org/2001/XMLSchema#boolean')
93             : RDF::Query::Node::Literal->new('false', undef, 'http://www.w3.org/2001/XMLSchema#boolean');
94             }
95 35         349 );
96            
97             RDF::Query::Functions->install_function(
98             ["http://jena.hpl.hp.com/ARQ/function#listMember", "java:com.hp.hpl.jena.query.function.library.listMember"],
99             sub {
100 3     3   5 my $query = shift;
101            
102 3         5 my $list = shift;
103 3         5 my $value = shift;
104            
105 3         10 my $first = RDF::Query::Node::Resource->new( 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first' );
106 3         51 my $rest = RDF::Query::Node::Resource->new( 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest' );
107            
108 3         36 my $result;
109 3         14 LIST: while ($list) {
110 6 50 33     93 if ($list->is_resource and $list->uri_value eq 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil') {
111 0         0 return RDF::Query::Node::Literal->new('false', undef, 'http://www.w3.org/2001/XMLSchema#boolean');
112             } else {
113 6         73 my $stream = $query->model->get_statements( $list, $first, undef );
114 6         19896 while (my $stmt = $stream->next()) {
115 6         677 my $member = $stmt->object;
116 6 100       44 return RDF::Query::Node::Literal->new('true', undef, 'http://www.w3.org/2001/XMLSchema#boolean') if ($value->equal( $member ));
117             }
118            
119 3         122 my $stmt = $query->model->get_statements( $list, $rest, undef )->next();
120 3 50       10128 return RDF::Query::Node::Literal->new('false', undef, 'http://www.w3.org/2001/XMLSchema#boolean') unless ($stmt);
121            
122 3         11 my $tail = $stmt->object;
123 3 50       20 if ($tail) {
124 3         47 $list = $tail;
125 3         34 next; #next LIST;
126             } else {
127 0           return RDF::Query::Node::Literal->new('false', undef, 'http://www.w3.org/2001/XMLSchema#boolean');
128             }
129             }
130             }
131            
132 0           return RDF::Query::Node::Literal->new('false', undef, 'http://www.w3.org/2001/XMLSchema#boolean');
133             }
134 35         357 );
135             }
136              
137             1;
138              
139             __END__
140              
141             =head1 AUTHOR
142              
143             Gregory Williams <gwilliams@cpan.org>.
144              
145             =cut