Search in sources :

Example 91 with XmlParser

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

the class IntegrityChecker method checkExamplesXml.

private void checkExamplesXml(String dst) throws FileNotFoundException, IOException {
    Map<String, byte[]> files = loadZip(new FileInputStream(Utilities.path(dst, "examples.zip")));
    for (Entry<String, byte[]> t : files.entrySet()) {
        try {
            new XmlParser().parse(t.getValue());
            System.out.print(".");
        } catch (Exception e) {
            System.out.println("");
            System.out.println("Error parsing " + t.getKey() + ": " + e.getMessage());
        }
    }
}
Also used : XmlParser(org.hl7.fhir.r4b.formats.XmlParser) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

Example 92 with XmlParser

use of org.hl7.fhir.r4.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 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.r5.formats.XmlParser) DataFormatException(ca.uhn.fhir.parser.DataFormatException) BundleEntryComponent(org.hl7.fhir.r5.model.Bundle.BundleEntryComponent) FHIRFormatError(org.hl7.fhir.exceptions.FHIRFormatError) 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 93 with XmlParser

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

Example 94 with XmlParser

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

the class ToolsHelper method executeJson.

public String executeJson(String[] args) throws IOException, FHIRException {
    FileInputStream in;
    File source = new CSFile(args[1]);
    File dest = new CSFile(args[2]);
    File destc = new CSFile(Utilities.changeFileExt(args[2], ".canonical.json"));
    File destt = new CSFile(args[2] + ".tmp");
    File destr = new CSFile(Utilities.changeFileExt(args[2], ".ttl"));
    if (!source.exists())
        throw new FHIRException("Source File \"" + source.getAbsolutePath() + "\" not found");
    in = new CSFileInputStream(source);
    XmlParser p = new XmlParser();
    Resource rf = p.parse(in);
    JsonParser json = new JsonParser();
    json.setOutputStyle(OutputStyle.PRETTY);
    FileOutputStream s = new FileOutputStream(dest);
    json.compose(s, rf);
    s.close();
    json.setOutputStyle(OutputStyle.CANONICAL);
    s = new FileOutputStream(destc);
    json.compose(s, rf);
    s.close();
    json.setSuppressXhtml("Snipped for Brevity");
    json.setOutputStyle(OutputStyle.PRETTY);
    s = new FileOutputStream(destt);
    json.compose(s, rf);
    s.close();
    RdfParserBase rdf = new RdfParser();
    s = new FileOutputStream(destr);
    rdf.compose(s, rf);
    s.close();
    return TextFile.fileToString(destt.getAbsolutePath());
}
Also used : XmlParser(org.hl7.fhir.r5.formats.XmlParser) FileOutputStream(java.io.FileOutputStream) Resource(org.hl7.fhir.r5.model.Resource) CSFile(org.hl7.fhir.utilities.CSFile) RdfParserBase(org.hl7.fhir.r5.formats.RdfParserBase) 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.r5.formats.JsonParser) RdfParser(org.hl7.fhir.r5.formats.RdfParser)

Example 95 with XmlParser

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

the class ToolsHelper method processExamples.

public void processExamples(String rootDir, Collection<String> list) throws FHIRException {
    for (String n : list) {
        try {
            String filename = rootDir + n + ".xml";
            // 1. produce canonical XML
            CSFileInputStream source = new CSFileInputStream(filename);
            FileOutputStream dest = new FileOutputStream(Utilities.changeFileExt(filename, ".canonical.xml"));
            XmlParser p = new XmlParser();
            Resource r = p.parse(source);
            XmlParser cxml = new XmlParser();
            cxml.setOutputStyle(OutputStyle.CANONICAL);
            cxml.compose(dest, r);
            // 2. produce JSON
            source = new CSFileInputStream(filename);
            dest = new FileOutputStream(Utilities.changeFileExt(filename, ".json"));
            r = p.parse(source);
            JsonParser json = new JsonParser();
            json.setOutputStyle(OutputStyle.PRETTY);
            json.compose(dest, r);
            json = new JsonParser();
            json.setOutputStyle(OutputStyle.CANONICAL);
            dest = new FileOutputStream(Utilities.changeFileExt(filename, ".canonical.json"));
            json.compose(dest, r);
            // 2. produce JSON
            dest = new FileOutputStream(Utilities.changeFileExt(filename, ".ttl"));
            RdfParserBase rdf = new RdfParser();
            rdf.compose(dest, r);
        } catch (Exception e) {
            e.printStackTrace();
            throw new FHIRException("Error Processing " + n + ".xml: " + e.getMessage(), e);
        }
    }
}
Also used : XmlParser(org.hl7.fhir.r5.formats.XmlParser) FileOutputStream(java.io.FileOutputStream) Resource(org.hl7.fhir.r5.model.Resource) RdfParserBase(org.hl7.fhir.r5.formats.RdfParserBase) FHIRException(org.hl7.fhir.exceptions.FHIRException) CSFileInputStream(org.hl7.fhir.utilities.CSFileInputStream) NotImplementedException(org.apache.commons.lang3.NotImplementedException) IOException(java.io.IOException) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) FHIRException(org.hl7.fhir.exceptions.FHIRException) JsonParser(org.hl7.fhir.r5.formats.JsonParser) RdfParser(org.hl7.fhir.r5.formats.RdfParser)

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