From 7b19236173ca67c6a1d51074741c889ec155955b Mon Sep 17 00:00:00 2001 From: Tim Prudhomme Date: Thu, 18 Apr 2024 22:38:37 -0400 Subject: [PATCH] Fix TTL resource serialization with TurtleParser --- .../hl7/fhir/tools/publisher/Publisher.java | 23 ++++++++++++------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/src/main/java/org/hl7/fhir/tools/publisher/Publisher.java b/src/main/java/org/hl7/fhir/tools/publisher/Publisher.java index 0f8d3ba..784be4d 100644 --- a/src/main/java/org/hl7/fhir/tools/publisher/Publisher.java +++ b/src/main/java/org/hl7/fhir/tools/publisher/Publisher.java @@ -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; @@ -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()); } }; @@ -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()); @@ -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());