Merge pull request #1012 from hapifhir/gg-202211-json-comments
Consistency around rendering comments in examples in IGs
This commit is contained in:
commit
65863d88d1
|
@ -333,7 +333,7 @@ public class TurtleParser extends ParserBase {
|
||||||
ontology.triple(ontologyId, "owl:versionIRI", ontologyId.replace(FHIR_URI_BASE, FHIR_VERSION_BASE));
|
ontology.triple(ontologyId, "owl:versionIRI", ontologyId.replace(FHIR_URI_BASE, FHIR_VERSION_BASE));
|
||||||
|
|
||||||
Subject subject = section.triple(subjId, "a", "fhir:" + e.getType());
|
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()) {
|
for (Element child : e.getChildren()) {
|
||||||
composeElement(section, subject, child, null);
|
composeElement(section, subject, child, null);
|
||||||
|
@ -398,17 +398,17 @@ public class TurtleParser extends ParserBase {
|
||||||
Complex t;
|
Complex t;
|
||||||
if (element.getSpecial() == SpecialElement.BUNDLE_ENTRY && parent != null && parent.getNamedChildValue("fullUrl") != null) {
|
if (element.getSpecial() == SpecialElement.BUNDLE_ENTRY && parent != null && parent.getNamedChildValue("fullUrl") != null) {
|
||||||
String url = "<"+parent.getNamedChildValue("fullUrl")+">";
|
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);
|
t = section.subject(url);
|
||||||
} else {
|
} 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)
|
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())
|
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))
|
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()))
|
if ("Coding".equals(element.getType()))
|
||||||
decorateCoding(t, element, section);
|
decorateCoding(t, element, section);
|
||||||
|
|
|
@ -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.IParser.OutputStyle;
|
||||||
import org.hl7.fhir.r5.formats.JsonCreator;
|
import org.hl7.fhir.r5.formats.JsonCreator;
|
||||||
import org.hl7.fhir.r5.formats.JsonCreatorCanonical;
|
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.formats.JsonCreatorGson;
|
||||||
import org.hl7.fhir.r5.model.Base;
|
import org.hl7.fhir.r5.model.Base;
|
||||||
import org.hl7.fhir.r5.model.ElementDefinition.TypeRefComponent;
|
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 {
|
private void checkComments(JsonElement element, Element b, String path) throws FHIRFormatError {
|
||||||
if (element.hasComments()) {
|
if (element != null && element.hasComments()) {
|
||||||
if (allowComments) {
|
if (allowComments) {
|
||||||
for (JsonComment c : element.getComments()) {
|
for (JsonComment c : element.getComments()) {
|
||||||
b.getFormatCommentsPre().add(c.getContent());
|
b.getComments().add(c.getContent());
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
for (JsonComment c : element.getComments()) {
|
for (JsonComment c : element.getComments()) {
|
||||||
|
@ -322,7 +323,7 @@ public class JsonParser extends ParserBase {
|
||||||
}
|
}
|
||||||
int c = 0;
|
int c = 0;
|
||||||
for (JsonElement am : arr) {
|
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++;
|
c++;
|
||||||
}
|
}
|
||||||
} else if (property.isJsonKeyArray()) {
|
} else if (property.isJsonKeyArray()) {
|
||||||
|
@ -394,7 +395,7 @@ public class JsonParser extends ParserBase {
|
||||||
if (propV.isPrimitive(pvl.getType(null))) {
|
if (propV.isPrimitive(pvl.getType(null))) {
|
||||||
parseChildPrimitiveInstance(n, pvl, pvl.getName(), npathV, fpathV, pv.getValue(), null);
|
parseChildPrimitiveInstance(n, pvl, pvl.getName(), npathV, fpathV, pv.getValue(), null);
|
||||||
} else if (pv.getValue() instanceof JsonObject || pv.getValue() instanceof JsonNull) {
|
} 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 {
|
} 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);
|
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()) {
|
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);
|
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();
|
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()) {
|
if (property.hasTypeSpecifier()) {
|
||||||
FHIRPathEngine fpe = new FHIRPathEngine(context);
|
FHIRPathEngine fpe = new FHIRPathEngine(context);
|
||||||
String type = null;
|
String type = null;
|
||||||
|
@ -454,6 +455,7 @@ public class JsonParser extends ParserBase {
|
||||||
JsonObject child = (JsonObject) e;
|
JsonObject child = (JsonObject) e;
|
||||||
Element n = new Element(name, property).markLocation(line(child), col(child));
|
Element n = new Element(name, property).markLocation(line(child), col(child));
|
||||||
n.setPath(fpath);
|
n.setPath(fpath);
|
||||||
|
checkComments(commentContext, n, commentPath);
|
||||||
checkObject(child, n, npath);
|
checkObject(child, n, npath);
|
||||||
element.getChildren().add(n);
|
element.getChildren().add(n);
|
||||||
if (property.isResource()) {
|
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
|
// we create an element marked as a null element so we know something was present
|
||||||
JsonNull child = (JsonNull) e;
|
JsonNull child = (JsonNull) e;
|
||||||
Element n = new Element(name, property).markLocation(line(child), col(child));
|
Element n = new Element(name, property).markLocation(line(child), col(child));
|
||||||
|
checkComments(commentContext, n, commentPath);
|
||||||
checkComments(child, n, fpath);
|
checkComments(child, n, fpath);
|
||||||
n.setPath(fpath);
|
n.setPath(fpath);
|
||||||
element.getChildren().add(n);
|
element.getChildren().add(n);
|
||||||
|
@ -677,11 +680,14 @@ public class JsonParser extends ParserBase {
|
||||||
}
|
}
|
||||||
|
|
||||||
OutputStreamWriter osw = new OutputStreamWriter(stream, "UTF-8");
|
OutputStreamWriter osw = new OutputStreamWriter(stream, "UTF-8");
|
||||||
if (style == OutputStyle.CANONICAL)
|
if (style == OutputStyle.CANONICAL) {
|
||||||
json = new JsonCreatorCanonical(osw);
|
json = new JsonCreatorCanonical(osw);
|
||||||
else
|
} else if (style == OutputStyle.PRETTY) {
|
||||||
json = new JsonCreatorGson(osw);
|
json = new JsonCreatorDirect(osw, true, allowComments);
|
||||||
json.setIndent(style == OutputStyle.PRETTY ? " " : "");
|
} else {
|
||||||
|
json = new JsonCreatorDirect(osw, false, allowComments);
|
||||||
|
}
|
||||||
|
checkComposeComments(e);
|
||||||
json.beginObject();
|
json.beginObject();
|
||||||
prop("resourceType", e.getType(), null);
|
prop("resourceType", e.getType(), null);
|
||||||
Set<String> done = new HashSet<String>();
|
Set<String> done = new HashSet<String>();
|
||||||
|
@ -693,12 +699,19 @@ public class JsonParser extends ParserBase {
|
||||||
osw.flush();
|
osw.flush();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void checkComposeComments(Element e) {
|
||||||
|
for (String s : e.getComments()) {
|
||||||
|
json.comment(s);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public void compose(Element e, JsonCreator json) throws Exception {
|
public void compose(Element e, JsonCreator json) throws Exception {
|
||||||
if (e.getPath() == null) {
|
if (e.getPath() == null) {
|
||||||
e.populatePaths(null);
|
e.populatePaths(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
this.json = json;
|
this.json = json;
|
||||||
|
checkComposeComments(e);
|
||||||
json.beginObject();
|
json.beginObject();
|
||||||
|
|
||||||
prop("resourceType", e.getType(), linkResolver == null ? null : linkResolver.resolveProperty(e.getProperty()));
|
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 {
|
private void compose(String path, Element e, Set<String> done, Element child) throws IOException {
|
||||||
|
checkComposeComments(child);
|
||||||
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
|
||||||
|
|
|
@ -69,6 +69,8 @@ public class TurtleParser extends ParserBase {
|
||||||
|
|
||||||
private String base;
|
private String base;
|
||||||
|
|
||||||
|
private OutputStyle style;
|
||||||
|
|
||||||
public static String FHIR_URI_BASE = "http://hl7.org/fhir/";
|
public static String FHIR_URI_BASE = "http://hl7.org/fhir/";
|
||||||
public static String FHIR_VERSION_BASE = "http://build.fhir.org/";
|
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;
|
return en.substring(0, en.lastIndexOf(".")+1)+elementName;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void compose(Element e, OutputStream stream, OutputStyle style, String base) throws IOException, FHIRException {
|
public void compose(Element e, OutputStream stream, OutputStyle style, String base) throws IOException, FHIRException {
|
||||||
this.base = base;
|
this.base = base;
|
||||||
|
this.style = style;
|
||||||
|
|
||||||
Turtle ttl = new Turtle();
|
Turtle ttl = new Turtle();
|
||||||
compose(e, ttl, base);
|
compose(e, ttl, base);
|
||||||
ttl.commit(stream, false);
|
ttl.commit(stream, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public void compose(Element e, Turtle ttl, String base) throws FHIRException {
|
public void compose(Element e, Turtle ttl, String base) throws FHIRException {
|
||||||
if (e.getPath() == null) {
|
if (e.getPath() == null) {
|
||||||
e.populatePaths(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("owl", "http://www.w3.org/2002/07/owl#");
|
||||||
ttl.prefix("xsd", "http://www.w3.org/2001/XMLSchema#");
|
ttl.prefix("xsd", "http://www.w3.org/2001/XMLSchema#");
|
||||||
|
|
||||||
|
|
||||||
Section section = ttl.section("resource");
|
Section section = ttl.section("resource");
|
||||||
|
if (style == OutputStyle.PRETTY) {
|
||||||
|
for (String s : e.getComments()) {
|
||||||
|
section.stringComment(s);
|
||||||
|
}
|
||||||
|
}
|
||||||
String subjId = genSubjectId(e);
|
String subjId = genSubjectId(e);
|
||||||
|
|
||||||
String ontologyId = subjId.replace(">", ".ttl>");
|
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));
|
ontology.triple(ontologyId, "owl:versionIRI", ontologyId.replace(FHIR_URI_BASE, FHIR_VERSION_BASE));
|
||||||
|
|
||||||
Subject subject = section.triple(subjId, "a", "fhir:" + e.getType());
|
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()) {
|
for (Element child : e.getChildren()) {
|
||||||
composeElement(section, subject, child, null);
|
composeElement(section, subject, child, null);
|
||||||
|
@ -364,13 +368,13 @@ public class TurtleParser extends ParserBase {
|
||||||
protected void decorateReference(Complex t, Element coding) {
|
protected void decorateReference(Complex t, Element coding) {
|
||||||
String refURI = getReferenceURI(coding.getChildValue("reference"));
|
String refURI = getReferenceURI(coding.getChildValue("reference"));
|
||||||
if(refURI != null)
|
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) {
|
protected void decorateCanonical(Complex t, Element canonical) {
|
||||||
String refURI = getReferenceURI(canonical.primitiveValue());
|
String refURI = getReferenceURI(canonical.primitiveValue());
|
||||||
if(refURI != null)
|
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) {
|
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 {
|
private void composeElement(Section section, Complex ctxt, Element element, Element parent) throws FHIRException {
|
||||||
// "Extension".equals(element.getType())?
|
// "Extension".equals(element.getType())?
|
||||||
// (element.getProperty().getDefinition().getIsModifier()? "modifierExtension" : "extension") ;
|
// (element.getProperty().getDefinition().getIsModifier()? "modifierExtension" : "extension") ;
|
||||||
|
|
||||||
String en = getFormalName(element);
|
String en = getFormalName(element);
|
||||||
|
|
||||||
if (!wantCompose(parent == null ? "" : parent.getPath(), element)) {
|
if (!wantCompose(parent == null ? "" : parent.getPath(), element)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
String comment = null;
|
||||||
|
if (style == OutputStyle.PRETTY) {
|
||||||
|
comment = String.join(", ", element.getComments());
|
||||||
|
}
|
||||||
Complex t;
|
Complex t;
|
||||||
if (element.getSpecial() == SpecialElement.BUNDLE_ENTRY && parent != null && parent.getNamedChildValue("fullUrl") != null) {
|
if (element.getSpecial() == SpecialElement.BUNDLE_ENTRY && parent != null && parent.getNamedChildValue("fullUrl") != null) {
|
||||||
String url = "<"+parent.getNamedChildValue("fullUrl")+">";
|
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);
|
t = section.subject(url);
|
||||||
} else {
|
} 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)
|
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())
|
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))
|
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()))
|
if ("Coding".equals(element.getType()))
|
||||||
decorateCoding(t, element, section);
|
decorateCoding(t, element, section);
|
||||||
|
@ -549,6 +558,12 @@ public class TurtleParser extends ParserBase {
|
||||||
Expression expression = SnomedExpressions.parse(code);
|
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|}
|
// 128045006|cellulitis (disorder)|:{363698007|finding site|=56459004|foot structure|}
|
||||||
|
|
|
@ -42,7 +42,7 @@ import java.math.BigDecimal;
|
||||||
*/
|
*/
|
||||||
public interface JsonCreator {
|
public interface JsonCreator {
|
||||||
|
|
||||||
void setIndent(String string);
|
void comment(String comment);
|
||||||
|
|
||||||
void beginObject() throws IOException;
|
void beginObject() throws IOException;
|
||||||
|
|
||||||
|
|
|
@ -116,7 +116,7 @@ public class JsonCreatorCanonical implements JsonCreator {
|
||||||
|
|
||||||
public JsonCreatorCanonical(OutputStreamWriter osw) {
|
public JsonCreatorCanonical(OutputStreamWriter osw) {
|
||||||
stack = new Stack<JsonCreatorCanonical.JsonCanObject>();
|
stack = new Stack<JsonCreatorCanonical.JsonCanObject>();
|
||||||
jj = new JsonCreatorDirect(osw);
|
jj = new JsonCreatorDirect(osw, false, false);
|
||||||
name = null;
|
name = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -126,13 +126,6 @@ public class JsonCreatorCanonical implements JsonCreator {
|
||||||
return res;
|
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
|
@Override
|
||||||
public void beginObject() throws IOException {
|
public void beginObject() throws IOException {
|
||||||
JsonCanObject obj = new JsonCanObject(takeName(), false);
|
JsonCanObject obj = new JsonCanObject(takeName(), false);
|
||||||
|
@ -277,5 +270,11 @@ public class JsonCreatorCanonical implements JsonCreator {
|
||||||
// not used
|
// not used
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void comment(String content) {
|
||||||
|
// canonical JSON ignores comments
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
|
@ -49,18 +49,24 @@ public class JsonCreatorDirect implements JsonCreator {
|
||||||
|
|
||||||
private Writer writer;
|
private Writer writer;
|
||||||
private boolean pretty;
|
private boolean pretty;
|
||||||
|
private boolean comments;
|
||||||
private boolean named;
|
private boolean named;
|
||||||
private List<Boolean> valued = new ArrayList<Boolean>();
|
private List<Boolean> valued = new ArrayList<Boolean>();
|
||||||
private int indent;
|
private int indent;
|
||||||
|
private List<String> commentList = new ArrayList<>();
|
||||||
|
|
||||||
public JsonCreatorDirect(Writer writer) {
|
public JsonCreatorDirect(Writer writer, boolean pretty, boolean comments) {
|
||||||
super();
|
super();
|
||||||
this.writer = writer;
|
this.writer = writer;
|
||||||
|
this.pretty = pretty;
|
||||||
|
this.comments = pretty && comments;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setIndent(String indent) {
|
public void comment(String content) {
|
||||||
this.pretty = !Utilities.noString(indent);
|
if (comments) {
|
||||||
|
commentList.add(content);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -74,6 +80,21 @@ public class JsonCreatorDirect implements JsonCreator {
|
||||||
valued.add(0, false);
|
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 {
|
public void stepIn() throws IOException {
|
||||||
if (pretty) {
|
if (pretty) {
|
||||||
indent++;
|
indent++;
|
||||||
|
@ -95,6 +116,7 @@ public class JsonCreatorDirect implements JsonCreator {
|
||||||
}
|
}
|
||||||
|
|
||||||
private void checkState() throws IOException {
|
private void checkState() throws IOException {
|
||||||
|
commitComments();
|
||||||
if (named) {
|
if (named) {
|
||||||
if (pretty)
|
if (pretty)
|
||||||
writer.write(" : ");
|
writer.write(" : ");
|
||||||
|
|
|
@ -45,11 +45,6 @@ public class JsonCreatorGson implements JsonCreator {
|
||||||
gson = new JsonWriter(osw);
|
gson = new JsonWriter(osw);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void setIndent(String indent) {
|
|
||||||
gson.setIndent(indent);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void beginObject() throws IOException {
|
public void beginObject() throws IOException {
|
||||||
gson.beginObject();
|
gson.beginObject();
|
||||||
|
@ -121,4 +116,10 @@ public class JsonCreatorGson implements JsonCreator {
|
||||||
// not used
|
// not used
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void comment(String content) {
|
||||||
|
// gson (dense json) ignores comments
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -183,11 +183,13 @@ public abstract class JsonParserBase extends ParserBase implements IParser {
|
||||||
@Override
|
@Override
|
||||||
public void compose(OutputStream stream, Resource resource) throws IOException {
|
public void compose(OutputStream stream, Resource resource) throws IOException {
|
||||||
OutputStreamWriter osw = new OutputStreamWriter(stream, "UTF-8");
|
OutputStreamWriter osw = new OutputStreamWriter(stream, "UTF-8");
|
||||||
if (style == OutputStyle.CANONICAL)
|
if (style == OutputStyle.CANONICAL) {
|
||||||
json = new JsonCreatorCanonical(osw);
|
json = new JsonCreatorCanonical(osw);
|
||||||
else
|
} else if (style == OutputStyle.PRETTY) {
|
||||||
json = new JsonCreatorDirect(osw); // use this instead of Gson because this preserves decimal formatting
|
json = new JsonCreatorDirect(osw, true, false);
|
||||||
json.setIndent(style == OutputStyle.PRETTY ? " " : "");
|
} else {
|
||||||
|
json = new JsonCreatorDirect(osw, false, false); // use this instead of Gson because this preserves decimal formatting
|
||||||
|
}
|
||||||
json.beginObject();
|
json.beginObject();
|
||||||
composeResource(resource);
|
composeResource(resource);
|
||||||
json.endObject();
|
json.endObject();
|
||||||
|
@ -207,11 +209,13 @@ public abstract class JsonParserBase extends ParserBase implements IParser {
|
||||||
@Override
|
@Override
|
||||||
public void compose(OutputStream stream, DataType type, String rootName) throws IOException {
|
public void compose(OutputStream stream, DataType type, String rootName) throws IOException {
|
||||||
OutputStreamWriter osw = new OutputStreamWriter(stream, "UTF-8");
|
OutputStreamWriter osw = new OutputStreamWriter(stream, "UTF-8");
|
||||||
if (style == OutputStyle.CANONICAL)
|
if (style == OutputStyle.CANONICAL) {
|
||||||
json = new JsonCreatorCanonical(osw);
|
json = new JsonCreatorCanonical(osw);
|
||||||
else
|
} else if (style == OutputStyle.PRETTY) {
|
||||||
json = new JsonCreatorDirect(osw);// use this instead of Gson because this preserves decimal formatting
|
json = new JsonCreatorDirect(osw, true, false);
|
||||||
json.setIndent(style == OutputStyle.PRETTY ? " " : "");
|
} else {
|
||||||
|
json = new JsonCreatorDirect(osw, false, false); // use this instead of Gson because this preserves decimal formatting
|
||||||
|
}
|
||||||
json.beginObject();
|
json.beginObject();
|
||||||
composeTypeInner(type);
|
composeTypeInner(type);
|
||||||
json.endObject();
|
json.endObject();
|
||||||
|
|
|
@ -41,7 +41,7 @@ public class JsonDirectTests {
|
||||||
@Test
|
@Test
|
||||||
public void testEmptyObject() throws FHIRFormatError, FileNotFoundException, IOException {
|
public void testEmptyObject() throws FHIRFormatError, FileNotFoundException, IOException {
|
||||||
ByteArrayOutputStream bs = new ByteArrayOutputStream();
|
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.beginObject();
|
||||||
json.name("a");
|
json.name("a");
|
||||||
json.beginObject();
|
json.beginObject();
|
||||||
|
|
|
@ -77,10 +77,10 @@ public class Turtle {
|
||||||
return predicate(predicate, new StringType(object));
|
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);
|
predicateSet.add(predicate);
|
||||||
objectSet.add(object);
|
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) {
|
public Complex predicate(String predicate, Triple object) {
|
||||||
|
@ -104,12 +104,13 @@ public class Turtle {
|
||||||
return null;
|
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);
|
Predicate p = getPredicate(predicate);
|
||||||
if (p == null) {
|
if (p == null) {
|
||||||
p = new Predicate();
|
p = new Predicate();
|
||||||
p.predicate = predicate;
|
p.predicate = predicate;
|
||||||
p.link = link;
|
p.link = link;
|
||||||
|
p.comment = comment;
|
||||||
predicateSet.add(predicate);
|
predicateSet.add(predicate);
|
||||||
predicates.add(p);
|
predicates.add(p);
|
||||||
}
|
}
|
||||||
|
@ -126,10 +127,10 @@ public class Turtle {
|
||||||
return c;
|
return c;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Complex linkedPredicate(String predicate, String link) {
|
public Complex linkedPredicate(String predicate, String link, String comment) {
|
||||||
predicateSet.add(predicate);
|
predicateSet.add(predicate);
|
||||||
Complex c = complex();
|
Complex c = complex();
|
||||||
linkedPredicate(predicate, c, link);
|
linkedPredicate(predicate, c, link, comment);
|
||||||
return c;
|
return c;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -197,6 +198,7 @@ public class Turtle {
|
||||||
}
|
}
|
||||||
|
|
||||||
public class Section {
|
public class Section {
|
||||||
|
private List<String> comments = new ArrayList<>();
|
||||||
private String name;
|
private String name;
|
||||||
private List<Subject> subjects = new ArrayList<Subject>();
|
private List<Subject> subjects = new ArrayList<Subject>();
|
||||||
|
|
||||||
|
@ -244,6 +246,10 @@ public class Turtle {
|
||||||
return true;
|
return true;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void stringComment(String cnt) {
|
||||||
|
comments.add(cnt);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private List<Section> sections = new ArrayList<Section>();
|
private List<Section> sections = new ArrayList<Section>();
|
||||||
|
@ -437,6 +443,12 @@ public class Turtle {
|
||||||
private void commitSection(LineOutputStreamWriter writer, Section section) throws IOException {
|
private void commitSection(LineOutputStreamWriter writer, Section section) throws IOException {
|
||||||
writer.ln("# - "+section.name+" "+Utilities.padLeft("", '-', 75-section.name.length()));
|
writer.ln("# - "+section.name+" "+Utilities.padLeft("", '-', 75-section.name.length()));
|
||||||
writer.ln();
|
writer.ln();
|
||||||
|
if (!section.comments.isEmpty()) {
|
||||||
|
for (String s : section.comments) {
|
||||||
|
writer.ln("# "+s);
|
||||||
|
}
|
||||||
|
writer.ln();
|
||||||
|
}
|
||||||
for (Subject sbj : section.subjects) {
|
for (Subject sbj : section.subjects) {
|
||||||
if (Utilities.noString(sbj.id)) {
|
if (Utilities.noString(sbj.id)) {
|
||||||
writer.write("[");
|
writer.write("[");
|
||||||
|
@ -447,6 +459,7 @@ public class Turtle {
|
||||||
int i = 0;
|
int i = 0;
|
||||||
|
|
||||||
for (Predicate p : sbj.predicates) {
|
for (Predicate p : sbj.predicates) {
|
||||||
|
//
|
||||||
writer.write(p.getPredicate());
|
writer.write(p.getPredicate());
|
||||||
writer.write(" ");
|
writer.write(" ");
|
||||||
boolean first = true;
|
boolean first = true;
|
||||||
|
@ -481,12 +494,19 @@ public class Turtle {
|
||||||
private void commitSection(StringBuilder b, Section section) throws Exception {
|
private void commitSection(StringBuilder b, Section section) throws Exception {
|
||||||
b.append("# - "+section.name+" "+Utilities.padLeft("", '-', 75-section.name.length())+"\r\n");
|
b.append("# - "+section.name+" "+Utilities.padLeft("", '-', 75-section.name.length())+"\r\n");
|
||||||
b.append("\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) {
|
for (Subject sbj : section.subjects) {
|
||||||
b.append(Utilities.escapeXml(sbj.id));
|
b.append(Utilities.escapeXml(sbj.id));
|
||||||
b.append(" ");
|
b.append(" ");
|
||||||
int i = 0;
|
int i = 0;
|
||||||
|
|
||||||
for (Predicate p : sbj.predicates) {
|
for (Predicate p : sbj.predicates) {
|
||||||
|
// b.append("# test\r\n ");
|
||||||
b.append(p.makelink());
|
b.append(p.makelink());
|
||||||
b.append(" ");
|
b.append(" ");
|
||||||
boolean first = true;
|
boolean first = true;
|
||||||
|
|
|
@ -480,9 +480,9 @@ public class XMLWriter extends OutputStreamWriter implements IXMLWriter {
|
||||||
writePretty();
|
writePretty();
|
||||||
}
|
}
|
||||||
if (levels.inComment())
|
if (levels.inComment())
|
||||||
write("<!-- "+comment+" -- >");
|
write(" <!-- "+comment+" -- >");
|
||||||
else
|
else
|
||||||
write("<!-- "+comment+" -->");
|
write(" <!-- "+comment+" -->");
|
||||||
if (doPretty && !isPretty())
|
if (doPretty && !isPretty())
|
||||||
writePretty();
|
writePretty();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue