Question

For this project your task is to update the RSS Reader program you wrote for the...

For this project your task is to update the RSS Reader program you wrote for the previous project so that it reads multiple RSS feeds and generates the same nicely formatted HTML page of links for each feed, plus an HTML index page with links to the individual feed pages.

Your new program should ask the user for the name of an XML file containing a list of URLs for RSS v2.0 feeds (see below for the format of this file) and for the name of an output file in which to generate an HTML page with links to the pages for the individual RSS feeds (see below for an example). It should then read the input XML file into an XMLTree object and then process the list of RSS feeds from the XMLTree. For each RSS feed, the program should generate an HTML page with a table of links to all the news items in the feed (just like in the previous project). The program should also generate an HTML page with an index of links to the individual feed pages.

Format of the Input XML Document

The input XML document is pretty simple. This is the general structure:

1

2

3

4

5

6

<feeds title="Title for index page">

  <feed url="the feed source URL" name="name of feed for index page"

    file="name of HTML file for feed" />

  <feed url="..." name="..." file="..." />

  ...

</feeds>

The top-level tag, <feeds>, has a required attribute, title, whose value is the title to be used in the index page; nested inside the top-level tag are 1 or more <feed> tags with the following required attributes: url, the URL of the RSS feed, name, the name to use for the link to the feed in the index page, and file, the name of the HTML file in which to generate the feed's table of links to news items (with the same format as the output in the previous project).

Here is an example of a valid XML input file.

Format of the HTML Output Index Page

The HTML index page should include the following information:

the <feeds> title as the page title

a header with the page title inside

an unordered list where each item is the name of a feed linked to the feed URL

You can see an example of the index output here.

Method

Create a new Eclipse project by copying your RSSReader project and name the new project RSSAggregator.

Open the src folder of this project and then open (default package). Rename the RSSReader.java file to RSSAggregator.java. Open the RSSAggregator.java file in the editor.

Edit RSSAggregator.java to satisfy the problem requirements stated above. You should factor out the code in your previous project that processed the RSS feed into the following separate static method:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

/**

* Processes one XML RSS (version 2.0) feed from a given URL converting it

* into the corresponding HTML output file.

*

* @param url

*            the URL of the RSS feed

* @param file

*            the name of the HTML output file

* @param out

*            the output stream to report progress or errors

* @updates out.content

* @requires out.is_open

* @ensures <pre>

* [reads RSS feed from url, saves HTML document with table of news items

*   to file, appends to out.content any needed messages]

* </pre>

*/

private static void processFeed(String url, String file, SimpleWriter out) {...}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import components.simplereader.SimpleReader;
import components.simplereader.SimpleReader1L;
import components.simplewriter.SimpleWriter;
import components.simplewriter.SimpleWriter1L;
import components.xmltree.XMLTree;
import components.xmltree.XMLTree1;


public final class RSSAggregator {

    /**
     * Private constructor so this utility class cannot be instantiated.
     */
    private RSSAggregator() {
    }

    /**
     * Outputs the "opening" tags in the generated HTML file. These are the
     * expected elements generated by this method:
     *
     * <html> <head> <title> the channel tag title as the page
     * title </title> </head> <body>
     * <h1>the page title inside a link to the <channel> link
     * <h1>
     * <p>
     * the channel description
     * </p>
     * <table>
     * <tr>
     * <th>Date</th>
     * <th>Source</th>
     * <th>News</th>
     * </tr>
     *
     * @param channel
     *            the channel element XMLTree
     * @param out
     *            the output stream
     * @updates out.content
     * @requires [the root of channel is a <channel> tag] and out.is_open
     * @ensures out.content = #out.content * [the HTML "opening" tags]
     */
    private static void outputHeader(XMLTree channel, SimpleWriter out) {
        assert channel != null : "Violation of: channel is not null";
        assert out != null : "Violation of: out is not null";
        assert channel.isTag() && channel.label().equals("channel") : ""
                + "Violation of: the label root of channel is a <channel> tag";
        assert out.isOpen() : "Violation of: out.is_open";

        // Initial title as Empty Title, initial description as No description
        String title = "Empty Title";
        String description = "No description available";
        String link;

        // Retrieve the index of each tag
        int indexTitle = getChildElement(channel, "title");
        int indexDescription = getChildElement(channel, "description");
        int indexLink = getChildElement(channel, "link");

        // Check if title is empty
        if (channel.child(indexTitle).numberOfChildren() > 0) {
            title = channel.child(indexTitle).child(0).label();
        }

        // Check if description is empty
        if (channel.child(indexDescription).numberOfChildren() > 0) {
            description = channel.child(indexDescription).child(0).label();
        }

        link = channel.child(indexLink).child(0).label();

        // Print opening
        out.print("<html>\n" + "<head>\n" + "\t<title>" + title + "</title>\n"
                + "</head>\n" + "<body>\n" + "\t<h1>\n" + "\t\t<a href=\""
                + link + "\">" + title + "</a>\n" + "\t</h1>\n" + "\t<p>"
                + description + "</p>\n"
                + "<table border=\"1\" style=\"width:100%\">\n" + "\t<tr>\n"
                + "\t\t<th>Date</th>\n" + "\t\t<th>Source</th>\n"
                + "\t\t<th>News</th>\n" + "\t</tr>\n");
    }

    /**
     * Outputs the "closing" tags in the generated HTML file. These are the
     * expected elements generated by this method:
     *
     * </table>
     * </body> </html>
     *
     * @param out
     *            the output stream
     * @updates out.contents
     * @requires out.is_open
     * @ensures out.content = #out.content * [the HTML "closing" tags]
     */
    private static void outputFooter(SimpleWriter out) {
        assert out != null : "Violation of: out is not null";
        assert out.isOpen() : "Violation of: out.is_open";

        // Print footer
        out.println("\t</table>\n" + "</body>\n" + "</html>");
    }

    /**
     * Finds the first occurrence of the given tag among the children of the
     * given {@code XMLTree} and return its index; returns -1 if not found.
     *
     * @param xml
     *            the {@code XMLTree} to search
     * @param tag
     *            the tag to look for
     * @return the index of the first child of type tag of the {@code XMLTree}
     *         or -1 if not found
     * @requires [the label of the root of xml is a tag]
     * @ensures
     *
     *          <pre>
     * getChildElement =
     * [the index of the first child of type tag of the {@code XMLTree} or
     *   -1 if not found]
     *          </pre>
     */
    private static int getChildElement(XMLTree xml, String tag) {
        assert xml != null : "Violation of: xml is not null";
        assert tag != null : "Violation of: tag is not null";
        assert xml.isTag() : "Violation of: the label root of xml is a tag";

        int index = -1;
        // Using while loop to search the index
        int i = 0;
        while (i < xml.numberOfChildren() && index == -1) {
            if (xml.child(i).label().equals(tag)) {
                // Retrieve the index
                index = i;
            }
            i++;
        }

        return index;
    }

    /**
     * Processes one news item and outputs one table row. The row contains three
     * elements: the publication date, the source, and the title (or
     * description) of the item.
     *
     * @param item
     *            the news item
     * @param out
     *            the output stream
     * @updates out.content
     * @requires
     *
     *           <pre>
     * [the label of the root of item is an <item> tag] and out.is_open
     *           </pre>
     *
     * @ensures
     *
     *          <pre>
     * out.content = #out.content *
     *   [an HTML table row with publication date, source, and title of news item]
     *          </pre>
     */
    private static void processItem(XMLTree item, SimpleWriter out) {
        assert item != null : "Violation of: item is not null";
        assert out != null : "Violation of: out is not null";
        assert item.isTag() && item.label().equals("item") : ""
                + "Violation of: the label root of item is an <item> tag";
        assert out.isOpen() : "Violation of: out.is_open";

        String link = "";
        String sourceLink = "";
        String pubDate = "No date available";
        String source = "No source available";
        String title = "No title avaialble";

        // Retrieve the index of each tag
        int indexPubDate = getChildElement(item, "pubDate");
        int indexSource = getChildElement(item, "source");
        int indexTitle = getChildElement(item, "title");
        int indexLink = getChildElement(item, "link");

        // Check if item has pubdate
        if (indexPubDate != -1) {
            // Check if pubdate is empty
            if (item.child(indexPubDate).numberOfChildren() != 0) {
                pubDate = item.child(indexPubDate).child(0).label();
            }
        }

        // Check if item has source
        if (indexSource != -1) {
            source = item.child(indexSource).child(0).label();
            sourceLink = item.child(indexSource).attributeValue("url");
        }

        // Check if item has title

        if (item.child(indexTitle).numberOfChildren() != 0) {
            title = item.child(indexTitle).child(0).label();
        }

        // Check if item has link
        if (indexLink != -1) {
            // Check if link is empty
            if (item.child(indexLink).numberOfChildren() != 0) {
                link = item.child(indexLink).child(0).label();
                // Check if sourceLink is empty
                if (sourceLink.length() != 0) {
                    // Print out table elements
                    out.println("<tr>\n" + "\t<td>" + pubDate + "</td>\n"
                            + "\t<td>" + "\t<a href=\"" + sourceLink + "\">"
                            + source + "</a>\n" + "</td>\n" + "\t<td>\n"
                            + "\t\t<a href=\"" + link + "\">" + title + "</a>\n"
                            + "\t</td>\n" + "</tr>");
                } else {
                    // Print out table elements
                    out.println("<tr>\n" + "\t<td>" + pubDate + "</td>\n"
                            + "\t<td>" + source + "</td>\n" + "\t<td>\n"
                            + "\t\t<a href=\"" + link + "\">" + title + "</a>\n"
                            + "\t</td>\n" + "</tr>");
                }
            } else {
                // Check if sourceLink is empty
                if (sourceLink.length() != 0) {
                    // Print out table elements
                    out.println("<tr>\n" + "\t<td>" + pubDate + "</td>\n"
                            + "\t<td>" + "\t<a href=\"" + sourceLink + "\">"
                            + source + "</a>\n" + "</td>\n" + "\t<td>\n" + title
                            + "\t</td>\n" + "</tr>");
                } else {
                    // Print out table elements
                    out.println("<tr>\n" + "\t<td>" + pubDate + "</td>\n"
                            + "\t<td>" + source + "</td>\n" + "\t<td>\n" + title
                            + "\t</td>\n" + "</tr>");
                }
            }
        }
    }

    /**
     * Processes one XML RSS (version 2.0) feed from a given URL converting it
     * into the corresponding HTML output file.
     *
     * @param url
     *            the URL of the RSS feed
     * @param file
     *            the name of the HTML output file
     * @param out
     *            the output stream to report progress or errors
     * @updates out.content
     * @requires out.is_open
     * @ensures
     *
     *          <pre>
     * [reads RSS feed from url, saves HTML document with table of news items
     *   to file, appends to out.content any needed messages]
     *          </pre>
     */
    private static void processFeed(String url, String file, SimpleWriter out) {
        // Output it into a html file
        SimpleWriter fileOut = new SimpleWriter1L(file);
        XMLTree root = new XMLTree1(url);
        if (root.label().equals("rss")) {
            // Check if version is 2.0
            if (root.hasAttribute("version")) {
                if (root.attributeValue("version").equals("2.0")) {
                    XMLTree channel = root.child(0);
                    // Generate the formatted page
                    outputHeader(channel, fileOut);
                    for (int i = 0; i < channel.numberOfChildren(); i++) {
                        if (channel.child(i).label().equals("item")) {
                            processItem(channel.child(i), fileOut);
                        }
                    }
                    outputFooter(fileOut);
                }
            }
        } else {
            out.println("Wrong file");
        }
    }

    /**
     * Main method.
     *
     * @param args
     *            the command line arguments; unused here
     */
    public static void main(String[] args) {
        SimpleReader in = new SimpleReader1L();
        SimpleWriter out = new SimpleWriter1L();

        // Ask for the index xml
        out.println("Please enter an index xml: ");
        String url0 = in.nextLine();

        // Output it into a html file
        SimpleWriter fileOut = new SimpleWriter1L("index.html");
        XMLTree index = new XMLTree1(url0);

        // Use a for loop to generate html pages of it
        for (int i = 0; i < index.numberOfChildren(); i++) {
            String fileName = index.child(i).attributeValue("file");
            String urlName = index.child(i).attributeValue("url");
            processFeed(urlName, fileName, out);
        }

        // Get the title name
        String title = index.attributeValue("title");
        // Print the index page
        fileOut.print("<html>\n" + "<head>\n" + "\t<title>" + title
                + "</title>\n" + "</head>\n" + "<body>\n" + "<head>\n"
                + "\t<h1>\n" + title + "\t</h1>\n" + "</head>\n" + "\t<ul>\n");
        for (int j = 0; j < index.numberOfChildren(); j++) {
            String name = index.child(j).attributeValue("name");
            String fileName = index.child(j).attributeValue("file");
            fileOut.print("\t<li>\n" + "\t<p>\n" + "\t\t<a href=\"" + fileName
                    + "\">" + name + "</a>\n" + "\t</p>\n" + "\t</li>\n");
        }
        fileOut.print("\t</ul>\n" + "</body>\n" + "</html>");

        in.close();
        out.close();
        fileOut.close();
    }

}

feeds.xml

<feeds title="Top Stories">
   <feed url="http://feeds.abcnews.com/abcnews/topstories" name="ABC News: Top Stories" file="abc.html"/>
   <feed url="http://www.cbsnews.com/feeds/rss/main.rss" name="Breaking News: CBS News" file="cbs.html"/>
   <feed url="http://rss.cnn.com/rss/cnn_topstories.rss" name="CNN.com - Top Stories" file="cnn.html"/>
   <feed url="http://feeds.foxnews.com/foxnews/latest" name="FOXNews.com" file="fox.html"/>
   <feed url="http://rss.msnbc.msn.com/id/3032091/device/rss/rss.xml" name="NBCNews.com: Top NBCNews headlines" file="nbc.html"/>
   <feed url="http://news.nationalgeographic.com/index.rss" name="National Geographic" file="natgeo.html"/>
</feeds>

Add a comment
Know the answer?
Add Answer to:
For this project your task is to update the RSS Reader program you wrote for the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In this activity, you will complete the RSS Client that connects to a UNL RSS feed,...

    In this activity, you will complete the RSS Client that connects to a UNL RSS feed, processes the XML data and outputs the results to the standard output. Most of the client has been completed for you. You just Lab Handout: Structures 4 need to complete the design and implementation of a C structure that models the essential parts of an RSS item. Your structure will need to support an RSS item’s title, link, description, and publication date. As a...

  • Unit 1 Programming Assignment Follow the directions for each of the following questions. Answer the question...

    Unit 1 Programming Assignment Follow the directions for each of the following questions. Answer the question or provide the code in a space below the question. 1. Write the complete script tag set for a script whose line statement is document.write(“Hello, world.”); 2. Build a complete HTML document and include the answer to the previous question such that the page executes the script as the page loads. Open the document in your browser to test the results. 3. Add a...

  • C++ project we need to create a class for Book and Warehouse using Book.h and Warehouse.h header ...

    C++ project we need to create a class for Book and Warehouse using Book.h and Warehouse.h header files given. Then make a main program using Book and Warehouse to read data from book.dat and have functions to list and find book by isbn Objectives: Class Association and operator overloading This project is a continuation from Project 1. The program should accept the same input data file and support the same list and find operations. You will change the implementation of...

  • Python 12.10 LAB: Sorting TV Shows (dictionaries and lists) Write a program that first reads in...

    Python 12.10 LAB: Sorting TV Shows (dictionaries and lists) Write a program that first reads in the name of an input file and then reads the input file using the file.readlines() method. The input file contains an unsorted list of number of seasons followed by the corresponding TV show. Your program should put the contents of the input file into a dictionary where the number of seasons are the keys, and a list of TV shows are the values (since...

  • For milestone #1, we will start the CARIT site with three static HTML pages and a...

    For milestone #1, we will start the CARIT site with three static HTML pages and a CSS file. Create a dedicated folder for this project. This folder should contain all related files in this project. The future milestones are cumulative and built directly on top of your prior work. Function/content requirements: A home page named “index.html”, which include these contents at least: Description of the center. You may reference the example sites. Latest news: use list tags; make up some...

  • This week, you will start a course project. For this project, you will design and develop...

    This week, you will start a course project. For this project, you will design and develop a small website for a travel company. You will develop this website across the span of the course, building new project components each week, until you have a live, hosted website at the end of the course. This project is designed to replicate real-life situations where the clients provide only a few of their requirements and expect a prototype to be developed. Scenario Express...

  • Design a program using Python and using from flask Import flask that generates a lottery number...

    Design a program using Python and using from flask Import flask that generates a lottery number but in a website.. The program should have an Integer array with 9 elements. Write a loop that steps through the array, randomly generating a number in the range of 0 through 42 for each element. (Use the random function) Then write another loop that displays the contents of the array. Each number should be displayed as a list, the numbers should be generated...

  • Set-Up · Create a new project in your Eclipse workspace named: Lab13 · In the src...

    Set-Up · Create a new project in your Eclipse workspace named: Lab13 · In the src folder, create a package named: edu.ilstu · Import the following files from T:\it168\Labs\lab13. Note that the .txt file must be in the top level of your project, not inside your src folder. o Student.java o StudentList.java o students.txt Carefully examine the Student.java and StudentList.java files. You are going to complete the StudentList class (updating all necessary Javadoc comments), following the instruction in the code....

  • For this assignment, you will use your knowledge of arrays and ArrayLists to write a Java...

    For this assignment, you will use your knowledge of arrays and ArrayLists to write a Java program that will input a file of sentences and output a report showing the tokens and shingles (defined below) for each sentence. Templates are provided below for implementing the program as two separate files: a test driver class containing the main() method, and a sentence utilities class that computes the tokens and shingles, and reports their values. The test driver template already implements accepting...

  • CIS 22A C++ Project Exam Statistics Here is what your program will do: first it welcomes...

    CIS 22A C++ Project Exam Statistics Here is what your program will do: first it welcomes the user and displays the purpose of the program. It then prompts the user to enter the name of an input file (such as scores.txt). Assume the file contains the scores of the final exams; each score is preceded by a 5 characters student id. Create the input file: copy and paste the following data into a new text file named scores.txt DH232 89...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT