Rendering for Binary Resources
This commit is contained in:
parent
2845078c9e
commit
66fb0893ad
|
@ -0,0 +1,134 @@
|
|||
package org.hl7.fhir.r5.renderers;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.hl7.fhir.r5.model.Binary;
|
||||
import org.hl7.fhir.utilities.TextFile;
|
||||
import org.hl7.fhir.utilities.Utilities;
|
||||
import org.hl7.fhir.utilities.xhtml.NodeType;
|
||||
import org.hl7.fhir.utilities.xhtml.XhtmlComposer;
|
||||
import org.hl7.fhir.utilities.xhtml.XhtmlNode;
|
||||
|
||||
public class BinaryRenderer {
|
||||
|
||||
private String folder;
|
||||
private List<String> filenames = new ArrayList<String>();
|
||||
|
||||
public BinaryRenderer(String folder) {
|
||||
super();
|
||||
this.folder = folder;
|
||||
}
|
||||
|
||||
public String getFolder() {
|
||||
return folder;
|
||||
}
|
||||
|
||||
public List<String> getFilenames() {
|
||||
return filenames;
|
||||
}
|
||||
|
||||
public static String getBinContentAsString(Binary bin) {
|
||||
// for now, assume UTF8. To do: extract character encoding from mime type if possible (charset)
|
||||
return new String(bin.getContent(), StandardCharsets.UTF_8);
|
||||
}
|
||||
|
||||
public String display(Binary bin) throws IOException {
|
||||
XhtmlNode div = new XhtmlNode(NodeType.Element, "div");
|
||||
render(div, bin);
|
||||
return new XhtmlComposer(false, true).compose(div);
|
||||
}
|
||||
|
||||
|
||||
public void render(XhtmlNode x, Binary bin) throws IOException {
|
||||
filenames.clear();
|
||||
if (!bin.hasContentType()) {
|
||||
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 {
|
||||
error(x, "The Content Type '"+bin.getContentType()+"' is not rendered in this context");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void image(XhtmlNode x, Binary bin) throws IOException {
|
||||
String ext = null;
|
||||
if (bin.getContentType().startsWith("image/png")) {
|
||||
ext = ".png";
|
||||
} else if (bin.getContentType().startsWith("image/jpeg")) {
|
||||
ext = ".jpg";
|
||||
} else if (bin.getContentType().startsWith("image/gif")) {
|
||||
ext = ".gif";
|
||||
} else if (bin.getContentType().startsWith("image/svg")) {
|
||||
ext = ".svg";
|
||||
}
|
||||
|
||||
if (ext == null) {
|
||||
error(x, "The Image Type '"+bin.getContentType()+"' is not rendered in this context");
|
||||
} else {
|
||||
String fn = "Binary-Native-"+bin.getId()+ext;
|
||||
TextFile.bytesToFile(bin.getContent(), Utilities.path(folder, fn));
|
||||
filenames.add(fn);
|
||||
x.img("Binary-Native-"+bin.getId()+ext);
|
||||
}
|
||||
}
|
||||
|
||||
private void error(XhtmlNode x, String message) {
|
||||
x.tx("["+message+"]");
|
||||
}
|
||||
|
||||
private boolean isXml(String ct) {
|
||||
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);
|
||||
XhtmlNode pre = x.pre("xml");
|
||||
pre.code(content);
|
||||
}
|
||||
|
||||
private boolean isJson(String ct) {
|
||||
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);
|
||||
XhtmlNode pre = x.pre("json");
|
||||
pre.code(content);
|
||||
}
|
||||
|
||||
private boolean isTtl(String ct) {
|
||||
return ct.startsWith("text/rdf") || ct.contains("+turtle");
|
||||
}
|
||||
|
||||
private void ttl(XhtmlNode x, Binary bin) {
|
||||
String content = "\r\n"+getBinContentAsString(bin);
|
||||
XhtmlNode pre = x.pre("rdf language-turtle");
|
||||
pre.code(content);
|
||||
}
|
||||
|
||||
|
||||
private boolean isText(String ct) {
|
||||
return ct.startsWith("text/");
|
||||
}
|
||||
|
||||
private void text(XhtmlNode x, Binary bin) {
|
||||
String content = "\r\n"+getBinContentAsString(bin);
|
||||
XhtmlNode pre = x.pre();
|
||||
pre.code(content);
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -148,6 +148,37 @@ public class DataRenderer extends Renderer {
|
|||
return system;
|
||||
}
|
||||
|
||||
public String displaySystem(String system) {
|
||||
if (system == null)
|
||||
return "[not stated]";
|
||||
if (system.equals("http://loinc.org"))
|
||||
return "LOINC";
|
||||
if (system.startsWith("http://snomed.info"))
|
||||
return "SNOMED CT";
|
||||
if (system.equals("http://www.nlm.nih.gov/research/umls/rxnorm"))
|
||||
return "RxNorm";
|
||||
if (system.equals("http://hl7.org/fhir/sid/icd-9"))
|
||||
return "ICD-9";
|
||||
if (system.equals("http://dicom.nema.org/resources/ontology/DCM"))
|
||||
return "DICOM";
|
||||
if (system.equals("http://unitsofmeasure.org"))
|
||||
return "UCUM";
|
||||
|
||||
CodeSystem cs = context.getContext().fetchCodeSystem(system);
|
||||
if (cs != null) {
|
||||
return cs.present();
|
||||
}
|
||||
return tails(system);
|
||||
}
|
||||
|
||||
private String tails(String system) {
|
||||
if (system.contains("/")) {
|
||||
return system.substring(system.lastIndexOf("/")+1);
|
||||
} else {
|
||||
return "unknown";
|
||||
}
|
||||
}
|
||||
|
||||
protected String makeAnchor(String codeSystem, String code) {
|
||||
String s = codeSystem+'-'+code;
|
||||
StringBuilder b = new StringBuilder();
|
||||
|
|
|
@ -82,7 +82,7 @@ public class RenderingContext {
|
|||
private boolean pretty;
|
||||
private boolean header;
|
||||
|
||||
private ValidationOptions terminologyServiceOptions;
|
||||
private ValidationOptions terminologyServiceOptions = new ValidationOptions();
|
||||
private boolean noSlowLookup;
|
||||
private String tooCostlyNoteEmpty;
|
||||
private String tooCostlyNoteNotEmpty;
|
||||
|
@ -115,7 +115,9 @@ public class RenderingContext {
|
|||
this.specificationLink = specLink;
|
||||
this.localPrefix = localPrefix;
|
||||
this.mode = mode;
|
||||
this.terminologyServiceOptions = terminologyServiceOptions;
|
||||
if (terminologyServiceOptions != null) {
|
||||
this.terminologyServiceOptions = terminologyServiceOptions;
|
||||
}
|
||||
profileUtilities = new ProfileUtilities(worker, null, null);
|
||||
}
|
||||
|
||||
|
|
|
@ -191,6 +191,7 @@ public class ToolingExtensions {
|
|||
public static final String EXT_XML_NAMESPACE = "http://hl7.org/fhir/StructureDefinition/elementdefinition-namespace";
|
||||
public static final String EXT_XML_NAME = "http://hl7.org/fhir/StructureDefinition/elementdefinition-xml-name";
|
||||
public static final String EXT_BINDING_STYLE = "http://hl7.org/fhir/StructureDefinition/elementdefinition-binding-style";
|
||||
public static final String EXT_BINARY_FORMAT = "http://hl7.org/fhir/StructureDefinition/implementationguide-resource-format";
|
||||
|
||||
// specific extension helpers
|
||||
|
||||
|
|
|
@ -606,6 +606,10 @@ public class XhtmlNode implements IBaseXhtml {
|
|||
return addTag("pre");
|
||||
}
|
||||
|
||||
public XhtmlNode pre(String clss) {
|
||||
return addTag("pre").setAttribute("class", clss);
|
||||
}
|
||||
|
||||
public void br() {
|
||||
addTag("br");
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue