1 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 tmp; 46 String 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 ex) 60 { 61 ex.printStackTrace(); 62 63 return; 64 } 65 } 66 67 private void add(String tmp, Vector target, String start, String end, 68 String s2, String 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 t = start; 89 start = s2; 90 s2 = t; 91 92 t = end; 93 end = e2; 94 e2 = t; 95 } 96 97 String value = tmp.substring(x + start.length(), tmp.indexOf(end)); 99 100 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 |