package XML::RSS::Parser::Feed;

use strict;

use XML::Elemental::Document;
@XML::RSS::Parser::Feed::ISA = qw( XML::Elemental::Document );

sub new {
    my $class = shift;
    my $self = $class->SUPER::new(@_);
    $self->{rss_namespace_uri}='';
    $self;
}

# Very loose dtermination of the RSS namespace (if any). Goes to the 
# first child of the root element (most likely the channel) and extracts
# its namespace URI. We don't use the root element because in RSS 1.0 
# the root element is not in the RSS namespace.
sub find_rss_namespace {
    my $doc = shift;
    my $root = $doc->contents->[0];
    foreach my $node (@{$root->contents}) {  
        if (ref($node) eq 'XML::RSS::Parser::Element' ) {
            my ($ns,$name) = $node->name =~m!^(.*?)([^/#]+)$!; 
            $doc->{rss_namespace_uri} = $name ? $ns : '';
            return $doc->{rss_namespace_uri};
        }
    }
    return '';
}

sub rss_namespace_uri { 
	$_[0]->{rss_namespace_uri} = $_[1] if $_[1]; 
	$_[0]->{rss_namespace_uri};
}

sub as_xml { $_[0]->contents->[0]->as_xml }
# sub name { 'rss' }
sub query { $_[0]->contents->[0]->query($_[1]) }
sub channel { my @c = $_[0]->query('/channel'); $c[0];}
sub image { $_[0]->query('image'); }
sub items { $_[0]->query('item'); }
sub item_count { scalar $_[0]->items }

###--- hack to keep Class::XPath happy.
sub qname { '' }
sub attribute_qnames { };

1;

__END__

=begin

=head1 NAME

XML::RSS::Parser::Feed -- the root element of a parsed RSS feed.

=head1 METHODS

=over

=item XML::RSS::Parser::Feed->new

Constructor. Returns a XML::RSS::Parser::Feed object.

=item $feed->rss_namespace_uri

Returns the namespace URI the RSS elements are in, if at all. This
is important since different RSS namespaces are in use. Return a
null string if a namespace cannot be determined or was not defined
at all in the feed.

=item $feed->item_count

Returns an integer representing the number of C<item> elements in
the feed.

=back

=head2 ALIAS METHODS

=over

=item $feed->channel

Returns a reference to the channel element object.

=item $feed->items

Returns an array of reference to item elements object.

=item $feed->image

Returns a reference to the image object if one exists.

=item $feed->as_xml

Alias to the C<channel> element's C<as_xml> method which outputs
the XML of the entire feed. (See the C<as_xml> method in
L<XML::RSS::Parser::Element> for more detail.)

=back

=head1 SEE ALSO

L<XML::RSS::Parser>, L<XML::RSS::Parser::Element>,
L<XML::SimpleObject>

=head1 AUTHOR & COPYRIGHT

Please see the XML::RAI manpage for author, copyright, and
license information.

=cut

=end

