Version 0.1 ist fertig
[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                 Element title;
59                 Element 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 channel;
68                 Element itemListe;
69                 Element item;
70                 Iterator iter;
71 List liste = new ArrayList(); 
72 List items = new ArrayList();
73 JSONArray feld = null;
74 String tmp;
75 Text text;
76
77                 
78                 SAXBuilder builder = new SAXBuilder();
79                 try
80                 {
81                         // Die Ergebnisse "raussuchen"
82                         doc = builder.build(new StringReader(rss.toString()));
83                         root = doc.getRootElement();
84                         channel = root.getChild("channel");
85 int tt = channel.getContentSize();
86 items = channel.getChildren("item");
87 iter = items.iterator();
88 //                      iter = channel.getDescendants();
89                         // alle Items auslesen und die Links auslesen
90                         while (iter.hasNext())
91                         {
92                                 // Ist Item = Text dann überlesen
93                                 // anderenfalls die Rückgabe füllen
94                                 try
95                                 {
96                                         item = (Element) iter.next();
97 title = item.getChild("title");
98 link = item.getChild("link");
99 fm.setTitle(title.getText());
100 fm.setLink(link.getText());
101 liste.add(fm);
102 fm = new FeedMessage();
103
104 //                                      tmp = item.getName();
105 //                                      if ((tmp.equals("title")) || (tmp.equals("link")))
106 //                                      {
107 //                                              if (tmp.equals("title")) 
108 //                                              {
109 //                                                      fm.setTitle(item.getValue());                                           
110 //                                              }
111 //                                              if (tmp.equals("link"))
112 //                                              {
113 //                                                      fm.setLink(item.getValue());
114 //                                                      liste.add(fm);
115 //                                                      fm = new FeedMessage();
116 //                                              }
117 //                                      }
118                                         
119                                 }
120                                 catch (Exception e)
121                                 {
122 System.out.println("test1");                    
123                                 }
124 System.out.println("test2");                            
125                         }
126                         
127                 }
128                 catch (JDOMException e)
129                 {
130                         // TODO Auto-generated catch block
131                         e.printStackTrace();
132                 }
133                 catch (IOException e)
134                 {
135                         // TODO Auto-generated catch block
136                         e.printStackTrace();
137                 }
138                         
139         
140                 return (ArrayList)liste;
141
142         }
143
144 }