1 # $Id: Atom.pm 1958 2006-08-14 05:31:27Z btrott $
3 package XML::Feed::Format::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#']);
15 use XML::Atom::Content;
18 my ($feed, %args) = @_;
19 $args{'Version'} ||= '1.0';
21 $feed->{atom} = XML::Atom::Feed->new(%args);
29 if (XML::Atom::LIBXML) {
30 my $elem = $item->elem;
32 $elem->setAttributeNS('http://www.w3.org/XML/1998/namespace',
35 return $elem->getAttributeNS('http://www.w3.org/XML/1998/namespace', 'base');
38 $item->elem->setAttribute('xml:base', $_[0]);
40 return $item->elem->getAttribute('xml:base');
45 *XML::Atom::Feed::base = $sub unless XML::Atom::Feed->can('base');
46 *XML::Atom::Entry::base = $sub unless XML::Atom::Entry->can('base');
47 *XML::Atom::Content::base = $sub unless XML::Atom::Content->can('base');
54 $feed->{atom} = XML::Atom::Feed->new(Stream => $str)
55 or return $feed->error(XML::Atom::Feed->errstr);
62 sub title { shift->{atom}->title(@_) }
66 $feed->{atom}->add_link({ rel => 'alternate', href => $_[0],
67 type => 'text/html', });
69 my $l = first { !defined $_->rel || $_->rel eq 'alternate' } $feed->{atom}->link;
70 $l ? $l->href : undef;
78 $feed->{atom}->add_link({type => "application/atom+xml", rel => "self", href => $uri});
85 { !defined $_->rel || $_->rel eq 'self' }
89 return $l ? $l->href : undef;
93 sub description { shift->{atom}->tagline(@_) }
94 sub copyright { shift->{atom}->copyright(@_) }
95 sub language { shift->{atom}->language(@_) }
96 sub generator { shift->{atom}->generator(@_) }
97 sub id { shift->{atom}->id(@_) }
98 sub updated { shift->{atom}->updated(@_) }
99 sub add_link { shift->{atom}->add_link(@_) }
100 sub base { shift->{atom}->base(@_) }
105 my $person = XML::Atom::Person->new(Version => 1.0);
106 $person->name($_[0]);
107 $feed->{atom}->author($person);
109 $feed->{atom}->author ? $feed->{atom}->author->name : undef;
119 $feed->{atom}->modified(DateTime::Format::W3CDTF->format_datetime($_[0]));
121 return iso2dt($feed->{atom}->modified) if $feed->{atom}->modified;
122 return iso2dt($feed->{atom}->updated) if $feed->{atom}->updated;
129 for my $entry ($_[0]->{atom}->entries) {
130 push @entries, XML::Feed::Entry::Format::Atom->wrap($entry);
138 my $entry = shift || return;
139 $entry = $feed->_convert_entry($entry);
140 $feed->{atom}->add_entry($entry->unwrap);
143 sub as_xml { $_[0]->{atom}->as_xml }
145 package XML::Feed::Entry::Format::Atom;
148 use base qw( XML::Feed::Entry );
149 use XML::Atom::Util qw( iso2dt );
150 use XML::Feed::Content;
151 use XML::Atom::Entry;
152 use List::Util qw( first );
156 $entry->{entry} = XML::Atom::Entry->new(Version => 1.0);
160 sub title { shift->{entry}->title(@_) }
161 sub source { shift->{entry}->source(@_) }
162 sub updated { shift->{entry}->updated(@_) }
163 sub base { shift->{entry}->base(@_) }
168 $entry->{entry}->add_link({ rel => 'alternate', href => $_[0],
169 type => 'text/html', });
171 my $l = first { !defined $_->rel || $_->rel eq 'alternate' } $entry->{entry}->link;
172 $l ? $l->href : undef;
180 if (ref($_[0]) eq 'XML::Feed::Content') {
181 %param = (Body => $_[0]->body);
183 %param = (Body => $_[0]);
185 $entry->{entry}->summary(XML::Atom::Content->new(%param, Version => 1.0));
187 my $s = $entry->{entry}->summary;
188 # map Atom types to MIME types
189 my $type = ($s && ref($s) eq 'XML::Feed::Content') ? $s->type : undef;
191 $type = 'text/html' if $type eq 'xhtml' || $type eq 'html';
192 $type = 'text/plain' if $type eq 'text';
195 if (defined $s && ref($s) eq 'XML::Feed::Content') {
198 XML::Feed::Content->wrap({ type => $type,
204 'text/xhtml' => 'xhtml',
205 'text/html' => 'html',
206 'text/plain' => 'text',
214 if (ref($_[0]) eq 'XML::Feed::Content') {
215 if (defined $_[0]->type && defined $types{$_[0]->type}) {
216 %param = (Body => $_[0]->body, Type => $types{$_[0]->type});
218 %param = (Body => $_[0]->body);
220 $base = $_[0]->base if defined $_[0]->base;
222 %param = (Body => $_[0]);
224 $entry->{entry}->content(XML::Atom::Content->new(%param, Version => 1.0));
225 $entry->{entry}->content->base($base) if defined $base;
227 my $c = $entry->{entry}->content;
229 # map Atom types to MIME types
230 my $type = $c ? $c->type : undef;
232 $type = 'text/html' if $type eq 'xhtml' || $type eq 'html';
233 $type = 'text/plain' if $type eq 'text';
236 XML::Feed::Content->wrap({ type => $type,
237 base => $c ? $c->base : undef,
238 body => $c ? $c->body : undef });
244 my $ns = XML::Atom::Namespace->new(dc => 'http://purl.org/dc/elements/1.1/');
246 $entry->{entry}->add_category({ term => $_[0] });
248 my $category = $entry->{entry}->category;
249 my @return = $category ? ($category->label || $category->term) : $entry->{entry}->getlist($ns, 'subject');
250 return wantarray? @return : $return[0];
257 my $person = XML::Atom::Person->new(Version => 1.0);
258 $person->name($_[0]);
259 $entry->{entry}->author($person);
261 $entry->{entry}->author ? $entry->{entry}->author->name : undef;
265 sub id { shift->{entry}->id(@_) }
270 $entry->{entry}->issued(DateTime::Format::W3CDTF->format_datetime($_[0])) if $_[0];
272 $entry->{entry}->issued ? iso2dt($entry->{entry}->issued) : undef;
279 $entry->{entry}->modified(DateTime::Format::W3CDTF->format_datetime($_[0])) if $_[0];
281 return iso2dt($entry->{entry}->modified) if $entry->{entry}->modified;
282 return iso2dt($entry->{entry}->updated) if $entry->{entry}->updated;
290 $entry->{entry}->lat($_[0]) if $_[0];
292 $entry->{entry}->lat;
299 $entry->{entry}->long($_[0]) if $_[0];
301 $entry->{entry}->long;