File Coverage

blib/lib/XML/Atom/Ext/Inline.pm
Criterion Covered Total %
statement 9 9 100.0
branch n/a
condition n/a
subroutine 3 3 100.0
pod n/a
total 12 12 100.0


line stmt bran cond sub pod time code
1             package XML::Atom::Ext::Inline;
2              
3 2     2   51689 use warnings;
  2         4  
  2         51  
4 2     2   7 use strict;
  2         1  
  2         36  
5              
6 2     2   5 use base qw(XML::Atom::Base);
  2         4  
  2         837  
7              
8             use XML::Atom::Link;
9             use XML::Atom::Feed;
10             use XML::Atom::Entry;
11             use XML::Atom::Util qw(childlist);
12              
13             use Carp;
14              
15             =head1 NAME
16              
17             XML::Atom::Ext::Inline - In-lining Extensions for Atom
18              
19             =head1 VERSION
20              
21             Version 0.01
22              
23             =cut
24              
25             our $VERSION = '0.01_02';
26              
27             BEGIN {
28             XML::Atom::Link->mk_object_accessor(inline => 'XML::Atom::Ext::Inline');
29             no warnings;
30             *XML::Atom::Link::set = sub {XML::Atom::Base::set(@_)}; # hack to eliminate backwards compatibility hack in XML::Atom::Link
31             }
32              
33             =head1 SYNOPSIS
34              
35             This module implements In-lining extesions for Atom. You can see the RFC draft
36             here: L<http://tools.ietf.org/html/draft-mehta-atom-inline-01>
37              
38             The following code:
39              
40             use XML::Atom;
41             use XML::Atom::Ext::Inline;
42              
43             my $feed = XML::Atom::Feed->new(Version => '1.0');
44             my $parent_feed = XML::Atom::Feed->new(Version => '1.0');
45             $parent_feed->title('foo bar');
46              
47             my $inline = XML::Atom::Ext::Inline->new();
48             $inline->atom($parent_feed);
49            
50             my $link = XML::Atom::Link->new(Version => '1.0');
51             $link->rel('up');
52             $link->inline($inline);
53              
54             $feed->add_link($link);
55            
56             print $feed->as_xml();
57            
58             will produce:
59              
60             <?xml version="1.0" encoding="utf-8"?>
61             <feed xmlns="http://www.w3.org/2005/Atom">
62             <link rel="up">
63             <ae:inline xmlns:ae="http://purl.org/atom/ext/">
64             <feed>
65             <title>foo bar</title>
66             </feed>
67             </ae:inline>
68             </link>
69             </feed>
70              
71             =head1 USAGE
72              
73             =head2 atom($feed | $entry)
74              
75             Returns an I<XML::Atom::Feed> or I<XML::Atom::Entry> object representing the
76             inline element contents or C<undef> if there is no contents.
77              
78             If given an argument, adds the feed I<$feed> which must be an
79             I<XML::Atom::Feed> object or I<$entry> which must be I<XML::Atom::Entry>
80             object, into inline element.
81              
82             =cut
83              
84             sub atom {
85             my $inline = shift;
86             if (@_) {
87             if ($_[0]->isa('XML::Atom::Feed')) {
88             my ($feed) = @_;
89             my $ns_uri = $feed->{ns};
90             my @elem = childlist($inline->elem, $ns_uri, 'entry');
91             $inline->elem->removeChild($_) for @elem;
92             return $inline->set($ns_uri, 'feed', $feed);
93             }
94             elsif ($_[0]->isa('XML::Atom::Feed')) {
95             my ($entry) = @_;
96             my $ns_uri = $entry->{ns}->{uri};
97             my @elem = childlist($inline->elem, $ns_uri, 'feed');
98             $inline->elem->removeChild($_) for @elem;
99             return $inline->set($ns_uri, 'entry', $entry);
100             }
101             else {
102             my $r = ref $_[0];
103             carp "can't embed $r - should be XML::Atom::Feed or XML::Atom::Entry";
104             return;
105             }
106             }
107             else {
108             # looking for feed or entry or the same stuff again with old NS URI
109             return $inline->get_object('http://www.w3.org/2005/Atom', 'feed', 'XML::Atom::Feed')
110             || $inline->get_object('http://www.w3.org/2005/Atom', 'entry', 'XML::Atom::Entry')
111             || $inline->get_object('http://purl.org/atom/ns#', 'feed', 'XML::Atom::Feed')
112             || $inline->get_object('http://purl.org/atom/ns#', 'entry', 'XML::Atom::Entry');
113             }
114             }
115              
116             =head2 element_ns
117              
118             Returns the L<XML::Atom::Namespace> object representing our xmlns:ae="http://purl.org/atom/ext/">.
119              
120             =cut
121              
122             sub element_ns {
123             return XML::Atom::Namespace->new(
124             'ae' => q{http://purl.org/atom/ext/}
125             );
126             }
127              
128             sub element_name {'inline'}
129              
130             =head1 AUTHOR
131              
132             Dmitri Popov, C<< <operator at cv.dp-net.com> >>
133              
134             =head1 BUGS
135              
136             Please report more bugs here: L<http://github.com/pin/xml-atom-ext-inline/issues>
137              
138             =head1 SUPPORT
139              
140             You can find more information and usefull links on project wiki: L<http://wiki.github.com/pin/xml-atom-ext-inline>
141              
142             =head1 COPYRIGHT & LICENSE
143              
144             Copyright 2009 Dmitri Popov, all rights reserved.
145              
146             This program is free software; you can redistribute it and/or modify it
147             under the same terms as Perl itself.
148              
149             =cut
150              
151             1; # End of XML::Atom::Ext::Inline