Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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
- *
- */
- public final class RSSReader {
- /**
- * Private constructor so this utility class cannot be instantiated.
- */
- private RSSReader() {
- }
- /**
- * 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";
- int location = 0;
- out.println("<html>");
- out.println("<head>");
- out.println("<title>" + channel.child(getChildElement(channel, "title")).child(0) +"</title>");
- out.println("</head>");
- out.println("<body>");
- out.println("<h1>");
- location = getChildElement(channel,"link");
- int location2 = getChildElement(channel,"title");
- out.println("a href =\"" + channel.child(location2).child(0) +"\">" );
- out.print( channel.child(location).child(0)+"</a>");
- out.println("</h1>");
- out.println("<p>");
- location = getChildElement(channel,"description");
- out.println("\"" + channel.child(location).child(0) +"\"" );
- out.println("</p>");
- out.println("<table border=\"1\">");
- out.println("<tbody>");
- out.println("<tr>");
- out.println("<th>Date</th>");
- out.println("<th>Source</th>");
- out.println("<th>News</th>");
- out.println("</tr>");
- // get the tree for each item
- for(int i = 3; i < channel.numberOfChildren(); i++)
- {
- XMLTree items = channel.child(i);
- processItem(items,out);
- }
- outputFooter(out);
- }
- /**
- * 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";
- out.println("</tbody>");
- out.println("</table>");
- out.println("</body>");
- out.println("</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;
- int i = 0;
- int childCount = xml.numberOfChildren();
- if(xml.numberOfChildren() != 0)
- {
- while(i <= childCount)
- {
- if(xml.child(i).label().equals(tag))
- {
- 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 pubDate;
- String titledesc;
- String source;
- String sourceURL = null;
- String titleURL;
- // get the pubdate
- int childIndex = getChildElement(item,"pubDate");
- if(childIndex > -1)
- {
- pubDate = "No date available";
- }
- else
- {
- pubDate = item.child(childIndex).label();
- }
- //get the source
- int childIndex2 = getChildElement(item,"source");
- if(childIndex2 > -1)
- {
- source = "No source available";
- }
- else
- {
- source = item.child(childIndex2).label();
- sourceURL = item.child(childIndex2).attributeValue("url"); // URL FOR SOURCE
- }
- // source has attribute url
- // get the title or description
- int lIndex = getChildElement(item,"link");
- titleURL = item.child(lIndex).label(); // URL FOR TITLE/DESCRIPTION
- if(getChildElement(item,"title") > -1)
- {
- int childIndex3 = getChildElement(item,"description");
- titledesc = item.child(childIndex3).label();
- }
- else if(getChildElement(item,"description") > -1)
- {
- int childIndex3 = getChildElement(item,"title");
- titledesc = item.child(childIndex3).label();
- }
- else
- {
- titledesc = "No title or description available";
- }
- out.print("<td>");
- out.print(pubDate);
- out.print("</td>");
- out.println();
- out.print("<td>");
- out.print(source);
- out.println("a href =\"" + sourceURL +"\">" );
- out.print( source +"</a>");
- out.print("</td>");
- out.println();
- out.print("<td>");
- out.println("a href =\"" + titleURL +"\">" );
- out.print( titledesc +"</a>");
- out.print("</td>");
- }
- /**
- * 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("Please enter the URL of an XML feed");
- String url = in.nextLine();
- XMLTree xml = new XMLTree1(url);
- xml = xml.child(0);
- out.println("Please enter the name of an output file");
- String userOutFile = in.nextLine();
- SimpleWriter outputFile = new SimpleWriter1L(userOutFile);
- outputHeader(xml,outputFile);
- in.close();
- out.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement