/usr/share/perl5/RDF/Trine/Exporter/RDFPatch.pm is in librdf-trine-perl 1.019-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 | # RDF::Trine::Exporter::RDFPatch
# -----------------------------------------------------------------------------
=head1 NAME
RDF::Trine::Exporter::RDFPatch - RDF-Patch Export
=head1 VERSION
This document describes RDF::Trine::Exporter::RDFPatch version 1.019
=head1 SYNOPSIS
use RDF::Trine::Exporter::RDFPatch;
my $serializer = RDF::Trine::Exporter::RDFPatch->new();
=head1 DESCRIPTION
The RDF::Trine::Exporter::RDFPatch class provides an API for serializing RDF
graphs to the RDF-Patch syntax.
=head1 METHODS
=over 4
=cut
package RDF::Trine::Exporter::RDFPatch;
use strict;
use warnings;
use URI;
use Carp;
use Data::Dumper;
use Scalar::Util qw(blessed);
use List::Util qw(min);
use RDF::Trine::Node;
use RDF::Trine::Statement;
use RDF::Trine::Error qw(:try);
######################################################################
our ($VERSION);
BEGIN {
$VERSION = '1.019';
}
######################################################################
=item C<< new ( sink => $sink ) >>
Returns a new RDF-Patch exporter object.
=cut
sub new {
my $class = shift;
my $ns = {};
my %args = @_;
if (exists $args{ namespaces }) {
$ns = $args{ namespaces };
}
my $sink = $args{sink};
my %rev;
while (my ($ns, $uri) = each(%{ $ns })) {
if (blessed($uri)) {
$uri = $uri->uri_value;
if (blessed($uri)) {
$uri = $uri->uri_value;
}
}
$rev{ $uri } = $ns;
}
my $self = bless( {
first => 1,
ns => \%rev,
last => [],
sink => $sink,
}, $class );
return $self;
}
sub _sink {
my $self = shift;
return $self->{sink};
}
=item C<< comment ( $c ) >>
Serializes a comment with the given string.
=cut
sub comment {
my $self = shift;
my $c = shift;
$c =~ s/\n/\n# /g;
$self->_sink->emit("# $c\n");
}
=item C<< emit_operation ( $op, @operands ) >>
Serializes an operation identified by the character C<< $op >>, followed by C<< @operands >>
(separated by a single space) and a trailing DOT and newline.
=cut
sub emit_operation {
my $self = shift;
my $op = shift;
my @args = @_;
$self->_sink->emit($op);
foreach my $arg (@args) {
$self->_sink->emit(' ');
$self->_sink->emit($arg);
}
$self->_sink->emit(" .\n");
}
=item C<< add ( $st ) >>
Serializes an add/insert operation for the given statement object.
=cut
sub add {
my $self = shift;
my $st = shift;
if ($self->{first}) {
my $header = $self->_header();
$self->_sink->emit($header);
$self->{first} = 0;
}
my @list = $self->terms_as_string_list( $st->nodes );
$self->emit_operation( 'A', @list );
}
=item C<< delete ( $st ) >>
Serializes a delete operation for the given statement object.
=cut
sub delete {
my $self = shift;
my $st = shift;
if ($self->{first}) {
my $header = $self->_header();
$self->_sink->emit($header);
$self->{first} = 0;
}
my @list = $self->terms_as_string_list( $st->nodes );
$self->emit_operation( 'D', @list );
}
sub _header {
my $self = shift;
my %ns = reverse(%{ $self->{ns} });
my @nskeys = sort keys %ns;
my $header = '';
if (@nskeys) {
foreach my $ns (sort @nskeys) {
my $uri = $ns{ $ns };
$header .= "\@prefix $ns: <$uri> .\n";
}
$header .= "\n";
}
return $header;
}
=item C<< statement_as_string ( $st ) >>
Returns a string with the supplied RDF::Trine::Statement object serialized as an RDF-Patch string.
=cut
sub statement_as_string {
my $self = shift;
my $st = shift;
my @nodes = $st->nodes;
my @str_nodes = $self->terms_as_string_list( @nodes );
return join(' ', @str_nodes);
}
=item C<< terms_as_string_list ( @terms ) >>
Returns a list with each supplied term serialized as RDF-Patch strings.
=cut
sub terms_as_string_list {
my $self = shift;
my @nodes = @_;
my @str_nodes = map { $self->node_as_concise_string($_) } @nodes;
if (1) {
foreach my $i (0 .. min(scalar(@nodes), scalar(@{$self->{'last'}}))) {
if (defined($self->{'last'}[$i]) and $nodes[$i]->equal( $self->{'last'}[$i])) {
$str_nodes[$i] = 'R';
}
}
@{ $self->{'last'} } = @nodes;
}
return @str_nodes;
}
=item C<< node_as_concise_string >>
Returns a string representation using RDF-Patch syntax shortcuts (e.g. PrefixNames).
=cut
sub node_as_concise_string {
my $self = shift;
my $obj = shift;
if ($obj->isa('RDF::Trine::Node::Resource')) {
my $value;
try {
my ($ns,$local) = $obj->qname;
if (blessed($self) and exists $self->{ns}{$ns}) {
$value = join(':', $self->{ns}{$ns}, $local);
$self->{used_ns}{ $self->{ns}{$ns} }++;
}
} catch RDF::Trine::Error with {} otherwise {};
if ($value) {
return $value;
}
}
return $obj->as_ntriples;
}
1;
__END__
=back
=head1 NOTES
As described in L<RDF::Trine::Node::Resource/as_ntriples>, serialization will
decode any L<punycode|http://www.ietf.org/rfc/rfc3492.txt> that is included in the IRI,
and serialize it using unicode codepoint escapes.
=head1 BUGS
Please report any bugs or feature requests to through the GitHub web interface
at L<https://github.com/kasei/perlrdf/issues>.
=head1 SEE ALSO
L<http://afs.github.io/rdf-patch/>
=head1 AUTHOR
Gregory Todd Williams C<< <gwilliams@cpan.org> >>
=head1 COPYRIGHT
Copyright (c) 2006-2012 Gregory Todd Williams. This
program is free software; you can redistribute it and/or modify it under
the same terms as Perl itself.
=cut
|