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,7 +785,8 @@ public class JsonParser extends ParserBase {
} }
checkComposeComments(e); checkComposeComments(e);
json.beginObject(); json.beginObject();
prop("resourceType", e.getType(), null); // if (!isSuppressResourceType())
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()) {
compose(e.getName(), e, done, child); compose(e.getName(), e, done, child);
@ -807,7 +811,8 @@ public class JsonParser extends ParserBase {
checkComposeComments(e); checkComposeComments(e);
json.beginObject(); json.beginObject();
prop("resourceType", e.getType(), linkResolver == null ? null : linkResolver.resolveProperty(e.getProperty())); // if (!isSuppressResourceType())
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()) {
compose(e.getName(), e, done, child); compose(e.getName(), e, done, child);
@ -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
compose(path, child); if (child.isElided() && isElideElements() && json.canElide())
json.elide();
else
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());
composeList(path, list); 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 { 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,92 +806,125 @@ 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 (showDecorations) { if (!(isElideElements() && element.isElided())) {
@SuppressWarnings("unchecked") if (showDecorations) {
List<ElementDecoration> decorations = (List<ElementDecoration>) element.getUserData("fhir.decorations"); @SuppressWarnings("unchecked")
if (decorations != null) List<ElementDecoration> decorations = (List<ElementDecoration>) element.getUserData("fhir.decorations");
for (ElementDecoration d : decorations) if (decorations != null)
xml.decorate(d); for (ElementDecoration d : decorations)
} xml.decorate(d);
for (String s : element.getComments()) { }
xml.comment(s, true); for (String s : element.getComments()) {
xml.comment(s, true);
}
} }
if (isText(element.getProperty())) { if (isText(element.getProperty())) {
if (linkResolver != null) if (isElideElements() && element.isElided() && xml.canElide())
xml.link(linkResolver.resolveProperty(element.getProperty())); xml.elide();
xml.enter(element.getProperty().getXmlNamespace(),elementName); else {
if (linkResolver != null && element.getProperty().isReference()) { if (linkResolver != null)
String ref = linkResolver.resolveReference(getReferenceForElement(element)); xml.link(linkResolver.resolveProperty(element.getProperty()));
if (ref != null) { xml.enter(element.getProperty().getXmlNamespace(),elementName);
xml.externalLink(ref); if (linkResolver != null && element.getProperty().isReference()) {
String ref = linkResolver.resolveReference(getReferenceForElement(element));
if (ref != null) {
xml.externalLink(ref);
}
} }
xml.text(element.getValue());
xml.exit(element.getProperty().getXmlNamespace(),elementName);
} }
xml.text(element.getValue());
xml.exit(element.getProperty().getXmlNamespace(),elementName);
} else if (!element.hasChildren() && !element.hasValue()) { } else if (!element.hasChildren() && !element.hasValue()) {
if (element.getExplicitType() != null) if (isElideElements() && element.isElided() && xml.canElide())
xml.attribute("xsi:type", element.getExplicitType()); xml.elide();
xml.element(elementName); else {
if (element.getExplicitType() != null)
xml.attribute("xsi:type", element.getExplicitType());
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")) {
String rawXhtml = element.getValue(); if (isElideElements() && element.isElided() && xml.canElide())
if (isCdaText(element.getProperty())) { xml.elide();
new CDANarrativeFormat().convert(xml, new XhtmlParser().parseFragment(rawXhtml)); else {
} else { String rawXhtml = element.getValue();
xml.escapedText(rawXhtml); if (isCdaText(element.getProperty())) {
if (!markedXhtml) { new CDANarrativeFormat().convert(xml, new XhtmlParser().parseFragment(rawXhtml));
xml.anchor("end-xhtml"); } else {
markedXhtml = true; xml.escapedText(rawXhtml);
if (!markedXhtml) {
xml.anchor("end-xhtml");
markedXhtml = true;
}
} }
} }
} else if (isText(element.getProperty())) { } else if (isText(element.getProperty())) {
if (linkResolver != null) if (isElideElements() && element.isElided() && xml.canElide())
xml.link(linkResolver.resolveProperty(element.getProperty())); xml.elide();
xml.text(element.getValue()); else {
} else {
setXsiTypeIfIsTypeAttr(xml, element);
if (element.hasValue()) {
if (linkResolver != null) if (linkResolver != null)
xml.link(linkResolver.resolveType(element.getType())); xml.link(linkResolver.resolveProperty(element.getProperty()));
xml.attribute("value", element.getValue()); xml.text(element.getValue());
} }
if (linkResolver != null) } else {
xml.link(linkResolver.resolveProperty(element.getProperty())); if (isElideElements() && element.isElided())
if (element.hasChildren()) { xml.attributeElide();
xml.enter(element.getProperty().getXmlNamespace(), elementName); else {
if (linkResolver != null && element.getProperty().isReference()) { setXsiTypeIfIsTypeAttr(xml, element);
String ref = linkResolver.resolveReference(getReferenceForElement(element)); if (element.hasValue()) {
if (ref != null) { if (linkResolver != null)
xml.externalLink(ref); xml.link(linkResolver.resolveType(element.getType()));
} xml.attribute("value", element.getValue());
} }
for (Element child : element.getChildren()) if (linkResolver != null)
composeElement(xml, child, child.getName(), false); xml.link(linkResolver.resolveProperty(element.getProperty()));
xml.exit(element.getProperty().getXmlNamespace(),elementName); if (element.hasChildren()) {
} else xml.enter(element.getProperty().getXmlNamespace(), elementName);
xml.element(elementName); if (linkResolver != null && element.getProperty().isReference()) {
String ref = linkResolver.resolveReference(getReferenceForElement(element));
if (ref != null) {
xml.externalLink(ref);
}
}
for (Element child : element.getChildren())
composeElement(xml, child, child.getName(), false);
xml.exit(element.getProperty().getXmlNamespace(),elementName);
} else
xml.element(elementName);
}
} }
} else { } else {
setXsiTypeIfIsTypeAttr(xml, element); if (isElideElements() && element.isElided() && xml.canElide())
Set<String> handled = new HashSet<>(); xml.elide();
else {
setXsiTypeIfIsTypeAttr(xml, element);
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());
String av = child.getValue(); if (isElideElements() && child.isElided())
if (child.getProperty().isList()) { xml.attributeElide();
for (Element c2 : element.getChildren()) { else {
if (c2 != child && c2.getName().equals(child.getName())) { String av = child.getValue();
av = av + " "+c2.getValue(); if (child.getProperty().isList()) {
for (Element c2 : element.getChildren()) {
if (c2 != child && c2.getName().equals(child.getName())) {
if (c2.isElided())
av = av + " ...";
else
av = av + " " + c2.getValue();
}
} }
} }
if (linkResolver != null)
xml.link(linkResolver.resolveType(child.getType()));
if (ToolingExtensions.hasExtension(child.getProperty().getDefinition(), ToolingExtensions.EXT_DATE_FORMAT))
av = convertForDateFormatToExternal(ToolingExtensions.readStringExtension(child.getProperty().getDefinition(), ToolingExtensions.EXT_DATE_FORMAT), av);
xml.attribute(child.getProperty().getXmlNamespace(), child.getProperty().getXmlName(), av);
} }
if (linkResolver != null)
xml.link(linkResolver.resolveType(child.getType()));
if (ToolingExtensions.hasExtension(child.getProperty().getDefinition(), ToolingExtensions.EXT_DATE_FORMAT))
av = convertForDateFormatToExternal(ToolingExtensions.readStringExtension(child.getProperty().getDefinition(), ToolingExtensions.EXT_DATE_FORMAT), 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)) {
if (linkResolver != null) if (linkResolver != null)
xml.link(linkResolver.resolveProperty(element.getProperty())); xml.link(linkResolver.resolveProperty(element.getProperty()));
@ -914,12 +948,16 @@ 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 (isText(child.getProperty())) { if (isElideElements() && child.isElided() && xml.canElide())
if (linkResolver != null) xml.elide();
xml.link(linkResolver.resolveProperty(element.getProperty())); else {
xml.text(child.getValue()); if (isText(child.getProperty())) {
} else if (!isAttr(child.getProperty())) { if (linkResolver != null)
composeElement(xml, child, child.getName(), false); xml.link(linkResolver.resolveProperty(element.getProperty()));
xml.text(child.getValue());
} else if (!isAttr(child.getProperty())) {
composeElement(xml, child, child.getName(), false);
}
} }
} }
} }
@ -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
}
} }