Search in sources :

Example 86 with XmlParser

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

the class ToolsHelper method executeRoundTrip.

public void executeRoundTrip(String[] args) throws IOException, FHIRException {
    FileInputStream in;
    File source = new CSFile(args[1]);
    File dest = new CSFile(args[2]);
    if (args.length >= 4) {
        Utilities.copyFile(args[1], args[3]);
    }
    if (!source.exists())
        throw new FHIRException("Source File \"" + source.getAbsolutePath() + "\" not found");
    in = new CSFileInputStream(source);
    XmlParser p = new XmlParser();
    JsonParser parser = new JsonParser();
    JsonParser pj = parser;
    Resource rf = p.parse(in);
    ByteArrayOutputStream json = new ByteArrayOutputStream();
    parser.setOutputStyle(OutputStyle.PRETTY);
    parser.compose(json, rf);
    json.close();
    TextFile.stringToFile(new String(json.toByteArray()), Utilities.changeFileExt(dest.getAbsolutePath(), ".json"));
    rf = pj.parse(new ByteArrayInputStream(json.toByteArray()));
    FileOutputStream s = new FileOutputStream(dest);
    new XmlParser().compose(s, rf, true);
    s.close();
}
Also used : XmlParser(org.hl7.fhir.r4.formats.XmlParser) ByteArrayInputStream(java.io.ByteArrayInputStream) FileOutputStream(java.io.FileOutputStream) Resource(org.hl7.fhir.r4.model.Resource) CSFile(org.hl7.fhir.utilities.CSFile) ByteArrayOutputStream(java.io.ByteArrayOutputStream) CSFile(org.hl7.fhir.utilities.CSFile) File(java.io.File) TextFile(org.hl7.fhir.utilities.TextFile) FHIRException(org.hl7.fhir.exceptions.FHIRException) FileInputStream(java.io.FileInputStream) CSFileInputStream(org.hl7.fhir.utilities.CSFileInputStream) CSFileInputStream(org.hl7.fhir.utilities.CSFileInputStream) JsonParser(org.hl7.fhir.r4.formats.JsonParser)

Example 87 with XmlParser

use of org.hl7.fhir.r4b.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.r4.formats.XmlParser) FileOutputStream(java.io.FileOutputStream)

Example 88 with XmlParser

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

the class SimpleWorkerContext method loadFromFile.

public void loadFromFile(InputStream stream, String name, IContextResourceLoader loader, ILoadFilter filter) throws IOException, FHIRException {
    Resource f;
    try {
        if (loader != null)
            f = loader.loadBundle(stream, false);
        else {
            XmlParser xml = new XmlParser();
            f = xml.parse(stream);
        }
    } catch (DataFormatException e1) {
        throw new org.hl7.fhir.exceptions.FHIRFormatError(formatMessage(I18nConstants.ERROR_PARSING_, name, e1.getMessage()), e1);
    } catch (Exception e1) {
        throw new org.hl7.fhir.exceptions.FHIRFormatError(formatMessage(I18nConstants.ERROR_PARSING_, name, e1.getMessage()), e1);
    }
    if (f instanceof Bundle) {
        Bundle bnd = (Bundle) f;
        for (BundleEntryComponent e : bnd.getEntry()) {
            if (e.getFullUrl() == null) {
                logger.logDebugMessage(LogCategory.CONTEXT, "unidentified resource in " + name + " (no fullUrl)");
            }
            if (filter == null || filter.isOkToLoad(e.getResource())) {
                String path = loader != null ? loader.getResourcePath(e.getResource()) : null;
                if (path != null) {
                    e.getResource().setUserData("path", path);
                }
                cacheResource(e.getResource());
            }
        }
    } else if (f instanceof CanonicalResource) {
        if (filter == null || filter.isOkToLoad(f)) {
            String path = loader != null ? loader.getResourcePath(f) : null;
            if (path != null) {
                f.setUserData("path", path);
            }
            cacheResource(f);
        }
    }
}
Also used : XmlParser(org.hl7.fhir.r4b.formats.XmlParser) DataFormatException(ca.uhn.fhir.parser.DataFormatException) BundleEntryComponent(org.hl7.fhir.r4b.model.Bundle.BundleEntryComponent) Bundle(org.hl7.fhir.r4b.model.Bundle) CanonicalResource(org.hl7.fhir.r4b.model.CanonicalResource) Resource(org.hl7.fhir.r4b.model.Resource) FHIRFormatError(org.hl7.fhir.exceptions.FHIRFormatError) CanonicalResource(org.hl7.fhir.r4b.model.CanonicalResource) FileNotFoundException(java.io.FileNotFoundException) DataFormatException(ca.uhn.fhir.parser.DataFormatException) DefinitionException(org.hl7.fhir.exceptions.DefinitionException) IOException(java.io.IOException) FHIRException(org.hl7.fhir.exceptions.FHIRException)

Example 89 with XmlParser

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

the class SimpleWorkerContext method allStructures.

@Override
public List<StructureDefinition> allStructures() {
    List<StructureDefinition> result = new ArrayList<StructureDefinition>();
    Set<StructureDefinition> set = new HashSet<StructureDefinition>();
    for (StructureDefinition sd : listStructures()) {
        if (!set.contains(sd)) {
            try {
                generateSnapshot(sd);
            // new XmlParser().setOutputStyle(OutputStyle.PRETTY).compose(new FileOutputStream(Utilities.path("[tmp]", "snapshot", tail(sd.getUrl())+".xml")), sd);
            } catch (Exception e) {
                System.out.println("Unable to generate snapshot for " + tail(sd.getUrl()) + " from " + tail(sd.getBaseDefinition()) + " because " + e.getMessage());
                if (true) {
                    e.printStackTrace();
                }
            }
            result.add(sd);
            set.add(sd);
        }
    }
    return result;
}
Also used : StructureDefinition(org.hl7.fhir.r4b.model.StructureDefinition) ArrayList(java.util.ArrayList) FileNotFoundException(java.io.FileNotFoundException) DataFormatException(ca.uhn.fhir.parser.DataFormatException) DefinitionException(org.hl7.fhir.exceptions.DefinitionException) IOException(java.io.IOException) FHIRException(org.hl7.fhir.exceptions.FHIRException) HashSet(java.util.HashSet)

Example 90 with XmlParser

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

the class ByteUtils method resourceToByteArray.

public static <T extends Resource> byte[] resourceToByteArray(T resource, boolean pretty, boolean isJson) {
    ByteArrayOutputStream baos = null;
    byte[] byteArray = null;
    try {
        baos = new ByteArrayOutputStream();
        IParser parser = null;
        if (isJson) {
            parser = new JsonParser();
        } else {
            parser = new XmlParser();
        }
        parser.setOutputStyle(pretty ? IParser.OutputStyle.PRETTY : IParser.OutputStyle.NORMAL);
        parser.compose(baos, resource);
        baos.close();
        byteArray = baos.toByteArray();
        baos.close();
    } catch (Exception e) {
        try {
            baos.close();
        } catch (Exception ex) {
            throw new EFhirClientException("Error closing output stream", ex);
        }
        throw new EFhirClientException("Error converting output stream to byte array", e);
    }
    return byteArray;
}
Also used : XmlParser(org.hl7.fhir.r4b.formats.XmlParser) ByteArrayOutputStream(java.io.ByteArrayOutputStream) EFhirClientException(org.hl7.fhir.r4b.utils.client.EFhirClientException) IOException(java.io.IOException) EFhirClientException(org.hl7.fhir.r4b.utils.client.EFhirClientException) IParser(org.hl7.fhir.r4b.formats.IParser) JsonParser(org.hl7.fhir.r4b.formats.JsonParser)

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