b0a85549161cb497b8d0e1f9418f9f37fd84d16e
[catagits/XML-Feed.git] / lib / XML / Feed / Format / Atom.pm
1 # $Id$
2
3 package XML::Feed::Format::Atom;
4 use strict;
5
6 use base qw( XML::Feed );
7 use XML::Atom::Feed;
8 use XML::Atom::Util qw( iso2dt );
9 use List::Util qw( first );
10 use DateTime::Format::W3CDTF;
11 use HTML::Entities;
12
13 use XML::Atom::Entry;
14 XML::Atom::Entry->mk_elem_accessors(qw( lat long ), ['http://www.w3.org/2003/01/geo/wgs84_pos#']);
15
16 use XML::Atom::Content;
17
18 sub identify {
19     my $class   = shift;
20     my $xml     = shift;
21     my $tag     = $class->_get_first_tag($xml);
22     return ($tag eq 'feed');
23 }
24
25
26 sub init_empty {
27     my ($feed, %args) = @_;
28     $args{'Version'} ||= '1.0';
29     
30     $feed->{atom} = XML::Atom::Feed->new(%args);
31     $feed;
32 }
33
34 sub init_string {
35     my $feed = shift;
36     my($str) = @_;
37     if ($str) {
38         $feed->{atom} = XML::Atom::Feed->new(Stream => $str)
39             or return $feed->error(XML::Atom::Feed->errstr);
40     }
41     $feed;
42 }
43
44 sub format { 'Atom' }
45
46 sub title { shift->{atom}->title(@_) }
47 sub link {
48     my $feed = shift;
49     if (@_) {
50         $feed->{atom}->add_link({ rel => 'alternate', href => $_[0],
51                                   type => 'text/html', });
52     } else {
53         my $l = first { !defined $_->rel || $_->rel eq 'alternate' } $feed->{atom}->link;
54         $l ? $l->href : undef;
55     }
56 }
57
58 sub self_link {
59     my $feed = shift;
60     if (@_) {
61         my $uri = shift;
62         $feed->{atom}->add_link({type => "application/atom+xml", rel => "self", href => $uri});
63         return $uri;
64     } 
65     else
66     {
67         my $l =
68             first
69             { !defined $_->rel || $_->rel eq 'self' }
70             $feed->{atom}->link;
71             ;
72
73         return $l ? $l->href : undef;
74     }
75 }
76
77 sub description { shift->{atom}->tagline(@_) }
78 sub copyright   { shift->{atom}->copyright(@_) }
79 sub language    { shift->{atom}->language(@_) }
80 sub generator   { shift->{atom}->generator(@_) }
81 sub id          { shift->{atom}->id(@_) }
82 sub updated     { shift->{atom}->updated(@_) }
83 sub add_link    { shift->{atom}->add_link(@_) }
84 sub base        { shift->{atom}->base(@_) }
85
86 sub author {
87     my $feed = shift;
88     if (@_ && $_[0]) {
89         my $person = XML::Atom::Person->new(Version => 1.0);
90         $person->name($_[0]);
91         $feed->{atom}->author($person);
92     } else {
93         $feed->{atom}->author ? $feed->{atom}->author->name : undef;
94     }
95 }
96
97
98
99
100 sub modified {
101     my $feed = shift;
102     if (@_) {
103         $feed->{atom}->modified(DateTime::Format::W3CDTF->format_datetime($_[0]));
104     } else {
105         return iso2dt($feed->{atom}->modified) if $feed->{atom}->modified;
106         return iso2dt($feed->{atom}->updated)  if $feed->{atom}->updated;
107         return undef;
108     }
109 }
110
111 sub entries {
112     my @entries;
113     for my $entry ($_[0]->{atom}->entries) {
114         push @entries, XML::Feed::Entry::Format::Atom->wrap($entry);
115     }
116
117     @entries;
118 }
119
120 sub add_entry {
121     my $feed  = shift;
122     my $entry = shift || return;
123     $entry    = $feed->_convert_entry($entry);
124     $feed->{atom}->add_entry($entry->unwrap);
125 }
126
127 sub as_xml { $_[0]->{atom}->as_xml }
128
129 package XML::Feed::Entry::Format::Atom;
130 use strict;
131
132 use base qw( XML::Feed::Entry );
133 use XML::Atom::Util qw( iso2dt );
134 use XML::Feed::Content;
135 use XML::Atom::Entry;
136 use List::Util qw( first );
137
138 sub init_empty {
139     my $entry = shift;
140     $entry->{entry} = XML::Atom::Entry->new(Version => 1.0);
141     1;
142 }
143
144 sub format { 'Atom' }
145
146 sub title { shift->{entry}->title(@_) }
147 sub source { shift->{entry}->source(@_) }
148 sub updated { shift->{entry}->updated(@_) }
149 sub base { shift->{entry}->base(@_) }
150
151 sub link {
152     my $entry = shift;
153     if (@_) {
154         $entry->{entry}->add_link({ rel => 'alternate', href => $_[0],
155                                     type => 'text/html', });
156     } else {
157         my $l = first { !defined $_->rel || $_->rel eq 'alternate' } $entry->{entry}->link;
158         $l ? $l->href : undef;
159     }
160 }
161
162 sub summary {
163     my $entry = shift;
164     if (@_) {
165                 my %param;
166                 if (ref($_[0]) eq 'XML::Feed::Content') {
167                         %param = (Body => $_[0]->body);
168                 } else {
169                          %param = (Body => $_[0]);
170                 }
171                 $entry->{entry}->summary(XML::Atom::Content->new(%param, Version => 1.0));
172     } else {
173                 my $s = $entry->{entry}->summary;
174         # map Atom types to MIME types
175         my $type = ($s && ref($s) eq 'XML::Feed::Content') ? $s->type : undef;
176         if ($type) {
177             $type = 'text/html'  if $type eq 'xhtml' || $type eq 'html';
178             $type = 'text/plain' if $type eq 'text';
179         }
180                 my $body = $s;  
181                 if (defined $s && ref($s) eq 'XML::Feed::Content') {
182                         $body = $s->body;
183                 }
184         XML::Feed::Content->wrap({ type => $type,
185                                    body => $body });
186     }
187 }
188
189 my %types = (
190         'text/xhtml' => 'xhtml',
191         'text/html'  => 'html',
192         'text/plain' => 'text',
193 );
194
195 sub content {
196     my $entry = shift;
197     if (@_) {
198         my %param;
199         my $base;
200         my $orig_body;
201         if (ref($_[0]) eq 'XML::Feed::Content') {
202             $orig_body = $_[0]->body;
203             if (defined $_[0]->type && defined $types{$_[0]->type}) {
204                 %param = (Body => $orig_body, Type => $types{$_[0]->type});
205
206                 if ($param{Type} eq 'html') {
207                     $param{Body} = HTML::Entities::encode_entities($param{Body});
208                 }
209             } else {
210                 # %param = (Body => $_[0]->body);
211             }
212             $base = $_[0]->base if defined $_[0]->base;
213         } else {
214             $orig_body = $_[0];
215             # %param = (Body => $_[0]);
216         }
217         if (!exists $param{Body}) {
218             $param{Body} = $orig_body;
219         }
220         $entry->{entry}->content(XML::Atom::Content->new(%param, Version => 1.0));
221         $entry->{entry}->content->base($base) if defined $base;
222     } else {
223         my $c = $entry->{entry}->content;
224
225         # map Atom types to MIME types
226         my $type = $c ? $c->type : undef;
227         if ($type) {
228             $type = 'text/html'  if $type eq 'xhtml' || $type eq 'html';
229             $type = 'text/plain' if $type eq 'text';
230         }
231
232         XML::Feed::Content->wrap({ type => $type,
233                                    base => $c ? $c->base : undef, 
234                                    body => $c ? $c->body : undef });
235     }
236 }
237
238 sub category {
239     my $entry = shift;
240     my $ns = XML::Atom::Namespace->new(dc => 'http://purl.org/dc/elements/1.1/');
241     if (@_) {
242         $entry->{entry}->add_category({ term => $_ }) for @_;
243         return 1
244     } else {
245
246
247         my @category = ($entry->{entry}->can('categories')) ? $entry->{entry}->categories : $entry->{entry}->category;
248         my @return = @category
249             ? (map { $_->label || $_->term } @category)
250             : $entry->{entry}->getlist($ns, 'subject');
251
252         return wantarray? @return : $return[0];
253     }
254 }
255
256 sub author {
257     my $entry = shift;
258     if (@_ && $_[0]) {
259         my $person = XML::Atom::Person->new(Version => 1.0);
260         $person->name($_[0]);
261         $entry->{entry}->author($person);
262     } else {
263         $entry->{entry}->author ? $entry->{entry}->author->name : undef;
264     }
265 }
266
267 sub id { shift->{entry}->id(@_) }
268
269 sub issued {
270     my $entry = shift;
271     if (@_) {
272         $entry->{entry}->issued(DateTime::Format::W3CDTF->format_datetime($_[0])) if $_[0];
273     } else {
274         $entry->{entry}->issued ? iso2dt($entry->{entry}->issued) : undef;
275     }
276 }
277
278 sub modified {
279     my $entry = shift;
280     if (@_) {
281         $entry->{entry}->modified(DateTime::Format::W3CDTF->format_datetime($_[0])) if $_[0];
282     } else {
283         return iso2dt($entry->{entry}->modified) if $entry->{entry}->modified;
284         return iso2dt($entry->{entry}->updated)  if $entry->{entry}->updated;
285         return undef;
286     }
287 }
288
289 sub lat {
290     my $entry = shift;
291     if (@_) {
292    $entry->{entry}->lat($_[0]) if $_[0];
293     } else {
294    $entry->{entry}->lat;
295     }
296 }
297
298 sub long {
299     my $entry = shift;
300     if (@_) {
301    $entry->{entry}->long($_[0]) if $_[0];
302     } else {
303    $entry->{entry}->long;
304     }
305 }
306
307
308 sub enclosure {
309     my $entry = shift;
310
311     if (@_) {
312         my $enclosure = shift;
313         my $method    = ($XML::Feed::MULTIPLE_ENCLOSURES)? 'add_link' : 'link';
314         $entry->{entry}->$method({ rel => 'enclosure', href => $enclosure->{url},
315                                 length => $enclosure->{length},
316                                 type   => $enclosure->{type} });
317         return 1;
318     } else {
319         my @links = grep { defined $_->rel && $_->rel eq 'enclosure' } $entry->{entry}->link;
320         return undef unless @links;
321         my @encs = map { XML::Feed::Enclosure->new({ url => $_->href, length => $_->length, type => $_->type }) } @links ;
322         return ($XML::Feed::MULTIPLE_ENCLOSURES)? @encs : $encs[-1];
323     }
324 }
325
326
327 1;