1 |
dpavlin |
1 |
# $Id: Atom.pm 1958 2006-08-14 05:31:27Z btrott $ |
2 |
|
|
|
3 |
|
|
package XML::Feed::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 |
|
|
|
12 |
|
|
sub init_empty { |
13 |
|
|
my $feed = shift; |
14 |
|
|
$feed->{atom} = XML::Atom::Feed->new(Version => 1.0); |
15 |
|
|
$feed; |
16 |
|
|
} |
17 |
|
|
|
18 |
|
|
sub init_string { |
19 |
|
|
my $feed = shift; |
20 |
|
|
my($str) = @_; |
21 |
|
|
if ($str) { |
22 |
|
|
$feed->{atom} = XML::Atom::Feed->new(Stream => $str) |
23 |
|
|
or return $feed->error(XML::Atom::Feed->errstr); |
24 |
|
|
} |
25 |
|
|
$feed; |
26 |
|
|
} |
27 |
|
|
|
28 |
|
|
sub format { 'Atom' } |
29 |
|
|
|
30 |
|
|
sub title { shift->{atom}->title(@_) } |
31 |
|
|
sub link { |
32 |
|
|
my $feed = shift; |
33 |
|
|
if (@_) { |
34 |
|
|
$feed->{atom}->add_link({ rel => 'alternate', href => $_[0], |
35 |
|
|
type => 'text/html', }); |
36 |
|
|
} else { |
37 |
|
|
my $l = first { !defined $_->rel || $_->rel eq 'alternate' } $feed->{atom}->link; |
38 |
|
|
$l ? $l->href : undef; |
39 |
|
|
} |
40 |
|
|
} |
41 |
|
|
sub description { shift->{atom}->tagline(@_) } |
42 |
|
|
sub copyright { shift->{atom}->copyright(@_) } |
43 |
|
|
sub language { shift->{atom}->language(@_) } |
44 |
|
|
sub generator { shift->{atom}->generator(@_) } |
45 |
|
|
|
46 |
|
|
sub author { |
47 |
|
|
my $feed = shift; |
48 |
|
|
if (@_ && $_[0]) { |
49 |
|
|
my $person = XML::Atom::Person->new(Version => 1.0); |
50 |
|
|
$person->name($_[0]); |
51 |
|
|
$feed->{atom}->author($person); |
52 |
|
|
} else { |
53 |
|
|
$feed->{atom}->author ? $feed->{atom}->author->name : undef; |
54 |
|
|
} |
55 |
|
|
} |
56 |
|
|
|
57 |
|
|
sub modified { |
58 |
|
|
my $feed = shift; |
59 |
|
|
if (@_) { |
60 |
|
|
$feed->{atom}->modified(DateTime::Format::W3CDTF->format_datetime($_[0])); |
61 |
|
|
} else { |
62 |
|
|
$feed->{atom}->modified ? iso2dt($feed->{atom}->modified) : undef; |
63 |
|
|
} |
64 |
|
|
} |
65 |
|
|
|
66 |
|
|
sub entries { |
67 |
|
|
my @entries; |
68 |
|
|
for my $entry ($_[0]->{atom}->entries) { |
69 |
|
|
push @entries, XML::Feed::Entry::Atom->wrap($entry); |
70 |
|
|
} |
71 |
|
|
|
72 |
|
|
@entries; |
73 |
|
|
} |
74 |
|
|
|
75 |
|
|
sub add_entry { |
76 |
|
|
my $feed = shift; |
77 |
|
|
my($entry) = @_; |
78 |
|
|
$feed->{atom}->add_entry($entry->unwrap); |
79 |
|
|
} |
80 |
|
|
|
81 |
|
|
sub as_xml { $_[0]->{atom}->as_xml } |
82 |
|
|
|
83 |
|
|
package XML::Feed::Entry::Atom; |
84 |
|
|
use strict; |
85 |
|
|
|
86 |
|
|
use base qw( XML::Feed::Entry ); |
87 |
|
|
use XML::Atom::Util qw( iso2dt ); |
88 |
|
|
use XML::Feed::Content; |
89 |
|
|
use XML::Atom::Entry; |
90 |
|
|
use List::Util qw( first ); |
91 |
|
|
|
92 |
|
|
sub init_empty { |
93 |
|
|
my $entry = shift; |
94 |
|
|
$entry->{entry} = XML::Atom::Entry->new(Version => 1.0); |
95 |
|
|
1; |
96 |
|
|
} |
97 |
|
|
|
98 |
|
|
sub title { shift->{entry}->title(@_) } |
99 |
|
|
sub link { |
100 |
|
|
my $entry = shift; |
101 |
|
|
if (@_) { |
102 |
|
|
$entry->{entry}->add_link({ rel => 'alternate', href => $_[0], |
103 |
|
|
type => 'text/html', }); |
104 |
|
|
} else { |
105 |
|
|
my $l = first { !defined $_->rel || $_->rel eq 'alternate' } $entry->{entry}->link; |
106 |
|
|
$l ? $l->href : undef; |
107 |
|
|
} |
108 |
|
|
} |
109 |
|
|
|
110 |
|
|
sub summary { |
111 |
|
|
my $entry = shift; |
112 |
|
|
if (@_) { |
113 |
|
|
$entry->{entry}->summary(ref($_[0]) eq 'XML::Feed::Content' ? |
114 |
|
|
$_[0]->body : $_[0]); |
115 |
|
|
} else { |
116 |
|
|
XML::Feed::Content->wrap({ type => 'html', |
117 |
|
|
body => $entry->{entry}->summary }); |
118 |
|
|
} |
119 |
|
|
} |
120 |
|
|
|
121 |
|
|
sub content { |
122 |
|
|
my $entry = shift; |
123 |
|
|
if (@_) { |
124 |
|
|
my %param; |
125 |
|
|
if (ref($_[0]) eq 'XML::Feed::Content') { |
126 |
|
|
%param = (Body => $_[0]->body); |
127 |
|
|
} else { |
128 |
|
|
%param = (Body => $_[0]); |
129 |
|
|
} |
130 |
|
|
$entry->{entry}->content(XML::Atom::Content->new(%param, Version => 1.0)); |
131 |
|
|
} else { |
132 |
|
|
my $c = $entry->{entry}->content; |
133 |
|
|
|
134 |
|
|
# map Atom types to MIME types |
135 |
|
|
my $type = $c ? $c->type : undef; |
136 |
|
|
if ($type) { |
137 |
|
|
$type = 'text/html' if $type eq 'xhtml' || $type eq 'html'; |
138 |
|
|
$type = 'text/plain' if $type eq 'text'; |
139 |
|
|
} |
140 |
|
|
|
141 |
|
|
XML::Feed::Content->wrap({ type => $type, |
142 |
|
|
body => $c ? $c->body : undef }); |
143 |
|
|
} |
144 |
|
|
} |
145 |
|
|
|
146 |
|
|
sub category { |
147 |
|
|
my $entry = shift; |
148 |
|
|
my $ns = XML::Atom::Namespace->new(dc => 'http://purl.org/dc/elements/1.1/'); |
149 |
|
|
if (@_) { |
150 |
|
|
$entry->{entry}->add_category({ term => $_[0] }); |
151 |
|
|
} else { |
152 |
|
|
my $category = $entry->{entry}->category; |
153 |
|
|
$category ? ($category->label || $category->term) : $entry->{entry}->get($ns, 'subject'); |
154 |
|
|
} |
155 |
|
|
} |
156 |
|
|
|
157 |
|
|
sub author { |
158 |
|
|
my $entry = shift; |
159 |
|
|
if (@_ && $_[0]) { |
160 |
|
|
my $person = XML::Atom::Person->new(Version => 1.0); |
161 |
|
|
$person->name($_[0]); |
162 |
|
|
$entry->{entry}->author($person); |
163 |
|
|
} else { |
164 |
|
|
$entry->{entry}->author ? $entry->{entry}->author->name : undef; |
165 |
|
|
} |
166 |
|
|
} |
167 |
|
|
|
168 |
|
|
sub id { shift->{entry}->id(@_) } |
169 |
|
|
|
170 |
|
|
sub issued { |
171 |
|
|
my $entry = shift; |
172 |
|
|
if (@_) { |
173 |
|
|
$entry->{entry}->issued(DateTime::Format::W3CDTF->format_datetime($_[0])) if $_[0]; |
174 |
|
|
} else { |
175 |
|
|
$entry->{entry}->issued ? iso2dt($entry->{entry}->issued) : undef; |
176 |
|
|
} |
177 |
|
|
} |
178 |
|
|
|
179 |
|
|
sub modified { |
180 |
|
|
my $entry = shift; |
181 |
|
|
if (@_) { |
182 |
|
|
$entry->{entry}->modified(DateTime::Format::W3CDTF->format_datetime($_[0])) if $_[0]; |
183 |
|
|
} else { |
184 |
|
|
$entry->{entry}->modified ? iso2dt($entry->{entry}->modified) : undef; |
185 |
|
|
} |
186 |
|
|
} |
187 |
|
|
|
188 |
|
|
1; |