Search in sources :

Example 26 with JsonParser

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

the class R5ToR5Loader method loadBundle.

@Override
public Bundle loadBundle(InputStream stream, boolean isJson) throws FHIRException, IOException {
    Resource r5 = null;
    if (isJson)
        r5 = new JsonParser().parse(stream);
    else
        r5 = new XmlParser().parse(stream);
    Bundle b;
    if (r5 instanceof Bundle)
        b = (Bundle) r5;
    else {
        b = new Bundle();
        b.setId(UUID.randomUUID().toString().toLowerCase());
        b.setType(BundleType.COLLECTION);
        b.addEntry().setResource(r5).setFullUrl(r5 instanceof CanonicalResource ? ((CanonicalResource) r5).getUrl() : null);
    }
    for (CodeSystem cs : cslist) {
        BundleEntryComponent be = b.addEntry();
        be.setFullUrl(cs.getUrl());
        be.setResource(cs);
    }
    if (killPrimitives) {
        List<BundleEntryComponent> remove = new ArrayList<BundleEntryComponent>();
        for (BundleEntryComponent be : b.getEntry()) {
            if (be.hasResource() && be.getResource() instanceof StructureDefinition) {
                StructureDefinition sd = (StructureDefinition) be.getResource();
                if (sd.getKind() == StructureDefinitionKind.PRIMITIVETYPE)
                    remove.add(be);
            }
        }
        b.getEntry().removeAll(remove);
    }
    if (patchUrls) {
        for (BundleEntryComponent be : b.getEntry()) {
            if (be.hasResource() && be.getResource() instanceof StructureDefinition) {
                StructureDefinition sd = (StructureDefinition) be.getResource();
                sd.setUrl(sd.getUrl().replace(URL_BASE, URL_R4));
                sd.addExtension().setUrl(URL_ELEMENT_DEF_NAMESPACE).setValue(new UriType(URL_BASE));
                for (ElementDefinition ed : sd.getSnapshot().getElement()) patchUrl(ed);
                for (ElementDefinition ed : sd.getDifferential().getElement()) patchUrl(ed);
            }
        }
    }
    return b;
}
Also used : XmlParser(org.hl7.fhir.r5.formats.XmlParser) BundleEntryComponent(org.hl7.fhir.r5.model.Bundle.BundleEntryComponent) ArrayList(java.util.ArrayList) JsonParser(org.hl7.fhir.r5.formats.JsonParser)

Example 27 with JsonParser

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

the class CountryCodesConverter method execute.

private void execute() throws ParserConfigurationException, SAXException, IOException {
    Document src = load();
    CodeSystem cs1 = new CodeSystem();
    CodeSystem cs2 = new CodeSystem();
    CodeSystem cs3 = new CodeSystem();
    setMetadata(src, cs1, "iso3166", "urn:iso:std:iso:3166", "", "");
    setMetadata(src, cs2, "iso3166-2", "urn:iso:std:iso:3166:-2", "Part2", " Part 2");
    cs1.addProperty().setCode("canonical").setDescription("The 2 letter code that identifies the same country (so 2/3/numeric codes can be aligned)").setType(PropertyType.CODE);
    cs2.addProperty().setCode("country").setDescription("The 2 letter code that identifies the country for the subdivision").setType(PropertyType.CODE);
    for (Element e : XMLUtil.getNamedChildren(src.getDocumentElement(), "country")) {
        System.out.println(e.getAttribute("id"));
        String c2 = XMLUtil.getNamedChildText(e, "alpha-2-code");
        String c3 = XMLUtil.getNamedChildText(e, "alpha-3-code");
        String cN = XMLUtil.getNamedChildText(e, "numeric-code");
        Element n = XMLUtil.getNamedChildByAttribute(e, "short-name", "lang3code", "eng");
        if (n == null)
            n = XMLUtil.getNamedChildByAttribute(e, "short-name-upper-case", "lang3code", "eng");
        if (n == null)
            continue;
        String name = n.getTextContent();
        n = XMLUtil.getNamedChildByAttribute(e, "full-name", "lang3code", "eng");
        if (n == null)
            n = XMLUtil.getNamedChildByAttribute(e, "full-name-upper-case", "lang3code", "eng");
        if (n == null)
            n = XMLUtil.getNamedChildByAttribute(e, "short-name", "lang3code", "eng");
        if (n == null)
            n = XMLUtil.getNamedChildByAttribute(e, "short-name-upper-case", "lang3code", "eng");
        String desc = n.getTextContent();
        ConceptDefinitionComponent cc = cs1.addConcept();
        cc.setCode(c2);
        cc.setDisplay(name);
        cc.setDefinition(desc);
        poplang(e, cc);
        if (c3 != null) {
            cc = cs1.addConcept();
            cc.setCode(c3);
            cc.setDisplay(name);
            cc.setDefinition(desc);
            cc.addProperty().setCode("canonical").setValue(new CodeType(c2));
            poplang(e, cc);
        }
        if (cN != null) {
            cc = cs1.addConcept();
            cc.setCode(cN);
            cc.setDisplay(name);
            cc.setDefinition(desc);
            cc.addProperty().setCode("canonical").setValue(new CodeType(c2));
            poplang(e, cc);
        }
        for (Element sd : XMLUtil.getNamedChildren(e, "subdivision")) {
            cc = cs2.addConcept();
            cc.setCode(XMLUtil.getNamedChildText(sd, "subdivision-code"));
            Element l = XMLUtil.getNamedChild(sd, "subdivision-locale");
            cc.setDisplay(XMLUtil.getNamedChildText(l, "subdivision-locale-name"));
            cc.addProperty().setCode("country").setValue(new CodeType(c2));
        }
    }
    cs1.setCount(cs1.getConcept().size());
    cs2.setCount(cs2.getConcept().size());
    throw new Error("Needs revisiting");
// new JsonParser().setOutputStyle(OutputStyle.PRETTY).compose(new FileOutputStream(Utilities.path(dest, "4.0.1", "package", "CodeSstem-iso3166.json")), cs1);
// new JsonParser().setOutputStyle(OutputStyle.PRETTY).compose(new FileOutputStream(Utilities.path(dest, "3.0.2", "package", "CodeSstem-iso3166.json")), cs1); // format hasn't changed
// new JsonParser().setOutputStyle(OutputStyle.PRETTY).compose(new FileOutputStream(Utilities.path(dest, "4.0.1", "package", "CodeSstem-iso3166-2.json")), cs2);
// new JsonParser().setOutputStyle(OutputStyle.PRETTY).compose(new FileOutputStream(Utilities.path(dest, "3.0.2", "package", "CodeSstem-iso3166-2.json")), cs2); // format hasn't changed
}
Also used : ConceptDefinitionComponent(org.hl7.fhir.r4.model.CodeSystem.ConceptDefinitionComponent) Element(org.w3c.dom.Element) CodeType(org.hl7.fhir.r4.model.CodeType) Document(org.w3c.dom.Document) CodeSystem(org.hl7.fhir.r4.model.CodeSystem)

Example 28 with JsonParser

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

the class ArgonautConverter method saveResource.

private void saveResource(Resource resource, String extraType) throws Exception {
    if (!WANT_SAVE)
        return;
    DomainResource dr = null;
    if (resource instanceof DomainResource) {
        dr = (DomainResource) resource;
        if (!dr.hasText()) {
            NarrativeGenerator generator = new NarrativeGenerator("", "", context);
            generator.generate(dr);
        }
    }
    XmlParser xparser = new XmlParser();
    xparser.setOutputStyle(OutputStyle.PRETTY);
    JsonParser jparser = new JsonParser();
    jparser.setOutputStyle(OutputStyle.PRETTY);
    ByteArrayOutputStream ba = new ByteArrayOutputStream();
    xparser.compose(ba, resource);
    ba.close();
    byte[] srcX = ba.toByteArray();
    ba = new ByteArrayOutputStream();
    jparser.compose(ba, resource);
    ba.close();
    byte[] srcJ = ba.toByteArray();
    String rn = resource.getResourceType().toString();
    if (extraType != null)
        rn = rn + extraType;
    zipX.addBytes(resource.getId() + ".xml", srcX, false);
    zipJ.addBytes(resource.getId() + ".json", srcJ, false);
    if (!zipsX.containsKey(rn)) {
        zipsX.put(rn, new ZipGenerator(Utilities.path(destFolder, "xml/type", rn + ".xml.zip")));
        zipsJ.put(rn, new ZipGenerator(Utilities.path(destFolder, "json/type", rn + ".json.zip")));
        stats.put(rn, new Stats());
    }
    zipsJ.get(rn).addBytes(resource.getId() + ".json", srcJ, false);
    zipsX.get(rn).addBytes(resource.getId() + ".xml", srcX, false);
    Stats ss = stats.get(rn);
    ss.setInstances(ss.getInstances() + 1);
    String profile = resource.getUserString("profile");
    validate(srcX, profile, resource, ss);
}
Also used : NarrativeGenerator(org.hl7.fhir.dstu3.utils.NarrativeGenerator) XmlParser(org.hl7.fhir.dstu3.formats.XmlParser) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ZipGenerator(org.hl7.fhir.utilities.ZipGenerator) JsonParser(org.hl7.fhir.dstu3.formats.JsonParser)

Example 29 with JsonParser

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

the class BatchLoader method main.

public static void main(String[] args) throws IOException, Exception {
    if (args.length < 4) {
        System.out.println("Batch uploader takes 4 parameters in order: server base url, file/folder to upload, xml/json, and batch size");
    } else {
        String server = args[0];
        String file = args[1];
        // args[2].equals("json") ? new JsonParser() : new XmlParser();
        IParser p = new JsonParser();
        int size = Integer.parseInt(args[3]);
        size = 500;
        if (file.endsWith(".xml")) {
            throw new FHIRException("Unimplemented file type " + file);
        } else if (file.endsWith(".json")) {
            throw new FHIRException("Unimplemented file type " + file);
        } else if (file.endsWith(".zip")) {
            LoadZipFile(server, file, p, size, 0, -1);
        } else if (new File(file).isDirectory()) {
            LoadDirectory(server, file, p, size);
        } else
            throw new FHIRException("Unknown file type " + file);
    }
}
Also used : FHIRException(org.hl7.fhir.exceptions.FHIRException) File(java.io.File) IParser(org.hl7.fhir.dstu2.formats.IParser) JsonParser(org.hl7.fhir.dstu2.formats.JsonParser)

Example 30 with JsonParser

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

the class R2016MayToR5Loader method loadResource.

@Override
public org.hl7.fhir.r5.model.Resource loadResource(InputStream stream, boolean isJson) throws FHIRException, IOException {
    Resource r2016may = null;
    if (isJson)
        r2016may = new JsonParser().parse(stream);
    else
        r2016may = new XmlParser().parse(stream);
    org.hl7.fhir.r5.model.Resource r5 = VersionConvertorFactory_14_50.convertResource(r2016may);
    setPath(r5);
    if (!advisor.getCslist().isEmpty()) {
        throw new FHIRException("Error: Cannot have included code systems");
    }
    if (killPrimitives) {
        throw new FHIRException("Cannot kill primitives when using deferred loading");
    }
    if (patchUrls) {
        if (r5 instanceof StructureDefinition) {
            StructureDefinition sd = (StructureDefinition) r5;
            sd.setUrl(sd.getUrl().replace(URL_BASE, URL_R4));
            sd.addExtension().setUrl(URL_ELEMENT_DEF_NAMESPACE).setValue(new UriType(URL_BASE));
            for (ElementDefinition ed : sd.getSnapshot().getElement()) patchUrl(ed);
            for (ElementDefinition ed : sd.getDifferential().getElement()) patchUrl(ed);
        }
    }
    return r5;
}
Also used : XmlParser(org.hl7.fhir.dstu2016may.formats.XmlParser) Resource(org.hl7.fhir.dstu2016may.model.Resource) org.hl7.fhir.r5.model(org.hl7.fhir.r5.model) FHIRException(org.hl7.fhir.exceptions.FHIRException) JsonParser(org.hl7.fhir.dstu2016may.formats.JsonParser)

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