Merge pull request #1729 from hapifhir/EllipseSupport

Added support for tracking whether elements are ellipsed and for rend…
This commit is contained in:
Grahame Grieve 2024-09-22 08:51:39 -04:00 committed by GitHub
commit 6314ad61bb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 1822 additions and 1660 deletions

View File

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

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.TypeRefComponent;
import org.hl7.fhir.r5.model.ElementDefinition; import org.hl7.fhir.r5.model.ElementDefinition;
import org.hl7.fhir.r5.model.StructureDefinition; 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.CommaSeparatedStringBuilder;
import org.hl7.fhir.utilities.StringPair; import org.hl7.fhir.utilities.StringPair;
import org.hl7.fhir.utilities.TextFile; import org.hl7.fhir.utilities.TextFile;
@ -85,6 +86,8 @@ public class JsonParser extends ParserBase {
private JsonCreator json; private JsonCreator json;
private boolean allowComments; private boolean allowComments;
private boolean elideElements;
// private boolean suppressResourceType;
private Element baseElement; private Element baseElement;
private boolean markedXhtml; private boolean markedXhtml;
@ -782,6 +785,7 @@ public class JsonParser extends ParserBase {
} }
checkComposeComments(e); checkComposeComments(e);
json.beginObject(); json.beginObject();
// if (!isSuppressResourceType())
prop("resourceType", e.getType(), null); prop("resourceType", e.getType(), null);
Set<String> done = new HashSet<String>(); Set<String> done = new HashSet<String>();
for (Element child : e.getChildren()) { for (Element child : e.getChildren()) {
@ -807,6 +811,7 @@ public class JsonParser extends ParserBase {
checkComposeComments(e); checkComposeComments(e);
json.beginObject(); json.beginObject();
// if (!isSuppressResourceType())
prop("resourceType", e.getType(), linkResolver == null ? null : linkResolver.resolveProperty(e.getProperty())); prop("resourceType", e.getType(), linkResolver == null ? null : linkResolver.resolveProperty(e.getProperty()));
Set<String> done = new HashSet<String>(); Set<String> done = new HashSet<String>();
for (Element child : e.getChildren()) { for (Element child : e.getChildren()) {
@ -821,15 +826,50 @@ public class JsonParser extends ParserBase {
if (wantCompose(path, child)) { if (wantCompose(path, child)) {
boolean isList = child.hasElementProperty() ? child.getElementProperty().isList() : child.getProperty().isList(); boolean isList = child.hasElementProperty() ? child.getElementProperty().isList() : child.getProperty().isList();
if (!isList) {// for specials, ignore the cardinality of the stated type if (!isList) {// for specials, ignore the cardinality of the stated type
if (child.isElided() && isElideElements() && json.canElide())
json.elide();
else
compose(path, child); compose(path, child);
} else if (!done.contains(child.getName())) { } else if (!done.contains(child.getName())) {
done.add(child.getName()); done.add(child.getName());
List<Element> list = e.getChildrenByName(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); 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 { private void composeList(String path, List<Element> list) throws IOException {
// there will be at least one element // there will be at least one element
@ -847,7 +887,9 @@ public class JsonParser extends ParserBase {
if (prim) { if (prim) {
openArray(name, linkResolver == null ? null : linkResolver.resolveProperty(list.get(0).getProperty())); openArray(name, linkResolver == null ? null : linkResolver.resolveProperty(list.get(0).getProperty()));
for (Element item : list) { for (Element item : list) {
if (item.hasValue()) { if (item.isElided() && json.canElide())
json.elide();
else if (item.hasValue()) {
if (linkResolver != null && item.getProperty().isReference()) { if (linkResolver != null && item.getProperty().isReference()) {
String ref = linkResolver.resolveReference(getReferenceForElement(item)); String ref = linkResolver.resolveReference(getReferenceForElement(item));
if (ref != null) { if (ref != null) {
@ -866,7 +908,9 @@ public class JsonParser extends ParserBase {
openArray(name, linkResolver == null ? null : linkResolver.resolveProperty(list.get(0).getProperty())); openArray(name, linkResolver == null ? null : linkResolver.resolveProperty(list.get(0).getProperty()));
int i = 0; int i = 0;
for (Element item : list) { for (Element item : list) {
if (item.hasChildren()) { if (item.isElided() && json.canElide())
json.elide();
else if (item.hasChildren()) {
open(null,null); open(null,null);
if (item.getProperty().isResource()) { if (item.getProperty().isResource()) {
prop("resourceType", item.getType(), linkResolver == null ? null : linkResolver.resolveType(item.getType())); prop("resourceType", item.getType(), linkResolver == null ? null : linkResolver.resolveType(item.getType()));
@ -933,9 +977,10 @@ public class JsonParser extends ParserBase {
json.externalLink(ref); json.externalLink(ref);
} }
} }
Set<String> done = new HashSet<String>(); Set<String> done = new HashSet<String>();
for (Element child : element.getChildren()) { for (Element child : element.getChildren()) {
compose(path+"."+element.getName(), element, done, child); compose(path + "." + element.getName(), element, done, child);
} }
close(); close();
} }
@ -951,5 +996,23 @@ public class JsonParser extends ParserBase {
return this; return this;
} }
public boolean isElideElements() {
return elideElements;
}
public JsonParser setElideElements(boolean elideElements) {
this.elideElements = elideElements;
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 { public class XmlParser extends ParserBase {
private boolean allowXsiLocation; private boolean allowXsiLocation;
private String version; private String version;
private boolean elideElements;
public XmlParser(IWorkerContext context) { public XmlParser(IWorkerContext context) {
super(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 { private void composeElement(IXMLWriter xml, Element element, String elementName, boolean root) throws IOException, FHIRException {
if (!(isElideElements() && element.isElided())) {
if (showDecorations) { if (showDecorations) {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
List<ElementDecoration> decorations = (List<ElementDecoration>) element.getUserData("fhir.decorations"); List<ElementDecoration> decorations = (List<ElementDecoration>) element.getUserData("fhir.decorations");
@ -815,7 +817,11 @@ public class XmlParser extends ParserBase {
for (String s : element.getComments()) { for (String s : element.getComments()) {
xml.comment(s, true); xml.comment(s, true);
} }
}
if (isText(element.getProperty())) { if (isText(element.getProperty())) {
if (isElideElements() && element.isElided() && xml.canElide())
xml.elide();
else {
if (linkResolver != null) if (linkResolver != null)
xml.link(linkResolver.resolveProperty(element.getProperty())); xml.link(linkResolver.resolveProperty(element.getProperty()));
xml.enter(element.getProperty().getXmlNamespace(),elementName); xml.enter(element.getProperty().getXmlNamespace(),elementName);
@ -827,12 +833,20 @@ public class XmlParser extends ParserBase {
} }
xml.text(element.getValue()); xml.text(element.getValue());
xml.exit(element.getProperty().getXmlNamespace(),elementName); xml.exit(element.getProperty().getXmlNamespace(),elementName);
}
} else if (!element.hasChildren() && !element.hasValue()) { } else if (!element.hasChildren() && !element.hasValue()) {
if (isElideElements() && element.isElided() && xml.canElide())
xml.elide();
else {
if (element.getExplicitType() != null) if (element.getExplicitType() != null)
xml.attribute("xsi:type", element.getExplicitType()); xml.attribute("xsi:type", element.getExplicitType());
xml.element(elementName); xml.element(elementName);
}
} else if (element.isPrimitive() || (element.hasType() && isPrimitive(element.getType()))) { } else if (element.isPrimitive() || (element.hasType() && isPrimitive(element.getType()))) {
if (element.getType().equals("xhtml")) { if (element.getType().equals("xhtml")) {
if (isElideElements() && element.isElided() && xml.canElide())
xml.elide();
else {
String rawXhtml = element.getValue(); String rawXhtml = element.getValue();
if (isCdaText(element.getProperty())) { if (isCdaText(element.getProperty())) {
new CDANarrativeFormat().convert(xml, new XhtmlParser().parseFragment(rawXhtml)); new CDANarrativeFormat().convert(xml, new XhtmlParser().parseFragment(rawXhtml));
@ -843,11 +857,19 @@ public class XmlParser extends ParserBase {
markedXhtml = true; markedXhtml = true;
} }
} }
}
} else if (isText(element.getProperty())) { } else if (isText(element.getProperty())) {
if (isElideElements() && element.isElided() && xml.canElide())
xml.elide();
else {
if (linkResolver != null) if (linkResolver != null)
xml.link(linkResolver.resolveProperty(element.getProperty())); xml.link(linkResolver.resolveProperty(element.getProperty()));
xml.text(element.getValue()); xml.text(element.getValue());
}
} else { } else {
if (isElideElements() && element.isElided())
xml.attributeElide();
else {
setXsiTypeIfIsTypeAttr(xml, element); setXsiTypeIfIsTypeAttr(xml, element);
if (element.hasValue()) { if (element.hasValue()) {
if (linkResolver != null) if (linkResolver != null)
@ -870,17 +892,27 @@ public class XmlParser extends ParserBase {
} else } else
xml.element(elementName); xml.element(elementName);
} }
}
} else { } else {
if (isElideElements() && element.isElided() && xml.canElide())
xml.elide();
else {
setXsiTypeIfIsTypeAttr(xml, element); setXsiTypeIfIsTypeAttr(xml, element);
Set<String> handled = new HashSet<>(); Set<String> handled = new HashSet<>();
for (Element child : element.getChildren()) { for (Element child : element.getChildren()) {
if (!handled.contains(child.getName()) && isAttr(child.getProperty()) && wantCompose(element.getPath(), child)) { if (!handled.contains(child.getName()) && isAttr(child.getProperty()) && wantCompose(element.getPath(), child)) {
handled.add(child.getName()); handled.add(child.getName());
if (isElideElements() && child.isElided())
xml.attributeElide();
else {
String av = child.getValue(); String av = child.getValue();
if (child.getProperty().isList()) { if (child.getProperty().isList()) {
for (Element c2 : element.getChildren()) { for (Element c2 : element.getChildren()) {
if (c2 != child && c2.getName().equals(child.getName())) { if (c2 != child && c2.getName().equals(child.getName())) {
av = av + " "+c2.getValue(); if (c2.isElided())
av = av + " ...";
else
av = av + " " + c2.getValue();
} }
} }
} }
@ -888,7 +920,9 @@ public class XmlParser extends ParserBase {
xml.link(linkResolver.resolveType(child.getType())); xml.link(linkResolver.resolveType(child.getType()));
if (ToolingExtensions.hasExtension(child.getProperty().getDefinition(), ToolingExtensions.EXT_DATE_FORMAT)) if (ToolingExtensions.hasExtension(child.getProperty().getDefinition(), ToolingExtensions.EXT_DATE_FORMAT))
av = convertForDateFormatToExternal(ToolingExtensions.readStringExtension(child.getProperty().getDefinition(), ToolingExtensions.EXT_DATE_FORMAT), av); av = convertForDateFormatToExternal(ToolingExtensions.readStringExtension(child.getProperty().getDefinition(), ToolingExtensions.EXT_DATE_FORMAT), av);
xml.attribute(child.getProperty().getXmlNamespace(),child.getProperty().getXmlName(), av); xml.attribute(child.getProperty().getXmlNamespace(), child.getProperty().getXmlName(), av);
}
}
} }
} }
if (!element.getProperty().getDefinition().hasExtension(ToolingExtensions.EXT_ID_CHOICE_GROUP)) { if (!element.getProperty().getDefinition().hasExtension(ToolingExtensions.EXT_ID_CHOICE_GROUP)) {
@ -914,6 +948,9 @@ public class XmlParser extends ParserBase {
} }
for (Element child : element.getChildren()) { for (Element child : element.getChildren()) {
if (wantCompose(element.getPath(), child)) { if (wantCompose(element.getPath(), child)) {
if (isElideElements() && child.isElided() && xml.canElide())
xml.elide();
else {
if (isText(child.getProperty())) { if (isText(child.getProperty())) {
if (linkResolver != null) if (linkResolver != null)
xml.link(linkResolver.resolveProperty(element.getProperty())); 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 (!element.getProperty().getDefinition().hasExtension(ToolingExtensions.EXT_ID_CHOICE_GROUP)) {
if (!root && element.getSpecial() != null) if (!root && element.getSpecial() != null)
xml.exit(element.getProperty().getXmlNamespace(),element.getType()); xml.exit(element.getProperty().getXmlNamespace(),element.getType());
@ -1034,4 +1072,13 @@ public class XmlParser extends ParserBase {
// do nothing // do nothing
} }
} }
public boolean isElideElements() {
return elideElements;
}
public void setElideElements(boolean elideElements) {
this.elideElements = elideElements;
}
} }

View File

@ -71,4 +71,6 @@ public interface JsonCreator {
void link(String href); void link(String href);
void anchor(String string); void anchor(String string);
void externalLink(String string); void externalLink(String string);
void elide();
boolean canElide();
} }

View File

@ -280,5 +280,12 @@ public class JsonCreatorCanonical implements JsonCreator {
// not used // not used
} }
@Override
public boolean canElide() { return false; }
@Override
public void elide() {
// not used
}
} }

View File

@ -243,4 +243,12 @@ public class JsonCreatorDirect implements JsonCreator {
public void externalLink(String string) { public void externalLink(String string) {
// not used // not used
} }
@Override
public boolean canElide() { return false; }
@Override
public void elide() {
// not used
}
} }

View File

@ -127,4 +127,12 @@ public class JsonCreatorGson implements JsonCreator {
// not used // not used
} }
@Override
public void elide() {
// not used
}
@Override
public boolean canElide() { return false;}
} }

View File

@ -104,4 +104,9 @@ public interface IXMLWriter {
public abstract void link(String href); public abstract void link(String href);
public abstract void anchor(String name); public abstract void anchor(String name);
public abstract void externalLink(String ref) throws IOException; public abstract void externalLink(String ref) throws IOException;
// this is only implemented by an implementation that is producing an xhtml representation and handles ellipsing elements
public abstract boolean canElide();
public abstract void elide() throws IOException;
public abstract void attributeElide();
} }

View File

@ -912,5 +912,17 @@ public class XMLWriter extends OutputStreamWriter implements IXMLWriter {
// ignore this // ignore this
} }
@Override
public boolean canElide() { return false; }
@Override
public void attributeElide() {
// ignore this
}
@Override
public void elide() throws IOException {
// ignore this
}
} }