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();
}
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);
}
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());
}
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]);
}
}
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();
}
Aggregations