Merge pull request #1012 from hapifhir/gg-202211-json-comments

Consistency around rendering comments in examples in IGs
This commit is contained in:
Grahame Grieve 2022-11-27 05:33:35 -03:00 committed by GitHub
commit 65863d88d1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 365 additions and 290 deletions

View File

@ -333,7 +333,7 @@ public class TurtleParser extends ParserBase {
ontology.triple(ontologyId, "owl:versionIRI", ontologyId.replace(FHIR_URI_BASE, FHIR_VERSION_BASE));
Subject subject = section.triple(subjId, "a", "fhir:" + e.getType());
subject.linkedPredicate("fhir:nodeRole", "fhir:treeRoot", linkResolver == null ? null : linkResolver.resolvePage("rdf.html#tree-root"));
subject.linkedPredicate("fhir:nodeRole", "fhir:treeRoot", linkResolver == null ? null : linkResolver.resolvePage("rdf.html#tree-root"), null);
for (Element child : e.getChildren()) {
composeElement(section, subject, child, null);
@ -398,17 +398,17 @@ public class TurtleParser extends ParserBase {
Complex t;
if (element.getSpecial() == SpecialElement.BUNDLE_ENTRY && parent != null && parent.getNamedChildValue("fullUrl") != null) {
String url = "<"+parent.getNamedChildValue("fullUrl")+">";
ctxt.linkedPredicate("fhir:"+en, url, linkResolver == null ? null : linkResolver.resolveProperty(element.getProperty()));
ctxt.linkedPredicate("fhir:"+en, url, linkResolver == null ? null : linkResolver.resolveProperty(element.getProperty()), null);
t = section.subject(url);
} else {
t = ctxt.linkedPredicate("fhir:"+en, linkResolver == null ? null : linkResolver.resolveProperty(element.getProperty()));
t = ctxt.linkedPredicate("fhir:"+en, linkResolver == null ? null : linkResolver.resolveProperty(element.getProperty()), null);
}
if (element.getSpecial() != null)
t.linkedPredicate("a", "fhir:"+element.fhirType(), linkResolver == null ? null : linkResolver.resolveType(element.fhirType()));
t.linkedPredicate("a", "fhir:"+element.fhirType(), linkResolver == null ? null : linkResolver.resolveType(element.fhirType()), null);
if (element.hasValue())
t.linkedPredicate("fhir:value", ttlLiteral(element.getValue(), element.getType()), linkResolver == null ? null : linkResolver.resolveType(element.getType()));
t.linkedPredicate("fhir:value", ttlLiteral(element.getValue(), element.getType()), linkResolver == null ? null : linkResolver.resolveType(element.getType()), null);
if (element.getProperty().isList() && (!element.isResource() || element.getSpecial() == SpecialElement.CONTAINED))
t.linkedPredicate("fhir:index", Integer.toString(element.getIndex()), linkResolver == null ? null : linkResolver.resolvePage("rdf.html#index"));
t.linkedPredicate("fhir:index", Integer.toString(element.getIndex()), linkResolver == null ? null : linkResolver.resolvePage("rdf.html#index"), null);
if ("Coding".equals(element.getType()))
decorateCoding(t, element, section);

View File

@ -54,6 +54,7 @@ import org.hl7.fhir.r5.elementmodel.Element.SpecialElement;
import org.hl7.fhir.r5.formats.IParser.OutputStyle;
import org.hl7.fhir.r5.formats.JsonCreator;
import org.hl7.fhir.r5.formats.JsonCreatorCanonical;
import org.hl7.fhir.r5.formats.JsonCreatorDirect;
import org.hl7.fhir.r5.formats.JsonCreatorGson;
import org.hl7.fhir.r5.model.Base;
import org.hl7.fhir.r5.model.ElementDefinition.TypeRefComponent;
@ -182,10 +183,10 @@ public class JsonParser extends ParserBase {
}
private void checkComments(JsonElement element, Element b, String path) throws FHIRFormatError {
if (element.hasComments()) {
if (element != null && element.hasComments()) {
if (allowComments) {
for (JsonComment c : element.getComments()) {
b.getFormatCommentsPre().add(c.getContent());
b.getComments().add(c.getContent());
}
} else {
for (JsonComment c : element.getComments()) {
@ -322,7 +323,7 @@ public class JsonParser extends ParserBase {
}
int c = 0;
for (JsonElement am : arr) {
parseChildComplexInstance(npath+"["+c+"]", fpath+"["+c+"]", element, property, name, am);
parseChildComplexInstance(npath+"["+c+"]", fpath+"["+c+"]", element, property, name, am, c == 0 ? arr : null, path);
c++;
}
} else if (property.isJsonKeyArray()) {
@ -394,7 +395,7 @@ public class JsonParser extends ParserBase {
if (propV.isPrimitive(pvl.getType(null))) {
parseChildPrimitiveInstance(n, pvl, pvl.getName(), npathV, fpathV, pv.getValue(), null);
} else if (pv.getValue() instanceof JsonObject || pv.getValue() instanceof JsonNull) {
parseChildComplexInstance(npathV, fpathV, n, pvl, pvl.getName(), pv.getValue());
parseChildComplexInstance(npathV, fpathV, n, pvl, pvl.getName(), pv.getValue(), null, null);
} else {
logError(ValidationMessage.NO_RULE_DATE, line(e), col(e), npath, IssueType.INVALID, context.formatMessage(I18nConstants.THIS_PROPERTY_MUST_BE_AN_OBJECT_NOT_, describe(pv.getValue())), IssueSeverity.ERROR);
}
@ -407,7 +408,7 @@ public class JsonParser extends ParserBase {
if (property.isList()) {
logError(ValidationMessage.NO_RULE_DATE, line(e), col(e), npath, IssueType.INVALID, context.formatMessage(I18nConstants.THIS_PROPERTY_MUST_BE_AN_ARRAY_NOT_, describe(e), name, path), IssueSeverity.ERROR);
}
parseChildComplexInstance(npath, fpath, element, property, name, e);
parseChildComplexInstance(npath, fpath, element, property, name, e, null, null);
}
}
@ -419,7 +420,7 @@ public class JsonParser extends ParserBase {
return b.toString();
}
private void parseChildComplexInstance(String npath, String fpath, Element element, Property property, String name, JsonElement e) throws FHIRException {
private void parseChildComplexInstance(String npath, String fpath, Element element, Property property, String name, JsonElement e, JsonElement commentContext, String commentPath) throws FHIRException {
if (property.hasTypeSpecifier()) {
FHIRPathEngine fpe = new FHIRPathEngine(context);
String type = null;
@ -454,6 +455,7 @@ public class JsonParser extends ParserBase {
JsonObject child = (JsonObject) e;
Element n = new Element(name, property).markLocation(line(child), col(child));
n.setPath(fpath);
checkComments(commentContext, n, commentPath);
checkObject(child, n, npath);
element.getChildren().add(n);
if (property.isResource()) {
@ -465,6 +467,7 @@ public class JsonParser extends ParserBase {
// we create an element marked as a null element so we know something was present
JsonNull child = (JsonNull) e;
Element n = new Element(name, property).markLocation(line(child), col(child));
checkComments(commentContext, n, commentPath);
checkComments(child, n, fpath);
n.setPath(fpath);
element.getChildren().add(n);
@ -677,11 +680,14 @@ public class JsonParser extends ParserBase {
}
OutputStreamWriter osw = new OutputStreamWriter(stream, "UTF-8");
if (style == OutputStyle.CANONICAL)
if (style == OutputStyle.CANONICAL) {
json = new JsonCreatorCanonical(osw);
else
json = new JsonCreatorGson(osw);
json.setIndent(style == OutputStyle.PRETTY ? " " : "");
} else if (style == OutputStyle.PRETTY) {
json = new JsonCreatorDirect(osw, true, allowComments);
} else {
json = new JsonCreatorDirect(osw, false, allowComments);
}
checkComposeComments(e);
json.beginObject();
prop("resourceType", e.getType(), null);
Set<String> done = new HashSet<String>();
@ -693,12 +699,19 @@ public class JsonParser extends ParserBase {
osw.flush();
}
private void checkComposeComments(Element e) {
for (String s : e.getComments()) {
json.comment(s);
}
}
public void compose(Element e, JsonCreator json) throws Exception {
if (e.getPath() == null) {
e.populatePaths(null);
}
this.json = json;
checkComposeComments(e);
json.beginObject();
prop("resourceType", e.getType(), linkResolver == null ? null : linkResolver.resolveProperty(e.getProperty()));
@ -711,6 +724,7 @@ public class JsonParser extends ParserBase {
}
private void compose(String path, Element e, Set<String> done, Element child) throws IOException {
checkComposeComments(child);
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

View File

@ -69,6 +69,8 @@ public class TurtleParser extends ParserBase {
private String base;
private OutputStyle style;
public static String FHIR_URI_BASE = "http://hl7.org/fhir/";
public static String FHIR_VERSION_BASE = "http://build.fhir.org/";
@ -303,18 +305,16 @@ public class TurtleParser extends ParserBase {
return en.substring(0, en.lastIndexOf(".")+1)+elementName;
}
@Override
public void compose(Element e, OutputStream stream, OutputStyle style, String base) throws IOException, FHIRException {
this.base = base;
this.style = style;
Turtle ttl = new Turtle();
compose(e, ttl, base);
ttl.commit(stream, false);
}
public void compose(Element e, Turtle ttl, String base) throws FHIRException {
if (e.getPath() == null) {
e.populatePaths(null);
@ -325,8 +325,12 @@ public class TurtleParser extends ParserBase {
ttl.prefix("owl", "http://www.w3.org/2002/07/owl#");
ttl.prefix("xsd", "http://www.w3.org/2001/XMLSchema#");
Section section = ttl.section("resource");
if (style == OutputStyle.PRETTY) {
for (String s : e.getComments()) {
section.stringComment(s);
}
}
String subjId = genSubjectId(e);
String ontologyId = subjId.replace(">", ".ttl>");
@ -337,7 +341,7 @@ public class TurtleParser extends ParserBase {
ontology.triple(ontologyId, "owl:versionIRI", ontologyId.replace(FHIR_URI_BASE, FHIR_VERSION_BASE));
Subject subject = section.triple(subjId, "a", "fhir:" + e.getType());
subject.linkedPredicate("fhir:nodeRole", "fhir:treeRoot", linkResolver == null ? null : linkResolver.resolvePage("rdf.html#tree-root"));
subject.linkedPredicate("fhir:nodeRole", "fhir:treeRoot", linkResolver == null ? null : linkResolver.resolvePage("rdf.html#tree-root"), null);
for (Element child : e.getChildren()) {
composeElement(section, subject, child, null);
@ -364,13 +368,13 @@ public class TurtleParser extends ParserBase {
protected void decorateReference(Complex t, Element coding) {
String refURI = getReferenceURI(coding.getChildValue("reference"));
if(refURI != null)
t.linkedPredicate("fhir:link", refURI, linkResolver == null ? null : linkResolver.resolvePage("rdf.html#reference"));
t.linkedPredicate("fhir:link", refURI, linkResolver == null ? null : linkResolver.resolvePage("rdf.html#reference"), null);
}
protected void decorateCanonical(Complex t, Element canonical) {
String refURI = getReferenceURI(canonical.primitiveValue());
if(refURI != null)
t.linkedPredicate("fhir:link", refURI, linkResolver == null ? null : linkResolver.resolvePage("rdf.html#reference"));
t.linkedPredicate("fhir:link", refURI, linkResolver == null ? null : linkResolver.resolvePage("rdf.html#reference"), null);
}
private String genSubjectId(Element e) {
@ -397,26 +401,31 @@ public class TurtleParser extends ParserBase {
private void composeElement(Section section, Complex ctxt, Element element, Element parent) throws FHIRException {
// "Extension".equals(element.getType())?
// (element.getProperty().getDefinition().getIsModifier()? "modifierExtension" : "extension") ;
String en = getFormalName(element);
if (!wantCompose(parent == null ? "" : parent.getPath(), element)) {
return;
}
String comment = null;
if (style == OutputStyle.PRETTY) {
comment = String.join(", ", element.getComments());
}
Complex t;
if (element.getSpecial() == SpecialElement.BUNDLE_ENTRY && parent != null && parent.getNamedChildValue("fullUrl") != null) {
String url = "<"+parent.getNamedChildValue("fullUrl")+">";
ctxt.linkedPredicate("fhir:"+en, url, linkResolver == null ? null : linkResolver.resolveProperty(element.getProperty()));
ctxt.linkedPredicate("fhir:"+en, url, linkResolver == null ? null : linkResolver.resolveProperty(element.getProperty()), comment);
t = section.subject(url);
} else {
t = ctxt.linkedPredicate("fhir:"+en, linkResolver == null ? null : linkResolver.resolveProperty(element.getProperty()));
t = ctxt.linkedPredicate("fhir:"+en, linkResolver == null ? null : linkResolver.resolveProperty(element.getProperty()), comment);
}
if (element.getSpecial() != null)
t.linkedPredicate("a", "fhir:"+element.fhirType(), linkResolver == null ? null : linkResolver.resolveType(element.fhirType()));
t.linkedPredicate("a", "fhir:"+element.fhirType(), linkResolver == null ? null : linkResolver.resolveType(element.fhirType()), null);
if (element.hasValue())
t.linkedPredicate("fhir:value", ttlLiteral(element.getValue(), element.getType()), linkResolver == null ? null : linkResolver.resolveType(element.getType()));
t.linkedPredicate("fhir:value", ttlLiteral(element.getValue(), element.getType()), linkResolver == null ? null : linkResolver.resolveType(element.getType()), null);
if (element.getProperty().isList() && (!element.isResource() || element.getSpecial() == SpecialElement.CONTAINED))
t.linkedPredicate("fhir:index", Integer.toString(element.getIndex()), linkResolver == null ? null : linkResolver.resolvePage("rdf.html#index"));
t.linkedPredicate("fhir:index", Integer.toString(element.getIndex()), linkResolver == null ? null : linkResolver.resolvePage("rdf.html#index"), null);
if ("Coding".equals(element.getType()))
decorateCoding(t, element, section);
@ -549,6 +558,12 @@ public class TurtleParser extends ParserBase {
Expression expression = SnomedExpressions.parse(code);
}
public OutputStyle getStyle() {
return style;
}
public void setStyle(OutputStyle style) {
this.style = style;
}
// 128045006|cellulitis (disorder)|:{363698007|finding site|=56459004|foot structure|}

View File

@ -42,7 +42,7 @@ import java.math.BigDecimal;
*/
public interface JsonCreator {
void setIndent(String string);
void comment(String comment);
void beginObject() throws IOException;

View File

@ -116,7 +116,7 @@ public class JsonCreatorCanonical implements JsonCreator {
public JsonCreatorCanonical(OutputStreamWriter osw) {
stack = new Stack<JsonCreatorCanonical.JsonCanObject>();
jj = new JsonCreatorDirect(osw);
jj = new JsonCreatorDirect(osw, false, false);
name = null;
}
@ -126,13 +126,6 @@ public class JsonCreatorCanonical implements JsonCreator {
return res;
}
@Override
public void setIndent(String indent) {
if (!indent.equals(""))
throw new Error("do not use pretty when canonical is set");
jj.setIndent(indent);
}
@Override
public void beginObject() throws IOException {
JsonCanObject obj = new JsonCanObject(takeName(), false);
@ -277,5 +270,11 @@ public class JsonCreatorCanonical implements JsonCreator {
// not used
}
@Override
public void comment(String content) {
// canonical JSON ignores comments
}
}

View File

@ -49,18 +49,24 @@ public class JsonCreatorDirect implements JsonCreator {
private Writer writer;
private boolean pretty;
private boolean comments;
private boolean named;
private List<Boolean> valued = new ArrayList<Boolean>();
private int indent;
private List<String> commentList = new ArrayList<>();
public JsonCreatorDirect(Writer writer) {
public JsonCreatorDirect(Writer writer, boolean pretty, boolean comments) {
super();
this.writer = writer;
this.pretty = pretty;
this.comments = pretty && comments;
}
@Override
public void setIndent(String indent) {
this.pretty = !Utilities.noString(indent);
public void comment(String content) {
if (comments) {
commentList.add(content);
}
}
@Override
@ -74,6 +80,21 @@ public class JsonCreatorDirect implements JsonCreator {
valued.add(0, false);
}
private void commitComments() throws IOException {
if (comments) {
for (String s : commentList) {
writer.write("// ");
writer.write(s);
writer.write("\r\n");
for (int i = 0; i < indent; i++) {
writer.write(" ");
}
}
commentList.clear();
}
}
public void stepIn() throws IOException {
if (pretty) {
indent++;
@ -95,6 +116,7 @@ public class JsonCreatorDirect implements JsonCreator {
}
private void checkState() throws IOException {
commitComments();
if (named) {
if (pretty)
writer.write(" : ");

View File

@ -45,11 +45,6 @@ public class JsonCreatorGson implements JsonCreator {
gson = new JsonWriter(osw);
}
@Override
public void setIndent(String indent) {
gson.setIndent(indent);
}
@Override
public void beginObject() throws IOException {
gson.beginObject();
@ -121,4 +116,10 @@ public class JsonCreatorGson implements JsonCreator {
// not used
}
@Override
public void comment(String content) {
// gson (dense json) ignores comments
}
}

View File

@ -183,11 +183,13 @@ public abstract class JsonParserBase extends ParserBase implements IParser {
@Override
public void compose(OutputStream stream, Resource resource) throws IOException {
OutputStreamWriter osw = new OutputStreamWriter(stream, "UTF-8");
if (style == OutputStyle.CANONICAL)
if (style == OutputStyle.CANONICAL) {
json = new JsonCreatorCanonical(osw);
else
json = new JsonCreatorDirect(osw); // use this instead of Gson because this preserves decimal formatting
json.setIndent(style == OutputStyle.PRETTY ? " " : "");
} else if (style == OutputStyle.PRETTY) {
json = new JsonCreatorDirect(osw, true, false);
} else {
json = new JsonCreatorDirect(osw, false, false); // use this instead of Gson because this preserves decimal formatting
}
json.beginObject();
composeResource(resource);
json.endObject();
@ -207,11 +209,13 @@ public abstract class JsonParserBase extends ParserBase implements IParser {
@Override
public void compose(OutputStream stream, DataType type, String rootName) throws IOException {
OutputStreamWriter osw = new OutputStreamWriter(stream, "UTF-8");
if (style == OutputStyle.CANONICAL)
if (style == OutputStyle.CANONICAL) {
json = new JsonCreatorCanonical(osw);
else
json = new JsonCreatorDirect(osw);// use this instead of Gson because this preserves decimal formatting
json.setIndent(style == OutputStyle.PRETTY ? " " : "");
} else if (style == OutputStyle.PRETTY) {
json = new JsonCreatorDirect(osw, true, false);
} else {
json = new JsonCreatorDirect(osw, false, false); // use this instead of Gson because this preserves decimal formatting
}
json.beginObject();
composeTypeInner(type);
json.endObject();

View File

@ -41,7 +41,7 @@ public class JsonDirectTests {
@Test
public void testEmptyObject() throws FHIRFormatError, FileNotFoundException, IOException {
ByteArrayOutputStream bs = new ByteArrayOutputStream();
JsonCreatorDirect json = new JsonCreatorDirect(new OutputStreamWriter(bs, "UTF-8"));
JsonCreatorDirect json = new JsonCreatorDirect(new OutputStreamWriter(bs, "UTF-8"), false, false);
json.beginObject();
json.name("a");
json.beginObject();

View File

@ -77,10 +77,10 @@ public class Turtle {
return predicate(predicate, new StringType(object));
}
public Complex linkedPredicate(String predicate, String object, String link) {
public Complex linkedPredicate(String predicate, String object, String link, String comment) {
predicateSet.add(predicate);
objectSet.add(object);
return linkedPredicate(predicate, new StringType(object), link);
return linkedPredicate(predicate, new StringType(object), link, comment);
}
public Complex predicate(String predicate, Triple object) {
@ -104,12 +104,13 @@ public class Turtle {
return null;
}
public Complex linkedPredicate(String predicate, Triple object, String link) {
public Complex linkedPredicate(String predicate, Triple object, String link, String comment) {
Predicate p = getPredicate(predicate);
if (p == null) {
p = new Predicate();
p.predicate = predicate;
p.link = link;
p.comment = comment;
predicateSet.add(predicate);
predicates.add(p);
}
@ -126,10 +127,10 @@ public class Turtle {
return c;
}
public Complex linkedPredicate(String predicate, String link) {
public Complex linkedPredicate(String predicate, String link, String comment) {
predicateSet.add(predicate);
Complex c = complex();
linkedPredicate(predicate, c, link);
linkedPredicate(predicate, c, link, comment);
return c;
}
@ -197,6 +198,7 @@ public class Turtle {
}
public class Section {
private List<String> comments = new ArrayList<>();
private String name;
private List<Subject> subjects = new ArrayList<Subject>();
@ -244,6 +246,10 @@ public class Turtle {
return true;
return false;
}
public void stringComment(String cnt) {
comments.add(cnt);
}
}
private List<Section> sections = new ArrayList<Section>();
@ -437,6 +443,12 @@ public class Turtle {
private void commitSection(LineOutputStreamWriter writer, Section section) throws IOException {
writer.ln("# - "+section.name+" "+Utilities.padLeft("", '-', 75-section.name.length()));
writer.ln();
if (!section.comments.isEmpty()) {
for (String s : section.comments) {
writer.ln("# "+s);
}
writer.ln();
}
for (Subject sbj : section.subjects) {
if (Utilities.noString(sbj.id)) {
writer.write("[");
@ -447,6 +459,7 @@ public class Turtle {
int i = 0;
for (Predicate p : sbj.predicates) {
//
writer.write(p.getPredicate());
writer.write(" ");
boolean first = true;
@ -481,12 +494,19 @@ public class Turtle {
private void commitSection(StringBuilder b, Section section) throws Exception {
b.append("# - "+section.name+" "+Utilities.padLeft("", '-', 75-section.name.length())+"\r\n");
b.append("\r\n");
if (!section.comments.isEmpty()) {
for (String s : section.comments) {
b.append("# "+s+"\r\n");
}
b.append("\r\n");
}
for (Subject sbj : section.subjects) {
b.append(Utilities.escapeXml(sbj.id));
b.append(" ");
int i = 0;
for (Predicate p : sbj.predicates) {
// b.append("# test\r\n ");
b.append(p.makelink());
b.append(" ");
boolean first = true;