Search in sources :

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

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

Example 48 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);
            // 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.dstu2016may.formats.XmlParser) FileOutputStream(java.io.FileOutputStream) Resource(org.hl7.fhir.dstu2016may.model.Resource) RdfParserBase(org.hl7.fhir.dstu2016may.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.dstu2016may.formats.JsonParser) RdfParser(org.hl7.fhir.dstu2016may.formats.RdfParser)

Example 49 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.dstu2016may.formats.XmlParser) FileOutputStream(java.io.FileOutputStream) Resource(org.hl7.fhir.dstu2016may.model.Resource) FileInputStream(java.io.FileInputStream) CSFileInputStream(org.hl7.fhir.utilities.CSFileInputStream) JsonParser(org.hl7.fhir.dstu2016may.formats.JsonParser)

Example 50 with JsonParser

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

the class JavaCoreGenerator method generate.

private void generate(String version, String src, String dest) throws Exception {
    long start = System.currentTimeMillis();
    Date date = new Date();
    String ap = Utilities.path(src);
    System.out.println("Load Configuration from " + ap);
    Configuration config = new Configuration(ap);
    String pid = VersionUtilities.isR4BVer(version) ? "r4b" : "r5";
    String jid = VersionUtilities.isR4BVer(version) ? "r4b" : "r5";
    FilesystemPackageCacheManager pcm = new FilesystemPackageCacheManager(true, ToolsVersion.TOOLS_VERSION);
    System.out.println("Cache: " + pcm.getFolder());
    System.out.println("Load hl7.fhir." + pid + ".core");
    NpmPackage npm = pcm.loadPackage("hl7.fhir." + pid + ".core", version);
    Definitions master = VersionUtilities.isR4BVer(version) ? DefinitionsLoaderR4B.load(npm) : DefinitionsLoaderR5.load(npm);
    master.fix();
    markValueSets(master, config);
    System.out.println("Load hl7.fhir." + pid + ".expansions");
    Definitions expansions = DefinitionsLoaderR5.load(pcm.loadPackage("hl7.fhir." + pid + ".expansions", version));
    System.out.println("Process Expansions");
    updateExpansions(master, expansions);
    System.out.println("Generate Model");
    System.out.println(" .. Constants");
    JavaConstantsGenerator cgen = new JavaConstantsGenerator(new FileOutputStream(Utilities.path(dest, "src", "main", "java", "org", "hl7", "fhir", jid, "model", "Constants.java")), master, config, date, npm.version(), jid);
    cgen.generate();
    cgen.close();
    System.out.println(" .. Enumerations");
    JavaEnumerationsGenerator egen = new JavaEnumerationsGenerator(new FileOutputStream(Utilities.path(dest, "src", "main", "java", "org", "hl7", "fhir", jid, "model", "Enumerations.java")), master, config, date, npm.version(), jid);
    egen.generate();
    egen.close();
    JavaFactoryGenerator fgen = new JavaFactoryGenerator(new FileOutputStream(Utilities.path(dest, "src", "main", "java", "org", "hl7", "fhir", jid, "model", "ResourceFactory.java")), master, config, date, npm.version(), jid);
    JavaTypeGenerator tgen = new JavaTypeGenerator(new FileOutputStream(Utilities.path(dest, "src", "main", "java", "org", "hl7", "fhir", jid, "model", "ResourceType.java")), master, config, date, npm.version(), jid);
    JavaParserJsonGenerator jgen = new JavaParserJsonGenerator(new FileOutputStream(Utilities.path(dest, "src", "main", "java", "org", "hl7", "fhir", jid, "formats", "JsonParser.java")), master, config, date, npm.version(), jid);
    JavaParserXmlGenerator xgen = new JavaParserXmlGenerator(new FileOutputStream(Utilities.path(dest, "src", "main", "java", "org", "hl7", "fhir", jid, "formats", "XmlParser.java")), master, config, date, npm.version(), jid);
    JavaParserRdfGenerator rgen = new JavaParserRdfGenerator(new FileOutputStream(Utilities.path(dest, "src", "main", "java", "org", "hl7", "fhir", jid, "formats", "RdfParser.java")), master, config, date, npm.version(), jid);
    if (VersionUtilities.isR4BVer(version)) {
        StructureDefinition sd = master.getStructures().get("http://hl7.org/fhir/StructureDefinition/Element");
        genClass(version, dest, date, config, jid, npm, master, jgen, xgen, rgen, sd);
    }
    for (StructureDefinition sd : master.getStructures().getList()) {
        if (sd.getDerivation() == TypeDerivationRule.SPECIALIZATION && sd.getKind() == StructureDefinitionKind.COMPLEXTYPE) {
            if (!Utilities.existsInList(sd.getName(), "Base", "PrimitiveType") && !sd.getName().contains(".") && sd.getAbstract()) {
                genClass(version, dest, date, config, jid, npm, master, jgen, xgen, rgen, sd);
            }
        }
    }
    for (StructureDefinition sd : master.getStructures().getList()) {
        if (sd.getDerivation() == TypeDerivationRule.SPECIALIZATION && sd.getKind() == StructureDefinitionKind.COMPLEXTYPE) {
            if (!Utilities.existsInList(sd.getName(), "Base", "PrimitiveType") && !sd.getName().contains(".") && !sd.getAbstract()) {
                genClass(version, dest, date, config, jid, npm, master, jgen, xgen, rgen, sd);
            }
        }
    }
    if (VersionUtilities.isR4BVer(version)) {
        StructureDefinition sd = master.getStructures().get("http://hl7.org/fhir/StructureDefinition/Resource");
        genClass(version, dest, date, config, jid, npm, master, jgen, xgen, rgen, sd);
    }
    for (StructureDefinition sd : master.getStructures().getList()) {
        if (sd.getDerivation() == TypeDerivationRule.SPECIALIZATION && sd.getKind() == StructureDefinitionKind.RESOURCE) {
            if (!Utilities.existsInList(sd.getName(), "Base", "PrimitiveType") && !sd.getName().contains(".") && sd.getAbstract()) {
                genClass(version, dest, date, config, jid, npm, master, jgen, xgen, rgen, sd);
            }
        }
    }
    for (StructureDefinition sd : master.getStructures().getList()) {
        if (sd.getDerivation() == TypeDerivationRule.SPECIALIZATION && sd.getKind() == StructureDefinitionKind.RESOURCE) {
            if (!Utilities.existsInList(sd.getName(), "Base", "PrimitiveType") && !sd.getName().contains(".") && !sd.getAbstract()) {
                genClass(version, dest, date, config, jid, npm, master, jgen, xgen, rgen, sd);
            }
        }
    }
    System.out.println(" .. Factory");
    fgen.generate();
    fgen.close();
    System.out.println(" .. Types");
    tgen.generate();
    tgen.close();
    System.out.println(" .. JsonParser");
    jgen.generate();
    jgen.close();
    System.out.println(" .. XmlParser");
    xgen.generate();
    xgen.close();
    System.out.println(" .. RdfParser");
    rgen.generate();
    rgen.close();
    System.out.println("Done (" + Long.toString(System.currentTimeMillis() - start) + "ms)");
}
Also used : JavaEnumerationsGenerator(org.hl7.fhir.core.generator.codegen.JavaEnumerationsGenerator) FilesystemPackageCacheManager(org.hl7.fhir.utilities.npm.FilesystemPackageCacheManager) JavaTypeGenerator(org.hl7.fhir.core.generator.codegen.JavaTypeGenerator) Configuration(org.hl7.fhir.core.generator.codegen.Configuration) JavaParserRdfGenerator(org.hl7.fhir.core.generator.codegen.JavaParserRdfGenerator) JavaParserJsonGenerator(org.hl7.fhir.core.generator.codegen.JavaParserJsonGenerator) JavaParserXmlGenerator(org.hl7.fhir.core.generator.codegen.JavaParserXmlGenerator) Date(java.util.Date) StructureDefinition(org.hl7.fhir.r5.model.StructureDefinition) NpmPackage(org.hl7.fhir.utilities.npm.NpmPackage) JavaFactoryGenerator(org.hl7.fhir.core.generator.codegen.JavaFactoryGenerator) FileOutputStream(java.io.FileOutputStream) JavaConstantsGenerator(org.hl7.fhir.core.generator.codegen.JavaConstantsGenerator)

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