Advertisement

03.07.2005 at 07:20AM PST, ID: 21340588
[x]
Attachment Details

How to efficiently wrap similar objects?

Asked by kandura in Perl Programming Language

Tags: rai, xml

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
 
Keywords: How to efficiently wrap similar objects?
 
Loading Advertisement...
 
[+][-]03.07.2005 at 09:31AM PST, ID: 13478447

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: Perl Programming Language
Tags: rai, xml
Sign Up Now!
Solution Provided By: davorg
Participating Experts: 2
Solution Grade: A
 
 
[+][-]03.07.2005 at 10:24AM PST, ID: 13479045

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 7-day free trial to view this Assisted Solution or ask the Experts your question.

 
[+][-]03.07.2005 at 04:37PM PST, ID: 13482470

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]03.26.2005 at 02:16PM PST, ID: 13637402

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]05.12.2005 at 07:56PM PDT, ID: 13992985

Experts Exchange has a courteous staff of administrators who help members get the most out of the website by means of administrative comments like this one.

Start your 7-day free trial to view this Administrative Comment or ask the Experts your question.

 
[+][-]07.01.2005 at 09:15AM PDT, ID: 14349140

Experts Exchange has a courteous staff of administrators who help members get the most out of the website by means of administrative comments like this one.

Start your 7-day free trial to view this Administrative Comment or ask the Experts your question.

 
[+][-]07.05.2005 at 02:59AM PDT, ID: 14367331

Experts Exchange has a courteous staff of administrators who help members get the most out of the website by means of administrative comments like this one.

Start your 7-day free trial to view this Administrative Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32