formatting and exception handling in EventParser

Change-Id: I6157b664a34106d7681651dfc2119047880395ce
This commit is contained in:
Axel Uhl
2018-04-06 11:39:27 +02:00
parent 624a2dee0b
commit 0666d49ff1
@@ -1,26 +1,26 @@
package com.sapsailing.xrr.structureimport.eventimport;
import com.sap.sse.util.HttpUrlConnectionHelper;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import com.sap.sse.util.HttpUrlConnectionHelper;
public class EventParser {
private static final Logger logger = Logger.getLogger(EventParser.class.getName());
public EventResults parseEvent(String url){
EventResults eventResults = null;
JSONObject jsonRoot;
try {
InputStreamReader streamReader = getStreamReader(url);
@@ -30,7 +30,6 @@ public class EventParser {
String name = (String) jsonRoot.get("Name");
String xrrUrl = (String) jsonRoot.get("XrrUrl");
eventResults = new EventResults(id, name, xrrUrl);
JSONArray jsonRegattas = (JSONArray) jsonRoot.get("Regattas");
for (Object regattaObject: jsonRegattas) {
RegattaJSON regatta = new RegattaJSON();
@@ -43,39 +42,24 @@ public class EventParser {
regatta.setXrrPreliminaryUrl((String) jsonRegatta.get("XrrPreliminaryUrl"));
regatta.setXrrFinalUrl((String) jsonRegatta.get("XrrFinalUrl"));
regatta.setHtmlUrl((String) jsonRegatta.get("HtmlUrl"));
eventResults.addRegatta(regatta);
}
streamReader.close();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
} catch (Exception e) {
logger.log(Level.SEVERE, "Error during parsing event structure", e);
}
return eventResults;
}
private InputStreamReader getStreamReader(String url) throws UnsupportedEncodingException{
InputStream is = null;
URLConnection connection = null;
try {
connection = HttpUrlConnectionHelper.redirectConnection(new URL(url));
is = connection.getInputStream();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
logger.log(Level.SEVERE, "Error during connecting to event structure url "+url, e);
}
return new InputStreamReader(is, "UTF-8");
}
}