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 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();
}
use of org.hl7.fhir.r4.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;
}
use of org.hl7.fhir.r4.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);
}
}
}
use of org.hl7.fhir.r4.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;
}
}
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]);
}
}
Aggregations