Search in sources :

Example 41 with JsonParser

use of org.hl7.fhir.r4b.formats.JsonParser 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);
        } catch (Exception e) {
            e.printStackTrace();
            throw new FHIRException("Error Processing " + n + ".xml: " + e.getMessage(), e);
        }
    }
}
Also used : XmlParser(org.hl7.fhir.dstu2.formats.XmlParser) FileOutputStream(java.io.FileOutputStream) Resource(org.hl7.fhir.dstu2.model.Resource) 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.dstu2.formats.JsonParser)

Example 42 with JsonParser

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

Example 43 with JsonParser

use of org.hl7.fhir.r4b.formats.JsonParser 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");
    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();
    return TextFile.fileToString(destt.getAbsolutePath());
}
Also used : XmlParser(org.hl7.fhir.dstu2.formats.XmlParser) FileOutputStream(java.io.FileOutputStream) Resource(org.hl7.fhir.dstu2.model.Resource) CSFile(org.hl7.fhir.utilities.CSFile) 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.dstu2.formats.JsonParser)

Example 44 with JsonParser

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

the class BaseWorkerContext method determineCacheId.

private String determineCacheId(ValueSet vs, boolean heirarchical) throws Exception {
    // just the content logical definition is hashed
    ValueSet vsid = new ValueSet();
    vsid.setCompose(vs.getCompose());
    JsonParser parser = new JsonParser();
    parser.setOutputStyle(OutputStyle.NORMAL);
    ByteArrayOutputStream b = new ByteArrayOutputStream();
    parser.compose(b, vsid);
    b.close();
    String s = new String(b.toByteArray(), Constants.CHARSET_UTF8);
    // any code systems we can find, we add these too.
    for (ConceptSetComponent inc : vs.getCompose().getInclude()) {
        CodeSystem cs = fetchCodeSystem(inc.getSystem());
        if (cs != null) {
            String css = cacheValue(cs);
            s = s + css;
        }
    }
    s = s + "-" + Boolean.toString(heirarchical);
    String r = Integer.toString(s.hashCode());
    // TextFile.stringToFile(s, Utilities.path(cache, r+".id.json"));
    return r;
}
Also used : ConceptSetComponent(org.hl7.fhir.dstu3.model.ValueSet.ConceptSetComponent) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ValueSet(org.hl7.fhir.dstu3.model.ValueSet) CodeSystem(org.hl7.fhir.dstu3.model.CodeSystem) JsonParser(org.hl7.fhir.dstu3.formats.JsonParser)

Example 45 with JsonParser

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

the class BaseWorkerContext method loadFromCache.

private ValueSetExpansionOutcome loadFromCache(ValueSet vs, String cacheFn) throws FileNotFoundException, Exception {
    JsonParser parser = new JsonParser();
    Resource r = parser.parse(new FileInputStream(cacheFn));
    if (r instanceof OperationOutcome) {
        return new ValueSetExpansionOutcome(((OperationOutcome) r).getIssue().get(0).getDetails().getText(), TerminologyServiceErrorClass.UNKNOWN);
    } else {
        vs.setExpansion(((ValueSet) r).getExpansion());
        return new ValueSetExpansionOutcome(vs);
    }
}
Also used : OperationOutcome(org.hl7.fhir.dstu3.model.OperationOutcome) Resource(org.hl7.fhir.dstu3.model.Resource) ValueSetExpansionOutcome(org.hl7.fhir.dstu3.terminologies.ValueSetExpander.ValueSetExpansionOutcome) FileInputStream(java.io.FileInputStream) JsonParser(org.hl7.fhir.dstu3.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