Use document for structured email

pull/214/head
M66B 1 year ago
parent ba16b871e3
commit 7036d3b143

@ -25,6 +25,7 @@ import org.json.JSONArray;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import org.jsoup.nodes.Document; import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import java.util.Iterator; import java.util.Iterator;
@ -40,18 +41,19 @@ public class StructuredEmail {
public String getHtml(Context context) throws JSONException { public String getHtml(Context context) throws JSONException {
try { try {
StringBuilder sb = new StringBuilder();
getHtml(jroot, 0, sb);
Document d = Document.createShell(""); Document d = Document.createShell("");
d.body().appendElement("hr"); d.body().appendElement("hr");
d.body().appendElement("div") d.body().appendElement("div")
.attr("style", "font-size: larger !important;") .attr("style",
.text("Linked Data"); "font-family: monospace; font-size: larger !important;")
.appendElement("a")
.attr("href", "https://json-ld.org/")
.text("Linked data");
d.body().appendElement("br"); d.body().appendElement("br");
d.body().appendElement("div") Element holder = d.body().appendElement("div")
.attr("style", "font-size: smaller !important;") .attr("style",
.html(HtmlHelper.formatPlainText(sb.toString())); "font-family: monospace; font-size: smaller !important;");
getHtml(jroot, 0, holder);
d.body().appendElement("hr"); d.body().appendElement("hr");
return d.body().html(); return d.body().html();
} catch (Throwable ex) { } catch (Throwable ex) {
@ -62,7 +64,7 @@ public class StructuredEmail {
} }
} }
private void getHtml(Object obj, int indent, StringBuilder sb) throws JSONException { private void getHtml(Object obj, int indent, Element holder) throws JSONException {
if (obj instanceof JSONObject) { if (obj instanceof JSONObject) {
JSONObject jobject = (JSONObject) obj; JSONObject jobject = (JSONObject) obj;
Iterator<String> keys = jobject.keys(); Iterator<String> keys = jobject.keys();
@ -70,28 +72,28 @@ public class StructuredEmail {
String key = keys.next(); String key = keys.next();
if (key == null) if (key == null)
continue; continue;
indent(indent, holder);
Object v = (jobject.isNull(key) ? "" : jobject.get(key)); Object v = (jobject.isNull(key) ? "" : jobject.get(key));
holder.appendElement("strong")
.text(unCamelCase(key) + ": ");
if (v instanceof JSONObject || v instanceof JSONArray) { if (v instanceof JSONObject || v instanceof JSONArray) {
sb.append(unCamelCase(key)) holder.appendElement("br");
.append(':') getHtml(v, indent + 1, holder);
.append('\n');
getHtml(v, indent + 1, sb);
} else { } else {
sb.append(indent(indent)) holder.appendElement("span").text(v.toString());
.append(unCamelCase(key)) holder.appendElement("br");
.append(": ")
.append(v)
.append('\n');
} }
} }
} else if (obj instanceof JSONArray) { } else if (obj instanceof JSONArray) {
JSONArray jarray = (JSONArray) obj; JSONArray jarray = (JSONArray) obj;
for (int i = 0; i < jarray.length(); i++) for (int i = 0; i < jarray.length(); i++)
getHtml(jarray.get(i), indent + 1, sb); getHtml(jarray.get(i), indent, holder);
} else { } else {
sb.append(indent(indent)) indent(indent, holder);
.append(obj == null ? "" : obj.toString()) holder.appendElement("span").text(obj == null ? "" : obj.toString());
.append('\n'); holder.appendElement("br");
} }
} }
@ -115,10 +117,11 @@ public class StructuredEmail {
return sb.toString(); return sb.toString();
} }
private static String indent(int count) { private static void indent(int count, Element holder) {
StringBuilder sb = new StringBuilder(); if (count > 0) {
Element span = holder.appendElement("span");
for (int i = 0; i < count; i++) for (int i = 0; i < count; i++)
sb.append(" "); span.append("&nbsp;&nbsp;");
return sb.toString(); }
} }
} }

Loading…
Cancel
Save