Search in sources :

Example 91 with JsonParser

use of org.hl7.fhir.r5.elementmodel.JsonParser in project org.hl7.fhir.core by hapifhir.

the class Unbundler method unbundle.

private static void unbundle(String src) throws FHIRFormatError, FileNotFoundException, IOException {
    String folder = Utilities.getDirectoryForFile(src);
    Bundle bnd = (Bundle) new JsonParser().parse(new FileInputStream(src));
    for (BundleEntryComponent be : bnd.getEntry()) {
        Resource r = be.getResource();
        if (r != null) {
            String tgt = Utilities.path(folder, r.fhirType() + "-" + r.getId() + ".json");
            new JsonParser().compose(new FileOutputStream(tgt), r);
        }
    }
}
Also used : BundleEntryComponent(org.hl7.fhir.dstu3.model.Bundle.BundleEntryComponent) Bundle(org.hl7.fhir.dstu3.model.Bundle) FileOutputStream(java.io.FileOutputStream) Resource(org.hl7.fhir.dstu3.model.Resource) FileInputStream(java.io.FileInputStream) JsonParser(org.hl7.fhir.dstu3.formats.JsonParser)

Example 92 with JsonParser

use of org.hl7.fhir.r5.elementmodel.JsonParser in project org.hl7.fhir.core by hapifhir.

the class R3TEchnicalCorrectionProcessor method extractToPackageMaster.

private void extractToPackageMaster(List<Resource> list, String root) throws IOException, FHIRFormatError {
    System.out.println("Updating Packages Master");
    String corePath = Utilities.path(root, "hl7.fhir.r3.core", "package");
    String examplesPath = Utilities.path(root, "hl7.fhir.r3.examples", "package");
    String elementsPath = Utilities.path(root, "hl7.fhir.r3.elements", "package");
    int coreTotal = new File(corePath).list().length - 1;
    int examplesTotal = new File(examplesPath).list().length - 1;
    int elementsTotal = new File(elementsPath).list().length - 1;
    int coreCount = 0;
    int examplesCount = 0;
    int elementsCount = 0;
    for (Resource r : list) {
        String n = r.fhirType() + "-" + r.getId() + ".json";
        FileOutputStream dst = null;
        if (n.startsWith("DataElement-")) {
            elementsCount++;
            dst = new FileOutputStream(Utilities.path(elementsPath, n));
            new JsonParser().setOutputStyle(OutputStyle.NORMAL).compose(dst, r);
        } else {
            dst = new FileOutputStream(Utilities.path(examplesPath, n));
            new JsonParser().setOutputStyle(OutputStyle.NORMAL).compose(dst, r);
            examplesCount++;
            if (isCoreResource(r.fhirType())) {
                coreCount++;
                DomainResource dr = (DomainResource) r;
                dr.setText(null);
                new JsonParser().setOutputStyle(OutputStyle.NORMAL).compose(new FileOutputStream(Utilities.path(corePath, n)), r);
            }
        }
    }
    System.out.println("  Core @ " + corePath + ": Replaced " + coreCount + " of " + coreTotal);
    System.out.println("  Examples @ " + examplesPath + ": Replaced " + examplesCount + " of " + examplesTotal);
    System.out.println("  Elements @ " + elementsPath + ": Replaced " + elementsCount + " of " + elementsTotal);
}
Also used : DomainResource(org.hl7.fhir.dstu3.model.DomainResource) FileOutputStream(java.io.FileOutputStream) Resource(org.hl7.fhir.dstu3.model.Resource) DomainResource(org.hl7.fhir.dstu3.model.DomainResource) File(java.io.File) JsonParser(org.hl7.fhir.dstu3.formats.JsonParser)

Example 93 with JsonParser

use of org.hl7.fhir.r5.elementmodel.JsonParser in project org.hl7.fhir.core by hapifhir.

the class R3TEchnicalCorrectionProcessor method produceDefinitionsJson.

private void produceDefinitionsJson(Map<String, Resource> definitions, String dest) throws IOException {
    for (String n : definitions.keySet()) {
        File f = new File(Utilities.path(dest, "definitions.json", Utilities.changeFileExt(n, ".json")));
        new JsonParser().setOutputStyle(OutputStyle.PRETTY).compose(new FileOutputStream(f), definitions.get(n));
    }
}
Also used : FileOutputStream(java.io.FileOutputStream) File(java.io.File) JsonParser(org.hl7.fhir.dstu3.formats.JsonParser)

Example 94 with JsonParser

use of org.hl7.fhir.r5.elementmodel.JsonParser in project org.hl7.fhir.core by hapifhir.

the class R3TEchnicalCorrectionProcessor method execute.

private void execute(String src, String packageRoot) throws FileNotFoundException, IOException {
    System.out.println("Loading resources from " + src);
    List<Resource> resources = new ArrayList<>();
    Map<String, Resource> definitions = new HashMap<>();
    for (File f : new File(src).listFiles()) {
        if (f.getName().endsWith(".xml") && !(f.getName().endsWith("warnings.xml") || f.getName().endsWith(".diff.xml"))) {
            try {
                Resource r = new XmlParser().parse(new FileInputStream(f));
                if (f.getName().contains("canonical")) {
                    resources.add(r);
                }
                if (Utilities.existsInList(f.getName(), "conceptmaps.xml", "dataelements.xml", "extension-definitions.xml", "profiles-others.xml", "profiles-resources.xml", "profiles-types.xml", "search-parameters.xml", "v2-tables.xml", "v3-codesystems.xml", "valuesets.xml")) {
                    definitions.put(f.getName(), r);
                }
                r.setUserData("path", f.getName().substring(0, f.getName().indexOf(".")));
            // FileUtils.copyFile(f, new File(f.getAbsolutePath()+"1"));
            // FileUtils.copyFile(f, new File(f.getAbsolutePath()+"2"));
            } catch (Exception e) {
                System.out.println("Unable to load " + f.getName() + ": " + e.getMessage());
            }
        }
        if (f.getName().endsWith(".json") && !(f.getName().endsWith("schema.json") || f.getName().endsWith(".diff.json"))) {
            try {
            // new JsonParser().parse(new FileInputStream(f));
            // FileUtils.copyFile(f, new File(f.getAbsolutePath()+"1"));
            // FileUtils.copyFile(f, new File(f.getAbsolutePath()+"2"));
            } catch (Exception e) {
                System.out.println("Unable to load " + f.getName() + ": " + e.getMessage());
            }
        }
    }
    System.out.println(Integer.toString(resources.size()) + " resources");
    System.out.println(Integer.toString(definitions.size()) + " resources");
    produceExamplesXml(resources, src);
    produceDefinitionsXml(definitions, src);
    produceExamplesJson(resources, src);
    produceDefinitionsJson(definitions, src);
    for (Resource r : definitions.values()) {
        if (r instanceof Bundle) {
            Bundle bnd = (Bundle) r;
            for (BundleEntryComponent be : bnd.getEntry()) {
                resources.add(be.getResource());
            }
        }
    }
    extractToPackageMaster(resources, packageRoot);
    System.out.println("Done");
}
Also used : XmlParser(org.hl7.fhir.dstu3.formats.XmlParser) BundleEntryComponent(org.hl7.fhir.dstu3.model.Bundle.BundleEntryComponent) HashMap(java.util.HashMap) Bundle(org.hl7.fhir.dstu3.model.Bundle) Resource(org.hl7.fhir.dstu3.model.Resource) DomainResource(org.hl7.fhir.dstu3.model.DomainResource) ArrayList(java.util.ArrayList) File(java.io.File) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) ParseException(java.text.ParseException)

Example 95 with JsonParser

use of org.hl7.fhir.r5.elementmodel.JsonParser in project org.hl7.fhir.core by hapifhir.

the class JsonDirectTests method test.

@Test
public void test() throws FHIRFormatError, FileNotFoundException, IOException {
    File src = new File(Utilities.path("[tmp]", "obs.xml"));
    File xml = new File(Utilities.path("[tmp]", "xml.xml"));
    File json = new File(Utilities.path("[tmp]", "json.json"));
    File json2 = new File(Utilities.path("[tmp]", "json2.json"));
    FileUtils.copyFile(new File("C:\\work\\org.hl7.fhir\\build\\publish\\observation-decimal.xml"), src);
    Observation obs = (Observation) new XmlParser().parse(new FileInputStream(src));
    new JsonParser().setOutputStyle(OutputStyle.PRETTY).compose(new FileOutputStream(json), obs);
    obs = (Observation) new JsonParser().parse(new FileInputStream(json));
    new JsonParser().setOutputStyle(OutputStyle.PRETTY).compose(new FileOutputStream(json2), obs);
    new XmlParser().setOutputStyle(OutputStyle.PRETTY).compose(new FileOutputStream(xml), obs);
}
Also used : XmlParser(org.hl7.fhir.r4.formats.XmlParser) Observation(org.hl7.fhir.r4.model.Observation) JsonParser(org.hl7.fhir.r4.formats.JsonParser) Test(org.junit.jupiter.api.Test)

Aggregations

FileOutputStream (java.io.FileOutputStream)82 JsonParser (org.hl7.fhir.r5.formats.JsonParser)66 FHIRException (org.hl7.fhir.exceptions.FHIRException)58 FileInputStream (java.io.FileInputStream)53 IOException (java.io.IOException)49 XmlParser (org.hl7.fhir.r5.formats.XmlParser)44 ByteArrayOutputStream (java.io.ByteArrayOutputStream)41 File (java.io.File)36 JsonParser (org.hl7.fhir.r4.formats.JsonParser)35 CSFileInputStream (org.hl7.fhir.utilities.CSFileInputStream)29 JsonParser (org.hl7.fhir.dstu3.formats.JsonParser)26 JsonParser (org.hl7.fhir.r4b.formats.JsonParser)26 CSFile (org.hl7.fhir.utilities.CSFile)26 ArrayList (java.util.ArrayList)23 TextFile (org.hl7.fhir.utilities.TextFile)20 InputStream (java.io.InputStream)18 IParser (org.hl7.fhir.r5.formats.IParser)18 Resource (org.hl7.fhir.dstu3.model.Resource)17 FileNotFoundException (java.io.FileNotFoundException)16 Resource (org.hl7.fhir.r4.model.Resource)16