Initial commit
[mustard2:mustard2.git] / src / org / mustard2 / statusnet / StatusNetAtomUtil.java
1 package
2 org.mustard2.statusnet;
3
4 import java.io.IOException;
5 import java.text.DateFormat;
6 import java.text.SimpleDateFormat;
7 import java.util.ArrayList;
8 import java.util.Locale;
9
10 import javax.xml.parsers.DocumentBuilder;
11 import javax.xml.parsers.DocumentBuilderFactory;
12 import javax.xml.parsers.ParserConfigurationException;
13
14 import org.mustard2.android.MustardApp;
15 import org.mustard2.microblog.Notice;
16 import org.mustard2.microblog.Status;
17 import org.mustard2.microblog.User;
18 import org.w3c.dom.Document;
19 import org.w3c.dom.Element;
20 import org.w3c.dom.Node;
21 import org.w3c.dom.NodeList;
22 import org.xml.sax.SAXException;
23
24 import android.util.Log;
25
26
27 public class StatusNetAtomUtil {
28         
29         private static DateFormat df =  new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ",Locale.ENGLISH);
30         
31         private Document dom ;
32         
33         public StatusNetAtomUtil(String xml) {
34                 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
35         try {
36                 DocumentBuilder builder = factory.newDocumentBuilder();
37                 dom = builder.parse(xml);
38         } catch (IOException e) {
39                 e.printStackTrace();
40         } catch (ParserConfigurationException e) {
41                         e.printStackTrace();
42                 } catch (SAXException e) {
43                         e.printStackTrace();
44                 }
45         }
46
47         
48         public ArrayList<Status> getStatuses() {
49                 ArrayList<Status> statuses = new ArrayList<Status>();
50                 Element root = dom.getDocumentElement();
51                 NodeList feedNode = root.getElementsByTagName("feed");
52                 if (feedNode == null) {
53                         Log.e("mustard", "\"feed\" is null");
54                         return statuses;
55                 }
56                 Log.e("mustard", "feed found");
57                 Element feed = (Element)feedNode.item(0);
58                 NodeList items = feed.getElementsByTagName("entry");
59                 for (int i = 0; i < items.getLength(); i++) {
60                         
61                         try {
62                                 Element entry = (Element)items.item(i);
63                                 statuses.add(getStatus(entry));
64                         } catch (Exception e) {
65                                 e.printStackTrace();
66                         }
67                 }
68                 return statuses;
69         }
70         /**
71          * Parse the atom entry and create the Status object
72          * <entry>
73          *  <source>
74          *   <id>http://identi.ca/api/statuses/user_timeline/360485.atom</id>
75          *   <title>Evaggelos Balaskas</title>
76          *   <link rel="alternate" type="text/html" href="http://identi.ca/ebalaskas"/>
77          *   <link rel="self" type="application/atom+xml" href="http://identi.ca/api/statuses/user_timeline/360485.atom"/>
78          *   <icon>http://avatar.identi.ca/360485-96-20110110205121.png</icon>
79          *   <updated>2011-02-06T11:18:06+00:00</updated>
80          *   <link rel="license" href="http://creativecommons.org/licenses/by/3.0/"/>
81          * </source>
82          *  <title>picture of Dieter Plaetinck  after his !archlinux presentation on !fosdem http://ebalaskas.gr/img/Dieter.Plaetinck.Fosdem2011.jpg</title>
83          * <author>
84          *  <name>ebalaskas</name>
85          *  <uri>http://identi.ca/user/360485</uri>
86          *  <statusnet:profile_info following="false" blocking="false"></statusnet:profile_info>
87          * </author>
88          * <activity:actor>
89          *  <activity:object-type>http://activitystrea.ms/schema/1.0/person</activity:object-type>
90          *  <id>http://identi.ca/user/360485</id>
91          *  <title>Evaggelos Balaskas</title>
92          *  <link rel="alternate" type="text/html" href="http://identi.ca/ebalaskas"/>
93          *  <link rel="avatar" type="image/png" media:width="250" media:height="250" href="http://avatar.identi.ca/360485-250-20110110205121.png"/>
94          *  <link rel="avatar" type="image/png" media:width="96" media:height="96" href="http://avatar.identi.ca/360485-96-20110110205121.png"/>
95          *  <link rel="avatar" type="image/png" media:width="48" media:height="48" href="http://avatar.identi.ca/360485-48-20110110205122.png"/>
96          *  <link rel="avatar" type="image/png" media:width="24" media:height="24" href="http://avatar.identi.ca/360485-24-20110110205122.png"/>
97          *  <georss:point>37.97945 23.71622</georss:point>
98          * <poco:preferredUsername>ebalaskas</poco:preferredUsername>
99          * <poco:displayName>Evaggelos Balaskas</poco:displayName>
100          * <poco:note>Unix System Engineer Python Inotify Rsync Daemon: http://goo.gl/vTuoN</poco:note>
101          * <poco:address>
102          *  <poco:formatted>Athens, GReece</poco:formatted>
103          * </poco:address>
104          * <poco:urls>
105          *  <poco:type>homepage</poco:type>
106          *  <poco:value>http://ebalaskas.gr</poco:value>
107          *  <poco:primary>true</poco:primary>
108          * </poco:urls>
109          * </activity:actor>
110          *  <link rel="alternate" type="text/html" href="http://identi.ca/notice/63826217"/>
111          *  <id>http://identi.ca/notice/63826217</id>
112          *  <published>2011-02-06T11:18:06+00:00</published>
113          *  <updated>2011-02-06T11:18:06+00:00</updated>
114          *  <content type="html">picture of Dieter Plaetinck  after his !&lt;span class=&quot;vcard&quot;&gt;&lt;a href=&quot;http://identi.ca/group/55/id&quot; class=&quot;url&quot; title=&quot;Arch Linux (archlinux)&quot;&gt;&lt;span class=&quot;fn nickname&quot;&gt;archlinux&lt;/span&gt;&lt;/a&gt;&lt;/span&gt; presentation on !&lt;span class=&quot;vcard&quot;&gt;&lt;a href=&quot;http://identi.ca/group/400/id&quot; class=&quot;url&quot; title=&quot;FOSDEM (fosdem)&quot;&gt;&lt;span class=&quot;fn nickname&quot;&gt;fosdem&lt;/span&gt;&lt;/a&gt;&lt;/span&gt; &lt;a href=&quot;http://ebalaskas.gr/img/Dieter.Plaetinck.Fosdem2011.jpg&quot; title=&quot;http://ebalaskas.gr/img/Dieter.Plaetinck.Fosdem2011.jpg&quot; class=&quot;attachment&quot; id=&quot;attachment-38641291&quot; rel=&quot;nofollow external&quot;&gt;http://ebalaskas.gr/img/Dieter.Plaetinck.Fosdem2011.jpg&lt;/a&gt;</content>
115          *  <activity:verb>http://activitystrea.ms/schema/1.0/post</activity:verb>
116          *  <activity:object-type>http://activitystrea.ms/schema/1.0/note</activity:object-type>
117          *  <statusnet:notice_info local_id="63826217" source="web" favorite="false" repeated="false"></statusnet:notice_info>
118          *  <link rel="ostatus:conversation" href="http://identi.ca/conversation/63106093"/>
119          *  <link rel="ostatus:attention" href="http://identi.ca/group/55/id"/>
120          *  <link rel="mentioned" href="http://identi.ca/group/55/id"/>
121          *  <link rel="ostatus:attention" href="http://identi.ca/group/400/id"/>
122          *  <link rel="mentioned" href="http://identi.ca/group/400/id"/>
123          *  <category term="archlinux"></category>
124          *  <category term="fosdem"></category>
125          *  <link rel="enclosure" href="http://ebalaskas.gr/img/Dieter.Plaetinck.Fosdem2011.jpg" type="image/jpeg" length="467620"/>
126          * </entry>
127          * 
128          * 
129          * @param nStatus
130          * @return
131          * @throws Exception
132          */
133         public Status getStatus(Element entry) throws Exception {
134
135                 if (entry == null) {
136                         Log.e("mustard","entry is null");
137                         return null;
138                 }
139                 Status status = new Status();
140                 Notice notice = new Notice();
141                 User user = new User();
142                 Log.v("mustard", ">>>>>>>>>>>>>>>>> ENTRY <<<<<<<<<<<<<<<<<<<");
143                 
144                 NodeList entryChilds = entry.getChildNodes();
145                 
146                 for (int i=0;i<entryChilds.getLength();i++) {
147                         Node n= entryChilds.item(i);
148             if(n.getNodeType()==Node.ELEMENT_NODE){
149                 String nodeName = n.getNodeName();
150                 Log.v("mustard", "found node: " + nodeName);
151                 if (nodeName.equals("published")) {
152                         notice.setCreated_at(df.parse(n.getFirstChild().getNodeValue()));
153                 } else if (nodeName.equals("title")) {
154                         notice.setText(n.getFirstChild().getNodeValue());
155                 } else if (nodeName.equals("author")) {
156                         NodeList autorChilds = n.getChildNodes();
157                         for  (int j=0;j<autorChilds.getLength();j++) {
158                                 Node nn = autorChilds.item(j);
159                                 if (nn.getNodeType()==Node.ELEMENT_NODE){
160                                         String nnodeName = nn.getNodeName();
161                                         Log.v("mustard", "  >>  found node: " + nnodeName);
162                                         if (nnodeName.equals("name")) {
163                                                 user.setScreen_name(nn.getFirstChild().getNodeValue());
164                                         } else if (nnodeName.equals("statusnet:profile_info")) {
165                                                 user.setFollowing( nn.getAttributes().getNamedItem("following").getNodeValue().equals("true") );
166                                         }
167                                 }
168                         }
169                 } else if (nodeName.equals("statusnet:notice_info") ) {
170                         notice.setId(Long.parseLong(n.getAttributes().getNamedItem("local_id").getNodeValue()));
171                         notice.setSource(n.getAttributes().getNamedItem("source").getNodeValue());
172                 }
173             }
174                 }
175                 if(MustardApp.DEBUG)
176                         Log.d(MustardApp.APPLICATION_NAME, notice.toString());
177
178                 
179                 
180                 status.setNotice(notice);
181                 status.setUser(user);
182                 return status;
183
184         }
185 }