Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch remaining R5 Turtle RDF generation to TurtleParser implementation #148

Merged
merged 13 commits into from
Dec 17, 2024
Merged
24 changes: 16 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,15 +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) {
IParser rdf = new RdfParser().setOutputStyle(OutputStyle.PRETTY);
rdf.compose(new FileOutputStream(Utilities.path(page.getFolders().dstDir, baseFileName + ".ttl")), 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 @@ -4494,16 +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 {
IParser rdf = new RdfParser().setOutputStyle(OutputStyle.PRETTY);
// rdf.setSuppressXhtml("Snipped for Brevity");
rdf.compose(bytes, r);
org.hl7.fhir.r5.elementmodel.Element resourceElement = parseR5ElementFromResource(r);
ParserBase tp = Manager.makeParser(page.getWorkerContext(), FhirFormat.TURTLE);
tp.compose(resourceElement, bytes, OutputStyle.PRETTY, null);
}
bytes.close();
return new String(bytes.toByteArray());
Expand Down Expand Up @@ -5583,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
Loading