Initial commit
[mustard2:mustard2.git] / src / org / mustard2 / urlshortener / LilUrlShortener.java
1 package org.mustard2.urlshortener;
2
3 import java.io.IOException;
4 import java.net.MalformedURLException;
5 import java.net.URL;
6 import java.util.ArrayList;
7 import java.util.HashMap;
8
9 import org.apache.http.NameValuePair;
10 import org.apache.http.message.BasicNameValuePair;
11 import org.mustard2.android.AuthException;
12 import org.mustard2.android.MustardException;
13 import org.mustard2.util.HttpManager;
14 import org.w3c.dom.Document;
15 import org.w3c.dom.Element;
16 import org.w3c.dom.NamedNodeMap;
17 import org.w3c.dom.Node;
18 import org.w3c.dom.NodeList;
19
20 import android.content.Context;
21
22 public abstract class LilUrlShortener implements UrlShortener {
23
24         protected String lilUrl;
25
26         protected Context mContext;
27         
28         public LilUrlShortener(Context context) {
29                 mContext=context;
30         }
31         
32         public String doShort(String longUrl, HashMap<String, String> params)
33         throws MustardException {
34                 return doShort(longUrl);
35         }
36
37         public String doShort(String longUrl) throws MustardException {
38                 URL uri = null;
39                 try {
40                         uri =new URL(lilUrl);
41                 } catch (MalformedURLException e) {
42                         throw new MustardException(e.getMessage());
43                 }
44                 HttpManager hm = new HttpManager(mContext,uri.getHost());
45                 ArrayList<NameValuePair> params = new ArrayList<NameValuePair>();
46                 params.add(new BasicNameValuePair("longurl", longUrl));
47                 String ret = "";
48                 try {
49
50                         Document dom = hm.getDocument(lilUrl,HttpManager.POST,params);
51                         Element root = dom.getDocumentElement();
52                         NodeList feedNode = root.getElementsByTagName("body");
53                         if (feedNode == null) {
54                                 //Log.i("mustard", "\"body\" is null");
55                                 return null;
56                         }
57                         //Log.i("mustard", "body found");
58
59                         for (int i=0;i<feedNode.getLength();i++){
60
61                                 Node item = feedNode.item(i);
62                                 String itemName = item.getNodeName();
63                                 //Log.i("mustard", "found 1 " + itemName);
64
65                                 if (itemName.equals("body")) {
66                                         NodeList pchilds = item.getChildNodes();
67
68                                         for (int ii=0;ii<pchilds.getLength();ii++){
69
70                                                 Node aNode = pchilds.item(ii);
71                                                 if (aNode.getNodeType()!=Node.ELEMENT_NODE) 
72                                                         continue;
73                                                 String iitemName = aNode.getNodeName();
74                                                 //Log.i("mustard", "found 2 " + iitemName);
75
76
77                                                 if (iitemName.equals("p")) {
78                                                         NodeList ppchilds = aNode.getChildNodes();
79                                                         for (int iii=0;iii<ppchilds.getLength();iii++){
80                                                                 Node aaNode = ppchilds.item(iii);
81                                                                 if (aaNode.getNodeType()!=Node.ELEMENT_NODE) 
82                                                                         continue;
83                                                                 String iiitemName = aaNode.getNodeName();
84                                                                 //Log.i("mustard", "found 3 " + iiitemName);
85                                                                 if(iiitemName.equals("a")) {
86                                                                         NamedNodeMap attr = aaNode.getAttributes();
87                                                                         ret = attr.getNamedItem("href").getNodeValue();
88                                                                         //Log.i("mustard", "href= " + ret);
89                                                                         return ret.trim();
90                                                                 }
91                                                         }
92                                                 }
93                                         }
94                                 }
95
96
97                         }
98                 } catch (AuthException e) {
99                         // Never thrown, no auth required
100                         throw new MustardException("Unauth");
101                 } catch (IOException e) {
102                         throw new MustardException(e.getMessage());
103                 }
104                 return ret;
105         }
106
107 }