Search in sources :

Example 81 with XmlParser

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

the class NarrativeGeneratorTests method process.

private void process(String path) throws FileNotFoundException, IOException, XmlPullParserException, EOperationOutcome, FHIRException {
    XmlParser p = new XmlParser();
    DomainResource r = (DomainResource) p.parse(new FileInputStream(path));
    gen.generate(r);
    FileOutputStream s = new FileOutputStream(Utilities.path("[tmp]", "gen.xml"));
    new XmlParser().compose(s, r, true);
    s.close();
}
Also used : XmlParser(org.hl7.fhir.dstu3.formats.XmlParser) DomainResource(org.hl7.fhir.dstu3.model.DomainResource) FileOutputStream(java.io.FileOutputStream) FileInputStream(java.io.FileInputStream)

Example 82 with XmlParser

use of org.hl7.fhir.r4b.formats.XmlParser 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 83 with XmlParser

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

Example 84 with XmlParser

use of org.hl7.fhir.r4b.formats.XmlParser 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.r4.formats.XmlParser) FileOutputStream(java.io.FileOutputStream) Resource(org.hl7.fhir.r4.model.Resource) FileInputStream(java.io.FileInputStream) CSFileInputStream(org.hl7.fhir.utilities.CSFileInputStream) JsonParser(org.hl7.fhir.r4.formats.JsonParser)

Example 85 with XmlParser

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

the class ToolsHelper method executeFragments.

public void executeFragments(String[] args) throws IOException {
    try {
        File source = new CSFile(args[1]);
        if (!source.exists())
            throw new FHIRException("Source File \"" + source.getAbsolutePath() + "\" not found");
        XmlPullParser xpp = loadXml(new FileInputStream(source));
        nextNoWhitespace(xpp);
        if (!xpp.getName().equals("tests"))
            throw new FHIRFormatError("Unable to parse file - starts with " + xpp.getName());
        xpp.next();
        nextNoWhitespace(xpp);
        StringBuilder s = new StringBuilder();
        s.append("<results>\r\n");
        int fail = 0;
        while (xpp.getEventType() == XmlPullParser.START_TAG && xpp.getName().equals("test")) {
            String id = xpp.getAttributeValue(null, "id");
            String type = xpp.getAttributeValue(null, "type");
            // test
            xpp.next();
            nextNoWhitespace(xpp);
            // pre
            xpp.next();
            nextNoWhitespace(xpp);
            XmlParser p = new XmlParser();
            try {
                p.parseFragment(xpp, type);
                s.append("<result id=\"" + id + "\" outcome=\"ok\"/>\r\n");
                nextNoWhitespace(xpp);
            } catch (Exception e) {
                s.append("<result id=\"" + id + "\" outcome=\"error\" msg=\"" + Utilities.escapeXml(e.getMessage()) + "\"/>\r\n");
                fail++;
            }
            while (xpp.getEventType() != XmlPullParser.END_TAG || !xpp.getName().equals("pre")) xpp.next();
            xpp.next();
            nextNoWhitespace(xpp);
            xpp.next();
            nextNoWhitespace(xpp);
        }
        s.append("</results>\r\n");
        TextFile.stringToFile(s.toString(), args[2]);
    } catch (Exception e) {
        e.printStackTrace();
        TextFile.stringToFile(e.getMessage(), args[2]);
    }
}
Also used : XmlParser(org.hl7.fhir.r4.formats.XmlParser) XmlPullParser(org.xmlpull.v1.XmlPullParser) FHIRFormatError(org.hl7.fhir.exceptions.FHIRFormatError) 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) NotImplementedException(org.apache.commons.lang3.NotImplementedException) IOException(java.io.IOException) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) FHIRException(org.hl7.fhir.exceptions.FHIRException)

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