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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.3 - (hide annotations) (download)
Wed Nov 13 08:08:51 2002 UTC (22 years ago) by wakaba
Branch: MAIN
CVS Tags: before-dis2-200411, manakai-release-0-3-2, manakai-release-0-3-1, manakai-release-0-4-0, manakai-200612, msg-0-1, HEAD
Branch point for: branch-suikawiki-1, experimental-xml-parser-200401, stable
Changes since 1.2: +7 -3 lines
2002-08-05  Wakaba <w@suika.fam.cx>

	* Util.pm:
	- (sprintxf): Use Message::Util::Wide::unquote_if_quoted_string
	instead of Message::Util::unquote_if_quoted_string.
	- (Message::Util::Wide): New package.
	- (%Message::Util::Wide::REG): New hash.
	- (Message::Util::unquote_if_quoted_string): New function.
	- NOTE: "Wide" package is created to support utf8 string
	of perl 5.7.3 or later.  Utf8 string does not work
	only for [\x00-\xFF] regex of current functions,
	and this regex is used as (?:.|\x0D|\x0A).  (Without
	's' option, "." does not match with newline character.)
	When we can do away problematic code from all
	Message::* modules, we can also do away "Wide" package.

1 wakaba 1.1
2     =head1 NAME
3    
4     Message::Field::AngleQuoted --- A Perl Module for Internet Message
5     Header Field Bodies filled with a URI
6    
7     =cut
8    
9     package Message::Field::AngleQuoted;
10     use strict;
11     require 5.6.0;
12     use re 'eval';
13     use vars qw(%DEFAULT @ISA %REG $VERSION);
14 wakaba 1.3 $VERSION=do{my @r=(q$Revision: 1.2 $=~/\d+/g);sprintf "%d."."%02d" x $#r,@r};
15 wakaba 1.1 require Message::Field::Structured;
16     push @ISA, qw(Message::Field::Structured);
17    
18     %REG = %Message::Util::REG;
19    
20     =head1 CONSTRUCTORS
21    
22     The following methods construct new objects:
23    
24     =over 4
25    
26     =cut
27    
28     %DEFAULT = (
29     -_MEMBERS => [qw|display_name keyword|],
30     -_METHODS => [qw|display_name value
31     comment_add comment_delete comment_item
32     comment_count|],
33     -allow_empty => 0,
34     -comment_to_display_name => 0,
35     #encoding_after_encode
36     #encoding_before_decode
37     #field_param_name
38     #field_name
39     #hook_encode_string
40     #hook_decode_string
41     -output_angle_bracket => 1,
42     -output_comment => 1,
43     -output_display_name => 1,
44     -output_keyword => 0,
45     #parse_all
46     -unsafe_rule_of_display_name => 'NON_http_attribute_char_wsp',
47     -unsafe_rule_of_keyword => 'NON_http_attribute_char_wsp',
48     -use_comment => 1,
49     -use_comment_in_angle => 0,
50     -use_display_name => 1,
51     -use_keyword => 0,
52     );
53    
54     sub _init ($;%) {
55     my $self = shift;
56     my %options = @_;
57     my $DEFAULT = Message::Util::make_clone (\%DEFAULT);
58     $self->SUPER::_init (%$DEFAULT, %options);
59     }
60    
61     =item $uri = Message::Field::URI->new ([%options])
62    
63     Constructs a new object. You might pass some options as parameters
64     to the constructor.
65    
66     =cut
67    
68     ## Inherited
69    
70     =item $uri = Message::Field::URI->parse ($field-body, [%options])
71    
72     Constructs a new object with given field body. You might pass
73     some options as parameters to the constructor.
74    
75     =cut
76    
77     sub parse ($$;%) {
78     my $class = shift;
79     my $self = bless {}, $class;
80     my $body = shift;
81     $self->_init (@_);
82     my ($value, $dname, @comment, $keyword);
83     ($body, @comment)
84     = $self->Message::Util::delete_comment_to_array ($body,
85     -use_angle_quoted => $self->{option}->{use_comment_in_angle}? 0: 1,
86     )
87     if $self->{option}->{use_comment};
88     if ($body =~ /($REG{angle_qcontent})?$REG{M_angle_quoted}/) {
89     ($dname, $value) = ($1, $2);
90     $dname =~ s/^$REG{WSP}+//; $dname =~ s/$REG{WSP}+$//;
91     $dname = $self->Message::Util::decode_quoted_string ($dname);
92     } elsif ($self->{option}->{use_keyword}
93     && $body =~ /^$REG{FWS}($REG{atext_dot})$REG{FWS}$/) {
94     #$keyword = Message::Util::remove_meaningless_wsp ($1);
95     $keyword = $1; $keyword =~ tr/\x09\x20//d;
96     } else {
97     $value = $body;
98     }
99     $self->_save_value ($value, $dname, \@comment, keyword => $keyword);
100     $self;
101     }
102    
103     ## $self->_save_value ($value, $display_name, \@comment)
104     sub _save_value ($$\@%) {
105     my $self = shift;
106     my ($v, $dn, $comment, %misc) = @_;
107     $self->{comment} = $comment;
108     $self->{value} = $v;
109     $self->{display_name} = $dn;
110     $self->{keyword} = $misc{keyword};
111     }
112    
113     sub value ($;$%) {
114     my $self = shift;
115     my $value = shift;
116     if (defined $value) {
117     $self->{value} = $value;
118     }
119     $self->{value};
120     }
121    
122     sub display_name ($;$%) {
123     my $self = shift;
124     my $dname = shift;
125     if (defined $dname) {
126     $self->{display_name} = $dname;
127     }
128 wakaba 1.3 if (length $self->{display_name}) {
129     $self->{display_name};
130     } elsif ($self->{option}->{comment_to_display_name}) {
131     $self->{comment}->[0];
132     }
133 wakaba 1.1 }
134    
135    
136     sub stringify ($;%) {
137     my $self = shift;
138     my %o = @_; my %option = %{$self->{option}};
139     for (grep {/^-/} keys %o) {$option{substr ($_, 1)} = $o{$_}}
140     my %v = $self->_stringify_value (\%option);
141     my ($dn, $as, $cm) = ('', '', '');
142     if (length $v{keyword}) {
143     if ($option{output_keyword}) {
144     my %s = &{$option{hook_encode_string}} ($self, $v{keyword}, type => 'phrase');
145     $as = Message::Util::quote_unsafe_string
146     ($s{value}, unsafe => $option{unsafe_rule_of_keyword});
147     } else {
148     $as = '('. $self->Message::Util::encode_ccontent ($v{keyword}) .')';
149     }
150     } else {
151     if (length ($v{value}) == 0 && !$option{allow_empty}) {
152     return '';
153     }
154     if (length $v{display_name}) {
155     if ($option{use_display_name} && $option{output_display_name}) {
156     my %s = &{$option{hook_encode_string}} ($self,
157     $v{display_name}, type => 'phrase');
158     $dn = Message::Util::quote_unsafe_string
159     ($s{value}, unsafe => $option{unsafe_rule_of_display_name}) . ' ';
160     } elsif ($option{use_comment} && $option{output_comment}) {
161     $dn = ' ('. $self->Message::Util::encode_ccontent ($v{display_name}) .')';
162     }
163     } elsif ($option{comment_to_display_name}
164     && $option{use_display_name} && $option{output_display_name}) {
165     my $fullname = ${$v{comment}}[0]; $option{_comment_min} = 1;
166     if (length $fullname) {
167     my %s = &{$option{hook_encode_string}} ($self, $fullname, type => 'phrase');
168     $dn = Message::Util::quote_unsafe_string
169     ($s{value}, unsafe => $option{unsafe_rule_of_display_name}) . ' ';
170     }
171     }
172    
173     if ($option{output_angle_bracket}) {
174     $as = '<' . $v{value} . '>';
175     } else {
176     $as = $v{value};
177     }
178     }
179     if ($option{use_comment} && $option{output_comment}) {
180     $cm = $self->_comment_stringify (\%option);
181     $cm = ' ' . $cm if $cm;
182     if ($dn && !($option{use_display_name} && $option{output_display_name})) {
183     $cm = $dn . $cm; $dn = '';
184     }
185     }
186     $dn . $as . $cm;
187     }
188     *as_string = \&stringify;
189    
190     ## $self->_stringify_value (\%option)
191     sub _stringify_value ($\%) {
192     my $self = shift;
193     my $option = shift;
194     my %r;
195     $r{value} = ''.$self->{value};
196     $r{display_name} = $self->{display_name};
197     $r{comment} = $self->{comment};
198     $r{keyword} = $self->{keyword};
199     %r;
200     }
201    
202     ## $self->_option_recursive (\%argv)
203     sub _option_recursive ($\%) {
204     my $self = shift;
205     my $o = shift;
206     eval { $self->{value}->option (%$o) if ref $self->{value} };
207     }
208    
209     =head1 LICENSE
210    
211     Copyright 2002 wakaba E<lt>w@suika.fam.cxE<gt>.
212    
213     This program is free software; you can redistribute it and/or modify
214     it under the terms of the GNU General Public License as published by
215     the Free Software Foundation; either version 2 of the License, or
216     (at your option) any later version.
217    
218     This program is distributed in the hope that it will be useful,
219     but WITHOUT ANY WARRANTY; without even the implied warranty of
220     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
221     GNU General Public License for more details.
222    
223     You should have received a copy of the GNU General Public License
224     along with this program; see the file COPYING. If not, write to
225     the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
226     Boston, MA 02111-1307, USA.
227    
228     =head1 CHANGE
229    
230     See F<ChangeLog>.
231 wakaba 1.3 $Date: 2002/06/23 12:10:15 $
232 wakaba 1.1
233     =cut
234    
235     1;

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24