KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > jftp > tools > RSSParser


1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * 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
25 public class RSSParser
26 {
27     URL file;
28     Vector titles = new Vector();
29     Vector descs = new Vector();
30     Vector links = new Vector();
31     Vector content = new Vector();
32
33     public RSSParser(URL f)
34     {
35         file = f;
36         parse();
37     }
38
39     private void parse()
40     {
41         try
42         {
43             DataInputStream in = new DataInputStream(new BufferedInputStream(file.openStream()));
44
45             String JavaDoc tmp;
46             String JavaDoc data = "";
47
48             while((tmp = in.readLine()) != null)
49             {
50                 data += tmp;
51             }
52
53             add(data, content, "<title>", "</title>", "<description>",
54                 "</description>");
55             add(data, titles, "<title>", "</title>", null, null);
56             add(data, descs, "<description>", "</description>", null, null);
57             add(data, links, "<link>", "</link>", null, null);
58         }
59         catch(Exception JavaDoc ex)
60         {
61             ex.printStackTrace();
62
63             return;
64         }
65     }
66
67     private void add(String JavaDoc tmp, Vector target, String JavaDoc start, String JavaDoc end,
68                      String JavaDoc s2, String JavaDoc e2)
69     {
70         if(s2 == null)
71         {
72             s2 = start;
73             e2 = end;
74         }
75
76         int x = tmp.indexOf(start);
77         int x2 = tmp.indexOf(s2);
78
79         if(((x < 0) && (x2 < 0)) || ((x < 0) && start.equals(s2)))
80         {
81             return;
82         }
83
84         if((x2 >= 0) && ((x2 < x) || (x < 0)))
85         {
86             x = x2;
87
88             String JavaDoc t = start;
89             start = s2;
90             s2 = t;
91
92             t = end;
93             end = e2;
94             e2 = t;
95         }
96
97         //System.out.println(tmp+":::"+x+":::"+x2);
98
String JavaDoc value = tmp.substring(x + start.length(), tmp.indexOf(end));
99
100         //System.out.println(value);
101
target.add(value);
102
103         tmp = tmp.substring(tmp.indexOf(end) + end.length());
104
105         add(tmp, target, start, end, s2, e2);
106     }
107 }
108
Popular Tags