Search in sources :

Example 56 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.dstu3.formats.XmlParser) ByteArrayInputStream(java.io.ByteArrayInputStream) FileOutputStream(java.io.FileOutputStream) Resource(org.hl7.fhir.dstu3.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.dstu3.formats.JsonParser)

Example 57 with JsonParser

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

the class ToolsHelper method testRoundTrip.

public void testRoundTrip(String rootDir, String tmpDir, Collection<String> names) throws Throwable {
    try {
        System.err.println("Round trip from " + rootDir + " to " + tmpDir + ":" + Integer.toString(names.size()) + " files");
        for (String n : names) {
            System.err.print("  " + n);
            String source = rootDir + n + ".xml";
            // String tmpJson = tmpDir + n + ".json";
            String tmp = tmpDir + n.replace(File.separator, "-") + ".tmp";
            String dest = tmpDir + n.replace(File.separator, "-") + ".java.xml";
            FileInputStream in = new FileInputStream(source);
            XmlParser xp = new XmlParser();
            Resource r = xp.parse(in);
            System.err.print(".");
            JsonParser jp = new JsonParser();
            FileOutputStream out = new FileOutputStream(tmp);
            jp.setOutputStyle(OutputStyle.PRETTY);
            jp.compose(out, r);
            out.close();
            r = null;
            System.err.print(".");
            in = new FileInputStream(tmp);
            System.err.print(",");
            r = jp.parse(in);
            System.err.print(".");
            out = new FileOutputStream(dest);
            new XmlParser().compose(out, r, true);
            System.err.println("!");
            out.close();
            r = null;
            System.gc();
        }
    } catch (Throwable e) {
        System.err.println("Error: " + e.getMessage());
        throw e;
    }
}
Also used : XmlParser(org.hl7.fhir.dstu3.formats.XmlParser) FileOutputStream(java.io.FileOutputStream) Resource(org.hl7.fhir.dstu3.model.Resource) FileInputStream(java.io.FileInputStream) CSFileInputStream(org.hl7.fhir.utilities.CSFileInputStream) JsonParser(org.hl7.fhir.dstu3.formats.JsonParser)

Example 58 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");
    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.dstu3.formats.XmlParser) FileOutputStream(java.io.FileOutputStream) Resource(org.hl7.fhir.dstu3.model.Resource) CSFile(org.hl7.fhir.utilities.CSFile) RdfParserBase(org.hl7.fhir.dstu3.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.dstu3.formats.JsonParser) RdfParser(org.hl7.fhir.dstu3.formats.RdfParser)

Example 59 with JsonParser

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

the class ResourceTest method test.

public Resource test() throws FHIRFormatError, FileNotFoundException, IOException {
    IParser p;
    if (isJson())
        p = new JsonParser();
    else
        p = new XmlParser(false);
    Resource rf = p.parse(new FileInputStream(source));
    FileOutputStream out = new FileOutputStream(source.getAbsoluteFile() + ".out.json");
    JsonParser json1 = new JsonParser();
    json1.setOutputStyle(OutputStyle.PRETTY);
    json1.compose(out, rf);
    out.close();
    JsonParser json = new JsonParser();
    rf = json.parse(new FileInputStream(source.getAbsoluteFile() + ".out.json"));
    out = new FileOutputStream(source.getAbsoluteFile() + ".out.xml");
    XmlParser atom = new XmlParser();
    atom.setOutputStyle(OutputStyle.PRETTY);
    atom.compose(out, rf, true);
    out.close();
    return rf;
}
Also used : XmlParser(org.hl7.fhir.dstu3.formats.XmlParser) FileOutputStream(java.io.FileOutputStream) Resource(org.hl7.fhir.dstu3.model.Resource) FileInputStream(java.io.FileInputStream) IParser(org.hl7.fhir.dstu3.formats.IParser) JsonParser(org.hl7.fhir.dstu3.formats.JsonParser)

Example 60 with JsonParser

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

the class ByteUtils method encodeFormSubmission.

public static byte[] encodeFormSubmission(Map<String, String> parameters, String resourceName, Resource resource, String boundary) throws IOException {
    ByteArrayOutputStream b = new ByteArrayOutputStream();
    OutputStreamWriter w = new OutputStreamWriter(b, StandardCharsets.UTF_8);
    for (String name : parameters.keySet()) {
        w.write("--");
        w.write(boundary);
        w.write("\r\nContent-Disposition: form-data; name=\"" + name + "\"\r\n\r\n");
        w.write(parameters.get(name) + "\r\n");
    }
    w.write("--");
    w.write(boundary);
    w.write("\r\nContent-Disposition: form-data; name=\"" + resourceName + "\"\r\n\r\n");
    w.close();
    JsonParser json = new JsonParser();
    json.setOutputStyle(IParser.OutputStyle.NORMAL);
    json.compose(b, resource);
    b.close();
    w = new OutputStreamWriter(b, StandardCharsets.UTF_8);
    w.write("\r\n--");
    w.write(boundary);
    w.write("--");
    w.close();
    return b.toByteArray();
}
Also used : OutputStreamWriter(java.io.OutputStreamWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) 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