Initial commit
[mustard2:mustard2.git] / src / org / mustard2 / statusnet / XmlPullStatusNetParser.java
1 package org.mustard2.statusnet;
2
3 import java.io.InputStream;
4 import java.util.ArrayList;
5
6 import org.mustard2.android.MustardApp;
7 import org.mustard2.microblog.Notice;
8 import org.mustard2.microblog.Status;
9 import org.mustard2.microblog.User;
10 import org.xmlpull.v1.XmlPullParser;
11
12 import android.text.Html;
13 import android.util.Log;
14 import android.util.Xml;
15
16 public class XmlPullStatusNetParser extends BaseStatusNetParser {
17
18         public XmlPullStatusNetParser(InputStream is) {
19                 super(is);
20         }
21
22         public ArrayList<Status> parse() {
23                 ArrayList<Status> statuses = null;
24                 XmlPullParser parser = Xml.newPullParser();
25                 try {
26                         // auto-detect the encoding from the stream
27                         parser.setInput(this.getInputStream(), null);
28                         int eventType = parser.getEventType();
29                         Status currentStatus = null;
30                         boolean done = false;
31                         boolean inAuthor=false;
32                         boolean inLocation=false;
33                         while (eventType != XmlPullParser.END_DOCUMENT && !done){
34                                 String name = null;
35                                 String ns = null;
36                                 switch (eventType){
37                                         case XmlPullParser.START_DOCUMENT:
38                                                 statuses = new ArrayList<Status>();
39                                                 break;
40                                         case XmlPullParser.START_TAG:
41                                                 name = parser.getName();
42                                                 ns = parser.getNamespace().toLowerCase();
43 //                                              Log.i(MustardApp.APPLICATION_NAME,"StartTag: " + name + " from " + ns);
44                                                 if (name.equalsIgnoreCase(ENTRY)){
45                                                         currentStatus = new Status();
46                                                         Notice notice = new Notice();
47                                                         User user = new User();
48                                                         currentStatus.setNotice(notice);
49                                                         currentStatus.setUser(user);
50                                                 } else if (currentStatus != null){
51                                                         if (name.equalsIgnoreCase(PUBLISHED) && ns.equals(NS_ATOM)){
52                                                                 Log.i(MustardApp.APPLICATION_NAME, "\tStart " + PUBLISHED);
53                                                                 currentStatus.getNotice().setCreated_at(df.parse(parser.nextText()));
54                                                         } else if (name.equalsIgnoreCase(NOTICE_INFO) && ns.equals(NS_STATUSNET)){
55                                                                 for (int i=0;i<parser.getAttributeCount();i++) {
56                                                                         String _name = parser.getAttributeName(i);
57                                                                         String _ns = parser.getAttributeNamespace(i);
58                                                                         String _value = parser.getAttributeValue(i);
59                                                                         if (_name.equalsIgnoreCase(LOCALID))
60                                                                                 currentStatus.getNotice().setId(Long.valueOf(_value));
61                                                                         else if (_name.equalsIgnoreCase(SOURCE))
62                                                                                 currentStatus.getNotice().setSource(Html.fromHtml(_value).toString());
63                                                                         else if (_name.equalsIgnoreCase(FAVORITE))
64                                                                                 currentStatus.getNotice().setFavorited(_value.equalsIgnoreCase("true"));
65                                                                         Log.i(MustardApp.APPLICATION_NAME,"\t" + _ns +"." + _name + " = " + _value);
66                                                                 }
67                                                         } else if (name.equalsIgnoreCase(AUTHOR) && ns.equals(NS_ATOM)){
68                                                                 Log.i(MustardApp.APPLICATION_NAME, "\tStart " + AUTHOR);
69                                                                 inAuthor=true;
70                                                         } else if (name.equalsIgnoreCase(LINK) && ns.equals(NS_ATOM)) {
71                                                                 boolean inAvatar=false;
72                                                                 boolean isMedia=false;
73                                                                 boolean isMediaBig=false;
74                                                                 for (int i=0;i<parser.getAttributeCount();i++) {
75                                                                         String _name = parser.getAttributeName(i);
76                                                                         String _ns = parser.getAttributeNamespace(i);
77                                                                         String _value = parser.getAttributeValue(i);
78                                                                         currentStatus.getNotice().setFavorited(_value.equalsIgnoreCase("true"));
79                                                                         if(MustardApp.DEBUG)
80                                                                                 Log.i(MustardApp.APPLICATION_NAME,"\t" + _ns +"." + _name + " = " + _value);
81                                                                         if(inAuthor) {
82                                                                                 if(_name.equals("rel")) {
83                                                                                         if(_value.equalsIgnoreCase("avatar")) {
84                                                                                                 Log.i(MustardApp.APPLICATION_NAME, "inAvatar true");
85                                                                                                 inAvatar=true;
86                                                                                                 // reset media attribute
87                                                                                                 isMedia=false;
88                                                                                                 isMediaBig=false;
89                                                                                         } else {
90                                                                                                 inAvatar=false;
91                                                                                                 Log.i(MustardApp.APPLICATION_NAME, "inAvatar false");
92                                                                                         }
93                                                                                 }
94                                                                                 if(inAvatar && _ns.equals(NS_MEDIA) && _name.equalsIgnoreCase(WIDTH)) {
95                                                                                         if (_value.equals("48")) {
96                                                                                                 Log.i(MustardApp.APPLICATION_NAME, "isMedia");
97                                                                                                 isMedia=true;
98                                                                                         } else if (_value.equals("96")) {
99                                                                                                 isMediaBig=true;
100                                                                                         }
101                                                                                 }
102                                                                                 if(inAvatar && _name.equalsIgnoreCase(HREF)) {
103                                                                                         if(isMedia)
104                                                                                                 currentStatus.getUser().setProfile_image_url(_value);
105                                                                                         else if (isMediaBig)
106                                                                                                 currentStatus.getUser().setProfile_image_url_big(_value);
107                                                                                 }
108                                                                         } else {
109
110                                                                         }
111                                                                 }
112                                                         } else if (name.equalsIgnoreCase(NAME) && ns.equals(NS_ATOM)) {
113                                                                 if(inAuthor) {
114                                                                         currentStatus.getUser().setScreen_name(parser.nextText());
115                                                                 }
116                                                         } else if (name.equalsIgnoreCase(DISPLAY_NAME) && ns.equals(NS_POCO)) {
117                                                                 if(inAuthor) {
118                                                                         currentStatus.getUser().setName(parser.nextText());
119                                                                 }
120                                                         } else if (name.equalsIgnoreCase(ADDRESS) && ns.equals(NS_POCO)) {
121                                                                 if(inAuthor) {
122                                                                         inLocation=true;
123                                                                 }
124                                                         }else if (name.equalsIgnoreCase(FORMATTED) && ns.equals(NS_POCO)) {
125                                                                 if(inAuthor && inLocation) {
126                                                                         currentStatus.getUser().setLocation(parser.nextText());
127                                                                         
128                                                                 }
129                                                         } else if (name.equalsIgnoreCase(NOTE) && ns.equals(NS_POCO)) {
130                                                                 if(inAuthor) {
131                                                                         currentStatus.getUser().setDescription(parser.nextText());
132                                                                 }
133                                                         }else if (name.equalsIgnoreCase(CONTENT) && ns.equals(NS_ATOM)) {
134                                                                 boolean isHtml=false;
135                                                                 for (int i=0;i<parser.getAttributeCount();i++) {
136                                                                         String _name = parser.getAttributeName(i);
137                                                                         String _ns = parser.getAttributeNamespace(i);
138                                                                         String _value = parser.getAttributeValue(i);
139                                                                         if(MustardApp.DEBUG)
140                                                                                 Log.i(MustardApp.APPLICATION_NAME,"\t" + _ns +"." + _name + " = " + _value);
141                                                                         
142                                                                         if (_name.equals(TYPE)) {
143                                                                                 
144                                                                                 if(_value.equalsIgnoreCase(HTML)) {
145                                                                                         isHtml=true;
146                                                                                 }
147                                                                                 
148                                                                         }
149                                                                 }
150                                                                 String s = parser.nextText();
151                                                                 if(isHtml) {
152                                                                         currentStatus.getNotice().setText(Html.fromHtml(s).toString());
153                                                                 } else {
154                                                                         currentStatus.getNotice().setText(s);
155                                                                 }
156                                                         } else if (name.equalsIgnoreCase(POINT) && ns.equals(NS_GEORSS)) {
157                                                                 String l = parser.nextText();
158                                                                 String[] ll = l.split(" ");
159                                                                 currentStatus.getNotice().setLon(Double.parseDouble(ll[0]));
160                                                                 currentStatus.getNotice().setLat(Double.parseDouble(ll[1]));
161                                                         }
162                                                 
163                                                 }
164                                                 break;
165                                         case XmlPullParser.END_TAG:
166                                                 name = parser.getName();
167                                                 ns = parser.getNamespace();
168                                                 if(MustardApp.DEBUG)
169                                                         Log.i(MustardApp.APPLICATION_NAME,"EndTag: " + name + " from " + ns);
170                                                 if (name.equalsIgnoreCase(ENTRY) && currentStatus != null){
171                                                         statuses.add(currentStatus);
172                                                         Log.i(MustardApp.APPLICATION_NAME, currentStatus.toString());
173                                                         return statuses;
174                                                 } else if (name.equalsIgnoreCase(FEED)){
175                                                         done = true;
176                                                 } else if (name.equalsIgnoreCase(AUTHOR)) {
177                                                         inAuthor=false;
178                                                         Log.i(MustardApp.APPLICATION_NAME, "\tEnd Author");
179                                                 } else if (name.equalsIgnoreCase(ADDRESS) && ns.equals(NS_POCO)) {
180                                                         if(inAuthor) {
181                                                                 inLocation=false;
182                                                         }
183                                                 }
184                                                 break;
185                                 }
186                                 eventType = parser.next();
187                         }
188                 } catch (Exception e) {
189                         Log.e(MustardApp.APPLICATION_NAME+"::PullStatusNetParser", e.getMessage(), e);
190                         throw new RuntimeException(e);
191                 }
192                 return statuses;
193         }
194 }