Added support for tracking whether elements are ellipsed and for rendering XHTML versions of XML and JSON content with ellipsed elements

This commit is contained in:
Lloyd McKenzie 2024-08-29 21:17:20 -06:00
parent 2721ebb8c6
commit 975653e07d
7 changed files with 830 additions and 695 deletions

View File

@ -162,6 +162,7 @@ public class Element extends Base implements NamedItem {
private FhirFormat format;
private Object nativeObject;
private List<SliceDefinition> sliceDefinitions;
private boolean ellipsed;
public Element(String name) {
super();
@ -1429,6 +1430,8 @@ public class Element extends Base implements NamedItem {
public Base copy() {
Element element = new Element(this);
this.copyValues(element);
if (this.isEllipsed())
element.setEllipsed(true);
return element;
}
@ -1638,4 +1641,11 @@ public class Element extends Base implements NamedItem {
return FhirPublication.fromCode(property.getStructure().getVersion());
}
public void setEllipsed(boolean ellipsed) {
this.ellipsed = ellipsed;
}
public boolean isEllipsed() {
return this.ellipsed;
}
}

View File

@ -62,6 +62,7 @@ import org.hl7.fhir.r5.formats.JsonCreatorDirect;
import org.hl7.fhir.r5.model.ElementDefinition.TypeRefComponent;
import org.hl7.fhir.r5.model.ElementDefinition;
import org.hl7.fhir.r5.model.StructureDefinition;
import org.hl7.fhir.r5.utils.ToolingExtensions;
import org.hl7.fhir.utilities.CommaSeparatedStringBuilder;
import org.hl7.fhir.utilities.StringPair;
import org.hl7.fhir.utilities.TextFile;
@ -85,6 +86,8 @@ public class JsonParser extends ParserBase {
private JsonCreator json;
private boolean allowComments;
private boolean ellipseElements;
private boolean suppressResourceType;
private Element baseElement;
private boolean markedXhtml;
@ -782,6 +785,7 @@ public class JsonParser extends ParserBase {
}
checkComposeComments(e);
json.beginObject();
if (!isSuppressResourceType())
prop("resourceType", e.getType(), null);
Set<String> done = new HashSet<String>();
for (Element child : e.getChildren()) {
@ -807,6 +811,7 @@ public class JsonParser extends ParserBase {
checkComposeComments(e);
json.beginObject();
if (!isSuppressResourceType())
prop("resourceType", e.getType(), linkResolver == null ? null : linkResolver.resolveProperty(e.getProperty()));
Set<String> done = new HashSet<String>();
for (Element child : e.getChildren()) {
@ -821,15 +826,50 @@ public class JsonParser extends ParserBase {
if (wantCompose(path, child)) {
boolean isList = child.hasElementProperty() ? child.getElementProperty().isList() : child.getProperty().isList();
if (!isList) {// for specials, ignore the cardinality of the stated type
if (child.isEllipsed() && ellipseElements)
json.ellipse();
else
compose(path, child);
} else if (!done.contains(child.getName())) {
done.add(child.getName());
List<Element> list = e.getChildrenByName(child.getName());
if (child.getProperty().getDefinition().hasExtension(ToolingExtensions.EXT_JSON_PROP_KEY))
composeKeyList(path, list);
else
composeList(path, list);
}
}
}
private void composeKeyList(String path, List<Element> list) throws IOException {
String keyName = list.get(0).getProperty().getDefinition().getExtensionString(ToolingExtensions.EXT_JSON_PROP_KEY);
json.name(list.get(0).getName());
json.beginObject();
for (Element e: list) {
Element key = null;
Element value = null;
for (Element child: e.getChildren()) {
if (child.getName().equals(keyName))
key = child;
else
value = child;
}
if (value.isPrimitive())
primitiveValue(key.getValue(), value);
else {
json.name(key.getValue());
checkComposeComments(e);
json.beginObject();
Set<String> done = new HashSet<String>();
for (Element child : value.getChildren()) {
compose(value.getName(), value, done, child);
}
json.endObject();
compose(path + "." + key.getValue(), value);
}
}
json.endObject();
}
private void composeList(String path, List<Element> list) throws IOException {
// there will be at least one element
@ -847,7 +887,9 @@ public class JsonParser extends ParserBase {
if (prim) {
openArray(name, linkResolver == null ? null : linkResolver.resolveProperty(list.get(0).getProperty()));
for (Element item : list) {
if (item.hasValue()) {
if (item.isEllipsed())
json.ellipse();
else if (item.hasValue()) {
if (linkResolver != null && item.getProperty().isReference()) {
String ref = linkResolver.resolveReference(getReferenceForElement(item));
if (ref != null) {
@ -866,7 +908,9 @@ public class JsonParser extends ParserBase {
openArray(name, linkResolver == null ? null : linkResolver.resolveProperty(list.get(0).getProperty()));
int i = 0;
for (Element item : list) {
if (item.hasChildren()) {
if (item.isEllipsed())
json.ellipse();
else if (item.hasChildren()) {
open(null,null);
if (item.getProperty().isResource()) {
prop("resourceType", item.getType(), linkResolver == null ? null : linkResolver.resolveType(item.getType()));
@ -933,6 +977,7 @@ public class JsonParser extends ParserBase {
json.externalLink(ref);
}
}
Set<String> done = new HashSet<String>();
for (Element child : element.getChildren()) {
compose(path + "." + element.getName(), element, done, child);
@ -951,5 +996,23 @@ public class JsonParser extends ParserBase {
return this;
}
public boolean isEllipseElements() {
return ellipseElements;
}
public JsonParser setEllipseElements(boolean ellipseElements) {
this.ellipseElements = ellipseElements;
return this;
}
public boolean isSuppressResourceType() {
return suppressResourceType;
}
public JsonParser setSuppressResourceType(boolean suppressResourceType) {
this.suppressResourceType = suppressResourceType;
return this;
}
}

View File

@ -94,6 +94,7 @@ import org.xml.sax.XMLReader;
public class XmlParser extends ParserBase {
private boolean allowXsiLocation;
private String version;
private boolean ellipseElements;
public XmlParser(IWorkerContext context) {
super(context);
@ -805,6 +806,7 @@ public class XmlParser extends ParserBase {
}
private void composeElement(IXMLWriter xml, Element element, String elementName, boolean root) throws IOException, FHIRException {
if (!(isEllipseElements() && element.isEllipsed())) {
if (showDecorations) {
@SuppressWarnings("unchecked")
List<ElementDecoration> decorations = (List<ElementDecoration>) element.getUserData("fhir.decorations");
@ -815,7 +817,11 @@ public class XmlParser extends ParserBase {
for (String s : element.getComments()) {
xml.comment(s, true);
}
}
if (isText(element.getProperty())) {
if (isEllipseElements() && element.isEllipsed())
xml.ellipse();
else {
if (linkResolver != null)
xml.link(linkResolver.resolveProperty(element.getProperty()));
xml.enter(element.getProperty().getXmlNamespace(),elementName);
@ -827,12 +833,20 @@ public class XmlParser extends ParserBase {
}
xml.text(element.getValue());
xml.exit(element.getProperty().getXmlNamespace(),elementName);
}
} else if (!element.hasChildren() && !element.hasValue()) {
if (isEllipseElements() && element.isEllipsed())
xml.ellipse();
else {
if (element.getExplicitType() != null)
xml.attribute("xsi:type", element.getExplicitType());
xml.element(elementName);
}
} else if (element.isPrimitive() || (element.hasType() && isPrimitive(element.getType()))) {
if (element.getType().equals("xhtml")) {
if (isEllipseElements() && element.isEllipsed())
xml.ellipse();
else {
String rawXhtml = element.getValue();
if (isCdaText(element.getProperty())) {
new CDANarrativeFormat().convert(xml, new XhtmlParser().parseFragment(rawXhtml));
@ -843,11 +857,19 @@ public class XmlParser extends ParserBase {
markedXhtml = true;
}
}
}
} else if (isText(element.getProperty())) {
if (isEllipseElements() && element.isEllipsed())
xml.ellipse();
else {
if (linkResolver != null)
xml.link(linkResolver.resolveProperty(element.getProperty()));
xml.text(element.getValue());
}
} else {
if (isEllipseElements() && element.isEllipsed())
xml.attributeEllipse();
else {
setXsiTypeIfIsTypeAttr(xml, element);
if (element.hasValue()) {
if (linkResolver != null)
@ -870,16 +892,26 @@ public class XmlParser extends ParserBase {
} else
xml.element(elementName);
}
}
} else {
if (isEllipseElements() && element.isEllipsed())
xml.ellipse();
else {
setXsiTypeIfIsTypeAttr(xml, element);
Set<String> handled = new HashSet<>();
for (Element child : element.getChildren()) {
if (!handled.contains(child.getName()) && isAttr(child.getProperty()) && wantCompose(element.getPath(), child)) {
handled.add(child.getName());
if (isEllipseElements() && child.isEllipsed())
xml.attributeEllipse();
else {
String av = child.getValue();
if (child.getProperty().isList()) {
for (Element c2 : element.getChildren()) {
if (c2 != child && c2.getName().equals(child.getName())) {
if (c2.isEllipsed())
av = av + " ...";
else
av = av + " " + c2.getValue();
}
}
@ -891,6 +923,8 @@ public class XmlParser extends ParserBase {
xml.attribute(child.getProperty().getXmlNamespace(), child.getProperty().getXmlName(), av);
}
}
}
}
if (!element.getProperty().getDefinition().hasExtension(ToolingExtensions.EXT_ID_CHOICE_GROUP)) {
if (linkResolver != null)
xml.link(linkResolver.resolveProperty(element.getProperty()));
@ -914,6 +948,9 @@ public class XmlParser extends ParserBase {
}
for (Element child : element.getChildren()) {
if (wantCompose(element.getPath(), child)) {
if (isEllipseElements() && child.isEllipsed())
xml.ellipse();
else {
if (isText(child.getProperty())) {
if (linkResolver != null)
xml.link(linkResolver.resolveProperty(element.getProperty()));
@ -923,6 +960,7 @@ public class XmlParser extends ParserBase {
}
}
}
}
if (!element.getProperty().getDefinition().hasExtension(ToolingExtensions.EXT_ID_CHOICE_GROUP)) {
if (!root && element.getSpecial() != null)
xml.exit(element.getProperty().getXmlNamespace(),element.getType());
@ -1034,4 +1072,13 @@ public class XmlParser extends ParserBase {
// do nothing
}
}
public boolean isEllipseElements() {
return ellipseElements;
}
public void setEllipseElements(boolean ellipseElements) {
this.ellipseElements = ellipseElements;
}
}

View File

@ -71,4 +71,5 @@ public interface JsonCreator {
void link(String href);
void anchor(String string);
void externalLink(String string);
void ellipse();
}

View File

@ -280,5 +280,9 @@ public class JsonCreatorCanonical implements JsonCreator {
// not used
}
@Override
public void ellipse() {
// not used
}
}

View File

@ -243,4 +243,9 @@ public class JsonCreatorDirect implements JsonCreator {
public void externalLink(String string) {
// not used
}
@Override
public void ellipse() {
// not used
}
}

View File

@ -127,4 +127,9 @@ public class JsonCreatorGson implements JsonCreator {
// not used
}
@Override
public void ellipse() {
// not used
}
}