use of org.hl7.fhir.r4.utils.StructureMapUtilities in project org.hl7.fhir.core by hapifhir.
the class StructureMapTests method testLoadCDA.
@Test
public void testLoadCDA() throws FileNotFoundException, Exception {
Map<String, StructureMap> maps = new HashMap<String, StructureMap>();
if (TestingUtilities.context == null)
TestingUtilities.context = SimpleWorkerContext.fromPack("C:\\work\\org.hl7.fhir\\build\\publish\\validation-min.xml.zip");
StructureMapUtilities scu = new StructureMapUtilities(TestingUtilities.context, maps, null);
for (String f : new File("C:\\work\\org.hl7.fhir\\build\\guides\\ccda\\CDA").list()) {
try {
StructureDefinition sd = (StructureDefinition) new XmlParser().parse(new FileInputStream("C:\\work\\org.hl7.fhir\\build\\guides\\ccda\\CDA\\" + f));
((SimpleWorkerContext) TestingUtilities.context).seeResource(sd.getUrl(), sd);
} catch (Exception e) {
}
}
for (String f : new File("C:\\work\\org.hl7.fhir\\build\\guides\\ccda\\maps").list()) {
try {
StructureMap map = scu.parse(TextFile.fileToString("C:\\work\\org.hl7.fhir\\build\\guides\\ccda\\maps\\" + f));
maps.put(map.getUrl(), map);
} catch (Exception e) {
}
}
Element cda = Manager.parse(TestingUtilities.context, new FileInputStream("C:\\work\\org.hl7.fhir\\build\\guides\\ccda\\Example\\ccd.xml"), FhirFormat.XML);
Manager.compose(TestingUtilities.context, cda, new FileOutputStream("C:\\work\\org.hl7.fhir\\build\\guides\\ccda\\Example\\ccd.out.json"), FhirFormat.JSON, OutputStyle.PRETTY, null);
Manager.compose(TestingUtilities.context, cda, new FileOutputStream("C:\\work\\org.hl7.fhir\\build\\guides\\ccda\\Example\\ccd.out.xml"), FhirFormat.XML, OutputStyle.PRETTY, null);
Bundle bundle = new Bundle();
scu.transform(null, cda, maps.get("http://hl7.org/fhir/StructureMap/cda"), bundle);
new XmlParser().setOutputStyle(OutputStyle.PRETTY).compose(new FileOutputStream(Utilities.path("[tmp]", "bundle.xml")), bundle);
}
use of org.hl7.fhir.r4.utils.StructureMapUtilities in project org.hl7.fhir.core by hapifhir.
the class R2R3ConversionManager method prepare.
private void prepare() throws FHIRException {
if (contextR2 == null)
throw new FHIRException("No R2 definitions provided");
if (contextR3 == null)
throw new FHIRException("No R3 definitions provided");
if (library == null)
throw new FHIRException("No R2/R# conversion maps provided");
if (needPrepare) {
for (StructureDefinition sd : contextR2.allStructures()) {
StructureDefinition sdn = sd.copy();
sdn.getExtension().clear();
contextR3.seeResource(sdn.getUrl(), sdn);
}
for (StructureDefinition sd : contextR3.allStructures()) {
if (sd.getKind() == StructureDefinitionKind.PRIMITIVETYPE) {
contextR2.seeResource(sd.getUrl(), sd);
StructureDefinition sdn = sd.copy();
sdn.setUrl(sdn.getUrl().replace("http://hl7.org/fhir/", "http://hl7.org/fhir/DSTU2/"));
sdn.addExtension().setUrl("http://hl7.org/fhir/StructureDefinition/elementdefinition-namespace").setValue(new UriType("http://hl7.org/fhir"));
contextR2.seeResource(sdn.getUrl(), sdn);
contextR3.seeResource(sdn.getUrl(), sdn);
}
}
contextR2.setExpansionProfile(new ExpansionProfile().setUrl("urn:uuid:" + UUID.randomUUID().toString().toLowerCase()));
contextR3.setExpansionProfile(new ExpansionProfile().setUrl("urn:uuid:" + UUID.randomUUID().toString().toLowerCase()));
smu3 = new StructureMapUtilities(contextR3, library, this);
smu2 = new StructureMapUtilities(contextR2, library, this);
needPrepare = false;
}
}
use of org.hl7.fhir.r4.utils.StructureMapUtilities in project org.hl7.fhir.core by hapifhir.
the class IgLoader method checkFormat.
private Manager.FhirFormat checkFormat(byte[] cnt, String filename) {
System.out.println(" ..Detect format for " + filename);
try {
JsonTrackingParser.parseJson(cnt);
return Manager.FhirFormat.JSON;
} catch (Exception e) {
log("Not JSON: " + e.getMessage());
}
try {
ValidatorUtils.parseXml(cnt);
return Manager.FhirFormat.XML;
} catch (Exception e) {
log("Not XML: " + e.getMessage());
}
try {
new Turtle().parse(TextFile.bytesToString(cnt));
return Manager.FhirFormat.TURTLE;
} catch (Exception e) {
log("Not Turtle: " + e.getMessage());
}
try {
new StructureMapUtilities(getContext(), null, null).parse(TextFile.bytesToString(cnt), null);
return Manager.FhirFormat.TEXT;
} catch (Exception e) {
log("Not Text: " + e.getMessage());
}
log(" .. not a resource: " + filename);
return null;
}
use of org.hl7.fhir.r4.utils.StructureMapUtilities in project org.hl7.fhir.core by hapifhir.
the class IgLoader method loadResourceByVersion.
public Resource loadResourceByVersion(String fhirVersion, byte[] content, String fn) throws IOException, FHIRException {
Resource r;
if (fhirVersion.startsWith("3.0")) {
org.hl7.fhir.dstu3.model.Resource res;
if (fn.endsWith(".xml") && !fn.endsWith("template.xml"))
res = new org.hl7.fhir.dstu3.formats.XmlParser().parse(new ByteArrayInputStream(content));
else if (fn.endsWith(".json") && !fn.endsWith("template.json"))
res = new org.hl7.fhir.dstu3.formats.JsonParser().parse(new ByteArrayInputStream(content));
else if (fn.endsWith(".txt") || fn.endsWith(".map"))
res = new org.hl7.fhir.dstu3.utils.StructureMapUtilities(null).parse(new String(content));
else
throw new FHIRException("Unsupported format for " + fn);
r = VersionConvertorFactory_30_50.convertResource(res);
} else if (fhirVersion.startsWith("4.0")) {
org.hl7.fhir.r4.model.Resource res;
if (fn.endsWith(".xml") && !fn.endsWith("template.xml"))
res = new org.hl7.fhir.r4.formats.XmlParser().parse(new ByteArrayInputStream(content));
else if (fn.endsWith(".json") && !fn.endsWith("template.json"))
res = new org.hl7.fhir.r4.formats.JsonParser().parse(new ByteArrayInputStream(content));
else if (fn.endsWith(".txt") || fn.endsWith(".map"))
res = new org.hl7.fhir.r4.utils.StructureMapUtilities(null).parse(new String(content), fn);
else
throw new FHIRException("Unsupported format for " + fn);
r = VersionConvertorFactory_40_50.convertResource(res);
} else if (fhirVersion.startsWith("1.4")) {
org.hl7.fhir.dstu2016may.model.Resource res;
if (fn.endsWith(".xml") && !fn.endsWith("template.xml"))
res = new org.hl7.fhir.dstu2016may.formats.XmlParser().parse(new ByteArrayInputStream(content));
else if (fn.endsWith(".json") && !fn.endsWith("template.json"))
res = new org.hl7.fhir.dstu2016may.formats.JsonParser().parse(new ByteArrayInputStream(content));
else
throw new FHIRException("Unsupported format for " + fn);
r = VersionConvertorFactory_14_50.convertResource(res);
} else if (fhirVersion.startsWith("1.0")) {
org.hl7.fhir.dstu2.model.Resource res;
if (fn.endsWith(".xml") && !fn.endsWith("template.xml"))
res = new org.hl7.fhir.dstu2.formats.JsonParser().parse(new ByteArrayInputStream(content));
else if (fn.endsWith(".json") && !fn.endsWith("template.json"))
res = new org.hl7.fhir.dstu2.formats.JsonParser().parse(new ByteArrayInputStream(content));
else
throw new FHIRException("Unsupported format for " + fn);
r = VersionConvertorFactory_10_50.convertResource(res, new org.hl7.fhir.convertors.misc.IGR2ConvertorAdvisor5());
} else if (fhirVersion.equals(Constants.VERSION) || "current".equals(fhirVersion)) {
if (fn.endsWith(".xml") && !fn.endsWith("template.xml"))
r = new XmlParser().parse(new ByteArrayInputStream(content));
else if (fn.endsWith(".json") && !fn.endsWith("template.json"))
r = new JsonParser().parse(new ByteArrayInputStream(content));
else if (fn.endsWith(".txt"))
r = new StructureMapUtilities(getContext(), null, null).parse(TextFile.bytesToString(content), fn);
else if (fn.endsWith(".map"))
r = new StructureMapUtilities(null).parse(new String(content), fn);
else
throw new FHIRException("Unsupported format for " + fn);
} else
throw new FHIRException("Unsupported version " + fhirVersion);
return r;
}
use of org.hl7.fhir.r4.utils.StructureMapUtilities in project org.hl7.fhir.core by hapifhir.
the class Transformer method process.
public boolean process() {
try {
System.out.println(" .. load definitions from " + definitions);
IWorkerContext context = SimpleWorkerContext.fromPack(definitions);
scu = new StructureMapUtilities(context, new HashMap<String, StructureMap>(), null);
for (String folder : folders) {
System.out.println(" .. load additional definitions from " + folder);
((SimpleWorkerContext) context).loadFromFolder(folder);
loadMaps(folder);
}
System.out.println(" .. load source from " + source);
Element e = Manager.parse(context, new FileInputStream(source), FhirFormat.XML);
Bundle bundle = new Bundle();
StructureMap map = scu.getLibrary().get(mapUri);
if (map == null)
throw new Error("Unable to find map " + mapUri + " (Known Maps = " + Utilities.listCanonicalUrls(scu.getLibrary().keySet()) + ")");
scu.transform(null, e, map, bundle);
new XmlParser().setOutputStyle(OutputStyle.PRETTY).compose(new FileOutputStream(output), bundle);
return true;
} catch (Exception e) {
e.printStackTrace();
message = e.getMessage();
return false;
}
}
Aggregations