Created
April 15, 2020 15:52
-
-
Save keithboone/e7e2e21be7a8ac69578fe035fb39f7f8 to your computer and use it in GitHub Desktop.
Converting between JSON and XML for FHIR.
This file contains 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.ainq.kboone.tools; | |
import java.io.File; | |
import java.io.IOException; | |
import java.nio.charset.StandardCharsets; | |
import java.util.List; | |
import org.apache.commons.io.FileUtils; | |
import org.hl7.fhir.instance.model.api.IBaseResource; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
import org.springframework.boot.ApplicationArguments; | |
import org.springframework.boot.DefaultApplicationArguments; | |
import ca.uhn.fhir.context.FhirContext; | |
import ca.uhn.fhir.parser.IParser; | |
/** | |
* Convert a file from JSON to XML or XML to JSON | |
* using various FHIR Versions. | |
* | |
* To use this, you need | |
* <dependency> | |
* <groupId>ca.uhn.hapi.fhir</groupId> | |
* <artifactId>hapi-fhir-structures-hl7org-dstu2</artifactId> | |
* <version>4.2.0</version> | |
* </dependency> | |
* <dependency> | |
* <groupId>ca.uhn.hapi.fhir</groupId> | |
* <artifactId>hapi-fhir-structures-r4</artifactId> | |
* <version>4.2.0</version> | |
* </dependency> | |
* <dependency> | |
* <groupId>ca.uhn.hapi.fhir</groupId> | |
* <artifactId>hapi-fhir-validation-resources-dstu2</artifactId> | |
* <version>4.2.0</version> | |
* </dependency> | |
* <dependency> | |
* <groupId>org.springframework.boot</groupId> | |
* <artifactId>spring-boot-starter-actuator</artifactId> | |
* <version>1.5.6.RELEASE</version> | |
* </dependency> | |
* | |
* @author Keith W. Boone | |
* | |
*/ | |
public class Converter { | |
public static final Logger LOGGER = LoggerFactory.getLogger(Converter.class); | |
private static <T> T coalesce(T a, T b) { | |
return a != null ? a : b; | |
} | |
public static void main(String args[]) { | |
ApplicationArguments cmdLineArgs = new DefaultApplicationArguments(args); | |
FhirContext ctx = null; | |
String version = "default"; | |
if (cmdLineArgs.containsOption("v") || cmdLineArgs.containsOption("version")) { | |
List<String> versions = coalesce(cmdLineArgs.getOptionValues("v"), cmdLineArgs.getOptionValues("version")); | |
if (!versions.isEmpty()) { | |
if (versions.size() > 1) { | |
LOGGER.error("Can only convert one version at a time: {}", versions); | |
System.exit(1); | |
} | |
version = versions.get(0); | |
} | |
} | |
switch (version.toLowerCase()) { | |
case "dstu2": | |
ctx = FhirContext.forDstu2Hl7Org(); | |
break; | |
case "stu3": | |
ctx = FhirContext.forDstu3(); | |
break; | |
case "default": | |
case "r4": | |
ctx = FhirContext.forR4(); | |
break; | |
} | |
for (String arg: cmdLineArgs.getNonOptionArgs()) { | |
convert(ctx, arg); | |
} | |
} | |
private static void convert(FhirContext ctx, String arg) { | |
IParser jp = ctx.newJsonParser(), | |
xp = ctx.newXmlParser(); | |
String input = null, output = null, file = null; | |
try { | |
input = FileUtils.readFileToString(new File(arg), StandardCharsets.UTF_8); | |
} catch (IOException e) { | |
LOGGER.error("Error reading {}", arg, e); | |
return; | |
} | |
boolean inputIsXml = false; | |
if (arg.toLowerCase().endsWith(".xml")) { | |
inputIsXml = true; | |
} | |
IParser ip = inputIsXml ? xp : jp, | |
op = inputIsXml ? jp : xp; | |
IBaseResource ri = null; | |
ri = ip.parseResource(input); | |
output = op.setPrettyPrint(true).encodeResourceToString(ri); | |
try { | |
file = arg.replace(inputIsXml ? "xml" : "json", | |
inputIsXml ? "json" : "xml"); | |
FileUtils.writeStringToFile(new File(file), output, StandardCharsets.UTF_8); | |
} catch (IOException e) { | |
LOGGER.error("Error reading {}", arg, e); | |
return; | |
} | |
LOGGER.info("Converted {} to {}", arg, file); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is a simple program to Convert between JSON and XML representations for FHIR Resources.
Usage: java com.ainq.kboone.tools.Converter resourceFileName{.json|.xml} ...