From 9f461c52c912e7a8b06010f92380b4d22d6cd3fd Mon Sep 17 00:00:00 2001 From: Tim Prudhomme Date: Thu, 7 Sep 2023 18:25:00 -0400 Subject: [PATCH 1/3] Switch remaining (R5) RDF generation to TurtleParser implementation --- .../org/hl7/fhir/tools/publisher/Publisher.java | 17 +++++++++-------- 1 file changed, 9 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 44c0aa32..bba9870f 100644 --- a/src/main/java/org/hl7/fhir/tools/publisher/Publisher.java +++ b/src/main/java/org/hl7/fhir/tools/publisher/Publisher.java @@ -2103,15 +2103,14 @@ void serializeResource(Resource r, String baseFileName, String description, Stri json.compose(new FileOutputStream(Utilities.path(page.getFolders().dstDir, baseFileName + ".canonical.json")), r); } if (showTtl) { - IParser rdf = new RdfParser().setOutputStyle(OutputStyle.PRETTY); - rdf.compose(new FileOutputStream(Utilities.path(page.getFolders().dstDir, baseFileName + ".ttl")), r); + resource2Ttl(r, baseFileName); } } 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, resource2Ttl(r, baseFileName), pageType, crumbTitle, null, wg, r.fhirType()+"/"+r.getId()); } }; @@ -4473,16 +4472,18 @@ private String resource2Json(Resource r) throws Exception { return new String(bytes.toByteArray()); } - private String resource2Ttl(Resource r) throws Exception { + private String resource2Ttl(Resource r, String baseFileName) 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 { - IParser rdf = new RdfParser().setOutputStyle(OutputStyle.PRETTY); -// rdf.setSuppressXhtml("Snipped for Brevity"); - rdf.compose(bytes, r); + // TODO get r5.elementmodel.Element direct from XML serialization (?) instead of re-parsing file + ParserBase xp = Manager.makeParser(page.getWorkerContext(), FhirFormat.XML); + org.hl7.fhir.r5.elementmodel.Element exe = xp.parseSingle(new FileInputStream(Utilities.path(page.getFolders().dstDir, baseFileName + ".xml"))); + ParserBase tp = Manager.makeParser(page.getWorkerContext(), FhirFormat.TURTLE); + tp.compose(exe, bytes, OutputStyle.PRETTY, null); } bytes.close(); return new String(bytes.toByteArray()); @@ -5547,7 +5548,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, resource2Ttl(rp, fName), "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()); From 0c396e0dda89a608ded36bd62895516f2504f935 Mon Sep 17 00:00:00 2001 From: Tim Prudhomme Date: Wed, 4 Oct 2023 10:21:38 -0400 Subject: [PATCH 2/3] Serialize turtle Element from Resource directly --- .../org/hl7/fhir/tools/publisher/Publisher.java | 16 ++++++++-------- 1 file changed, 8 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 bba9870f..a3c5448d 100644 --- a/src/main/java/org/hl7/fhir/tools/publisher/Publisher.java +++ b/src/main/java/org/hl7/fhir/tools/publisher/Publisher.java @@ -2103,14 +2103,14 @@ 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, baseFileName); + resource2Ttl(r); } } 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, baseFileName), pageType, crumbTitle, null, wg, r.fhirType()+"/"+r.getId()); + ttlToXhtml(baseFileName, description, resource2Ttl(r), pageType, crumbTitle, null, wg, r.fhirType()+"/"+r.getId()); } }; @@ -4472,18 +4472,18 @@ private String resource2Json(Resource r) throws Exception { return new String(bytes.toByteArray()); } - private String resource2Ttl(Resource r, String baseFileName) throws Exception { + private String resource2Ttl(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 { - // TODO get r5.elementmodel.Element direct from XML serialization (?) instead of re-parsing file - ParserBase xp = Manager.makeParser(page.getWorkerContext(), FhirFormat.XML); - org.hl7.fhir.r5.elementmodel.Element exe = xp.parseSingle(new FileInputStream(Utilities.path(page.getFolders().dstDir, baseFileName + ".xml"))); + // 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); ParserBase tp = Manager.makeParser(page.getWorkerContext(), FhirFormat.TURTLE); - tp.compose(exe, bytes, OutputStyle.PRETTY, null); + tp.compose(exe, bytes, OutputStyle.PRETTY, null); } bytes.close(); return new String(bytes.toByteArray()); @@ -5548,7 +5548,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, fName), "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()); 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()); From 7b19236173ca67c6a1d51074741c889ec155955b Mon Sep 17 00:00:00 2001 From: Tim Prudhomme Date: Thu, 18 Apr 2024 22:38:37 -0400 Subject: [PATCH 3/3] 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 0f8d3ba3..784be4d8 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());