Advertisement
Guest User

Untitled

a guest
Oct 2nd, 2014
536
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.90 KB | None | 0 0
  1. import components.simplereader.SimpleReader;
  2. import components.simplereader.SimpleReader1L;
  3. import components.simplewriter.SimpleWriter;
  4. import components.simplewriter.SimpleWriter1L;
  5. import components.xmltree.XMLTree;
  6. import components.xmltree.XMLTree1;
  7.  
  8. /**
  9. * Program to convert an XML RSS (version 2.0) feed from a given URL into the
  10. * corresponding HTML output file.
  11. *
  12. * @author Put your name here
  13. *
  14. */
  15. public final class RSSReader {
  16.  
  17. /**
  18. * Private constructor so this utility class cannot be instantiated.
  19. */
  20. private RSSReader() {
  21. }
  22.  
  23. /**
  24. * Outputs the "opening" tags in the generated HTML file. These are the
  25. * expected elements generated by this method:
  26. *
  27. * <html>
  28. * <head>
  29. * <title>the channel tag title as the page title</title>
  30. * </head>
  31. * <body>
  32. * <h1>the page title inside a link to the <channel> link<h1>
  33. * <p>the channel description</p>
  34. * <table>
  35. * <tr>
  36. * <th>Date</th>
  37. * <th>Source</th>
  38. * <th>News</th>
  39. * </tr>
  40. *
  41. * @param channel
  42. * the channel element XMLTree
  43. * @param out
  44. * the output stream
  45. * @updates out.content
  46. * @requires [the root of channel is a <channel> tag] and out.is_open
  47. * @ensures out.content = #out.content * [the HTML "opening" tags]
  48. */
  49. private static void outputHeader(XMLTree channel, SimpleWriter out) {
  50. assert channel != null : "Violation of: channel is not null";
  51. assert out != null : "Violation of: out is not null";
  52. assert channel.isTag() && channel.label().equals("channel") : ""
  53. + "Violation of: the label root of channel is a <channel> tag";
  54. assert out.isOpen() : "Violation of: out.is_open";
  55. int location = 0;
  56. out.println("<html>");
  57. out.println("<head>");
  58. out.println("<title>" + channel.child(getChildElement(channel, "title")).child(0) +"</title>");
  59. out.println("</head>");
  60. out.println("<body>");
  61. out.println("<h1>");
  62. location = getChildElement(channel,"link");
  63. int location2 = getChildElement(channel,"title");
  64. out.println("a href =\"" + channel.child(location2).child(0) +"\">" );
  65. out.print( channel.child(location).child(0)+"</a>");
  66. out.println("</h1>");
  67.  
  68. out.println("<p>");
  69. location = getChildElement(channel,"description");
  70. out.println("\"" + channel.child(location).child(0) +"\"" );
  71. out.println("</p>");
  72. out.println("<table border=\"1\">");
  73. out.println("<tbody>");
  74. out.println("<tr>");
  75. out.println("<th>Date</th>");
  76. out.println("<th>Source</th>");
  77. out.println("<th>News</th>");
  78. out.println("</tr>");
  79. // get the tree for each item
  80. for(int i = 3; i < channel.numberOfChildren(); i++)
  81. {
  82. XMLTree items = channel.child(i);
  83. processItem(items,out);
  84. }
  85.  
  86. outputFooter(out);
  87.  
  88.  
  89. }
  90.  
  91. /**
  92. * Outputs the "closing" tags in the generated HTML file. These are the
  93. * expected elements generated by this method:
  94. *
  95. * </table>
  96. * </body>
  97. * </html>
  98. *
  99. * @param out
  100. * the output stream
  101. * @updates out.contents
  102. * @requires out.is_open
  103. * @ensures out.content = #out.content * [the HTML "closing" tags]
  104. */
  105. private static void outputFooter(SimpleWriter out) {
  106. assert out != null : "Violation of: out is not null";
  107. assert out.isOpen() : "Violation of: out.is_open";
  108.  
  109. out.println("</tbody>");
  110. out.println("</table>");
  111. out.println("</body>");
  112. out.println("</html>");
  113. }
  114.  
  115. /**
  116. * Finds the first occurrence of the given tag among the children of the
  117. * given {@code XMLTree} and return its index; returns -1 if not found.
  118. *
  119. * @param xml
  120. * the {@code XMLTree} to search
  121. * @param tag
  122. * the tag to look for
  123. * @return the index of the first child of type tag of the {@code XMLTree}
  124. * or -1 if not found
  125. * @requires [the label of the root of xml is a tag]
  126. * @ensures <pre>
  127. * getChildElement =
  128. * [the index of the first child of type tag of the {@code XMLTree} or
  129. * -1 if not found]
  130. * </pre>
  131. */
  132. private static int getChildElement(XMLTree xml, String tag) {
  133. assert xml != null : "Violation of: xml is not null";
  134. assert tag != null : "Violation of: tag is not null";
  135. assert xml.isTag() : "Violation of: the label root of xml is a tag";
  136. int index = -1;
  137. int i = 0;
  138. int childCount = xml.numberOfChildren();
  139. if(xml.numberOfChildren() != 0)
  140. {
  141. while(i <= childCount)
  142. {
  143. if(xml.child(i).label().equals(tag))
  144. {
  145. index = i;
  146. }
  147. i++;
  148.  
  149. }
  150. }
  151.  
  152. return index;
  153. }
  154.  
  155. /**
  156. * Processes one news item and outputs one table row. The row contains three
  157. * elements: the publication date, the source, and the title (or
  158. * description) of the item.
  159. *
  160. * @param item
  161. * the news item
  162. * @param out
  163. * the output stream
  164. * @updates out.content
  165. * @requires <pre>
  166. * [the label of the root of item is an <item> tag] and out.is_open
  167. * </pre>
  168. * @ensures <pre>
  169. * out.content = #out.content *
  170. * [an HTML table row with publication date, source, and title of news item]
  171. * </pre>
  172. */
  173. private static void processItem(XMLTree item, SimpleWriter out) {
  174. assert item != null : "Violation of: item is not null";
  175. assert out != null : "Violation of: out is not null";
  176. assert item.isTag() && item.label().equals("item") : ""
  177. + "Violation of: the label root of item is an <item> tag";
  178. assert out.isOpen() : "Violation of: out.is_open";
  179. String pubDate;
  180. String titledesc;
  181. String source;
  182. String sourceURL = null;
  183. String titleURL;
  184.  
  185. // get the pubdate
  186. int childIndex = getChildElement(item,"pubDate");
  187. if(childIndex > -1)
  188. {
  189. pubDate = "No date available";
  190. }
  191. else
  192. {
  193. pubDate = item.child(childIndex).label();
  194. }
  195.  
  196. //get the source
  197. int childIndex2 = getChildElement(item,"source");
  198. if(childIndex2 > -1)
  199. {
  200. source = "No source available";
  201. }
  202. else
  203. {
  204. source = item.child(childIndex2).label();
  205. sourceURL = item.child(childIndex2).attributeValue("url"); // URL FOR SOURCE
  206.  
  207. }
  208. // source has attribute url
  209.  
  210.  
  211. // get the title or description
  212. int lIndex = getChildElement(item,"link");
  213. titleURL = item.child(lIndex).label(); // URL FOR TITLE/DESCRIPTION
  214. if(getChildElement(item,"title") > -1)
  215. {
  216. int childIndex3 = getChildElement(item,"description");
  217. titledesc = item.child(childIndex3).label();
  218. }
  219. else if(getChildElement(item,"description") > -1)
  220. {
  221. int childIndex3 = getChildElement(item,"title");
  222. titledesc = item.child(childIndex3).label();
  223. }
  224. else
  225. {
  226. titledesc = "No title or description available";
  227. }
  228.  
  229. out.print("<td>");
  230. out.print(pubDate);
  231. out.print("</td>");
  232. out.println();
  233. out.print("<td>");
  234. out.print(source);
  235. out.println("a href =\"" + sourceURL +"\">" );
  236. out.print( source +"</a>");
  237. out.print("</td>");
  238. out.println();
  239. out.print("<td>");
  240. out.println("a href =\"" + titleURL +"\">" );
  241. out.print( titledesc +"</a>");
  242. out.print("</td>");
  243.  
  244.  
  245. }
  246.  
  247. /**
  248. * Main method.
  249. *
  250. * @param args
  251. * the command line arguments; unused here
  252. */
  253. public static void main(String[] args) {
  254. SimpleReader in = new SimpleReader1L();
  255. SimpleWriter out = new SimpleWriter1L();
  256.  
  257. out.println("Please enter the URL of an XML feed");
  258. String url = in.nextLine();
  259. XMLTree xml = new XMLTree1(url);
  260. xml = xml.child(0);
  261.  
  262. out.println("Please enter the name of an output file");
  263. String userOutFile = in.nextLine();
  264. SimpleWriter outputFile = new SimpleWriter1L(userOutFile);
  265.  
  266.  
  267. outputHeader(xml,outputFile);
  268.  
  269. in.close();
  270. out.close();
  271. }
  272.  
  273. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement