Refactoring

This commit is contained in:
M66B
2023-11-28 12:29:08 +01:00
parent 7036d3b143
commit 66b5971847
5 changed files with 9 additions and 9 deletions

View File

@@ -0,0 +1,129 @@
package eu.faircode.email;
/*
This file is part of FairEmail.
FairEmail is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
FairEmail is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with FairEmail. If not, see <http://www.gnu.org/licenses/>.
Copyright 2018-2023 by Marcel Bokhorst (M66B)
*/
import android.content.Context;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import java.util.Iterator;
// https://json-ld.org/
// https://schema.org/
// https://schema.org/FlightReservation
// https://structured.email/content/introduction/getting_started.html
public class JsonLd {
private final JSONObject jroot;
public JsonLd(String json) throws JSONException {
jroot = new JSONObject(json);
}
public String getHtml(Context context) throws JSONException {
try {
Document d = Document.createShell("");
d.body().appendElement("hr");
d.body().appendElement("div")
.attr("style",
"font-family: monospace; font-size: larger !important;")
.appendElement("a")
.attr("href", "https://json-ld.org/")
.text("Linked data");
d.body().appendElement("br");
Element holder = d.body().appendElement("div")
.attr("style",
"font-family: monospace; font-size: smaller !important;");
getHtml(jroot, 0, holder);
d.body().appendElement("hr");
return d.body().html();
} catch (Throwable ex) {
Log.e(ex);
Document d = Document.createShell("");
d.body().append("pre").text(Log.formatThrowable(ex, false));
return d.body().html();
}
}
private void getHtml(Object obj, int indent, Element holder) throws JSONException {
if (obj instanceof JSONObject) {
JSONObject jobject = (JSONObject) obj;
Iterator<String> keys = jobject.keys();
while (keys.hasNext()) {
String key = keys.next();
if (key == null)
continue;
indent(indent, holder);
Object v = (jobject.isNull(key) ? "" : jobject.get(key));
holder.appendElement("strong")
.text(unCamelCase(key) + ": ");
if (v instanceof JSONObject || v instanceof JSONArray) {
holder.appendElement("br");
getHtml(v, indent + 1, holder);
} else {
holder.appendElement("span").text(v.toString());
holder.appendElement("br");
}
}
} else if (obj instanceof JSONArray) {
JSONArray jarray = (JSONArray) obj;
for (int i = 0; i < jarray.length(); i++)
getHtml(jarray.get(i), indent, holder);
} else {
indent(indent, holder);
holder.appendElement("span").text(obj == null ? "" : obj.toString());
holder.appendElement("br");
}
}
private static String unCamelCase(String key) {
boolean split = false;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < key.length(); i++) {
char kar = key.charAt(i);
if (Character.isUpperCase(kar)) {
if (split)
sb.append(kar);
else {
split = true;
sb.append(' ').append(Character.toLowerCase(kar));
}
} else {
split = false;
sb.append(kar);
}
}
return sb.toString();
}
private static void indent(int count, Element holder) {
if (count > 0) {
Element span = holder.appendElement("span");
for (int i = 0; i < count; i++)
span.append("&nbsp;&nbsp;");
}
}
}