Search in sources :

Example 16 with StructureMapUtilities

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);
}
Also used : XmlParser(org.hl7.fhir.dstu2016may.formats.XmlParser) HashMap(java.util.HashMap) Bundle(org.hl7.fhir.dstu2016may.model.Bundle) Element(org.hl7.fhir.dstu2016may.metamodel.Element) SimpleWorkerContext(org.hl7.fhir.dstu2016may.utils.SimpleWorkerContext) StructureMapUtilities(org.hl7.fhir.dstu2016may.utils.StructureMapUtilities) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) FHIRException(org.hl7.fhir.exceptions.FHIRException) StructureMap(org.hl7.fhir.dstu2016may.model.StructureMap) StructureDefinition(org.hl7.fhir.dstu2016may.model.StructureDefinition) FileOutputStream(java.io.FileOutputStream) File(java.io.File) TextFile(org.hl7.fhir.utilities.TextFile) Test(org.junit.jupiter.api.Test)

Example 17 with StructureMapUtilities

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;
    }
}
Also used : FHIRException(org.hl7.fhir.exceptions.FHIRException) StructureMapUtilities(org.hl7.fhir.dstu3.utils.StructureMapUtilities)

Example 18 with StructureMapUtilities

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;
}
Also used : Turtle(org.hl7.fhir.utilities.turtle.Turtle) FHIRException(org.hl7.fhir.exceptions.FHIRException) StructureMapUtilities(org.hl7.fhir.r5.utils.structuremap.StructureMapUtilities)

Example 19 with StructureMapUtilities

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;
}
Also used : JsonParser(org.hl7.fhir.r5.formats.JsonParser) XmlParser(org.hl7.fhir.r5.formats.XmlParser) Resource(org.hl7.fhir.r5.model.Resource) FHIRException(org.hl7.fhir.exceptions.FHIRException) StructureMapUtilities(org.hl7.fhir.r5.utils.structuremap.StructureMapUtilities)

Example 20 with StructureMapUtilities

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;
    }
}
Also used : XmlParser(org.hl7.fhir.dstu2016may.formats.XmlParser) HashMap(java.util.HashMap) Bundle(org.hl7.fhir.dstu2016may.model.Bundle) Element(org.hl7.fhir.dstu2016may.metamodel.Element) FileInputStream(java.io.FileInputStream) StructureMap(org.hl7.fhir.dstu2016may.model.StructureMap) FileOutputStream(java.io.FileOutputStream)

Aggregations

StructureMapUtilities (org.hl7.fhir.r5.utils.structuremap.StructureMapUtilities)10 FHIRException (org.hl7.fhir.exceptions.FHIRException)9 Test (org.junit.jupiter.api.Test)6 File (java.io.File)5 TextFile (org.hl7.fhir.utilities.TextFile)5 IniFile (org.hl7.fhir.utilities.IniFile)4 IOException (java.io.IOException)3 HashMap (java.util.HashMap)3 StructureMap (org.hl7.fhir.dstu2016may.model.StructureMap)3 StructureMapUtilities (org.hl7.fhir.dstu3.utils.StructureMapUtilities)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 FileInputStream (java.io.FileInputStream)2 FileNotFoundException (java.io.FileNotFoundException)2 FileOutputStream (java.io.FileOutputStream)2 XmlParser (org.hl7.fhir.dstu2016may.formats.XmlParser)2 Element (org.hl7.fhir.dstu2016may.metamodel.Element)2 Bundle (org.hl7.fhir.dstu2016may.model.Bundle)2 StructureMapUtilities (org.hl7.fhir.dstu2016may.utils.StructureMapUtilities)2 DefinitionException (org.hl7.fhir.exceptions.DefinitionException)2 MetadataResource (org.hl7.fhir.r4.model.MetadataResource)2