Added README.txt and GPL Header to Source Files
[DHBWCampusApp.git] / app / src / main / java / de / dhbwloe / campusapp / search / StuvSearchHelper.java
1 /* StuvSearchHelper.java
2  *
3  * This program is free software: you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License as published by
5  * the Free Software Foundation, either version 3 of the License, or
6  * (at your option) any later version.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  */
16 package de.dhbwloe.campusapp.search;
17
18 import android.util.Xml;
19
20 import com.loopj.android.http.AsyncHttpClient;
21 import com.loopj.android.http.AsyncHttpResponseHandler;
22
23 import org.jsoup.nodes.Document;
24
25 import java.io.ByteArrayInputStream;
26 import java.io.UnsupportedEncodingException;
27 import java.net.URLEncoder;
28 import java.util.ArrayList;
29 import java.util.List;
30
31 import de.dhbwloe.campusapp.fragments.AppSearchListItem;
32 import de.dhbwloe.campusapp.network.XmlEntry;
33 import de.dhbwloe.campusapp.network.XmlRequestHelper;
34
35 /**
36  * Created by pk910 on 25.01.2016.
37  */
38 public class StuvSearchHelper extends SearchHelper {
39
40     public StuvSearchHelper() {
41         super(false);
42
43     }
44
45     @Override
46     protected void processWebRequest(AsyncHttpResponseHandler handler, String keywords) {
47         AsyncHttpClient client = new AsyncHttpClient();
48         String url = null;
49         try {
50             url = "http://stuv-loerrach.de/feed/?s="+ URLEncoder.encode(keywords, "UTF-8");
51         } catch (UnsupportedEncodingException e) {
52         }
53         client.get(url, null, handler);
54     }
55
56     @Override
57     protected List<AppSearchListItem> onHtmlDocumentReceived(Document document) {
58         //unused in here
59         return null;
60     }
61
62     @Override
63     protected List<AppSearchListItem> onPlainTextReceived(byte[] data) {
64         ByteArrayInputStream inputStream = new ByteArrayInputStream(data);
65         List<XmlEntry> entries = XmlRequestHelper.parseXml(inputStream, "channel", "item");
66         ArrayList<AppSearchListItem> results = new ArrayList<>();
67
68         for(XmlEntry entry : entries) {
69             XmlEntry element;
70
71             SearchTarget target = new SearchTarget();
72             target.setInAppTarget(true);
73             target.setTargetUrl("WebBrowser");
74
75             element = XmlEntry.FindXmlEntryByName(entry, "title");
76             String title = (element == null ? null : element.getValue());
77
78             element = XmlEntry.FindXmlEntryByName(entry, "link");
79             String link = (element == null ? null : element.getValue());
80
81             element = XmlEntry.FindXmlEntryByName(entry, "description");
82             String description = (element == null ? null : element.getValue());
83
84             target.setArgument("url", link);
85
86             AppSearchListItem result = new AppSearchListItem("STUV: "+title, description, target);
87             results.add(result);
88
89         }
90
91         return results;
92     }
93
94 }