Search in sources :

Example 61 with XmlParser

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

the class NarrativeGeneratorTests method process.

private void process(String path) throws IOException, 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.dstu2.formats.XmlParser) DomainResource(org.hl7.fhir.dstu2.model.DomainResource) FileOutputStream(java.io.FileOutputStream) FileInputStream(java.io.FileInputStream)

Example 62 with XmlParser

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

the class ToolsHelper method executeCanonicalXml.

public void executeCanonicalXml(String[] args) throws FHIRException, IOException {
    FileInputStream in;
    File source = new CSFile(args[1]);
    File dest = new CSFile(args[2]);
    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);
    XmlParser cxml = new XmlParser();
    cxml.setOutputStyle(OutputStyle.NORMAL);
    cxml.compose(new FileOutputStream(dest), rf);
}
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)

Example 63 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.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 64 with XmlParser

use of org.hl7.fhir.r4.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.dstu2016may.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)

Example 65 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.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)

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