1 |
wakaba |
1.1 |
|
2 |
|
|
=head1 NAME |
3 |
|
|
|
4 |
wakaba |
1.7 |
Message::Field::CSV --- Perl module for Internet message |
5 |
|
|
field body consist of comma separated values |
6 |
wakaba |
1.1 |
|
7 |
|
|
=cut |
8 |
|
|
|
9 |
|
|
package Message::Field::CSV; |
10 |
|
|
require 5.6.0; |
11 |
|
|
use strict; |
12 |
|
|
use re 'eval'; |
13 |
wakaba |
1.7 |
use vars qw(@ISA %REG $VERSION); |
14 |
wakaba |
1.9 |
$VERSION=do{my @r=(q$Revision: 1.8 $=~/\d+/g);sprintf "%d."."%02d" x $#r,@r}; |
15 |
wakaba |
1.4 |
require Message::Util; |
16 |
wakaba |
1.7 |
require Message::Field::Structured; |
17 |
|
|
push @ISA, qw(Message::Field::Structured); |
18 |
|
|
|
19 |
|
|
use overload '""' => sub { $_[0]->stringify }, |
20 |
|
|
'0+' => sub { $_[0]->count }, |
21 |
wakaba |
1.9 |
'@{}' => sub { $_[0]->{value} }, ## SHOULD NOT be used |
22 |
wakaba |
1.7 |
'.=' => sub { $_[0]->add ($_[1]); $_[0] }, |
23 |
|
|
fallback => 1; |
24 |
|
|
|
25 |
|
|
*REG = \%Message::Util::REG; |
26 |
|
|
## Inherited: comment, quoted_string, domain_literal, angle_quoted |
27 |
|
|
## WSP, FWS, atext |
28 |
|
|
|
29 |
|
|
## From usefor-article |
30 |
|
|
$REG{NON_component} = qr/[^\x2B\x2D\x2E\x30-\x39\x41-\x5A\x5F\x61-\x7A\x80-\xFF\x2F\x3D\x3F]/; |
31 |
|
|
$REG{NON_distribution} = qr/[^\x21\x2B\x2D\x30-\x39\x41-\x5A\x5F\x61-\x7A]/; |
32 |
|
|
|
33 |
|
|
=head1 CONSTRUCTORS |
34 |
|
|
|
35 |
|
|
The following methods construct new objects: |
36 |
|
|
|
37 |
|
|
=over 4 |
38 |
|
|
|
39 |
|
|
=cut |
40 |
|
|
|
41 |
|
|
## Initialize of this class -- called by constructors |
42 |
|
|
sub _init ($;%) { |
43 |
|
|
my $self = shift; |
44 |
|
|
my %options = @_; |
45 |
|
|
my %DEFAULT = ( |
46 |
wakaba |
1.9 |
#encoding_after_encode |
47 |
|
|
#encoding_before_decode |
48 |
wakaba |
1.7 |
-field_name => 'keywords', |
49 |
wakaba |
1.9 |
#format |
50 |
|
|
#hook_encode_string |
51 |
|
|
#hook_decode_string |
52 |
wakaba |
1.7 |
-is_quoted_string => 1, ## Can itself quoted-string? |
53 |
|
|
-long_count => 10, |
54 |
|
|
-parse_all => 0, |
55 |
|
|
-remove_comment => 1, |
56 |
|
|
-separator => ', ', |
57 |
|
|
-separator_long => ', ', |
58 |
|
|
-max => 0, |
59 |
wakaba |
1.9 |
#value_type |
60 |
wakaba |
1.7 |
-value_unsafe_rule => 'NON_http_token_wsp', |
61 |
|
|
); |
62 |
|
|
$self->SUPER::_init (%DEFAULT, %options); |
63 |
|
|
$self->{value} = []; |
64 |
wakaba |
1.1 |
|
65 |
|
|
## Keywords: foo, bar, "and so on" |
66 |
|
|
## Newsgroups: local.test,local.foo,local.bar |
67 |
|
|
## Accept: text/html; q=1.0, text/plain; q=0.03; *; q=0.01 |
68 |
|
|
|
69 |
|
|
my %field_type = qw(accept-charset accept accept-encoding accept |
70 |
|
|
accept-language accept |
71 |
wakaba |
1.2 |
content-language keywords |
72 |
wakaba |
1.1 |
followup-to newsgroups |
73 |
wakaba |
1.4 |
list-archive list- list-digest list- list-help list- |
74 |
|
|
list-owner list- list-post list- list-subscribe list- |
75 |
|
|
list-unsubscribe list- list-url list- uri list- |
76 |
wakaba |
1.6 |
posted-to newsgroups |
77 |
wakaba |
1.9 |
x-brother x-moe x-boss x-moe x-daughter x-moe x-dearfriend x-moe |
78 |
|
|
x-favoritesong x-moe |
79 |
|
|
x-friend x-moe x-me x-moe |
80 |
|
|
x-respect x-moe x-sister x-moe x-son x-moe x-sublimate x-moe x-wife x-moe); |
81 |
wakaba |
1.7 |
my $field_name = lc $self->{option}->{field_name}; |
82 |
wakaba |
1.1 |
$field_name = $field_type{$field_name} || $field_name; |
83 |
wakaba |
1.2 |
if ($field_name eq 'newsgroups') { |
84 |
|
|
$self->{option}->{separator} = ','; |
85 |
wakaba |
1.4 |
$self->{option}->{separator_long} = ', '; |
86 |
|
|
$self->{option}->{long_count} = 5; |
87 |
|
|
$self->{option}->{value_unsafe_rule} = 'NON_component'; |
88 |
wakaba |
1.5 |
$self->{option}->{encoding_after_encode} = 'utf-8'; |
89 |
wakaba |
1.4 |
} 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 |
wakaba |
1.2 |
} elsif ($field_name eq 'x-moe') { |
95 |
wakaba |
1.7 |
$self->{option}->{is_quoted_string} = 0; |
96 |
wakaba |
1.9 |
$self->{option}->{value_type}->{'*default'} = ['Message::Field::XMoe']; |
97 |
wakaba |
1.1 |
} elsif ($field_name eq 'accept') { |
98 |
wakaba |
1.7 |
$self->{option}->{is_quoted_string} = 0; |
99 |
wakaba |
1.9 |
$self->{option}->{value_type}->{'*default'} = ['Message::Field::ValueParams']; |
100 |
wakaba |
1.4 |
} elsif ($field_name eq 'list-') { |
101 |
wakaba |
1.7 |
$self->{option}->{is_quoted_string} = 0; |
102 |
|
|
$self->{option}->{remove_comment} = 0; |
103 |
wakaba |
1.9 |
$self->{option}->{value_type}->{'*default'} = ['Message::Field::URI']; |
104 |
wakaba |
1.8 |
#} elsif ($field_name eq 'p3p') { |
105 |
|
|
# $self->{option}->{is_quoted_string} = 0; |
106 |
wakaba |
1.9 |
# $self->{option}->{value_type}->{'*default'} = ['Message::Field::Params']; |
107 |
wakaba |
1.1 |
} elsif ($field_name eq 'encrypted') { |
108 |
|
|
$self->{option}->{max} = 2; |
109 |
|
|
} |
110 |
wakaba |
1.7 |
|
111 |
|
|
if (ref $options{value} eq 'ARRAY') { |
112 |
|
|
$self->add (@{$options{value}}); |
113 |
|
|
} elsif ($options{value}) { |
114 |
|
|
$self->add ($options{value}); |
115 |
|
|
} |
116 |
wakaba |
1.1 |
$self; |
117 |
|
|
} |
118 |
|
|
|
119 |
wakaba |
1.7 |
=item $csv = Message::Field::CSV->new ([%options]) |
120 |
wakaba |
1.1 |
|
121 |
wakaba |
1.7 |
Constructs a new object. You might pass some options as parameters |
122 |
|
|
to the constructor. |
123 |
wakaba |
1.1 |
|
124 |
|
|
=cut |
125 |
|
|
|
126 |
wakaba |
1.7 |
## Inherited |
127 |
wakaba |
1.1 |
|
128 |
wakaba |
1.7 |
=item $csv = Message::Field::CSV->parse ($field-body, [%options]) |
129 |
wakaba |
1.1 |
|
130 |
wakaba |
1.7 |
Constructs a new object with given field body. You might pass |
131 |
|
|
some options as parameters to the constructor. |
132 |
wakaba |
1.1 |
|
133 |
|
|
=cut |
134 |
|
|
|
135 |
|
|
sub parse ($$;%) { |
136 |
wakaba |
1.7 |
my $class = shift; |
137 |
|
|
my $self = bless {}, $class; |
138 |
wakaba |
1.1 |
my $field_body = shift; |
139 |
wakaba |
1.7 |
$self->_init (@_); |
140 |
|
|
$field_body = Message::Util::delete_comment ($field_body) |
141 |
|
|
if $self->{option}->{remove_comment}; |
142 |
|
|
push @{$self->{value}}, $self->_parse_list ($field_body); |
143 |
wakaba |
1.1 |
$self; |
144 |
|
|
} |
145 |
|
|
|
146 |
wakaba |
1.7 |
## Parses csv string and returns array |
147 |
wakaba |
1.1 |
sub _parse_list ($$) { |
148 |
|
|
my $self = shift; |
149 |
|
|
my $fb = shift; |
150 |
|
|
my @ids; |
151 |
wakaba |
1.7 |
$fb =~ s{((?:$REG{quoted_string}|$REG{angle_quoted}|$REG{domain_literal}|$REG{comment}|[^\x22\x28\x2C\x3C\x5B])+)}{ |
152 |
wakaba |
1.1 |
my $s = $1; $s =~ s/^$REG{WSP}+//; $s =~ s/$REG{WSP}+$//; |
153 |
wakaba |
1.7 |
if ($self->{option}->{is_quoted_string}) { |
154 |
wakaba |
1.9 |
$s = $self->_parse_value ('*default' => |
155 |
|
|
Message::Util::decode_quoted_string ($self, $s)) |
156 |
wakaba |
1.7 |
if $self->{option}->{parse_all}; |
157 |
|
|
push @ids, Message::Util::decode_quoted_string ($self, $s); |
158 |
wakaba |
1.1 |
} else { |
159 |
wakaba |
1.9 |
$s = $self->_parse_value ('*default' => $s) if $self->{option}->{parse_all}; |
160 |
wakaba |
1.7 |
push @ids, $s; |
161 |
wakaba |
1.1 |
} |
162 |
|
|
}goex; |
163 |
|
|
@ids; |
164 |
|
|
} |
165 |
|
|
|
166 |
wakaba |
1.7 |
=back |
167 |
|
|
|
168 |
|
|
=head1 METHODS |
169 |
|
|
|
170 |
|
|
=over 4 |
171 |
|
|
|
172 |
|
|
=head2 $values = $csv->value ($index1, [$index2, $index3,...]) |
173 |
|
|
|
174 |
|
|
Returns C<$index>'th value(s). |
175 |
|
|
|
176 |
|
|
=cut |
177 |
|
|
|
178 |
|
|
sub value ($@) { |
179 |
|
|
my $self = shift; |
180 |
|
|
my @index = @_; |
181 |
|
|
my @ret = (); |
182 |
|
|
for (@index) { |
183 |
wakaba |
1.9 |
$self->{value}->[$_] = $self->_parse_value ('*default' => $self->{value}->[$_]); |
184 |
wakaba |
1.7 |
push @ret, $self->{value}->[$_]; |
185 |
|
|
} |
186 |
|
|
@ret; |
187 |
|
|
} |
188 |
|
|
|
189 |
|
|
=item $number = $csv->count |
190 |
wakaba |
1.1 |
|
191 |
wakaba |
1.7 |
Returns number of values. |
192 |
wakaba |
1.1 |
|
193 |
|
|
=cut |
194 |
|
|
|
195 |
wakaba |
1.7 |
sub count ($) { |
196 |
|
|
my $self = shift; |
197 |
|
|
$self->_delete_empty; |
198 |
|
|
$#{$self->{value}}+1; |
199 |
|
|
} |
200 |
|
|
|
201 |
|
|
=iterm $csv->add ($value1, [$value2, $value3,...]) |
202 |
|
|
|
203 |
|
|
Adds (appends) new value(s). |
204 |
wakaba |
1.1 |
|
205 |
wakaba |
1.7 |
=iterm $csv->prepend ($value1, [$value2, $value3,...]) |
206 |
wakaba |
1.1 |
|
207 |
wakaba |
1.7 |
Prepends new value(s). |
208 |
wakaba |
1.1 |
|
209 |
|
|
=cut |
210 |
|
|
|
211 |
wakaba |
1.7 |
sub add ($@) { |
212 |
wakaba |
1.1 |
my $self = shift; |
213 |
wakaba |
1.7 |
push @{$self->{value}}, @_; |
214 |
wakaba |
1.2 |
} |
215 |
|
|
|
216 |
wakaba |
1.7 |
sub prepend ($@) { |
217 |
wakaba |
1.2 |
my $self = shift; |
218 |
wakaba |
1.7 |
unshift @{$self->{value}}, @_; |
219 |
wakaba |
1.1 |
} |
220 |
|
|
|
221 |
wakaba |
1.7 |
=item $field-body = $csv->stringify () |
222 |
|
|
|
223 |
|
|
Returns C<field-body> as a string. |
224 |
|
|
|
225 |
|
|
=cut |
226 |
|
|
|
227 |
wakaba |
1.1 |
sub stringify ($;%) { |
228 |
|
|
my $self = shift; |
229 |
wakaba |
1.7 |
my %o = @_; |
230 |
|
|
my %option = %{$self->{option}}; |
231 |
|
|
for (grep {/^-/} keys %o) {$option{substr ($_, 1)} = $o{$_}} |
232 |
|
|
$self->_delete_empty; |
233 |
wakaba |
1.1 |
$option{max}--; |
234 |
wakaba |
1.7 |
$option{max} = $#{$self->{value}} if $option{max} <= 0; |
235 |
|
|
$option{max} = $#{$self->{value}} if $#{$self->{value}} < $option{max}; |
236 |
wakaba |
1.4 |
$option{separator} = $option{separator_long} |
237 |
|
|
if $option{max} >= $option{long_count}; |
238 |
wakaba |
1.1 |
join $option{separator}, |
239 |
wakaba |
1.3 |
map { |
240 |
wakaba |
1.7 |
if ($option{is_quoted_string}) { |
241 |
wakaba |
1.3 |
my %s = &{$self->{option}->{hook_encode_string}} ($self, |
242 |
|
|
$_, type => 'phrase'); |
243 |
wakaba |
1.7 |
Message::Util::quote_unsafe_string ($s{value}, |
244 |
wakaba |
1.4 |
unsafe => $option{value_unsafe_rule}); |
245 |
wakaba |
1.3 |
} else { |
246 |
|
|
$_; |
247 |
|
|
} |
248 |
|
|
} @{$self->{value}}[0..$option{max}]; |
249 |
wakaba |
1.1 |
} |
250 |
wakaba |
1.7 |
*as_string = \&stringify; |
251 |
|
|
|
252 |
|
|
=item $option-value = $ua->option ($option-name) |
253 |
|
|
|
254 |
|
|
Gets option value. |
255 |
wakaba |
1.1 |
|
256 |
wakaba |
1.7 |
=item $csv->option ($option-name, $option-value, ...) |
257 |
wakaba |
1.4 |
|
258 |
wakaba |
1.7 |
Set option value(s). You can pass multiple option name-value pair |
259 |
|
|
as parameter when setting. |
260 |
wakaba |
1.4 |
|
261 |
|
|
=cut |
262 |
|
|
|
263 |
wakaba |
1.7 |
## Inherited |
264 |
|
|
|
265 |
|
|
=item $type = $csv->value_type |
266 |
|
|
|
267 |
|
|
Gets value-type. Value-type is package name of module |
268 |
|
|
used for value modification. A special value-type, ':none:' |
269 |
|
|
is used to indicate values are non-structured (and no module |
270 |
|
|
is automatically used). |
271 |
|
|
|
272 |
|
|
=item $csv->value_type ([$type]) |
273 |
|
|
|
274 |
|
|
Set value-type. |
275 |
|
|
|
276 |
|
|
=cut |
277 |
wakaba |
1.4 |
|
278 |
wakaba |
1.7 |
sub value_type ($;$) { |
279 |
wakaba |
1.5 |
my $self = shift; |
280 |
|
|
my $new_value_type = shift; |
281 |
wakaba |
1.7 |
if (ref $new_value_type eq 'ARRAY') { |
282 |
|
|
$self->{option}->{value_type} = $new_value_type; |
283 |
|
|
} elsif ($new_value_type) { |
284 |
wakaba |
1.5 |
$self->{option}->{value_type}->[0] = $new_value_type; |
285 |
|
|
} |
286 |
|
|
$self->{option}->{value_type}->[0] || ':none:'; |
287 |
|
|
} |
288 |
|
|
|
289 |
wakaba |
1.7 |
=item $clone = $ua->clone () |
290 |
wakaba |
1.1 |
|
291 |
wakaba |
1.7 |
Returns a copy of the object. |
292 |
wakaba |
1.1 |
|
293 |
wakaba |
1.7 |
=cut |
294 |
wakaba |
1.1 |
|
295 |
wakaba |
1.7 |
sub clone ($) { |
296 |
|
|
my $self = shift; |
297 |
|
|
$self->_delete_empty; |
298 |
|
|
my $clone = $self->SUPER::clone; |
299 |
|
|
$clone->{value_type} = Message::Util::make_clone ($self->{value_type}); |
300 |
|
|
$clone; |
301 |
|
|
} |
302 |
wakaba |
1.1 |
|
303 |
wakaba |
1.7 |
=back |
304 |
wakaba |
1.1 |
|
305 |
|
|
=cut |
306 |
|
|
|
307 |
wakaba |
1.7 |
## Internal functions |
308 |
wakaba |
1.1 |
|
309 |
wakaba |
1.7 |
sub _delete_empty ($) { |
310 |
wakaba |
1.1 |
my $self = shift; |
311 |
wakaba |
1.7 |
$self->{value} = [grep {length $_} @{$self->{value}}]; |
312 |
wakaba |
1.1 |
} |
313 |
|
|
|
314 |
|
|
=head1 LICENSE |
315 |
|
|
|
316 |
|
|
Copyright 2002 wakaba E<lt>w@suika.fam.cxE<gt>. |
317 |
|
|
|
318 |
|
|
This program is free software; you can redistribute it and/or modify |
319 |
|
|
it under the terms of the GNU General Public License as published by |
320 |
|
|
the Free Software Foundation; either version 2 of the License, or |
321 |
|
|
(at your option) any later version. |
322 |
|
|
|
323 |
|
|
This program is distributed in the hope that it will be useful, |
324 |
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
325 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
326 |
|
|
GNU General Public License for more details. |
327 |
|
|
|
328 |
|
|
You should have received a copy of the GNU General Public License |
329 |
|
|
along with this program; see the file COPYING. If not, write to |
330 |
|
|
the Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
331 |
|
|
Boston, MA 02111-1307, USA. |
332 |
|
|
|
333 |
|
|
=head1 CHANGE |
334 |
|
|
|
335 |
|
|
See F<ChangeLog>. |
336 |
wakaba |
1.9 |
$Date: 2002/04/22 08:28:20 $ |
337 |
wakaba |
1.1 |
|
338 |
|
|
=cut |
339 |
|
|
|
340 |
|
|
1; |