Added new (currently failing) test for the double-encoding bug.
[catagits/XML-Feed.git] / lib / XML / Feed / Format / Atom.pm
index 4b2f395..b0a8554 100644 (file)
@@ -1,4 +1,4 @@
-# $Id: Atom.pm 1958 2006-08-14 05:31:27Z btrott $
+# $Id$
 
 package XML::Feed::Format::Atom;
 use strict;
@@ -8,12 +8,21 @@ use XML::Atom::Feed;
 use XML::Atom::Util qw( iso2dt );
 use List::Util qw( first );
 use DateTime::Format::W3CDTF;
+use HTML::Entities;
 
 use XML::Atom::Entry;
 XML::Atom::Entry->mk_elem_accessors(qw( lat long ), ['http://www.w3.org/2003/01/geo/wgs84_pos#']);
 
 use XML::Atom::Content;
 
+sub identify {
+    my $class   = shift;
+    my $xml     = shift;
+    my $tag     = $class->_get_first_tag($xml);
+    return ($tag eq 'feed');
+}
+
+
 sub init_empty {
     my ($feed, %args) = @_;
     $args{'Version'} ||= '1.0';
@@ -22,31 +31,6 @@ sub init_empty {
     $feed;
 }
 
-# monkey patch
-{
-    my $sub =  sub {
-        my $item = shift;
-        if (XML::Atom::LIBXML) {
-            my $elem = $item->elem;
-            if (@_) {
-                $elem->setAttributeNS('http://www.w3.org/XML/1998/namespace',
-                    'base', $_[0]);
-            }
-            return $elem->getAttributeNS('http://www.w3.org/XML/1998/namespace', 'base');
-        } else {
-            if (@_) {
-                $item->elem->setAttribute('xml:base', $_[0]);
-            }
-            return $item->elem->getAttribute('xml:base');
-        }
-
-    };
-    no strict 'refs';
-    *XML::Atom::Feed::base    = $sub unless XML::Atom::Feed->can('base');
-    *XML::Atom::Entry::base   = $sub unless XML::Atom::Entry->can('base');
-    *XML::Atom::Content::base = $sub unless XML::Atom::Content->can('base');
-}
-
 sub init_string {
     my $feed = shift;
     my($str) = @_;
@@ -157,6 +141,8 @@ sub init_empty {
     1;
 }
 
+sub format { 'Atom' }
+
 sub title { shift->{entry}->title(@_) }
 sub source { shift->{entry}->source(@_) }
 sub updated { shift->{entry}->updated(@_) }
@@ -211,15 +197,25 @@ sub content {
     if (@_) {
         my %param;
         my $base;
+        my $orig_body;
         if (ref($_[0]) eq 'XML::Feed::Content') {
-                       if (defined $_[0]->type && defined $types{$_[0]->type}) {
-                   %param = (Body => $_[0]->body, Type => $types{$_[0]->type});
-                       } else {
-                   %param = (Body => $_[0]->body);
-                       }
+            $orig_body = $_[0]->body;
+            if (defined $_[0]->type && defined $types{$_[0]->type}) {
+                %param = (Body => $orig_body, Type => $types{$_[0]->type});
+
+                if ($param{Type} eq 'html') {
+                    $param{Body} = HTML::Entities::encode_entities($param{Body});
+                }
+            } else {
+                # %param = (Body => $_[0]->body);
+            }
             $base = $_[0]->base if defined $_[0]->base;
         } else {
-            %param = (Body => $_[0]);
+            $orig_body = $_[0];
+            # %param = (Body => $_[0]);
+        }
+        if (!exists $param{Body}) {
+            $param{Body} = $orig_body;
         }
         $entry->{entry}->content(XML::Atom::Content->new(%param, Version => 1.0));
         $entry->{entry}->content->base($base) if defined $base;
@@ -243,10 +239,16 @@ sub category {
     my $entry = shift;
     my $ns = XML::Atom::Namespace->new(dc => 'http://purl.org/dc/elements/1.1/');
     if (@_) {
-        $entry->{entry}->add_category({ term => $_[0] });
+        $entry->{entry}->add_category({ term => $_ }) for @_;
+        return 1
     } else {
-        my $category = $entry->{entry}->category;
-        my @return = $category ? ($category->label || $category->term) : $entry->{entry}->getlist($ns, 'subject');
+
+
+        my @category = ($entry->{entry}->can('categories')) ? $entry->{entry}->categories : $entry->{entry}->category;
+        my @return = @category
+            ? (map { $_->label || $_->term } @category)
+            : $entry->{entry}->getlist($ns, 'subject');
+
         return wantarray? @return : $return[0];
     }
 }
@@ -302,4 +304,24 @@ sub long {
     }
 }
 
+
+sub enclosure {
+    my $entry = shift;
+
+    if (@_) {
+        my $enclosure = shift;
+        my $method    = ($XML::Feed::MULTIPLE_ENCLOSURES)? 'add_link' : 'link';
+        $entry->{entry}->$method({ rel => 'enclosure', href => $enclosure->{url},
+                                length => $enclosure->{length},
+                                type   => $enclosure->{type} });
+        return 1;
+    } else {
+        my @links = grep { defined $_->rel && $_->rel eq 'enclosure' } $entry->{entry}->link;
+        return undef unless @links;
+        my @encs = map { XML::Feed::Enclosure->new({ url => $_->href, length => $_->length, type => $_->type }) } @links ;
+        return ($XML::Feed::MULTIPLE_ENCLOSURES)? @encs : $encs[-1];
+    }
+}
+
+
 1;