| 1 | # RDF::Trine::Serializer::RDFXML | ||||||||||||||||||||||||||||
| 2 | # ----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | =head1 NAME | ||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | RDF::Trine::Serializer::RDFXML - RDF/XML Serializer | ||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | =head1 VERSION | ||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | This document describes RDF::Trine::Serializer::RDFXML version 1.019 | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | use RDF::Trine::Serializer::RDFXML; | ||||||||||||||||||||||||||||
| 15 | my $serializer = RDF::Trine::Serializer::RDFXML->new( namespaces => { ex => 'http://example/' } ); | ||||||||||||||||||||||||||||
| 16 | print $serializer->serialize_model_to_string($model); | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | The RDF::Trine::Serializer::Turtle class provides an API for serializing RDF | ||||||||||||||||||||||||||||
| 21 | graphs to the RDF/XML syntax. | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | =head1 METHODS | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | Beyond the methods documented below, this class inherits methods from the | ||||||||||||||||||||||||||||
| 26 | L<RDF::Trine::Serializer> class. | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | =over 4 | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | =cut | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | package RDF::Trine::Serializer::RDFXML; | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 68 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 35 | 68 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 36 | 68 | ▶ | use base qw(RDF::Trine::Serializer); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 68 | ▶ | use URI; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 39 | 68 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 40 | 68 | ▶ | use Data::Dumper; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 41 | 68 | ▶ | use Scalar::Util qw(blessed); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 68 | ▶ | use RDF::Trine qw(variable); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 44 | 68 | ▶ | use RDF::Trine::Statement; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 45 | 68 | ▶ | use RDF::Trine::Error qw(:try); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | ###################################################################### | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 68 | our ($VERSION); | |||||||||||||||||||||||||||
| 50 | 68 | ▶ | BEGIN { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 51 | 68 | $VERSION = '1.019'; | |||||||||||||||||||||||||||
| 52 | 68 | $RDF::Trine::Serializer::serializer_names{ 'rdfxml' } = __PACKAGE__; | |||||||||||||||||||||||||||
| 53 | 68 | $RDF::Trine::Serializer::format_uris{ 'http://www.w3.org/ns/formats/RDF_XML' } = __PACKAGE__; | |||||||||||||||||||||||||||
| 54 | 68 | foreach my $type (qw(application/rdf+xml)) { | |||||||||||||||||||||||||||
| 55 | 68 | $RDF::Trine::Serializer::media_types{ $type } = __PACKAGE__; | |||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | ###################################################################### | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | =item C<< new ( namespaces => \%namespaces, base_uri => $baseuri ) >> | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | Returns a new RDF/XML serializer object. | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | =cut | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 22 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=11 SCAR=24.1
Pod
documented
| |||||||||||||||||||||||||||||
| 68 | 22 | my $class = shift; | |||||||||||||||||||||||||||
| 69 | 22 | my %args = @_; | |||||||||||||||||||||||||||
| 70 | 22 | my $self = bless( { namespaces => { 'http://www.w3.org/1999/02/22-rdf-syntax-ns#' => 'rdf' } }, $class); | |||||||||||||||||||||||||||
| 71 | 22 | ▶ | if (my $ns = $args{namespaces}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | 4 | my %nsmap; | |||||||||||||||||||||||||||
| 73 | 4 | ▶ | if (blessed($ns) and $ns->isa('RDF::Trine::NamespaceMap')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
builtin::blessed($ns) and $ns->isa("RDF::Trine::NamespaceMap")
MC/DC50%1/2
builtin::blessed($ns) and $ns->isa("RDF::Trine::NamespaceMap")
builtin::blessed($ns)
$ns->isa("RDF::Trine::NamespaceMap")
Truth table66%2/3
A and B
A= builtin::blessed($ns)B= $ns->isa("RDF::Trine::NamespaceMap")
| |||||||||||||||||||||||||||||
| 74 | 1 | for my $prefix ($ns->list_prefixes) { | |||||||||||||||||||||||||||
| 75 | # way convoluted | ||||||||||||||||||||||||||||
| 76 | 2 | my $nsuri = $ns->namespace_uri($prefix)->uri->value; | |||||||||||||||||||||||||||
| 77 | 2 | $nsmap{$nsuri} = $prefix; | |||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | else { | ||||||||||||||||||||||||||||
| 81 | 3 | my %ns = %{ $ns }; | |||||||||||||||||||||||||||
| 82 | 3 | ▶ | while (my ($ns, $uri) = each(%ns)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | 7 | for (1..2) { | |||||||||||||||||||||||||||
| 84 | 14 | ▶ | $uri = $uri->uri_value if (blessed($uri)); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | 7 | $nsmap{ $uri } = $ns; | |||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | 4 | @{ $self->{namespaces} }{ keys %nsmap } = values %nsmap; | |||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | 22 | ▶ | if ($args{base}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 92 | 0 | $self->{base_uri} = $args{base}; | |||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | 22 | ▶ | if ($args{base_uri}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | 1 | $self->{base_uri} = $args{base_uri}; | |||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | 22 | ▶ | if ($args{scoped_namespaces}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 98 | 1 | $self->{scoped_namespaces} = $args{scoped_namespaces}; | |||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | 22 | return $self; | |||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | =item C<< serialize_model_to_file ( $fh, $model ) >> | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | Serializes the C<$model> to RDF/XML, printing the results to the supplied | ||||||||||||||||||||||||||||
| 106 | filehandle C<<$fh>>. | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | =cut | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 13 | ▶ | sub serialize_model_to_file { | ||||||||||||||||||||||||||
|
Subroutine
serialize_model_to_file: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 111 | 13 | my $self = shift; | |||||||||||||||||||||||||||
| 112 | 13 | my $fh = shift; | |||||||||||||||||||||||||||
| 113 | 13 | my $model = shift; | |||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 13 | my $st = RDF::Trine::Statement->new( map { variable($_) } qw(s p o) ); | |||||||||||||||||||||||||||
| 116 | 13 | my $pat = RDF::Trine::Pattern->new( $st ); | |||||||||||||||||||||||||||
| 117 | 13 | my $stream = $model->get_pattern( $pat, undef, orderby => [ qw(s ASC p ASC o ASC) ] ); | |||||||||||||||||||||||||||
| 118 | 13 | my $iter = $stream->as_statements( qw(s p o) ); | |||||||||||||||||||||||||||
| 119 | # my $iter = $model->as_stream; | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 13 | $self->serialize_iterator_to_file( $fh, $iter ); | |||||||||||||||||||||||||||
| 122 | 11 | return 1; | |||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | =item C<< serialize_model_to_string ( $model ) >> | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | Serializes the C<$model> to RDF/XML, returning the result as a string. | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | =cut | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | =item C<< serialize_iterator_to_file ( $file, $iter ) >> | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | Serializes the iterator to RDF/XML, printing the results to the supplied | ||||||||||||||||||||||||||||
| 134 | filehandle C<<$fh>>. | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | =cut | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | 20 | ▶ | sub serialize_iterator_to_file { | ||||||||||||||||||||||||||
|
Subroutine
serialize_iterator_to_file: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 139 | 20 | my $self = shift; | |||||||||||||||||||||||||||
| 140 | 20 | my $fh = shift; | |||||||||||||||||||||||||||
| 141 | 20 | my $iter = shift; | |||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | 20 | my $ns = $self->_top_xmlns(); | |||||||||||||||||||||||||||
| 144 | 20 | my $base_uri = ''; | |||||||||||||||||||||||||||
| 145 | 20 | ▶ | if ($self->{base_uri}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 146 | 1 | $base_uri = "xml:base=\"$self->{base_uri}\" "; | |||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||
| 148 | 20 | print {$fh} qq[<?xml version="1.0" encoding="utf-8"?>\n<rdf:RDF ${base_uri}xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"$ns>\n]; | |||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | 20 | my $st = $iter->next; | |||||||||||||||||||||||||||
| 151 | 20 | my @statements; | |||||||||||||||||||||||||||
| 152 | 20 | ▶ | push(@statements, $st) if blessed($st); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 153 | 20 | ▶ | while (@statements) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 154 | 24 | my $st = shift(@statements); | |||||||||||||||||||||||||||
| 155 | 24 | my @samesubj; | |||||||||||||||||||||||||||
| 156 | 24 | push(@samesubj, $st); | |||||||||||||||||||||||||||
| 157 | 24 | my $subj = $st->subject; | |||||||||||||||||||||||||||
| 158 | 24 | ▶ | while (my $row = $iter->next) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 159 | 28 | ▶ | if ($row->subject->equal( $subj )) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 160 | 22 | push(@samesubj, $row); | |||||||||||||||||||||||||||
| 161 | } else { | ||||||||||||||||||||||||||||
| 162 | 6 | push(@statements, $row); | |||||||||||||||||||||||||||
| 163 | 6 | last; | |||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | 24 | print {$fh} $self->_statements_same_subject_as_string( @samesubj ); | |||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | 18 | print {$fh} qq[</rdf:RDF>\n]; | |||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | 24 | ▶ | sub _statements_same_subject_as_string { | ||||||||||||||||||||||||||
|
Subroutine
_statements_same_subject_as_string: called CC=16 SCAR=27.7
| |||||||||||||||||||||||||||||
| 174 | 24 | my $self = shift; | |||||||||||||||||||||||||||
| 175 | 24 | my @statements = @_; | |||||||||||||||||||||||||||
| 176 | 24 | my $s = $statements[0]->subject; | |||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | 24 | my $id; | |||||||||||||||||||||||||||
| 179 | 24 | ▶ | if ($s->isa('RDF::Trine::Node::Blank')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 180 | 10 | my $b = $s->blank_identifier; | |||||||||||||||||||||||||||
| 181 | 10 | $id = qq[rdf:nodeID="$b"]; | |||||||||||||||||||||||||||
| 182 | } else { | ||||||||||||||||||||||||||||
| 183 | 14 | my $i = $s->uri_value; | |||||||||||||||||||||||||||
| 184 | 14 | for ($i) { | |||||||||||||||||||||||||||
| 185 | 14 | s/&/&/g; | |||||||||||||||||||||||||||
| 186 | 14 | s/</</g; | |||||||||||||||||||||||||||
| 187 | 14 | s/"/"/g; | |||||||||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||||||||
| 189 | 14 | $id = qq[rdf:about="$i"]; | |||||||||||||||||||||||||||
| 190 | } | ||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | 24 | my $counter = 1; | |||||||||||||||||||||||||||
| 193 | 24 | my %namespaces = %{ $self->{namespaces} }; | |||||||||||||||||||||||||||
| 194 | 24 | my $string = ''; | |||||||||||||||||||||||||||
| 195 | 24 | foreach my $st (@statements) { | |||||||||||||||||||||||||||
| 196 | 46 | my (undef, $p, $o) = $st->nodes; | |||||||||||||||||||||||||||
| 197 | 46 | my %used_namespaces; | |||||||||||||||||||||||||||
| 198 | 46 | my ($ns, $ln); | |||||||||||||||||||||||||||
| 199 | 46 | ▶ | try { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 200 | 46 | ($ns,$ln) = $p->qname; | |||||||||||||||||||||||||||
| 201 | 2 | ▶ | } catch RDF::Trine::Error with { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 202 | 2 | my $uri = $p->uri_value; | |||||||||||||||||||||||||||
| 203 | 2 | throw RDF::Trine::Error::SerializationError -text => "Can't turn predicate $uri into a QName."; | |||||||||||||||||||||||||||
| 204 | 46 | }; | |||||||||||||||||||||||||||
| 205 | 44 | $used_namespaces{ $ns }++; | |||||||||||||||||||||||||||
| 206 | 44 | ▶ | unless (exists $namespaces{ $ns }) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 207 | 17 | $namespaces{ $ns } = 'ns' . $counter++; | |||||||||||||||||||||||||||
| 208 | } | ||||||||||||||||||||||||||||
| 209 | 44 | my $prefix = $namespaces{ $ns }; | |||||||||||||||||||||||||||
| 210 | 44 | my $nsdecl = ''; | |||||||||||||||||||||||||||
| 211 | 44 | ▶ | if ($self->{scoped_namespaces}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 212 | 3 | $nsdecl = qq[ xmlns:$prefix="$ns"]; | |||||||||||||||||||||||||||
| 213 | } | ||||||||||||||||||||||||||||
| 214 | 44 | ▶ | if ($o->isa('RDF::Trine::Node::Literal')) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 215 | 24 | my $lv = $o->literal_value; | |||||||||||||||||||||||||||
| 216 | 24 | for ($lv) { | |||||||||||||||||||||||||||
| 217 | 24 | s/&/&/g; | |||||||||||||||||||||||||||
| 218 | 24 | s/</</g; | |||||||||||||||||||||||||||
| 219 | 24 | s/"/"/g; | |||||||||||||||||||||||||||
| 220 | } | ||||||||||||||||||||||||||||
| 221 | 24 | my $lang = $o->literal_value_language; | |||||||||||||||||||||||||||
| 222 | 24 | my $dt = $o->literal_datatype; | |||||||||||||||||||||||||||
| 223 | 24 | my $tag = join(':', $prefix, $ln); | |||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||
| 225 | 24 | ▶ | if ($lang) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 226 | 9 | $string .= qq[\t<${tag}${nsdecl} xml:lang="${lang}">${lv}</${tag}>\n]; | |||||||||||||||||||||||||||
| 227 | } elsif ($dt) { | ||||||||||||||||||||||||||||
| 228 | 1 | $string .= qq[\t<${tag}${nsdecl} rdf:datatype="${dt}">${lv}</${tag}>\n]; | |||||||||||||||||||||||||||
| 229 | } else { | ||||||||||||||||||||||||||||
| 230 | 14 | $string .= qq[\t<${tag}${nsdecl}>${lv}</${tag}>\n]; | |||||||||||||||||||||||||||
| 231 | } | ||||||||||||||||||||||||||||
| 232 | } elsif ($o->isa('RDF::Trine::Node::Blank')) { | ||||||||||||||||||||||||||||
| 233 | 6 | my $b = $o->blank_identifier; | |||||||||||||||||||||||||||
| 234 | 6 | for ($b) { | |||||||||||||||||||||||||||
| 235 | 6 | s/&/&/g; | |||||||||||||||||||||||||||
| 236 | 6 | s/</</g; | |||||||||||||||||||||||||||
| 237 | 6 | s/"/"/g; | |||||||||||||||||||||||||||
| 238 | } | ||||||||||||||||||||||||||||
| 239 | 6 | $string .= qq[\t<${prefix}:$ln${nsdecl} rdf:nodeID="$b"/>\n]; | |||||||||||||||||||||||||||
| 240 | } else { | ||||||||||||||||||||||||||||
| 241 | 14 | my $u = $o->uri_value; | |||||||||||||||||||||||||||
| 242 | 14 | for ($u) { | |||||||||||||||||||||||||||
| 243 | 14 | s/&/&/g; | |||||||||||||||||||||||||||
| 244 | 14 | s/</</g; | |||||||||||||||||||||||||||
| 245 | 14 | s/"/"/g; | |||||||||||||||||||||||||||
| 246 | } | ||||||||||||||||||||||||||||
| 247 | 14 | $string .= qq[\t<${prefix}:$ln${nsdecl} rdf:resource="$u"/>\n]; | |||||||||||||||||||||||||||
| 248 | } | ||||||||||||||||||||||||||||
| 249 | } | ||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||
| 251 | 22 | $string .= qq[</rdf:Description>\n]; | |||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||
| 253 | # rdf namespace is already defined in the <rdf:RDF> tag, so ignore it here | ||||||||||||||||||||||||||||
| 254 | 22 | my %seen = %{ $self->{namespaces} }; | |||||||||||||||||||||||||||
| 255 | 22 | my @ns; | |||||||||||||||||||||||||||
| 256 | 22 | foreach my $uri (sort { $namespaces{$a} cmp $namespaces{$b} } grep { not($seen{$_}) } (keys %namespaces)) { | |||||||||||||||||||||||||||
| 257 | 18 | my $ns = $namespaces{$uri}; | |||||||||||||||||||||||||||
| 258 | 18 | ▶ | my $str = ($ns eq '') ? qq[xmlns="$uri"] : qq[xmlns:${ns}="$uri"]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 259 | 18 | push(@ns, $str); | |||||||||||||||||||||||||||
| 260 | } | ||||||||||||||||||||||||||||
| 261 | 22 | my $ns = join(' ', @ns); | |||||||||||||||||||||||||||
| 262 | 22 | ▶ | if ($ns) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 263 | 17 | return qq[<rdf:Description ${ns} $id>\n] . $string; | |||||||||||||||||||||||||||
| 264 | } else { | ||||||||||||||||||||||||||||
| 265 | 5 | return qq[<rdf:Description $id>\n] . $string; | |||||||||||||||||||||||||||
| 266 | } | ||||||||||||||||||||||||||||
| 267 | } | ||||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||
| 269 | =item C<< serialize_iterator_to_string ( $iter ) >> | ||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||
| 271 | Serializes the iterator to RDF/XML, returning the result as a string. | ||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||
| 273 | =cut | ||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||
| 275 | 0 | ▶ | sub _serialize_bounded_description { | ||||||||||||||||||||||||||
|
Subroutine
_serialize_bounded_description: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 276 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 277 | 0 | my $model = shift; | |||||||||||||||||||||||||||
| 278 | 0 | my $node = shift; | |||||||||||||||||||||||||||
| 279 | 0 | my $seen = {}; | |||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||
| 281 | 0 | my $ns = $self->_top_xmlns(); | |||||||||||||||||||||||||||
| 282 | 0 | my $base_uri = ''; | |||||||||||||||||||||||||||
| 283 | 0 | ▶ | if ($self->{base_uri}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 284 | 0 | $base_uri = "xml:base=\"$self->{base_uri}\" "; | |||||||||||||||||||||||||||
| 285 | } | ||||||||||||||||||||||||||||
| 286 | 0 | my $string = qq[<?xml version="1.0" encoding="utf-8"?>\n<rdf:RDF $base_uri$ns>\n]; | |||||||||||||||||||||||||||
| 287 | 0 | $string .= $self->__serialize_bounded_description( $model, $node, $seen ); | |||||||||||||||||||||||||||
| 288 | 0 | $string .= qq[</rdf:RDF>\n]; | |||||||||||||||||||||||||||
| 289 | 0 | return $string; | |||||||||||||||||||||||||||
| 290 | } | ||||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||
| 292 | 0 | ▶ | sub __serialize_bounded_description { | ||||||||||||||||||||||||||
|
Subroutine
__serialize_bounded_description: not called CC=6 SCAR=37.4
| |||||||||||||||||||||||||||||
| 293 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 294 | 0 | my $model = shift; | |||||||||||||||||||||||||||
| 295 | 0 | my $node = shift; | |||||||||||||||||||||||||||
| 296 | 0 | ▶ | my $seen = shift || {}; | ||||||||||||||||||||||||||
|
Condition0%0/2
shift() || {}
MC/DC0%0/1
shift() || {}
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 297 | 0 | ▶ | return '' if ($seen->{ $node->sse }++); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||
| 299 | 0 | my $string = ''; | |||||||||||||||||||||||||||
| 300 | 0 | my $st = RDF::Trine::Statement->new( $node, map { RDF::Trine::Node::Variable->new($_) } qw(p o) ); | |||||||||||||||||||||||||||
| 301 | 0 | my $pat = RDF::Trine::Pattern->new( $st ); | |||||||||||||||||||||||||||
| 302 | 0 | my $iter = $model->get_pattern( $pat, undef, orderby => [ qw(p ASC o ASC) ] ); | |||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||
| 304 | 0 | my @bindings = $iter->get_all; | |||||||||||||||||||||||||||
| 305 | 0 | ▶ | if (@bindings) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 306 | 0 | my @samesubj = map { RDF::Trine::Statement->new( $node, $_->{p}, $_->{o} ) } @bindings; | |||||||||||||||||||||||||||
| 307 | 0 | ▶ | my @blanks = grep { blessed($_) and $_->isa('RDF::Trine::Node::Blank') } map { $_->{o} } @bindings; | ||||||||||||||||||||||||||
|
Condition0%0/3
builtin::blessed($_) && $_->isa('RDF::Trine::Node::Blank')
MC/DC0%0/2
builtin::blessed($_) && $_->isa('RDF::Trine::Node::Blank')
builtin::blessed($_)
$_->isa('RDF::Trine::Node::Blank')
Truth table0%0/3
A && B
A= builtin::blessed($_)B= $_->isa('RDF::Trine::Node::Blank')
| |||||||||||||||||||||||||||||
| 308 | 0 | $string .= $self->_statements_same_subject_as_string( @samesubj ); | |||||||||||||||||||||||||||
| 309 | 0 | foreach my $object (@blanks) { | |||||||||||||||||||||||||||
| 310 | 0 | $string .= $self->__serialize_bounded_description( $model, $object, $seen ); | |||||||||||||||||||||||||||
| 311 | } | ||||||||||||||||||||||||||||
| 312 | } | ||||||||||||||||||||||||||||
| 313 | 0 | return $string; | |||||||||||||||||||||||||||
| 314 | } | ||||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||
| 316 | 20 | ▶ | sub _top_xmlns { | ||||||||||||||||||||||||||
|
Subroutine
_top_xmlns: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||
| 317 | 20 | my $self = shift; | |||||||||||||||||||||||||||
| 318 | 20 | my $namespaces = $self->{namespaces}; | |||||||||||||||||||||||||||
| 319 | 20 | my @keys = sort { $namespaces->{$a} cmp $namespaces->{$b} } keys %$namespaces; | |||||||||||||||||||||||||||
| 320 | 20 | ▶ | return '' if ($self->{scoped_namespaces}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 321 | |||||||||||||||||||||||||||||
| 322 | 19 | my @ns; | |||||||||||||||||||||||||||
| 323 | 19 | foreach my $v (@keys) { | |||||||||||||||||||||||||||
| 324 | 25 | ▶ | next if ($v eq 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 325 | 6 | my $k = $namespaces->{$v}; | |||||||||||||||||||||||||||
| 326 | 6 | ▶ | if (blessed($v)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 327 | 0 | $v = $v->uri_value; | |||||||||||||||||||||||||||
| 328 | } | ||||||||||||||||||||||||||||
| 329 | 6 | ▶ | my $str = ($k eq '') ? qq[xmlns="$v"] : qq[xmlns:$k="$v"]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 330 | 6 | push(@ns, $str); | |||||||||||||||||||||||||||
| 331 | } | ||||||||||||||||||||||||||||
| 332 | 19 | my $ns = join(' ', @ns); | |||||||||||||||||||||||||||
| 333 | 19 | ▶ | if (length($ns)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 334 | 3 | $ns = " $ns"; | |||||||||||||||||||||||||||
| 335 | } | ||||||||||||||||||||||||||||
| 336 | 19 | return $ns; | |||||||||||||||||||||||||||
| 337 | } | ||||||||||||||||||||||||||||
| 338 | |||||||||||||||||||||||||||||
| 339 | 68 | 1; | |||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||
| 341 | __END__ | ||||||||||||||||||||||||||||