01: /*
02: * This program is free software; you can redistribute it and/or
03: * modify it under the terms of the GNU General Public License
04: * as published by the Free Software Foundation; either version 2
05: * of the License, or (at your option) any later version.
06: *
07: * This program is distributed in the hope that it will be useful,
08: * but WITHOUT ANY WARRANTY; without even the implied warranty of
09: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10: * GNU General Public License for more details.
11:
12: * You should have received a copy of the GNU General Public License
13: * along with this program; if not, write to the Free Software
14: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15: */
16: package net.sf.jftp.tools;
17:
18: import java.io.*;
19:
20: import java.net.*;
21:
22: import java.util.*;
23:
24: public class RSSParser {
25: URL file;
26: Vector titles = new Vector();
27: Vector descs = new Vector();
28: Vector links = new Vector();
29: Vector content = new Vector();
30:
31: public RSSParser(URL f) {
32: file = f;
33: parse();
34: }
35:
36: private void parse() {
37: try {
38: DataInputStream in = new DataInputStream(
39: new BufferedInputStream(file.openStream()));
40:
41: String tmp;
42: String data = "";
43:
44: while ((tmp = in.readLine()) != null) {
45: data += tmp;
46: }
47:
48: add(data, content, "<title>", "</title>", "<description>",
49: "</description>");
50: add(data, titles, "<title>", "</title>", null, null);
51: add(data, descs, "<description>", "</description>", null,
52: null);
53: add(data, links, "<link>", "</link>", null, null);
54: } catch (Exception ex) {
55: ex.printStackTrace();
56:
57: return;
58: }
59: }
60:
61: private void add(String tmp, Vector target, String start,
62: String end, String s2, String e2) {
63: if (s2 == null) {
64: s2 = start;
65: e2 = end;
66: }
67:
68: int x = tmp.indexOf(start);
69: int x2 = tmp.indexOf(s2);
70:
71: if (((x < 0) && (x2 < 0)) || ((x < 0) && start.equals(s2))) {
72: return;
73: }
74:
75: if ((x2 >= 0) && ((x2 < x) || (x < 0))) {
76: x = x2;
77:
78: String t = start;
79: start = s2;
80: s2 = t;
81:
82: t = end;
83: end = e2;
84: e2 = t;
85: }
86:
87: //System.out.println(tmp+":::"+x+":::"+x2);
88: String value = tmp.substring(x + start.length(), tmp
89: .indexOf(end));
90:
91: //System.out.println(value);
92: target.add(value);
93:
94: tmp = tmp.substring(tmp.indexOf(end) + end.length());
95:
96: add(tmp, target, start, end, s2, e2);
97: }
98: }
|