use of org.hl7.fhir.r5.elementmodel.JsonParser 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);
} catch (Exception e) {
e.printStackTrace();
throw new FHIRException("Error Processing " + n + ".xml: " + e.getMessage(), e);
}
}
}
use of org.hl7.fhir.r5.elementmodel.JsonParser 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();
}
use of org.hl7.fhir.r5.elementmodel.JsonParser 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");
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();
return TextFile.fileToString(destt.getAbsolutePath());
}
use of org.hl7.fhir.r5.elementmodel.JsonParser in project org.hl7.fhir.core by hapifhir.
the class BaseWorkerContext method determineCacheId.
private String determineCacheId(ValueSet vs, boolean heirarchical) throws Exception {
// just the content logical definition is hashed
ValueSet vsid = new ValueSet();
vsid.setCompose(vs.getCompose());
JsonParser parser = new JsonParser();
parser.setOutputStyle(OutputStyle.NORMAL);
ByteArrayOutputStream b = new ByteArrayOutputStream();
parser.compose(b, vsid);
b.close();
String s = new String(b.toByteArray(), Constants.CHARSET_UTF8);
// any code systems we can find, we add these too.
for (ConceptSetComponent inc : vs.getCompose().getInclude()) {
CodeSystem cs = fetchCodeSystem(inc.getSystem());
if (cs != null) {
String css = cacheValue(cs);
s = s + css;
}
}
s = s + "-" + Boolean.toString(heirarchical);
String r = Integer.toString(s.hashCode());
// TextFile.stringToFile(s, Utilities.path(cache, r+".id.json"));
return r;
}
use of org.hl7.fhir.r5.elementmodel.JsonParser in project org.hl7.fhir.core by hapifhir.
the class BaseWorkerContext method loadFromCache.
private ValueSetExpansionOutcome loadFromCache(ValueSet vs, String cacheFn) throws FileNotFoundException, Exception {
JsonParser parser = new JsonParser();
Resource r = parser.parse(new FileInputStream(cacheFn));
if (r instanceof OperationOutcome) {
return new ValueSetExpansionOutcome(((OperationOutcome) r).getIssue().get(0).getDetails().getText(), TerminologyServiceErrorClass.UNKNOWN);
} else {
vs.setExpansion(((ValueSet) r).getExpansion());
return new ValueSetExpansionOutcome(vs);
}
}
Aggregations