/[suikacvs]/messaging/bunshin/Bunshin.pm
Suika

Diff of /messaging/bunshin/Bunshin.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.3 by wakaba, Thu Jun 20 11:36:32 2002 UTC revision 1.6 by wakaba, Tue Sep 10 23:37:43 2002 UTC
# Line 20  sub new ($;%) { Line 20  sub new ($;%) {
20    my $class = shift;    my $class = shift;
21    my $self = bless {}, $class;    my $self = bless {}, $class;
22    $self->{fmt2str} = Message::Util::make_clone    $self->{fmt2str} = Message::Util::make_clone
23      ($Message::Field::Date::DEFAULT{-fmt2str});      (\%Message::Field::Date::FMT2STR);
24    $self;    $self;
25  }  }
26    
# Line 51  sub set_hook_function ($$\&) { Line 51  sub set_hook_function ($$\&) {
51    $self->{'hook_'.$name} = $function;    $self->{'hook_'.$name} = $function;
52  }  }
53    
54    sub set_element_decoders ($%) {
55      my $self = shift;
56      my %list = @_;
57      for (keys %list) {
58        if ($list{$_} eq 'enentity_html') {
59          $list{$_} = \&Message::Util::enentity_html;
60        } elsif ($list{$_} eq 'deentity_html') {
61          $list{$_} = \&Message::Util::deentity_html;
62        ## TODO: escape_uri, unescape_uri
63        }
64      }
65      $self->{element_decoder} = %list;
66    }
67    
68  sub set_format ($$\&) {  sub set_format ($$\&) {
69    my $self = shift;    my $self = shift;
70    my $name = shift;    my $name = shift;
# Line 67  sub set_elements ($$@) { Line 81  sub set_elements ($$@) {
81  sub set_source ($%) {  sub set_source ($%) {
82    my $self = shift;    my $self = shift;
83    my %option = @_;    my %option = @_;
84      ($self->{source}, $self->{meta_info}) = $self->_get_resource (%option);
85      $self->default_parameter (base_uri => $option{uri}) if $option{uri};
86            ## BUG: Doesn't support redirection
87      if ($option{uri} || $option{file}) {
88        my $c = $self->{hook_code_conversion} || \&_code_conversion;
89        $self->{source} = &$c ($self, $self->{source}, \%option, $self->{meta_info});
90      }
91      $self->{source} =~ s/\x0D(?!\x0A)/\x0D\x0A/g;
92      $self->{source} =~ s/(?<!\x0D)\x0A/\x0D\x0A/g;
93      $self;
94    }
95    
96    sub _get_resource ($%) {
97      my $self = shift;
98      my %option = @_;
99      my ($resource, $meta);
100    if (defined $option{value}) {    if (defined $option{value}) {
101      $self->{source} = $option{value};      $resource = $option{value};
102    } elsif ($option{uri}) {    } elsif ($option{uri}) {
103      require Message::Field::UA;      require Message::Field::UA;
104      require LWP::UserAgent;      require LWP::UserAgent;
# Line 79  sub set_source ($%) { Line 109  sub set_source ($%) {
109      $lwp->agent ($ua->stringify);      $lwp->agent ($ua->stringify);
110      my $req = HTTP::Request->new (GET => $option{uri});      my $req = HTTP::Request->new (GET => $option{uri});
111      my $res = $lwp->request ($req);      my $res = $lwp->request ($req);
112      my $c = $self->{hook_code_conversion} || \&_code_conversion;      $resource = $res->content;
113      $self->{source} = &$c ($self, $res->content, \%option);      $meta = parse Message::Header $res->headers_as_string,
114      $self->default_parameter (base_uri => $option{uri});        -parse_all => 0, -format => 'http-response',
115        ;
116    } elsif ($option{file}) {    } elsif ($option{file}) {
117      my $f = new FileHandle $option{file} => 'r';      my $f = new FileHandle $option{file} => 'r';
118      Carp::croak "set_source: $option{file}: $!" unless defined $f;      Carp::croak "set_source: $option{file}: $!" unless defined $f;
119      my $c = $self->{hook_code_conversion} || \&_code_conversion;      my $c = $self->{hook_code_conversion} || \&_code_conversion;
120      local $/ = undef;      local $/ = undef;
121      $self->{source} = &$c ($self, $f->getline, \%option);      $resource = $f->getline;
122    } else {    } else {
123      Carp::croak "set_source: $_[0]: Unsupported data source type";      Carp::croak "_get_resource: $_[0]: Unsupported data source type";
124    }    }
125    $self->{source} =~ s/\x0D(?!\x0A)/\x0D\x0A/g;    ($resource, $meta);
   $self->{source} =~ s/(?<!\x0D)\x0A/\x0D\x0A/g;  
   $self;  
126  }  }
127    
128  ## $self->_code_conversion ($string, \%option)  ## $self->_code_conversion ($string, \%option, $meta_info)
129  sub _code_conversion ($$\%) {  sub _code_conversion ($$\%$) {
130    $_[1];    $_[1];
131  }  }
132    
# Line 122  sub make_msgs ($) { Line 151  sub make_msgs ($) {
151      for my $i (0..$#{$self->{elements_message}}) {      for my $i (0..$#{$self->{elements_message}}) {
152        $p{$self->{elements_message}->[$i]} = ${$i+1};        $p{$self->{elements_message}->[$i]} = ${$i+1};
153      }      }
154        for my $n (keys %{$self->{element_decoder}}) {
155          if ($p{$n} && ref $self->{element_decoder}->{$n}) {
156            $p{$n} = &{ $self->{element_decoder}->{$n} } ($p{$n});
157          }
158        }
159      my $msg = &$f ($self, %p);      my $msg = &$f ($self, %p);
160      push @msg, $msg;      push @msg, $msg;
161    }gesx;    }gesx;
# Line 136  sub _make_a_msg ($@) { Line 170  sub _make_a_msg ($@) {
170      -fill_date  => 0,      -fill_date  => 0,
171      -fill_msgid => 0,      -fill_msgid => 0,
172      -fill_ua_name       => 'x-shimbun-agent',      -fill_ua_name       => 'x-shimbun-agent',
173        -format     => 'news-usefor',
174      -parse_all  => 1,      -parse_all  => 1,
175    ;    ;
176    my $hdr = $msg->header;    my $hdr = $msg->header;
# Line 184  sub _make_a_msg ($@) { Line 219  sub _make_a_msg ($@) {
219        if ($p{base_uri} && /uri/ && length $p{$_}) {        if ($p{base_uri} && /uri/ && length $p{$_}) {
220          require URI::WithBase;          require URI::WithBase;
221          $a->add ($name => URI::WithBase->new ($p{$_}, $p{base_uri})->abs);          $a->add ($name => URI::WithBase->new ($p{$_}, $p{base_uri})->abs);
222          } elsif (/color/) {
223            $p{$_} = '#'.$p{$_} if $p{$_} =~ /^[A-Za-z0-9]{6}$/;
224            $a->add ($name => $p{$_}) if length $p{$_};
225        } else {        } else {
226          $a->add ($name => $p{$_}) if length $p{$_};          $a->add ($name => $p{$_}) if length $p{$_};
227        }        }
# Line 205  sub _make_a_msg ($@) { Line 243  sub _make_a_msg ($@) {
243        $uri->display_name ($p{list_name}) if length $p{list_name};        $uri->display_name ($p{list_name}) if length $p{list_name};
244      }      }
245      if ($p{urn_template}) {      if ($p{urn_template}) {
246        my $urn = $self->Message::Field::Date::_date2str ({        my $urn = $date->stringify (
247          format_template => $p{urn_template},          -format_macros  => $self->{fmt2str},
248          date_time       => $date->unix_time,          -format_template        => $p{urn_template},
249          zone    => $date->zone,          -format_parameters      => \%p,
250          fmt2str => $self->{fmt2str},        );
251        }, \%p);        #my $urn = $self->Message::Field::Date::_date2str ({
252          #  format_template        => $p{urn_template},
253          #  date_time      => $date->unix_time,
254          #  zone   => $date->zone,
255          #  fmt2str        => $self->{fmt2str},
256          #}, \%p);
257        $hdr->add ('x-uri')->value ($urn);        $hdr->add ('x-uri')->value ($urn);
258      }      }
259    ## Additional information    ## Additional information
# Line 240  sub default_parameter ($@) { Line 283  sub default_parameter ($@) {
283    $self;    $self;
284  }  }
285    
286    =head1 $meta = $b->meta_information
287    
288    Gets meta information from resource requesting protocol.
289    Usually returned value is Message::Header object.
290    When resource is getten via HTTP, its content is HTTP response
291    header.
292    
293    =cut
294    
295    sub meta_information ($) {
296      my $self = shift;
297      $self->{meta_info};
298    }
299    
300  =head1 LICENSE  =head1 LICENSE
301    
302  Copyright 2002 wakaba E<lt>w@suika.fam.cxE<gt>.  Copyright 2002 wakaba E<lt>w@suika.fam.cxE<gt>.

Legend:
Removed from v.1.3  
changed lines
  Added in v.1.6

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24