Search in sources :

Example 41 with XmlParser

use of org.hl7.fhir.r4.formats.XmlParser 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 42 with XmlParser

use of org.hl7.fhir.r4.formats.XmlParser 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 43 with XmlParser

use of org.hl7.fhir.r4.formats.XmlParser in project org.hl7.fhir.core by hapifhir.

the class ADLImporter method execute.

private void execute() throws Exception {
    // load config
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);
    DocumentBuilder builder = factory.newDocumentBuilder();
    adlConfig = builder.parse(new FileInputStream(config)).getDocumentElement();
    // load ADL
    builder = factory.newDocumentBuilder();
    adl = builder.parse(new FileInputStream(source)).getDocumentElement();
    check("root", adl.getNamespaceURI(), "http://schemas.openehr.org/v1", "Wrong namespace for ADL XML");
    check("root", adl.getNodeName(), "archetype", "Wrong XML for ADL XML");
    check("root", XMLUtil.getNamedChild(adl, "adl_version").getTextContent(), "1.4", "unsupported ADL version");
    String id = XMLUtil.getFirstChild(XMLUtil.getNamedChild(adl, "archetype_id")).getTextContent().split("\\.")[1];
    // create structure definition
    StructureDefinition sd = new StructureDefinition();
    sd.setId(id);
    // populate metadata
    Element description = XMLUtil.getNamedChild(adl, "description");
    Element details = XMLUtil.getNamedChild(description, "details");
    sd.setDescription(XMLUtil.getNamedChild(details, "purpose").getTextContent());
    sd.setCopyright(XMLUtil.getNamedChild(details, "copyright").getTextContent());
    sd.setPurpose("Use:\r\n" + XMLUtil.getNamedChild(details, "use").getTextContent() + "\r\n\r\nMisuse:\r\n" + XMLUtil.getNamedChild(details, "misuse").getTextContent());
    List<Element> set = new ArrayList<Element>();
    XMLUtil.getNamedChildren(details, "keywords", set);
    for (Element e : set) sd.addKeyword().setDisplay(e.getTextContent());
    String status = XMLUtil.getNamedChild(description, "lifecycle_state").getTextContent();
    if ("CommitteeDraft".equals(status) || "AuthorDraft".equals(status))
        sd.setStatus(PublicationStatus.DRAFT);
    else
        throw new Exception("Unknown life cycle state " + XMLUtil.getNamedChild(description, "lifecycle_state").getTextContent());
    // load texts from ontology
    Element ontology = XMLUtil.getNamedChild(adl, "ontology");
    Element term_definitions = XMLUtil.getNamedChild(ontology, "term_definitions");
    set.clear();
    XMLUtil.getNamedChildren(term_definitions, "items", set);
    for (Element item : set) {
        processTextItem(item);
    }
    // load data and protocol
    Element definition = XMLUtil.getNamedChild(adl, "definition");
    NodeTreeEntry root = new NodeTreeEntry();
    root.setTypeName(XMLUtil.getNamedChild(definition, "rm_type_name").getTextContent());
    root.setAtCode(XMLUtil.getNamedChild(definition, "node_id").getTextContent());
    root.setName(generateToken(root.getAtCode(), true));
    sd.setName(root.getName());
    root.setCardinality(readCardinality("root", XMLUtil.getNamedChild(definition, "occurrences")));
    set.clear();
    XMLUtil.getNamedChildren(definition, "attributes", set);
    for (Element item : set) {
        // we're actually skipping this level - we don't care about data protocol etc.
        // XMLUtil.getNamedChild(XMLUtil.getNamedChild(item, "children"), "attributes");
        Element attributes = item;
        loadChildren(root.getAtCode(), root, attributes);
    }
    dumpChildren("", root);
    genElements(sd, root.getName(), root);
    // save
    new XmlParser().setOutputStyle(OutputStyle.PRETTY).compose(new FileOutputStream(dest), sd);
    System.out.println("done. saved as " + dest);
}
Also used : XmlParser(org.hl7.fhir.dstu3.formats.XmlParser) StructureDefinition(org.hl7.fhir.dstu3.model.StructureDefinition) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) Element(org.w3c.dom.Element) FileOutputStream(java.io.FileOutputStream) ArrayList(java.util.ArrayList) FileInputStream(java.io.FileInputStream)

Example 44 with XmlParser

use of org.hl7.fhir.r4.formats.XmlParser 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 45 with XmlParser

use of org.hl7.fhir.r4.formats.XmlParser in project org.hl7.fhir.core by hapifhir.

the class LoincToDEConvertor method saveBundle.

private void saveBundle() throws FHIRFormatError, IOException, XmlPullParserException {
    XmlParser xml = new XmlParser();
    FileOutputStream s = new FileOutputStream(dest);
    xml.compose(s, bundle, true);
    s.close();
}
Also used : XmlParser(org.hl7.fhir.dstu2.formats.XmlParser) FileOutputStream(java.io.FileOutputStream)

Aggregations

FileOutputStream (java.io.FileOutputStream)130 XmlParser (org.hl7.fhir.r5.formats.XmlParser)97 FHIRException (org.hl7.fhir.exceptions.FHIRException)84 FileInputStream (java.io.FileInputStream)77 File (java.io.File)64 IOException (java.io.IOException)59 CSFileInputStream (org.hl7.fhir.utilities.CSFileInputStream)51 CSFile (org.hl7.fhir.utilities.CSFile)48 JsonParser (org.hl7.fhir.r5.formats.JsonParser)45 ArrayList (java.util.ArrayList)35 XmlParser (org.hl7.fhir.dstu3.formats.XmlParser)35 TextFile (org.hl7.fhir.utilities.TextFile)33 XmlParser (org.hl7.fhir.r4.formats.XmlParser)31 FileNotFoundException (java.io.FileNotFoundException)27 ByteArrayOutputStream (java.io.ByteArrayOutputStream)24 Resource (org.hl7.fhir.r5.model.Resource)24 XmlParser (org.hl7.fhir.r4b.formats.XmlParser)23 StructureDefinition (org.hl7.fhir.r5.model.StructureDefinition)22 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)19 NotImplementedException (org.apache.commons.lang3.NotImplementedException)19