Last active
July 7, 2024 19:47
-
-
Save bartolli/3bd1d991fdac6cf7422e149427541474 to your computer and use it in GitHub Desktop.
FhirConverter_v1.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.example; | |
| import ca.uhn.fhir.context.FhirContext; | |
| import ca.uhn.fhir.parser.IParser; | |
| import org.apache.jena.query.ARQ; | |
| import org.hl7.fhir.instance.model.api.IBaseResource; | |
| import java.io.IOException; | |
| import java.nio.file.Files; | |
| import java.nio.file.Paths; | |
| public class FhirConverter { | |
| private FhirContext ctx; | |
| public FhirConverter() { | |
| // Initialize FHIR context for R4 | |
| this.ctx = FhirContext.forR4(); | |
| // Initialize ARQ context | |
| ARQ.init(); | |
| } | |
| // Method to convert JSON to XML | |
| public String convertJsonToXml(String json) { | |
| IParser jsonParser = ctx.newJsonParser().setPrettyPrint(true); | |
| IBaseResource resource = jsonParser.parseResource(json); | |
| IParser xmlParser = ctx.newXmlParser().setPrettyPrint(true); | |
| return xmlParser.encodeResourceToString(resource); | |
| } | |
| // Method to convert XML to JSON | |
| public String convertXmlToJson(String xml) { | |
| IParser xmlParser = ctx.newXmlParser().setPrettyPrint(true); | |
| IBaseResource resource = xmlParser.parseResource(xml); | |
| IParser jsonParser = ctx.newJsonParser().setPrettyPrint(true); | |
| return jsonParser.encodeResourceToString(resource); | |
| } | |
| // Method to convert JSON to RDF (Turtle) | |
| public String convertJsonToTurtle(String json) { | |
| IParser jsonParser = ctx.newJsonParser(); | |
| IBaseResource resource = jsonParser.parseResource(json); | |
| IParser rdfParser = ctx.newRDFParser(); | |
| return rdfParser.encodeResourceToString(resource); | |
| } | |
| // Method to convert RDF (Turtle) to JSON | |
| public String convertTurtleToJson(String turtle) { | |
| IParser rdfParser = ctx.newRDFParser(); | |
| IBaseResource resource = rdfParser.parseResource(turtle); | |
| IParser jsonParser = ctx.newJsonParser().setPrettyPrint(true); | |
| return jsonParser.encodeResourceToString(resource); | |
| } | |
| public static void main(String[] args) { | |
| if (args.length != 3) { | |
| System.err.println("Usage: java -jar fhir-converter.jar <inputFile> <outputFile> <format>"); | |
| System.err.println("<format>: xml-to-json, json-to-xml, json-to-turtle, turtle-to-json"); | |
| System.exit(1); | |
| } | |
| String inputFile = args[0]; | |
| String outputFile = args[1]; | |
| String format = args[2]; | |
| try { | |
| FhirConverter converter = new FhirConverter(); | |
| String inputContent = new String(Files.readAllBytes(Paths.get(inputFile))); | |
| String outputContent = null; | |
| switch (format) { | |
| case "xml-to-json": | |
| outputContent = converter.convertXmlToJson(inputContent); | |
| break; | |
| case "json-to-xml": | |
| outputContent = converter.convertJsonToXml(inputContent); | |
| break; | |
| case "json-to-turtle": | |
| outputContent = converter.convertJsonToTurtle(inputContent); | |
| break; | |
| case "turtle-to-json": | |
| outputContent = converter.convertTurtleToJson(inputContent); | |
| break; | |
| default: | |
| System.err.println("Unknown format: " + format); | |
| System.exit(1); | |
| } | |
| Files.write(Paths.get(outputFile), outputContent.getBytes()); | |
| System.out.println("Conversion successful! Output written to " + outputFile); | |
| } catch (IOException e) { | |
| e.printStackTrace(); | |
| System.exit(1); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment