Skip to content

Commit

Permalink
Fix TTL resource serialization with TurtleParser
Browse files Browse the repository at this point in the history
  • Loading branch information
tmprd committed Apr 19, 2024
1 parent d12b093 commit 7b19236
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions src/main/java/org/hl7/fhir/tools/publisher/Publisher.java
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWIS
import org.hl7.fhir.r5.elementmodel.Manager.FhirFormat;
import org.hl7.fhir.r5.elementmodel.ParserBase;
import org.hl7.fhir.r5.elementmodel.ParserBase.ValidationPolicy;
import org.hl7.fhir.r5.elementmodel.ResourceParser;
import org.hl7.fhir.r5.fhirpath.FHIRPathEngine;
import org.hl7.fhir.r5.fhirpath.TypeDetails;
import org.hl7.fhir.r5.formats.FormatUtilities;
Expand Down Expand Up @@ -2116,14 +2117,16 @@ void serializeResource(Resource r, String baseFileName, String description, Stri
json.compose(new FileOutputStream(Utilities.path(page.getFolders().dstDir, baseFileName + ".canonical.json")), r);
}
if (showTtl) {
resource2Ttl(r);
org.hl7.fhir.r5.elementmodel.Element resourceElement = parseR5ElementFromResource(r);
ParserBase tp = Manager.makeParser(page.getWorkerContext(), FhirFormat.TURTLE);
tp.compose(resourceElement, new FileOutputStream(Utilities.path(page.getFolders().dstDir, baseFileName + ".ttl")), OutputStyle.PRETTY, null);
}
}
if (description!=null) {
cloneToXhtml(baseFileName, description, false, pageType, crumbTitle, null, wg, r.fhirType()+"/"+r.getId());
jsonToXhtml(baseFileName, description, resource2Json(r), pageType, crumbTitle, null, wg, r.fhirType()+"/"+r.getId());
if (showTtl)
ttlToXhtml(baseFileName, description, resource2Ttl(r), pageType, crumbTitle, null, wg, r.fhirType()+"/"+r.getId());
ttlToXhtml(baseFileName, description, convertResourceToTtl(r), pageType, crumbTitle, null, wg, r.fhirType()+"/"+r.getId());
}
};

Expand Down Expand Up @@ -4493,18 +4496,22 @@ private String resource2Json(Resource r) throws Exception {
return new String(bytes.toByteArray());
}

private String resource2Ttl(Resource r) throws Exception {
private org.hl7.fhir.r5.elementmodel.Element parseR5ElementFromResource(Resource resource) {
// Some R5+ serializations rely on r5.elementmodel.Element, so transform Resource directly into Element for this purpose (instead of getting Element by re-parsing another serialization)
ResourceParser resourceParser = new org.hl7.fhir.r5.elementmodel.ResourceParser(page.getWorkerContext());
return resourceParser.parse(resource);
}

private String convertResourceToTtl(Resource r) throws Exception {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
if (VersionUtilities.isR4BVer(page.getVersion().toCode())) {
org.hl7.fhir.r4.formats.IParser rdf = new org.hl7.fhir.r4.formats.RdfParser().setOutputStyle(org.hl7.fhir.r4.formats.IParser.OutputStyle.PRETTY);
// rdf.setSuppressXhtml("Snipped for Brevity");
rdf.compose(bytes, VersionConvertorFactory_40_50.convertResource(r));
} else {
// R5+ serialization relies on r5.elementmodel.Element, so transform Resource directly into it (instead of getting Element by re-parsing another serialization)
org.hl7.fhir.r5.elementmodel.ResourceParser resourceParser = new org.hl7.fhir.r5.elementmodel.ResourceParser(page.getWorkerContext());
org.hl7.fhir.r5.elementmodel.Element exe = resourceParser.parse(r);
org.hl7.fhir.r5.elementmodel.Element resourceElement = parseR5ElementFromResource(r);
ParserBase tp = Manager.makeParser(page.getWorkerContext(), FhirFormat.TURTLE);
tp.compose(exe, bytes, OutputStyle.PRETTY, null);
tp.compose(resourceElement, bytes, OutputStyle.PRETTY, null);
}
bytes.close();
return new String(bytes.toByteArray());
Expand Down Expand Up @@ -5584,7 +5591,7 @@ private StructureDefinition generateProfile(ResourceDefn root, String n, String
saveAsPureHtml(rp, new FileOutputStream(page.getFolders().dstDir + "html" + File.separator + n + ".html"));
cloneToXhtml(n + ".profile", "StructureDefinition for " + n, true, "profile-instance:resource:" + root.getName(), "Profile", root, root.getWg(), rp.fhirType()+"/"+rp.getId());
jsonToXhtml(n + ".profile", "StructureDefinition for " + n, resource2Json(rp), "profile-instance:resource:" + root.getName(), "Profile", root, root.getWg(), rp.fhirType()+"/"+rp.getId());
ttlToXhtml(n + ".profile", "StructureDefinition for " + n, resource2Ttl(rp), "profile-instance:resource:" + root.getName(), "Profile", root, root.getWg(), rp.fhirType()+"/"+rp.getId());
ttlToXhtml(n + ".profile", "StructureDefinition for " + n, convertResourceToTtl(rp), "profile-instance:resource:" + root.getName(), "Profile", root, root.getWg(), rp.fhirType()+"/"+rp.getId());
String shex = new ShExGenerator(page.getWorkerContext()).generate(HTMLLinkPolicy.NONE, rp);
TextFile.stringToFile(shex, page.getFolders().dstDir + n+".shex");
shexToXhtml(n, "ShEx statement for " + n, shex, "profile-instance:type:" + root.getName(), "Type", root, root.getWg(), rp.fhirType()+"/"+rp.getId());
Expand Down

0 comments on commit 7b19236

Please sign in to comment.