/[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.10 - (hide annotations) (download)
Wed May 8 09:11:31 2002 UTC (22 years, 6 months ago) by wakaba
Branch: MAIN
Changes since 1.9: +25 -22 lines
2002-05-08  wakaba <w@suika.fam.cx>

	* Structured.pm (item, method_available): New methods.
	* Addresses.pm: New module.

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

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24