import components.simplereader.SimpleReader; import components.simplereader.SimpleReader1L; import components.simplewriter.SimpleWriter; import components.simplewriter.SimpleWriter1L; import components.xmltree.XMLTree; import components.xmltree.XMLTree1; /** * Program to convert an XML RSS (version 2.0) feed from a given URL into the * corresponding HTML output file. * * @author Put your name here http://xml.weather.yahoo.com/forecastrss/43210.xml * */ public final class RSSAggregator { /** * Private constructor so this utility class cannot be instantiated. */ private RSSAggregator() { } /** * Outputs the opening tags in the generated HTML file. * * @param channel * the channel element XMLTree * @param out * the output stream * @updates {@code out.content} * @requires
     * {@code [the root of channel is a  tag] and out.is_open}
     * 
* @ensures
     * {@code 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 tag"; assert out.isOpen() : "Violation of: out.is_open"; out.println(""); out.println(""); out.println(""); int index = getChildElement(channel, "title"); String title = ""; if (index >= 0) { title = channel.child(index).child(0).label(); out.println(title); } out.println(""); out.println(""); out.println(""); index = getChildElement(channel, "link"); if (index >= 0) { String link = channel.child(index).child(0).label(); out.print("

"); out.print(title); out.println("

"); } // TODO: Output Description // TODO: Output table header for (int i = 0; i < channel.numberOfChildren(); i++) { if (channel.child(i).isTag()) { if (channel.child(i).label() == "item") { processItem(channel.child(i), out); } } } outputFooter(out); } /** * Outputs the closing tags in the generated HTML file. * * @param out * the output stream * @updates {@code out.contents} * @requires
     * {@code out.is_open}
     * 
* @ensures
     * {@code 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"; out.println(" "); out.println(""); out.println(""); /* * TODO: fill in body */ } /** * 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
     * {@code [the label of the root of xml is a tag]}
     * 
* @ensures
     * {@code getChildElement =
     *  [the index of the first child of type tag of the {@code XMLTree} or
     *   -1 if not found]}
     * 
*/ 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"; /* * TODO: fill in body */ for (int i = 0; i < xml.numberOfChildren(); i++) { if (xml.child(i).isTag()) { if (xml.child(i).label() == tag) { return i; } } } return -1; } /** * 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 {@code out.content} * @requires
     * {@code [the label of the root of item is an  tag] and out.is_open}
     * 
* @ensures
     * {@code out.content = #out.content *
     *   [an HTML table row with publication date, source, and title of news item]}
     * 
*/ 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 tag"; assert out.isOpen() : "Violation of: out.is_open"; out.println(""); out.println(""); out.print(""); out.print(""); out.print(""); out.print(""); out.println(""); dateCell(item, out); sourceCell(item, out); titleCell(item, out); out.println(""); } private static void dateCell(XMLTree item, SimpleWriter out) { int index = getChildElement(item, "pubDate"); String date = ""; out.print(""); } private static void titleCell(XMLTree item, SimpleWriter out) { out.print(""); } private static void sourceCell(XMLTree item, SimpleWriter out) { out.print(""); } /* * TODO: TODO: Output title with HTML table stuff TODO: Output description * with HTML table stuff */ /** * Main method. * * @param args * the command line arguments; unused here */ public static void main(String[] args) { SimpleReader in = new SimpleReader1L(); SimpleWriter out = new SimpleWriter1L(); out.println("enter url: "); String url = in.nextLine(); XMLTree tree = new XMLTree1(url); out.println("output file"); String file = in.nextLine(); SimpleWriter outFile = new SimpleWriter1L(file + ".html"); int index = getChildElement(tree, "channel"); outputHeader(tree.child(index), outFile); in.close(); out.close(); } }
"); out.print("date"); out.println(""); out.print("source"); out.println(""); out.print("title"); out.println("
"); if (index >= 0) { date = item.child(index).child(0).label(); out.print(date); } else { out.print("No Source Available"); } out.println(""); int index = getChildElement(item, "title"); String title = ""; String link = ""; int index3 = getChildElement(item, "link"); if (index3 >= 0) { link = item.child(index3).child(0).label(); out.print(""); } if (index >= 0) { title = item.child(index).child(0).label(); out.print(title); } else { int index2 = getChildElement(item, "description"); String description = ""; if (index2 >= 0) { description = item.child(index2).child(0).label(); out.print(description); } else { out.print("No Source Available"); } } if (index3 >= 0) { out.print(""); } out.println(""); int index = getChildElement(item, "source"); String source = ""; String url = ""; if (index >= 0) { source = item.child(index).child(0).label(); boolean urlexists = item.child(index).hasAttribute("url"); if (urlexists) { url = item.child(index).attributeValue("url"); out.print(""); } out.print(source); if (urlexists) { out.print(""); } } else { out.print("No Source Available"); } out.println("