fix problems rendering Binary files in IGs

This commit is contained in:
Grahame Grieve 2024-04-12 22:11:14 +10:00
parent 3d62e098c2
commit eb553df2df
1 changed files with 59 additions and 33 deletions

View File

@ -3,8 +3,10 @@ package org.hl7.fhir.r5.renderers;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Base64;
import java.util.List;
import org.hl7.fhir.r5.elementmodel.Element;
import org.hl7.fhir.r5.model.Binary;
import org.hl7.fhir.utilities.TextFile;
import org.hl7.fhir.utilities.Utilities;
@ -30,9 +32,9 @@ public class BinaryRenderer {
return filenames;
}
public static String getBinContentAsString(Binary bin) {
public static String getBinContentAsString(byte[] bin) {
// for now, assume UTF8. To do: extract character encoding from mime type if possible (charset)
return new String(bin.getContent(), StandardCharsets.UTF_8);
return new String(bin, StandardCharsets.UTF_8);
}
public String display(Binary bin) throws IOException {
@ -40,47 +42,71 @@ public class BinaryRenderer {
render(div, bin);
return new XhtmlComposer(false, true).compose(div);
}
public String display(Element bin) throws IOException {
XhtmlNode div = new XhtmlNode(NodeType.Element, "div");
render(div, bin);
return new XhtmlComposer(false, true).compose(div);
}
public String display(String id, String ct, byte[] cnt) throws IOException {
XhtmlNode div = new XhtmlNode(NodeType.Element, "div");
render(div, id, ct, cnt);
return new XhtmlComposer(false, true).compose(div);
}
public void render(XhtmlNode x, Binary bin) throws IOException {
String ct = bin.getContentType();
byte[] cnt = bin.getContent();
render(x, bin.getId(), ct, cnt);
}
public void render(XhtmlNode x, Element bin) throws IOException {
String ct = bin.getNamedChildValue("contentType");
String id = bin.getNamedChildValue("id");
byte[] cnt = Base64.getMimeDecoder().decode(bin.getNamedChildValue("data"));
render(x, id, ct, cnt);
}
public void render(XhtmlNode x, String id, String ct, byte[] cnt) throws IOException {
filenames.clear();
if (!bin.hasContentType()) {
if (ct == null) {
error(x, /*!#*/"No Content Type");
} else if (bin.getContentType().startsWith("image/")) {
image(x, bin);
} else if (isXml(bin.getContentType())) {
xml(x, bin);
} else if (isJson(bin.getContentType())) {
json(x, bin);
} else if (isTtl(bin.getContentType())) {
ttl(x, bin);
} else if (isText(bin.getContentType())) {
text(x, bin);
} else if (ct.startsWith("image/")) {
image(x, id, ct, cnt);
} else if (isXml(ct)) {
xml(x, cnt);
} else if (isJson(ct)) {
json(x, cnt);
} else if (isTtl(ct)) {
ttl(x, cnt);
} else if (isText(ct)) {
text(x, cnt);
} else {
error(x, "The Content Type '"+bin.getContentType()+"' is not rendered in this context");
error(x, "The Content Type '"+ct+"' is not rendered in this context");
}
}
private void image(XhtmlNode x, Binary bin) throws IOException {
private void image(XhtmlNode x, String id, String ct, byte[] cnt) throws IOException {
String ext = null;
if (bin.getContentType().startsWith("image/png")) {
if (ct.startsWith("image/png")) {
ext = ".png";
} else if (bin.getContentType().startsWith("image/jpeg")) {
} else if (ct.startsWith("image/jpeg")) {
ext = ".jpg";
} else if (bin.getContentType().startsWith("image/gif")) {
} else if (ct.startsWith("image/gif")) {
ext = ".gif";
} else if (bin.getContentType().startsWith("image/svg")) {
} else if (ct.startsWith("image/svg")) {
ext = ".svg";
}
if (ext == null) {
error(x, /*!#*/"The Image Type '"+bin.getContentType()+"' is not rendered in this context");
error(x, /*!#*/"The Image Type '"+ct+"' is not rendered in this context");
} else {
String fn = "Binary-Native-"+bin.getId()+ext;
TextFile.bytesToFile(bin.getContent(), Utilities.path(folder, fn));
String fn = "Binary-Native-"+id+ext;
TextFile.bytesToFile(cnt, Utilities.path(folder, fn));
filenames.add(fn);
x.img("Binary-Native-"+bin.getId()+ext, "binary");
x.img("Binary-Native-"+id+ext, "binary");
}
}
@ -92,8 +118,8 @@ public class BinaryRenderer {
return ct.startsWith("text/xml") || ct.startsWith("application/xml") || ct.contains("+xml");
}
private void xml(XhtmlNode x, Binary bin) {
String content = "\r\n"+getBinContentAsString(bin);
private void xml(XhtmlNode x, byte[] cnt) {
String content = "\r\n"+getBinContentAsString(cnt);
XhtmlNode pre = x.pre("xml");
pre.code(content);
}
@ -102,8 +128,8 @@ public class BinaryRenderer {
return ct.startsWith("text/json") || ct.startsWith("application/json") || ct.contains("+json");
}
private void json(XhtmlNode x, Binary bin) {
String content = "\r\n"+getBinContentAsString(bin);
private void json(XhtmlNode x, byte[] cnt) {
String content = "\r\n"+getBinContentAsString(cnt);
XhtmlNode pre = x.pre("json");
pre.code(content);
}
@ -112,8 +138,8 @@ public class BinaryRenderer {
return ct.startsWith("text/rdf") || ct.contains("+turtle");
}
private void ttl(XhtmlNode x, Binary bin) {
String content = "\r\n"+getBinContentAsString(bin);
private void ttl(XhtmlNode x, byte[] cnt) {
String content = "\r\n"+getBinContentAsString(cnt);
XhtmlNode pre = x.pre("rdf language-turtle");
pre.code(content);
}
@ -123,8 +149,8 @@ public class BinaryRenderer {
return ct.startsWith("text/");
}
private void text(XhtmlNode x, Binary bin) {
String content = "\r\n"+getBinContentAsString(bin);
private void text(XhtmlNode x, byte[] cnt) {
String content = "\r\n"+getBinContentAsString(cnt);
XhtmlNode pre = x.pre();
pre.code(content);
}