/[suikacvs]/messaging/manakai/lib/Message/Field/CSV.pm
Suika

Contents of /messaging/manakai/lib/Message/Field/CSV.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.6 - (show annotations) (download)
Sat Apr 13 01:33:54 2002 UTC (22 years, 7 months ago) by wakaba
Branch: MAIN
Changes since 1.5: +5 -3 lines
2002-04-13  wakaba <w@suika.fam.cx>

	* Path.pm: Reformed.

1
2 =head1 NAME
3
4 Message::Field::CSV Perl module
5
6 =head1 DESCRIPTION
7
8 Perl module for comma separated C<field>.
9
10 This module supports a number of fields that contains
11 (or does not contain:-)) of comma separated values,
12 such as C<Keywords:>, C<Newsgroups:> and so on.
13
14 =cut
15
16 package Message::Field::CSV;
17 require 5.6.0;
18 use strict;
19 use re 'eval';
20 use vars qw(%OPTION %REG $VERSION);
21 $VERSION=do{my @r=(q$Revision: 1.5 $=~/\d+/g);sprintf "%d."."%02d" x $#r,@r};
22 use overload '@{}' => sub {shift->value},
23 '""' => sub {shift->stringify};
24 require Message::Util;
25 $REG{comment} = qr/\x28(?:\x5C[\x00-\xFF]|[\x00-\x0C\x0E-\x27\x2A-\x5B\x5D-\xFF]+|(??{$REG{comment}}))*\x29/;
26 $REG{quoted_string} = qr/\x22(?:\x5C[\x00-\xFF]|[\x00-\x0C\x0E-\x21\x23-\x5B\x5D-\xFF])*\x22/;
27 $REG{domain_literal} = qr/\x5B(?:\x5C[\x00-\xFF]|[\x00-\x0C\x0E-\x5A\x5E-\xFF])*\x5D/;
28 $REG{uri_literal} = qr/\x3C[\x09\x20\x21\x23-\x3B\x3D\x3F-\x5B\x5D\x5F\x61-\x7A\x7E]*\x3E/;
29
30 $REG{WSP} = qr/[\x20\x09]+/;
31 $REG{FWS} = qr/[\x20\x09]*/;
32 $REG{atext} = qr/[\x21\x23-\x27\x2A\x2B\x2D\x2F\x30-\x39\x3D\x3F\x41-\x5A\x5E-\x7E]+/;
33 $REG{dot_atom} = qr/$REG{atext}(?:$REG{FWS}\x2E$REG{FWS}$REG{atext})*/;
34 $REG{dot_word} = qr/(?:$REG{atext}|$REG{quoted_string})(?:$REG{FWS}\x2E$REG{FWS}(?:$REG{atext}|$REG{quoted_string}))*/;
35 $REG{phrase} = qr/(?:$REG{atext}|$REG{quoted_string})(?:$REG{atext}|$REG{quoted_string}|\.|$REG{FWS})*/;
36 $REG{M_quoted_string} = qr/\x22((?:\x5C[\x00-\xFF]|[\x00-\x0C\x0E-\x21\x23-\x5B\x5D-\xFF])*)\x22/;
37 $REG{NON_atom} = qr/[^\x09\x21\x23-\x27\x2A\x2B\x2D\x2F\x30-\x39\x3D\x3F\x41-\x5A\x5E-\x7E\x2E]/;
38 $REG{NON_atext} = qr/[^\x21\x23-\x27\x2A\x2B\x2D\x2F\x30-\x39\x3D\x3F\x41-\x5A\x5E-\x7E]/;
39 $REG{NON_atext_dot} = qr/[^\x21\x23-\x27\x2A\x2B\x2D-\x39\x3D\x3F\x41-\x5A\x5E-\x7E]/;
40 $REG{NON_atext_dot_wsp} = qr/[^\x09\x20\x21\x23-\x27\x2A\x2B\x2D-\x39\x3D\x3F\x41-\x5A\x5E-\x7E]/;
41 $REG{NON_http_token_wsp} = qr/[^\x09\x20\x21\x23-\x27\x2A\x2B\x2D\x2E\x30-\x39\x41-\x5A\x5E-\x7A\x7C\x7E]/;
42 $REG{NON_component} = qr/[^\x2B\x2D\x2E\x30-\x39\x41-\x5A\x5F\x61-\x7A\x80-\xFF\x2F\x3D\x3F]/;
43 $REG{NON_distribution} = qr/[^\x21\x2B\x2D\x30-\x39\x41-\x5A\x5F\x61-\x7A]/;
44 $REG{S_encoded_word} = qr/=\x3F$REG{atext_dot}\x3F=/;
45
46 ## Keywords: foo, bar, "and so on"
47 ## Newsgroups: local.test,local.foo,local.bar
48 ## Accept: text/html; q=1.0, text/plain; q=0.03; *; q=0.01
49
50 %OPTION = (
51 field_name => 'keywords',
52 encoding_after_encode => '*default',
53 encoding_before_decode => '*default',
54 hook_encode_string => #sub {shift; (value => shift, @_)},
55 \&Message::Util::encode_header_string,
56 hook_decode_string => #sub {shift; (value => shift, @_)},
57 \&Message::Util::decode_header_string,
58 is_quoted_string => 1, ## Can itself quoted-string?
59 long_count => 10,
60 remove_comment => 1,
61 separator => ', ',
62 separator_long => ', ',
63 max => -1,
64 value_type => [':none:'],
65 value_unsafe_rule => 'NON_http_token_wsp',
66 );
67
68 sub _init_option ($) {
69 my $self = shift;
70 my %field_type = qw(accept-charset accept accept-encoding accept
71 accept-language accept
72 content-language keywords
73 followup-to newsgroups
74 list-archive list- list-digest list- list-help list-
75 list-owner list- list-post list- list-subscribe list-
76 list-unsubscribe list- list-url list- uri list-
77 posted-to newsgroups
78 x-brother x-moe x-daughter x-moe
79 x-respect x-moe x-syster x-moe x-wife x-moe);
80 my $field_name = lc $self->{option}->{field_name}
81 || lc $self->{option}->{-field_name}; ## AD HOC
82 $field_name = $field_type{$field_name} || $field_name;
83 if ($field_name eq 'newsgroups') {
84 $self->{option}->{separator} = ',';
85 $self->{option}->{separator_long} = ', ';
86 $self->{option}->{long_count} = 5;
87 $self->{option}->{value_unsafe_rule} = 'NON_component';
88 $self->{option}->{encoding_after_encode} = 'utf-8';
89 } elsif ($field_name eq 'distribution') {
90 $self->{option}->{separator} = ',';
91 $self->{option}->{separator_long} = ', ';
92 $self->{option}->{long_count} = 15;
93 $self->{option}->{value_unsafe_rule} = 'NON_distribution';
94 } elsif ($field_name eq 'x-moe') {
95 $self->{option}->{is_quoted_string} = -1;
96 $self->{option}->{value_type} = ['Message::Field::ValueParams',
97 {format => $self->{option}->{format}}];
98 } elsif ($field_name eq 'accept') {
99 $self->{option}->{is_quoted_string} = -1;
100 $self->{option}->{value_type} = ['Message::Field::ValueParams',
101 {format => $self->{option}->{format}}];
102 } elsif ($field_name eq 'list-') {
103 $self->{option}->{is_quoted_string} = -1;
104 $self->{option}->{remove_comment} = -1;
105 $self->{option}->{value_type} = ['Message::Field::URI',
106 {field_name => $self->{option}->{field_name},
107 format => $self->{option}->{format}}];
108 } elsif ($field_name eq 'encrypted') {
109 $self->{option}->{max} = 2;
110 }
111 $self;
112 }
113
114 =head2 Message::Field::CSV->new ()
115
116 Returns new CSV field body.
117
118 =cut
119
120 sub new ($;%) {
121 my $self = bless {}, shift;
122 my %option = @_;
123 for (%OPTION) {$option{$_} ||= $OPTION{$_}}
124 $self->{option} = \%option;
125 $self->_init_option ();
126 $self;
127 }
128
129 =head2 Message::Field::CSV->parse ($unfolded_field_body)
130
131 Parses C<field-body>.
132
133 =cut
134
135 sub parse ($$;%) {
136 my $self = bless {}, shift;
137 my $field_body = shift;
138 my %option = @_;
139 for (%OPTION) {$option{$_} ||= $OPTION{$_}}
140 $self->{option} = \%option;
141 $self->_init_option ();
142 $field_body = $self->_delete_comment ($field_body)
143 unless $option{remove_comment}<0;
144 @{$self->{value}} = $self->_parse_list ($field_body);
145 $self;
146 }
147
148 sub _parse_list ($$) {
149 my $self = shift;
150 my $fb = shift;
151 my @ids;
152 $fb =~ s{((?:$REG{quoted_string}|$REG{uri_literal}|$REG{domain_literal}|$REG{comment}|[^\x22\x28\x2C\x3C\x5B])+)}{
153 my $s = $1; $s =~ s/^$REG{WSP}+//; $s =~ s/$REG{WSP}+$//;
154 if ($self->{option}->{is_quoted_string}>0) {
155 push @ids, $self->_value ($self->_decode_quoted_string ($s));
156 } else {
157 push @ids, $self->_value ($s);
158 }
159 }goex;
160 @ids;
161 }
162
163 =head2 $self->value ()
164
165 Returns array reference to value list.
166
167 =cut
168
169 sub value ($) {shift->{value}}
170
171 =head2 $self->add ($value, [%option])
172
173 Adds new value.
174
175 =cut
176
177 sub add ($;$%) {
178 my $self = shift;
179 my ($value, %option) = @_;
180 push @{$self->{value}}, $self->_value ($value);
181 $value;
182 }
183
184 ## Hook called before returning C<value>.
185 ## $self->_param_value ($name, $value);
186 sub _value ($$) {
187 my $self = shift;
188 my $value = shift;
189 my $vtype = $self->{option}->{value_type}->[0];
190 my %vopt; %vopt = %{$self->{option}->{value_type}->[1]}
191 if ref $self->{option}->{value_type}->[1];
192 if (ref $value) {
193 return $value;
194 } elsif ($vtype eq ':none:') {
195 return $value;
196 } elsif ($value) {
197 eval "require $vtype";
198 return $vtype->parse ($value, %vopt);
199 } else {
200 eval "require $vtype";
201 return $vtype->new (%vopt);
202 }
203 }
204
205 sub stringify ($;%) {
206 my $self = shift;
207 my %option = @_;
208 $option{separator} ||= $self->{option}->{separator};
209 $option{separator_long} ||= $self->{option}->{separator_long};
210 $option{long_count} ||= $self->{option}->{long_count};
211 $option{max} ||= $self->{option}->{max};
212 $option{is_quoted_string} ||= $self->{option}->{is_quoted_string};
213 $option{value_unsafe_rule} ||= $self->{option}->{value_unsafe_rule};
214 $self->_delete_empty ();
215 $option{max}--;
216 $option{max} = $#{$self->{value}} if $option{max}<0;
217 $option{max} = $#{$self->{value}} if $#{$self->{value}}<$option{max};
218 $option{separator} = $option{separator_long}
219 if $option{max} >= $option{long_count};
220 join $option{separator},
221 map {
222 if ($option{is_quoted_string}>0) {
223 my %s = &{$self->{option}->{hook_encode_string}} ($self,
224 $_, type => 'phrase');
225 $self->_quote_unsafe_string ($s{value},
226 unsafe => $option{value_unsafe_rule});
227 } else {
228 $_;
229 }
230 } @{$self->{value}}[0..$option{max}];
231 }
232
233 =head2 $self->option ($option_name, [$option_value])
234
235 Set/gets new value of the option.
236
237 =cut
238
239 sub option ($$;$) {
240 my $self = shift;
241 my ($name, $value) = @_;
242 if (defined $value) {
243 $self->{option}->{$name} = $value;
244 }
245 $self->{option}->{$name};
246 }
247
248 sub value_type ($;$%) {
249 my $self = shift;
250 my $new_value_type = shift;
251 if ($new_value_type) {
252 $self->{option}->{value_type}->[0] = $new_value_type;
253 }
254 $self->{option}->{value_type}->[0] || ':none:';
255 }
256
257 sub _delete_empty ($) {
258 my $self = shift;
259 my @nid;
260 for my $id (@{$self->{value}}) {push @nid, $id if length $id}
261 $self->{value} = \@nid;
262 }
263
264 sub _quote_unsafe_string ($$;%) {
265 my $self = shift;
266 my $string = shift;
267 my %option = @_;
268 $option{unsafe} ||= 'NON_atext_dot';
269 if ($string =~ /$REG{$option{unsafe}}/ || $string =~ /$REG{WSP}$REG{WSP}+/) {
270 $string =~ s/([\x22\x5C])([\x21-\x7E])?/"\x5C$1".(defined $2?"\x5C$2":'')/ge;
271 $string = '"'.$string.'"';
272 }
273 $string;
274 }
275
276
277 =head2 $self->_unquote_quoted_string ($string)
278
279 Unquote C<quoted-string>. Get rid of C<DQUOTE>s and
280 C<REVERSED SOLIDUS> included in C<quoted-pair>.
281 This method is intended for internal use.
282
283 =cut
284
285 sub _unquote_quoted_string ($$) {
286 my $self = shift;
287 my $quoted_string = shift;
288 $quoted_string =~ s{$REG{M_quoted_string}}{
289 my $qtext = $1;
290 $qtext =~ s/\x5C([\x00-\xFF])/$1/g;
291 $qtext;
292 }goex;
293 $quoted_string;
294 }
295
296 sub _decode_quoted_string ($$) {
297 my $self = shift;
298 my $quoted_string = shift;
299 $quoted_string =~ s{$REG{M_quoted_string}|([^\x22]+)}{
300 my ($qtext,$t) = ($1, $2);
301 if ($t) {
302 my %s = &{$self->{option}->{hook_decode_string}} ($self, $t,
303 type => 'phrase');
304 $s{value};
305 } else {
306 $qtext =~ s/\x5C([\x00-\xFF])/$1/g;
307 my %s = &{$self->{option}->{hook_decode_string}} ($self, $qtext,
308 type => 'phrase/quoted');
309 $s{value};
310 }
311 }goex;
312 $quoted_string;
313 }
314
315 =head2 $self->_delete_comment ($field_body)
316
317 Remove all C<comment> in given strictured C<field-body>.
318 This method is intended to be used for internal process.
319
320 =cut
321
322 sub _delete_comment ($$) {
323 my $self = shift;
324 my $body = shift;
325 $body =~ s{($REG{quoted_string}|$REG{uri_literal}|$REG{domain_literal})|$REG{comment}}{
326 my $o = $1; $o? $o : ' ';
327 }gex;
328 $body;
329 }
330
331 =head1 EXAMPLE
332
333
334 =head1 LICENSE
335
336 Copyright 2002 wakaba E<lt>w@suika.fam.cxE<gt>.
337
338 This program is free software; you can redistribute it and/or modify
339 it under the terms of the GNU General Public License as published by
340 the Free Software Foundation; either version 2 of the License, or
341 (at your option) any later version.
342
343 This program is distributed in the hope that it will be useful,
344 but WITHOUT ANY WARRANTY; without even the implied warranty of
345 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
346 GNU General Public License for more details.
347
348 You should have received a copy of the GNU General Public License
349 along with this program; see the file COPYING. If not, write to
350 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
351 Boston, MA 02111-1307, USA.
352
353 =head1 CHANGE
354
355 See F<ChangeLog>.
356 $Date: 2002/04/01 05:32:15 $
357
358 =cut
359
360 1;

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24