Hi,
I'm currently writing an application that handles RSS and Atom feeds. I have a bit of a conceptual problem though, and that is how I should encapsulate the different parsers, and the objects they emit, so that I can present a unified interface to the rest of my program.
Is there a common solution to this type of problem? Is there an elegant way to wrap similar-but-differing parsers that emit similar objects?
I've been reading a lot on Design Patterns, OO and such stuff lately, but all that is either too abstract, or I can't see how to apply it to this situation.
I'd appreciate tips, pointers to tutorials or CPAN modules that could help me in this.
Here's a more detailed description of the situation I'm in.
There doesn't exist one single parser for the different kinds of feeds, so I'm using XML::RAI to parse RSS feeds, and XML::Atom::Feed to parse Atom feeds. My own "parse" routine is now an if/else tree like this:
sub parse {
my ($xml, $type) = @_;
if($type eq 'RSS') {
return parse_rss($xml);
}
elsif($type eq 'ATOM') {
return parse_atom($xml);
}
else {
# we don't know what this is,
# so we try each in turn
return parse_atom($xml) || parse_rss($xml);
}
}
sub parse_atom {
my $xml = shift;
my $parser = XML::Atom::Feed->new(\$xml
);
# lots of code specific to XML::Atom::Feed
}
sub parse_rss {
my $xml = shift;
my $parser = XML::RAI->parse($xml);
# lots of code specific to XML::RAI
}
Ugly. Clumsy.
Then there's the issue with the objects these parsers emit: each one returns their own specific Feed object; each has a method to get at the items, which are objects specific to each parser. I'd like to be able to represent those in a unified way, so that my application doesn't have to worry about the underlying types.
Right now I have resorted to making my own Feed and FeedItem objects (using Class::Accessor), which I populate in the parse_atom and parse_rss subs. But this feels cumbersome, and it looks ugly to me.
Thanks!
Start Free Trial