Search in sources :

Example 11 with StructureMapUtilities

use of org.hl7.fhir.r4.utils.StructureMapUtilities in project org.hl7.fhir.core by hapifhir.

the class StructureMapUtilitiesTest method testSyntax.

@Test
public void testSyntax() throws IOException, FHIRException {
    StructureMapUtilities scu = new StructureMapUtilities(context, this);
    String fileMap = TestingUtilities.loadTestResource("r5", "structure-mapping", "syntax.map");
    System.out.println(fileMap);
    StructureMap structureMap = scu.parse(fileMap, "Syntax");
    assertSerializeDeserialize(structureMap);
    String renderedMap = StructureMapUtilities.render(structureMap);
    StructureMap map = scu.parse(renderedMap, "Syntax");
    System.out.println(map);
    assertSerializeDeserialize(map);
}
Also used : StructureMap(org.hl7.fhir.r4b.model.StructureMap) StructureMapUtilities(org.hl7.fhir.r4b.utils.structuremap.StructureMapUtilities) Test(org.junit.jupiter.api.Test)

Example 12 with StructureMapUtilities

use of org.hl7.fhir.r4.utils.StructureMapUtilities in project org.hl7.fhir.core by hapifhir.

the class ValidationEngine method transformVersion.

public byte[] transformVersion(String source, String targetVer, FhirFormat format, Boolean canDoNative) throws FHIRException, IOException, Exception {
    Content cnt = igLoader.loadContent(source, "validate", false);
    org.hl7.fhir.r5.elementmodel.Element src = Manager.parseSingle(context, new ByteArrayInputStream(cnt.focus), cnt.cntType);
    // if the src has a url, we try to use the java code
    if ((canDoNative == null && src.hasChild("url")) || (canDoNative != null && canDoNative)) {
        try {
            if (VersionUtilities.isR2Ver(version)) {
                return VersionConvertor.convertVersionNativeR2(targetVer, cnt, format);
            } else if (VersionUtilities.isR2BVer(version)) {
                return VersionConvertor.convertVersionNativeR2b(targetVer, cnt, format);
            } else if (VersionUtilities.isR3Ver(version)) {
                return VersionConvertor.convertVersionNativeR3(targetVer, cnt, format);
            } else if (VersionUtilities.isR4Ver(version)) {
                return VersionConvertor.convertVersionNativeR4(targetVer, cnt, format);
            } else {
                throw new FHIRException("Source version not supported yet: " + version);
            }
        } catch (Exception e) {
            System.out.println("Conversion failed using Java convertor: " + e.getMessage());
        }
    }
    // ok, we try converting using the structure maps
    System.out.println("Loading hl7.fhir.xver.r4");
    igLoader.loadIg(getIgs(), getBinaries(), "hl7.fhir.xver.r4", false);
    String type = src.fhirType();
    String url = getMapId(type, targetVer);
    List<Base> outputs = new ArrayList<Base>();
    StructureMapUtilities scu = new StructureMapUtilities(context, new TransformSupportServices(outputs, mapLog, context));
    StructureMap map = context.getTransform(url);
    if (map == null)
        throw new Error("Unable to find map " + url + " (Known Maps = " + context.listMapUrls() + ")");
    org.hl7.fhir.r5.elementmodel.Element resource = getTargetResourceFromStructureMap(map);
    scu.transform(null, src, map, resource);
    ByteArrayOutputStream bs = new ByteArrayOutputStream();
    Manager.compose(context, resource, bs, format, OutputStyle.PRETTY, null);
    return bs.toByteArray();
}
Also used : Element(org.hl7.fhir.r5.elementmodel.Element) FHIRException(org.hl7.fhir.exceptions.FHIRException) URISyntaxException(java.net.URISyntaxException) SAXException(org.xml.sax.SAXException) DefinitionException(org.hl7.fhir.exceptions.DefinitionException) FHIRException(org.hl7.fhir.exceptions.FHIRException) StructureMapUtilities(org.hl7.fhir.r5.utils.structuremap.StructureMapUtilities)

Example 13 with StructureMapUtilities

use of org.hl7.fhir.r4.utils.StructureMapUtilities in project org.hl7.fhir.core by hapifhir.

the class ValidationEngine method transform.

public org.hl7.fhir.r5.elementmodel.Element transform(byte[] source, FhirFormat cntType, String mapUri) throws FHIRException, IOException {
    List<Base> outputs = new ArrayList<>();
    StructureMapUtilities scu = new StructureMapUtilities(context, new TransformSupportServices(outputs, mapLog, context));
    org.hl7.fhir.r5.elementmodel.Element src = Manager.parseSingle(context, new ByteArrayInputStream(source), cntType);
    StructureMap map = context.getTransform(mapUri);
    if (map == null)
        throw new Error("Unable to find map " + mapUri + " (Known Maps = " + context.listMapUrls() + ")");
    org.hl7.fhir.r5.elementmodel.Element resource = getTargetResourceFromStructureMap(map);
    scu.transform(null, src, map, resource);
    return resource;
}
Also used : Element(org.hl7.fhir.r5.elementmodel.Element) StructureMapUtilities(org.hl7.fhir.r5.utils.structuremap.StructureMapUtilities)

Example 14 with StructureMapUtilities

use of org.hl7.fhir.r4.utils.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());
}
Also used : StructureMap(org.hl7.fhir.r5.model.StructureMap) StructureMapUtilities(org.hl7.fhir.r5.utils.structuremap.StructureMapUtilities) Test(org.junit.jupiter.api.Test)

Example 15 with StructureMapUtilities

use of org.hl7.fhir.r4.utils.StructureMapUtilities in project org.hl7.fhir.core by hapifhir.

the class StructureMapUtilitiesTest method testSyntax.

@Test
public void testSyntax() throws IOException, FHIRException {
    StructureMapUtilities scu = new StructureMapUtilities(context, this);
    String fileMap = TestingUtilities.loadTestResource("r5", "structure-mapping", "syntax.map");
    System.out.println(fileMap);
    StructureMap structureMap = scu.parse(fileMap, "Syntax");
    assertSerializeDeserialize(structureMap);
    String renderedMap = StructureMapUtilities.render(structureMap);
    StructureMap map = scu.parse(renderedMap, "Syntax");
    System.out.println(map);
    assertSerializeDeserialize(map);
}
Also used : StructureMap(org.hl7.fhir.r5.model.StructureMap) StructureMapUtilities(org.hl7.fhir.r5.utils.structuremap.StructureMapUtilities) Test(org.junit.jupiter.api.Test)

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