1 # $Id: Atom.pm 1958 2006-08-14 05:31:27Z btrott $
3 package XML::Feed::Atom;
6 use base qw( XML::Feed );
8 use XML::Atom::Util qw( iso2dt );
9 use List::Util qw( first );
10 use DateTime::Format::W3CDTF;
13 XML::Atom::Entry->mk_elem_accessors(qw( lat long ), ['http://www.w3.org/2003/01/geo/wgs84_pos#']);
16 my ($feed, %args) = @_;
17 $args{'Version'} ||= '1.0';
19 $feed->{atom} = XML::Atom::Feed->new(%args);
27 $feed->{atom} = XML::Atom::Feed->new(Stream => $str)
28 or return $feed->error(XML::Atom::Feed->errstr);
35 sub title { shift->{atom}->title(@_) }
39 $feed->{atom}->add_link({ rel => 'alternate', href => $_[0],
40 type => 'text/html', });
42 my $l = first { !defined $_->rel || $_->rel eq 'alternate' } $feed->{atom}->link;
43 $l ? $l->href : undef;
51 $feed->{atom}->add_link({type => "application/atom+xml", rel => "self", href => $uri});
58 { !defined $_->rel || $_->rel eq 'self' }
62 return $l ? $l->href : undef;
66 sub description { shift->{atom}->tagline(@_) }
67 sub copyright { shift->{atom}->copyright(@_) }
68 sub language { shift->{atom}->language(@_) }
69 sub generator { shift->{atom}->generator(@_) }
70 sub id { shift->{atom}->id(@_) }
71 sub updated { shift->{atom}->updated(@_) }
72 sub add_link { shift->{atom}->add_link(@_) }
77 my $person = XML::Atom::Person->new(Version => 1.0);
79 $feed->{atom}->author($person);
81 $feed->{atom}->author ? $feed->{atom}->author->name : undef;
88 $feed->{atom}->modified(DateTime::Format::W3CDTF->format_datetime($_[0]));
90 return iso2dt($feed->{atom}->modified) if $feed->{atom}->modified;
91 return iso2dt($feed->{atom}->updated) if $feed->{atom}->updated;
98 for my $entry ($_[0]->{atom}->entries) {
99 push @entries, XML::Feed::Entry::Atom->wrap($entry);
108 $feed->{atom}->add_entry($entry->unwrap);
111 sub as_xml { $_[0]->{atom}->as_xml }
113 package XML::Feed::Entry::Atom;
116 use base qw( XML::Feed::Entry );
117 use XML::Atom::Util qw( iso2dt );
118 use XML::Feed::Content;
119 use XML::Atom::Entry;
120 use List::Util qw( first );
124 $entry->{entry} = XML::Atom::Entry->new(Version => 1.0);
128 sub title { shift->{entry}->title(@_) }
129 sub source { shift->{entry}->source(@_) }
130 sub updated { shift->{entry}->updated(@_) }
135 $entry->{entry}->add_link({ rel => 'alternate', href => $_[0],
136 type => 'text/html', });
138 my $l = first { !defined $_->rel || $_->rel eq 'alternate' } $entry->{entry}->link;
139 $l ? $l->href : undef;
147 if (ref($_[0]) eq 'XML::Feed::Content') {
148 %param = (Body => $_[0]->body);
150 %param = (Body => $_[0]);
152 $entry->{entry}->summary(XML::Atom::Content->new(%param, Version => 1.0));
154 my $s = $entry->{entry}->summary;
155 # map Atom types to MIME types
156 my $type = ($s && ref($s) eq 'XML::Feed::Content') ? $s->type : undef;
158 $type = 'text/html' if $type eq 'xhtml' || $type eq 'html';
159 $type = 'text/plain' if $type eq 'text';
162 if (defined $s && ref($s) eq 'XML::Feed::Content') {
165 XML::Feed::Content->wrap({ type => $type,
171 'text/xhtml' => 'xhtml',
172 'text/html' => 'html',
173 'text/plain' => 'text',
180 if (ref($_[0]) eq 'XML::Feed::Content') {
181 if (defined $_[0]->type && defined $types{$_[0]->type}) {
182 %param = (Body => $_[0]->body, Type => $types{$_[0]->type});
184 %param = (Body => $_[0]->body);
187 %param = (Body => $_[0]);
189 $entry->{entry}->content(XML::Atom::Content->new(%param, Version => 1.0));
191 my $c = $entry->{entry}->content;
193 # map Atom types to MIME types
194 my $type = $c ? $c->type : undef;
196 $type = 'text/html' if $type eq 'xhtml' || $type eq 'html';
197 $type = 'text/plain' if $type eq 'text';
200 XML::Feed::Content->wrap({ type => $type,
201 body => $c ? $c->body : undef });
207 my $ns = XML::Atom::Namespace->new(dc => 'http://purl.org/dc/elements/1.1/');
209 $entry->{entry}->add_category({ term => $_[0] });
211 my $category = $entry->{entry}->category;
212 my @return = $category ? ($category->label || $category->term) : $entry->{entry}->getlist($ns, 'subject');
213 return wantarray? @return : $return[0];
220 my $person = XML::Atom::Person->new(Version => 1.0);
221 $person->name($_[0]);
222 $entry->{entry}->author($person);
224 $entry->{entry}->author ? $entry->{entry}->author->name : undef;
228 sub id { shift->{entry}->id(@_) }
233 $entry->{entry}->issued(DateTime::Format::W3CDTF->format_datetime($_[0])) if $_[0];
235 $entry->{entry}->issued ? iso2dt($entry->{entry}->issued) : undef;
242 $entry->{entry}->modified(DateTime::Format::W3CDTF->format_datetime($_[0])) if $_[0];
244 return iso2dt($entry->{entry}->modified) if $entry->{entry}->modified;
245 return iso2dt($entry->{entry}->updated) if $entry->{entry}->updated;
253 $entry->{entry}->lat($_[0]) if $_[0];
255 $entry->{entry}->lat;
262 $entry->{entry}->long($_[0]) if $_[0];
264 $entry->{entry}->long;