use of org.hl7.fhir.r4b.utils.structuremap.StructureMapUtilities in project org.hl7.fhir.core by hapifhir.
the class ResourceChecker method checkIsResource.
// protected static Manager.FhirFormat checkIsResource(SimpleWorkerContext context, boolean debug, String path) throws IOException {
//
// if (Utilities.existsInList(ext, "json"))
// return Manager.FhirFormat.JSON;
// if (Utilities.existsInList(ext, "map"))
// return Manager.FhirFormat.TEXT;
// if (Utilities.existsInList(ext, "txt"))
// return Manager.FhirFormat.TEXT;
// if (Utilities.existsInList(ext, "jwt", "jws"))
// return Manager.FhirFormat.SHC;
//
// return checkIsResource(context, debug, TextFile.fileToBytes(path), path);
// }
public static Manager.FhirFormat checkIsResource(SimpleWorkerContext context, boolean debug, byte[] cnt, String filename, boolean guessFromExtension) {
System.out.println(" ..Detect format for " + filename);
if (cnt.length == 0) {
System.out.println(" " + filename + " is empty");
return null;
}
if (guessFromExtension) {
String ext = Utilities.getFileExtension(filename);
if (Utilities.existsInList(ext, "xml")) {
return FhirFormat.XML;
}
if (Utilities.existsInList(ext, "ttl")) {
return FhirFormat.TURTLE;
}
if (Utilities.existsInList(ext, "map")) {
return Manager.FhirFormat.TEXT;
}
if (Utilities.existsInList(ext, "jwt", "jws")) {
return Manager.FhirFormat.SHC;
}
if (Utilities.existsInList(ext, "json")) {
// no, we have to look inside, and decide.
try {
JsonObject json = JsonTrackingParser.parseJson(cnt);
if (json.has("verifiableCredential")) {
return FhirFormat.SHC;
}
} catch (Exception e) {
}
return FhirFormat.JSON;
}
if (Utilities.existsInList(ext, "txt")) {
try {
String src = TextFile.bytesToString(cnt);
if (src.startsWith("shc:/")) {
return FhirFormat.SHC;
}
} catch (Exception e) {
}
return Manager.FhirFormat.TEXT;
}
}
try {
Manager.parse(context, new ByteArrayInputStream(cnt), Manager.FhirFormat.JSON);
return Manager.FhirFormat.JSON;
} catch (Exception e) {
if (debug) {
System.out.println("Not JSON: " + e.getMessage());
}
}
try {
ValidatorUtils.parseXml(cnt);
return Manager.FhirFormat.XML;
} catch (Exception e) {
if (debug) {
System.out.println("Not XML: " + e.getMessage());
}
}
try {
Manager.parse(context, new ByteArrayInputStream(cnt), Manager.FhirFormat.TURTLE);
return Manager.FhirFormat.TURTLE;
} catch (Exception e) {
if (debug) {
System.out.println("Not Turtle: " + e.getMessage());
}
}
try {
String s = new String(cnt, StandardCharsets.UTF_8);
if (s.startsWith("shc:/"))
s = SHCParser.decodeQRCode(s);
JWT jwt = new SHCParser(context).decodeJWT(s);
return Manager.FhirFormat.SHC;
} catch (Exception e) {
if (debug) {
System.out.println("Not a smart health card: " + e.getMessage());
}
}
try {
new StructureMapUtilities(context, null, null).parse(TextFile.bytesToString(cnt), null);
return Manager.FhirFormat.TEXT;
} catch (Exception e) {
if (debug) {
System.out.println("Not Text: " + e.getMessage());
}
}
if (debug)
System.out.println(" .. not a resource: " + filename);
return null;
}
use of org.hl7.fhir.r4b.utils.structuremap.StructureMapUtilities in project org.hl7.fhir.core by hapifhir.
the class StructureMapUtilitiesTest method testParseRuleName.
@Test
public void testParseRuleName() throws IOException, FHIRException {
StructureMapUtilities scu = new StructureMapUtilities(context, this);
String fileMap = TestingUtilities.loadTestResource("r5", "structure-mapping", "ActivityDefinition.map");
StructureMap structureMap = scu.parse(fileMap, "ActivityDefinition3To4");
// StructureMap/ActivityDefinition3to4: StructureMap.group[3].rule[2].name error id value '"expression"' is not valid
Assertions.assertEquals("expression", structureMap.getGroup().get(2).getRule().get(1).getName());
}
use of org.hl7.fhir.r4b.utils.structuremap.StructureMapUtilities in project org.hl7.fhir.core by hapifhir.
the class R3R4ConversionTests method test.
@SuppressWarnings("deprecation")
@ParameterizedTest(name = "{index}: id {0}")
@MethodSource("data")
public void test(String name, byte[] content) throws Exception {
checkLoad();
StructureMapUtilities smu4 = new StructureMapUtilities(contextR4, this);
StructureMapUtilities smu3 = new StructureMapUtilities(contextR3, this);
String tn = null;
workingid = null;
byte[] cnt = content;
Exception executionError = null;
List<ValidationMessage> r4validationErrors = new ArrayList<ValidationMessage>();
String roundTripError = null;
try {
extras = new ArrayList<Resource>();
// load the example (r3)
org.hl7.fhir.r4.elementmodel.Element r3 = new org.hl7.fhir.r4.elementmodel.XmlParser(contextR3).parse(new ByteArrayInputStream(content));
tn = r3.fhirType();
workingid = r3.getChildValue("id");
if (SAVING) {
ByteArrayOutputStream bso = new ByteArrayOutputStream();
new org.hl7.fhir.r4.elementmodel.JsonParser(contextR3).compose(r3, bso, OutputStyle.PRETTY, null);
cnt = bso.toByteArray();
Utilities.createDirectory(Utilities.path(TestingUtilities.home(), "implementations", "r3maps", "test-output"));
TextFile.bytesToFile(cnt, Utilities.path(TestingUtilities.home(), "implementations", "r3maps", "test-output", tn + "-" + workingid + ".input.json"));
}
String mapFile = Utilities.path(TestingUtilities.home(), "implementations", "r3maps", "R3toR4", r3.fhirType() + ".map");
if (new File(mapFile).exists()) {
StructureMap sm = smu4.parse(TextFile.fileToString(mapFile), mapFile);
tn = smu4.getTargetType(sm).getType();
// convert from r3 to r4
Resource r4 = ResourceFactory.createResource(tn);
smu4.transform(contextR4, r3, sm, r4);
ByteArrayOutputStream bs = new ByteArrayOutputStream();
new org.hl7.fhir.r4.formats.JsonParser().setOutputStyle(OutputStyle.PRETTY).compose(bs, r4);
if (SAVING) {
TextFile.bytesToFile(bs.toByteArray(), Utilities.path(TestingUtilities.home(), "implementations", "r3maps", "test-output", tn + "-" + workingid + ".r4.json"));
for (Resource r : extras) {
bs = new ByteArrayOutputStream();
new org.hl7.fhir.r4.formats.JsonParser().setOutputStyle(OutputStyle.PRETTY).compose(bs, r);
TextFile.bytesToFile(bs.toByteArray(), Utilities.path(TestingUtilities.home(), "implementations", "r3maps", "test-output", r.fhirType() + "-" + r.getId() + ".r4.json"));
}
}
// validate against R4
IResourceValidator validator = contextR4.newValidator();
validator.setNoTerminologyChecks(true);
validator.setFetcher(this);
validator.validate(null, r4validationErrors, r4);
// load the R4 to R3 map
mapFile = Utilities.path(TestingUtilities.home(), "implementations", "r3maps", "R4toR3", getMapFor(r4.fhirType(), r3.fhirType()) + ".map");
sm = smu3.parse(TextFile.fileToString(mapFile), mapFile);
// convert to R3
StructureDefinition sd = smu3.getTargetType(sm);
org.hl7.fhir.r4.elementmodel.Element ro3 = Manager.build(contextR3, sd);
smu3.transform(contextR3, r4, sm, ro3);
// compare the XML
bs = new ByteArrayOutputStream();
new org.hl7.fhir.r4.elementmodel.JsonParser(contextR3).compose(ro3, bs, OutputStyle.PRETTY, null);
if (SAVING)
TextFile.bytesToFile(bs.toByteArray(), Utilities.path(TestingUtilities.home(), "implementations", "r3maps", "test-output", tn + "-" + workingid + ".output.json"));
// check(errors, tn, workingid);
roundTripError = TestingUtilities.checkJsonSrcIsSame(new String(cnt), new String(bs.toByteArray()), filter != null);
if (roundTripError != null && roundTripError.equals(rules.getStringProperty(tn + "/" + workingid, "roundtrip")))
roundTripError = null;
} else {
if (loadErrors.containsKey(r3.fhirType() + ".map")) {
executionError = loadErrors.get(r3.fhirType() + ".map");
}
}
} catch (Exception e) {
executionError = e;
}
if (tn != null && workingid != null)
updateOutcomes(tn, workingid, executionError, r4validationErrors, roundTripError);
if (executionError != null)
throw executionError;
}
use of org.hl7.fhir.r4b.utils.structuremap.StructureMapUtilities in project org.hl7.fhir.core by hapifhir.
the class R3R4ConversionTests method loadLib.
private void loadLib(String dir) throws FileNotFoundException, IOException {
StructureMapUtilities smu = new StructureMapUtilities(contextR4);
for (String s : new File(dir).list()) {
String map = TextFile.fileToString(Utilities.path(dir, s));
try {
StructureMap sm = smu.parse(map, s);
contextR3.cacheResource(sm);
contextR4.cacheResource(sm);
for (Resource r : sm.getContained()) {
if (r instanceof MetadataResource) {
MetadataResource mr = (MetadataResource) r.copy();
mr.setUrl(sm.getUrl() + "#" + r.getId());
contextR3.cacheResource(mr);
contextR4.cacheResource(mr);
}
}
} catch (FHIRException e) {
System.out.println("Unable to load " + Utilities.path(dir, s) + ": " + e.getMessage());
loadErrors.put(s, e);
// e.printStackTrace();
}
}
}
use of org.hl7.fhir.r4b.utils.structuremap.StructureMapUtilities in project kindling by HL7.
the class PageProcessor method dtR3R4Transform.
private String dtR3R4Transform(String name) throws Exception {
File f = new File(Utilities.path(folders.rootDir, "implementations", "r3maps", "R4toR3", name + ".map"));
if (!f.exists()) {
return "No R3/R4 map exists for " + name;
}
String n = name.toLowerCase();
String status = r3r4StatusForResource(name);
String fwds = TextFile.fileToString(Utilities.path(folders.rootDir, "implementations", "r3maps", "R3toR4", r3nameForResource(name) + ".map"));
String bcks = TextFile.fileToString(Utilities.path(folders.rootDir, "implementations", "r3maps", "R4toR3", name + ".map"));
String fwdsStatus = "";
String bcksStatus = "";
try {
new StructureMapUtilities(workerContext).parse(fwds, r3nameForResource(name) + ".map");
} catch (FHIRException e) {
fwdsStatus = "<p style=\"background-color: #ffb3b3; border:1px solid maroon; padding: 5px;\">This script does not compile: " + e.getMessage() + "</p>\r\n";
}
try {
new StructureMapUtilities(workerContext).parse(bcks, name + ".map");
} catch (FHIRException e) {
bcksStatus = "<p style=\"background-color: #ffb3b3; border:1px solid maroon; padding: 5px;\">This script does not compile: " + e.getMessage() + "</p>\r\n";
}
return "<p>Functional status for this map: " + status + " (based on R2 -> R3 -> R2 round tripping)</p>\r\n" + "\r\n" + "<h4>R3 to R4</h4>\r\n" + "\r\n" + "<div class=\"mapping\">\r\n" + "<pre>\r\n" + Utilities.escapeXml(fwds) + "\r\n" + "</pre>\r\n" + "</div>\r\n" + "\r\n" + fwdsStatus + "\r\n" + "\r\n" + "<h4>R4 to R3</h4>\r\n" + "\r\n" + "<div class=\"mapping\">\r\n" + "<pre>\r\n" + Utilities.escapeXml(bcks) + "\r\n" + "</pre>\r\n" + "</div>\r\n" + "\r\n" + bcksStatus + "\r\n";
}
Aggregations