Move to the Formats identifying what content to look at
Simon Wistow [Mon, 3 Nov 2008 23:54:28 +0000 (23:54 +0000)]
Build.PL
lib/XML/Feed.pm
lib/XML/Feed/Format/Atom.pm
lib/XML/Feed/Format/RSS.pm

index 93b70cd..d60c8ff 100644 (file)
--- a/Build.PL
+++ b/Build.PL
@@ -15,6 +15,7 @@ my $build = Module::Build
                           'HTML::TokeParser'           => 0,
                           'List::Util'                 => 0,
                           'LWP::UserAgent'             => 0,
+                          'Module::Pluggable'          => 0,
                           'URI::Fetch'                 => 0,
                           'XML::Atom'                  => 0.23,
                           'XML::LibXML'                => 1.66,
index cd1f1e4..db800ff 100644 (file)
@@ -8,8 +8,15 @@ use Feed::Find;
 use URI::Fetch;
 use LWP::UserAgent;
 use Carp;
-
-our $VERSION = '0.23';
+use Module::Pluggable search_path => "XML::Feed::Format",
+                      require     => 1,
+                      sub_name    => 'formatters';
+
+our $VERSION = '0.3';
+our @formatters;
+BEGIN {
+       @formatters = __PACKAGE__->formatters;
+}
 
 sub new {
     my $class = shift;
@@ -70,8 +77,23 @@ sub parse {
 }
 
 sub identify_format {
-    my $feed = shift;
-    my($xml) = @_;
+    my $feed   = shift;
+    my($xml)   = @_;
+       foreach my $class (@formatters) {
+               my ($name) = ($class =~ m!([^:]+)$!);
+               # TODO ugly
+               my $tmp = $$xml;
+               return $name if eval { $class->identify(\$tmp) };
+               return $feed->error($@) if $@;
+       } 
+       return $feed->error("Cannot detect feed type");
+}
+
+sub _get_first_tag {
+       my $class  = shift;
+       my ($xml)  = @_;
+
+
     ## Auto-detect feed type based on first element. This is prone
     ## to breakage, but then again we don't want to parse the whole
     ## feed ourselves.
@@ -81,15 +103,9 @@ sub identify_format {
         my $first = substr $t, 0, 1;
         $tag = $t, last unless $first eq '?' || $first eq '!';
     }
-    return $feed->error("Cannot find first element") unless $tag;
+       die ("Cannot find first element") unless $tag;
     $tag =~ s/^.*://;
-    if ($tag eq 'rss' || $tag eq 'RDF') {
-        return 'RSS';
-    } elsif ($tag eq 'feed') {
-        return 'Atom';
-    } else {
-        return $feed->error("Cannot detect feed type");
-    }
+       return $tag;
 }
 
 sub find_feeds {
index 4b2f395..9336acb 100644 (file)
@@ -14,6 +14,14 @@ XML::Atom::Entry->mk_elem_accessors(qw( lat long ), ['http://www.w3.org/2003/01/
 
 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';
index 3d43c33..7c04616 100644 (file)
@@ -9,6 +9,14 @@ use DateTime::Format::W3CDTF;
 
 our $PREFERRED_PARSER = "XML::RSS";
 
+
+sub identify {
+    my $class   = shift;
+    my $xml     = shift;
+    my $tag     = $class->_get_first_tag($xml);
+    return ($tag eq 'rss' || $tag eq 'RDF');
+}
+
 sub init_empty {
     my ($feed, %args) = @_;
     $args{'version'} ||= '2.0';