/
RSSAggregator.java
386 lines (342 loc) · 13.2 KB
/
RSSAggregator.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
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 of RSS (version 2.0) feeds from a given file into
* the corresponding HTML output files.
*
* @author Ronit Kumar
*
*/
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 <pre>
* {@code [the root of channel is a <channel> tag] and out.is_open}
* </pre>
* @ensures <pre>
* {@code out.content = #out.content * [the HTML opening tags]}
* </pre>
*/
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";
// Prints to the file the opening tags
out.println("<?xml version='1.0' encoding='ISO-8859-1' ?>");
out.println("<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Strict//EN'"
+ " 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'>");
out.println("<html xmlns='http://www.w3.org/1999/xhtml'>");
out.println("<head>");
out.println("<meta http-equiv='Content-Type'"
+ " content='text/html; charset=ISO-8859-1' />");
// Prints the <channel>title as the page title
XMLTree title = channel.child(getChildElement(channel, "title"));
String header = title.child(0).label();
out.println("<title>" + header + "</title>");
// Prints out more opening tags
out.println("</head>");
out.println("<body>");
// Gets the content in the link tag
XMLTree link = channel.child(getChildElement(channel, "link"));
String headerLink = link.child(0).label();
out.println("<h1><a href = " + headerLink + ">" + header + "</a></h1>");
}
/**
* 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 <pre>
* {@code [the root of channel is a <channel> tag] and out.is_open}
* </pre>
* @ensures <pre>
* {@code out.content = #out.content * [the HTML opening tags]}
* </pre>
*/
private static void outputIndex(XMLTree channel, SimpleWriter out) {
assert channel != null : "Violation of: channel is not null";
assert out != null : "Violation of: out is not null";
assert out.isOpen() : "Violation of: out.is_open";
// Prints to the file the opening tags
out.println("<?xml version='1.0' encoding='ISO-8859-1' ?>");
out.println("<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Strict//EN'"
+ " 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'>");
out.println("<html xmlns='http://www.w3.org/1999/xhtml'>");
out.println("<head>");
out.println("<meta http-equiv='Content-Type'"
+ " content='text/html; charset=ISO-8859-1' />");
// Prints the <channel>title as the page title
String header = channel.attributeValue("title");
out.println("<title>" + header + "</title>");
// Prints out more opening tags
out.println("</head>");
out.println("<body>");
out.println("<h2>" + header + "</h2>");
out.println("<ul>");
int k = 0;
while (k < channel.numberOfChildren()) {
String file = channel.child(k).attributeValue("file");
String name = channel.child(k).attributeValue("name");
out.println("<li><a href=\"" + file + "\">" + name + "</a></li>");
k++;
}
out.println("</ul>");
out.println("</body>");
out.println("</html>");
}
/**
* Outputs the closing tags in the generated HTML file.
*
* @param out
* the output stream
* @updates {@code out.contents}
* @requires <pre>
* {@code out.is_open}
* </pre>
* @ensures <pre>
* {@code out.content = #out.content * [the HTML closing tags]}
* </pre>
*/
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("</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 <pre>
* {@code [the label of the root of xml is a tag]}
* </pre>
* @ensures <pre>
* {@code 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 k = 0, j = 0;
String name = xml.child(k).label();
// Finds the child element that the tag is
while (!name.equals(tag) && k < xml.numberOfChildren()) {
name = xml.child(k).label();
if (name.equals(tag)) {
j = k;
}
k++;
}
// Returns the appropriate child element
if (!name.equals(tag)) {
return -1;
} else {
return j;
}
}
/**
* 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 <pre>
* {@code [the label of the root of item is an <item> tag] and out.is_open}
* </pre>
* @ensures <pre>
* {@code 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";
// Starts the table row
out.println("<tr>");
// Prints the publication date if present
int k = getChildElement(item, "pubDate");
if (k == -1) {
out.println("<td>No date available</td>");
} else {
String pubDateContent = item.child(k).child(0).label();
out.println("<td>" + pubDateContent + "</td>");
}
// Prints the source if present
k = getChildElement(item, "source");
if (k == -1) {
out.println("<td>No source available</td>");
} else {
String url = item.child(k).attributeValue("url");
String sourceContent = item.child(k).child(0).label();
out.println("<td><a href = " + url + ">" + sourceContent
+ "</a></td>");
}
// Prints the title if present
k = getChildElement(item, "title");
if (k == -1) {
k = getChildElement(item, "description");
String descriptionContent = item.child(k).child(0).label();
k = getChildElement(item, "link");
if (k == -1) {
out.println("<td>" + descriptionContent + "</td>");
} else {
String url = item.child(k).child(0).label();
out.println("<td><a href = " + url + ">" + descriptionContent
+ "</a></td>");
}
} else {
String titleContent = item.child(k).child(0).label();
k = getChildElement(item, "link");
if (k == -1) {
out.println("<td>" + titleContent + "</td>");
} else {
String url = item.child(k).child(0).label();
out.println("<td><a href = " + url + ">" + titleContent
+ "</a></td>");
}
}
// Ends the table row
out.println("</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 {@code out.content}
* @requires <pre>
* {@code out.is_open}
* </pre>
* @ensures <pre>
* {@code [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 feed, String file, SimpleWriter out) {
XMLTree t = new XMLTree1(feed);
String versionvalue = null;
String rootlabel = t.label();
boolean b = t.hasAttribute("version");
if (b) {
versionvalue = t.attributeValue("version");
}
// Makes sure that the tested XMLTree object is an RSS 2.0 feed
while (rootlabel != "rss" && versionvalue != "2.0") {
out.println("Not a proper RSS 2.0 feed.");
out.println("Enter another URL of an RSS 2.0 feed:");
XMLTree tree = new XMLTree1(feed);
rootlabel = tree.label();
b = tree.hasAttribute("version");
if (b) {
versionvalue = t.attributeValue("version");
}
}
// Gets the name of an output file
SimpleWriter outfile = new SimpleWriter1L(file);
// Prints out the header to the file and opening lines
XMLTree channel = t.child(0);
outputHeader(channel, outfile);
// Outputs a paragraph in the file
XMLTree description = channel.child(getChildElement(channel,
"description"));
if (getChildElement(channel, "description") == -1) {
String descriptionContent = description.child(0).label();
outfile.println("<p>" + descriptionContent + "</p>");
}
// Sets up the table and gives the column headers
outfile.println("<p>Table of latest news and headlines.</p>");
outfile.println("<table>");
outfile.println("<tr>");
outfile.println("<td><strong>Date</strong></td>");
outfile.println("<td><strong>Source</strong></td>");
outfile.println("<td><strong>News</strong></td>");
outfile.println("</tr>");
// Outputs the table to the file
int n = getChildElement(channel, "item");
if (n != -1) {
processItem(channel.child(n), outfile);
n++;
while (n < channel.numberOfChildren()) {
if (channel.child(n).label().equals("item")) {
processItem(channel.child(n), outfile);
}
n++;
}
}
// Ends the table tag
outfile.println("</table>");
// Outputs closing tags for html document
outputFooter(outfile);
outfile.close();
}
/**
* Main method.
*
* @param args
* the command line arguments; unused here
*/
public static void main(String[] args) {
SimpleReader in = new SimpleReader1L();
SimpleWriter out = new SimpleWriter1L();
// Asks for XML file containing list of feeds
out.println("Enter feed list: ");
XMLTree source = new XMLTree1(in.nextLine());
source.display();
// Gets the name of an output file
out.println("Enter name of output file:");
String file = in.nextLine();
SimpleWriter index = new SimpleWriter1L(file);
outputIndex(source, index);
int k = 0;
while (k < source.numberOfChildren()) {
String outfile = "out" + (k + 1);
String feed = source.child(k).attributeValue("url");
processFeed(feed, outfile, out);
k++;
}
in.close();
out.close();
index.close();
}
}