erste Daten kommen zurück
[yacyandroid:yacyandroid.git] / src / net / yacy / rss / RSSFeedParser.java
1 package net.yacy.rss;
2
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.io.StringReader;
6 import java.net.MalformedURLException;
7 import java.net.URL;
8 import java.util.ArrayList;
9 import java.util.Iterator;
10 import java.util.List;
11
12 import javax.xml.parsers.ParserConfigurationException;
13 import javax.xml.parsers.SAXParser;
14 import javax.xml.parsers.SAXParserFactory;
15
16 import org.jdom.Document;
17 import org.jdom.Element;
18 import org.jdom.JDOMException;
19 import org.jdom.Text;
20 import org.jdom.input.SAXBuilder;
21
22 import org.json.JSONArray;
23 import org.json.JSONException;
24 import org.json.JSONObject;
25 import org.xml.sax.InputSource;
26 import org.xml.sax.SAXException;
27 import org.xml.sax.XMLReader;
28 import org.xml.sax.helpers.DefaultHandler;
29
30 import android.content.Context;
31 import android.widget.Toast;
32
33
34 public class RSSFeedParser
35 {
36         static final String TITLE = "title";
37         static final String DESCRIPTION = "description";
38         static final String CHANNEL = "channel";
39         static final String LANGUAGE = "language";
40         static final String COPYRIGHT = "copyright";
41         static final String LINK = "link";
42         static final String AUTHOR = "author";
43         static final String ITEM = "item";
44         static final String PUB_DATE = "pubDate";
45         static final String GUID = "guid";
46
47         
48         
49
50         @SuppressWarnings("null")
51         public ArrayList readFeed(StringBuilder rss) 
52         {
53                 Feed feed = null;
54                 FeedMessage fm = new FeedMessage();
55                 boolean isFeedHeader = true;
56                 // Set header values intial to the empty string
57                 String description = "";
58                 String title = "";
59                 String link = "";
60                 String language = "";
61                 String copyright = "";
62                 String author = "";
63                 String pubdate = "";
64                 String guid = "";
65                 Document doc = null;
66                 Element root;
67                 Element itemListe;
68                 Element item;
69                 Iterator iter;
70 List liste = new ArrayList(); 
71 JSONArray feld = null;
72 String tmp;
73 Text text;
74                 
75                 SAXBuilder builder = new SAXBuilder();
76                 try
77                 {
78                         // Die Ergebnisse "raussuchen"
79                         doc = builder.build(new StringReader(rss.toString()));
80                         root = doc.getRootElement();
81                         itemListe = root.getChild("channel");
82                         iter = itemListe.getDescendants();
83                         // alle Items auslesen und die Links auslesen
84                         while (iter.hasNext())
85                         {
86                                 // Ist Item = Text dann überlesen
87                                 // anderenfalls die Rückgabe füllen
88                                 try
89                                 {
90                                         item = (Element) iter.next();
91                                         tmp = item.getName();
92                                         if ((tmp.equals("title")) || (tmp.equals("link")))
93                                         {
94                                                 if (tmp.equals("title")) 
95                                                 {
96                                                         fm.setTitle(item.getValue());                                           
97                                                 }
98                                                 if (tmp.equals("link"))
99                                                 {
100                                                         fm.setLink(item.getValue());
101                                                         liste.add(fm);
102                                                 }
103                                                 
104                                         }
105                                         
106                                 }
107                                 catch (Exception e)
108                                 {
109 System.out.println("test1");                    
110                                 }
111 System.out.println("test2");                            
112                         }
113                         
114                 }
115                 catch (JDOMException e)
116                 {
117                         // TODO Auto-generated catch block
118                         e.printStackTrace();
119                 }
120                 catch (IOException e)
121                 {
122                         // TODO Auto-generated catch block
123                         e.printStackTrace();
124                 }
125                         
126         
127                 return (ArrayList)liste;
128
129         }
130
131 }