Added fail-safe

pull/214/head
M66B 1 year ago
parent 0b2f2d81f6
commit 210d8b717b

@ -41,141 +41,149 @@ public class StructuredEmail {
} }
public String getHtml(Context context) throws JSONException { public String getHtml(Context context) throws JSONException {
String title; try {
StringBuilder sb = new StringBuilder(); String title;
StringBuilder sb = new StringBuilder();
if ("https://schema.org".equals(jroot.optString("@context")) &&
"FlightReservation".equals(jroot.optString("@type"))) { if ("https://schema.org".equals(jroot.optString("@context")) &&
// https://www.schema.org/FlightReservation "FlightReservation".equals(jroot.optString("@type"))) {
title = "Flight reservation"; // https://www.schema.org/FlightReservation
title = "Flight reservation";
sb.append("Reservation #");
if (jroot.has("reservationId")) sb.append("Reservation #");
sb.append(jroot.getString("reservationId")); if (jroot.has("reservationId"))
sb.append('\n'); sb.append(jroot.getString("reservationId"));
sb.append('\n');
sb.append("Passenger: ");
if (jroot.has("underName")) { sb.append("Passenger: ");
JSONObject jundername = jroot.getJSONObject("underName"); if (jroot.has("underName")) {
if ("Person".equals(jundername.optString("@type")) && JSONObject jundername = jroot.getJSONObject("underName");
jundername.has("name")) if ("Person".equals(jundername.optString("@type")) &&
sb.append(jundername.getString("name")); jundername.has("name"))
} sb.append(jundername.getString("name"));
sb.append('\n');
sb.append("Flight: ");
if (jroot.has("reservationFor")) {
JSONObject jreservation = jroot.getJSONObject("reservationFor");
if ("Flight".equals(jreservation.optString("@type"))) {
if (jreservation.has("flightNumber"))
sb.append(jreservation.getString("flightNumber"));
} }
} sb.append('\n');
sb.append('\n');
sb.append("Flight: ");
sb.append("Operated by: "); if (jroot.has("reservationFor")) {
if (jroot.has("reservationFor")) { JSONObject jreservation = jroot.getJSONObject("reservationFor");
JSONObject jreservation = jroot.getJSONObject("reservationFor"); if ("Flight".equals(jreservation.optString("@type"))) {
if ("Flight".equals(jreservation.optString("@type"))) { if (jreservation.has("flightNumber"))
if (jreservation.has("provider")) { sb.append(jreservation.getString("flightNumber"));
JSONObject jprovider = jreservation.getJSONObject("provider");
if (jprovider.has("name"))
sb.append(jprovider.getString("name"));
} }
} }
} sb.append('\n');
sb.append('\n');
sb.append("Operated by: ");
sb.append("Departing: "); if (jroot.has("reservationFor")) {
if (jroot.has("reservationFor")) { JSONObject jreservation = jroot.getJSONObject("reservationFor");
JSONObject jreservation = jroot.getJSONObject("reservationFor"); if ("Flight".equals(jreservation.optString("@type"))) {
if (jreservation.has("departureAirport")) { if (jreservation.has("provider")) {
JSONObject jdeparture = jreservation.getJSONObject("departureAirport"); JSONObject jprovider = jreservation.getJSONObject("provider");
if ("Airport".equals(jdeparture.getString("@type"))) { if (jprovider.has("name"))
if (jdeparture.has("name")) sb.append(jprovider.getString("name"));
sb.append(jdeparture.getString("name")).append(' '); }
if (jdeparture.has("iataCode"))
sb.append('(').append(jdeparture.getString("iataCode")).append(") ");
} }
} }
if (jreservation.has("departureTime")) sb.append('\n');
sb.append(jreservation.getString("departureTime"));
} sb.append("Departing: ");
sb.append('\n'); if (jroot.has("reservationFor")) {
JSONObject jreservation = jroot.getJSONObject("reservationFor");
sb.append("Arriving: "); if (jreservation.has("departureAirport")) {
if (jroot.has("reservationFor")) { JSONObject jdeparture = jreservation.getJSONObject("departureAirport");
JSONObject jreservation = jroot.getJSONObject("reservationFor"); if ("Airport".equals(jdeparture.getString("@type"))) {
if (jreservation.has("arrivalAirport")) { if (jdeparture.has("name"))
JSONObject jarrival = jreservation.getJSONObject("arrivalAirport"); sb.append(jdeparture.getString("name")).append(' ');
if ("Airport".equals(jarrival.getString("@type"))) { if (jdeparture.has("iataCode"))
if (jarrival.has("name")) sb.append('(').append(jdeparture.getString("iataCode")).append(") ");
sb.append(jarrival.getString("name")).append(' '); }
if (jarrival.has("iataCode"))
sb.append('(').append(jarrival.getString("iataCode")).append(") ");
} }
if (jreservation.has("departureTime"))
sb.append(jreservation.getString("departureTime"));
} }
if (jreservation.has("arrivalTime")) sb.append('\n');
sb.append(jreservation.getString("arrivalTime"));
} sb.append("Arriving: ");
sb.append('\n'); if (jroot.has("reservationFor")) {
JSONObject jreservation = jroot.getJSONObject("reservationFor");
sb.append("Passenger sequence number: "); if (jreservation.has("arrivalAirport")) {
if (jroot.has("passengerSequenceNumber")) JSONObject jarrival = jreservation.getJSONObject("arrivalAirport");
sb.append(jroot.getString("passengerSequenceNumber")); if ("Airport".equals(jarrival.getString("@type"))) {
sb.append('\n'); if (jarrival.has("name"))
sb.append(jarrival.getString("name")).append(' ');
sb.append("Boarding priority: "); if (jarrival.has("iataCode"))
if (jroot.has("passengerPriorityStatus")) sb.append('(').append(jarrival.getString("iataCode")).append(") ");
sb.append(jroot.getString("passengerPriorityStatus")); }
sb.append('\n'); }
if (jreservation.has("arrivalTime"))
sb.append("Boarding policy: "); sb.append(jreservation.getString("arrivalTime"));
if (jroot.has("reservationFor")) { }
JSONObject jreservation = jroot.getJSONObject("reservationFor"); sb.append('\n');
if ("Flight".equals(jreservation.optString("@type"))) {
if (jreservation.has("provider")) { sb.append("Passenger sequence number: ");
JSONObject jprovider = jreservation.getJSONObject("provider"); if (jroot.has("passengerSequenceNumber"))
if (jprovider.has("boardingPolicy")) { sb.append(jroot.getString("passengerSequenceNumber"));
String policy = jprovider.getString("boardingPolicy"); sb.append('\n');
try {
Uri p = Uri.parse(policy); sb.append("Boarding priority: ");
String path = p.getPath(); if (jroot.has("passengerPriorityStatus"))
if (!TextUtils.isEmpty(path)) { sb.append(jroot.getString("passengerPriorityStatus"));
path = path.substring(1); sb.append('\n');
if (!TextUtils.isEmpty(path))
policy = split(path); sb.append("Boarding policy: ");
if (jroot.has("reservationFor")) {
JSONObject jreservation = jroot.getJSONObject("reservationFor");
if ("Flight".equals(jreservation.optString("@type"))) {
if (jreservation.has("provider")) {
JSONObject jprovider = jreservation.getJSONObject("provider");
if (jprovider.has("boardingPolicy")) {
String policy = jprovider.getString("boardingPolicy");
try {
Uri p = Uri.parse(policy);
String path = p.getPath();
if (!TextUtils.isEmpty(path)) {
path = path.substring(1);
if (!TextUtils.isEmpty(path))
policy = split(path);
}
} catch (Throwable ex) {
Log.w(ex);
} }
} catch (Throwable ex) { sb.append(policy);
Log.w(ex);
} }
sb.append(policy);
} }
} }
} }
sb.append('\n');
sb.append("Security screening: ");
if (jroot.has("securityScreening"))
sb.append(jroot.getString("securityScreening"));
sb.append('\n');
} else {
title = "Linked data";
getHtml(jroot, 0, sb);
} }
sb.append('\n');
sb.append("Security screening: "); Document d = Document.createShell("");
if (jroot.has("securityScreening")) d.body().appendElement("hr");
sb.append(jroot.getString("securityScreening")); d.body().appendElement("div")
sb.append('\n'); .attr("style", "font-size: larger !important;")
} else { .text(title);
title = "Linked data"; d.body().appendElement("br");
getHtml(jroot, 0, sb); d.body().appendElement("div")
.attr("style", "font-size: smaller !important;")
.html(HtmlHelper.formatPlainText(sb.toString()));
d.body().appendElement("hr");
return d.body().html();
} catch (Throwable ex) {
Log.e(ex);
Document d = Document.createShell("");
d.body().appendElement("pre").text(Log.formatThrowable(ex, false));
return d.body().html();
} }
Document d = Document.createShell("");
d.appendElement("hr");
d.appendElement("div")
.attr("style", "font-size: larger !important;")
.text(title);
d.appendElement("br");
d.appendElement("div")
.attr("style", "font-size: smaller !important;")
.html(HtmlHelper.formatPlainText(sb.toString()));
d.appendElement("hr");
return d.html();
} }
private void getHtml(Object obj, int indent, StringBuilder sb) throws JSONException { private void getHtml(Object obj, int indent, StringBuilder sb) throws JSONException {

Loading…
Cancel
Save