Search in sources :

Example 6 with SimpleWorkerContext

use of org.hl7.fhir.r4b.context.SimpleWorkerContext 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;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) JWT(org.hl7.fhir.r5.elementmodel.SHCParser.JWT) JsonObject(com.google.gson.JsonObject) SHCParser(org.hl7.fhir.r5.elementmodel.SHCParser) IOException(java.io.IOException) StructureMapUtilities(org.hl7.fhir.r5.utils.structuremap.StructureMapUtilities)

Example 7 with SimpleWorkerContext

use of org.hl7.fhir.r4b.context.SimpleWorkerContext in project org.hl7.fhir.core by hapifhir.

the class ValidatorUtils method messagesToOutcome.

protected static OperationOutcome messagesToOutcome(List<ValidationMessage> messages, SimpleWorkerContext context, FHIRPathEngine fpe) throws IOException, FHIRException, EOperationOutcome {
    OperationOutcome op = new OperationOutcome();
    for (ValidationMessage vm : filterMessages(messages)) {
        try {
            fpe.parse(vm.getLocation());
        } catch (Exception e) {
            System.out.println("Internal error in location for message: '" + e.getMessage() + "', loc = '" + vm.getLocation() + "', err = '" + vm.getMessage() + "'");
        }
        op.getIssue().add(OperationOutcomeUtilities.convertToIssue(vm, op));
    }
    if (!op.hasIssue()) {
        op.addIssue().setSeverity(OperationOutcome.IssueSeverity.INFORMATION).setCode(OperationOutcome.IssueType.INFORMATIONAL).getDetails().setText(context.formatMessage(I18nConstants.ALL_OK));
    }
    RenderingContext rc = new RenderingContext(context, null, null, "http://hl7.org/fhir", "", null, RenderingContext.ResourceRendererMode.END_USER);
    RendererFactory.factory(op, rc).render(op);
    return op;
}
Also used : RenderingContext(org.hl7.fhir.r5.renderers.utils.RenderingContext) ValidationMessage(org.hl7.fhir.utilities.validation.ValidationMessage) OperationOutcome(org.hl7.fhir.r5.model.OperationOutcome) EOperationOutcome(org.hl7.fhir.r5.utils.EOperationOutcome) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException) FHIRException(org.hl7.fhir.exceptions.FHIRException)

Example 8 with SimpleWorkerContext

use of org.hl7.fhir.r4b.context.SimpleWorkerContext in project org.hl7.fhir.core by hapifhir.

the class ValidationTestConvertor method main.

/**
 * @param args
 * @throws FHIRException
 * @throws IOException
 * @throws FileNotFoundException
 */
public static void main(String[] args) throws FileNotFoundException, IOException, FHIRException {
    SimpleWorkerContext context = SimpleWorkerContext.fromPack("C:\\work\\org.hl7.fhir\\build\\publish\\validation-min.xml.zip");
    for (File f : new File("C:\\work\\org.hl7.fhir\\build\\tests\\validation-examples").listFiles()) {
        if (f.getAbsolutePath().endsWith(".xml")) {
            File t = new File(Utilities.changeFileExt(f.getAbsolutePath(), ".ttl"));
            if (!t.exists()) {
                try {
                    System.out.print("Process " + f.getAbsolutePath());
                    Element e = Manager.parse(context, new FileInputStream(f), FhirFormat.XML);
                    Manager.compose(context, e, new FileOutputStream(t), FhirFormat.TURTLE, OutputStyle.PRETTY, null);
                    System.out.println("   .... success");
                } catch (Exception e) {
                    System.out.println("   .... fail: " + e.getMessage());
                }
            }
        }
        if (f.getAbsolutePath().endsWith(".json")) {
            if (!new File(Utilities.changeFileExt(f.getAbsolutePath(), ".ttl")).exists()) {
                File t = new File(Utilities.changeFileExt(f.getAbsolutePath(), ".ttl"));
                if (!t.exists()) {
                    try {
                        System.out.print("Process " + f.getAbsolutePath());
                        Element e = Manager.parse(context, new FileInputStream(f), FhirFormat.JSON);
                        Manager.compose(context, e, new FileOutputStream(t), FhirFormat.TURTLE, OutputStyle.PRETTY, null);
                        System.out.println("   .... success");
                    } catch (Exception e) {
                        System.out.println("   .... fail: " + e.getMessage());
                    }
                }
            }
        }
    }
}
Also used : Element(org.hl7.fhir.r4.elementmodel.Element) SimpleWorkerContext(org.hl7.fhir.r4.context.SimpleWorkerContext) FHIRException(org.hl7.fhir.exceptions.FHIRException)

Example 9 with SimpleWorkerContext

use of org.hl7.fhir.r4b.context.SimpleWorkerContext in project org.hl7.fhir.core by hapifhir.

the class CDARoundTripTests method setUp.

@BeforeAll
public void setUp() throws Exception {
    context = new SimpleWorkerContext();
    FilesystemPackageCacheManager pcm = new FilesystemPackageCacheManager(true, ToolsVersion.TOOLS_VERSION);
    context.loadFromPackage(pcm.loadPackage("hl7.fhir.core", "current"), null, "StructureDefinition");
    context.loadFromPackage(pcm.loadPackage("hl7.fhir.cda", "current"), null, "StructureDefinition");
}
Also used : FilesystemPackageCacheManager(org.hl7.fhir.utilities.npm.FilesystemPackageCacheManager) SimpleWorkerContext(org.hl7.fhir.r4.context.SimpleWorkerContext) BeforeAll(org.junit.jupiter.api.BeforeAll)

Example 10 with SimpleWorkerContext

use of org.hl7.fhir.r4b.context.SimpleWorkerContext in project org.hl7.fhir.core by hapifhir.

the class ConceptMapEngineTest method getConceptMapEngine.

@Nonnull
private ConceptMapEngine getConceptMapEngine(Collection<ConceptMap.SourceElementComponent> elements) throws IOException {
    ConceptMap conceptMap = getConceptMap(elements);
    SimpleWorkerContext simpleWorkerContext = mock(SimpleWorkerContext.class);
    when(simpleWorkerContext.fetchResource(ConceptMap.class, CONCEPT_MAP_URL)).thenReturn(conceptMap);
    return new ConceptMapEngine(simpleWorkerContext);
}
Also used : SimpleWorkerContext(org.hl7.fhir.r4.context.SimpleWorkerContext) ConceptMap(org.hl7.fhir.r4.model.ConceptMap) Nonnull(javax.annotation.Nonnull)

Aggregations

FileInputStream (java.io.FileInputStream)7 IOException (java.io.IOException)7 FHIRException (org.hl7.fhir.exceptions.FHIRException)6 ZipInputStream (java.util.zip.ZipInputStream)5 SimpleWorkerContext (org.hl7.fhir.r4.context.SimpleWorkerContext)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 FileNotFoundException (java.io.FileNotFoundException)4 FileOutputStream (java.io.FileOutputStream)4 NotImplementedException (org.apache.commons.lang3.NotImplementedException)4 SimpleWorkerContext (org.hl7.fhir.r5.context.SimpleWorkerContext)4 FilesystemPackageCacheManager (org.hl7.fhir.utilities.npm.FilesystemPackageCacheManager)4 File (java.io.File)3 InputStream (java.io.InputStream)3 SimpleWorkerContext (org.hl7.fhir.dstu3.context.SimpleWorkerContext)3 CSFileInputStream (org.hl7.fhir.utilities.CSFileInputStream)3 HashMap (java.util.HashMap)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 StructureMap (org.hl7.fhir.dstu2016may.model.StructureMap)2