|
| 1 | +import ca.uhn.fhir.context.FhirContext; |
| 2 | +import ca.uhn.fhir.parser.IParser; |
| 3 | + |
| 4 | +import java.io.FileWriter; |
| 5 | +import java.io.IOException; |
| 6 | +import java.nio.file.Files; |
| 7 | +import java.nio.file.Path; |
| 8 | +import java.util.ArrayList; |
| 9 | +import java.util.List; |
| 10 | +import java.util.regex.Matcher; |
| 11 | +import java.util.regex.Pattern; |
| 12 | + |
| 13 | +public class ConvertToJson { |
| 14 | + |
| 15 | + private static String loadMaskedXML(String filePath, List<String> dtPlaceholders) { |
| 16 | + // First, load the raw XML |
| 17 | + String XML; |
| 18 | + try { |
| 19 | + XML = Files.readString(Path.of(filePath)); |
| 20 | + } catch (IOException e) { |
| 21 | + System.err.println(e.toString()); |
| 22 | + return ""; |
| 23 | + } |
| 24 | + |
| 25 | + // Collect and then replace all the datetime placeholders. |
| 26 | + Pattern dtPlaceholderPattern = Pattern.compile("\\$\\{(CURRENT)?DATE.*?\\}"); |
| 27 | + Matcher dtPlaceholderMatcher = dtPlaceholderPattern.matcher(XML); |
| 28 | + while (dtPlaceholderMatcher.find()) { |
| 29 | + dtPlaceholders.add(dtPlaceholderMatcher.group()); |
| 30 | + } |
| 31 | + |
| 32 | + // Now replace all datetime placeholders with our known pattern. |
| 33 | + XML = dtPlaceholderMatcher.replaceAll("0001-01-01"); |
| 34 | + return XML; |
| 35 | + } |
| 36 | + |
| 37 | + private static String unmaskJSON(String maskedJSON, List<String> dtPlaceholders) { |
| 38 | + // And restore all placeholders |
| 39 | + String unmaskedJSON = ""; |
| 40 | + Pattern dtMaskPattern = Pattern.compile("0001-01-01"); |
| 41 | + Matcher dtMaskMatcher = dtMaskPattern.matcher(maskedJSON); |
| 42 | + int pos = 0; |
| 43 | + int placeholderNum = 0; |
| 44 | + while (dtMaskMatcher.find()) { |
| 45 | + unmaskedJSON += maskedJSON.substring(pos, dtMaskMatcher.start()) + dtPlaceholders.get(placeholderNum); |
| 46 | + pos = dtMaskMatcher.end(); |
| 47 | + } |
| 48 | + unmaskedJSON += maskedJSON.substring(pos); |
| 49 | + return unmaskedJSON; |
| 50 | + } |
| 51 | + |
| 52 | + public static void main(String[] args) { |
| 53 | + String version = args[0]; |
| 54 | + |
| 55 | + String fileList; |
| 56 | + try { |
| 57 | + fileList = Files.readString(Path.of(args[1])); |
| 58 | + } catch (IOException e) { |
| 59 | + System.err.println(e.toString()); |
| 60 | + System.err.println("File containing the list of fixtures to convert couldn't be read."); |
| 61 | + return; |
| 62 | + } |
| 63 | + |
| 64 | + Object xmlParser = null; |
| 65 | + FhirContext context = null; |
| 66 | + if (version.equals("STU3")) { |
| 67 | + context = FhirContext.forDstu3(); |
| 68 | + xmlParser = new org.hl7.fhir.dstu3.formats.XmlParser(); |
| 69 | + } else if (version.equals("R4")) { |
| 70 | + context = FhirContext.forR4(); |
| 71 | + xmlParser = new org.hl7.fhir.r4.formats.XmlParser(); |
| 72 | + } |
| 73 | + |
| 74 | + IParser jsonParser = context.newJsonParser(); |
| 75 | + jsonParser.setPrettyPrint(true); |
| 76 | + |
| 77 | + String[] fixtures = fileList.split(" "); |
| 78 | + for (String fixture: fixtures) { |
| 79 | + List<String>dtPlaceholders = new ArrayList<String>(); |
| 80 | + String XML = loadMaskedXML(fixture, dtPlaceholders); |
| 81 | + String JSON = ""; |
| 82 | + try { |
| 83 | + if (version.equals("STU3")) { |
| 84 | + org.hl7.fhir.dstu3.model.Resource resource = ((org.hl7.fhir.dstu3.formats.XmlParser)xmlParser).parse(XML); |
| 85 | + JSON = jsonParser.encodeResourceToString(resource); |
| 86 | + } else if (version.equals("R4")) { |
| 87 | + org.hl7.fhir.r4.model.Resource resource = ((org.hl7.fhir.r4.formats.XmlParser)xmlParser).parse(XML); |
| 88 | + JSON = jsonParser.encodeResourceToString(resource); |
| 89 | + } |
| 90 | + } catch (IOException e) { |
| 91 | + System.out.print("Input file could not be parsed: " + fixture); |
| 92 | + return; |
| 93 | + } |
| 94 | + JSON = unmaskJSON(JSON, dtPlaceholders); |
| 95 | + |
| 96 | + // And write the result to disk |
| 97 | + String base_name = fixture.substring(0, fixture.lastIndexOf(".")); |
| 98 | + String out_path = base_name + ".json"; |
| 99 | + try { |
| 100 | + FileWriter jsonWriter = new FileWriter(out_path); |
| 101 | + jsonWriter.write(JSON); |
| 102 | + jsonWriter.close(); |
| 103 | + } catch (IOException e) { |
| 104 | + System.out.print("Output file could not be opened: " + out_path); |
| 105 | + } |
| 106 | + |
| 107 | + System.out.println("Converted fixture " + fixture + " to JSON"); |
| 108 | + } |
| 109 | + } |
| 110 | +} |
0 commit comments