Search in sources :

Example 26 with CSFile

use of org.hl7.fhir.utilities.CSFile 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 27 with CSFile

use of org.hl7.fhir.utilities.CSFile 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 28 with CSFile

use of org.hl7.fhir.utilities.CSFile 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.dstu3.formats.XmlParser) FileOutputStream(java.io.FileOutputStream) Resource(org.hl7.fhir.dstu3.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 29 with CSFile

use of org.hl7.fhir.utilities.CSFile 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.dstu3.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 30 with CSFile

use of org.hl7.fhir.utilities.CSFile 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

CSFile (org.hl7.fhir.utilities.CSFile)86 File (java.io.File)45 TextFile (org.hl7.fhir.utilities.TextFile)42 CSFileInputStream (org.hl7.fhir.utilities.CSFileInputStream)39 FHIRException (org.hl7.fhir.exceptions.FHIRException)37 FileOutputStream (java.io.FileOutputStream)36 ArrayList (java.util.ArrayList)29 FileInputStream (java.io.FileInputStream)27 XmlParser (org.hl7.fhir.r5.formats.XmlParser)21 IOException (java.io.IOException)16 IniFile (org.hl7.fhir.utilities.IniFile)16 JsonParser (org.hl7.fhir.r5.formats.JsonParser)13 ByteArrayOutputStream (java.io.ByteArrayOutputStream)10 FileNotFoundException (java.io.FileNotFoundException)10 DocumentBuilder (javax.xml.parsers.DocumentBuilder)9 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)9 StructureDefinition (org.hl7.fhir.r5.model.StructureDefinition)9 Document (org.w3c.dom.Document)9 ByteArrayInputStream (java.io.ByteArrayInputStream)7 NotImplementedException (org.apache.commons.lang3.NotImplementedException)7