Template/SAXFilter.pm

filter: Statementstmt 28.7% 19 / 66 Branchbran 0.0% 0 / 32 Conditioncond 0.0% 0 / 14 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 66.6% 6 / 9 Podpod 100% 3 / 3 totaltotal 21.2% 28 / 132 CC 22 SCAR 53.4
CC
22
Cov
17%
start_element
342.0
end_element
20.0
new
6.0
CRAP 208.5
↑ Summary Template/Document.pm »
1 package RDF::RDFa::Template::SAXFilter;
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::SAXFilter - A SAX Filter that does variable insertions and removes templates
9  
10 =cut
11  
12 2 ▶use RDF::RDFa::Template::Unit;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use base qw(XML::SAX::Base);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 2 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
Pod documented
19 0 my ($class, %options) = @_;
20 0 ▶ $options{BaseURI} ||= './';
Condition0%0/2
$options{"BaseURI"} ||= "./"
l!l
00
MC/DC0%0/1
$options{"BaseURI"} ||= "./"
$options{"BaseURI"}
Truth table0%0/2
A
A= $options{"BaseURI"}
Aresult
11
00
21 0 $options{_is_in_graph} = 0;
22 0 $options{_currentgraph} = undef;
23 0 return bless \%options, $class;
24 }
25  
26  
27 0 ▶sub start_element {
Subroutine start_element: not called CC=18 SCAR=58.3
Pod documented
28 0 my ($self, $element) = @_;
29 # $self->{Doc} here is set by the parameter used in the call to this
30 # module, which is usually set to $self->{DOC} in the calling
31 # code. TODO: Use methods here.
32 0 ▶ if ($element->{Attributes}->{'{' . $self->{Doc}->{RATURI} . '}doctype-system'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($element->{"Attributes"}{'{' . $self->{"Doc"}{"RATURI"} . '}doctype-system'}) 0 0 0
33 0 delete $element->{Attributes}->{'{' . $self->{Doc}->{RATURI} . '}doctype-system'};
34 }
35 0 ▶ if ($element->{Attributes}->{'{' . $self->{Doc}->{RATURI} . '}doctype-public'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($element->{"Attributes"}{'{' . $self->{"Doc"}{"RATURI"} . '}doctype-public'}) 0 0 0
36 0 delete $element->{Attributes}->{'{' . $self->{Doc}->{RATURI} . '}doctype-public'};
37 }
38 0 ▶ if ($element->{Attributes}->{'{}datatype'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($element->{"Attributes"}{"{}datatype"}) 0 0 0
39 0 $self->{_element_with_datatype} = $element;
40 }
41 # TODO: RATURI method
42 0 ▶ if (defined($element->{NamespaceURI}) && ($element->{NamespaceURI} eq $self->{Doc}->{RATURI})) {
Branch: 4 branches
BranchTrueFalseTotal
if (defined $element->{'NamespaceURI'} and $element->{'NamespaceURI'} eq $self->{'Doc'}{'RATURI'}) { } 0 0 0
elsif ($self->{'_is_in_graph'} and $element->{'Attributes'}{'{}about'} and $element->{'Attributes'}{'{}about'}{'Value'} =~ /sub:(\w+)/) { } 0 0 0
elsif ($self->{'_element_with_datatype'}) { } 0 0 0
elsif ($self->{'_is_in_graph'}) { } 0 0 0
Condition0%0/3
defined $element->{"NamespaceURI"} and $element->{"NamespaceURI"} eq $self->{"Doc"}{"RATURI"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $element->{"NamespaceURI"} and $element->{"NamespaceURI"} eq $self->{"Doc"}{"RATURI"}
defined $element->{"NamespaceURI"} $element->{"NamespaceURI"} eq $self->{"Doc"}{"RATURI"}
Truth table0%0/3
A and B
A= defined $element->{"NamespaceURI"}B= $element->{"NamespaceURI"} eq $self->{"Doc"}{"RATURI"}
ABresult
0X0
100
111
43 0 ▶ if ($element->{LocalName} eq 'graph') {
Branch: 2 branches
BranchTrueFalseTotal
if ($element->{'LocalName'} eq 'graph') { } 0 0 0
elsif ($element->{'LocalName'} eq 'variable') { } 0 0 0
44 # This element should not be sent to the result document,
45 # but its contents should be looped and variables substituted
46 0 $self->{_currentgraph} = $element->{Attributes}->{'{http://example.org/graph#}graph'}->{Value}; # TODO: Fix, don't require hardcoding
47 0 ▶ if (defined($self->{_currentgraph})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"_currentgraph"}) 0 0 0
48 0 $self->{_is_in_graph} = 1;
49 }
50 0 ▶ croak "couldn't find current graph name" unless $self->{_currentgraph};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_currentgraph"} 0 0 0
51 0 $self->{_results} = $self->{Doc}->unit($self->{Doc}->{PARSED}->uri . $self->{_currentgraph})->results; # TODO: PARSED method
52 } elsif ($element->{LocalName} eq 'variable') {
53 0 delete $self->{_element_with_datatype}->{Attributes}->{'{}datatype'};
54 0 my ($var) = $element->{Attributes}->{'{}name'}->{Value} =~ m/sub:(\w+)/; # TODO: Don't hardcode sub-prefix
55 0 my $binding = $self->{_results}->binding_value_by_name($var);
56 0 my %attrs = %{$self->{_element_with_datatype}->{Attributes}};
57 0 ▶ if ($binding->has_language) {
Branch: 1 branch
BranchTrueFalseTotal
if ($binding->has_language) 0 0 0
58 0 $attrs{'http://www.w3.org/XML/1998/namespace}lang'} =
59 {'LocalName' => 'lang',
60 'Prefix' => 'xml',
61 'Value' => $binding->literal_value_language,
62 'Name' => 'xml:lang',
63 'NamespaceURI' => 'http://www.w3.org/XML/1998/namespace'};
64 }
65 0 ▶ if ($binding->has_datatype) {
Branch: 1 branch
BranchTrueFalseTotal
if ($binding->has_datatype) 0 0 0
66 0 $attrs{'{}datatype'} = { 'LocalName' => 'datatype',
67 'Prefix' => '',
68 'Value' => $binding->literal_datatype,
69 'Name' => 'datatype',
70 'NamespaceURI' => undef
71 }
72 }
73 0 $self->{_element_with_datatype}->{Attributes} = \%attrs;
74 0 $self->SUPER::start_element($self->{_element_with_datatype});
75 0 $self->SUPER::characters({Data => $binding->literal_value});
76 }
77 ▶ } elsif ($self->{_is_in_graph} && $element->{Attributes}->{'{}about'}
Condition0%0/6
$self->{"_is_in_graph"} and $element->{"Attributes"}{"{}about"} and $element->{"Attributes"}{"{}about"}{"Value"} =~ /sub:(\w+)/
!ll&&!rl&&r
000
$self->{"_is_in_graph"} and $element->{"Attributes"}{"{}about"}
!ll&&!rl&&r
000
MC/DC0%0/3
$self->{"_is_in_graph"} and $element->{"Attributes"}{"{}about"} and $element->{"Attributes"}{"{}about"}{"Value"} =~ /sub:(\w+)/
$self->{"_is_in_graph"} $element->{"Attributes"}{"{}about"} $element->{"Attributes"}{"{}about"}{"Value"} =~ /sub:(\w+)/
Truth table0%0/4
A and B and C
A= $self->{"_is_in_graph"}B= $element->{"Attributes"}{"{}about"}C= $element->{"Attributes"}{"{}about"}{"Value"} =~ /sub:(\w+)/
ABCresult
0XX0
10X0
1100
1111
78 && ($element->{Attributes}->{'{}about'}->{Value} =~ m/sub:(\w+)/)) {
79 0 my $uri = $self->{_results}->binding_value_by_name($1)->uri_value;
80 0 $element->{Attributes}->{'{}about'}->{Value} = $uri;
81 0 $self->SUPER::start_element($element);
82 } elsif ($self->{_element_with_datatype}) {
83 0 ▶ unless ($element->{Attributes}->{'{}datatype'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($element->{"Attributes"}{"{}datatype"}) 0 0 0
84 0 $self->{_element_with_datatype} = undef;
85 }
86 } elsif ($self->{_is_in_graph}) {
87 0 $self->SUPER::start_element($element);
88 } else {
89 0 $self->SUPER::start_element($element);
90 }
91 0 return $self;
92 }
93  
94 0 ▶sub end_element {
Subroutine end_element: not called CC=4 SCAR=30.0
Pod documented
95 0 my ($self, $element) = @_;
96 0 ▶ if (defined($element->{NamespaceURI}) && ($element->{NamespaceURI} eq $self->{Doc}->{RATURI})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $element->{'NamespaceURI'} and $element->{'NamespaceURI'} eq $self->{'Doc'}{'RATURI'}) { } 0 0 0
Condition0%0/3
defined $element->{"NamespaceURI"} and $element->{"NamespaceURI"} eq $self->{"Doc"}{"RATURI"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $element->{"NamespaceURI"} and $element->{"NamespaceURI"} eq $self->{"Doc"}{"RATURI"}
defined $element->{"NamespaceURI"} $element->{"NamespaceURI"} eq $self->{"Doc"}{"RATURI"}
Truth table0%0/3
A and B
A= defined $element->{"NamespaceURI"}B= $element->{"NamespaceURI"} eq $self->{"Doc"}{"RATURI"}
ABresult
0X0
100
111
97 0 ▶ if ($element->{LocalName} eq 'graph') {
Branch: 1 branch
BranchTrueFalseTotal
if ($element->{"LocalName"} eq "graph") 0 0 0
98 # Then, reset everything
99 0 $self->{_currentgraph} = undef;
100 0 $self->{_is_in_graph} = 0;
101 0 $self->{_results} = undef;
102 }
103 } else {
104 0 $self->SUPER::end_element($element);
105 }
106 0 return $self;
107 }
108  
109 =head1 SYNOPSIS
110  
111 use XML::LibXML::SAX::Parser;
112 use XML::LibXML::SAX::Builder;
113 my $builder = XML::LibXML::SAX::Builder->new();
114 my $sax = RDF::RDFa::Template::SAXFilter->new(Handler => $builder, Doc => $doc);
115 my $generator = XML::LibXML::SAX::Parser->new(Handler => $sax);
116 $generator->generate($doc->dom);
117  
118 $doc must contain a L<XML::LibXML::Document> and the interesting result
119 from this operation can be found by saying $builder->result;
120  
121 =head1 METHODS
122  
123 =head2 C<new>
124  
125 =head2 C<start_element>
126  
127 =head2 C<end_element>
128  
129 This is a SAX Filter implementation and so implements these methods,
130 but they are of little concern to the user.
131  
132 =head1 AUTHOR
133  
134 Kjetil Kjernsmo, C<< <kjetilk at cpan.org> >>
135  
136 =head1 ACKNOWLEDGEMENTS
137  
138 This would have been hard to do without the help of the dahuts,
139 especially Kip Hampton and Chris Prather.
140  
141  
142 =head1 COPYRIGHT & LICENSE
143  
144 Copyright 2010 Kjetil Kjernsmo.
145  
146 This program is free software; you can redistribute it and/or modify it
147 under the same terms as Perl itself.
148  
149 =cut
150  
151 2 1;
↑ Summary Template/Document.pm »