FCP/Metadata.pm

filter: Statementstmt 24.5% 13 / 53 Branchbran 0.0% 0 / 36 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 33.3% 4 / 12 Podpod 71.4% 5 / 7 totaltotal 20.3% 22 / 108 CC 19 SCAR 39.7
CC
19
Cov
15%
parse_metadata
132.0
new
12.0
add_redirect
12.0
CRAP 53.0
↑ Summary FCP.pm »
1 =head1 NAME
2  
3 Net::FCP::Metadata - metadata utility class.
4  
5 =head1 SYNOPSIS
6  
7 use Net::FCP::Metadata;
8  
9 =head1 DESCRIPTION
10  
11 =over 4
12  
13 =cut
14  
15 package Net::FCP::Metadata;
16  
17 1 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 1 ▶use Net::FCP::Util qw(tolc touc xeh);
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 1 ▶no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 use overload
24 0 ▶ '""' => sub { $_[0]->as_string };
Subroutine BEGIN: called CC=1 SCAR=0.2
Subroutine __ANON__: not called CC=1 SCAR=0.2
25  
26 =item $metadata = new Net::FCP::Metadata [$string_or_object]
27  
28 Creates a new metadata Object from the given string or reference. The
29 object is overloaded and will stringify into the corresponding string form
30 (which might be slightly different than the string it was created from).
31  
32 If no arguments is given, creates a new metadata object with just a
33 C<version> part.
34  
35 The object is implemented as a hash reference. See C<parse_metadata>,
36 below, for info on it's structure.
37  
38 =cut
39  
40 0 ▶sub new {
Subroutine new: not called CC=3 SCAR=24.8
Pod documented
41 0 my ($class, $data) = @_;
42  
43 0 ▶ $data = ref $data ? %$data
Branch: 2 branches
BranchTrueFalseTotal
ref $data ? : 0 0 0
$data ? : 0 0 0
44 : $data ? parse_metadata ($data)
45 : { version => { revision => 1 } };
46  
47 0 bless $data, $class;
48 }
49  
50 =item $metadata->as_string
51  
52 Returns the string form of the metadata data.
53  
54 =cut
55  
56 0 ▶sub as_string {
Subroutine as_string: not called CC=1 SCAR=6.9
Pod documented
57 0 build_metadata ($_[0]);
58 }
59  
60 =item $metadata->add_redirect ($name, $target[ info1 => arg1...])
61  
62 Add a simple redirection to the C<document> section to the given
63 target. All extra arguments will be added to the C<info> subsection and
64 often contains C<description> and C<format> fields.
65  
66 =cut
67  
68 0 ▶sub add_redirect {
Subroutine add_redirect: not called CC=3 SCAR=24.8
Pod documented
69 0 my ($self, $name, $target, %info) = @_;
70  
71 0 ▶ push @{ $self->{document} }, {
Branch: 2 branches
BranchTrueFalseTotal
$name ? : 0 0 0
%info ? : 0 0 0
72 redirect => { target => $target },
73 $name ? (name => $name) : (),
74 %info ? (info => \%info) : (),
75 };
76 }
77  
78 =item $meta = Net::FCP::Metadata::parse_metadata $string
79  
80 Internal utility function, do not use directly!
81  
82 Parse a metadata string and return it.
83  
84 The metadata will be a hashref with key C<version> (containing the
85 mandatory version header entries) and key C<raw> containing the original
86 metadata string.
87  
88 All other headers are represented by arrayrefs (they can be repeated).
89  
90 Since this description is confusing, here is a rather verbose example of a
91 parsed manifest:
92  
93 (
94 raw => "Version...",
95 version => { revision => 1 },
96 document => [
97 {
98 info => { format" => "image/jpeg" },
99 name => "background.jpg",
100 redirect => { target => "freenet:CHK\@ZcagI,ra726bSw" },
101 },
102 {
103 info => { format" => "text/html" },
104 name => ".next",
105 redirect => { target => "freenet:SSK\@ilUPAgM/TFEE/3" },
106 },
107 {
108 info => { format" => "text/html" },
109 redirect => { target => "freenet:CHK\@8M8Po8ucwI,8xA" },
110 }
111 ]
112 )
113  
114 =cut
115  
116 0 ▶sub parse_metadata {
Subroutine parse_metadata: not called CC=11 SCAR=48.8
Pod documented
117 0 my $data = shift;
118 0 my $meta = { raw => $data };
119  
120 0 ▶ if ($data =~ /^Version\015?\012/gc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data =~ /^Version\015?\012/cg) 0 0 0
121 0 my $hdr = $meta->{version} = {};
122  
123 0 for (;;) {
124 0 ▶ while ($data =~ /\G([^=\015\012]+)=([^\015\012]*)\015?\012/gc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data =~ /\G([^=\015\012]+)=([^\015\012]*)\015?\012/cg) 0 0 0
125 0 my ($k, $v) = ($1, $2);
126 0 my @p = split /\./, tolc $k, 3;
127  
128 0 ▶ $hdr->{$p[0]} = $v if @p == 1; # lamest code I ever wrote
Branch: 1 branch
BranchTrueFalseTotal
if @p == 1 0 0 0
129 0 ▶ $hdr->{$p[0]}{$p[1]} = $v if @p == 2;
Branch: 1 branch
BranchTrueFalseTotal
if @p == 2 0 0 0
130 0 ▶ $hdr->{$p[0]}{$p[1]}{$p[2]} = $v if @p == 3;
Branch: 1 branch
BranchTrueFalseTotal
if @p == 3 0 0 0
131 0 ▶ die "FATAL: 4+ dot metadata" if @p >= 4;
Branch: 1 branch
BranchTrueFalseTotal
if @p >= 4 0 0 0
132 }
133  
134 0 ▶ if ($data =~ /\GEndPart\015?\012/gc) {
Branch: 4 branches
BranchTrueFalseTotal
if ($data =~ /\GEndPart\015?\012/cg) { } 0 0 0
elsif ($data =~ /\GEnd(\015?\012|$)/cg) { } 0 0 0
elsif ($data =~ /\G([A-Za-z0-9.\-]+)\015?\012/cgs) { } 0 0 0
elsif ($data =~ /\G(.*)/cgs) { } 0 0 0
135 # nop
136 } elsif ($data =~ /\GEnd(\015?\012|$)/gc) {
137 0 last;
138 } elsif ($data =~ /\G([A-Za-z0-9.\-]+)\015?\012/gcs) {
139 0 push @{$meta->{tolc $1}}, $hdr = {};
140 } elsif ($data =~ /\G(.*)/gcs) {
141 0 print STDERR "metadata format error ($1), please report this string: <<$data>>";
142 0 die "metadata format error";
143 }
144 }
145 }
146  
147 #$meta->{tail} = substr $data, pos $data;
148  
149 0 $meta;
150 }
151  
152 =item $string = Net::FCP::Metadata::build_metadata $meta
153  
154 Internal utility function, do not use directly!
155  
156 Takes a hash reference as returned by C<Net::FCP::parse_metadata> and
157 returns the corresponding string form. If a string is given, it's returned
158 as is.
159  
160 =cut
161  
162 0 ▶sub build_metadata_subhash($$$) {
Subroutine build_metadata_subhash: not called CC=3 SCAR=24.8
Pod undocumented
163 0 my ($prefix, $level, $hash) = @_;
164  
165 join "",
166 map
167 ref $hash->{$_} ? build_metadata_subhash ($prefix . (Net::FCP::touc $_) . ".", $level + 1, $hash->{$_})
168 0 ▶ : $prefix . ($level > 1 ? $_ : Net::FCP::touc $_) . "=" . $hash->{$_} . "\n",
Branch: 2 branches
BranchTrueFalseTotal
ref $hash->{$_} ? : 0 0 0
$level > 1 ? : 0 0 0
169 keys %$hash;
170 }
171  
172 0 ▶sub build_metadata_hash($$) {
Subroutine build_metadata_hash: not called CC=2 SCAR=17.9
Pod undocumented
173 0 my ($header, $hash) = @_;
174  
175 0 ▶ if (ref $hash eq ARRAY::) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $hash eq 'ARRAY') { } 0 0 0
176 0 join "", map build_metadata_hash ($header, $_), @$hash
177 } else {
178 0 (Net::FCP::touc $header) . "\n"
179 . (build_metadata_subhash "", 0, $hash)
180 . "EndPart\n";
181 }
182 }
183  
184 0 ▶sub build_metadata($) {
Subroutine build_metadata: not called CC=2 SCAR=17.9
Pod documented
185 0 my ($meta) = @_;
186  
187 0 ▶ return $meta unless ref $meta;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $meta 0 0 0
188  
189 0 $meta = { %$meta };
190  
191 0 delete $meta->{raw};
192  
193 my $res =
194 (build_metadata_hash version => delete $meta->{version})
195 0 . (join "", map +(build_metadata_hash $_, $meta->{$_}), keys %$meta);
196  
197 0 substr $res, -5, 4, ""; # get rid of "Part". Broken Syntax....
198  
199 0 $res;
200 }
201  
202 =back
203  
204 =head1 SEE ALSO
205  
206 L<Net::FCP>.
207  
208 =head1 BUGS
209  
210 Not heavily tested.
211  
212 =head1 AUTHOR
213  
214 Marc Lehmann <schmorp@schmorp.de>
215 http://home.schmorp.de/
216  
217 =cut
218  
219 1 1;
220  
↑ Summary FCP.pm »