line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# $File: //member/autrijus/RDF-YAML/lib/RDF/Simple/Serialiser/YAML.pm $ $Author: autrijus $ |
2
|
|
|
|
|
|
|
# $Revision: #1 $ $Change: 8523 $ $DateTime: 2003/10/22 04:14:34 $ |
3
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
package RDF::Simple::Serialiser::YAML; |
5
|
|
|
|
|
|
|
$RDF::Simple::Serialiser::YAML::VERSION = '0.01'; |
6
|
|
|
|
|
|
|
|
7
|
1
|
|
|
1
|
|
6
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
36
|
|
8
|
1
|
|
|
1
|
|
725
|
use RDF::Simple::Serialiser; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
use base 'RDF::Simple::Serialiser'; |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
=head1 NAME |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
RDF::Simple::Serialiser::YAML - Simple RDF/YAML serialiser |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
=head1 SYNOPSIS |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
my $ser = RDF::Simple::Serialiser::YAML->new; |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
my @triples = ( |
20
|
|
|
|
|
|
|
['http://example.com/url#', 'dc:creator', 'zool@example.com'], |
21
|
|
|
|
|
|
|
['http://example.com/url#', 'foaf:Topic', '_id:1234'], |
22
|
|
|
|
|
|
|
['_id:1234','http://www.w3.org/2003/01/geo/wgs84_pos#lat','51.334422'] |
23
|
|
|
|
|
|
|
); |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
my $yaml_string = $ser->serialise(@triples); |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
=head1 DESCRIPTION |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
This module is a subclass of L to |
30
|
|
|
|
|
|
|
produce serialised RDF/YAML documents from an array of triples. |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
Please see L for a list of supported |
33
|
|
|
|
|
|
|
methods. |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
=cut |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
# Escapes for unprintable characters |
38
|
|
|
|
|
|
|
my @escapes = qw(\z \x01 \x02 \x03 \x04 \x05 \x06 \a |
39
|
|
|
|
|
|
|
\x08 \t \n \v \f \r \x0e \x0f |
40
|
|
|
|
|
|
|
\x10 \x11 \x12 \x13 \x14 \x15 \x16 \x17 |
41
|
|
|
|
|
|
|
\x18 \x19 \x1a \e \x1c \x1d \x1e \x1f |
42
|
|
|
|
|
|
|
); |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
sub render { |
45
|
|
|
|
|
|
|
my ($self, $template, $data, $out_object) = @_; |
46
|
|
|
|
|
|
|
$data->{emit} = sub { $self->emit($_[0]) }; |
47
|
|
|
|
|
|
|
return $self->SUPER::render($template, $data, $out_object); |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
sub emit { |
51
|
|
|
|
|
|
|
my ($self, $string) = @_; |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
use bytes; |
54
|
|
|
|
|
|
|
if ($string =~ /^\W|[\x00-\x08\x0b-\x0d\x0e-\x1f:#]|^\s|\s\z|\A\z/) { |
55
|
|
|
|
|
|
|
$string =~ s/\\/\\\\/g; |
56
|
|
|
|
|
|
|
$string =~ s/"/\\"/g; |
57
|
|
|
|
|
|
|
$string =~ s/([\x00-\x1f])/$escapes[ord($1)]/ge; |
58
|
|
|
|
|
|
|
return qq{"$string"}; |
59
|
|
|
|
|
|
|
} |
60
|
|
|
|
|
|
|
else { |
61
|
|
|
|
|
|
|
return $string; |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
sub get_template { |
66
|
|
|
|
|
|
|
my $template = <<'END_TEMPLATE'; |
67
|
|
|
|
|
|
|
[% USE url('') %]--- #YAML:1.0 |
68
|
|
|
|
|
|
|
'':[% FOREACH key = ns.keys.sort %] |
69
|
|
|
|
|
|
|
[% IF key.length %][% key %][% ELSE %]''[% END %]: [% url(ns.$key) %][% END %][% FOREACH object = objects %] |
70
|
|
|
|
|
|
|
[% IF object.Uri %][% object.Uri %][% ELSE %][% object.NodeId %][% END %]:[% IF object.Class != 'rdf:Description' %] |
71
|
|
|
|
|
|
|
rdf:type: [% object.Class %][% END %][% FOREACH lit = object.literal.keys.sort %][% FOREACH prop = object.literal.$lit %] |
72
|
|
|
|
|
|
|
[% lit %]: [% emit(prop) %][% END %][% END %][% FOREACH res = object.resource.keys.sort %][% FOREACH prop = object.resource.$res %] |
73
|
|
|
|
|
|
|
[% res %]: [% url(prop) %][% END %][% END %][% FOREACH node = object.nodeid.keys.sort %][% FOREACH prop = object.nodeid.$node %] |
74
|
|
|
|
|
|
|
[% node %]: [% prop %][% END %][% END %][% END %] |
75
|
|
|
|
|
|
|
END_TEMPLATE |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
return \$template; |
78
|
|
|
|
|
|
|
} |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
1; |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
=head1 SEE ALSO |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
L, L |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
=head1 AUTHORS |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
Autrijus Tang Eautrijus@autrijus.orgE |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
=head1 COPYRIGHT |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
Copyright 2003 by Autrijus Tang Eautrijus@autrijus.orgE. |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or |
95
|
|
|
|
|
|
|
modify it under the same terms as Perl itself. |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
See L |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
=cut |