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;
20 my $tag = $class->_get_first_tag($xml);
21 return ($tag eq 'feed');
26 my ($feed, %args) = @_;
27 $args{'Version'} ||= '1.0';
29 $feed->{atom} = XML::Atom::Feed->new(%args);
37 if (XML::Atom::LIBXML) {
38 my $elem = $item->elem;
40 $elem->setAttributeNS('http://www.w3.org/XML/1998/namespace',
43 return $elem->getAttributeNS('http://www.w3.org/XML/1998/namespace', 'base');
46 $item->elem->setAttribute('xml:base', $_[0]);
48 return $item->elem->getAttribute('xml:base');
53 *XML::Atom::Feed::base = $sub unless XML::Atom::Feed->can('base');
54 *XML::Atom::Entry::base = $sub unless XML::Atom::Entry->can('base');
55 *XML::Atom::Content::base = $sub unless XML::Atom::Content->can('base');
62 $feed->{atom} = XML::Atom::Feed->new(Stream => $str)
63 or return $feed->error(XML::Atom::Feed->errstr);
70 sub title { shift->{atom}->title(@_) }
74 $feed->{atom}->add_link({ rel => 'alternate', href => $_[0],
75 type => 'text/html', });
77 my $l = first { !defined $_->rel || $_->rel eq 'alternate' } $feed->{atom}->link;
78 $l ? $l->href : undef;
86 $feed->{atom}->add_link({type => "application/atom+xml", rel => "self", href => $uri});
93 { !defined $_->rel || $_->rel eq 'self' }
97 return $l ? $l->href : undef;
101 sub description { shift->{atom}->tagline(@_) }
102 sub copyright { shift->{atom}->copyright(@_) }
103 sub language { shift->{atom}->language(@_) }
104 sub generator { shift->{atom}->generator(@_) }
105 sub id { shift->{atom}->id(@_) }
106 sub updated { shift->{atom}->updated(@_) }
107 sub add_link { shift->{atom}->add_link(@_) }
108 sub base { shift->{atom}->base(@_) }
113 my $person = XML::Atom::Person->new(Version => 1.0);
114 $person->name($_[0]);
115 $feed->{atom}->author($person);
117 $feed->{atom}->author ? $feed->{atom}->author->name : undef;
127 $feed->{atom}->modified(DateTime::Format::W3CDTF->format_datetime($_[0]));
129 return iso2dt($feed->{atom}->modified) if $feed->{atom}->modified;
130 return iso2dt($feed->{atom}->updated) if $feed->{atom}->updated;
137 for my $entry ($_[0]->{atom}->entries) {
138 push @entries, XML::Feed::Entry::Format::Atom->wrap($entry);
146 my $entry = shift || return;
147 $entry = $feed->_convert_entry($entry);
148 $feed->{atom}->add_entry($entry->unwrap);
151 sub as_xml { $_[0]->{atom}->as_xml }
153 package XML::Feed::Entry::Format::Atom;
156 use base qw( XML::Feed::Entry );
157 use XML::Atom::Util qw( iso2dt );
158 use XML::Feed::Content;
159 use XML::Atom::Entry;
160 use List::Util qw( first );
164 $entry->{entry} = XML::Atom::Entry->new(Version => 1.0);
168 sub title { shift->{entry}->title(@_) }
169 sub source { shift->{entry}->source(@_) }
170 sub updated { shift->{entry}->updated(@_) }
171 sub base { shift->{entry}->base(@_) }
176 $entry->{entry}->add_link({ rel => 'alternate', href => $_[0],
177 type => 'text/html', });
179 my $l = first { !defined $_->rel || $_->rel eq 'alternate' } $entry->{entry}->link;
180 $l ? $l->href : undef;
188 if (ref($_[0]) eq 'XML::Feed::Content') {
189 %param = (Body => $_[0]->body);
191 %param = (Body => $_[0]);
193 $entry->{entry}->summary(XML::Atom::Content->new(%param, Version => 1.0));
195 my $s = $entry->{entry}->summary;
196 # map Atom types to MIME types
197 my $type = ($s && ref($s) eq 'XML::Feed::Content') ? $s->type : undef;
199 $type = 'text/html' if $type eq 'xhtml' || $type eq 'html';
200 $type = 'text/plain' if $type eq 'text';
203 if (defined $s && ref($s) eq 'XML::Feed::Content') {
206 XML::Feed::Content->wrap({ type => $type,
212 'text/xhtml' => 'xhtml',
213 'text/html' => 'html',
214 'text/plain' => 'text',
222 if (ref($_[0]) eq 'XML::Feed::Content') {
223 if (defined $_[0]->type && defined $types{$_[0]->type}) {
224 %param = (Body => $_[0]->body, Type => $types{$_[0]->type});
226 %param = (Body => $_[0]->body);
228 $base = $_[0]->base if defined $_[0]->base;
230 %param = (Body => $_[0]);
232 $entry->{entry}->content(XML::Atom::Content->new(%param, Version => 1.0));
233 $entry->{entry}->content->base($base) if defined $base;
235 my $c = $entry->{entry}->content;
237 # map Atom types to MIME types
238 my $type = $c ? $c->type : undef;
240 $type = 'text/html' if $type eq 'xhtml' || $type eq 'html';
241 $type = 'text/plain' if $type eq 'text';
244 XML::Feed::Content->wrap({ type => $type,
245 base => $c ? $c->base : undef,
246 body => $c ? $c->body : undef });
252 my $ns = XML::Atom::Namespace->new(dc => 'http://purl.org/dc/elements/1.1/');
254 $entry->{entry}->add_category({ term => $_[0] });
256 my $category = $entry->{entry}->category;
257 my @return = $category ? ($category->label || $category->term) : $entry->{entry}->getlist($ns, 'subject');
258 return wantarray? @return : $return[0];
265 my $person = XML::Atom::Person->new(Version => 1.0);
266 $person->name($_[0]);
267 $entry->{entry}->author($person);
269 $entry->{entry}->author ? $entry->{entry}->author->name : undef;
273 sub id { shift->{entry}->id(@_) }
278 $entry->{entry}->issued(DateTime::Format::W3CDTF->format_datetime($_[0])) if $_[0];
280 $entry->{entry}->issued ? iso2dt($entry->{entry}->issued) : undef;
287 $entry->{entry}->modified(DateTime::Format::W3CDTF->format_datetime($_[0])) if $_[0];
289 return iso2dt($entry->{entry}->modified) if $entry->{entry}->modified;
290 return iso2dt($entry->{entry}->updated) if $entry->{entry}->updated;
298 $entry->{entry}->lat($_[0]) if $_[0];
300 $entry->{entry}->lat;
307 $entry->{entry}->long($_[0]) if $_[0];
309 $entry->{entry}->long;