private void composeList(String path, List<Element> list) throws IOException { // there will be at least one element String en = getFormalName(list.get(0)); openArray(en); for (Element item : list) { open(null); json.name("index"); json.value(item.getIndex()); if (item.isPrimitive() || isPrimitive(item.getType())) { if (item.hasValue()) primitiveValue(item); } if (item.getProperty().isResource()) { prop("@type", "fhir:" + item.getType()); } Set<String> done = new HashSet<String>(); for (Element child : item.getChildren()) { compose(path + "." + item.getName(), item, done, child); } if ("Coding".equals(item.getType())) decorateCoding(item); if ("CodeableConcept".equals(item.getType())) decorateCodeableConcept(item); if ("Reference".equals(item.getType())) decorateReference(item); close(); } closeArray(); }
private void compose(String path, Element element) throws IOException { Property p = element.hasElementProperty() ? element.getElementProperty() : element.getProperty(); String en = getFormalName(element); if (element.fhirType().equals("xhtml")) { json.name(en); json.value(element.getValue()); } else if (element.hasChildren() || element.hasComments() || element.hasValue()) { open(en); if (element.getProperty().isResource()) { prop("@type", "fhir:" + element.getType()); // element = element.getChildren().get(0); } if (element.isPrimitive() || isPrimitive(element.getType())) { if (element.hasValue()) primitiveValue(element); } Set<String> done = new HashSet<String>(); for (Element child : element.getChildren()) { compose(path + "." + element.getName(), element, done, child); } if ("Coding".equals(element.getType())) decorateCoding(element); if ("CodeableConcept".equals(element.getType())) decorateCodeableConcept(element); if ("Reference".equals(element.getType())) decorateReference(element); close(); } }
@Override public void compose(Element e, OutputStream stream, OutputStyle style, String base) throws Exception { this.base = base; OutputStreamWriter osw = new OutputStreamWriter(stream, "UTF-8"); if (style == OutputStyle.CANONICAL) json = new JsonCreatorCanonical(osw); else json = new JsonCreatorGson(osw); json.setIndent(style == OutputStyle.PRETTY ? " " : ""); json.beginObject(); prop("@type", "fhir:" + e.getType()); prop("@context", jsonLDBase + "fhir.jsonld"); prop("role", "fhir:treeRoot"); String id = e.getChildValue("id"); if (base != null && id != null) { if (base.endsWith("#")) prop("@id", base + e.getType() + "-" + id + ">"); else prop("@id", Utilities.pathReverse(base, e.getType(), id)); } Set<String> done = new HashSet<String>(); for (Element child : e.getChildren()) { compose(e.getName(), e, done, child); } json.endObject(); json.finish(); osw.flush(); }
private void decorateReference(Element element) throws IOException { String ref = element.getChildValue("reference"); if (ref != null && (ref.startsWith("http://") || ref.startsWith("https://"))) { json.name("link"); json.value(ref); } else if (base != null && ref != null && ref.contains("/")) { json.name("link"); json.value(Utilities.pathReverse(base, ref)); } }
protected void decorateCoding(Element coding) throws IOException { String system = coding.getChildValue("system"); String code = coding.getChildValue("code"); if (system == null) return; if ("http://snomed.info/sct".equals(system)) { json.name("concept"); json.value("http://snomed.info/id/" + code); } else if ("http://loinc.org".equals(system)) { json.name("concept"); json.value("http://loinc.org/owl#" + code); } }
protected void closeArray() throws IOException { json.endArray(); }
protected void openArray(String name) throws IOException { if (name != null) json.name(name); json.beginArray(); }
protected void close() throws IOException { json.endObject(); }
protected void prop(String name, String value) throws IOException { if (name != null) json.name(name); json.value(value); }
private void primitiveValue(Element item) throws IOException { String type = item.getType(); if (Utilities.existsInList(type, "date", "dateTime", "instant")) { String v = item.getValue(); if (v.length() > 10) { int i = v.substring(10).indexOf("-"); if (i == -1) i = v.substring(10).indexOf("+"); v = i == -1 ? v : v.substring(0, 10 + i); } if (v.length() > 10) json.name("dateTime"); else if (v.length() == 10) json.name("date"); else if (v.length() == 7) json.name("gYearMonth"); else if (v.length() == 4) json.name("gYear"); json.value(item.getValue()); } else if (Utilities.existsInList(type, "boolean")) { json.name("boolean"); json.value(item.getValue().equals("true") ? new Boolean(true) : new Boolean(false)); } else if (Utilities.existsInList(type, "integer", "unsignedInt", "positiveInt")) { json.name("integer"); json.value(new Integer(item.getValue())); } else if (Utilities.existsInList(type, "decimal")) { json.name("decimal"); json.value(item.getValue()); } else if (Utilities.existsInList(type, "base64Binary")) { json.name("binary"); json.value(item.getValue()); } else { json.name("value"); json.value(item.getValue()); } }