Template/Document.pm

filter: Statementstmt 31.6% 25 / 79 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 57.1% 8 / 14 Podpod 100% 5 / 5 totaltotal 29.9% 38 / 127 CC 13 SCAR 40.6
CC
13
Cov
24%
extract
132.0
_check_resource
12.0
new
2.0
CRAP 57.8
« Template/SAXFilter.pm ↑ Summary Template/Unit.pm »
1 package RDF::RDFa::Template::Document;
2  
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 =head1 NAME
7  
8 RDF::RDFa::Template::Document - A parsed Template document
9  
10 =cut
11  
12 2 ▶use RDF::RDFa::Template::Unit;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use RDF::Query::Algebra::BasicGraphPattern;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use RDF::Query::Algebra::Triple;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use RDF::Query::Node::Variable;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 2 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
Pod documented
21 0 my $class = shift;
22 0 my $self = {
23 PARSED => shift,
24 UNITS => {},
25 SUBURI => 'http://www.kjetil.kjernsmo.net/software/rat/substitutions#',
26 RATURI => 'http://www.kjetil.kjernsmo.net/software/rat/xmlns',
27 };
28 0 bless ($self, $class);
29 0 return $self;
30 }
31  
32  
33 =head1 SYNOPSIS
34  
35 my $parser = RDF::RDFa::Parser->new($self->{RAT}, 'http://example.org/foo/',
36 {
37 use_rtnlx => 1,
38 graph => 1,
39 graph_type => 'about',
40 graph_attr => '{http://example.org/graph#}graph',
41 });
42 $parser->consume;
43 my $doc = RDF::RDFa::Template::Document->($parser);
44 $doc->extract;
45  
46 =head1 METHODS
47  
48 It implements the following methods:
49  
50 =head2 new
51  
52 The constructor. An L<RDF::RDFa::Parser> object is a required
53 argument.
54  
55 =head2 extract
56  
57 Extracts the Basic Graph Patterns from the parsed document. Returns
58 the number of patterns extracted.
59  
60 =head2 unit( $graph_name )
61  
62 Returns a RDF::RDFa::Template::Unit for the specified graph name.
63  
64 =head2 units
65  
66 Returns an array of RDF::RDFa::Template::Unit objects
67  
68 =head2 dom
69  
70 Return an XML::LibXML::Document of the parsed source document.
71  
72 =cut
73  
74 0 ▶sub dom {
Subroutine dom: not called CC=1 SCAR=6.9
Pod documented
75 0 my $self = shift;
76 0 return $self->{PARSED}->dom;
77 }
78  
79 0 ▶sub extract {
Subroutine extract: not called CC=11 SCAR=48.8
Pod documented
80 0 my $self = shift;
81 0 my $dom = $self->{PARSED}->dom;
82 0 my $return = 0;
83 0 my %units;
84 0 my %graphs = %{$self->{PARSED}->graphs};
85 0 ▶ while (my ($graph, $model) = each(%graphs)) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %graphs) 0 0 0
86 0 ▶ next if ($graph eq '_:RDFaDefaultGraph'); # We don't need the default graph
Branch: 1 branch
BranchTrueFalseTotal
if $graph eq "_:RDFaDefaultGraph" 0 0 0
87  
88 0 my $baseuri = $self->{PARSED}->uri;
89 0 my ($local_graph) = $graph =~ m/^$baseuri(.*?)$/;
90 # TODO: Don't hardcode the graph node name or the rat prefix
91 0 my $nodes = $dom->findnodes('//rat:graph[@g:graph = ' . "'$local_graph']" );
92 0 my @triples;
93 0 my $iterator = $model->as_stream;
94 0 ▶ while (my $statement = $iterator->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $statement = $iterator->next) 0 0 0
95 # Go through each statement to look for variables
96  
97 # First, lets check the object, which needs to be a XMLLiteral to be a variable
98 0 my $object = RDF::Query::Node->from_trine($statement->object);
99 0 ▶ if ($statement->object->isa('RDF::Trine::Node::Literal::XML')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($statement->object->isa("RDF::Trine::Node::Literal::XML")) 0 0 0
100 0 my $element = $statement->object->xml_element->firstChild; # TODO: Reliable?
101 0 ▶ if ($element->isa('XML::LibXML::Node')
Branch: 1 branch
BranchTrueFalseTotal
if ($element->isa("XML::LibXML::Node") and $element->namespaceURI eq $self->{"RATURI"} and $element->localname eq "variable") 0 0 0
Condition0%0/6
$element->isa("XML::LibXML::Node") and $element->namespaceURI eq $self->{"RATURI"} and $element->localname eq "variable"
!ll&&!rl&&r
000
$element->isa("XML::LibXML::Node") and $element->namespaceURI eq $self->{"RATURI"}
!ll&&!rl&&r
000
MC/DC0%0/3
$element->isa("XML::LibXML::Node") and $element->namespaceURI eq $self->{"RATURI"} and $element->localname eq "variable"
$element->isa("XML::LibXML::Node") $element->namespaceURI eq $self->{"RATURI"} $element->localname eq "variable"
Truth table0%0/4
A and B and C
A= $element->isa("XML::LibXML::Node")B= $element->namespaceURI eq $self->{"RATURI"}C= $element->localname eq "variable"
ABCresult
0XX0
10X0
1100
1111
102 && ($element->namespaceURI eq $self->{RATURI})
103 && ($element->localname eq 'variable')) {
104 # Now, we know that we have a variable
105 0 my $varname = $element->attributes->getNamedItem('name')->getValue();
106 0 ▶ if ($varname =~ m/^(\w*):(\w*)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($varname =~ /^(\w*):(\w*)$/) 0 0 0
107 0 my $prefix = $1;
108 0 my $localname = $2;
109 0 ▶ if ($dom->firstChild->lookupNamespaceURI($prefix) eq $self->{SUBURI}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dom->firstChild->lookupNamespaceURI($prefix) eq $self->{'SUBURI'}) { } 0 0 0
110 0 $object = RDF::Query::Node::Variable->new($localname);
111 } else {
112 0 carp "No variable found in the " . $element->nodeName . " field. Have you remember the $self->{SUBURI} namespace?";
113 }
114 }
115 }
116 }
117  
118  
119 0 my $newstatement = RDF::Query::Algebra::Triple->new($self->_check_resource($statement->subject),
120 $self->_check_resource($statement->predicate),
121 $object);
122 0 push(@triples, $newstatement);
123 }
124 0 $return++;
125 0 my $endpoint = undef;
126 0 my $node = $nodes->shift;
127 0 ▶ if ($node->attributes->getNamedItem('endpoint')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->attributes->getNamedItem("endpoint")) 0 0 0
128 0 $endpoint = $node->attributes->getNamedItem('endpoint')->getValue;
129 }
130 0 $units{$graph} = RDF::RDFa::Template::Unit->new(
131 triples => \@triples,
132 endpoint => $endpoint,
133 doc_graph => $graph);
134 0 $self->{UNITS} = \%units;
135 }
136 0 return $return;
137 }
138  
139 0 ▶sub unit {
Subroutine unit: not called CC=1 SCAR=6.9
Pod documented
140 0 my ($self, $graph_name) = @_;
141 0 return $self->{UNITS}->{$graph_name};
142 }
143  
144 0 ▶sub units {
Subroutine units: not called CC=1 SCAR=6.9
Pod documented
145 0 my $self = shift;
146 0 return values(%{$self->{UNITS}});
147 }
148  
149 0 ▶sub _check_resource {
Subroutine _check_resource: not called CC=3 SCAR=24.8
150 0 my ($self, $resource) = @_;
151 0 my $return = RDF::Query::Node->from_trine($resource); # Promotes the resource to a Query node
152 0 ▶ if ($resource->uri_value =~ m/^(\w*):(\w*)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($resource->uri_value =~ /^(\w*):(\w*)$/) 0 0 0
153 0 my $prefix = $1;
154 0 my $localname = $2;
155 0 ▶ if ($self->{PARSED}->dom->firstChild->lookupNamespaceURI( $prefix )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"PARSED"}->dom->firstChild->lookupNamespaceURI($prefix) eq $self->{"SUBURI"}) 0 0 0
156 eq $self->{SUBURI}) {
157 0 $return = RDF::Query::Node::Variable->new($localname);
158 }
159 }
160
161 0 return $return;
162 }
163  
164  
165 =head1 AUTHOR
166  
167 Kjetil Kjernsmo, C<< <kjetilk at cpan.org> >>
168  
169  
170 =head1 COPYRIGHT & LICENSE
171  
172 Copyright 2010 Kjetil Kjernsmo.
173  
174 This program is free software; you can redistribute it and/or modify it
175 under the same terms as Perl itself.
176  
177 =cut
178  
179 2 1;
« Template/SAXFilter.pm ↑ Summary Template/Unit.pm »