Writing the Quote Generator

Before you go any further with the multiproject setup, let's take a look at how the core project has implemented the quote generator. This application uses the online quote generator located at http://www.quotationspage.com/; a site which generates an RSS feed of quotes. To parse this feed, you will make use of the Rome framework feed parser (https://rome.dev.java.net/) which has built-in support for parsing RSS and Atom feeds. To get started, add the following code to core/src/main/mdn/qotd/core/QuoteGenerator.java:

package mdn.qotd.core;
  
import java.net.URL;
  
import com.sun.syndication.feed.synd.SyndContent;
import com.sun.syndication.feed.synd.SyndEntry;
import com.sun.syndication.feed.synd.SyndFeed;
import com.sun.syndication.io.SyndFeedInput;
import com.sun.syndication.io.XmlReader;
  
public class QuoteGenerator
{
    private static final String QUOTE_URL = 
        "http://www.quotationspage.com/data/qotd.rss";
    
    public String generate()
    {
        SyndFeed quoteFeed;
        try
        {
            SyndFeedInput input = new SyndFeedInput();
            quoteFeed = input.build(new XmlReader(new URL(QUOTE_URL)));
        }
        catch (Exception e)
        {
            throw new RuntimeException("Failed to get RSS Quote Feed [" 
                + QUOTE_URL + "]", e);
        }
  
        SyndEntry firstQuoteEntry = 
            (SyndEntry) quoteFeed.getEntries().get(0);
            SyndContent firstQuoteContent = 
            (SyndContent) firstQuoteEntry.getContents().get(0);
        
        return firstQuoteContent.getValue();
    }
    
}

If you are test-infected, you are probably wondering why you didn't write a unit test for QuoteGenerator ...

Get Maven: A Developer's Notebook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.