01: // Copyright (c) 2002 Cunningham & Cunningham, Inc.
02: // Released under the terms of the GNU General Public License version 2 or later.
03:
04: package eg;
05:
06: import fit.*;
07: import java.net.*;
08: import java.io.*;
09: import java.util.StringTokenizer;
10:
11: public class Page extends RowFixture {
12: static URL url;
13: static String text;
14:
15: // actions //////////////////////////////////
16:
17: public void location(String url) throws Exception {
18: Page.url = new URL(url);
19: Page.text = get(Page.url);
20: }
21:
22: public String title() throws Exception {
23: return new Parse(text, new String[] { "title" }).text();
24: }
25:
26: public void link(String label) throws Exception {
27: Parse links = new Parse(text, new String[] { "a" });
28: while (!links.text().startsWith(label)) {
29: links = links.more;
30: }
31: StringTokenizer tokens = new StringTokenizer(links.tag,
32: "<> =\"");
33: while (!tokens.nextToken().toLowerCase().equals("href")) {
34: }
35: ;
36: url = new URL(url, tokens.nextToken());
37: text = get(url);
38: }
39:
40: // rows /////////////////////////////////////
41:
42: public Class getTargetClass() {
43: return Row.class;
44: }
45:
46: public Object[] query() throws Exception {
47: String tags[] = { "wiki", "table", "tr", "td" };
48: Parse rows = new Parse(text, tags).at(0, 0, 2);
49: Row result[] = new Row[rows.size()];
50: for (int i = 0; i < result.length; i++) {
51: result[i] = new Row(rows);
52: rows = rows.more;
53: }
54: return result;
55: }
56:
57: public class Row {
58: Parse cells;
59:
60: Row(Parse row) {
61: this .cells = row.parts;
62: }
63:
64: public String numerator() {
65: return cells.at(0).text();
66: }
67:
68: public String denominator() {
69: return cells.at(1).text();
70: }
71:
72: public String quotient() {
73: return cells.at(2).text();
74: }
75: }
76:
77: // utilities ////////////////////////////////
78:
79: private String get(URL url) throws IOException {
80: Page.url = url;
81: InputStream stream = (InputStream) url.getContent();
82: BufferedReader reader = new BufferedReader(
83: new InputStreamReader(stream));
84: StringBuffer buffer = new StringBuffer(10000);
85: for (String line; (line = reader.readLine()) != null;) {
86: buffer.append(line);
87: buffer.append('\n');
88: }
89: return buffer.toString();
90: }
91:
92: }
|