Added README.txt and GPL Header to Source Files
[DHBWCampusApp.git] / app / src / main / java / de / dhbwloe / campusapp / network / XmlRequestHelper.java
1 /* XmlRequestHelper.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.network;
17 import android.os.Bundle;
18 import android.util.Log;
19
20 import com.loopj.android.http.AsyncHttpClient;
21 import com.loopj.android.http.AsyncHttpResponseHandler;
22
23 import net.fortuna.ical4j.data.CalendarBuilder;
24 import net.fortuna.ical4j.data.ParserException;
25 import net.fortuna.ical4j.model.Calendar;
26
27 import org.xmlpull.v1.XmlPullParser;
28 import org.xmlpull.v1.XmlPullParserException;
29 import org.xmlpull.v1.XmlPullParserFactory;
30
31 import java.io.ByteArrayInputStream;
32 import java.io.IOException;
33 import java.io.InputStream;
34 import java.io.UnsupportedEncodingException;
35 import java.security.KeyStore;
36 import java.util.ArrayList;
37 import java.util.List;
38
39 import cz.msebera.android.httpclient.Header;
40
41 /**
42  * Created by pk910 on 20.01.2016.
43  */
44 public abstract class XmlRequestHelper {
45     private static final String ns = null;
46     private String sRootElementName, sElementName;
47
48     protected void requestXmlFromWeb(String url, String rootElementName, String elementName) {
49         AsyncHttpClient client = new AsyncHttpClient();
50
51         sRootElementName = rootElementName;
52         sElementName = elementName;
53
54         Log.i("HTTPClient", "Request: "+url);
55         client.get(url, new AsyncHttpResponseHandler() {
56             @Override
57             public void onStart() {
58             }
59
60             @Override
61             public void onSuccess(int statusCode, Header[] headers, byte[] response) {
62                 ByteArrayInputStream inputStream = new ByteArrayInputStream(response);
63                 List<XmlEntry> entries = parseXml(inputStream, sRootElementName, sElementName);
64
65                 onXmlReceived(entries);
66             }
67
68             @Override
69             public void onFailure(int statusCode, Header[] headers, byte[] errorResponse, Throwable e) {
70                 String error = null;
71                 try {
72                     error = new String(errorResponse, "US-ASCII");
73                 } catch (Exception e1) {
74                 }
75                 Log.i("HTTPClient", "  Error: " + statusCode + " - " + error);
76                 onXmlRequestFail(statusCode, error);
77             }
78
79             @Override
80             public void onRetry(int retryNo) {
81             }
82         });
83     }
84
85     public static List<XmlEntry> parseXml(InputStream input, String rootElementName, String elementName) {
86         List<XmlEntry> entries = null;
87         try {
88             XmlPullParserFactory xmlFactoryObject = XmlPullParserFactory.newInstance();
89             XmlPullParser myparser = xmlFactoryObject.newPullParser();
90
91             myparser.setInput(input, null);
92
93             do {
94                 myparser.nextTag();
95             } while(!myparser.getName().equalsIgnoreCase(rootElementName));
96
97             entries = readXml(myparser, rootElementName, elementName);
98         } catch (Exception e) {
99             e.printStackTrace();
100         }
101
102         return entries;
103     }
104
105     private static List<XmlEntry> readXml(XmlPullParser parser, String rootElementName, String elementName) throws XmlPullParserException, IOException {
106         List<XmlEntry> entries = new ArrayList<XmlEntry>();
107
108         parser.require(XmlPullParser.START_TAG, ns, rootElementName);
109         while (parser.next() != XmlPullParser.END_TAG) {
110
111             if (parser.getEventType() != XmlPullParser.START_TAG) {
112                 continue;
113             }
114             String name = parser.getName();
115             // Starts by looking for the entry tag
116             if (name.equalsIgnoreCase(elementName)) {
117                 Log.i("XMLParser", "found "+elementName);
118                 entries.add(readXmlEntry(parser, elementName));
119             } else {
120                 entries.addAll(readXml(parser, name, elementName));
121             }
122         }
123         return entries;
124     }
125
126     private static XmlEntry readXmlEntry(XmlPullParser parser, String elementName) throws XmlPullParserException, IOException {
127         parser.require(XmlPullParser.START_TAG, ns, elementName);
128         XmlEntry entry = new XmlEntry(elementName);
129         int depth = 1;
130         int attrcount = parser.getAttributeCount();
131         Bundle attributes = new Bundle();
132         for(int i = 0; i < attrcount; i++) {
133             attributes.putString(parser.getAttributeName(i), parser.getAttributeValue(i));
134         }
135         entry.setAttributes(attributes);
136         while (depth != 0) {
137             switch (parser.next()) {
138                 case XmlPullParser.START_TAG:
139                     XmlEntry child = readXmlEntry(parser, parser.getName());
140                     entry.addChild(child);
141                     break;
142                 case XmlPullParser.END_TAG:
143                     depth = 0;
144                     break;
145                 case XmlPullParser.TEXT:
146                     entry.setValue(parser.getText());
147                     break;
148             }
149         }
150         return entry;
151     }
152
153     private static void skip(XmlPullParser parser) throws XmlPullParserException, IOException {
154         if (parser.getEventType() != XmlPullParser.START_TAG) {
155             throw new IllegalStateException();
156         }
157         int depth = 1;
158         while (depth != 0) {
159             switch (parser.next()) {
160                 case XmlPullParser.END_TAG:
161                     depth--;
162                     break;
163                 case XmlPullParser.START_TAG:
164                     depth++;
165                     break;
166             }
167         }
168     }
169
170     protected abstract void onXmlReceived(List<XmlEntry> entries);
171     protected abstract void onXmlRequestFail(int statusCode, String errorMessage);
172
173 }