| 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"} ||= "./"
MC/DC0%0/1
$options{"BaseURI"} ||= "./"
$options{"BaseURI"}
Truth table0%0/2
A
A= $options{"BaseURI"}
| |||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||
| 33 | 0 | delete $element->{Attributes}->{'{' . $self->{Doc}->{RATURI} . '}doctype-system'}; | |||||||||||||||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||||||||||||||
| 35 | 0 | ▶ | if ($element->{Attributes}->{'{' . $self->{Doc}->{RATURI} . '}doctype-public'}) { | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 36 | 0 | delete $element->{Attributes}->{'{' . $self->{Doc}->{RATURI} . '}doctype-public'}; | |||||||||||||||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||||||||||||||
| 38 | 0 | ▶ | if ($element->{Attributes}->{'{}datatype'}) { | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 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
Condition0%0/3
defined $element->{"NamespaceURI"} and $element->{"NamespaceURI"} eq $self->{"Doc"}{"RATURI"}
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"}
| |||||||||||||||||||||||||||||||||||||||||
| 43 | 0 | ▶ | if ($element->{LocalName} eq 'graph') { | ||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||
| 48 | 0 | $self->{_is_in_graph} = 1; | |||||||||||||||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||||||||||||||
| 50 | 0 | ▶ | croak "couldn't find current graph name" unless $self->{_currentgraph}; | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||
| 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+)/
$self->{"_is_in_graph"} and $element->{"Attributes"}{"{}about"}
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+)/
| |||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||
| 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
Condition0%0/3
defined $element->{"NamespaceURI"} and $element->{"NamespaceURI"} eq $self->{"Doc"}{"RATURI"}
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"}
| |||||||||||||||||||||||||||||||||||||||||
| 97 | 0 | ▶ | if ($element->{LocalName} eq 'graph') { | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 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; | |||||||||||||||||||||||||||||||||||||||