Search in sources :

Example 51 with XmlParser

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

the class R3ToR5Loader method loadBundle.

@Override
public Bundle loadBundle(InputStream stream, boolean isJson) throws FHIRException, IOException {
    Resource r3 = null;
    if (isJson)
        r3 = new JsonParser().parse(stream);
    else
        r3 = new XmlParser().parse(stream);
    org.hl7.fhir.r5.model.Resource r5 = VersionConvertorFactory_30_50.convertResource(r3, advisor);
    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 : advisor.getCslist()) {
        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_DSTU3));
                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.dstu3.formats.XmlParser) Resource(org.hl7.fhir.dstu3.model.Resource) ArrayList(java.util.ArrayList) BundleEntryComponent(org.hl7.fhir.r5.model.Bundle.BundleEntryComponent) org.hl7.fhir.r5.model(org.hl7.fhir.r5.model) JsonParser(org.hl7.fhir.dstu3.formats.JsonParser)

Example 52 with XmlParser

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

the class GenValueSetExpansionConvertor method main.

public static void main(String[] args) throws FHIRFormatError, IOException {
    String src = args[0];
    String tgt = args[1];
    Bundle bundle = (Bundle) new XmlParser().parse(new FileInputStream(src));
    for (BundleEntryComponent be : bundle.getEntry()) {
        Resource res = be.getResource();
        if (res != null) {
            String id = res.getId();
            if (Utilities.noString(id))
                id = tail(((ValueSet) res).getUrl());
            String dst = Utilities.path(tgt, res.fhirType() + "-" + id + ".json");
            System.out.println(dst);
            new JsonParser().setOutputStyle(OutputStyle.NORMAL).compose(new FileOutputStream(dst), res);
        }
    }
}
Also used : XmlParser(org.hl7.fhir.dstu2.formats.XmlParser) BundleEntryComponent(org.hl7.fhir.dstu2.model.Bundle.BundleEntryComponent) Bundle(org.hl7.fhir.dstu2.model.Bundle) FileOutputStream(java.io.FileOutputStream) Resource(org.hl7.fhir.dstu2.model.Resource) FileInputStream(java.io.FileInputStream) JsonParser(org.hl7.fhir.dstu2.formats.JsonParser)

Example 53 with XmlParser

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

the class R2ToR3Loader method loadBundle.

@Override
public Bundle loadBundle(InputStream stream, boolean isJson) throws FHIRException, IOException {
    Resource r2;
    if (isJson)
        r2 = new JsonParser().parse(stream);
    else
        r2 = new XmlParser().parse(stream);
    org.hl7.fhir.dstu3.model.Resource r3 = VersionConvertorFactory_10_30.convertResource(r2, advisor_10_30);
    Bundle b;
    if (r3 instanceof Bundle) {
        b = (Bundle) r3;
    } else {
        b = new Bundle();
        b.setId(UUID.randomUUID().toString().toLowerCase());
        b.setType(BundleType.COLLECTION);
        b.addEntry().setResource(r3).setFullUrl(r3 instanceof MetadataResource ? ((MetadataResource) r3).getUrl() : null);
    }
    advisor_10_30.getCslist().forEach(cs -> {
        BundleEntryComponent be = b.addEntry();
        be.setFullUrl(cs.getUrl());
        be.setResource(cs);
    });
    advisor_10_30.getCslist().clear();
    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) {
        b.getEntry().stream().filter(be -> be.hasResource() && be.getResource() instanceof StructureDefinition).map(be -> (StructureDefinition) be.getResource()).forEach(sd -> {
            sd.setUrl(sd.getUrl().replace(URL_BASE, URL_DSTU2));
            sd.addExtension().setUrl(URL_ELEMENT_DEF_NAMESPACE).setValue(new UriType(URL_BASE));
        });
    }
    return b;
}
Also used : MetadataResource(org.hl7.fhir.dstu3.model.MetadataResource) StructureDefinitionKind(org.hl7.fhir.dstu3.model.StructureDefinition.StructureDefinitionKind) JsonParser(org.hl7.fhir.dstu2.formats.JsonParser) Bundle(org.hl7.fhir.dstu3.model.Bundle) VersionConvertorFactory_10_30(org.hl7.fhir.convertors.factory.VersionConvertorFactory_10_30) BundleEntryComponent(org.hl7.fhir.dstu3.model.Bundle.BundleEntryComponent) UriType(org.hl7.fhir.dstu3.model.UriType) BundleType(org.hl7.fhir.dstu3.model.Bundle.BundleType) BaseAdvisor_10_30(org.hl7.fhir.convertors.advisors.impl.BaseAdvisor_10_30) IOException(java.io.IOException) UUID(java.util.UUID) XmlParser(org.hl7.fhir.dstu2.formats.XmlParser) MetadataResource(org.hl7.fhir.dstu3.model.MetadataResource) ArrayList(java.util.ArrayList) List(java.util.List) Resource(org.hl7.fhir.dstu2.model.Resource) StructureDefinition(org.hl7.fhir.dstu3.model.StructureDefinition) FHIRException(org.hl7.fhir.exceptions.FHIRException) InputStream(java.io.InputStream) XmlParser(org.hl7.fhir.dstu2.formats.XmlParser) Bundle(org.hl7.fhir.dstu3.model.Bundle) MetadataResource(org.hl7.fhir.dstu3.model.MetadataResource) Resource(org.hl7.fhir.dstu2.model.Resource) ArrayList(java.util.ArrayList) UriType(org.hl7.fhir.dstu3.model.UriType) StructureDefinition(org.hl7.fhir.dstu3.model.StructureDefinition) BundleEntryComponent(org.hl7.fhir.dstu3.model.Bundle.BundleEntryComponent) JsonParser(org.hl7.fhir.dstu2.formats.JsonParser)

Example 54 with XmlParser

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

the class ProfileUtilitiesTests method compareXml.

private void compareXml(StructureDefinition base, StructureDefinition focus) throws FileNotFoundException, IOException {
    base.setText(null);
    focus.setText(null);
    base.setDifferential(null);
    // focus.setDifferential(null);
    String f1 = Utilities.path("c:", "temp", "base.xml");
    String f2 = Utilities.path("c:", "temp", "derived.xml");
    new XmlParser().setOutputStyle(OutputStyle.PRETTY).compose(new FileOutputStream(f1), base);
    ;
    new XmlParser().setOutputStyle(OutputStyle.PRETTY).compose(new FileOutputStream(f2), focus);
    ;
    String diff = Utilities.path(System.getenv("ProgramFiles(X86)"), "WinMerge", "WinMergeU.exe");
    List<String> command = new ArrayList<String>();
    command.add("\"" + diff + "\" \"" + f1 + "\" \"" + f2 + "\"");
    ProcessBuilder builder = new ProcessBuilder(command);
    builder.directory(new CSFile(Utilities.path("[tmp]")));
    builder.start();
}
Also used : XmlParser(org.hl7.fhir.dstu2.formats.XmlParser) FileOutputStream(java.io.FileOutputStream) ArrayList(java.util.ArrayList) CSFile(org.hl7.fhir.utilities.CSFile)

Example 55 with XmlParser

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

the class ProfileUtilitiesTests method compare.

private void compare(String fn1, String fn2) throws FHIRFormatError, FileNotFoundException, IOException, DefinitionException {
    System.out.println("Compare " + fn1 + " to " + fn2);
    System.out.println("  .. load");
    StructureDefinition left = (StructureDefinition) new XmlParser().parse(new FileInputStream(Utilities.path(root, fn1)));
    StructureDefinition right = (StructureDefinition) new XmlParser().parse(new FileInputStream(Utilities.path(root, fn2)));
    System.out.println(" .. compare");
    comp.compareProfiles(left, right);
}
Also used : XmlParser(org.hl7.fhir.dstu2.formats.XmlParser) StructureDefinition(org.hl7.fhir.dstu2.model.StructureDefinition) FileInputStream(java.io.FileInputStream)

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